// Execute a parsed command static uint32_t ICACHE_FLASH_ATTR CMD_Exec(const CmdList *scp, CmdPacket *packet) { uint16_t crc = 0; // Iterate through the command table and call the appropriate function while (scp->sc_function != NULL) { if(scp->sc_name == packet->cmd) { //os_printf("CMD: Dispatching cmd=%d\n", packet->cmd); // call command function uint32_t ret = scp->sc_function(packet); // if requestor asked for a response, send it if (packet->_return){ #ifdef CMD_DBG os_printf("CMD: Response: 0x%lx, cmd: %d\r\n", ret, packet->cmd); #endif crc = CMD_ResponseStart(packet->cmd, 0, ret, 0); CMD_ResponseEnd(crc); } else { #ifdef CMD_DBG os_printf("CMD: no response (%lu)\n", packet->_return); #endif } return ret; } scp++; } #ifdef CMD_DBG os_printf("CMD: cmd=%d not found\n", packet->cmd); #endif return 0; }
void ICACHE_FLASH_ATTR cmdMqttPublishedCb(uint32_t* args) { MQTT_Client* client = (MQTT_Client*)args; MqttCmdCb* cb = (MqttCmdCb*)client->user_data; #ifdef MQTTCMD_DBG os_printf("MQTT: Published\n"); #endif uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->publishedCb, 0, 0); CMD_ResponseEnd(crc); }
void ICACHE_FLASH_ATTR cmdMqttDataCb(uint32_t* args, const char* topic, uint32_t topic_len, const char* data, uint32_t data_len) { uint16_t crc = 0; MQTT_Client* client = (MQTT_Client*)args; MqttCmdCb* cb = (MqttCmdCb*)client->user_data; crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->dataCb, 0, 2); crc = CMD_ResponseBody(crc, (uint8_t*)topic, topic_len); crc = CMD_ResponseBody(crc, (uint8_t*)data, data_len); CMD_ResponseEnd(crc); }
void ICACHE_FLASH_ATTR cmdMqttConnectedCb(uint32_t* args) { MQTT_Client* client = (MQTT_Client*)args; MqttCmdCb* cb = (MqttCmdCb*)client->user_data; #ifdef MQTTCMD_DBG os_printf("MQTT: Connected connectedCb=%p, disconnectedCb=%p, publishedCb=%p, dataCb=%p\n", (void*)cb->connectedCb, (void*)cb->disconnectedCb, (void*)cb->publishedCb, (void*)cb->dataCb); #endif uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->connectedCb, 0, 0); CMD_ResponseEnd(crc); }
// Callback from wifi subsystem to notify us of status changes static void ICACHE_FLASH_ATTR CMD_WifiCb(uint8_t wifiStatus) { if (wifiStatus != lastWifiStatus){ DBG("CMD_WifiCb: wifiStatus=%d\n", wifiStatus); lastWifiStatus = wifiStatus; cmdCallback *wifiCb = CMD_GetCbByName("wifiCb"); if ((uint32_t)wifiCb->callback != -1) { uint8_t status = wifiStatus == wifiGotIP ? 5 : 1; uint16_t crc = CMD_ResponseStart(CMD_WIFI_CONNECT, (uint32_t)wifiCb->callback, 0, 1); crc = CMD_ResponseBody(crc, (uint8_t*)&status, 1); CMD_ResponseEnd(crc); } } }
uint32_t ICACHE_FLASH_ATTR NTP_Time(PACKET_CMD *cmd) { uint32 current_stamp = 0; char *time = NULL; uint16_t crc; current_stamp = sntp_get_current_timestamp(); time = sntp_get_real_time(current_stamp); crc = CMD_ResponseStart(CMD_NTP_TIME, cmd->callback, 0, 1); crc = CMD_ResponseBody(crc, (uint8_t*)time, os_strlen(time)); CMD_ResponseEnd(crc); return 0; }
LOCAL uint32_t ICACHE_FLASH_ATTR CMD_Exec(const CMD_LIST *scp, PACKET_CMD *packet) { uint32_t ret; uint16_t crc = 0; while (scp->sc_name != CMD_NULL){ if(scp->sc_name == packet->cmd) { ret = scp->sc_function(packet); if(packet->_return){ INFO("CMD: Response return value: %d, cmd: %d\r\n", ret, packet->cmd); crc = CMD_ResponseStart(packet->cmd, 0, ret, 0); CMD_ResponseEnd(crc); } return ret; } scp++; } return 0; }
uint32_t ICACHE_FLASH_ATTR SYSTEM_Handler(PACKET_CMD *cmd) { enum system_event event; REQUEST req; uint8_t mac[6]; uint16_t crc; CMD_Request(&req, cmd); if (CMD_GetArgc(&req) != 1) { return 0; } CMD_PopArgs(&req, (uint8_t*)&event); switch (event) { case GET_STATION_MAC: case GET_AP_MAC: if (event == GET_STATION_MAC) { wifi_get_macaddr(STATION_IF, mac); } else { wifi_get_macaddr(SOFTAP_IF, mac); } crc = CMD_ResponseStart(CMD_SYSTEM, cmd->callback, 0, 2); crc = CMD_ResponseBody(crc, (uint8_t*)&event, sizeof(event)); crc = CMD_ResponseBody(crc, mac, 6); CMD_ResponseEnd(crc); break; default: break; } return 0; }
// Receive HTTP response - this hacky function assumes that the full response is received in // one go. Sigh... static void ICACHE_FLASH_ATTR tcpclient_recv(void *arg, char *pdata, unsigned short len) { struct espconn *pCon = (struct espconn*)arg; RestClient *client = (RestClient *)pCon->reverse; // parse status line int pi = 0; int32_t code = -1; char statusCode[4] = "\0\0\0\0"; int statusLen = 0; bool inStatus = false; while (pi < len) { if (pdata[pi] == '\n') { // end of status line if (code == -1) code = 502; // BAD GATEWAY break; } else if (pdata[pi] == ' ') { if (inStatus) code = atoi(statusCode); inStatus = !inStatus; } else if (inStatus) { if (statusLen < 3) statusCode[statusLen] = pdata[pi]; statusLen++; } pi++; } // parse header, all this does is look for the end of the header bool currentLineIsBlank = false; while (pi < len) { if (pdata[pi] == '\n') { if (currentLineIsBlank) { // body is starting pi++; break; } currentLineIsBlank = true; } else if (pdata[pi] != '\r') { currentLineIsBlank = false; } pi++; } //if (pi < len && pdata[pi] == '\r') pi++; // hacky! // collect body and send it uint16_t crc; int body_len = len-pi; #ifdef REST_DBG os_printf("REST: status=%ld, body=%d\n", code, body_len); #endif if (pi == len) { crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 0); } else { crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 1); crc = CMD_ResponseBody(crc, (uint8_t*)(pdata+pi), body_len); CMD_ResponseEnd(crc); #ifdef REST_DBG os_printf("REST: body="); for (int j=pi; j<len; j++) os_printf(" %02x", pdata[j]); os_printf("\n"); #endif } //if(client->security) // espconn_secure_disconnect(client->pCon); //else espconn_disconnect(client->pCon); }
void ICACHE_FLASH_ATTR tcpclient_recv(void *arg, char *pdata, unsigned short len) { uint8_t currentLineIsBlank = 0; uint8_t httpBody = 0; uint8_t inStatus = 0; char statusCode[4]; int i = 0, j; uint32_t code = 0; uint16_t crc; struct espconn *pCon = (struct espconn*)arg; REST_CLIENT *client = (REST_CLIENT *)pCon->reverse; for(j=0 ;j<len; j++){ char c = pdata[j]; if(c == ' ' && !inStatus){ inStatus = 1; } if(inStatus && i < 3 && c != ' '){ statusCode[i] = c; i++; //start edit for emma if(i == 3) { statusCode[i] = '\0'; code = atoi(statusCode); INFO("REST: status = %d\r\n",code); } //end edit for emma } if(i == 3){ statusCode[i] = '\0'; code = atoi(statusCode); } if(httpBody){ //only write response if its not null uint32_t body_len = len - j; INFO("REST: status = %d, body_len = %d\r\n",code, body_len); if(body_len == 0){ crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 0); } else { crc = CMD_ResponseStart(CMD_REST_EVENTS, client->resp_cb, code, 1); crc = CMD_ResponseBody(crc, &pdata[j], body_len); } CMD_ResponseEnd(crc); break; } else { if (c == '\n' && currentLineIsBlank) { httpBody = true; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } if(client->security) espconn_secure_disconnect(client->pCon); else espconn_disconnect(client->pCon); }