Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
3a5e845b
Commit
3a5e845b
authored
Nov 09, 2015
by
Łukasz Oleś
Browse files
Allow to set content type in COAP (fix #658)
parent
7ef1711e
Changes
4
Show whitespace changes
Inline
Side-by-side
README.md
View file @
3a5e845b
...
...
@@ -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"
)
...
...
app/coap/coap.h
View file @
3a5e845b
...
...
@@ -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
{
...
...
app/coap/endpoints.c
View file @
3a5e845b
...
...
@@ -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
[]
=
...
...
app/modules/coap.c
View file @
3a5e845b
...
...
@@ -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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment