Commit c4600006 authored by funshine's avatar funshine
Browse files

reduce coap module memory usage

parent d28a2c9e
#ifndef _COAP_SERVER_H #ifndef _COAP_CLIENT_H
#define _COAP_SERVER_H 1 #define _COAP_CLIENT_H 1
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
size_t coap_server_respond(char *data, unsigned short len, unsigned short size); void coap_client_response_handler(char *data, unsigned short len, unsigned short size, const uint32_t ip, const uint32_t port);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -3,14 +3,10 @@ ...@@ -3,14 +3,10 @@
#include "coap.h" #include "coap.h"
size_t coap_server_respond(char *data, unsigned short len, unsigned short size) size_t coap_server_respond(char *req, unsigned short reqlen, char *rsp, unsigned short rsplen)
{ {
NODE_DBG("coap_server_respond is called.\n"); NODE_DBG("coap_server_respond is called.\n");
if(len>size){ size_t rlen = rsplen;
NODE_DBG("len:%d, size:%d\n",len,size);
return 0;
}
size_t rsplen = size;
coap_packet_t pkt; coap_packet_t pkt;
uint8_t scratch_raw[4]; uint8_t scratch_raw[4];
coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)}; coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
...@@ -18,11 +14,11 @@ size_t coap_server_respond(char *data, unsigned short len, unsigned short size) ...@@ -18,11 +14,11 @@ size_t coap_server_respond(char *data, unsigned short len, unsigned short size)
#ifdef COAP_DEBUG #ifdef COAP_DEBUG
NODE_DBG("Received: "); NODE_DBG("Received: ");
coap_dump(data, len, true); coap_dump(req, reqlen, true);
NODE_DBG("\n"); NODE_DBG("\n");
#endif #endif
if (0 != (rc = coap_parse(&pkt, data, len))){ if (0 != (rc = coap_parse(&pkt, req, reqlen))){
NODE_DBG("Bad packet rc=%d\n", rc); NODE_DBG("Bad packet rc=%d\n", rc);
return 0; return 0;
} }
...@@ -33,7 +29,7 @@ size_t coap_server_respond(char *data, unsigned short len, unsigned short size) ...@@ -33,7 +29,7 @@ size_t coap_server_respond(char *data, unsigned short len, unsigned short size)
coap_dumpPacket(&pkt); coap_dumpPacket(&pkt);
#endif #endif
coap_handle_req(&scratch_buf, &pkt, &rsppkt); coap_handle_req(&scratch_buf, &pkt, &rsppkt);
if (0 != (rc = coap_build(data, &rsplen, &rsppkt))){ if (0 != (rc = coap_build(rsp, &rlen, &rsppkt))){
NODE_DBG("coap_build failed rc=%d\n", rc); NODE_DBG("coap_build failed rc=%d\n", rc);
return 0; return 0;
} }
...@@ -41,13 +37,13 @@ size_t coap_server_respond(char *data, unsigned short len, unsigned short size) ...@@ -41,13 +37,13 @@ size_t coap_server_respond(char *data, unsigned short len, unsigned short size)
{ {
#ifdef COAP_DEBUG #ifdef COAP_DEBUG
NODE_DBG("Responding: "); NODE_DBG("Responding: ");
coap_dump(data, rsplen, true); coap_dump(rsp, rlen, true);
NODE_DBG("\n"); NODE_DBG("\n");
#endif #endif
#ifdef COAP_DEBUG #ifdef COAP_DEBUG
coap_dumpPacket(&rsppkt); coap_dumpPacket(&rsppkt);
#endif #endif
} }
return rsplen; return rlen;
} }
} }
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
extern "C" { extern "C" {
#endif #endif
size_t coap_server_respond(char *data, unsigned short len, unsigned short size); size_t coap_server_respond(char *req, unsigned short reqlen, char *rsp, unsigned short rsplen);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
#include "os_type.h" #include "os_type.h"
static char rsp[MAX_PAYLOAD_SIZE] = ""; static char rsp[512] = "";
const uint16_t rsplen = MAX_PAYLOAD_SIZE; const uint16_t rsplen = 512;
void build_well_known_rsp(void); void build_well_known_rsp(void);
void endpoint_setup(void) void endpoint_setup(void)
...@@ -29,6 +29,7 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra ...@@ -29,6 +29,7 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra
{ {
const coap_option_t *opt; const coap_option_t *opt;
uint8_t count; uint8_t count;
int n;
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count))) 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] if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
...@@ -54,14 +55,15 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra ...@@ -54,14 +55,15 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE); 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)) if(c_strlen(h->name))
{ {
n = lua_gettop(h->L);
lua_getglobal(h->L, h->name); lua_getglobal(h->L, h->name);
if (!lua_isnumber(h->L, -1)) { if (!lua_isnumber(h->L, -1)) {
NODE_DBG ("should be a number.\n"); NODE_DBG ("should be a number.\n");
lua_pop(h->L, 1); 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); return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
} else { } else {
const char *res = lua_tostring(h->L,-1); const char *res = lua_tostring(h->L,-1);
lua_pop(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); 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);
} }
} }
...@@ -84,6 +86,7 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr ...@@ -84,6 +86,7 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
{ {
const coap_option_t *opt; const coap_option_t *opt;
uint8_t count; uint8_t count;
int n;
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count))) 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] if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
...@@ -111,10 +114,11 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr ...@@ -111,10 +114,11 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
if(c_strlen(h->name)) if(c_strlen(h->name))
{ {
n = lua_gettop(h->L);
lua_getglobal(h->L, h->name); lua_getglobal(h->L, h->name);
if (lua_type(h->L, -1) != LUA_TFUNCTION) { if (lua_type(h->L, -1) != LUA_TFUNCTION) {
NODE_DBG ("should be a function\n"); NODE_DBG ("should be a function\n");
lua_pop(h->L, 1); 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); return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
} else { } 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_pushlstring(h->L, inpkt->payload.p, inpkt->payload.len); // make sure payload.p is filled with '\0' after payload.len, or use lua_pushlstring
...@@ -125,14 +129,17 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr ...@@ -125,14 +129,17 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
size_t len = 0; size_t len = 0;
const char *ret = luaL_checklstring( h->L, -1, &len ); const char *ret = luaL_checklstring( h->L, -1, &len );
if(len > MAX_PAYLOAD_SIZE){ if(len > MAX_PAYLOAD_SIZE){
lua_settop(h->L, n);
luaL_error( h->L, "return string:<MAX_PAYLOAD_SIZE" ); 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); 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((char *)ret);
NODE_DBG("\n"); 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); return coap_make_response(scratch, outpkt, ret, len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
} }
} else { } 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); return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
} }
} }
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#define LUA_USE_MODULES_OW #define LUA_USE_MODULES_OW
#define LUA_USE_MODULES_BIT #define LUA_USE_MODULES_BIT
#define LUA_USE_MODULES_MQTT #define LUA_USE_MODULES_MQTT
// #define LUA_USE_MODULES_COAP // need about 4k more ram for now // #define LUA_USE_MODULES_COAP // need about 1k more ram for now
#define LUA_USE_MODULES_U8G #define LUA_USE_MODULES_U8G
#define LUA_USE_MODULES_WS2812 #define LUA_USE_MODULES_WS2812
#endif /* LUA_USE_MODULES */ #endif /* LUA_USE_MODULES */
......
...@@ -37,20 +37,20 @@ static void coap_received(void *arg, char *pdata, unsigned short len) ...@@ -37,20 +37,20 @@ static void coap_received(void *arg, char *pdata, unsigned short len)
struct espconn *pesp_conn = arg; struct espconn *pesp_conn = arg;
lcoap_userdata *cud = (lcoap_userdata *)pesp_conn->reverse; lcoap_userdata *cud = (lcoap_userdata *)pesp_conn->reverse;
static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0' // static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
c_memset(buf, 0, sizeof(buf)); // wipe prev data c_memset(buf, 0, sizeof(buf)); // wipe prev data
if (len > MAX_MESSAGE_SIZE) { if (len > MAX_MESSAGE_SIZE) {
NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client... NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client...
return; return;
} else {
c_memcpy(buf, pdata, len);
} }
// c_memcpy(buf, pdata, len);
size_t rsplen = coap_server_respond(buf, len, MAX_MESSAGE_SIZE+1); size_t rsplen = coap_server_respond(pdata, len, buf, MAX_MESSAGE_SIZE+1);
espconn_sent(pesp_conn, (unsigned char *)buf, rsplen); espconn_sent(pesp_conn, (unsigned char *)buf, rsplen);
c_memset(buf, 0, sizeof(buf)); // c_memset(buf, 0, sizeof(buf));
} }
static void coap_sent(void *arg) static void coap_sent(void *arg)
...@@ -227,26 +227,17 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len) ...@@ -227,26 +227,17 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len)
struct espconn *pesp_conn = arg; struct espconn *pesp_conn = arg;
coap_packet_t pkt; coap_packet_t pkt;
static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0' // static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
c_memset(buf, 0, sizeof(buf)); // wipe prev data c_memset(buf, 0, sizeof(buf)); // wipe prev data
static int n = 0;
int rc; int rc;
if ((len == 1460) && (1460 <= MAX_MESSAGE_SIZE)){ if( len > MAX_MESSAGE_SIZE )
c_memcpy(buf, pdata, len); // max length is 1460, another part of data is coming in next callback {
n = len; NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client...
return; return;
} else {
if( len > MAX_MESSAGE_SIZE )
{
NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client...
c_memset(buf, 0, sizeof(buf)); // wipe prev data
n = 0;
return;
}
c_memcpy(buf + n, pdata, len);
len += n; // more than 1460
} }
c_memcpy(buf, pdata, len);
if (0 != (rc = coap_parse(&pkt, buf, len))){ if (0 != (rc = coap_parse(&pkt, buf, len))){
NODE_DBG("Bad packet rc=%d\n", rc); NODE_DBG("Bad packet rc=%d\n", rc);
...@@ -306,8 +297,7 @@ end: ...@@ -306,8 +297,7 @@ end:
if(pesp_conn->proto.udp->remote_port || pesp_conn->proto.udp->local_port) if(pesp_conn->proto.udp->remote_port || pesp_conn->proto.udp->local_port)
espconn_delete(pesp_conn); espconn_delete(pesp_conn);
} }
c_memset(buf, 0, sizeof(buf)); // c_memset(buf, 0, sizeof(buf));
n = 0;
} }
// Lua: client:request( [CON], uri, [payload] ) // Lua: client:request( [CON], uri, [payload] )
......
...@@ -355,8 +355,11 @@ cs:listen(5683) ...@@ -355,8 +355,11 @@ cs:listen(5683)
myvar=1 myvar=1
cs:var("myvar") -- get coap://192.168.18.103:5683/v1/v/myvar will return the value of myvar: 1 cs:var("myvar") -- get coap://192.168.18.103:5683/v1/v/myvar will return the value of myvar: 1
function myfun() -- function should tack one string, return one string.
function myfun(payload)
print("myfun called") print("myfun called")
respond = "hello"
return respond
end end
cs:func("myfun") -- post coap://192.168.18.103:5683/v1/f/myfun will call myfun cs:func("myfun") -- post coap://192.168.18.103:5683/v1/f/myfun will call myfun
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment