xref: /third_party/libcoap/src/coap_io_riot.c (revision c87c5fba)
1/* coap_io_riot.c -- Default network I/O functions for libcoap on RIOT
2 *
3 * Copyright (C) 2019 Olaf Bergmann <bergmann@tzi.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
11/**
12 * @file coap_io_riot.c
13 * @brief RIOT specific I/O functions
14 */
15
16#include "coap3/coap_internal.h"
17
18#ifdef HAVE_STDIO_H
19#  include <stdio.h>
20#endif
21
22#ifdef HAVE_SYS_SOCKET_H
23# include <sys/socket.h>
24# define OPTVAL_T(t)         (t)
25# define OPTVAL_GT(t)        (t)
26#endif
27#ifdef HAVE_SYS_IOCTL_H
28#include <sys/ioctl.h>
29#endif
30#ifdef HAVE_NETINET_IN_H
31# include <netinet/in.h>
32#endif
33#ifdef HAVE_SYS_UIO_H
34# include <sys/uio.h>
35#endif
36#ifdef HAVE_UNISTD_H
37# include <unistd.h>
38#endif
39
40#include "net/gnrc.h"
41#include "net/gnrc/ipv6.h"
42#include "net/gnrc/netreg.h"
43#include "net/udp.h"
44
45#include "coap3/coap_riot.h"
46
47/*
48 * dgram
49 * return +ve Number of bytes written.
50 *         -1 Error error in errno).
51 */
52ssize_t
53coap_socket_send(coap_socket_t *sock,
54                 const coap_session_t *session,
55                 const uint8_t *data,
56                 size_t datalen) {
57  ssize_t bytes_written = 0;
58
59  if (!coap_debug_send_packet()) {
60    bytes_written = (ssize_t)datalen;
61  } else if (sock->flags & COAP_SOCKET_CONNECTED) {
62    bytes_written = send(sock->fd, data, datalen, 0);
63  } else {
64    bytes_written = sendto(sock->fd, data, datalen, 0,
65                           &session->addr_info.remote.addr.sa,
66                           session->addr_info.remote.size);
67  }
68
69  if (bytes_written < 0)
70    coap_log_crit("coap_socket_send: %s\n", coap_socket_strerror());
71
72  return bytes_written;
73}
74
75static msg_t _msg_q[LIBCOAP_MSG_QUEUE_SIZE];
76
77void
78coap_riot_startup(void) {
79  msg_init_queue(_msg_q, LIBCOAP_MSG_QUEUE_SIZE);
80}
81