38 |
#include "lwip/api_msg.h" |
#include "lwip/api_msg.h" |
39 |
#include "lwip/memp.h" |
#include "lwip/memp.h" |
40 |
|
|
41 |
/*-----------------------------------------------------------------------------------*/ |
|
42 |
struct |
struct |
43 |
netbuf *netbuf_new(void) |
netbuf *netbuf_new(void) |
44 |
{ |
{ |
53 |
return NULL; |
return NULL; |
54 |
} |
} |
55 |
} |
} |
56 |
/*-----------------------------------------------------------------------------------*/ |
|
57 |
void |
void |
58 |
netbuf_delete(struct netbuf *buf) |
netbuf_delete(struct netbuf *buf) |
59 |
{ |
{ |
65 |
memp_free(MEMP_NETBUF, buf); |
memp_free(MEMP_NETBUF, buf); |
66 |
} |
} |
67 |
} |
} |
68 |
/*-----------------------------------------------------------------------------------*/ |
|
69 |
void * |
void * |
70 |
netbuf_alloc(struct netbuf *buf, u16_t size) |
netbuf_alloc(struct netbuf *buf, u16_t size) |
71 |
{ |
{ |
80 |
buf->ptr = buf->p; |
buf->ptr = buf->p; |
81 |
return buf->p->payload; |
return buf->p->payload; |
82 |
} |
} |
83 |
/*-----------------------------------------------------------------------------------*/ |
|
84 |
void |
void |
85 |
netbuf_free(struct netbuf *buf) |
netbuf_free(struct netbuf *buf) |
86 |
{ |
{ |
89 |
} |
} |
90 |
buf->p = buf->ptr = NULL; |
buf->p = buf->ptr = NULL; |
91 |
} |
} |
92 |
/*-----------------------------------------------------------------------------------*/ |
|
93 |
void |
void |
94 |
netbuf_ref(struct netbuf *buf, void *dataptr, u16_t size) |
netbuf_ref(struct netbuf *buf, void *dataptr, u16_t size) |
95 |
{ |
{ |
101 |
buf->p->len = buf->p->tot_len = size; |
buf->p->len = buf->p->tot_len = size; |
102 |
buf->ptr = buf->p; |
buf->ptr = buf->p; |
103 |
} |
} |
104 |
/*-----------------------------------------------------------------------------------*/ |
|
105 |
void |
void |
106 |
netbuf_chain(struct netbuf *head, struct netbuf *tail) |
netbuf_chain(struct netbuf *head, struct netbuf *tail) |
107 |
{ |
{ |
109 |
head->ptr = head->p; |
head->ptr = head->p; |
110 |
memp_free(MEMP_NETBUF, tail); |
memp_free(MEMP_NETBUF, tail); |
111 |
} |
} |
112 |
/*-----------------------------------------------------------------------------------*/ |
|
113 |
u16_t |
u16_t |
114 |
netbuf_len(struct netbuf *buf) |
netbuf_len(struct netbuf *buf) |
115 |
{ |
{ |
116 |
return buf->p->tot_len; |
return buf->p->tot_len; |
117 |
} |
} |
118 |
/*-----------------------------------------------------------------------------------*/ |
|
119 |
err_t |
err_t |
120 |
netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len) |
netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len) |
121 |
{ |
{ |
126 |
*len = buf->ptr->len; |
*len = buf->ptr->len; |
127 |
return ERR_OK; |
return ERR_OK; |
128 |
} |
} |
129 |
/*-----------------------------------------------------------------------------------*/ |
|
130 |
s8_t |
s8_t |
131 |
netbuf_next(struct netbuf *buf) |
netbuf_next(struct netbuf *buf) |
132 |
{ |
{ |
139 |
} |
} |
140 |
return 0; |
return 0; |
141 |
} |
} |
142 |
/*-----------------------------------------------------------------------------------*/ |
|
143 |
void |
void |
144 |
netbuf_first(struct netbuf *buf) |
netbuf_first(struct netbuf *buf) |
145 |
{ |
{ |
146 |
buf->ptr = buf->p; |
buf->ptr = buf->p; |
147 |
} |
} |
148 |
/*-----------------------------------------------------------------------------------*/ |
|
149 |
void |
void |
150 |
netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset) |
netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset) |
151 |
{ |
{ |
154 |
|
|
155 |
left = 0; |
left = 0; |
156 |
|
|
157 |
if (buf == NULL) { |
if(buf == NULL || dataptr == NULL) { |
158 |
return; |
return; |
159 |
} |
} |
160 |
|
|
174 |
} |
} |
175 |
} |
} |
176 |
} |
} |
177 |
/*-----------------------------------------------------------------------------------*/ |
|
178 |
void |
void |
179 |
netbuf_copy(struct netbuf *buf, void *dataptr, u16_t len) |
netbuf_copy(struct netbuf *buf, void *dataptr, u16_t len) |
180 |
{ |
{ |
181 |
netbuf_copy_partial(buf, dataptr, len, 0); |
netbuf_copy_partial(buf, dataptr, len, 0); |
182 |
} |
} |
183 |
/*-----------------------------------------------------------------------------------*/ |
|
184 |
struct ip_addr * |
struct ip_addr * |
185 |
netbuf_fromaddr(struct netbuf *buf) |
netbuf_fromaddr(struct netbuf *buf) |
186 |
{ |
{ |
187 |
return buf->fromaddr; |
return buf->fromaddr; |
188 |
} |
} |
189 |
/*-----------------------------------------------------------------------------------*/ |
|
190 |
u16_t |
u16_t |
191 |
netbuf_fromport(struct netbuf *buf) |
netbuf_fromport(struct netbuf *buf) |
192 |
{ |
{ |
193 |
return buf->fromport; |
return buf->fromport; |
194 |
} |
} |
195 |
/*-----------------------------------------------------------------------------------*/ |
|
196 |
struct |
struct |
197 |
netconn *netconn_new(enum netconn_type t) |
netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto, |
198 |
|
void (*callback)(struct netconn *, enum netconn_evt, u16_t len)) |
199 |
{ |
{ |
200 |
struct netconn *conn; |
struct netconn *conn; |
201 |
|
struct api_msg *msg; |
202 |
|
|
203 |
conn = memp_malloc(MEMP_NETCONN); |
conn = memp_malloc(MEMP_NETCONN); |
204 |
if (conn == NULL) { |
if (conn == NULL) { |
205 |
return NULL; |
return NULL; |
206 |
} |
} |
207 |
|
|
208 |
|
conn->err = ERR_OK; |
209 |
conn->type = t; |
conn->type = t; |
210 |
conn->pcb.tcp = NULL; |
conn->pcb.tcp = NULL; |
211 |
|
|
218 |
conn->sem = SYS_SEM_NULL; |
conn->sem = SYS_SEM_NULL; |
219 |
conn->state = NETCONN_NONE; |
conn->state = NETCONN_NONE; |
220 |
conn->socket = 0; |
conn->socket = 0; |
221 |
conn->callback = 0; |
conn->callback = callback; |
222 |
conn->recv_avail = 0; |
conn->recv_avail = 0; |
223 |
|
|
224 |
|
if((msg = memp_malloc(MEMP_API_MSG)) == NULL) { |
225 |
|
memp_free(MEMP_NETCONN, conn); |
226 |
|
return NULL; |
227 |
|
} |
228 |
|
|
229 |
|
msg->type = API_MSG_NEWCONN; |
230 |
|
msg->msg.msg.bc.port = proto; /* misusing the port field */ |
231 |
|
msg->msg.conn = conn; |
232 |
|
api_msg_post(msg); |
233 |
|
sys_mbox_fetch(conn->mbox, NULL); |
234 |
|
memp_free(MEMP_API_MSG, msg); |
235 |
|
|
236 |
|
if ( conn->err != ERR_OK ) { |
237 |
|
memp_free(MEMP_NETCONN, conn); |
238 |
|
return NULL; |
239 |
|
} |
240 |
|
|
241 |
return conn; |
return conn; |
242 |
} |
} |
243 |
/*-----------------------------------------------------------------------------------*/ |
|
244 |
|
|
245 |
|
struct |
246 |
|
netconn *netconn_new(enum netconn_type t) |
247 |
|
{ |
248 |
|
return netconn_new_with_proto_and_callback(t,0,NULL); |
249 |
|
} |
250 |
|
|
251 |
struct |
struct |
252 |
netconn *netconn_new_with_callback(enum netconn_type t, |
netconn *netconn_new_with_callback(enum netconn_type t, |
253 |
void (*callback)(struct netconn *, enum netconn_evt, u16_t len)) |
void (*callback)(struct netconn *, enum netconn_evt, u16_t len)) |
254 |
{ |
{ |
255 |
struct netconn *conn; |
return netconn_new_with_proto_and_callback(t,0,callback); |
|
|
|
|
/* get a netconn and then initialize callback pointer and socket */ |
|
|
conn = netconn_new(t); |
|
|
if (conn) |
|
|
conn->callback = callback; |
|
|
return conn; |
|
256 |
} |
} |
257 |
|
|
258 |
/*-----------------------------------------------------------------------------------*/ |
|
259 |
err_t |
err_t |
260 |
netconn_delete(struct netconn *conn) |
netconn_delete(struct netconn *conn) |
261 |
{ |
{ |
309 |
memp_free(MEMP_NETCONN, conn); |
memp_free(MEMP_NETCONN, conn); |
310 |
return ERR_OK; |
return ERR_OK; |
311 |
} |
} |
312 |
/*-----------------------------------------------------------------------------------*/ |
|
313 |
enum netconn_type |
enum netconn_type |
314 |
netconn_type(struct netconn *conn) |
netconn_type(struct netconn *conn) |
315 |
{ |
{ |
316 |
return conn->type; |
return conn->type; |
317 |
} |
} |
318 |
/*-----------------------------------------------------------------------------------*/ |
|
319 |
err_t |
err_t |
320 |
netconn_peer(struct netconn *conn, struct ip_addr *addr, |
netconn_peer(struct netconn *conn, struct ip_addr *addr, |
321 |
u16_t *port) |
u16_t *port) |
322 |
{ |
{ |
323 |
switch (conn->type) { |
switch (conn->type) { |
324 |
|
case NETCONN_RAW: |
325 |
|
/* return an error as connecting is only a helper for upper layers */ |
326 |
|
return ERR_CONN; |
327 |
case NETCONN_UDPLITE: |
case NETCONN_UDPLITE: |
328 |
case NETCONN_UDPNOCHKSUM: |
case NETCONN_UDPNOCHKSUM: |
329 |
case NETCONN_UDP: |
case NETCONN_UDP: |
342 |
} |
} |
343 |
return (conn->err = ERR_OK); |
return (conn->err = ERR_OK); |
344 |
} |
} |
345 |
/*-----------------------------------------------------------------------------------*/ |
|
346 |
err_t |
err_t |
347 |
netconn_addr(struct netconn *conn, struct ip_addr **addr, |
netconn_addr(struct netconn *conn, struct ip_addr **addr, |
348 |
u16_t *port) |
u16_t *port) |
349 |
{ |
{ |
350 |
switch (conn->type) { |
switch (conn->type) { |
351 |
|
case NETCONN_RAW: |
352 |
|
*addr = &(conn->pcb.raw->local_ip); |
353 |
|
*port = conn->pcb.raw->protocol; |
354 |
|
break; |
355 |
case NETCONN_UDPLITE: |
case NETCONN_UDPLITE: |
356 |
case NETCONN_UDPNOCHKSUM: |
case NETCONN_UDPNOCHKSUM: |
357 |
case NETCONN_UDP: |
case NETCONN_UDP: |
365 |
} |
} |
366 |
return (conn->err = ERR_OK); |
return (conn->err = ERR_OK); |
367 |
} |
} |
368 |
/*-----------------------------------------------------------------------------------*/ |
|
369 |
err_t |
err_t |
370 |
netconn_bind(struct netconn *conn, struct ip_addr *addr, |
netconn_bind(struct netconn *conn, struct ip_addr *addr, |
371 |
u16_t port) |
u16_t port) |
396 |
return conn->err; |
return conn->err; |
397 |
} |
} |
398 |
|
|
399 |
/*-----------------------------------------------------------------------------------*/ |
|
400 |
err_t |
err_t |
401 |
netconn_connect(struct netconn *conn, struct ip_addr *addr, |
netconn_connect(struct netconn *conn, struct ip_addr *addr, |
402 |
u16_t port) |
u16_t port) |
447 |
return conn->err; |
return conn->err; |
448 |
|
|
449 |
} |
} |
450 |
/*-----------------------------------------------------------------------------------*/ |
|
451 |
err_t |
err_t |
452 |
netconn_listen(struct netconn *conn) |
netconn_listen(struct netconn *conn) |
453 |
{ |
{ |
474 |
memp_free(MEMP_API_MSG, msg); |
memp_free(MEMP_API_MSG, msg); |
475 |
return conn->err; |
return conn->err; |
476 |
} |
} |
477 |
/*-----------------------------------------------------------------------------------*/ |
|
478 |
struct netconn * |
struct netconn * |
479 |
netconn_accept(struct netconn *conn) |
netconn_accept(struct netconn *conn) |
480 |
{ |
{ |
491 |
|
|
492 |
return newconn; |
return newconn; |
493 |
} |
} |
494 |
/*-----------------------------------------------------------------------------------*/ |
|
495 |
struct netbuf * |
struct netbuf * |
496 |
netconn_recv(struct netconn *conn) |
netconn_recv(struct netconn *conn) |
497 |
{ |
{ |
587 |
|
|
588 |
return buf; |
return buf; |
589 |
} |
} |
590 |
/*-----------------------------------------------------------------------------------*/ |
|
591 |
err_t |
err_t |
592 |
netconn_send(struct netconn *conn, struct netbuf *buf) |
netconn_send(struct netconn *conn, struct netbuf *buf) |
593 |
{ |
{ |
615 |
memp_free(MEMP_API_MSG, msg); |
memp_free(MEMP_API_MSG, msg); |
616 |
return conn->err; |
return conn->err; |
617 |
} |
} |
618 |
/*-----------------------------------------------------------------------------------*/ |
|
619 |
err_t |
err_t |
620 |
netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy) |
netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy) |
621 |
{ |
{ |
691 |
|
|
692 |
return conn->err; |
return conn->err; |
693 |
} |
} |
694 |
/*-----------------------------------------------------------------------------------*/ |
|
695 |
err_t |
err_t |
696 |
netconn_close(struct netconn *conn) |
netconn_close(struct netconn *conn) |
697 |
{ |
{ |
719 |
memp_free(MEMP_API_MSG, msg); |
memp_free(MEMP_API_MSG, msg); |
720 |
return conn->err; |
return conn->err; |
721 |
} |
} |
722 |
/*-----------------------------------------------------------------------------------*/ |
|
723 |
err_t |
err_t |
724 |
netconn_err(struct netconn *conn) |
netconn_err(struct netconn *conn) |
725 |
{ |
{ |
726 |
return conn->err; |
return conn->err; |
727 |
} |
} |
728 |
/*-----------------------------------------------------------------------------------*/ |
|