/** for data xchange channel */ bool __plugin_pm_sleep(void *class_ptr, char *method_name, void *input_pack) { uint8_t *arg_ptr = (uint8_t *)input_pack; uint32_t sec; memcpy(&sec, arg_ptr, sizeof(uint32_t)); arg_ptr += sizeof(uint32_t); Serial1.printf("deep sleep %lu sec\r\n", sec); writer_print(TYPE_STRING, "\"ok, deep sleep\""); response_msg_close(STREAM_DATA); delay(100); //turn off grove's power digitalWrite(SWITCH_GROVE_POWER, 0); system_deep_sleep(sec * 1000000); return true; }
bool __plugin_pm_power_the_groves(void *class_ptr, char *method_name, void *input_pack) { uint8_t *arg_ptr = (uint8_t *)input_pack; uint8_t onoff; memcpy(&onoff, arg_ptr, sizeof(uint8_t)); arg_ptr += sizeof(uint8_t); Serial1.printf("power the groves: %d\r\n", onoff); if (onoff) { if (_grove_system_power_on) { return true; } digitalWrite(SWITCH_GROVE_POWER, 1); resource_t *p_res = p_first_resource; while (p_res) { if (p_res->rw == METHOD_INTERNAL && strncmp("?poweron", p_res->method_name, 9) == 0) { p_res->method_ptr(p_res->class_ptr, p_res->method_name, NULL); } p_res = p_res->next; delay(0); } //writer_print(TYPE_STRING, "\"ok, power on groves.\""); _grove_system_power_on = true; } else { if (_grove_system_power_on == false) { return true; } resource_t *p_res = p_first_resource; while (p_res) { if (p_res->rw == METHOD_INTERNAL && strncmp("?poweroff", p_res->method_name, 10) == 0) { if (!p_res->method_ptr(p_res->class_ptr, p_res->method_name, NULL)) { writer_print(TYPE_STRING, "\""); writer_print(TYPE_STRING, p_res->grove_name); writer_print(TYPE_STRING, " not support power down-n-up.\""); return false; } } p_res = p_res->next; delay(0); } //writer_print(TYPE_STRING, "\"ok, power off groves.\""); digitalWrite(SWITCH_GROVE_POWER, 0); _grove_system_power_on = false; } return true; }
void rpc_server_loop() { //writer_print(TYPE_INT, &parse_stage); switch (parse_stage) { case PARSE_GET_POST: { bool parsed_get_post = false; buff[0] = buff[1]; buff[1] = buff[2]; buff[2] = buff[3]; buff[3] = stream_read(); //printf(" %s\r\n", buff); if (memcmp(buff, "GET", 3) == 0 || memcmp(buff, "get", 3) == 0) { req_type = REQ_GET; parsed_get_post = true; } if (memcmp(buff, "POST", 4) == 0 || memcmp(buff, "post", 4) == 0) { req_type = REQ_POST; parsed_get_post = true; stream_read(); //read " " out } if(parsed_get_post) { ch = stream_read(); if (ch != '/') { //error request format writer_print(TYPE_STRING, "BAD REQUEST: missing root:'/'."); } else { parse_stage = PARSE_GROVE_NAME; p_resource = NULL; offset = 0; } } break; } case PARSE_GROVE_NAME: { ch = stream_read(); if(ch == '\r' || ch == '\n') { //TODO: get /.well-known buff[offset] = '\0'; if(strcmp(buff, ".well-known") == 0) { writer_print(TYPE_STRING, "/.well-known is not implemented"); parse_stage = PARSE_GET_POST; }else { writer_print(TYPE_STRING, "BAD REQUEST: missing method name."); parse_stage = PARSE_GET_POST; } } else if (ch != '/' && offset <= 31) { buff[offset++] = ch; }else { buff[offset] = '\0'; memcpy(grove_name, buff, offset+1); while (ch != '/') { ch = stream_read(); } parse_stage = PARSE_METHOD; offset = 0; } break; } case PARSE_METHOD: { ch = stream_read(); if (ch == '\r' || ch == '\n') { buff[offset] = '\0'; memcpy(method_name, buff, offset + 1); if(req_type == REQ_POST) parse_stage = CHECK_POST_ARGS; else parse_stage = PARSE_CALL; } else if (ch == '/') { buff[offset] = '\0'; memcpy(method_name, buff, offset + 1); parse_stage = PRE_PARSE_ARGS; } else if (offset >= 32) { buff[offset] = '\0'; memcpy(method_name, buff, offset + 1); while (ch != '/' && ch != '\r' && ch != '\n') { ch = stream_read(); } if (ch == '\r' || ch == '\n') parse_stage = PARSE_CALL; else parse_stage = PRE_PARSE_ARGS; } else { buff[offset++] = ch; } break; } case CHECK_POST_ARGS: { p_resource = __find_resource((char *)grove_name, (char *)method_name); if (!p_resource) { writer_print(TYPE_STRING, "METHOD NOT FOUND"); parse_stage = PARSE_GET_POST; break; } if (p_resource->arg_types[0] != TYPE_NONE) { writer_print(TYPE_STRING, "MISSING ARGS"); parse_stage = PARSE_GET_POST; break; } parse_stage = PARSE_CALL; break; } case PRE_PARSE_ARGS: { p_resource = __find_resource((char *)grove_name, (char *)method_name); if (!p_resource) { writer_print(TYPE_STRING, "METHOD NOT FOUND"); parse_stage = PARSE_GET_POST; break; } parse_stage = PARSE_ARGS; arg_index = 0; arg_offset = 0; offset = 0; break; } case PARSE_ARGS: { ch = stream_read(); if (ch == '\r' || ch == '\n' || ch == '/') { buff[offset] = '\0'; } else if (offset >= 32) { buff[offset] = '\0'; while (ch != '/' && ch != '\r' && ch != '\n') { ch = stream_read(); } } else { buff[offset++] = ch; } if (ch == '/') { char *p = buff; int len = __convert_arg(arg_buff + arg_offset, &p, p_resource->arg_types[arg_index++]); arg_offset += len; offset = 0; } if (ch == '\r' || ch == '\n') { if ((arg_index<3 && p_resource->arg_types[arg_index+1] != TYPE_NONE) || (arg_index<=3 && p_resource->arg_types[arg_index] != TYPE_NONE && strlen(buff)<1)) { writer_print(TYPE_STRING, "MISSING ARGS"); parse_stage = PARSE_GET_POST; break; } char *p = buff; int len = __convert_arg(arg_buff + arg_offset, &p, p_resource->arg_types[arg_index++]); arg_offset += len; offset = 0; parse_stage = PARSE_CALL; } break; } case PARSE_CALL: { if(!p_resource) p_resource = __find_resource((char *)grove_name, (char *)method_name); if (!p_resource) { writer_print(TYPE_STRING, "METHOD NOT FOUND"); parse_stage = PARSE_GET_POST; break; } writer_print(TYPE_STRING, "{"); p_resource->method_ptr(arg_buff); writer_print(TYPE_STRING, "}"); parse_stage = PARSE_GET_POST; break; } default: break; } }