This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathrx.c
273 lines (220 loc) · 8.09 KB
/
rx.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
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 ([email protected])
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/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "internal.h"
#include "rx.h"
#include "xbee_int.h"
#include "mode.h"
#include "frame.h"
#include "conn.h"
#include "log.h"
#include "ll.h"
xbee_err xbee_rxAlloc(struct xbee_rxInfo **nInfo) {
static char logColor = 1;
size_t memSize;
struct xbee_rxInfo *info;
if (!nInfo) return XBEE_EMISSINGPARAM;
memSize = sizeof(*info);
if (!(info = malloc(memSize))) return XBEE_ENOMEM;
memset(info, 0, memSize);
info->bufList = xbee_ll_alloc();
xsys_sem_init(&info->sem);
/* give it a log color */
info->logColor = logColor;
if (logColor++ > 7) logColor = 7;
*nInfo = info;
return XBEE_ENONE;
}
xbee_err xbee_rxFree(struct xbee_rxInfo *info) {
if (!info) return XBEE_EMISSINGPARAM;
xbee_ll_free(info->bufList, (void(*)(void*))xbee_pktFree);
xsys_sem_destroy(&info->sem);
free(info);
return XBEE_ENONE;
}
/* ######################################################################### */
xbee_err xbee_rx(struct xbee *xbee, int *restart, void *arg) {
xbee_err ret;
struct xbee_rxInfo *info;
struct xbee_tbuf *buf;
info = arg;
if (!info->bufList || !info->ioFunc) {
*restart = 0;
return XBEE_EINVAL;
}
while (!xbee->die) {
buf = NULL;
if ((ret = info->ioFunc(xbee, info->ioArg, &buf)) != XBEE_ENONE) {
if (ret == XBEE_EEOF) {
*restart = 0;
if (info->eofCallback) info->eofCallback(xbee, info);
return XBEE_EEOF;
} else if (ret == XBEE_ESHUTDOWN && xbee->die) {
break;
}
xbee_log(1, "rx() returned %d (%s)... retrying in 10 ms", ret, xbee_errorToStr(ret));
usleep(10000); /* 10 ms */
continue;
}
#ifndef XBEE_DISABLE_LOGGING
#ifndef XBEE_LOG_NO_RX
if (xbee->log->enable_rx) {
/* format: tx[0x0000000000000000] */
char label[42]; /* enough space for a 64-bit pointer and ANSI color codes */
#ifndef XBEE_LOG_NO_COLOR
if (xbee->log->use_color) {
snprintf(label, sizeof(label), "Rx[%c[%dm%p%c[0m]", 27, 30 + info->logColor, info, 27);
} else {
#endif /* !XBEE_LOG_NO_COLOR */
snprintf(label, sizeof(label), "Rx[%p]", info);
#ifndef XBEE_LOG_NO_COLOR
}
#endif /* !XBEE_LOG_NO_COLOR */
xbee_logData(25, label, buf->data, buf->len);
}
#endif /* !XBEE_LOG_NO_RX */
#endif /* !XBEE_DISABLE_LOGGING */
if (xbee_ll_add_tail(info->bufList, buf) != XBEE_ENONE) return XBEE_ELINKEDLIST;
buf = NULL;
if (xsys_sem_post(&info->sem) != 0) return XBEE_ESEMAPHORE;
}
return XBEE_ESHUTDOWN;
}
/* ######################################################################### */
xbee_err xbee_rxHandler(struct xbee *xbee, int *restart, void *arg) {
xbee_err ret;
struct xbee_rxInfo *info;
struct xbee_tbuf *buf;
struct xbee_modeConType *conType;
struct xbee_frameInfo frameInfo;
struct xbee_conAddress address;
struct xbee_pkt *pkt;
struct xbee_con *con;
ret = XBEE_ENONE;
info = arg;
buf = NULL;
if (!info->bufList) {
*restart = 0;
return XBEE_EINVAL;
}
memset(&frameInfo, 0, sizeof(frameInfo));
conType = NULL;
while (!xbee->die) {
/* this is here so that it will be triggered AFTER the packet has been queued (assuming a packet needed to be queued)
handle any frame info (prod someone who may be waiting for ACK/NAK/etc...) */
if (info->fBlock && frameInfo.active != 0 && conType && conType->allowFrameId != 0) {
xbee_log(20, "received Tx status (block: %p, frame: 0x%02X, status: 0x%02X)", info->fBlock, frameInfo.id, frameInfo.retVal);
if ((ret = xbee_framePost(info->fBlock, frameInfo.id, frameInfo.retVal)) != XBEE_ENONE) {
xbee_log(2, "failed to respond to frame (block: %p, frame: 0x%02X)... xbee_framePost() returned %d", info->fBlock, frameInfo.id, ret);
ret = XBEE_ENONE;
}
}
xsys_sem_wait(&info->sem);
/* get the next buffer */
if ((ret = xbee_ll_ext_head(info->bufList, (void**)&buf)) != XBEE_ENONE && ret != XBEE_ERANGE) return XBEE_ELINKEDLIST;
ret = XBEE_ENONE;
if (!buf) continue;
/* check we actually have some data to work with... */
if (buf->len < 1) goto done;
/* locate the connection type of this buffer */
if ((ret = xbee_modeLocateConType(*info->conTypes, 1, NULL, &buf->data[0], NULL, &conType)) == XBEE_ENOTEXISTS || !conType) {
xbee_log(4, "Unknown message type recieved... (0x%02X)", buf->data[0]);
goto done;
} else if (ret != XBEE_ENONE) {
/* some other error occured */
break;
}
/* prepare the buckets */
memset(&frameInfo, 0, sizeof(frameInfo));
memset(&address, 0, sizeof(address));
pkt = NULL;
/* process the buffer into the buckets */
if ((ret = conType->rxHandler->func(xbee, info->handlerArg, conType->rxHandler->identifier, buf, &frameInfo, &address, &pkt)) != XBEE_ENONE) break;
/* its possible that the buffer ONLY contained frame information... if so, were done! */
if (!pkt) goto done;
memcpy(&pkt->address, &address, sizeof(address));
pkt->conType = conType->name;
pkt->apiIdentifier = buf->data[0];
if (info->fBlock && frameInfo.active != 0 && conType && conType->allowFrameId != 0) {
pkt->frameId = frameInfo.id;
}
/* if the packet handler didn't fill in the timestamp, then we should do it here */
if (pkt->timestamp.tv_sec == 0 && pkt->timestamp.tv_nsec == 0) {
memcpy(&pkt->timestamp, &buf->ts, sizeof(buf->ts));
}
xbee_log(12, "received '%s' type packet with %d bytes of data...", conType->name, pkt->dataLen);
/* match the address to a connection */
if (((ret = xbee_conLocate(conType->conList, &address, &con, CON_SNOOZE)) != XBEE_ENONE &&
ret != XBEE_ESLEEPING &&
ret != XBEE_ECATCHALL) ||
!con) {
xbee_pktFree(pkt);
if (ret == XBEE_ENOTEXISTS) {
xbee_log(5, "connectionless '%s' packet (%d bytes)...", conType->name, buf->len);
xbee_conLogAddress(xbee, 10, &address);
goto done;
}
xbee_log(1, "xbee_conLocate() returned %d...", ret);
break;
}
xbee_log(15, "matched packet with con @ %p", con);
xbee_conLogAddress(xbee, 16, &address);
if (conType->rxHandler->funcPost) {
xbee_err ret;
if ((ret = conType->rxHandler->funcPost(xbee, con, pkt)) != XBEE_ENONE) {
xbee_log(1, "funcPost() failed for con @ %p - returned %d\n", con, ret);
}
}
/* wake the connection if necessary */
if (con->sleepState != CON_AWAKE) {
con->sleepState = CON_AWAKE;
xbee_log(1, "woke connection @ %p", con);
}
con->info.countRx++;
con->info.lastRxTime = time(NULL);
if (!con->settings.catchAll) {
if (address.addr16_enabled && !con->address.addr16_enabled && conType->save_addr16) {
if (conType->addressTest(address.addr16, sizeof(address.addr16)) == XBEE_ENONE) {
con->address.addr16_enabled = 1;
memcpy(con->address.addr16, address.addr16, 2);
}
}
if (address.addr64_enabled && !con->address.addr64_enabled && conType->save_addr64) {
if (conType->addressTest(address.addr64, sizeof(address.addr64)) == XBEE_ENONE) {
con->address.addr64_enabled = 1;
memcpy(con->address.addr64, address.addr64, 8);
}
}
}
/* add the packet to the connection's tail! */
if ((ret = xbee_conLinkPacket(con, pkt)) != XBEE_ENONE) {
xbee_log(1, "failed to store packet with connection... xbee_conLinkPacket() returned %d", ret);
break;
}
done:
xbee_ll_ext_item(needsFree, buf);
free(buf);
buf = NULL;
}
if (buf) {
xbee_ll_ext_item(needsFree, buf);
free(buf);
}
if (xbee->die && ret == XBEE_ENONE) return XBEE_ESHUTDOWN;
return ret;
}