static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { if (inpkt->payload.len == 0) return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); if (inpkt->payload.p[0] == '1') { light = '1'; #ifdef ARDUINO digitalWrite(led, HIGH); #else printf("ON\n"); #endif return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); } else { light = '0'; #ifdef ARDUINO digitalWrite(led, LOW); #else printf("OFF\n"); #endif return coap_make_response(scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); } }
static int handle_post_command(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { if (inpkt->payload.len == 0) return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); if (inpkt->payload.len > 0) { lua_Load *load = &gLoad; if(load->line_position == 0){ coap_buffer_to_string(load->line, load->len,&inpkt->payload); load->line_position = c_strlen(load->line)+1; // load->line[load->line_position-1] = '\n'; // load->line[load->line_position] = 0; // load->line_position++; load->done = 1; NODE_DBG("Get command:\n"); NODE_DBG_(load->line); // buggy here NODE_DBG("\nResult(if any):\n"); /* os_timer_disarm(&lua_timer); os_timer_setfn(&lua_timer, (os_timer_func_t *)dojob, load); os_timer_arm(&lua_timer, READLINE_INTERVAL, 0); // no repeat */ set_lua_dojob(load); } return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); } }
static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { const coap_option_t *opt; uint8_t count; int n; if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count))) { if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable] { NODE_DBG("should never happen.\n"); goto end; } if (count == ep->path->count + 1) { coap_luser_entry *h = ep->user_entry->next; // ->next: skip the first entry(head) while(NULL != h){ if (opt[count-1].buf.len != c_strlen(h->name)) { h = h->next; continue; } if (0 == c_memcmp(h->name, opt[count-1].buf.p, opt[count-1].buf.len)) { NODE_DBG("/v1/v/"); NODE_DBG_((char *)h->name); NODE_DBG(" match.\n"); if(h->L == NULL) return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); if(c_strlen(h->name)) { n = lua_gettop(h->L); lua_getglobal(h->L, h->name); if (!lua_isnumber(h->L, -1)) { NODE_DBG ("should be a number.\n"); lua_settop(h->L, n); return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); } else { const char *res = lua_tostring(h->L,-1); lua_settop(h->L, n); return coap_make_response(scratch, outpkt, (const uint8_t *)res, c_strlen(res), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); } } } else { h = h->next; } } }else{ NODE_DBG("/v1/v match.\n"); goto end; } } NODE_DBG("none match.\n"); end: return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); }
static int handle_put_lightB2(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { if (inpkt->payload.len == 0) return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); lightB2 = atoi(inpkt->payload.p); stripSet2(lightR2, lightG2, lightB2); char tmp[30]; sprintf(tmp,"%d",lightB2); return coap_make_response(scratch, outpkt, (const uint8_t *)tmp, strlen(tmp), id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); }
////////////////////////////////////////////////////////////////////////// // Method function: put_request ////////////////////////////////////////////////////////////////////////// static int handle_put_request(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { if (inpkt->payload.len == 0) return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); strncpy(rqst,(const char *)&inpkt->payload.p[0],inpkt->payload.len); // Process request & get reply string ProcessCoAPrequest(rqst,&replystring[0]); return coap_make_response(scratch, outpkt, (const uint8_t *)&replystring[0], strlen(replystring), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); }
static int handle_put_hello(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { if (inpkt->payload.len == 0) return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); if (inpkt->payload.p[0] == '1') { retHello = '1'; return coap_make_response(scratch, outpkt, (const uint8_t *)&retHello, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); } else { retHello = '0'; return coap_make_response(scratch, outpkt, (const uint8_t *)&retHello, 1, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); } }
static int handle_GET_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { char response[16]; dtostrf(analogRead(A0)*100.0/1023.0, 5, 2, response); return coap_make_response(scratch, outpkt, (const uint8_t *)response, strlen(response), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); }
static int handle_GET_humidity(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { char response[16]; int chk = DHT.read22(DHT22PIN); switch (chk) { case DHTLIB_OK: // 1H30 de travail : pour trouver la seule fonction du monde en AVR capable de transormer un double en string dtostrf(DHT.humidity, 5, 2, response); break; case DHTLIB_ERROR_CHECKSUM: strncpy(response, "Checksum error", 16); break; case DHTLIB_ERROR_TIMEOUT: strncpy(response, "Time out error", 16); break; case DHTLIB_ERROR_CONNECT: strncpy(response, "Connect error", 16); break; case DHTLIB_ERROR_ACK_L: strncpy(response, "Ack Low error", 16); break; case DHTLIB_ERROR_ACK_H: strncpy(response, "Ack High error", 16); break; default: strncpy(response, "Unknown error", 16); break; } return coap_make_response(scratch, outpkt, (const uint8_t *)response, strlen(response), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); }
// FIXME, if this looked in the table at the path before the method then // it could more easily return 405 errors int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt) { const coap_option_t *opt; uint8_t count; int i; const coap_endpoint_t *ep = endpoints; while(NULL != ep->handler) { if (ep->method != inpkt->hdr.code) goto next; if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count))) { if (count != ep->path->count) goto next; for (i=0;i<count;i++) { if (opt[i].buf.len != strlen(ep->path->elems[i])) goto next; if (0 != memcmp(ep->path->elems[i], opt[i].buf.p, opt[i].buf.len)) goto next; } // match! return ep->handler(scratch, inpkt, outpkt, inpkt->hdr.id[0], inpkt->hdr.id[1]); } next: ep++; } coap_make_response(scratch, outpkt, NULL, 0, inpkt->hdr.id[0], inpkt->hdr.id[1], COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); return 0; }
////////////////////////////////////////////////////////////////////////// // Method function: put_light_blink ////////////////////////////////////////////////////////////////////////// static int handle_put_light_blink(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { int i,nblink; if (inpkt->payload.len == 0) { return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN); } if(inpkt->payload.len<9) { strcpy(light,(const char *)&inpkt->payload.p[0]); } if(numbers_only(light)) { nblink = atoi(inpkt->payload); blinkLed(nblink); //Blink Led nblink times using non-blocking timer } return coap_make_response(scratch, outpkt, (const uint8_t *)&light[0], inpkt->payload.len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN); }
static int handle_get_well_known_core(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { outpkt->content.p = (uint8_t *)c_zalloc(MAX_PAYLOAD_SIZE); // this should be free-ed when outpkt is built in coap_server_respond() if(outpkt->content.p == NULL){ NODE_DBG("not enough memory\n"); return COAP_ERR_BUFFER_TOO_SMALL; } outpkt->content.len = MAX_PAYLOAD_SIZE; build_well_known_rsp(outpkt->content.p, outpkt->content.len); return coap_make_response(scratch, outpkt, (const uint8_t *)outpkt->content.p, c_strlen(outpkt->content.p), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT); }
static int handle_get_riot_board(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { const char *riot_name = RIOT_BOARD; int len = strlen(RIOT_BOARD); memcpy(response, riot_name, len); return coap_make_response(scratch, outpkt, (const uint8_t *)response, len, id_hi, id_lo, &inpkt->token, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN, false); }
static int handle_get_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { return coap_make_response( scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN ); }
static int handle_put_custom(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { if (inpkt->payload.len == 0) { return coap_make_response( scratch, outpkt, NULL, 0, id_hi, id_lo, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN ); } custom = inpkt->payload.p[0]; uint16_t i; xprintf(PSTR("> ")); for (i = 0; i < inpkt->payload.len; ++i) { xprintf(PSTR("%c"),inpkt->payload.p[i]); } xprintf(PSTR("\r\n")); return coap_make_response( scratch, outpkt, (const uint8_t *)&custom, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN ); }
static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { if (inpkt->payload.len == 0) { return coap_make_response( scratch, outpkt, NULL, 0, id_hi, id_lo, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN ); } if (inpkt->payload.p[0] == '1') { light = '1'; led2_high(); } else { light = '0'; led2_low(); } return coap_make_response( scratch, outpkt, (const uint8_t *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN ); }
static int handle_get_well_known_core(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { static char *rsp = "</light>,</custom>"; return coap_make_response( scratch, outpkt, (const uint8_t *)rsp, strlen(rsp), id_hi, id_lo, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ); }
static int handle_post_rgb(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { coap_responsecode_t resp = COAP_RSPCODE_CHANGED; const char *str = (const char *) inpkt->payload.p; if (str[0] == '#') { color_str2rgb(&str[1], &rgb); rgbled_set(&led, &rgb); } return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->token, resp, COAP_CONTENTTYPE_TEXT_PLAIN, false); }
static int handle_post_rgb(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { coap_responsecode_t resp = COAP_RSPCODE_CHANGED; const char *str = (const char *) inpkt->payload.p; if (str[0] == '#') { hex_rgb = (((str[1] > '9') ? (str[1] &~ 0x20) - 'A' + 10 : (str[1] - '0')) << 20) | // R (((str[2] > '9') ? (str[2] &~ 0x20) - 'A' + 10 : (str[2] - '0')) << 16) | // R (((str[3] > '9') ? (str[3] &~ 0x20) - 'A' + 10 : (str[3] - '0')) << 12) | // G (((str[4] > '9') ? (str[4] &~ 0x20) - 'A' + 10 : (str[4] - '0')) << 8) | // G (((str[5] > '9') ? (str[5] &~ 0x20) - 'A' + 10 : (str[5] - '0')) << 4) | // B (((str[6] > '9') ? (str[6] &~ 0x20) - 'A' + 10 : (str[6] - '0')) << 0) ; // B } return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->token, resp, COAP_CONTENTTYPE_TEXT_PLAIN, false); }
static int handle_post_led(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { coap_responsecode_t resp = COAP_RSPCODE_CHANGED; printf("Hello, we got a post request to LED\n"); printf("payload has length %i\n", (int)inpkt->payload.len); uint8_t val = inpkt->payload.p[0]; if ((inpkt->payload.len == 1) && ((val == '1') || (val == '0'))) { gpio_write(LED0_PIN, (val - '1')); printf("LED something: %c\n", (char)val); } else { puts("wrong payload"); resp = COAP_RSPCODE_NOT_ACCEPTABLE; } return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->token, resp, COAP_CONTENTTYPE_TEXT_PLAIN, false); }
////////////////////////////////////////////////////////////////////////// // Method function: get_request ////////////////////////////////////////////////////////////////////////// static int handle_get_request(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { return coap_make_response(scratch, outpkt, (const uint8_t *)&rqst, strlen(rqst), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); }
static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { const coap_option_t *opt; uint8_t count; int n; if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count))) { if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable] { NODE_DBG("should never happen.\n"); goto end; } if (count == ep->path->count + 1) { coap_luser_entry *h = ep->user_entry->next; // ->next: skip the first entry(head) while(NULL != h){ if (opt[count-1].buf.len != c_strlen(h->name)) { h = h->next; continue; } if (0 == c_memcmp(h->name, opt[count-1].buf.p, opt[count-1].buf.len)) { NODE_DBG("/v1/f/"); NODE_DBG_((char *)h->name); NODE_DBG(" match.\n"); if(h->L == NULL) return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); if(c_strlen(h->name)) { n = lua_gettop(h->L); lua_getglobal(h->L, h->name); if (lua_type(h->L, -1) != LUA_TFUNCTION) { NODE_DBG ("should be a function\n"); lua_settop(h->L, n); return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); } else { lua_pushlstring(h->L, inpkt->payload.p, inpkt->payload.len); // make sure payload.p is filled with '\0' after payload.len, or use lua_pushlstring lua_call(h->L, 1, 1); if (!lua_isnil(h->L, -1)){ /* get return? */ if( lua_isstring(h->L, -1) ) // deal with the return string { size_t len = 0; const char *ret = luaL_checklstring( h->L, -1, &len ); if(len > MAX_PAYLOAD_SIZE){ lua_settop(h->L, n); luaL_error( h->L, "return string:<MAX_PAYLOAD_SIZE" ); return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); } NODE_DBG_((char *)ret); NODE_DBG("\n"); lua_settop(h->L, n); return coap_make_response(scratch, outpkt, ret, len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); } } else { lua_settop(h->L, n); return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); } } } } else { h = h->next; } } }else{ NODE_DBG("/v1/f match.\n"); goto end; } } NODE_DBG("none match.\n"); end: return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); }
static int handle_get_id(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { id = system_get_chip_id(); return coap_make_response(scratch, outpkt, (const uint8_t *)(&id), sizeof(uint32_t), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); }
static int handle_get_lightB1(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { char tmp[30]; sprintf(tmp,"%d",lightB1); return coap_make_response(scratch, outpkt, (const uint8_t *)&tmp, strlen(tmp), id_hi, id_lo, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN); }
static int handle_GET_well_known_core(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo) { return coap_make_response(scratch, outpkt, (const uint8_t *)rsp, strlen(rsp), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT); }