CoAP: replaced coap_send_message() with coap_sendto() that returns

number of bytes sent or error.
This commit is contained in:
Niclas Finne 2017-11-09 14:42:45 +01:00
parent 074391fa7d
commit a866fcbfad
8 changed files with 55 additions and 33 deletions

View File

@ -230,18 +230,17 @@ coap_transport_init(void)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void int
coap_send_message(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len) coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
{ {
if(!coap_endpoint_is_connected(ep)) { if(!coap_endpoint_is_connected(ep)) {
PRINTF("CoAP endpoint not connected\n"); PRINTF("CoAP endpoint not connected\n");
return; return -1;
} }
#ifdef WITH_DTLS #ifdef WITH_DTLS
if(coap_endpoint_is_secure(ep)) { if(coap_endpoint_is_secure(ep)) {
dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, len); return dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, len);
return;
} }
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
@ -251,6 +250,8 @@ coap_send_message(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
printf("%02x", data[i]); printf("%02x", data[i]);
} }
printf("\n"); printf("\n");
return len;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int int

View File

@ -416,35 +416,45 @@ coap_transport_init(void)
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void int
coap_send_message(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len) coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
{ {
int ret;
if(!coap_endpoint_is_connected(ep)) { if(!coap_endpoint_is_connected(ep)) {
PRINTF("CoAP-IPv4: endpoint "); PRINTF("CoAP-IPv4: endpoint ");
PRINTEP(ep); PRINTEP(ep);
PRINTF(" not connected - dropping packet\n"); PRINTF(" not connected - dropping packet\n");
return; return -1;
} }
#ifdef WITH_DTLS #ifdef WITH_DTLS
if(coap_endpoint_is_secure(ep)) { if(coap_endpoint_is_secure(ep)) {
if(dtls_context) { if(dtls_context) {
dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, len); ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, len);
PRINTF("CoAP-IPv4: SENT DTLS to ");
PRINTEP(ep);
if(ret < 0) {
PRINTF(" - error %d\n", ret);
} else {
PRINTF(" %d/%u bytes\n", ret, len);
}
return ret;
} }
return; PRINTF("CoAP-IPv4: no DTLS context\n");
return -1;
} }
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
if(coap_ipv4_fd >= 0) { if(coap_ipv4_fd >= 0) {
if(sendto(coap_ipv4_fd, data, len, 0, ret = sendto(coap_ipv4_fd, data, len, 0, (struct sockaddr *)&ep->addr,
(struct sockaddr *)&ep->addr, ep->size) < 1) { ep->size);
PRINTF("CoAP-IPv4: failed to send to "); PRINTF("CoAP-IPv4: SENT to ");
PRINTEP(ep); PRINTEP(ep);
PRINTF(" %u bytes: %s\n", len, strerror(errno)); if(ret < 0) {
PRINTF(" - error %d: %s\n", ret, strerror(errno));
} else { } else {
PRINTF("CoAP-IPv4: SENT to "); PRINTF(" %d/%u bytes\n", ret, len);
PRINTEP(ep);
PRINTF(" %u bytes\n", len);
if(DEBUG_VERBOSE) { if(DEBUG_VERBOSE) {
int i; int i;
@ -455,7 +465,11 @@ coap_send_message(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
PRINTF("\n"); PRINTF("\n");
} }
} }
return ret;
} }
PRINTF("CoAP-IPv4: failed to send - no socket\n");
return -1;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* DTLS */ /* DTLS */

View File

@ -356,7 +356,7 @@ coap_receive(const coap_endpoint_t *src,
message->mid); message->mid);
coap_set_payload(message, coap_error_message, coap_set_payload(message, coap_error_message,
strlen(coap_error_message)); strlen(coap_error_message));
coap_send_message(src, payload, coap_serialize_message(message, payload)); coap_sendto(src, payload, coap_serialize_message(message, payload));
} }
/* if(new data) */ /* if(new data) */

View File

@ -179,7 +179,7 @@ simple_reply(coap_message_type_t type, const coap_endpoint_t *endpoint,
coap_init_message(response, type, NO_ERROR, notification->mid); coap_init_message(response, type, NO_ERROR, notification->mid);
len = coap_serialize_message(response, coap_databuf()); len = coap_serialize_message(response, coap_databuf());
coap_send_message(endpoint, coap_databuf(), len); coap_sendto(endpoint, coap_databuf(), len);
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
static coap_notification_flag_t static coap_notification_flag_t

View File

@ -100,8 +100,8 @@ coap_separate_accept(coap_packet_t *coap_req, coap_separate_t *separate_store)
/* ACK with empty code (0) */ /* ACK with empty code (0) */
coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid); coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid);
/* serializing into IPBUF: Only overwrites header parts that are already parsed into the request struct */ /* serializing into IPBUF: Only overwrites header parts that are already parsed into the request struct */
coap_send_message(ep, coap_databuf(), coap_sendto(ep, coap_databuf(),
coap_serialize_message(ack, coap_databuf())); coap_serialize_message(ack, coap_databuf()));
} }
} }

View File

@ -98,7 +98,7 @@ coap_send_transaction(coap_transaction_t *t)
{ {
PRINTF("Sending transaction %u\n", t->mid); PRINTF("Sending transaction %u\n", t->mid);
coap_send_message(&t->endpoint, t->packet, t->packet_len); coap_sendto(&t->endpoint, t->packet, t->packet_len);
if(COAP_TYPE_CON == if(COAP_TYPE_CON ==
((COAP_HEADER_TYPE_MASK & t->packet[0]) >> COAP_HEADER_TYPE_POSITION)) { ((COAP_HEADER_TYPE_MASK & t->packet[0]) >> COAP_HEADER_TYPE_POSITION)) {

View File

@ -42,8 +42,7 @@
void coap_transport_init(void); void coap_transport_init(void);
void coap_send_message(const coap_endpoint_t *ep, const uint8_t *data, int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len);
uint16_t length);
uint8_t *coap_databuf(void); uint8_t *coap_databuf(void);
uint16_t coap_datalen(void); uint16_t coap_datalen(void);

View File

@ -335,31 +335,38 @@ process_data(void)
coap_receive(get_src_endpoint(0), uip_appdata, uip_datalen()); coap_receive(get_src_endpoint(0), uip_appdata, uip_datalen());
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void int
coap_send_message(const coap_endpoint_t *ep, const uint8_t *data, coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t length)
uint16_t length)
{ {
if(ep == NULL) { if(ep == NULL) {
PRINTF("coap-uip: failed to send - no endpoint\n"); PRINTF("coap-uip: failed to send - no endpoint\n");
return; return -1;
} }
if(!coap_endpoint_is_connected(ep)) { if(!coap_endpoint_is_connected(ep)) {
PRINTF("coap-uip: endpoint "); PRINTF("coap-uip: endpoint ");
PRINTEP(ep); PRINTEP(ep);
PRINTF(" not connected - dropping packet\n"); PRINTF(" not connected - dropping packet\n");
return; return -1;
} }
#ifdef WITH_DTLS #ifdef WITH_DTLS
if(coap_endpoint_is_secure(ep)) { if(coap_endpoint_is_secure(ep)) {
if(dtls_context) { if(dtls_context) {
dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, length); int ret;
ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, length);
PRINTF("coap-uip: sent DTLS to "); PRINTF("coap-uip: sent DTLS to ");
PRINTEP(ep); PRINTEP(ep);
PRINTF(" %u bytes\n", length); if(ret < 0) {
PRINTF(" - error %d\n", ret);
} else {
PRINTF(" %d/%u bytes\n", ret, length);
}
return ret;
} }
return; PRINTF("coap-uip: no DTLS context\n");
return -1;
} }
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
@ -367,6 +374,7 @@ coap_send_message(const coap_endpoint_t *ep, const uint8_t *data,
PRINTF("coap-uip: sent to "); PRINTF("coap-uip: sent to ");
PRINTEP(ep); PRINTEP(ep);
PRINTF(" %u bytes\n", length); PRINTF(" %u bytes\n", length);
return length;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
PROCESS_THREAD(coap_engine, ev, data) PROCESS_THREAD(coap_engine, ev, data)