pxt-common-packages 9.4.7 → 9.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ npx html-minifier --removeAttributeQuotes --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --quoteCharacter "'" --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true --html5 true -o login.html login.full.html
@@ -1,4 +1,7 @@
1
1
  namespace net {
2
+ //% shim=_wifi::_readLastAccessPointCredentials
3
+ declare function _readLastAccessPointCredentials(): string[];
4
+
2
5
  const EV_ScanCompleted = 1000
3
6
 
4
7
  export class WifiController extends net.Controller {
@@ -31,7 +34,6 @@ namespace net {
31
34
  this.networks.push(ap)
32
35
  i += entrySize
33
36
  }
34
- control.dmesg(`scan completed`)
35
37
  control.raiseEvent(_wifi.eventID(), EV_ScanCompleted)
36
38
  }
37
39
 
@@ -44,6 +46,22 @@ namespace net {
44
46
  return this.networks
45
47
  }
46
48
 
49
+ public startLoginServer(hostName: string): void {
50
+ this.disconnect()
51
+ _wifi.startLoginServer(hostName);
52
+ control.onEvent(_wifi.eventID(), WifiEvent.AccessPointCredentialsAvailable, () => {
53
+ const credentials = _readLastAccessPointCredentials()
54
+ if (!!credentials && credentials.length == 2) {
55
+ const ssid = credentials[0]
56
+ const pwd = credentials[1]
57
+ net.updateAccessPoint(ssid, pwd)
58
+ console.debug(`restarting...`);
59
+ pause(100);
60
+ control.reset();
61
+ }
62
+ })
63
+ }
64
+
47
65
  public disconnectAP() {
48
66
  _wifi.disconnect()
49
67
  }
@@ -8,6 +8,8 @@
8
8
  GotIP = 2,
9
9
  //%
10
10
  Disconnected = 3,
11
+ //%
12
+ AccessPointCredentialsAvailable = 4,
11
13
  }
12
14
 
13
15
  // Auto-generated. Do not edit. Really.
@@ -0,0 +1,166 @@
1
+ #include "wifi.h"
2
+ #include "esp_netif.h"
3
+ #include "esp_wifi.h"
4
+ #include <esp_http_server.h>
5
+ #include <mdns.h>
6
+
7
+ #define TAG "http"
8
+ #define LOG(...) ESP_LOGI(TAG, __VA_ARGS__)
9
+
10
+ namespace _wifi {
11
+
12
+ static httpd_handle_t _server = NULL;
13
+ static const char* _lastApBuffer = NULL;
14
+ static wifi_config_t wifi_config;
15
+
16
+ esp_err_t login_handler(httpd_req_t *req)
17
+ {
18
+ LOG("login");
19
+ const char resp[] = "<style>html{background:#aaa}*{font-size:xx-large;font-family:monospace}form{min-height:100vh;display:flex;justify-content:center;align-items:center;flex-direction:column;gap:.5rem}</style><meta name='viewport'content='width=device-width,initial-scale=1'><form action='/add-ap'><label for='ssid'>WiFi:</label> <input name='name'id='ssid'placeholder='WiFi'required> <label for='password'>Password:</label> <input type='password'name='password'id='password'placeholder='Password'required> <input id='submit'type='submit'value='connect'></form>";
20
+ httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
21
+ return ESP_OK;
22
+ }
23
+
24
+ esp_err_t add_ap_handler(httpd_req_t *req)
25
+ {
26
+ LOG("add_ap");
27
+
28
+ /* Read URL query string length and allocate memory for length + 1,
29
+ * extra byte for null termination */
30
+ size_t buf_len = httpd_req_get_url_query_len(req) + 1;
31
+ if (buf_len > 1 && buf_len < 256) {
32
+ char* buf = (char*)malloc(buf_len);
33
+ if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
34
+ char name[64];
35
+ char password[64];
36
+ /* Get value of expected key from query string */
37
+ if ((httpd_query_key_value(buf, "name", name, sizeof(name)) == ESP_OK) &&
38
+ (httpd_query_key_value(buf, "password", password, sizeof(password)) == ESP_OK)) {
39
+ // save ap info, let TS handle it
40
+ if (NULL == _lastApBuffer) {
41
+ _lastApBuffer = buf;
42
+ pxt::raiseEvent(_wifi::eventID(), (int)_wifi::WifiEvent::AccessPointCredentialsAvailable);
43
+ return ESP_OK;
44
+ }
45
+ }
46
+ }
47
+ free(buf);
48
+ return ESP_OK;
49
+ }
50
+
51
+ httpd_resp_send_500(req);
52
+ return ESP_OK;
53
+ }
54
+
55
+ //%
56
+ RefCollection *_readLastAccessPointCredentials() {
57
+ auto res = Array_::mk();
58
+ registerGCObj(res);
59
+
60
+ char name[64];
61
+ char password[64];
62
+ /* Get value of expected key from query string */
63
+ if (NULL != _lastApBuffer &&
64
+ (httpd_query_key_value(_lastApBuffer, "name", name, sizeof(name)) == ESP_OK) &&
65
+ (httpd_query_key_value(_lastApBuffer, "password", password, sizeof(password)) == ESP_OK)) {
66
+
67
+ {
68
+ auto str = mkString(name, -1);
69
+ registerGCObj(str);
70
+ res->head.push((TValue)str);
71
+ unregisterGCObj(str);
72
+ }
73
+ {
74
+ auto str = mkString(password, -1);
75
+ registerGCObj(str);
76
+ res->head.push((TValue)str);
77
+ unregisterGCObj(str);
78
+ }
79
+ }
80
+
81
+ unregisterGCObj(res);
82
+ return res;
83
+ }
84
+
85
+ // HTTP Error (404) Handler - Redirects all requests to the root page
86
+ esp_err_t http_404_error_handler(httpd_req_t *req, httpd_err_code_t err)
87
+ {
88
+ // Set status
89
+ httpd_resp_set_status(req, "302 Temporary Redirect");
90
+ // Redirect to the "/" root directory
91
+ httpd_resp_set_hdr(req, "Location", "/");
92
+ // iOS requires content in the response to detect a captive portal, simply redirecting is not sufficient.
93
+ httpd_resp_send(req, "Redirect to the captive portal", HTTPD_RESP_USE_STRLEN);
94
+
95
+ ESP_LOGI(TAG, "Redirecting to root");
96
+ return ESP_OK;
97
+ }
98
+
99
+ /* URI handler structure for GET / */
100
+ httpd_uri_t login_get = {
101
+ .uri = "/",
102
+ .method = HTTP_GET,
103
+ .handler = login_handler,
104
+ .user_ctx = NULL
105
+ };
106
+
107
+ /* URI handler structure for POST /uri */
108
+ httpd_uri_t add_ap_get = {
109
+ .uri = "/add-ap",
110
+ .method = HTTP_GET,
111
+ .handler = add_ap_handler,
112
+ .user_ctx = NULL
113
+ };
114
+
115
+ /* Function for starting the webserver */
116
+ static void init(const char* hostName)
117
+ {
118
+ LOG("starting login server %s", hostName);
119
+ const char* ssid = hostName;
120
+
121
+ esp_netif_create_default_wifi_ap();
122
+
123
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
124
+ ESP_ERROR_CHECK(esp_wifi_init(&cfg));
125
+
126
+ memcpy(wifi_config.ap.ssid, ssid, strlen(ssid));
127
+ wifi_config.ap.ssid_len = static_cast<uint8_t>(strlen(ssid));
128
+ wifi_config.ap.channel = 11;
129
+ wifi_config.ap.max_connection = 1;
130
+ wifi_config.ap.authmode = WIFI_AUTH_OPEN;
131
+
132
+ ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
133
+ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
134
+ ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
135
+
136
+ /* Generate default configuration */
137
+ httpd_config_t config = HTTPD_DEFAULT_CONFIG();
138
+
139
+ /* Empty handle to esp_http_server */
140
+ httpd_handle_t server = NULL;
141
+
142
+ ESP_ERROR_CHECK(httpd_start(&server, &config));
143
+
144
+ httpd_register_uri_handler(server, &login_get);
145
+ httpd_register_uri_handler(server, &add_ap_get);
146
+ httpd_register_err_handler(server, HTTPD_404_NOT_FOUND, http_404_error_handler);
147
+
148
+ esp_netif_ip_info_t ip_info;
149
+ esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"), &ip_info);
150
+ LOG("SoftAP ip: " IPSTR, IP2STR(&ip_info.ip));
151
+
152
+ LOG("start mDNS service %s", hostName);
153
+ ESP_ERROR_CHECK(mdns_init());
154
+ ESP_ERROR_CHECK(mdns_hostname_set(hostName));
155
+ ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0));
156
+
157
+ _server = server;
158
+ }
159
+
160
+ void startHttpServer(const char* hostName) {
161
+ if (NULL == _server) {
162
+ init(hostName);
163
+ }
164
+ }
165
+
166
+ }
@@ -0,0 +1,37 @@
1
+ <html>
2
+ <head>
3
+ <style>
4
+ html {
5
+ background: #aaa;
6
+ }
7
+ * {
8
+ font-size: xx-large;
9
+ font-family: monospace;
10
+ }
11
+ form {
12
+ min-height: 100vh;
13
+ display: flex;
14
+ justify-content: center;
15
+ align-items: center;
16
+ flex-direction: column;
17
+ gap: 0.5rem;
18
+ }
19
+ </style>
20
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
21
+ </head>
22
+ <body>
23
+ <form action="/add-ap">
24
+ <label for="ssid">WiFi:</label>
25
+ <input type="text" name="name" id="ssid" placeholder="WiFi" required />
26
+ <label for="password">Password:</label>
27
+ <input
28
+ type="password"
29
+ name="password"
30
+ id="password"
31
+ placeholder="Password"
32
+ required
33
+ />
34
+ <input id="submit" type="submit" value="connect" />
35
+ </form>
36
+ </body>
37
+ </html>
@@ -0,0 +1 @@
1
+ <style>html{background:#aaa}*{font-size:xx-large;font-family:monospace}form{min-height:100vh;display:flex;justify-content:center;align-items:center;flex-direction:column;gap:.5rem}</style><meta name='viewport'content='width=device-width,initial-scale=1'><form action='/add-ap'><label for='ssid'>WiFi:</label> <input name='name'id='ssid'placeholder='WiFi'required> <label for='password'>Password:</label> <input type='password'name='password'id='password'placeholder='Password'required> <input id='submit'type='submit'value='connect'></form>
@@ -7,6 +7,7 @@
7
7
  "enums.d.ts",
8
8
  "controller.ts",
9
9
  "wifi.h",
10
+ "httpserver.cpp",
10
11
  "socket.cpp",
11
12
  "wifi.cpp"
12
13
  ],
@@ -35,6 +35,10 @@ declare namespace _wifi {
35
35
  //% shim=_wifi::scanStart
36
36
  function scanStart(): void;
37
37
 
38
+ /** Starts an HTTP server with a login dialog route */
39
+ //% shim=_wifi::startLoginServer
40
+ function startLoginServer(hostName: string): void;
41
+
38
42
  /** Get the results of the scan if any. */
39
43
  //% shim=_wifi::scanResults
40
44
  function scanResults(): Buffer;
@@ -248,6 +248,9 @@ namespace pxsim._wifi {
248
248
  _raiseEvent(WifiEvent.ScanDone)
249
249
  }
250
250
 
251
+ export function startLoginServer(): void {
252
+ }
253
+
251
254
  export function scanResults(): RefBuffer {
252
255
  const b = new Uint8Array(7)
253
256
  b[0] = -20 // rssi
@@ -1,5 +1,4 @@
1
1
  #include "wifi.h"
2
-
3
2
  #include "freertos/event_groups.h"
4
3
  #include "esp_wifi.h"
5
4
  #include "esp_netif.h"
@@ -12,15 +11,6 @@ void settings_init(void);
12
11
 
13
12
  namespace _wifi {
14
13
 
15
- enum class WifiEvent {
16
- //%
17
- ScanDone = 1,
18
- //%
19
- GotIP = 2,
20
- //%
21
- Disconnected = 3,
22
- };
23
-
24
14
  /** Get ID used in events. */
25
15
  //%
26
16
  int eventID() {
@@ -65,16 +55,17 @@ static void init() {
65
55
 
66
56
  settings_init();
67
57
 
58
+ ESP_ERROR_CHECK(esp_netif_init());
68
59
  ESP_ERROR_CHECK(esp_event_loop_create_default());
60
+
69
61
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
70
62
  ESP_ERROR_CHECK(esp_wifi_init(&cfg));
71
63
 
72
- esp_netif_init();
73
64
  esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA();
74
65
  esp_netif_t *netif = esp_netif_new(&netif_config);
75
66
  assert(netif);
76
- esp_netif_attach_wifi_station(netif);
77
- esp_wifi_set_default_wifi_sta_handlers();
67
+ ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif));
68
+ ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
78
69
 
79
70
  ESP_ERROR_CHECK(
80
71
  esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &scan_done_handler, NULL));
@@ -84,8 +75,8 @@ static void init() {
84
75
  esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &got_ip_handler, NULL));
85
76
  ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
86
77
  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
87
- // ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
88
78
  ESP_ERROR_CHECK(esp_wifi_start());
79
+
89
80
  initialized = true;
90
81
  }
91
82
 
@@ -93,6 +84,7 @@ static void init() {
93
84
  //%
94
85
  void scanStart() {
95
86
  init();
87
+
96
88
  scan_done = false;
97
89
  wifi_scan_config_t scan_config;
98
90
  memset(&scan_config, 0, sizeof(scan_config));
@@ -101,6 +93,13 @@ void scanStart() {
101
93
  LOG("scan start: %d", err);
102
94
  }
103
95
 
96
+ /** Starts an HTTP server with a login dialog route */
97
+ //%
98
+ void startLoginServer(String hostName) {
99
+ init();
100
+ _wifi::startHttpServer(hostName->getUTF8Data());
101
+ }
102
+
104
103
  #define JD_WIFI_APFLAGS_HAS_PASSWORD 0x1
105
104
  #define JD_WIFI_APFLAGS_WPS 0x2
106
105
  #define JD_WIFI_APFLAGS_HAS_SECONDARY_CHANNEL_ABOVE 0x4
@@ -8,5 +8,17 @@ namespace pxt {
8
8
  } // namespace pxt
9
9
 
10
10
  namespace _wifi {
11
- int eventID();
11
+ enum class WifiEvent {
12
+ //%
13
+ ScanDone = 1,
14
+ //%
15
+ GotIP = 2,
16
+ //%
17
+ Disconnected = 3,
18
+ //%
19
+ AccessPointCredentialsAvailable = 4
20
+ };
21
+
22
+ int eventID();
23
+ void startHttpServer(const char* hostName);
12
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pxt-common-packages",
3
- "version": "9.4.7",
3
+ "version": "9.4.8",
4
4
  "description": "Microsoft MakeCode (PXT) common packages",
5
5
  "keywords": [
6
6
  "MakeCode",