mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-28 01:27:09 +03:00
[RIOT] coap_io.c: coap_run_once() for RIOT now in coap_io_riot.c
This commit is contained in:
1 parent
5d930d8683
commit
9a9b69a58f
2 files changed
+99
-1
No files matched your search
@@ -1209,6 +1209,7 @@ coap_io_prepare_io(coap_context_t *ctx,
|
||||
return (unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
|
||||
}
|
||||
|
||||
#ifndef RIOT_VERSION
|
||||
int
|
||||
coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) {
|
||||
return coap_io_process_with_fds(ctx, timeout_ms, 0, NULL, NULL, NULL);
|
||||
@@ -1403,6 +1404,7 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms,
|
||||
|
||||
return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND);
|
||||
}
|
||||
#endif /* RIOT_VERSION */
|
||||
|
||||
#else /* WITH_CONTIKI */
|
||||
int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) {
|
||||
|
||||
+97
-1
@@ -45,6 +45,8 @@
|
||||
#include "utlist.h"
|
||||
#include "resource.h"
|
||||
|
||||
#include "coap_riot.h"
|
||||
|
||||
ssize_t
|
||||
coap_network_send(coap_socket_t *sock,
|
||||
const coap_session_t *session,
|
||||
@@ -68,6 +70,12 @@ coap_network_send(coap_socket_t *sock,
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
static udp_hdr_t *
|
||||
get_udp_header(gnrc_pktsnip_t *pkt) {
|
||||
gnrc_pktsnip_t *udp = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_UDP);
|
||||
return udp ? (udp_hdr_t *)udp->data : NULL;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet) {
|
||||
size_t len;
|
||||
@@ -88,7 +96,6 @@ coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet) {
|
||||
sock->flags &= ~COAP_SOCKET_CAN_READ;
|
||||
}
|
||||
|
||||
coap_log(LOG_DEBUG, "libcoap_read_pkt called with pkt:\n");
|
||||
/* Search for the transport header in the packet received from the
|
||||
* network interface driver. */
|
||||
udp = gnrc_pktsnip_search_type(sock->pkt, type);
|
||||
@@ -134,3 +141,92 @@ coap_network_read(coap_socket_t *sock, struct coap_packet_t *packet) {
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static msg_t _msg_q[LIBCOAP_MSG_QUEUE_SIZE];
|
||||
|
||||
void
|
||||
coap_riot_startup(void) {
|
||||
msg_init_queue(_msg_q, LIBCOAP_MSG_QUEUE_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the port of @p addr in network byte order or 0 on error.
|
||||
*/
|
||||
static uint16_t
|
||||
get_port(const coap_address_t *addr) {
|
||||
if (addr) {
|
||||
switch (addr->addr.sa.sa_family) {
|
||||
case AF_INET: return addr->addr.sin.sin_port;
|
||||
case AF_INET6: return addr->addr.sin6.sin6_port;
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
coap_run_once(coap_context_t *ctx, unsigned timeout_ms) {
|
||||
coap_tick_t before, now;
|
||||
coap_socket_t *sockets[LIBCOAP_MAX_SOCKETS];
|
||||
unsigned int num_sockets = 0, timeout;
|
||||
gnrc_netreg_entry_t coap_reg =
|
||||
GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, thread_getpid());
|
||||
msg_t msg;
|
||||
|
||||
coap_ticks(&before);
|
||||
|
||||
timeout = coap_write(ctx, sockets, (unsigned int)(sizeof(sockets) / sizeof(sockets[0])), &num_sockets, before);
|
||||
if (timeout == 0 || timeout_ms < timeout)
|
||||
timeout = timeout_ms;
|
||||
|
||||
if (num_sockets > 0) {
|
||||
gnrc_netreg_register(GNRC_NETTYPE_UDP, &coap_reg);
|
||||
}
|
||||
|
||||
if (timeout == 0 || timeout_ms < timeout)
|
||||
timeout = timeout_ms;
|
||||
|
||||
xtimer_msg_receive_timeout(&msg, timeout_ms * US_PER_SEC);
|
||||
switch (msg.type) {
|
||||
case GNRC_NETAPI_MSG_TYPE_RCV: {
|
||||
coap_endpoint_t *ep;
|
||||
udp_hdr_t *udp_hdr = get_udp_header((gnrc_pktsnip_t *)msg.content.ptr);
|
||||
if (!udp_hdr)
|
||||
break;
|
||||
|
||||
/* set read flag only for the destination of the received packet */
|
||||
LL_FOREACH(ctx->endpoint, ep) {
|
||||
if (get_port(&ep->bind_addr) == udp_hdr->dst_port.u16) {
|
||||
for (unsigned int i = 0; i < num_sockets; i++) {
|
||||
if ((ep->sock.fd == sockets[i]->fd) &&
|
||||
(sockets[i]->flags & COAP_SOCKET_WANT_READ)) {
|
||||
coap_log(LOG_DEBUG, "fd %d on port %u can read\n",
|
||||
ep->sock.fd, ntohs(get_port(&ep->bind_addr)));
|
||||
sockets[i]->flags |= COAP_SOCKET_CAN_READ;
|
||||
sockets[i]->pkt = msg.content.ptr;
|
||||
break; /* found one, finish loop */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GNRC_NETAPI_MSG_TYPE_SND:
|
||||
break;
|
||||
case GNRC_NETAPI_MSG_TYPE_SET:
|
||||
/* fall through */
|
||||
case GNRC_NETAPI_MSG_TYPE_GET:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
coap_ticks(&now);
|
||||
coap_read(ctx, now);
|
||||
|
||||
/* cleanup */
|
||||
gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &coap_reg);
|
||||
|
||||
return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND);
|
||||
}
|
||||
Reference in new issue
Block a user