2015-02-04 18:32:57 +01:00
|
|
|
// Module for coapwork
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-02-04 18:32:57 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <string.h>
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stddef.h>
|
2015-02-04 18:32:57 +01:00
|
|
|
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
2015-02-04 18:32:57 +01:00
|
|
|
#include "mem.h"
|
2015-10-01 07:17:16 +02:00
|
|
|
#include "lwip/ip_addr.h"
|
2015-02-04 18:32:57 +01:00
|
|
|
#include "espconn.h"
|
|
|
|
#include "driver/uart.h"
|
|
|
|
|
2019-07-23 17:47:18 +02:00
|
|
|
#include "coap/coap.h"
|
|
|
|
#include "coap/uri.h"
|
|
|
|
#include "coap/node.h"
|
|
|
|
#include "coap/coap_timer.h"
|
|
|
|
#include "coap/coap_io.h"
|
|
|
|
#include "coap/coap_server.h"
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
coap_queue_t *gQueue = NULL;
|
|
|
|
|
|
|
|
typedef struct lcoap_userdata
|
|
|
|
{
|
|
|
|
struct espconn *pesp_conn;
|
|
|
|
int self_ref;
|
|
|
|
}lcoap_userdata;
|
|
|
|
|
|
|
|
static void coap_received(void *arg, char *pdata, unsigned short len)
|
|
|
|
{
|
|
|
|
NODE_DBG("coap_received is called.\n");
|
|
|
|
struct espconn *pesp_conn = arg;
|
|
|
|
lcoap_userdata *cud = (lcoap_userdata *)pesp_conn->reverse;
|
|
|
|
|
2015-03-15 06:44:50 +01:00
|
|
|
// 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'
|
2019-07-21 23:58:21 +02:00
|
|
|
memset(buf, 0, sizeof(buf)); // wipe prev data
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
if (len > MAX_MESSAGE_SIZE) {
|
|
|
|
NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client...
|
|
|
|
return;
|
|
|
|
}
|
2019-07-21 23:58:21 +02:00
|
|
|
// memcpy(buf, pdata, len);
|
2015-02-04 18:32:57 +01:00
|
|
|
|
2015-03-15 06:44:50 +01:00
|
|
|
size_t rsplen = coap_server_respond(pdata, len, buf, MAX_MESSAGE_SIZE+1);
|
2015-11-11 00:53:02 +01:00
|
|
|
|
|
|
|
// SDK 1.4.0 changed behaviour, for UDP server need to look up remote ip/port
|
|
|
|
remot_info *pr = 0;
|
|
|
|
if (espconn_get_connection_info (pesp_conn, &pr, 0) != ESPCONN_OK)
|
|
|
|
return;
|
|
|
|
pesp_conn->proto.udp->remote_port = pr->remote_port;
|
|
|
|
os_memmove (pesp_conn->proto.udp->remote_ip, pr->remote_ip, 4);
|
2019-07-21 23:58:21 +02:00
|
|
|
// The remot_info apparently should *not* be free()d, fyi
|
2015-11-11 00:53:02 +01:00
|
|
|
|
2015-02-04 18:32:57 +01:00
|
|
|
espconn_sent(pesp_conn, (unsigned char *)buf, rsplen);
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
// memset(buf, 0, sizeof(buf));
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
2015-02-04 18:58:47 +01:00
|
|
|
static void coap_sent(void *arg)
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
NODE_DBG("coap_sent is called.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: s = coap.create(function(conn))
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_create( lua_State* L, const char* mt )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
struct espconn *pesp_conn = NULL;
|
|
|
|
lcoap_userdata *cud;
|
|
|
|
unsigned type;
|
|
|
|
int stack = 1;
|
|
|
|
|
|
|
|
// create a object
|
|
|
|
cud = (lcoap_userdata *)lua_newuserdata(L, sizeof(lcoap_userdata));
|
|
|
|
// pre-initialize it, in case of errors
|
|
|
|
cud->self_ref = LUA_NOREF;
|
|
|
|
cud->pesp_conn = NULL;
|
|
|
|
|
|
|
|
// set its metatable
|
|
|
|
luaL_getmetatable(L, mt);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
|
|
|
// create the espconn struct
|
2019-07-21 23:58:21 +02:00
|
|
|
pesp_conn = (struct espconn *)calloc(1,sizeof(struct espconn));
|
2015-02-04 18:32:57 +01:00
|
|
|
if(!pesp_conn)
|
|
|
|
return luaL_error(L, "not enough memory");
|
|
|
|
|
|
|
|
cud->pesp_conn = pesp_conn;
|
|
|
|
|
|
|
|
pesp_conn->type = ESPCONN_UDP;
|
|
|
|
pesp_conn->proto.tcp = NULL;
|
|
|
|
pesp_conn->proto.udp = NULL;
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
pesp_conn->proto.udp = (esp_udp *)calloc(1,sizeof(esp_udp));
|
2015-02-04 18:32:57 +01:00
|
|
|
if(!pesp_conn->proto.udp){
|
2019-07-21 23:58:21 +02:00
|
|
|
free(pesp_conn);
|
2015-02-04 18:32:57 +01:00
|
|
|
cud->pesp_conn = pesp_conn = NULL;
|
|
|
|
return luaL_error(L, "not enough memory");
|
|
|
|
}
|
|
|
|
pesp_conn->state = ESPCONN_NONE;
|
|
|
|
NODE_DBG("UDP server/client is set.\n");
|
|
|
|
|
|
|
|
pesp_conn->reverse = cud;
|
|
|
|
|
|
|
|
NODE_DBG("coap_create is called.\n");
|
2019-02-17 19:26:29 +01:00
|
|
|
return 1;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:delete()
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_delete( lua_State* L, const char* mt )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
struct espconn *pesp_conn = NULL;
|
|
|
|
lcoap_userdata *cud;
|
|
|
|
|
|
|
|
cud = (lcoap_userdata *)luaL_checkudata(L, 1, mt);
|
|
|
|
luaL_argcheck(L, cud, 1, "Server/Client expected");
|
|
|
|
if(cud==NULL){
|
|
|
|
NODE_DBG("userdata is nil.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// free (unref) callback ref
|
|
|
|
if(LUA_NOREF!=cud->self_ref){
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, cud->self_ref);
|
|
|
|
cud->self_ref = LUA_NOREF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cud->pesp_conn)
|
|
|
|
{
|
|
|
|
if(cud->pesp_conn->proto.udp->remote_port || cud->pesp_conn->proto.udp->local_port)
|
|
|
|
espconn_delete(cud->pesp_conn);
|
2019-07-21 23:58:21 +02:00
|
|
|
free(cud->pesp_conn->proto.udp);
|
2015-02-04 18:32:57 +01:00
|
|
|
cud->pesp_conn->proto.udp = NULL;
|
2019-07-21 23:58:21 +02:00
|
|
|
free(cud->pesp_conn);
|
2015-02-04 18:32:57 +01:00
|
|
|
cud->pesp_conn = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_DBG("coap_delete is called.\n");
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:listen( port, ip )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_start( lua_State* L, const char* mt )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
struct espconn *pesp_conn = NULL;
|
|
|
|
lcoap_userdata *cud;
|
|
|
|
unsigned port;
|
|
|
|
size_t il;
|
|
|
|
ip_addr_t ipaddr;
|
|
|
|
|
|
|
|
cud = (lcoap_userdata *)luaL_checkudata(L, 1, mt);
|
|
|
|
luaL_argcheck(L, cud, 1, "Server/Client expected");
|
|
|
|
if(cud==NULL){
|
|
|
|
NODE_DBG("userdata is nil.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pesp_conn = cud->pesp_conn;
|
|
|
|
port = luaL_checkinteger( L, 2 );
|
|
|
|
pesp_conn->proto.udp->local_port = port;
|
|
|
|
NODE_DBG("UDP port is set: %d.\n", port);
|
|
|
|
|
|
|
|
if( lua_isstring(L,3) ) // deal with the ip string
|
|
|
|
{
|
|
|
|
const char *ip = luaL_checklstring( L, 3, &il );
|
|
|
|
if (ip == NULL)
|
|
|
|
{
|
|
|
|
ip = "0.0.0.0";
|
|
|
|
}
|
|
|
|
ipaddr.addr = ipaddr_addr(ip);
|
2019-07-21 23:58:21 +02:00
|
|
|
memcpy(pesp_conn->proto.udp->local_ip, &ipaddr.addr, 4);
|
2015-02-04 18:32:57 +01:00
|
|
|
NODE_DBG("UDP ip is set: ");
|
|
|
|
NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
|
|
|
|
NODE_DBG("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(LUA_NOREF==cud->self_ref){
|
|
|
|
lua_pushvalue(L, 1); // copy to the top of stack
|
|
|
|
cud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
espconn_regist_recvcb(pesp_conn, coap_received);
|
|
|
|
espconn_regist_sentcb(pesp_conn, coap_sent);
|
|
|
|
espconn_create(pesp_conn);
|
|
|
|
|
|
|
|
NODE_DBG("Coap Server started on port: %d\n", port);
|
|
|
|
NODE_DBG("coap_start is called.\n");
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:close()
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_close( lua_State* L, const char* mt )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
struct espconn *pesp_conn = NULL;
|
|
|
|
lcoap_userdata *cud;
|
|
|
|
|
|
|
|
cud = (lcoap_userdata *)luaL_checkudata(L, 1, mt);
|
|
|
|
luaL_argcheck(L, cud, 1, "Server/Client expected");
|
|
|
|
if(cud==NULL){
|
|
|
|
NODE_DBG("userdata is nil.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cud->pesp_conn)
|
|
|
|
{
|
|
|
|
if(cud->pesp_conn->proto.udp->remote_port || cud->pesp_conn->proto.udp->local_port)
|
|
|
|
espconn_delete(cud->pesp_conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(LUA_NOREF!=cud->self_ref){
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, cud->self_ref);
|
|
|
|
cud->self_ref = LUA_NOREF;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_DBG("coap_close is called.\n");
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server/client:on( "method", function(s) )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_on( lua_State* L, const char* mt )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
NODE_DBG("coap_on is called.\n");
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
2015-02-04 18:58:47 +01:00
|
|
|
static void coap_response_handler(void *arg, char *pdata, unsigned short len)
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
NODE_DBG("coap_response_handler is called.\n");
|
|
|
|
struct espconn *pesp_conn = arg;
|
|
|
|
|
|
|
|
coap_packet_t pkt;
|
2015-03-15 13:12:29 +01:00
|
|
|
pkt.content.p = NULL;
|
|
|
|
pkt.content.len = 0;
|
2015-03-15 06:44:50 +01:00
|
|
|
// 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'
|
2019-07-21 23:58:21 +02:00
|
|
|
memset(buf, 0, sizeof(buf)); // wipe prev data
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
int rc;
|
2015-03-15 06:44:50 +01:00
|
|
|
if( len > MAX_MESSAGE_SIZE )
|
|
|
|
{
|
|
|
|
NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client...
|
2015-02-04 18:32:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-07-21 23:58:21 +02:00
|
|
|
memcpy(buf, pdata, len);
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
if (0 != (rc = coap_parse(&pkt, buf, len))){
|
|
|
|
NODE_DBG("Bad packet rc=%d\n", rc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef COAP_DEBUG
|
|
|
|
coap_dumpPacket(&pkt);
|
|
|
|
#endif
|
|
|
|
/* check if this is a response to our original request */
|
|
|
|
if (!check_token(&pkt)) {
|
|
|
|
/* drop if this was just some message, or send RST in case of notification */
|
|
|
|
if (pkt.hdr.t == COAP_TYPE_CON || pkt.hdr.t == COAP_TYPE_NONCON){
|
|
|
|
// coap_send_rst(pkt); // send RST response
|
|
|
|
// or, just ignore it.
|
|
|
|
}
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt.hdr.t == COAP_TYPE_RESET) {
|
|
|
|
NODE_DBG("got RST\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ip = 0, port = 0;
|
|
|
|
coap_tid_t id = COAP_INVALID_TID;
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
memcpy(&ip, pesp_conn->proto.udp->remote_ip, sizeof(ip));
|
2015-02-04 18:32:57 +01:00
|
|
|
port = pesp_conn->proto.udp->remote_port;
|
|
|
|
|
|
|
|
coap_transaction_id(ip, port, &pkt, &id);
|
|
|
|
|
|
|
|
/* transaction done, remove the node from queue */
|
|
|
|
// stop timer
|
|
|
|
coap_timer_stop();
|
|
|
|
// remove the node
|
|
|
|
coap_remove_node(&gQueue, id);
|
|
|
|
// calculate time elapsed
|
|
|
|
coap_timer_update(&gQueue);
|
|
|
|
coap_timer_start(&gQueue);
|
|
|
|
|
|
|
|
if (COAP_RESPONSE_CLASS(pkt.hdr.code) == 2)
|
|
|
|
{
|
|
|
|
/* There is no block option set, just read the data and we are done. */
|
|
|
|
NODE_DBG("%d.%02d\t", (pkt.hdr.code >> 5), pkt.hdr.code & 0x1F);
|
2015-02-07 16:08:25 +01:00
|
|
|
NODE_DBG((char *)pkt.payload.p);
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
else if (COAP_RESPONSE_CLASS(pkt.hdr.code) >= 4)
|
|
|
|
{
|
|
|
|
NODE_DBG("%d.%02d\t", (pkt.hdr.code >> 5), pkt.hdr.code & 0x1F);
|
2015-02-07 16:08:25 +01:00
|
|
|
NODE_DBG((char *)pkt.payload.p);
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
if(!gQueue){ // if there is no node pending in the queue, disconnect from host.
|
|
|
|
if(pesp_conn->proto.udp->remote_port || pesp_conn->proto.udp->local_port)
|
|
|
|
espconn_delete(pesp_conn);
|
|
|
|
}
|
2019-07-21 23:58:21 +02:00
|
|
|
// memset(buf, 0, sizeof(buf));
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: client:request( [CON], uri, [payload] )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_request( lua_State* L, coap_method_t m )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
struct espconn *pesp_conn = NULL;
|
|
|
|
lcoap_userdata *cud;
|
|
|
|
int stack = 1;
|
|
|
|
|
|
|
|
cud = (lcoap_userdata *)luaL_checkudata(L, stack, "coap_client");
|
|
|
|
luaL_argcheck(L, cud, stack, "Server/Client expected");
|
|
|
|
if(cud==NULL){
|
|
|
|
NODE_DBG("userdata is nil.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack++;
|
|
|
|
pesp_conn = cud->pesp_conn;
|
|
|
|
ip_addr_t ipaddr;
|
2016-04-24 16:45:47 +02:00
|
|
|
uint8_t host[64];
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
unsigned t;
|
|
|
|
if ( lua_isnumber(L, stack) )
|
|
|
|
{
|
|
|
|
t = lua_tointeger(L, stack);
|
|
|
|
stack++;
|
|
|
|
if ( t != COAP_TYPE_CON && t != COAP_TYPE_NONCON )
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
} else {
|
|
|
|
t = COAP_TYPE_CON; // default to CON
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t l;
|
|
|
|
const char *url = luaL_checklstring( L, stack, &l );
|
|
|
|
stack++;
|
|
|
|
if (url == NULL)
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
|
|
|
|
coap_uri_t *uri = coap_new_uri(url, l); // should call free(uri) somewhere
|
|
|
|
if (uri == NULL)
|
|
|
|
return luaL_error( L, "uri wrong format." );
|
2016-04-24 16:45:47 +02:00
|
|
|
if (uri->host.length + 1 /* for the null */ > sizeof(host)) {
|
|
|
|
return luaL_error(L, "host too long");
|
|
|
|
}
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
pesp_conn->proto.udp->remote_port = uri->port;
|
|
|
|
NODE_DBG("UDP port is set: %d.\n", uri->port);
|
|
|
|
pesp_conn->proto.udp->local_port = espconn_port();
|
|
|
|
|
|
|
|
if(uri->host.length){
|
2019-07-21 23:58:21 +02:00
|
|
|
memcpy(host, uri->host.s, uri->host.length);
|
2015-02-04 18:32:57 +01:00
|
|
|
host[uri->host.length] = '\0';
|
|
|
|
|
|
|
|
ipaddr.addr = ipaddr_addr(host);
|
|
|
|
NODE_DBG("Host len(%d):", uri->host.length);
|
|
|
|
NODE_DBG(host);
|
|
|
|
NODE_DBG("\n");
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
memcpy(pesp_conn->proto.udp->remote_ip, &ipaddr.addr, 4);
|
2015-02-04 18:32:57 +01:00
|
|
|
NODE_DBG("UDP ip is set: ");
|
|
|
|
NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
|
|
|
|
NODE_DBG("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
coap_pdu_t *pdu = coap_new_pdu(); // should call coap_delete_pdu() somewhere
|
|
|
|
if(!pdu){
|
|
|
|
if(uri)
|
2019-07-21 23:58:21 +02:00
|
|
|
free(uri);
|
2016-01-20 09:37:03 +01:00
|
|
|
return luaL_error (L, "alloc fail");
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *payload = NULL;
|
|
|
|
l = 0;
|
|
|
|
if( lua_isstring(L, stack) ){
|
|
|
|
payload = luaL_checklstring( L, stack, &l );
|
|
|
|
if (payload == NULL)
|
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
coap_make_request(&(pdu->scratch), pdu->pkt, t, m, uri, payload, l);
|
|
|
|
|
|
|
|
#ifdef COAP_DEBUG
|
|
|
|
coap_dumpPacket(pdu->pkt);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
if (0 != (rc = coap_build(pdu->msg.p, &(pdu->msg.len), pdu->pkt))){
|
|
|
|
NODE_DBG("coap_build failed rc=%d\n", rc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef COAP_DEBUG
|
|
|
|
NODE_DBG("Sending: ");
|
|
|
|
coap_dump(pdu->msg.p, pdu->msg.len, true);
|
|
|
|
NODE_DBG("\n");
|
|
|
|
#endif
|
|
|
|
espconn_regist_recvcb(pesp_conn, coap_response_handler);
|
|
|
|
sint8_t con = espconn_create(pesp_conn);
|
|
|
|
if( ESPCONN_OK != con){
|
|
|
|
NODE_DBG("Connect to host. code:%d\n", con);
|
|
|
|
// coap_delete_pdu(pdu);
|
2019-02-17 19:26:29 +01:00
|
|
|
}
|
|
|
|
// else
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
coap_tid_t tid = COAP_INVALID_TID;
|
|
|
|
if (pdu->pkt->hdr.t == COAP_TYPE_CON){
|
|
|
|
tid = coap_send_confirmed(pesp_conn, pdu);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tid = coap_send(pesp_conn, pdu);
|
|
|
|
}
|
|
|
|
if (pdu->pkt->hdr.t != COAP_TYPE_CON || tid == COAP_INVALID_TID){
|
|
|
|
coap_delete_pdu(pdu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(uri)
|
2019-07-21 23:58:21 +02:00
|
|
|
free((void *)uri);
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
NODE_DBG("coap_request is called.\n");
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern coap_luser_entry *variable_entry;
|
|
|
|
extern coap_luser_entry *function_entry;
|
|
|
|
// Lua: coap:var/func( string )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_regist( lua_State* L, const char* mt, int isvar )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
size_t l;
|
|
|
|
const char *name = luaL_checklstring( L, 2, &l );
|
2015-11-09 09:21:39 +01:00
|
|
|
int content_type = luaL_optint(L, 3, COAP_CONTENTTYPE_TEXT_PLAIN);
|
2015-02-04 18:32:57 +01:00
|
|
|
if (name == NULL)
|
|
|
|
return luaL_error( L, "name must be set." );
|
|
|
|
|
|
|
|
coap_luser_entry *h = NULL;
|
|
|
|
// if(lua_isstring(L, 3))
|
|
|
|
if(isvar)
|
|
|
|
h = variable_entry;
|
|
|
|
else
|
|
|
|
h = function_entry;
|
|
|
|
|
|
|
|
while(NULL!=h->next){ // goto the end of the list
|
2019-07-21 23:58:21 +02:00
|
|
|
if(h->name!= NULL && strcmp(h->name, name)==0) // key exist, override it
|
2015-02-04 18:32:57 +01:00
|
|
|
break;
|
|
|
|
h = h->next;
|
|
|
|
}
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
if(h->name==NULL || strcmp(h->name, name)!=0){ // not exists. make a new one.
|
|
|
|
h->next = (coap_luser_entry *)calloc(1,sizeof(coap_luser_entry));
|
2015-02-04 18:32:57 +01:00
|
|
|
h = h->next;
|
|
|
|
if(h == NULL)
|
|
|
|
return luaL_error(L, "not enough memory");
|
|
|
|
h->next = NULL;
|
|
|
|
h->name = NULL;
|
2019-02-17 19:26:29 +01:00
|
|
|
}
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
h->name = name;
|
2015-11-09 09:21:39 +01:00
|
|
|
h->content_type = content_type;
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
NODE_DBG("coap_regist is called.\n");
|
2019-02-17 19:26:29 +01:00
|
|
|
return 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: s = coap.createServer(function(conn))
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_createServer( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_server";
|
|
|
|
return coap_create(L, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:delete()
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_server_delete( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_server";
|
|
|
|
return coap_delete(L, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:listen( port, ip, function(err) )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_server_listen( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_server";
|
|
|
|
return coap_start(L, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:close()
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_server_close( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_server";
|
|
|
|
return coap_close(L, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:on( "method", function(server) )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_server_on( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_server";
|
|
|
|
return coap_on(L, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:var( "name" )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_server_var( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_server";
|
|
|
|
return coap_regist(L, mt, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: server:func( "name" )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_server_func( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_server";
|
|
|
|
return coap_regist(L, mt, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: s = coap.createClient(function(conn))
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_createClient( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_client";
|
|
|
|
return coap_create(L, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: client:gcdelete()
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_client_gcdelete( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
const char *mt = "coap_client";
|
|
|
|
return coap_delete(L, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// client:get( string, function(sent) )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_client_get( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
return coap_request(L, COAP_METHOD_GET);
|
|
|
|
}
|
|
|
|
|
|
|
|
// client:post( string, function(sent) )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_client_post( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
return coap_request(L, COAP_METHOD_POST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// client:put( string, function(sent) )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_client_put( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
return coap_request(L, COAP_METHOD_PUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// client:delete( string, function(sent) )
|
2015-02-04 18:58:47 +01:00
|
|
|
static int coap_client_delete( lua_State* L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
return coap_request(L, COAP_METHOD_DELETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
LROT_BEGIN(coap_server, NULL, LROT_MASK_GC_INDEX)
|
|
|
|
LROT_FUNCENTRY( __gc, coap_server_delete )
|
|
|
|
LROT_TABENTRY( __index, coap_server )
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( listen, coap_server_listen )
|
|
|
|
LROT_FUNCENTRY( close, coap_server_close )
|
|
|
|
LROT_FUNCENTRY( var, coap_server_var )
|
|
|
|
LROT_FUNCENTRY( func, coap_server_func )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(coap_server, NULL, LROT_MASK_GC_INDEX)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
LROT_BEGIN(coap_client, NULL, LROT_MASK_GC_INDEX)
|
|
|
|
LROT_FUNCENTRY( __gc, coap_client_gcdelete )
|
|
|
|
LROT_TABENTRY( __index, coap_client )
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( get, coap_client_get )
|
|
|
|
LROT_FUNCENTRY( post, coap_client_post )
|
|
|
|
LROT_FUNCENTRY( put, coap_client_put )
|
|
|
|
LROT_FUNCENTRY( delete, coap_client_delete )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(coap_client, NULL, LROT_MASK_GC_INDEX)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(coap, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( Server, coap_createServer )
|
|
|
|
LROT_FUNCENTRY( Client, coap_createClient )
|
|
|
|
LROT_NUMENTRY( CON, COAP_TYPE_CON )
|
|
|
|
LROT_NUMENTRY( NON, COAP_TYPE_NONCON )
|
|
|
|
LROT_NUMENTRY( TEXT_PLAIN, COAP_CONTENTTYPE_TEXT_PLAIN )
|
|
|
|
LROT_NUMENTRY( LINKFORMAT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT )
|
|
|
|
LROT_NUMENTRY( XML, COAP_CONTENTTYPE_APPLICATION_XML )
|
|
|
|
LROT_NUMENTRY( OCTET_STREAM, COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM )
|
|
|
|
LROT_NUMENTRY( EXI, COAP_CONTENTTYPE_APPLICATION_EXI )
|
|
|
|
LROT_NUMENTRY( JSON, COAP_CONTENTTYPE_APPLICATION_JSON )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(coap, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
2015-02-04 18:32:57 +01:00
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
int luaopen_coap( lua_State *L )
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
endpoint_setup();
|
2019-05-08 13:08:20 +02:00
|
|
|
luaL_rometatable(L, "coap_server", LROT_TABLEREF(coap_server));
|
|
|
|
luaL_rometatable(L, "coap_client", LROT_TABLEREF(coap_client));
|
2015-02-04 18:32:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-12-16 06:04:58 +01:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
NODEMCU_MODULE(COAP, "coap", coap, luaopen_coap);
|