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
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
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;
}
示例#4
0
LOCAL bool ICACHE_FLASH_ATTR switch1_parse(char *data, uint16 data_len) {
    struct jsonparse_state parser;
    int type;

    int preference = switch1_hardware[1].preference;

    jsonparse_setup(&parser, data, data_len);
    while ((type = jsonparse_next(&parser)) != 0) {
        if (type == JSON_TYPE_PAIR_NAME) {
            if (jsonparse_strcmp_value(&parser, "Relay") == 0) {
                jsonparse_next(&parser);
                jsonparse_next(&parser);
                user_switch1_set(0, jsonparse_get_value_as_sint(&parser));
            } else if (jsonparse_strcmp_value(&parser, "Preference") == 0) {
                jsonparse_next(&parser);
                jsonparse_next(&parser);
                switch1_hardware[1].preference = jsonparse_get_value_as_sint(&parser);
            }
        }
    }

    return (
               preference != switch1_hardware[1].preference
           );
}
示例#5
0
void ICACHE_FLASH_ATTR relay_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;
	int state = relay_state;
	
	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, "Relay") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					state = jsonparse_get_value_as_sint(&parser);
				}
			}
		}
		
		user_relay_set(state);
	}
	
	user_relay_state(response);
}
示例#6
0
LOCAL char * ICACHE_FLASH_ATTR parse_params_pins_set(struct jsonparse_state *jparser, gpio_command_params *out, unsigned int all) {
	int type;
	int pin;
	unsigned int pinmask;
	os_memset(out, 0, sizeof(gpio_command_params));
	while ((type = jsonparse_next(jparser)) != JSON_TYPE_ERROR) {
		if (type == JSON_TYPE_PAIR_NAME) {
			if(jsonparse_strcmp_value(jparser, "all") == 0) {
				pinmask = all;
			} else {
				const int res = strToInt(&jparser->json[jparser->vstart], &pin);
				if(!res || pin < 0 || pin > 31)
					return "Wrong pin";
				pinmask =  (1 << pin);
			}
			jsonparse_next(jparser);
			if(jsonparse_next(jparser) != JSON_TYPE_ERROR) {
				if(jsonparse_strcmp_value(jparser, "1") == 0)
					out->pins_to_set |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "0") == 0)
					out->pins_to_clear |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "x") == 0)
					continue;
				else if(jsonparse_strcmp_value(jparser, "init") == 0)
					out->pins_to_init |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "pullup") == 0)
					out->pins_to_pullup |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "nopull") == 0)
					out->pins_to_nopull |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "disable") == 0)
					out->pins_to_disable |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "rising") == 0)
					out->pins_to_rising |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "falling") == 0)
					out->pins_to_falling |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "both") == 0)
					out->pins_to_both |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "low") == 0)
					out->pins_to_low |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "high") == 0)
					out->pins_to_high |= pinmask;
				else if(jsonparse_strcmp_value(jparser, "read") == 0)
					out->pins_to_read |= pinmask;
				else return "Unsupported action";
			}
		}
	}
	return NULL;
}
示例#7
0
/* load model from a json file */
ContainerRoot*
open_model(const char* filename)
{
	struct jsonparse_state jsonState;

	FILE *new_model_json = fopen(filename, "r");
	fseek(new_model_json, 0L, SEEK_END);
	int modelLength = ftell(new_model_json);
	fseek(new_model_json, 0L, SEEK_SET);

	char *jsonModel = malloc(modelLength + 1);
	int ch;
	bool firstChar = true;
	bool isFirst = true;
	int listLength = 0;

	while ((ch = fgetc(new_model_json)) != EOF) {
		if (firstChar) {
			sprintf(jsonModel, "%c", ch);
			firstChar = false;
		} else {
			sprintf(jsonModel, "%s%c", jsonModel, ch);
		}
	}

	fclose(new_model_json);
	jsonparse_setup(&jsonState, jsonModel, modelLength + 1);

	ContainerRoot * model = JSONKevDeserializer(&jsonState, jsonparse_next(&jsonState));
	free(jsonModel);

	return model;
}
/******************************************************************************
 * FunctionName : jsonparse_object_str
 * Description  : 
 * Parameters   : 
*******************************************************************************/
float ICACHE_FLASH_ATTR uhl_jsonparse_get_value_as_float(struct jsonparse_state *parser) {
	char dst[25];
	dst[0] = 0x00;
	int dst_len = 24;
	float flt_val = 0.0f;
	bool is_negative = false;
	int pos_len = jsonparse_get_len(parser);
	
	//debug("JSON1:type:%d, parserPOS:%d, pos.len:%d\n", parser->vtype, parser->pos, jsonparse_get_len(parser));
	
	if (parser->vtype == JSON_TYPE_ERROR) {
		jsonparse_next(parser);
		is_negative = true;
		pos_len = jsonparse_get_len(parser);
	}

	if (parser->vtype == JSON_TYPE_NUMBER || parser->vtype == JSON_TYPE_PAIR) {
		if (pos_len < dst_len) {
			os_memcpy(dst, parser->json + parser->pos - pos_len, pos_len);
			dst[pos_len] = '\0';
			flt_val = uhl_str2flt(dst);
		}
	}
	
	if (is_negative)
		return flt_val * -1.0f;
	else
		return flt_val;
}
示例#9
0
/******************************************************************************
 * FunctionName : jsonparse_get_value_as_sint
 * Description  : 
 * Parameters   : 
*******************************************************************************/
int ICACHE_FLASH_ATTR jsonparse_get_value_as_sint(struct jsonparse_state *parser) {
	if (parser->vtype == JSON_TYPE_ERROR) {
		jsonparse_next(parser);
		return -jsonparse_get_value_as_int(parser);
	}
	return jsonparse_get_value_as_int(parser);
}
示例#10
0
LOCAL int ICACHE_FLASH_ATTR msg_set(struct jsontree_context *js_ctx, struct jsonparse_state *parse)
{
    int type;
    while ((type = jsonparse_next(parse)) != 0)
    {
        if (jsonparse_strcmp_value(parse,"v") == 0)
        {
            int version=0;
            jsonparse_next(parse);
            jsonparse_next(parse);
            version = jsonparse_get_value_as_int(parse);
            os_printf("version : %d \r\n",version);
        }
    }
    return 0;
}
示例#11
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 char * ICACHE_FLASH_ATTR readUIntField(struct jsonparse_state *jparser, ALLOWED_FIELDS field, uint32_t *out, ALLOWED_FIELDS fields, ALLOWED_FIELDS *readedfields, unsigned int x) {
	if((fields & field) == 0)
		return UNEXPECTED;
	int value;
	jsonparse_next(jparser);
	if(jsonparse_next(jparser) != JSON_TYPE_ERROR) {
		if(x && jparser->vlen == 1 && jparser->json[jparser->vstart] == 'x' && (field & AF_CS)) {
			*out = x;
			*readedfields |= field;
			return 0;
		}
		const int res = strToUInt(&jparser->json[jparser->vstart], &value);
		if(!res)
			return NONINTEGER;
		*out = value;
		*readedfields |= field;
	}
	return 0;
}
示例#13
0
static int ICACHE_FLASH_ATTR
scanres_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
{
    int type;

    while ((type = jsonparse_next(parser)) != 0)
    {
        if (type == JSON_TYPE_PAIR_NAME)
        {
            if (jsonparse_strcmp_value(parser, "page") == 0)
            {
                jsonparse_next(parser);
                jsonparse_next(parser);
                param.pagenum = jsonparse_get_value_as_int(parser);
                return 0;
            }
        }
    }
    return 0;
}
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;
}
示例#15
0
Status FindPair(const char* jsonString, const char* name, struct jsonparse_state* state)
{
	jsonparse_setup(state, jsonString, strlen(jsonString));
	int nameLength = strlen(name);
	int next;
	do {
		next = jsonparse_next(state);
		if ((next == JSON_TYPE_PAIR_NAME) && (state->vlen == nameLength))
		{
			if (strncmp(name, jsonString + state->vstart, nameLength) == 0)
			{
				next = jsonparse_next(state);
				if (next == JSON_TYPE_PAIR)
				{
					jsonparse_next(state);
					return OK;
				}
			}
		}
	} while (next != 0);
	return JSON_NAME_NOT_FOUND;
}
示例#16
0
// ----------------------------------------------------------------------------
// 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);
            }
        }
示例#17
0
/******************************************************************************
 * FunctionName : jsonparse_object
 * Description  : 
 * Parameters   : 
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR jsonparse_object(struct jsonparse_state *parser, int *pos, int *len) {
	*pos = parser->pos;
	*len = 0;
	
	int depth = parser->depth;
	int json_type = jsonparse_get_type(parser);
	
	while (json_type != '}' && parser->depth != depth-1) {
		json_type = jsonparse_next(parser);
	}
	
	if (json_type == '}') {
		*len = parser->pos - *pos + 1;
	}
}
示例#18
0
bool Contains(const char* jsonString, const char* name, const char* value)
{
	struct jsonparse_state state;
	Status status = FindPair(jsonString, name, &state);
	if ((status == OK) /*&& (state.vtype == JSON_TYPE_ARRAY)*/)
	{
		int valueLength = strlen(value);
		int next;
		do {
			next = jsonparse_next(&state);
			if ((valueLength == state.vlen) && (strncmp(jsonString + state.vstart, value, valueLength) == 0))
			{
				return TRUE;
			}
		} while (next != 0);
	}
	return FALSE;
}
示例#19
0
void ICACHE_FLASH_ATTR mod_tc_mk2_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	i2c_status status;
	i2c_config *config = i2c_init_handler("MOD-TC-MK2", MOD_TC_MK2_URL, tc_init, url, response);
	if (config == NULL) {
		return;
	}
	
	tc_config_data *config_data = (tc_config_data *)config->data;
	
	struct jsonparse_state parser;
	int type;

	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, "Refresh") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					tc_refresh = jsonparse_get_value_as_int(&parser) * 1000;
				} else if (jsonparse_strcmp_value(&parser, "Each") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					tc_each = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Threshold") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					tc_threshold = jsonparse_get_value_as_int(&parser);
				}
			}
		}
		
		mod_tc_mk2_timer_init();
	}
	
	mod_tc_mk2_read(config, response, true);
}
示例#20
0
void ICACHE_FLASH_ATTR adc_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;

	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, "Refresh") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					adc_refresh = jsonparse_get_value_as_int(&parser) * 1000;
				} else if (jsonparse_strcmp_value(&parser, "Each") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					adc_each = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Threshold") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					adc_threshold = jsonparse_get_value_as_int(&parser);
				}
			}
		}
		
		user_adc_timer_init();
	}

	adc_read(response, true);
}
示例#21
0
int ICACHE_FLASH_ATTR cgiApi(HttpdConnData *connData)
{
	struct ApiData *api = (struct ApiData *)connData->cgiPrivData;
	int nbytes, status;
	char type;

	if (connData->conn == NULL) {
		goto done;
	}

	if (connData->requestType != HTTPD_METHOD_POST) {
		httpdStartResponse(connData, 501);
		httpdHeader(connData, "Content-Type", "text/html");
		httpdEndHeaders(connData);
		return HTTPD_CGI_DONE;
	}

	if (connData->post->received <= MAX_POST) {
		api = malloc(sizeof(struct ApiData));
		jsonparse_setup(&api->state, api->buff, MAX_POST);
		api->buffLen = 0;
		api->depth = 0;
		api->status = API_OK;

		connData->cgiPrivData = api;
	}

	if (api->status < API_OK) {
		goto finish;
	}

	while (true) {
		nbytes = API_MAX_BUFF - api->buffLen - 1;
		nbytes = connData->post->buffLen < nbytes ? connData->post->buffLen : nbytes;
		memcpy(&api->buff[api->buffLen], connData->post->buff, nbytes);
		api->buffLen += nbytes;
		memmove(connData->post->buff, &connData->post->buff[nbytes], connData->post->buffLen - nbytes);
		connData->post->buffLen -= nbytes;
		if (connData->post->buffLen == 0) {
			if (connData->post->received < connData->post->len) {
				return HTTPD_CGI_MORE;
			}
			break;
		}
		while (api->state.pos < API_MIN_CHUNK) {
			type = jsonparse_next(&api->state);
			if (type == ',') {
				/* do nothing */
			} else if (type == '[') {
				api->depth = api->state.depth;
			} else {
				api->status = API_ERROR_PARSE;
				goto finish;
			}
			if ((status = api_parse(&api->state)) != API_OK) {
				api->status = status;
				if (status < API_OK) {
					goto finish;
				}
			}
			while (api->state.depth > api->depth) {
				if (!jsonparse_next(&api->state)) {
					api->status = API_ERROR_PARSE;
					goto finish;
				}
			}
		}
		memmove(api->buff, &api->buff[api->state.pos], api->buffLen - api->state.pos);
		api->buffLen -= api->state.pos;
		api->state.pos = 0;
	}

	api->buff[api->buffLen] = '\0';
	api->state.len = api->buffLen;
	while (true) {
		type = jsonparse_next(&api->state);
		if (type == ',') {
			/* do nothing */
		} else if (type == '[') {
			api->depth = api->state.depth;
		} else {
			if (api->state.error != JSON_ERROR_OK) {
				api->status = API_ERROR_PARSE;
			}
			goto finish;
		}
		if ((status = api_parse(&api->state)) != API_OK) {
			api->status = status;
			if (status < API_OK) {
				goto finish;
			}
		}
		while (api->state.depth > api->depth) {
			if (!jsonparse_next(&api->state)) {
				api->status = API_ERROR_PARSE;
				goto finish;
			}
		}
	}

finish:
	if (connData->post->received < connData->post->len) {
		return HTTPD_CGI_MORE;
	}

	if (api->status < API_OK) {
		httpdStartResponse(connData, 500);
		httpdHeader(connData, "Content-Type", "text/html");
		httpdEndHeaders(connData);
		goto done;
	}

	httpdStartResponse(connData, 200);
	httpdHeader(connData, "Content-Type", "application/json");
	httpdEndHeaders(connData);
	httpdSend(connData, api->status == API_OK ? "true" : "false", -1);

	if (api_update) {
		api_update = false;
		master_update(true);
	}

	if (status_dirty) {
		status_save();
	}

done:
	free(api);
	connData->cgiPrivData = NULL;
	return HTTPD_CGI_DONE;
}
示例#22
0
void ICACHE_FLASH_ATTR mod_irda_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	i2c_status status = I2C_OK;
	i2c_config *config = i2c_init_handler(MOD_IRDA, MOD_IRDA_URL, irda_init, url, response);
	if (config == NULL) {
		return;
	}
	
	irda_config_data *config_data = (irda_config_data *)config->data;
	
	struct jsonparse_state parser;
	int type;
	
	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, "Mode") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					if (jsonparse_strcmp_value(&parser, "SIRC") == 0) {
						config_data->mode = 1;
					} else {
						config_data->mode = 0;
					}
				} else if (jsonparse_strcmp_value(&parser, "Device") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->device = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Command") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->command = jsonparse_get_value_as_int(&parser);
				}
			}
		}
		
		status = irda_set(config);
	}
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	if (status == I2C_OK) {
		char data_str[WEBSERVER_MAX_VALUE];
		json_data(
			response, MOD_IRDA, OK_STR,
			json_sprintf(
				data_str,
				"\"Mode\" : \"%s\", "
				"\"Device\" : %d, "
				"\"Command\" : %d ",
				irda_mode_str(config_data->mode), 
				config_data->device,
				config_data->command
			),
			address_str
		);
	} else {
		json_error(response, MOD_IRDA, i2c_status_str(status), address_str);
	}
}
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);
}
示例#24
0
void ICACHE_FLASH_ATTR dimmer_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	i2c_status status = I2C_OK;
	i2c_config *config = i2c_init_handler(DIMMER_STR, DIMMER_URL, dimmer_init, url, response);
	if (config == NULL) {
		return;
	}
	
	dimmer_config_data *config_data = (dimmer_config_data *)config->data;
	
	struct jsonparse_state parser;
	int type;

	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, "Relay") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->relay = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Brightness") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->brightness = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Refresh") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					dimmer_refresh = jsonparse_get_value_as_int(&parser) * 1000;
				} else if (jsonparse_strcmp_value(&parser, "Each") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					dimmer_each = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "Threshold") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					dimmer_threshold = jsonparse_get_value_as_int(&parser);
				}
			}
		}
		
		dimmer_timer_init();
		status = dimmer_set(config);
	}
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	if (status == I2C_OK) {
		dimmer_response(config, response, true);
	} else {
		json_error(response, DIMMER_STR, i2c_status_str(status), address_str);
	}
}
char * ICACHE_FLASH_ATTR parse_params_pins_set(const char *params, unsigned int paramslen, gpio_command_params *out, unsigned int all, unsigned int timeout, ALLOWED_FIELDS fields, ALLOWED_FIELDS *readedfields) {
	struct jsonparse_state jparser;
	if(paramslen)
		jsonparse_setup(&jparser, params, paramslen);
	else if(fields)
		return "No parameters specified";
	int type;
	int pinnum;
	unsigned int pins_found = 0;
	unsigned int pinmask;
	*readedfields = 0;
	load_defaults(out, timeout);
	while (jparser.pos < jparser.len) {
		type = jsonparse_next(&jparser);
		if (type == JSON_TYPE_PAIR_NAME) {
			if(strcmp_value(&jparser, "mode") == 0) {
				if((fields & AF_UARTMODE) == 0 && (fields & AF_SPIMODE) == 0)
					return UNEXPECTED;
				jsonparse_next(&jparser);
				if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) {
					int val;
					if(strcmp_value(&jparser, "disable") == 0 && (fields & AF_UARTMODE)) {
						*readedfields |= AF_UARTMODE;
						out->uart_speed = 0;
						continue;
					}
					int res = strToUInt(&jparser.json[jparser.vstart], &val);
					if(!res)
						return "Wrong mode integer value";
					if(fields & AF_UARTMODE) {
						*readedfields |= AF_UARTMODE;
						out->uart_speed = val;
						if(res < jparser.vlen && jparser.json[jparser.vstart + res] == ' ') {
							while(res < jparser.vlen && jparser.json[jparser.vstart + res] == ' ')
								res++;
							if(res + 3 == jparser.vlen) {
								const unsigned char b =  jparser.json[jparser.vstart + res] - '0';
								const unsigned char p =  jparser.json[jparser.vstart + res + 1];
								const unsigned char s =  jparser.json[jparser.vstart + res + 2] - '0';
								if(b < 10 && s < 10) {
									out->uart_bits = b;
									out->uart_partity = p;
									out->uart_stopbits = s;
								} else {
									return "Wrong mode framing";
								}
							}
						}
					}
					if(fields & AF_SPIMODE) {
						*readedfields |= AF_SPIMODE;
						out->spi_mode = val;
					}
				}
				continue;
			} else if(strcmp_value(&jparser, "count") == 0) {
				char * res = readUIntField(&jparser, AF_COUNT, &out->count, fields, readedfields, 0);
				if(res)
					return res;
				continue;
			} else if(strcmp_value(&jparser, "timeout") == 0) {
				char * res = readUIntField(&jparser, AF_TIMEOUT, &out->timeout, fields, readedfields, 0);
				if(res)
					return res;
				continue;
			} else if(strcmp_value(&jparser, "frequency") == 0) {
				if((fields & AF_PERIOD) == 0)
					return UNEXPECTED;
				float frequncy;
				jsonparse_next(&jparser);
				if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) {
					const int res = strToFloat(&jparser.json[jparser.vstart], &frequncy);
					if(!res)
						return "Wrong frequency float value";
					if(frequncy < 0.000499999f)
						out->periodus = 2000004000;
					else
						out->periodus = 1000000.0f / frequncy;
					*readedfields |= AF_PERIOD;
				}
				continue;
			} else if(strcmp_value(&jparser, "address") == 0) {
				if((fields & AF_ADDRESS) == 0)
					return UNEXPECTED;
				jsonparse_next(&jparser);
				if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) {
					char c;
					int p;
					if(jparser.json[jparser.vstart] == '0' && jparser.json[jparser.vstart + 1] == 'x')
						p = 2;
					else
						p = 0;
					const int res = hexToByte(&jparser.json[jparser.vstart + p], &c);
					if(res != (jparser.vlen - p))
						return "Address is wrong";
					out->address = c;
					*readedfields |= AF_ADDRESS;
				}
				continue;
			} else if(strcmp_value(&jparser, "SDA") == 0) {
				char * res = readUIntField(&jparser, AF_SDA, &out->SDA, fields, readedfields, 0);
				if(res)
					return res;
				continue;
			} else if(strcmp_value(&jparser, "SCL") == 0) {
				char * res = readUIntField(&jparser, AF_SCL, &out->SCL, fields, readedfields, 0);
				if(res)
					return res;
				continue;
			} else if(strcmp_value(&jparser, "CS") == 0) {
				char * res = readUIntField(&jparser, AF_CS, &out->CS, fields, readedfields,  ~(uint32_t)0U);
				if(res)
					return res;
				continue;
			} else if(strcmp_value(&jparser, "pin") == 0) {
				char * res = readUIntField(&jparser, AF_PIN, &out->pin, fields, readedfields, 0);
				if(res)
					return res;
				continue;
			} else if(strcmp_value(&jparser, "data") == 0) {
				if((fields & AF_DATA) == 0 || out->data_len)
					return UNEXPECTED;
				jsonparse_next(&jparser);
				if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) {
					out->data_len = dhdata_decode(&jparser.json[jparser.vstart], jparser.vlen, out->data, sizeof(out->data));
					if(out->data_len == 0)
						return "Data is broken";
					*readedfields |= AF_DATA;
				}
				continue;
			}  else if(strcmp_value(&jparser, "text") == 0) {
				if((fields & AF_TEXT_DATA) == 0 || out->data_len)
					return UNEXPECTED;
				jsonparse_next(&jparser);
				if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) {
					if (jparser.vlen > sizeof(out->data) - 1)
						return "Text is too long";
					os_memcpy(out->data, &jparser.json[jparser.vstart], jparser.vlen);
					out->data[jparser.vlen] = 0;
					out->data_len = jparser.vlen;
					*readedfields |= AF_TEXT_DATA;
				}
				continue;
			} else if(strcmp_value(&jparser, "all") == 0) {
				if(pins_found)
					return "Wrong argument";
				pins_found = ~(unsigned int)0;
				pinmask = all;
				pinnum = -1;
			} else {
				const int res = strToUInt(&jparser.json[jparser.vstart], &pinnum);
				if(!res || pinnum < 0 || pinnum > DHGPIO_MAXGPIONUM || (pins_found & (1 << pinnum)))
					return "Wrong argument";
				pins_found |= (1 << pinnum);
				pinmask =  (1 << pinnum);
			}
			jsonparse_next(&jparser);
			if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) {
				if(strcmp_value(&jparser, "x") == 0)
					continue;
				else if(strcmp_value(&jparser, "init") == 0) {
					if((fields & AF_INIT) == 0)
						return UNEXPECTED;
					out->pins_to_init |= pinmask;
					*readedfields |= AF_INIT;
				} else if(strcmp_value(&jparser, "pullup") == 0) {
					if((fields & AF_PULLUP) == 0)
						return UNEXPECTED;
					out->pins_to_pullup |= pinmask;
					*readedfields |= AF_PULLUP;
				} else if(strcmp_value(&jparser, "nopull") == 0) {
					if((fields & AF_NOPULLUP) == 0)
						return UNEXPECTED;
					out->pins_to_nopull |= pinmask;
					*readedfields |= AF_NOPULLUP;
				} else if(strcmp_value(&jparser, "disable") == 0) {
					if((fields & AF_VALUES) == 0 && (fields & AF_DISABLE) == 0) {
						return UNEXPECTED;
					}
					if (fields & AF_VALUES) {
						int i;
						if(pinnum > 0 )
							out->pin_value[pinnum] = 0;
						else for(i = 0; i <= DHGPIO_MAXGPIONUM; i++)
							out->pin_value[i] = 0;
						out->pin_value_readed |= pinmask;
						*readedfields |= AF_VALUES;
					}
					if(fields & AF_DISABLE) {
						out->pins_to_disable |= pinmask;
						*readedfields |= AF_DISABLE;
					}
				} else if(strcmp_value(&jparser, "rising") == 0) {
					if((fields & AF_RISING)== 0)
						return UNEXPECTED;
					out->pins_to_rising |= pinmask;
					*readedfields |= AF_RISING;
				} else if(strcmp_value(&jparser, "falling") == 0) {
					if((fields & AF_FALLING) == 0)
						return UNEXPECTED;
					out->pins_to_falling |= pinmask;
					*readedfields |= AF_FALLING;
				} else if(strcmp_value(&jparser, "both") == 0) {
					if((fields & AF_BOTH) == 0)
						return UNEXPECTED;
					out->pins_to_both |= pinmask;
					*readedfields |= AF_BOTH;
				} else if(strcmp_value(&jparser, "read") == 0) {
					if((fields & AF_READ) == 0)
						return UNEXPECTED;
					out->pins_to_read |= pinmask;
					*readedfields |= AF_READ;
				} else if(strcmp_value(&jparser, "presence") == 0) {
					if((fields & AF_PRESENCE) == 0)
						return UNEXPECTED;
					out->pins_to_presence |= pinmask;
					*readedfields |= AF_PRESENCE;
				} else if((fields & AF_VALUES)) { // BE CAREFULL, all digits values have to be under this if
					int value, i;
					if(!strToUInt(&jparser.json[jparser.vstart], &value))
						return NONINTEGER;
					if(pinnum > 0 )
						out->pin_value[pinnum] = value;
					else for(i = 0; i <= DHGPIO_MAXGPIONUM; i++)
						out->pin_value[i] = value;
					out->pin_value_readed |= pinmask;
					*readedfields |= AF_VALUES;
					if(value == 1 && (fields & AF_SET)) {
						out->pins_to_set |= pinmask;
						*readedfields |= AF_SET;
					} else if(value == 0 && (fields & AF_CLEAR)) {
						out->pins_to_clear |= pinmask;
						*readedfields |= AF_CLEAR;
					}
				} else if(strcmp_value(&jparser, "1") == 0) {
					if((fields & AF_SET) == 0)
						return UNEXPECTED;
					out->pins_to_set |= pinmask;
					*readedfields |= AF_SET;
				} else if(strcmp_value(&jparser, "0") == 0) {
					if((fields & AF_CLEAR) == 0)
						return UNEXPECTED;
					out->pins_to_clear |= pinmask;
					*readedfields |= AF_CLEAR;
				} else {
					return "Unsupported action";
				}
			}
		} else if(type == JSON_TYPE_ERROR) {
			return "Broken json";
		}
	}
	return NULL;
}
示例#26
0
void ICACHE_FLASH_ATTR mod_rgb_handler(
	struct espconn *pConnection, 
	request_method method, 
	char *url, 
	char *data, 
	uint16 data_len, 
	uint32 content_len, 
	char *response,
	uint16 response_len
) {
	i2c_status status;
	i2c_config *config = i2c_init_handler(MOD_RGB, MOD_RGB_URL, rgb_init, url, response);
	if (config == NULL) {
		return;
	}
	
	rgb_config_data *config_data = (rgb_config_data *)config->data;
	
	struct jsonparse_state parser;
	int type;
	
	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, "R") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->red = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "G") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->green = jsonparse_get_value_as_int(&parser);
				} else if (jsonparse_strcmp_value(&parser, "B") == 0) {
					jsonparse_next(&parser);
					jsonparse_next(&parser);
					config_data->blue = jsonparse_get_value_as_int(&parser);
				}
			}
		}
	}
	
	char address_str[MAX_I2C_ADDRESS];
	json_i2c_address(address_str, config->address);
	
	status = rgb_set(config);
	if (status == I2C_OK) {
		char data_str[WEBSERVER_MAX_VALUE];
		json_data(
			response, MOD_RGB, OK_STR,
			json_sprintf(
				data_str,
				"\"R\" : %d, \"G\" : %d, \"B\" : %d",
				config_data->red, 
				config_data->green, 
				config_data->blue
			),
			address_str
		);
	} else {
		json_error(response, MOD_RGB, i2c_status_str(status), address_str);
	}
}
示例#27
0
int ICACHE_FLASH_ATTR cgiSched(HttpdConnData *connData) {
	struct sched_entry *s = config.scheds;

	os_printf("post len=%d %s\n",connData->post->len, connData->post->buff);

	if (connData->post->len > 10)
	{

		set_all_relays_off();

		config.sched_count = 0;
		s = config.scheds;

		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, "schedules") == 0) {
					jsonparse_next(&j);
					while ( (type = jsonparse_next(&j) ) != 0)
					{
						if (type == JSON_TYPE_ARRAY || type == ',')
						{
							jsonparse_next(&j);
							while ( (type = jsonparse_next(&j) ) != 0)
							{
								if (type == '}')
								{
									s++;
									config.sched_count++;
									break;
								}
								if (type == JSON_TYPE_PAIR_NAME) {
									if (jsonparse_strcmp_value(&j, "zone") == 0) {
										jsonparse_next(&j);
										jsonparse_next(&j);
										s->zone = jsonparse_get_value_as_int(&j);
									}
									if (jsonparse_strcmp_value(&j, "start") == 0) {
										jsonparse_next(&j);
										jsonparse_next(&j);
										s->start = jsonparse_get_value_as_int(&j);
									}
									if (jsonparse_strcmp_value(&j, "end") == 0) {
										jsonparse_next(&j);
										jsonparse_next(&j);
										s->end = jsonparse_get_value_as_int(&j);
									}
									if (jsonparse_strcmp_value(&j, "time") == 0) {
										jsonparse_next(&j);
										jsonparse_next(&j);
										s->time = jsonparse_get_value_as_int(&j);
									}
									if (jsonparse_strcmp_value(&j, "duration") == 0) {
										jsonparse_next(&j);
										jsonparse_next(&j);
										s->duration = jsonparse_get_value_as_int(&j);
									}
									if (jsonparse_strcmp_value(&j, "repeat") == 0) {
										jsonparse_next(&j);
										jsonparse_next(&j);
										s->repeat = jsonparse_get_value_as_int(&j);
									}
									if (jsonparse_strcmp_value(&j, "dow") == 0) {
										jsonparse_next(&j);
										jsonparse_next(&j);
										s->dow = jsonparse_get_value_as_int(&j);
									}
								}
							}
						}
					}
				}
			}
		}
		save_config();
		compute_times();
	}

	char *data = (char *)alloca(100*config.sched_count);
	char *p = data;
	s = config.scheds;
	int i;
	os_sprintf(p,"{ \"name\": \"%s\", \"time\": %d, \"schedules\":[ ",config.myname,time());
	p += strlen(p);
	for (i = config.sched_count, s = config.scheds; i > 0; i--, s++)
	{
		os_sprintf(p,"{\"zone\":%d,\"start\":%d,\"end\":%d,\"time\":%d,\"duration\":%d,\"repeat\":%d,\"dow\":%d},",
				s->zone,s->start,s->end,s->time,s->duration,s->repeat,s->dow);
		p += strlen(p);
	}
	p--;
	*p++ = ']';
	*p++ = '}';
	*p++ = '\0';
	httpdSend(connData, data, -1);
	return HTTPD_CGI_DONE;
}
示例#28
0
void ICACHE_FLASH_ATTR dhcommand_do(int id, const char *command, const char *params, unsigned int paramslen) {
	struct jsonparse_state jparser;
	gpio_command_params parse_pins;
	char *parse_res;
	int interuption;
	if(paramslen)
		jsonparse_setup(&jparser, params, paramslen);
	if( os_strcmp(command, "gpio/write") == 0 ) {
		if(paramslen == 0)
			dhsender_response(id, STATUS_ERROR, "No parameters specified");
		parse_res = parse_params_pins_set(&jparser, &parse_pins, DHGPIO_SUITABLE_PINS);
		if (parse_res)
			dhsender_response(id, STATUS_ERROR, parse_res);
		else if (parse_pins.pins_to_set == 0 && parse_pins.pins_to_clear == 0)
			dhsender_response(id, STATUS_ERROR, "Wrong action");
		else if(dhgpio_write(parse_pins.pins_to_set, parse_pins.pins_to_clear))
			dhsender_response(id, STATUS_OK, "");
		else
			dhsender_response(id, STATUS_ERROR, "Unsuitable pin");
	} else if( os_strcmp(command, "gpio/read") == 0 ) {
		int init = 1;
		if(paramslen) {
			parse_res = parse_params_pins_set(&jparser, &parse_pins, DHGPIO_SUITABLE_PINS);
			if (parse_res) {
				dhsender_response(id, STATUS_ERROR, parse_res);
				init = 0;
			} else {
				init = dhgpio_init(parse_pins.pins_to_init, parse_pins.pins_to_pullup, parse_pins.pins_to_nopull);
			}
		}
		if (init) {
			char gpiostatebuff[192]; // 16 gpio inputs with '"10":"1", ' format - 10*16=128 + {,}, null terminated char etc
			dhgpio_read_to_json(gpiostatebuff, dhgpio_read());
			dhsender_response(id, STATUS_OK, gpiostatebuff);
		} else {
			dhsender_response(id, STATUS_ERROR, "Wrong initialization parameters");
		}
	} else if( os_strcmp(command, "gpio/int") == 0 ) {
		if(paramslen == 0)
			dhsender_response(id, STATUS_ERROR, "No parameters specified");
		parse_res = parse_params_pins_set(&jparser, &parse_pins, DHGPIO_SUITABLE_PINS);
		if (parse_res)
			dhsender_response(id, STATUS_ERROR, parse_res);
		else if (parse_pins.pins_to_disable == 0 && parse_pins.pins_to_rising == 0 && parse_pins.pins_to_falling == 0 \
				&& parse_pins.pins_to_both == 0 && parse_pins.pins_to_low == 0 && parse_pins.pins_to_high == 0)
			dhsender_response(id, STATUS_ERROR, "Wrong action");
		else if(dhgpio_int(parse_pins.pins_to_disable, parse_pins.pins_to_rising, parse_pins.pins_to_falling, \
				parse_pins.pins_to_both, parse_pins.pins_to_low, parse_pins.pins_to_high))
			dhsender_response(id, STATUS_OK, "");
		else
			dhsender_response(id, STATUS_ERROR, "Unsuitable pin");
	} else if( os_strcmp(command, "adc/read") == 0) {
		if(paramslen) {
			parse_res = parse_params_pins_set(&jparser, &parse_pins, DHADC_SUITABLE_PINS);
			if (parse_res) {
				dhsender_response(id, STATUS_ERROR, parse_res);
				return;
			} else if (parse_pins.pins_to_read != DHADC_SUITABLE_PINS) {
				dhsender_response(id, STATUS_ERROR, "Unknown ADC channel");
				return;
			}
		}
		char adcvalue[48];
		snprintf(adcvalue, sizeof(adcvalue), "{\"0\":\"%f\"}", dhadc_get_value()/1024.0f);
		dhsender_response(id, STATUS_OK, adcvalue);
	} else if( os_strcmp(command, "adc/int") == 0) {
		if(paramslen) {
			int type;
			while ((type = jsonparse_next(&jparser)) != JSON_TYPE_ERROR) {
				if (type == JSON_TYPE_PAIR_NAME) {
					if(jsonparse_strcmp_value(&jparser, "0") == 0) {
						jsonparse_next(&jparser);
						if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) {
							int period;
							if(jsonparse_strcmp_value(&jparser, "disable") == 0) {
								period = 0;
							} else {
								const int res = strToInt(&jparser.json[jparser.vstart], &period);
								if(!res || period < 50 || period > 0x7fffff) {
									dhsender_response(id, STATUS_ERROR, "Wrong period");
									return;
								}
							}
							dhnotification_adc(period);
							dhsender_response(id, STATUS_OK, "");
							return;
						}
					}
				}
			}
		}
		dhsender_response(id, STATUS_ERROR, "Wrong parameters");
	} else {
		dhsender_response(id, STATUS_ERROR, "Unknown command");
	}
}
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);
    }
}
示例#30
0
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();
}