// 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; }
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_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); }