main/task_http.c

branch
novnc
changeset 41
7639cfa6aec0
parent 38
537ffe280775
child 47
2aab3b5af4b5
equal deleted inserted replaced
40:71e06f6d80fd 41:7639cfa6aec0
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.
212 /** 216 /**
213 * @brief Handle web ui websocket events. 217 * @brief Handle web ui websocket events.
214 */ 218 */
215 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)
216 { 220 {
221 char jbuf[128];
222
217 switch(type) { 223 switch(type) {
218 case WEBSOCKET_CONNECT: 224 case WEBSOCKET_CONNECT:
219 ESP_LOGI(TAG,"Websocket client %i connected!",num); 225 ESP_LOGI(TAG,"Websocket client %i connected!",num);
220 break; 226 break;
221 227
230 case WEBSOCKET_DISCONNECT_ERROR: 236 case WEBSOCKET_DISCONNECT_ERROR:
231 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);
232 break; 238 break;
233 239
234 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.
235 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);
236 dump_buf(msg, len);
237 break; 263 break;
238 264
239 case WEBSOCKET_BIN: 265 case WEBSOCKET_BIN:
240 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);
241 dump_buf(msg, len);
242 break; 267 break;
243 268
244 case WEBSOCKET_PING: 269 case WEBSOCKET_PING:
245 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);
246 break; 271 break;
251 } 276 }
252 } 277 }
253 278
254 279
255 280
281 #if 0
256 void dump_buf(char *buf, uint16_t buflen) 282 void dump_buf(char *buf, uint16_t buflen)
257 { 283 {
258 int i = 0, l = 1; 284 int i = 0, l = 1;
259 285
260 printf("request length %d\n00: ", buflen); 286 printf("request length %d\n00: ", buflen);
275 } 301 }
276 i++; 302 i++;
277 } 303 }
278 printf("\n"); 304 printf("\n");
279 } 305 }
306 #endif
280 307
281 308
282 309
283 /** 310 /**
284 * @brief Serve any client. 311 * @brief Serve any client.
396 TFTstartWS(nr); 423 TFTstartWS(nr);
397 // Startup something? Init webscreen? 424 // Startup something? Init webscreen?
398 return; 425 return;
399 } 426 }
400 427
428 #if 0
401 dump_buf(buf, buflen); 429 dump_buf(buf, buflen);
430 #endif
402 431
403 if (strstr(buf, "GET /")) { 432 if (strstr(buf, "GET /")) {
404 ESP_LOGI(TAG, "%s request: %s", ipstr, buf); 433 ESP_LOGI(TAG, "%s request: %s", ipstr, buf);
405 http_error(conn, 404, "Not found", "Not found"); 434 http_error(conn, 404, "Not found", "Not found");
406 } else { 435 } else {

mercurial