forked from attie/libxbee3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_io.c
214 lines (175 loc) · 5.65 KB
/
net_io.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
/*
libxbee - a C/C++ library to aid the use of Digi's XBee wireless modules
running in API mode.
Copyright (C) 2009 onwards Attie Grande (attie@attie.co.uk)
libxbee is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libxbee is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* this file is used by the network client... so is not masked out by XBEE_NO_NET_SERVER */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "internal.h"
#include "xbee_int.h"
#include "mode.h"
#include "net.h"
#include "net_io.h"
#include "thread.h"
#include "ll.h"
/* for the dual-purpose-ness */
#include "modes/net/mode.h"
/* ######################################################################### */
xbee_err xbee_netRx(struct xbee *xbee, void *arg, struct xbee_tbuf **buf) {
char c;
char length[2];
int pos, len, ret;
struct xbee_tbuf *iBuf;
int fd;
if (!xbee || !buf) return XBEE_EMISSINGPARAM;
if (arg) {
/* this is on the server end */
struct xbee_netClientInfo *info;
info = arg;
if (xbee != info->xbee) return XBEE_EINVAL;
fd = info->fd;
} else {
/* this is on the client end */
struct xbee_modeData *info;
info = xbee->modeData;
fd = info->netInfo.fd;
}
while (1) {
do {
if ((ret = recv(fd, &c, 1, MSG_NOSIGNAL)) < 0) return XBEE_EIO;
if (ret == 0) goto eof;
} while (c != 0x7E);
for (len = 2, pos = 0; pos < len; pos += ret) {
ret = recv(fd, &(length[pos]), len - pos, MSG_NOSIGNAL);
if (ret > 0) continue;
if (ret == 0) goto eof;
return XBEE_EIO;
}
len = (((length[0] << 8) & 0xFF00) | (length[1] & 0xFF)) + 1;
if ((iBuf = malloc(sizeof(*iBuf) + len)) == NULL) return XBEE_ENOMEM;
xbee_ll_add_tail(needsFree, iBuf);
iBuf->len = len;
memset(&iBuf->ts, 0, sizeof(iBuf->ts));
for (pos = 0; pos < iBuf->len; pos += ret) {
ret = recv(fd, &(iBuf->data[pos]), iBuf->len - pos, MSG_NOSIGNAL);
if (ret > 0) continue;
xbee_ll_ext_item(needsFree, iBuf);
free(iBuf);
if (ret == 0) goto eof;
return XBEE_EIO;
}
break;
}
/* needs free is handled for us by xbee_rxHandler(), we just need to register it */
*buf = iBuf;
return XBEE_ENONE;
eof:
if (arg) {
struct xbee_netClientInfo *info;
#ifndef XBEE_NO_NET_SERVER
struct xbee_netClientInfo *deadClient;
#endif
struct xbee_con *con;
info = arg;
#ifndef XBEE_NO_NET_SERVER
/* tidy up any dead clients - not including us */
while (xbee_ll_ext_head(netDeadClientList, (void**)&deadClient) == XBEE_ENONE && deadClient != NULL) {
xbee_netClientShutdown(deadClient);
}
/* xbee_netRx() is responsible for free()ing memory and killing off client threads on the server
to do this, we need to add ourselves to the netDeadClientList, and remove ourselves from the clientList
the server thread will then cleanup any clients on the next accept() */
xbee_ll_add_tail(netDeadClientList, arg);
xbee_ll_ext_item(xbee->netInfo->clientList, arg);
#endif /* XBEE_NO_NET_SERVER */
/* kill the other threads */
/* excluding the rx thread... thats us! */
if (info->rxHandlerThread) {
xbee_threadKillJoin(info->xbee, info->rxHandlerThread, NULL);
info->rxHandlerThread = NULL;
}
if (info->txThread) {
xbee_threadKillJoin(info->xbee, info->txThread, NULL);
info->txThread = NULL;
}
/* close up the socket */
shutdown(info->fd, SHUT_RDWR);
xsys_close(info->fd);
info->fd = -1; /* <-- mark it closed */
/* end all of our connections */
for (con = NULL; xbee_ll_ext_head(info->conList, (void **)&con) == XBEE_ENONE && con; ) {
xbee_conEnd(con);
}
/* this leaves us with a call to xbee_threadKillJoin() and xbee_netClientFree() left! */
}
return XBEE_EEOF;
}
xbee_err xbee_netTx(struct xbee *xbee, void *arg, struct xbee_sbuf *buf) {
int pos, ret;
int fd;
size_t txSize;
size_t memSize;
struct xbee_buf *iBuf;
size_t *txBufSize;
struct xbee_buf **txBuf;
if (!xbee || !buf) return XBEE_EMISSINGPARAM;
if (arg) {
/* this is on the server end */
struct xbee_netClientInfo *info;
info = arg;
if (xbee != info->xbee) return XBEE_EINVAL;
fd = info->fd;
txBufSize = &info->txBufSize;
txBuf = &info->txBuf;
} else {
/* this is on the client end */
struct xbee_modeData *info;
info = xbee->modeData;
fd = info->netInfo.fd;
txBufSize = &info->netInfo.txBufSize;
txBuf = &info->netInfo.txBuf;
}
txSize = 3 + buf->len;
memSize = txSize + sizeof(*iBuf);
iBuf = *txBuf;
if (!iBuf || *txBufSize < memSize) {
void *p;
/* make sure we save this buffer... */
xbee_ll_lock(needsFree);
if ((p = realloc(iBuf, memSize)) == NULL) {
xbee_ll_unlock(needsFree);
return XBEE_ENOMEM;
}
if (iBuf) _xbee_ll_ext_item(needsFree, iBuf, 0);
_xbee_ll_add_tail(needsFree, p, 0);
xbee_ll_unlock(needsFree);
iBuf = p;
*txBuf = iBuf;
*txBufSize = memSize;
}
iBuf->len = txSize;
iBuf->data[0] = 0x7E;
iBuf->data[1] = ((buf->len - 1) >> 8) & 0xFF;
iBuf->data[2] = ((buf->len - 1) ) & 0xFF;
memcpy(&(iBuf->data[3]), buf->data, buf->len);
for (pos = 0; pos < iBuf->len; pos += ret) {
ret = send(fd, iBuf->data, iBuf->len - pos, MSG_NOSIGNAL);
if (ret >= 0) continue;
return XBEE_EIO;
}
return XBEE_ENONE;
}