Project

General

Profile

Revision 668

Updated port of wireless library to bay boards.

View differences:

wireless.c
51 51

  
52 52
/*Function Prototypes*/
53 53

  
54
void wl_do_timeout(void);
54
static void wl_do_timeout(void);
55 55

  
56 56
//Note: the actual frame sent has group as the first four bits and
57 57
//frame as the last four.
58
void wl_send_packet(char group, char type, char* data, int len,
59
					int dest, char options, char frame);
58
static int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame);
60 59

  
61 60
/*Data Members*/
62 61

  
63 62
//used to store incoming and outgoing packets
64
unsigned char wl_buf[128];
63
static unsigned char wl_buf[128];
65 64
//1 if we have timed out since we last checked, 0 otherwise.
66
int wl_timeout = 0;
65
static int wl_timeout = 0;
67 66

  
68
PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
67
static PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
69 68

  
70 69
#ifndef ROBOT
71 70

  
72 71
//called when we time out, or receive interrupt
73
void sig_handler(int signo)
72
static void sig_handler(int signo)
74 73
{
75 74
	switch (signo)
76 75
	{
......
87 86
#else
88 87

  
89 88
//called when the timer ticks
90
void timer_handler(void)
89
static void timer_handler(void)
91 90
{
92 91
	wl_timeout = 1;
93 92
}
......
97 96
/**
98 97
 * Initializes the wireless library. Must be called before any
99 98
 * other function.
99
 *
100
 * @param wl_port File descriptor for wireless port, or NULL for default.
100 101
 **/
101 102
int wl_init()
102 103
{
......
105 106
		wl_packet_groups[i] = NULL;
106 107

  
107 108
	if (xbee_lib_init() == -1) {
108
	  return -1;
109
		return -1;
109 110
	}
110 111

  
111 112
	//begin timeout timer
112
	#ifdef ROBOT
113
	#ifdef FIREFLY
113
#ifdef ROBOT
114
#ifdef FIREFLY
114 115
	rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
115
	#else
116
#else
116 117
	rtc_init(HALF_SECOND, &timer_handler);
117
	#endif
118
	#else
118
#endif
119
#else
119 120

  
120 121
	//create our timer
121 122
	struct itimerval timer_val;
......
141 142
	sigemptyset(&wl_sig_act.sa_mask);
142 143
	sigaction(SIGALRM, &wl_sig_act, 0);
143 144
	sigaction(SIGINT, &wl_sig_act, 0);
144
	#endif
145
#endif
145 146

  
146 147
	return 0;
147 148
}
......
152 153
void wl_terminate()
153 154
{
154 155
	int i;
155
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
156
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) {
156 157
		if (wl_packet_groups[i] != NULL &&
157
			wl_packet_groups[i]->unregister != NULL)
158
			wl_packet_groups[i]->unregister != NULL) {
158 159
			wl_packet_groups[i]->unregister();
160
		}
161
	}
159 162

  
160 163
	xbee_terminate();
161 164
}
......
167 170
 *
168 171
 * @see wl_get_pan
169 172
 **/
170
void wl_set_pan(int pan)
173
int wl_set_pan(int pan)
171 174
{
172
	xbee_set_pan_id(pan);
175
	return xbee_set_pan_id(pan);
173 176
}
174 177

  
175 178
/**
......
191 194
 *
192 195
 * @see wl_get_channel
193 196
 **/
194
void wl_set_channel(int channel)
197
int wl_set_channel(int channel)
195 198
{
196
	xbee_set_channel(channel);
199
	return xbee_set_channel(channel);
197 200
}
198 201

  
199 202
/**
......
228 231
 * @param dest the 16-bit address of the XBee to send the packet to
229 232
 * @param frame the frame number to see with a TX_STATUS response
230 233
 **/
231
void wl_send_robot_to_robot_global_packet(char group, char type,
232
		char* data, int len, int dest, char frame)
234
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame)
233 235
{
234
	wl_send_packet(group, type, data, len, dest,
235
			XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
236
	return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
236 237
}
237 238

  
238 239
/**
......
245 246
 * @param dest the 16-bit address of the XBee to send the packet to
246 247
 * @param frame the frame number to see with a TX_STATUS response
247 248
 **/
248
void wl_send_robot_to_robot_packet(char group, char type,
249
		char* data, int len, int dest, char frame)
249
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame)
250 250
{
251
	wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
252
			frame);
251
	return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE, frame);
253 252
}
254 253

  
255 254
/**
......
261 260
 * @param len the packet length in bytes
262 261
 * @param frame the frame number to see with a TX_STATUS response
263 262
 **/
264
void wl_send_global_packet(char group, char type,
265
		char* data, int len, char frame)
263
int wl_send_global_packet(char group, char type, char* data, int len, char frame)
266 264
{
267
	wl_send_packet(group, type, data, len, XBEE_BROADCAST,
268
			XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
265
	return wl_send_packet(group, type, data, len, XBEE_BROADCAST, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
269 266
}
270 267

  
271 268
/**
......
277 274
 * @param len the packet length in bytes
278 275
 * @param frame the frame number to see with a TX_STATUS response
279 276
 **/
280
void wl_send_pan_packet(char group, char type,
281
		char* data, int len, char frame)
277
void wl_send_pan_packet(char group, char type, char* data, int len, char frame)
282 278
{
283 279
	wl_send_packet(group, type, data, len, XBEE_BROADCAST,
284 280
			XBEE_OPTIONS_NONE, frame);
......
295 291
 * @param options the options for sending the packet
296 292
 * @param frame the frame number to see with a TX_STATUS response
297 293
 **/
298
void wl_send_packet(char group, char type, char* data, int len,
299
					int dest, char options, char frame)
294
int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame)
300 295
{
301 296
	char buf[128];
302 297
	int i;
303 298
	if (frame != 0)
304 299
		frame = (frame & 0x0F) | ((group & 0x0F) << 4);
300

  
305 301
	buf[0] = group;
306 302
	buf[1] = type;
307 303
	for (i = 0; i < len; i++)
308 304
		buf[2 + i] = data[i];
309
	xbee_send_packet(buf, len + 2, dest, options, frame);
305

  
306
	return xbee_send_packet(buf, len + 2, dest, options, frame);
310 307
}
311 308

  
312 309
/**
......
373 370
	}
374 371

  
375 372
	int len = xbee_get_packet(wl_buf);
376
	if (len < 0)//no packet received
373
	if (len == -1)
377 374
		return;
378 375

  
379 376
	if (wl_buf[0] == XBEE_TX_STATUS)
......
389 386
		int group = (int)(wl_buf[1] >> 4);
390 387
		int success = 0;
391 388
		if (wl_buf[2] == 0)
389
		{
392 390
			success = 1;
391
		}
393 392
		else
394 393
		{
395 394
			WL_DEBUG_PRINT("No response received.\r\n");
......
403 402
			}
404 403
		}
405 404

  
406
		if (wl_packet_groups[group] != NULL &&
407
					wl_packet_groups[group]->handle_response != NULL)
408
			wl_packet_groups[group]->handle_response(
409
					(int)wl_buf[1] & 0x0F, success);
410
		return;
405
		if (wl_packet_groups[group] != NULL && wl_packet_groups[group]->handle_response != NULL)
406
			wl_packet_groups[group]->handle_response((int)wl_buf[1] & 0x0F, success);
411 407
	}
412

  
413
	if (wl_buf[0] == XBEE_RX)
408
	else if (wl_buf[0] == XBEE_RX)
414 409
	{
415 410
		if (len < 7)
416 411
		{
......
431 426
		int type = wl_buf[6];
432 427
		int packetLen = len - 7;
433 428

  
434
		if (wl_packet_groups[group] != NULL
435
				&& wl_packet_groups[group]->handle_receive != NULL)
436
			wl_packet_groups[group]->handle_receive(type, source,
437
				wl_buf + 7, packetLen);
438
		return;
429
		if (wl_packet_groups[group] != NULL && wl_packet_groups[group]->handle_receive != NULL) {
430
			wl_packet_groups[group]->handle_receive(type, source, wl_buf + 7, packetLen);
431
		}
439 432
	}
440

  
441
	WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
442
	return;
433
	else
434
	{
435
		WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
436
	}
443 437
}
444 438

  
445 439

  

Also available in: Unified diff