LOCAL void parse_json(struct jsonparse_state *jparser) { int type; unsigned int id; char command[128] = ""; const char *params; int paramslen = 0; char timestamp[128] = ""; while ((type = jsonparse_next(jparser)) != JSON_TYPE_ERROR) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(jparser, "serverTimestamp") == 0) { jsonparse_next(jparser); if (jsonparse_next(jparser) != JSON_TYPE_ERROR) { jsonparse_copy_value(jparser, timestamp, sizeof(timestamp)); dhdebug("Timestamp received %s", timestamp); dhrequest_update_poll(&mPollRequest, timestamp); } break; } else if (jsonparse_strcmp_value(jparser, "command") == 0) { jsonparse_next(jparser); if(jsonparse_next(jparser) != JSON_TYPE_ERROR) jsonparse_copy_value(jparser, command, sizeof(command)); } else if (jsonparse_strcmp_value(jparser, "id") == 0) { jsonparse_next(jparser); if(jsonparse_next(jparser) != JSON_TYPE_ERROR) id = jsonparse_get_value_as_ulong(jparser); } else if (jsonparse_strcmp_value(jparser, "parameters") == 0) { jsonparse_next(jparser); if(jsonparse_next(jparser) != JSON_TYPE_ERROR) { // there is an issue with extracting subjson with jparser->vstart or jparser_copy_value params = &jparser->json[jparser->pos - 1]; if(*params == '{') { int end = jparser->pos; while(end < jparser->len && jparser->json[end] != '}') { end++; } paramslen = end - jparser->pos + 2; } } } else if (jsonparse_strcmp_value(jparser, "timestamp") == 0) { jsonparse_next(jparser); if(jsonparse_next(jparser) != JSON_TYPE_ERROR) jsonparse_copy_value(jparser, timestamp, sizeof(timestamp)); } } } if (mConnectionState == CS_POLL) { if(timestamp[0]) { dhdebug("Timestamp received %s", timestamp); dhrequest_update_poll(&mPollRequest, timestamp); } mCommandCallback(id, command, params, paramslen); } }
// ---------------------------------------------------------------------------- // CGI that receives the json list of canister contents // ---------------------------------------------------------------------------- void ICACHE_FLASH_ATTR put_contents(char *jsdata, int len) { char type; char str[80]; char buffer[1024]; // contents names struct jsonparse_state state; struct jsonparse_state *js = &state; jsonparse_setup(js, jsdata, len); type = jsonparse_next(js); if(type != JSON_TYPE_OBJECT) { os_printf("Not a valid JSON file(1)\n"); return; } type = jsonparse_next(js); if(type != JSON_TYPE_PAIR_NAME) { os_printf("Not a valid JSON file(2)\n"); return; } jsonparse_copy_value(js, str, 80); if(jsonparse_strcmp_value(js, "contents") != 0) { os_printf("Not a valid Contents file\n"); return; } int n = 0; for(int i=0; i < 1024; i++) buffer[i] = 0; while(js->pos < js->len) { type = jsonparse_next(js); if(type != JSON_TYPE_PAIR_NAME) continue; if(jsonparse_strcmp_value(js, "name") == 0) { type = jsonparse_next(js); if(type == JSON_TYPE_PAIR) { type = jsonparse_next(js); if(type == JSON_TYPE_STRING) { jsonparse_copy_value(js, &buffer[n], 30); n += 32; } } } if(jsonparse_strcmp_value(js, "amount") == 0) { type = jsonparse_next(js); if(type == JSON_TYPE_PAIR) { type = jsonparse_next(js); int val = 0; if(type == JSON_TYPE_NUMBER) val = jsonparse_get_value_as_int(js); else if(type == JSON_TYPE_STRING) { jsonparse_copy_value(js, str, 16); val = atoi(str); } buffer[n-2] = (uint8_t)(val >> 8); buffer[n-1] = (uint8_t)(val & 0xff); } }
LOCAL int ICACHE_FLASH_ATTR open_door_cmd_json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser) { int type; while ((type = jsonparse_next(parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { char buffer[64]; os_bzero(buffer, 64); if (jsonparse_strcmp_value(parser, "cmd") == 0) { jsonparse_next(parser); jsonparse_next(parser); jsonparse_copy_value(parser, buffer, sizeof(buffer)); if (!strcoll(buffer, "on")) { #ifdef MENJINDEBUG os_printf("JSON: door open \n"); #endif easygpio_outputSet(JIDIANQI_IO, 0); } else if (!strcoll(buffer, "off")) { #ifdef MENJINDEBUG os_printf("JSON: door clease \n"); #endif // OLED_Print(0, 2, " ", 2); OLED_Print(0, 2, " Permission ", 2); // OLED_Print(0, 4, " ", 2); OLED_Print(0, 4, " Denied ", 2); easygpio_outputSet(JIDIANQI_IO, 1); } } } } return 0; }
LOCAL int ICACHE_FLASH_ATTR user_info_json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser) { int type; while ((type = jsonparse_next(parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { char buffer[64]; os_bzero(buffer, 64); if (jsonparse_strcmp_value(parser, "studentID") == 0) { jsonparse_next(parser); jsonparse_next(parser); jsonparse_copy_value(parser, buffer, sizeof(buffer)); #ifdef ESP8266OLED if (!strcoll(buffer, "0000000000")) { } else { //FIXME 添加OLED显示学号 // OLED_Print(0, 2, " ", 2); OLED_Print(0, 2, " Welcome ", 2); OLED_Print(0, 4, " ", 2); OLED_Print(3, 4, buffer, 2); } #endif #ifdef MENJINDEBUG os_printf("\nstudent ID :%s\n", buffer); #endif } } } return 0; }
LOCAL int ICACHE_FLASH_ATTR json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser) { int type; INFO("Json set/n"); INFO(parser); INFO("/n"); while ((type = jsonparse_next(parser)) != 0) { INFO (parser); INFO("/n"); if (type == JSON_TYPE_PAIR_NAME) { char buffer[64]; os_bzero(buffer, 64); if (jsonparse_strcmp_value(parser, "switch") == 0) { jsonparse_next(parser); jsonparse_next(parser); jsonparse_copy_value(parser, buffer, sizeof(buffer)); if (!strcoll(buffer, "on")) { INFO("JSON: Switch on\n", buffer); GPIO_OUTPUT_SET(SWITCH03_GPIO, 1); } else if (!strcoll(buffer, "off")) { INFO("JSON: Switch off\n", buffer); GPIO_OUTPUT_SET(SWITCH03_GPIO, 0); } } } } return 0; }
int __jsonparse_next(struct jsonparse_state *j) { int type = jsonparse_next(j); char data[50] = ""; if (type == 'N' || type == '0' || type == '"') jsonparse_copy_value(j, data, sizeof(data)); os_printf("json type=%d %c -> %s\n",type,type,data); return(type); }
LOCAL bool ICACHE_FLASH_ATTR mod_led_8x8_rgb_parse(char *data, uint16 data_len) { struct jsonparse_state parser; int type; uint8 cols = mod_led_8x8_cols; uint8 rows = mod_led_8x8_rows; uint8 speed = mod_led_8x8_speed; if (mod_led_8x8_text == NULL) { mod_led_8x8_text = (char *)os_zalloc(MOD_LED_8x8_RGB_MAX_TEXT); } jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "cols") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); mod_led_8x8_cols = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "rows") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); mod_led_8x8_rows = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "Speed") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); mod_led_8x8_speed = jsonparse_get_value_as_int(&parser); if (mod_led_8x8_speed > MOD_LED_8x8_RGB_MAX_SPEED) { mod_led_8x8_speed = MOD_LED_8x8_RGB_MAX_SPEED; } } else if (jsonparse_strcmp_value(&parser, "R") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); mod_led_8x8_r = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "G") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); mod_led_8x8_g = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "B") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); mod_led_8x8_b = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "Text") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); jsonparse_copy_value(&parser, mod_led_8x8_text, MOD_LED_8x8_RGB_MAX_TEXT); } } } return ( cols != mod_led_8x8_cols || rows != mod_led_8x8_rows || speed != mod_led_8x8_speed ); }
LOCAL int ICACHE_FLASH_ATTR time_info_json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser) { int type; while ((type = jsonparse_next(parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { char buffer[64]; os_bzero(buffer, 64); if (jsonparse_strcmp_value(parser, "time") == 0) { jsonparse_next(parser); jsonparse_next(parser); jsonparse_copy_value(parser, buffer, sizeof(buffer)); //TODO 添加显示时间! OLED_Print(0, 2, " ", 2); OLED_Print(5, 2, buffer, 2); OLED_Print(10, 2, " ", 2); } } } return 0; }
void ICACHE_FLASH_ATTR mb_pcd8544_handler( struct espconn *pConnection, request_method method, char *url, char *data, uint16 data_len, uint32 content_len, char *response, uint16 response_len ) { struct jsonparse_state parser; int type; char tmp_str[20]; mb_pcd8544_config_t *p_config = mb_p_pcd8544_config; bool is_post = (method == POST); int start_cmd = -1; // 0=STOP, 1=START other none uint8 tmp_ret = 0xFF; // post config for INIT if (method == POST && data != NULL && data_len != 0) { jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "Reset_pin") == 0) { jsonparse_next(&parser);jsonparse_next(&parser); p_config->resetPin = jsonparse_get_value_as_sint(&parser); MB_PCD8544_DEBUG("PCD8544:CFG:Reset_pin:%d\n", p_config->resetPin); } else if (jsonparse_strcmp_value(&parser, "Sce_pin") == 0) { jsonparse_next(&parser);jsonparse_next(&parser); p_config->scePin = jsonparse_get_value_as_sint(&parser); MB_PCD8544_DEBUG("PCD8544:CFG:Sce_pin:%d\n", p_config->scePin); } else if (jsonparse_strcmp_value(&parser, "Dc_pin") == 0) { jsonparse_next(&parser);jsonparse_next(&parser); p_config->dcPin = jsonparse_get_value_as_sint(&parser); MB_PCD8544_DEBUG("PCD8544:CFG:Dc_pin:%d\n", p_config->dcPin); } else if (jsonparse_strcmp_value(&parser, "Sdin_pin") == 0) { jsonparse_next(&parser);jsonparse_next(&parser); p_config->sdinPin = jsonparse_get_value_as_sint(&parser); MB_PCD8544_DEBUG("PCD8544:CFG:Sdin_pin:%d\n", p_config->sdinPin); } else if (jsonparse_strcmp_value(&parser, "Sclk_pin") == 0) { jsonparse_next(&parser);jsonparse_next(&parser); p_config->sclkPin = jsonparse_get_value_as_sint(&parser); MB_PCD8544_DEBUG("PCD8544:CFG:Sclk_pin: %d\n", p_config->sclkPin); } else if (tmp_ret = user_app_config_handler_part(&parser) != 0xFF){ // check for common app commands MB_PCD8544_DEBUG("PCD8544:CFG:APPCONFIG:%d\n", tmp_ret); } else if (jsonparse_strcmp_value(&parser, "Goto_xy") == 0) { // 11,22 is_post = false; jsonparse_next(&parser);jsonparse_next(&parser); char tmp_goto[32]; jsonparse_copy_value(&parser, tmp_goto, 32); char *p_comma = (char *)os_strstr(tmp_goto, ","); if (tmp_goto > 0 && p_comma - tmp_goto < 32) { char tmp_goto_[32]; strncpy(tmp_goto_, tmp_goto, p_comma - tmp_goto - 1); int goto_x = atoi(tmp_goto_); tmp_goto_[0] = 0x00; strncpy(tmp_goto_, p_comma+1, 32); int goto_y = atoi(tmp_goto_); PCD8544_gotoXY(goto_x, goto_y); MB_PCD8544_DEBUG("PCD8544:Goto_x/y:%d/%d\n", goto_x, goto_y); } MB_PCD8544_DEBUG("PCD8544:Goto_x/y:NA\n"); } else if (jsonparse_strcmp_value(&parser, "") == 0) { is_post = false; jsonparse_next(&parser);jsonparse_next(&parser); char tmp_str[128]; jsonparse_copy_value(&parser, tmp_str, MB_VARNAMEMAX); PCD8544_lcdPrint(tmp_str); MB_PCD8544_DEBUG("PCD8544:lcdPrint:%s\n", tmp_str); } } } if (is_post) { mb_pcd8544_hw_init(); } } mb_PCD8544_set_response(response, false, is_post ? MB_REQTYPE_POST : MB_REQTYPE_GET); }
static void FUNCTION_ATTRIBUTE http_callback_login(char * response) { if(request != NULL) { pd_free(request); request = NULL; } if(response == NULL) { device_login_callback(PANDO_LOGIN_FAIL); return; } pd_printf("response=%s\n(end)\n", response); struct jsonparse_state json_state; jsonparse_setup(&json_state, response, pd_strlen(response)); int code; char message[MSG_BUF_LEN]; char access_token[ACCESS_TOKEN_LEN*2 + 16]; char access_addr[KEY_BUF_LEN]; access_token[ACCESS_TOKEN_LEN*2] = '\0'; int type; while ((type = jsonparse_next(&json_state)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if(jsonparse_strcmp_value(&json_state, "code") == 0) { jsonparse_next(&json_state); jsonparse_next(&json_state); code = jsonparse_get_value_as_int(&json_state); } else if(jsonparse_strcmp_value(&json_state, "message") == 0) { jsonparse_next(&json_state); jsonparse_next(&json_state); jsonparse_copy_value(&json_state, message, MSG_BUF_LEN); } else if(jsonparse_strcmp_value(&json_state, "data") == 0) { while((type = jsonparse_next(&json_state)) != 0 && json_state.depth > 1) { if(type == JSON_TYPE_PAIR_NAME) { if(jsonparse_strcmp_value(&json_state, "access_token") == 0) { jsonparse_next(&json_state); jsonparse_next(&json_state); jsonparse_copy_value(&json_state, access_token, ACCESS_TOKEN_LEN*2 + 16); } else if(jsonparse_strcmp_value(&json_state, "access_addr") == 0) { jsonparse_next(&json_state); jsonparse_next(&json_state); jsonparse_copy_value(&json_state, access_addr, KEY_BUF_LEN); } } } } } } if(code != 0) { pd_printf("device login failed: %s\n", message); if(device_login_callback != NULL) { device_login_callback(PANDO_LOGIN_FAIL); } return; } hex2bin(pando_device_token, access_token); pd_printf("device login success, access_addr : %s\n", access_addr); pando_data_set(DATANAME_ACCESS_ADDR, access_addr); pando_data_set(DATANAME_ACCESS_TOKEN, access_token); if(device_login_callback != NULL) { device_login_callback(PANDO_LOGIN_OK); } }
void ICACHE_FLASH_ATTR finger_handler( struct espconn *pConnection, request_method method, char *url, char *data, uint16 data_len, uint32 content_len, char *response, uint16 response_len ) { if (device_get_uart() != UART_FINGER) { json_error(response, MOD_FINGER, DEVICE_NOT_FOUND, NULL); return; } struct jsonparse_state parser; int type, delete_len; uint16 delete_id; if (method == POST && data != NULL && data_len != 0) { jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "Address") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); char *convert_err = NULL; char address_str[20]; jsonparse_copy_value(&parser, address_str, 20); uint32 address = strtoul(address_str, &convert_err, 16); if (*convert_err == '\0' && address != finger_address()) { finger_set_address(address, finger_default); } } else if (jsonparse_strcmp_value(&parser, "SecurityLevel") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); uint8 security_level = jsonparse_get_value_as_int(&parser); if (security_level != finger_security_level()) { finger_set_security_lefel(security_level, finger_default); } } else if (jsonparse_strcmp_value(&parser, "Mode") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); if (jsonparse_strcmp_value(&parser, "Read") == 0) { finger_current_mode = FINGER_READ; } else if (jsonparse_strcmp_value(&parser, "New") == 0) { finger_current_mode = FINGER_NEW; } else if (jsonparse_strcmp_value(&parser, "Delete") == 0) { finger_current_mode = FINGER_DELETE; } else if (jsonparse_strcmp_value(&parser, "Empty DB") == 0) { finger_current_mode = FINGER_EMPTY_DB; } } else if (jsonparse_strcmp_value(&parser, "DeleteID") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); delete_id = jsonparse_get_value_as_int(&parser); delete_len = jsonparse_get_len(&parser); } } } if (finger_current_mode == FINGER_DELETE && delete_len > 0) { finger_current_mode = FINGER_READ; finger_remove(delete_id, NULL); #if FINGER_DEBUG debug("FINGER: Delete ID: %d\n", delete_id); #endif } if (finger_current_mode == FINGER_EMPTY_DB) { finger_current_mode = FINGER_READ; finger_empty_db(NULL); #if FINGER_DEBUG debug("FINGER: Empty DB\n"); #endif } } webserver_set_status(0); finger_frech_params(); finger_start_read(); }
int ICACHE_FLASH_ATTR cgiConfig(HttpdConnData *connData) { //os_printf("post len=%d %s\n",connData->post->len, connData->post->buff); char data[100]; if (connData->post->len > 4) { struct jsonparse_state j; jsonparse_setup(&j, connData->post->buff, connData->post->len); int type; while ( (type = jsonparse_next(&j) ) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&j, "reset") == 0) { start_reset(); os_sprintf(data,"{ \"reset\":\"Device reset in 2 seconds\" }"); httpdSend(connData, data, -1); return HTTPD_CGI_DONE; } if (jsonparse_strcmp_value(&j, "config") == 0) { jsonparse_next(&j); while ( (type = jsonparse_next(&j) ) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&j, "name") == 0) { jsonparse_next(&j); jsonparse_next(&j); jsonparse_copy_value(&j, config.myname, sizeof(config.myname)); } if (jsonparse_strcmp_value(&j, "zoffset") == 0) { jsonparse_next(&j); jsonparse_next(&j); // the following doesn't work with negative numbers //config.zoffset = jsonparse_get_value_as_int(&j); // so we do it the hard way char data[20]; jsonparse_copy_value(&j, data, sizeof(data)); // ok so atoi() doesn't work either config.zoffset = atoi(data); #if 0 if (data[0] == '-') { config.zoffset = 0 - atoi(data+1); } else { config.zoffset = atoi(data); } #endif os_printf("zofffset=%d %08x\n",config.zoffset,config.zoffset); } } } } } } save_config(); } os_sprintf(data,"{ config: { \"name\": \"%s\", \"zoffset\":%d } }",config.myname,config.zoffset); httpdSend(connData, data, -1); return HTTPD_CGI_DONE; }