4 * This uses some ESP32 Websocket code written by Blake Felt - blake.w.felt@gmail.com |
4 * This uses some ESP32 Websocket code written by Blake Felt - blake.w.felt@gmail.com |
5 */ |
5 */ |
6 #include "config.h" |
6 #include "config.h" |
7 #include "mbedtls/base64.h" |
7 #include "mbedtls/base64.h" |
8 #include "mbedtls/sha1.h" |
8 #include "mbedtls/sha1.h" |
|
9 #include "cJSON.h" |
9 |
10 |
10 |
11 |
11 static const char *TAG = "task_http"; |
12 static const char *TAG = "task_http"; |
12 static QueueHandle_t client_queue; |
13 static QueueHandle_t client_queue; |
13 const static int client_queue_size = 10; |
14 const static int client_queue_size = 10; |
14 static TaskHandle_t xTaskHTTP = NULL; |
15 static TaskHandle_t xTaskHTTP = NULL; |
15 static TaskHandle_t xTaskQueue = NULL; |
16 static TaskHandle_t xTaskQueue = NULL; |
16 |
17 |
|
18 cJSON *root = NULL; |
|
19 cJSON *touch = NULL; |
17 |
20 |
18 |
21 |
19 /** |
22 /** |
20 * @brief Debug dump buffer |
23 * @brief Debug dump buffer |
21 * @param buf The buffer |
24 * @param buf The buffer |
22 * @param buflen Length of the buffer |
25 * @param buflen Length of the buffer |
23 */ |
26 */ |
|
27 #if 0 |
24 void dump_buf(char *buf, uint16_t buflen); |
28 void dump_buf(char *buf, uint16_t buflen); |
25 |
29 #endif |
26 |
30 |
27 |
31 |
28 /** |
32 /** |
29 * @brief Send HTTP error and message |
33 * @brief Send HTTP error and message |
30 * @param conn The socket to send to. |
34 * @param conn The socket to send to. |
208 } |
212 } |
209 |
213 |
210 |
214 |
211 |
215 |
212 /** |
216 /** |
213 * @brief Handle VNC websocket events. |
|
214 */ |
|
215 void websockify_callback(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len) |
|
216 { |
|
217 switch(type) { |
|
218 case WEBSOCKET_DISCONNECT_EXTERNAL: |
|
219 VncStopWS(num); |
|
220 break; |
|
221 |
|
222 case WEBSOCKET_DISCONNECT_INTERNAL: |
|
223 ESP_LOGI(TAG,"Websockify client %i was disconnected",num); |
|
224 VncStopWS(num); |
|
225 break; |
|
226 |
|
227 case WEBSOCKET_DISCONNECT_ERROR: |
|
228 ESP_LOGI(TAG,"Websockify client %i was disconnected due to an error",num); |
|
229 VncStopWS(num); |
|
230 break; |
|
231 |
|
232 case WEBSOCKET_BIN: |
|
233 VncGetWSmessage(msg, len); |
|
234 //dump_buf(msg, len); |
|
235 break; |
|
236 |
|
237 default: break; |
|
238 } |
|
239 } |
|
240 |
|
241 |
|
242 |
|
243 /** |
|
244 * @brief Handle web ui websocket events. |
217 * @brief Handle web ui websocket events. |
245 */ |
218 */ |
246 void websock_callback(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len) |
219 void websock_callback(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len) |
247 { |
220 { |
|
221 char jbuf[128]; |
|
222 |
248 switch(type) { |
223 switch(type) { |
249 case WEBSOCKET_CONNECT: |
224 case WEBSOCKET_CONNECT: |
250 ESP_LOGI(TAG,"Websocket client %i connected!",num); |
225 ESP_LOGI(TAG,"Websocket client %i connected!",num); |
251 break; |
226 break; |
252 |
227 |
261 case WEBSOCKET_DISCONNECT_ERROR: |
236 case WEBSOCKET_DISCONNECT_ERROR: |
262 ESP_LOGI(TAG,"Websocket client %i was disconnected due to an error",num); |
237 ESP_LOGI(TAG,"Websocket client %i was disconnected due to an error",num); |
263 break; |
238 break; |
264 |
239 |
265 case WEBSOCKET_TEXT: |
240 case WEBSOCKET_TEXT: |
|
241 /* |
|
242 * Handle json actions from the web clients, like button presses. |
|
243 */ |
|
244 if (len < 128) { // Safety, messages are small. |
|
245 memcpy(jbuf, msg, len); |
|
246 jbuf[len] = '\0'; |
|
247 if ((root = cJSON_Parse(jbuf))) { |
|
248 if ((touch = cJSON_GetObjectItem(root,"touch"))) { |
|
249 int x = cJSON_GetObjectItem(touch, "x")->valueint; |
|
250 int y = cJSON_GetObjectItem(touch, "y")->valueint; |
|
251 WS_touched(x, y); |
|
252 break; |
|
253 } else { |
|
254 ESP_LOGI(TAG,"not json touch"); |
|
255 } |
|
256 cJSON_Delete(root); |
|
257 } else { |
|
258 ESP_LOGI(TAG,"not json"); |
|
259 } |
|
260 } |
|
261 // Log if the message in not processed. |
266 ESP_LOGI(TAG,"Websocket client %i sent text message of size %i:\n%s",num,(uint32_t)len,msg); |
262 ESP_LOGI(TAG,"Websocket client %i sent text message of size %i:\n%s",num,(uint32_t)len,msg); |
267 dump_buf(msg, len); |
|
268 break; |
263 break; |
269 |
264 |
270 case WEBSOCKET_BIN: |
265 case WEBSOCKET_BIN: |
271 ESP_LOGI(TAG,"Websocket client %i sent bin message of size %i:\n",num,(uint32_t)len); |
266 ESP_LOGI(TAG,"Websocket client %i sent bin message of size %i:\n",num,(uint32_t)len); |
272 dump_buf(msg, len); |
|
273 break; |
267 break; |
274 |
268 |
275 case WEBSOCKET_PING: |
269 case WEBSOCKET_PING: |
276 ESP_LOGI(TAG,"client %i pinged us with message of size %i:\n%s",num,(uint32_t)len,msg); |
270 ESP_LOGI(TAG,"client %i pinged us with message of size %i:\n%s",num,(uint32_t)len,msg); |
277 break; |
271 break; |
417 netconn_delete(conn); |
413 netconn_delete(conn); |
418 netbuf_delete(inbuf); |
414 netbuf_delete(inbuf); |
419 return; |
415 return; |
420 } |
416 } |
421 |
417 |
422 // websocket for noVNC. |
|
423 if ((strstr(buf,"GET /websockify ") && strstr(buf,"Upgrade: websocket"))) { |
|
424 int nr = ws_server_add_client_protocol(conn, buf, buflen, "/websockify", "binary", websockify_callback); |
|
425 ESP_LOGI(TAG, "%s new websocket on /websockify client: %d", ipstr, nr); |
|
426 netbuf_delete(inbuf); |
|
427 VncStartWS(nr); |
|
428 return; |
|
429 } |
|
430 |
|
431 // websocket for web UI. |
418 // websocket for web UI. |
432 if ((strstr(buf,"GET /ws ") && strstr(buf,"Upgrade: websocket"))) { |
419 if ((strstr(buf,"GET /ws ") && strstr(buf,"Upgrade: websocket"))) { |
433 int nr = ws_server_add_client_protocol(conn, buf, buflen, "/ws", "binary", websock_callback); |
420 int nr = ws_server_add_client_protocol(conn, buf, buflen, "/ws", "binary", websock_callback); |
434 ESP_LOGI(TAG, "%s new websocket on /ws client: %d", ipstr, nr); |
421 ESP_LOGI(TAG, "%s new websocket on /ws client: %d", ipstr, nr); |
435 netbuf_delete(inbuf); |
422 netbuf_delete(inbuf); |
436 TFTstartWS(nr); |
423 TFTstartWS(nr); |
437 // Startup something? Init webscreen? |
424 // Startup something? Init webscreen? |
438 return; |
425 return; |
439 } |
426 } |
440 |
427 |
|
428 #if 0 |
441 dump_buf(buf, buflen); |
429 dump_buf(buf, buflen); |
|
430 #endif |
442 |
431 |
443 if (strstr(buf, "GET /")) { |
432 if (strstr(buf, "GET /")) { |
444 ESP_LOGI(TAG, "%s request: %s", ipstr, buf); |
433 ESP_LOGI(TAG, "%s request: %s", ipstr, buf); |
445 http_error(conn, 404, "Not found", "Not found"); |
434 http_error(conn, 404, "Not found", "Not found"); |
446 } else { |
435 } else { |