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 pathxbee.c
232 lines (174 loc) · 7.07 KB
/
xbee.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
/*
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 <stdarg.h>
#include <string.h>
#include "internal.h"
#include "xbee_int.h"
#include "conn.h"
#include "frame.h"
#include "thread.h"
#include "log.h"
#include "mode.h"
#include "rx.h"
#include "tx.h"
#include "net.h"
#include "ll.h"
struct xbee_ll_head *xbeeList = NULL;
struct xbee_ll_head *needsFree = NULL;
EXPORT void xbee_freeMemory(void *ptr) {
/* because the windows memory model is stupid, memory that is allocated from within
the DLL, must also be free'd from within the DLL */
free(ptr);
}
/* ######################################################################### */
EXPORT xbee_err xbee_validate(const struct xbee *xbee) {
if (xbee_ll_get_item(xbeeList, xbee) != XBEE_ENONE) return XBEE_EINVAL;
return XBEE_ENONE;
}
/* ######################################################################### */
xbee_err xbee_alloc(struct xbee **nXbee) {
size_t memSize;
struct xbee *xbee;
xbee_err ret;
if (!nXbee) return XBEE_EMISSINGPARAM;
memSize = sizeof(*xbee);
if (!(xbee = malloc(memSize))) return XBEE_ENOMEM;
memset(xbee, 0, memSize);
if ((ret = xbee_frameBlockAlloc(&xbee->fBlock)) != XBEE_ENONE) goto die1;
#ifndef XBEE_DISABLE_LOGGING
if ((ret = xbee_logAlloc(&xbee->log)) != XBEE_ENONE) goto die1;
#endif /* !XBEE_DISABLE_LOGGING */
if ((ret = xbee_txAlloc(&xbee->iface.tx)) != XBEE_ENONE) goto die1;
if ((ret = xbee_rxAlloc(&xbee->iface.rx)) != XBEE_ENONE) goto die1;
if ((ret = xbee_ll_add_tail(xbeeList, xbee)) != XBEE_ENONE) goto die1;
*nXbee = xbee;
return XBEE_ENONE;
die1:
xbee_free(xbee);
return ret;
}
xbee_err xbee_free(struct xbee *xbee) {
int i;
xbee_ll_ext_item(xbeeList, xbee);
xbee->die = 1;
if (xbee->iface.rx) {
xsys_sem_post(&xbee->iface.rx->sem);
}
if (xbee->iface.tx) {
xsys_sem_post(&xbee->iface.tx->sem);
}
/* sleep for 4 seconds because:
the rx thread should timeout every 2-ish econds
the rxHandler thread will need to run round one more time to clean up
the tx thread will need to run round one more time to clean up */
for (i = 0; i < 4; i++) usleep(1000000);
xbee_threadDestroyMine(xbee);
if (xbee->netInfo) xbee_netStop(xbee);
if (xbee->mode && xbee->mode->shutdown) xbee->mode->shutdown(xbee);
xbee_log(20, "Finalizing shutdown procedure for libxbee instance @ %p", xbee);
xbee_modeCleanup(xbee->iface.conTypes);
xbee_rxFree(xbee->iface.rx);
xbee_txFree(xbee->iface.tx);
#ifndef XBEE_DISABLE_LOGGING
xbee_logFree(xbee->log);
#endif /* !XBEE_DISABLE_LOGGING */
xbee_frameBlockFree(xbee->fBlock);
free(xbee);
return XBEE_ENONE;
}
/* ######################################################################### */
EXPORT xbee_err xbee_vsetup(struct xbee **retXbee, const char *mode, va_list ap) {
xbee_err ret;
const struct xbee_mode *xbeeMode;
struct xbee *xbee;
if (!retXbee || !mode) return XBEE_EMISSINGPARAM;
if ((ret = xbee_modeRetrieve(mode, &xbeeMode)) != XBEE_ENONE) return ret;
if ((ret = xbee_alloc(&xbee)) != XBEE_ENONE) return ret;
if ((ret = xbee_modeImport(&xbee->iface.conTypes, xbeeMode)) != XBEE_ENONE) goto die;
xbee->mode = xbeeMode;
xbee->iface.rx->ioFunc = xbee->mode->rx_io;
xbee->iface.rx->fBlock = xbee->fBlock;
xbee->iface.rx->conTypes = &xbee->iface.conTypes;
xbee->iface.tx->ioFunc = xbee->mode->tx_io;
if ((ret = xbee->mode->init(xbee, ap)) != XBEE_ENONE) goto die;
if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee_rx, xbee->iface.rx)) != XBEE_ENONE) goto die;
if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee_rxHandler, xbee->iface.rx)) != XBEE_ENONE) goto die;
if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee_tx, xbee->iface.tx)) != XBEE_ENONE) goto die;
if (xbee->mode->prepare) if ((ret = xbee->mode->prepare(xbee)) != XBEE_ENONE) goto die;
if (xbee->mode->thread) if ((ret = xbee_threadStart(xbee, NULL, 150000, 0, xbee->mode->thread, NULL)) != XBEE_ENONE) goto die;
xbee_ll_add_tail(xbeeList, xbee);
xbee_log(20, "Created new libxbee instance @ %p", xbee);
*retXbee = xbee;
return XBEE_ENONE;
die:
xbee_free(xbee);
return ret;
}
EXPORT xbee_err xbee_setup(struct xbee **retXbee, const char *mode, ...) {
xbee_err ret;
va_list ap;
va_start(ap, mode);
ret = xbee_vsetup(retXbee, mode, ap);
va_end(ap);
return ret;
}
xbee_err xbee_shutdownThread(struct xbee *xbee, int *restart, void *arg) {
/* detach the thread that called shutdown(), dont care on failure (it may well be the initial thread) */
xsys_thread_detach((xsys_thread)arg);
xbee_free(xbee);
*restart = -1;
return XBEE_ENONE;
}
EXPORT xbee_err xbee_shutdown(struct xbee *xbee) {
if (!xbee) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
if (xbee_validate(xbee) != XBEE_ENONE) return XBEE_EINVAL;
#endif /* XBEE_DISABLE_STRICT_OBJECTS */
/* pluck out the instance - from now on it is invalid */
xbee_ll_ext_item(xbeeList, xbee);
/* start a detached thread */
xbee_threadStart(xbee, NULL, -1, 1, xbee_shutdownThread, (void*)(xsys_thread_self()));
xbee_log(20, "Started shutdown procedure for libxbee instance @ %p", xbee);
return XBEE_ENONE;
}
EXPORT xbee_err xbee_attachEOFCallback(struct xbee *xbee, xbee_t_eofCallback eofCallback) {
if (!xbee || !eofCallback) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
if (xbee_validate(xbee) != XBEE_ENONE) return XBEE_EINVAL;
#endif /* XBEE_DISABLE_STRICT_OBJECTS */
if (xbee->iface.rx->eofCallback) return XBEE_EINUSE;
xbee->iface.rx->eofCallback = eofCallback;
return XBEE_ENONE;
}
EXPORT xbee_err xbee_dataSet(struct xbee *xbee, void *newData, void **oldData) {
if (!xbee) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
if (xbee_validate(xbee) != XBEE_ENONE) return XBEE_EINVAL;
#endif /* XBEE_DISABLE_STRICT_OBJECTS */
if (oldData) *oldData = xbee->userData;
xbee->userData = newData;
return XBEE_ENONE;
}
EXPORT xbee_err xbee_dataGet(struct xbee *xbee, void **curData) {
if (!xbee || !curData) return XBEE_EMISSINGPARAM;
#ifndef XBEE_DISABLE_STRICT_OBJECTS
if (xbee_validate(xbee) != XBEE_ENONE) return XBEE_EINVAL;
#endif /* XBEE_DISABLE_STRICT_OBJECTS */
*curData = xbee->userData;
return XBEE_ENONE;
}