Skip to content

Commit

Permalink
fixed content type processing
Browse files Browse the repository at this point in the history
  • Loading branch information
smlng committed Jun 10, 2016
1 parent 417d8df commit 71ed82a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
14 changes: 8 additions & 6 deletions coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,15 @@ int coap_build_endpoints(const coap_endpoint_t *endpoints, char *buf, size_t buf
if (buflen < 4) { // <>;
return COAP_ERR_BUFFER_TOO_SMALL;
}
size_t len = buflen-1;
memset(buf,0,buflen);
// loop over endpoints
int len = buflen - 1;
for (const coap_endpoint_t *ep = endpoints; ep->handler; ++ep) {
if (0 > len) {
return COAP_ERR_BUFFER_TOO_SMALL;
}
// skip if missing content type
if (NULL == ep->core_attr) {
ep++;
if (ep->ct == COAP_CONTENTTYPE_NONE) {
continue;
}
// comma separated list
Expand All @@ -447,8 +450,7 @@ int coap_build_endpoints(const coap_endpoint_t *endpoints, char *buf, size_t buf
strncat(buf, ">;", len);
len -= 2;
// append content type
strncat(buf, ep->core_attr, len);
len -= strlen(ep->core_attr);
len -= sprintf(buf + (buflen - len - 1), "ct=%d", (int)ep->ct);
}
return strlen(buf);
return COAP_SUCCESS;
}
16 changes: 9 additions & 7 deletions coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,12 @@ typedef struct coap_endpoint_path

typedef struct coap_endpoint
{
coap_method_t method; /* (i.e. POST, PUT or GET) */
coap_method_t method; // POST, PUT or GET
coap_endpoint_func handler; /* callback function which handles this
* type of endpoint (and calls
* coap_make_response() at some point) */
const coap_endpoint_path_t *path; /* path towards a resource (i.e. foo/bar/) */
const char *core_attr; /* the 'ct' attribute, as defined in RFC7252, section 7.2.1.:
* "The Content-Format code "ct" attribute
* provides a hint about the
* Content-Formats this resource returns."
* (Section 12.3. lists possible ct values.) */
const coap_endpoint_path_t *path; // resource path, e.g. foo/bar/
coap_content_type_t ct; // content type in payload
} coap_endpoint_t;


Expand All @@ -197,6 +193,10 @@ void coap_dump_packet(const coap_packet_t *pkt);

int coap_parse(coap_packet_t *pkt, const uint8_t *buf, size_t buflen);
int coap_build(uint8_t *buf, size_t *buflen, const coap_packet_t *pkt);
int coap_make_request(coap_rw_buffer_t *scratch, coap_packet_t *pkt,
const uint8_t *content, size_t content_len,
coap_content_type_t content_type, coap_method_t method,
const coap_endpoint_path_t *path);
int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt,
const uint8_t *content, size_t content_len,
uint16_t msgid, const coap_buffer_t* tok,
Expand All @@ -205,6 +205,8 @@ int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt,
int coap_handle_request(const coap_endpoint_t *endpoints,
coap_rw_buffer_t *scratch,
const coap_packet_t *inpkt, coap_packet_t *outpkt);
int coap_handle_response();
int coap_handle_packet();
int coap_build_endpoints(const coap_endpoint_t *endpoints,
char *buf, size_t buflen);

Expand Down
9 changes: 5 additions & 4 deletions example/endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void endpoint_setup(const coap_endpoint_t *endpoints)
void endpoint_setup(const coap_endpoint_t *endpoints)
{
coap_build_endpoints(endpoints, rsp, rsplen);
printf("endpoints: %s\n", rsp);
}
#endif

Expand Down Expand Up @@ -66,8 +67,8 @@ static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpk

const coap_endpoint_t endpoints[] =
{
{COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, "ct=40"},
{COAP_METHOD_GET, handle_get_light, &path_light, "ct=0"},
{COAP_METHOD_PUT, handle_put_light, &path_light, NULL},
{(coap_method_t)0, NULL, NULL, NULL}
{COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT},
{COAP_METHOD_GET, handle_get_light, &path_light, COAP_CONTENTTYPE_TEXT_PLAIN},
{COAP_METHOD_PUT, handle_put_light, &path_light, COAP_CONTENTTYPE_NONE},
{(coap_method_t)0, NULL, NULL, COAP_CONTENTTYPE_NONE}
};

0 comments on commit 71ed82a

Please sign in to comment.