-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathcoap_io_lwip.c
223 lines (189 loc) · 6.15 KB
/
coap_io_lwip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* coap_io_lwip.h -- Network I/O functions for libcoap on lwIP
*
* Copyright (C) 2012,2014 Olaf Bergmann <[email protected]>
* 2014 chrysn <[email protected]>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_config.h"
#include "mem.h"
#include "coap_io.h"
#include <lwip/udp.h>
#if NO_SYS
pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_t lwprot_thread = (pthread_t)0xDEAD;
int lwprot_count = 0;
#endif
#if 0
void coap_packet_copy_source(coap_packet_t *packet, coap_address_t *target)
{
target->port = packet->srcport;
memcpy(&target->addr, ip_current_src_addr(), sizeof(ip_addr_t));
}
#endif
void coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length)
{
LWIP_ASSERT("Can only deal with contiguous PBUFs to read the initial details", packet->pbuf->tot_len == packet->pbuf->len);
*address = packet->pbuf->payload;
*length = packet->pbuf->tot_len;
}
void coap_free_packet(coap_packet_t *packet)
{
if (packet->pbuf)
pbuf_free(packet->pbuf);
coap_free_type(COAP_PACKET, packet);
}
struct pbuf *coap_packet_extract_pbuf(coap_packet_t *packet)
{
struct pbuf *ret = packet->pbuf;
packet->pbuf = NULL;
return ret;
}
/** Callback from lwIP when a package was received.
*
* The current implementation deals this to coap_dispatch immediately, but
* other mechanisms (as storing the package in a queue and later fetching it
* when coap_read is called) can be envisioned.
*
* It handles everything coap_read does on other implementations.
*/
static void coap_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
coap_endpoint_t *ep = (coap_endpoint_t*)arg;
coap_pdu_t *pdu = NULL;
coap_session_t *session;
coap_tick_t now;
coap_packet_t *packet = coap_malloc_type(COAP_PACKET, sizeof(coap_packet_t));
/* this is fatal because due to the short life of the packet, never should there be more than one coap_packet_t required */
LWIP_ASSERT("Insufficient coap_packet_t resources.", packet != NULL);
packet->pbuf = p;
packet->src.port = port;
packet->src.addr = *addr;
packet->dst.port = upcb->local_port;
packet->dst.addr = *ip_current_dest_addr();
packet->ifindex = netif_get_index(ip_current_netif());
pdu = coap_pdu_from_pbuf(p);
if (!pdu)
goto error;
if (!coap_pdu_parse(ep->proto, p->payload, p->len, pdu)) {
goto error;
}
coap_ticks(&now);
session = coap_endpoint_get_session(ep, packet, now);
if (!session)
goto error;
LWIP_ASSERT("Proto not supported for LWIP", COAP_PROTO_NOT_RELIABLE(session->proto));
coap_dispatch(ep->context, session, pdu);
coap_delete_pdu(pdu);
packet->pbuf = NULL;
coap_free_packet(packet);
return;
error:
/* FIXME: send back RST? */
if (pdu) coap_delete_pdu(pdu);
if (packet) {
packet->pbuf = NULL;
coap_free_packet(packet);
}
return;
}
coap_endpoint_t *
coap_new_endpoint(coap_context_t *context, const coap_address_t *addr, coap_proto_t proto) {
coap_endpoint_t *result;
err_t err;
LWIP_ASSERT("Proto not supported for LWIP endpoints", proto == COAP_PROTO_UDP);
result = coap_malloc_type(COAP_ENDPOINT, sizeof(coap_endpoint_t));
if (!result) return NULL;
result->sock.pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
if (result->sock.pcb == NULL) goto error;
udp_recv(result->sock.pcb, coap_recv, (void*)result);
err = udp_bind(result->sock.pcb, &addr->addr, addr->port);
if (err) {
udp_remove(result->sock.pcb);
goto error;
}
result->default_mtu = COAP_DEFAULT_MTU;
result->context = context;
result->proto = proto;
return result;
error:
coap_free_type(COAP_ENDPOINT, result);
return NULL;
}
void coap_free_endpoint(coap_endpoint_t *ep)
{
udp_remove(ep->sock.pcb);
coap_free_type(COAP_ENDPOINT, ep);
}
ssize_t
coap_socket_send_pdu(coap_socket_t *sock, coap_session_t *session,
coap_pdu_t *pdu) {
/* FIXME: we can't check this here with the existing infrastructure, but we
* should actually check that the pdu is not held by anyone but us. the
* respective pbuf is already exclusively owned by the pdu. */
pbuf_realloc(pdu->pbuf, pdu->used_size + coap_pdu_parse_header_size(session->proto, pdu->pbuf->payload));
udp_sendto(sock->pcb, pdu->pbuf, &session->remote_addr.addr,
session->remote_addr.port);
return pdu->used_size;
}
ssize_t
coap_socket_send(coap_socket_t *sock, coap_session_t *session,
const uint8_t *data, size_t data_len ) {
/* Not implemented, use coap_socket_send_pdu instead */
return -1;
}
int
coap_socket_bind_udp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
return 0;
}
int
coap_socket_connect_udp(coap_socket_t *sock,
const coap_address_t *local_if,
const coap_address_t *server,
int default_port,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
return 0;
}
int
coap_socket_connect_tcp1(coap_socket_t *sock,
const coap_address_t *local_if,
const coap_address_t *server,
int default_port,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
return 0;
}
int
coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
return 0;
}
int
coap_socket_bind_tcp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
return 0;
}
int
coap_socket_accept_tcp(coap_socket_t *server,
coap_socket_t *new_client,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
return 0;
}
ssize_t
coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
return -1;
}
ssize_t
coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
return -1;
}
void coap_socket_close(coap_socket_t *sock) {
return;
}