From 3a5e845b294507d84d90dfb77befe45dbcf60854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ole=C5=9B?= Date: Mon, 9 Nov 2015 09:21:39 +0100 Subject: [PATCH] Allow to set content type in COAP (fix #658) --- README.md | 3 +++ app/coap/coap.h | 5 +++++ app/coap/endpoints.c | 10 +++++----- app/modules/coap.c | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c49b870a..d6ce7649 100644 --- a/README.md +++ b/README.md @@ -545,6 +545,9 @@ cs:listen(5683) myvar=1 cs:var("myvar") -- get coap://192.168.18.103:5683/v1/v/myvar will return the value of myvar: 1 +all='[1,2,3]' +cs:var("all", coap.JSON) -- sets content type to json + -- function should tack one string, return one string. function myfun(payload) print("myfun called") diff --git a/app/coap/coap.h b/app/coap/coap.h index 3862f57b..f47123f2 100644 --- a/app/coap/coap.h +++ b/app/coap/coap.h @@ -116,6 +116,10 @@ typedef enum COAP_CONTENTTYPE_NONE = -1, // bodge to allow us not to send option block COAP_CONTENTTYPE_TEXT_PLAIN = 0, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT = 40, + COAP_CONTENTTYPE_APPLICATION_XML = 41, + COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM = 42, + COAP_CONTENTTYPE_APPLICATION_EXI = 47, + COAP_CONTENTTYPE_APPLICATION_JSON = 50, } coap_content_type_t; /////////////////////// @@ -156,6 +160,7 @@ struct coap_luser_entry{ // char name[MAX_SEGMENTS_SIZE+1]; // +1 for string '\0' const char *name; coap_luser_entry *next; + int content_type; }; struct coap_endpoint_t{ diff --git a/app/coap/endpoints.c b/app/coap/endpoints.c index a54b1235..00705f93 100644 --- a/app/coap/endpoints.c +++ b/app/coap/endpoints.c @@ -64,14 +64,14 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra { n = lua_gettop(h->L); lua_getglobal(h->L, h->name); - if (!lua_isnumber(h->L, -1)) { - NODE_DBG ("should be a number.\n"); + if (!lua_isnumber(h->L, -1) && !lua_isstring(h->L, -1)) { + NODE_DBG ("should be a number or string.\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); + return coap_make_response(scratch, outpkt, (const uint8_t *)res, c_strlen(res), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, h->content_type); } } } else { @@ -198,10 +198,10 @@ static int handle_get_id(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, c 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); } -coap_luser_entry var_head = {NULL,NULL,NULL}; +coap_luser_entry var_head = {NULL,NULL,NULL,0}; coap_luser_entry *variable_entry = &var_head; -coap_luser_entry func_head = {NULL,NULL,NULL}; +coap_luser_entry func_head = {NULL,NULL,NULL,0}; coap_luser_entry *function_entry = &func_head; const coap_endpoint_t endpoints[] = diff --git a/app/modules/coap.c b/app/modules/coap.c index 1d496e5f..7dadb06d 100644 --- a/app/modules/coap.c +++ b/app/modules/coap.c @@ -429,6 +429,7 @@ static int coap_regist( lua_State* L, const char* mt, int isvar ) { size_t l; const char *name = luaL_checklstring( L, 2, &l ); + int content_type = luaL_optint(L, 3, COAP_CONTENTTYPE_TEXT_PLAIN); if (name == NULL) return luaL_error( L, "name must be set." ); @@ -457,6 +458,7 @@ static int coap_regist( lua_State* L, const char* mt, int isvar ) h->L = L; h->name = name; + h->content_type = content_type; NODE_DBG("coap_regist is called.\n"); return 0; @@ -585,6 +587,12 @@ const LUA_REG_TYPE coap_map[] = #if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "CON" ), LNUMVAL( COAP_TYPE_CON ) }, { LSTRKEY( "NON" ), LNUMVAL( COAP_TYPE_NONCON ) }, + { LSTRKEY( "TEXT_PLAIN"), LNUMVAL( COAP_CONTENTTYPE_TEXT_PLAIN ) }, + { LSTRKEY( "LINKFORMAT"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ) }, + { LSTRKEY( "XML"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_XML ) }, + { LSTRKEY( "OCTET_STREAM"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM ) }, + { LSTRKEY( "EXI"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_EXI ) }, + { LSTRKEY( "JSON"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_JSON) }, { LSTRKEY( "__metatable" ), LROVAL( coap_map ) }, #endif @@ -609,6 +617,12 @@ LUALIB_API int luaopen_coap( lua_State *L ) // Module constants MOD_REG_NUMBER( L, "CON", COAP_TYPE_CON ); MOD_REG_NUMBER( L, "NON", COAP_TYPE_NONCON ); + MOD_REG_NUMBER( L, "TEXT_PLAIN", COAP_CONTENTTYPE_TEXT_PLAIN ); + MOD_REG_NUMBER( L, "LINKFORMAT", COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ); + MOD_REG_NUMBER( L, "XML", COAP_CONTENTTYPE_APPLICATION_XML); + MOD_REG_NUMBER( L, "OCTET_STREAM", COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM); + MOD_REG_NUMBER( L, "EXI", COAP_CONTENTTYPE_APPLICATION_EXI); + MOD_REG_NUMBER( L, "JSON", COAP_CONTENTTYPE_APPLICATION_JSON); n = lua_gettop(L);