2015-02-04 18:32:57 +01:00
|
|
|
#include "user_config.h"
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <stdlib.h>
|
2015-02-04 18:32:57 +01:00
|
|
|
|
|
|
|
#include "coap.h"
|
|
|
|
|
2015-03-15 06:44:50 +01:00
|
|
|
size_t coap_server_respond(char *req, unsigned short reqlen, char *rsp, unsigned short rsplen)
|
2015-02-04 18:32:57 +01:00
|
|
|
{
|
|
|
|
NODE_DBG("coap_server_respond is called.\n");
|
2015-03-15 06:44:50 +01:00
|
|
|
size_t rlen = rsplen;
|
2015-02-04 18:32:57 +01:00
|
|
|
coap_packet_t pkt;
|
2015-03-15 13:12:29 +01:00
|
|
|
pkt.content.p = NULL;
|
|
|
|
pkt.content.len = 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
uint8_t scratch_raw[4];
|
|
|
|
coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
#ifdef COAP_DEBUG
|
|
|
|
NODE_DBG("Received: ");
|
2015-03-15 06:44:50 +01:00
|
|
|
coap_dump(req, reqlen, true);
|
2015-02-04 18:32:57 +01:00
|
|
|
NODE_DBG("\n");
|
|
|
|
#endif
|
|
|
|
|
2015-03-15 06:44:50 +01:00
|
|
|
if (0 != (rc = coap_parse(&pkt, req, reqlen))){
|
2015-02-04 18:32:57 +01:00
|
|
|
NODE_DBG("Bad packet rc=%d\n", rc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
coap_packet_t rsppkt;
|
2015-03-15 13:12:29 +01:00
|
|
|
rsppkt.content.p = NULL;
|
|
|
|
rsppkt.content.len = 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
#ifdef COAP_DEBUG
|
|
|
|
coap_dumpPacket(&pkt);
|
|
|
|
#endif
|
|
|
|
coap_handle_req(&scratch_buf, &pkt, &rsppkt);
|
2015-03-15 06:44:50 +01:00
|
|
|
if (0 != (rc = coap_build(rsp, &rlen, &rsppkt))){
|
2015-02-04 18:32:57 +01:00
|
|
|
NODE_DBG("coap_build failed rc=%d\n", rc);
|
2015-03-15 13:12:29 +01:00
|
|
|
// return 0;
|
|
|
|
rlen = 0;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef COAP_DEBUG
|
|
|
|
NODE_DBG("Responding: ");
|
2015-03-15 06:44:50 +01:00
|
|
|
coap_dump(rsp, rlen, true);
|
2015-02-04 18:32:57 +01:00
|
|
|
NODE_DBG("\n");
|
|
|
|
#endif
|
|
|
|
#ifdef COAP_DEBUG
|
|
|
|
coap_dumpPacket(&rsppkt);
|
|
|
|
#endif
|
|
|
|
}
|
2015-03-15 13:12:29 +01:00
|
|
|
if(rsppkt.content.p){
|
2019-07-21 23:58:21 +02:00
|
|
|
free(rsppkt.content.p);
|
2015-03-15 13:12:29 +01:00
|
|
|
rsppkt.content.p = NULL;
|
|
|
|
rsppkt.content.len = 0;
|
|
|
|
}
|
2015-03-15 06:44:50 +01:00
|
|
|
return rlen;
|
2015-02-04 18:32:57 +01:00
|
|
|
}
|
|
|
|
}
|