main/task_http.c

branch
stable
changeset 46
aaa095986ede
parent 41
7639cfa6aec0
child 47
2aab3b5af4b5
--- a/main/task_http.c	Thu May 02 11:52:36 2019 +0200
+++ b/main/task_http.c	Wed May 08 15:46:50 2019 +0200
@@ -6,6 +6,7 @@
 #include "config.h"
 #include "mbedtls/base64.h"
 #include "mbedtls/sha1.h"
+#include "cJSON.h"
 
 
 static const char		*TAG = "task_http";
@@ -14,6 +15,8 @@
 static TaskHandle_t             xTaskHTTP  = NULL;
 static TaskHandle_t             xTaskQueue = NULL;
 
+cJSON				*root = NULL;
+cJSON				*touch = NULL;
 
 
 /**
@@ -21,8 +24,9 @@
  * @param buf The buffer
  * @param buflen Length of the buffer
  */
+#if 0
 void dump_buf(char *buf, uint16_t buflen);
-
+#endif
 
 
 /**
@@ -210,41 +214,12 @@
 
 
 /**
- * @brief Handle VNC websocket events.
- */
-void websockify_callback(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len)
-{
-    switch(type) {
-	case WEBSOCKET_DISCONNECT_EXTERNAL:
-			VncStopWS(num);
-			break;
-
-	case WEBSOCKET_DISCONNECT_INTERNAL:
-			ESP_LOGI(TAG,"Websockify client %i was disconnected",num);
-			VncStopWS(num);
-			break;
-
-	case WEBSOCKET_DISCONNECT_ERROR:
-			ESP_LOGI(TAG,"Websockify client %i was disconnected due to an error",num);
-			VncStopWS(num);
-			break;
-	
-	case WEBSOCKET_BIN:
-			VncGetWSmessage(msg, len);
-			//dump_buf(msg, len);	
-			break;
-
-	default:	break;
-    }
-}
-
-
-
-/**
  * @brief Handle web ui websocket events.
  */
 void websock_callback(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len)
 {
+    char	jbuf[128];
+
     switch(type) {
 	case WEBSOCKET_CONNECT:
 			ESP_LOGI(TAG,"Websocket client %i connected!",num);
@@ -263,13 +238,32 @@
 			break;
 
 	case WEBSOCKET_TEXT:
+			/*
+			 * Handle json actions from the web clients, like button presses.
+			 */
+			if (len < 128) { // Safety, messages are small.
+			    memcpy(jbuf, msg, len);
+                            jbuf[len] = '\0';
+			    if ((root = cJSON_Parse(jbuf))) {
+				if ((touch = cJSON_GetObjectItem(root,"touch"))) {
+				    int x = cJSON_GetObjectItem(touch, "x")->valueint;
+				    int y = cJSON_GetObjectItem(touch, "y")->valueint;
+				    WS_touched(x, y);
+				    break;
+				} else {
+				    ESP_LOGI(TAG,"not json touch");
+				}
+				cJSON_Delete(root);
+			    } else {
+				ESP_LOGI(TAG,"not json");
+			    }
+			}
+			// Log if the message in not processed.
 			ESP_LOGI(TAG,"Websocket client %i sent text message of size %i:\n%s",num,(uint32_t)len,msg);
-			dump_buf(msg, len);
 			break;
 
 	case WEBSOCKET_BIN:
 			ESP_LOGI(TAG,"Websocket client %i sent bin message of size %i:\n",num,(uint32_t)len);
-			dump_buf(msg, len);
 			break;
 
 	case WEBSOCKET_PING:
@@ -284,6 +278,7 @@
 
 
 
+#if 0
 void dump_buf(char *buf, uint16_t buflen)
 {
     int i = 0, l = 1;
@@ -308,6 +303,7 @@
     }
     printf("\n");
 }
+#endif
 
 
 
@@ -419,15 +415,6 @@
 	    return;
 	}
 
-	// websocket for noVNC.
-	if ((strstr(buf,"GET /websockify ") && strstr(buf,"Upgrade: websocket"))) {
-	    int nr = ws_server_add_client_protocol(conn, buf, buflen, "/websockify", "binary", websockify_callback);
-	    ESP_LOGI(TAG, "%s new websocket on /websockify client: %d", ipstr, nr);
-	    netbuf_delete(inbuf);
-	    VncStartWS(nr);
-	    return;
-	}
-
 	// websocket for web UI.
 	if ((strstr(buf,"GET /ws ") && strstr(buf,"Upgrade: websocket"))) {
 	    int nr = ws_server_add_client_protocol(conn, buf, buflen, "/ws", "binary", websock_callback);
@@ -438,7 +425,9 @@
 	    return;
 	}
 
+#if 0
 	dump_buf(buf, buflen);
+#endif
 
 	if (strstr(buf, "GET /")) {
 		ESP_LOGI(TAG, "%s request: %s", ipstr, buf);

mercurial