Implemented: JSON response from MCU

pull/193/head
Karai Csaba 9 years ago committed by Thorsten von Eicken
parent a3432aa581
commit d2d89505e9
  1. 29
      httpd/httpd.c
  2. 1
      httpd/httpd.h
  3. 21
      web-server/web-server.c

@ -626,3 +626,32 @@ void ICACHE_FLASH_ATTR httpdInit(HttpdBuiltInUrl *fixedUrls, int port) {
espconn_accept(&httpdConn); espconn_accept(&httpdConn);
espconn_tcp_set_max_con_allow(&httpdConn, MAX_CONN); espconn_tcp_set_max_con_allow(&httpdConn, MAX_CONN);
} }
int ICACHE_FLASH_ATTR httpdNotify(uint8_t * ip, int port, const void * cgiArg) {
int i;
for (i = 0; i<MAX_CONN; i++)
{
HttpdConnData *conn = connData+i;
if (conn->conn == NULL)
continue;
if (conn->cgi == NULL)
continue;
if (conn->conn->proto.tcp->remote_port != port )
continue;
if (os_memcmp(conn->conn->proto.tcp->remote_ip, ip, 4) != 0)
continue;
char sendBuff[MAX_SENDBUFF_LEN];
conn->priv->sendBuff = sendBuff;
conn->priv->sendBuffLen = 0;
conn->cgiArg = cgiArg;
httpdProcessRequest(conn);
conn->cgiArg = NULL;
return HTTPD_CGI_DONE;
}
return HTTPD_CGI_NOTFOUND;
}

@ -66,5 +66,6 @@ void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn);
int ICACHE_FLASH_ATTR httpdGetHeader(HttpdConnData *conn, char *header, char *ret, int retLen); int ICACHE_FLASH_ATTR httpdGetHeader(HttpdConnData *conn, char *header, char *ret, int retLen);
int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len); int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len);
void ICACHE_FLASH_ATTR httpdFlush(HttpdConnData *conn); void ICACHE_FLASH_ATTR httpdFlush(HttpdConnData *conn);
int ICACHE_FLASH_ATTR httpdNotify(uint8_t * ip, int port, const void * cgiArg);
#endif #endif

@ -177,6 +177,18 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData)
connData->cgiData = (void *)1; connData->cgiData = (void *)1;
} }
if( connData->cgiArg != NULL ) // arrived data from MCU
{
noCacheHeaders(connData, 200);
httpdHeader(connData, "Content-Type", "application/json");
char cl[16];
os_sprintf(cl, "%d", os_strlen(connData->cgiArg));
httpdHeader(connData, "Content-Length", cl);
httpdEndHeaders(connData);
httpdSend(connData, connData->cgiArg, os_strlen(connData->cgiArg));
return HTTPD_CGI_DONE;
}
return HTTPD_CGI_MORE; return HTTPD_CGI_MORE;
} }
@ -185,25 +197,18 @@ void ICACHE_FLASH_ATTR WEB_JsonData(CmdPacket *cmd)
CmdRequest req; CmdRequest req;
cmdRequest(&req, cmd); cmdRequest(&req, cmd);
os_printf("JSON data\n"); // TODO
if (cmdGetArgc(&req) != 3) return; if (cmdGetArgc(&req) != 3) return;
uint8_t ip[4]; uint8_t ip[4];
cmdPopArg(&req, ip, 4); cmdPopArg(&req, ip, 4);
os_printf("IP:%d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); // TODO
uint16_t port; uint16_t port;
cmdPopArg(&req, &port, 2); cmdPopArg(&req, &port, 2);
os_printf("Port:%d\n", port); // TODO
int16_t len = cmdArgLen(&req); int16_t len = cmdArgLen(&req);
os_printf("Len:%d\n", len); // TODO
uint8_t json[len+1]; uint8_t json[len+1];
json[len] = 0; json[len] = 0;
cmdPopArg(&req, json, len); cmdPopArg(&req, json, len);
os_printf("%s\n", json); // TODO httpdNotify(ip, port, json);
// TODO
} }

Loading…
Cancel
Save