Project

General

Profile

Revision 343

Added by Jason knichel over 16 years ago

changed the brace style and reformatted the files

View differences:

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

  
61 61
/*Data Members*/
62 62

  
......
70 70
#ifndef ROBOT
71 71

  
72 72
//called when we time out, or receive interrupt
73
void sig_handler(int signo)
74
{
75
	switch (signo)
76
	{
77
		case SIGALRM:
78
			wl_timeout = 1;
79
			break;
80
		case SIGINT:
81
			wl_terminate();
82
			exit(1);
83
			break;
84
	}
85
	return;
73
void sig_handler(int signo) {
74
  switch (signo)	{
75
  case SIGALRM:
76
    wl_timeout = 1;
77
    break;
78
  case SIGINT:
79
    wl_terminate();
80
    exit(1);
81
    break;
82
  }
83
  return;
86 84
}
87 85
#else
88 86

  
89 87
//called when the timer ticks
90
void timer_handler(void)
91
{
92
	wl_timeout = 1;
88
void timer_handler(void) {
89
  wl_timeout = 1;
93 90
}
94 91

  
95 92
#endif
......
98 95
 * Initializes the wireless library. Must be called before any
99 96
 * other function.
100 97
 **/
101
int wl_init()
102
{
103
	int i;
104
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
105
		wl_packet_groups[i] = NULL;
98
int wl_init() {
99
  int i;
100
  for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
101
    wl_packet_groups[i] = NULL;
106 102

  
107
	if (xbee_lib_init() == -1) {
108
	  return -1;
109
	}
103
  if (xbee_lib_init() == -1) {
104
    return -1;
105
  }
110 106

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

  
120
	//create our timer
121
	struct itimerval timer_val;
122
	struct timeval interval;
123
	interval.tv_sec = 0;
124
	interval.tv_usec = 500000;
125
	struct timeval first_time;
126
	first_time.tv_sec = 0;
127
	first_time.tv_usec = 500000;
128
	timer_val.it_interval = interval;
129
	timer_val.it_value = first_time;
130
	if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
131
	{
132
		WL_DEBUG_PRINT("Error creating a timer.\r\n");
133
		perror("Failure's cause");
134
		exit(1);
135
	}
116
  //create our timer
117
  struct itimerval timer_val;
118
  struct timeval interval;
119
  interval.tv_sec = 0;
120
  interval.tv_usec = 500000;
121
  struct timeval first_time;
122
  first_time.tv_sec = 0;
123
  first_time.tv_usec = 500000;
124
  timer_val.it_interval = interval;
125
  timer_val.it_value = first_time;
126
  if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1) {
127
    WL_DEBUG_PRINT("Error creating a timer.\r\n");
128
    perror("Failure's cause");
129
    exit(1);
130
  }
136 131

  
137
	//create signal handler
138
	struct sigaction wl_sig_act;
139
	wl_sig_act.sa_handler = sig_handler;
140
	wl_sig_act.sa_flags = 0;
141
	sigemptyset(&wl_sig_act.sa_mask);
142
	sigaction(SIGALRM, &wl_sig_act, 0);
143
	sigaction(SIGINT, &wl_sig_act, 0);
144
	#endif
132
  //create signal handler
133
  struct sigaction wl_sig_act;
134
  wl_sig_act.sa_handler = sig_handler;
135
  wl_sig_act.sa_flags = 0;
136
  sigemptyset(&wl_sig_act.sa_mask);
137
  sigaction(SIGALRM, &wl_sig_act, 0);
138
  sigaction(SIGINT, &wl_sig_act, 0);
139
#endif
145 140

  
146
	return 0;
141
  return 0;
147 142
}
148 143

  
149 144
/**
150 145
 * Uninitializes the wireless library.
151 146
 **/
152
void wl_terminate()
153
{
154
	int i;
155
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
156
		if (wl_packet_groups[i] != NULL &&
157
			wl_packet_groups[i]->unregister != NULL)
158
			wl_packet_groups[i]->unregister();
147
void wl_terminate() {
148
  int i;
149
  for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
150
    if (wl_packet_groups[i] != NULL &&
151
	wl_packet_groups[i]->unregister != NULL)
152
      wl_packet_groups[i]->unregister();
159 153

  
160
	xbee_terminate();
154
  xbee_terminate();
161 155
}
162 156

  
163 157
/**
......
167 161
 *
168 162
 * @see wl_get_pan
169 163
 **/
170
void wl_set_pan(int pan)
171
{
172
	xbee_set_pan_id(pan);
164
void wl_set_pan(int pan) {
165
  xbee_set_pan_id(pan);
173 166
}
174 167

  
175 168
/**
......
179 172
 *
180 173
 * @see wl_set_pan
181 174
 **/
182
int wl_get_pan(void)
183
{
184
	return xbee_get_pan_id();
175
int wl_get_pan(void) {
176
  return xbee_get_pan_id();
185 177
}
186 178

  
187 179
/**
......
191 183
 *
192 184
 * @see wl_get_channel
193 185
 **/
194
void wl_set_channel(int channel)
195
{
196
	xbee_set_channel(channel);
186
void wl_set_channel(int channel) {
187
  xbee_set_channel(channel);
197 188
}
198 189

  
199 190
/**
......
203 194
 *
204 195
 * @see wl_set_channel
205 196
 **/
206
int wl_get_channel(void)
207
{
208
	return xbee_get_channel();
197
int wl_get_channel(void) {
198
  return xbee_get_channel();
209 199
}
210 200

  
211 201
/**
......
213 203
 *
214 204
 * @return the 16-bit address of the XBee module.
215 205
 **/
216
int wl_get_xbee_id()
217
{
218
	return xbee_get_address();
206
int wl_get_xbee_id() {
207
  return xbee_get_address();
219 208
}
220 209

  
221 210
/**
......
229 218
 * @param frame the frame number to see with a TX_STATUS response
230 219
 **/
231 220
void wl_send_robot_to_robot_global_packet(char group, char type,
232
		char* data, int len, int dest, char frame)
233
{
234
	wl_send_packet(group, type, data, len, dest,
235
			XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
221
					  char* data, int len, int dest, char frame) {
222
  wl_send_packet(group, type, data, len, dest,
223
		 XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
236 224
}
237 225

  
238 226
/**
......
246 234
 * @param frame the frame number to see with a TX_STATUS response
247 235
 **/
248 236
void wl_send_robot_to_robot_packet(char group, char type,
249
		char* data, int len, int dest, char frame)
250
{
251
	wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
252
			frame);
237
				   char* data, int len, int dest, char frame) {
238
  wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
239
		 frame);
253 240
}
254 241

  
255 242
/**
......
262 249
 * @param frame the frame number to see with a TX_STATUS response
263 250
 **/
264 251
void wl_send_global_packet(char group, char type,
265
		char* data, int len, char frame)
266
{
267
	wl_send_packet(group, type, data, len, XBEE_BROADCAST,
268
			XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
252
			   char* data, int len, char frame) {
253
  wl_send_packet(group, type, data, len, XBEE_BROADCAST,
254
		 XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
269 255
}
270 256

  
271 257
/**
......
278 264
 * @param frame the frame number to see with a TX_STATUS response
279 265
 **/
280 266
void wl_send_pan_packet(char group, char type,
281
		char* data, int len, char frame)
282
{
283
	wl_send_packet(group, type, data, len, XBEE_BROADCAST,
284
			XBEE_OPTIONS_NONE, frame);
267
			char* data, int len, char frame) {
268
  wl_send_packet(group, type, data, len, XBEE_BROADCAST,
269
		 XBEE_OPTIONS_NONE, frame);
285 270
}
286 271

  
287 272
/**
......
296 281
 * @param frame the frame number to see with a TX_STATUS response
297 282
 **/
298 283
void wl_send_packet(char group, char type, char* data, int len,
299
					int dest, char options, char frame)
300
{
301
	char buf[128];
302
	int i;
303
	if (frame != 0)
304
		frame = (frame & 0x0F) | ((group & 0x0F) << 4);
305
	buf[0] = group;
306
	buf[1] = type;
307
	for (i = 0; i < len; i++)
308
		buf[2 + i] = data[i];
309
	xbee_send_packet(buf, len + 2, dest, options, frame);
284
		    int dest, char options, char frame) {
285
  char buf[128];
286
  int i;
287
  if (frame != 0)
288
    frame = (frame & 0x0F) | ((group & 0x0F) << 4);
289
  buf[0] = group;
290
  buf[1] = type;
291
  for (i = 0; i < len; i++)
292
    buf[2 + i] = data[i];
293
  xbee_send_packet(buf, len + 2, dest, options, frame);
310 294
}
311 295

  
312 296
/**
......
316 300
 *
317 301
 * @param h the PacketGroupHandler to register
318 302
 **/
319
void wl_register_packet_group(PacketGroupHandler* h)
320
{
321
	if (h->groupCode >= WL_MAX_PACKET_GROUPS)
322
	{
323
		WL_DEBUG_PRINT("Packet group code too large.\r\n");
324
		return;
325
	}
326
	if (wl_packet_groups[h->groupCode] != NULL)
327
	{
328
		WL_DEBUG_PRINT("Packet group code already registered.\r\n");
329
		return;
330
	}
331
	wl_packet_groups[h->groupCode] = h;
303
void wl_register_packet_group(PacketGroupHandler* h) {
304
  if (h->groupCode >= WL_MAX_PACKET_GROUPS) {
305
    WL_DEBUG_PRINT("Packet group code too large.\r\n");
306
    return;
307
  }
308
  if (wl_packet_groups[h->groupCode] != NULL) {
309
    WL_DEBUG_PRINT("Packet group code already registered.\r\n");
310
    return;
311
  }
312
  wl_packet_groups[h->groupCode] = h;
332 313
}
333 314

  
334 315
/**
......
336 317
 *
337 318
 * @param h the packet group to remove
338 319
 **/
339
void wl_unregister_packet_group(PacketGroupHandler* h)
340
{
341
	unsigned int groupCode = h->groupCode;
342
	PacketGroupHandler* p = wl_packet_groups[groupCode];
343
	if (p != NULL && p->unregister != NULL)
344
		p->unregister();
345
	wl_packet_groups[groupCode] = NULL;
320
void wl_unregister_packet_group(PacketGroupHandler* h) {
321
  unsigned int groupCode = h->groupCode;
322
  PacketGroupHandler* p = wl_packet_groups[groupCode];
323
  if (p != NULL && p->unregister != NULL)
324
    p->unregister();
325
  wl_packet_groups[groupCode] = NULL;
346 326
}
347 327

  
348 328
/**
349 329
 * Called when the timer is triggered. This calls the timeout
350 330
 * handlers of all the registered packet groups.
351 331
 **/
352
void wl_do_timeout()
353
{
354
	int i;
355
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
356
		if (wl_packet_groups[i] != NULL &&
357
			wl_packet_groups[i]->timeout_handler != NULL)
358
			wl_packet_groups[i]->timeout_handler();
332
void wl_do_timeout() {
333
  int i;
334
  for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
335
    if (wl_packet_groups[i] != NULL &&
336
	wl_packet_groups[i]->timeout_handler != NULL)
337
      wl_packet_groups[i]->timeout_handler();
359 338
}
360 339

  
361 340
/**
......
364 343
 * This function will call timeout handlers, as well as
365 344
 * received packet and transmit status handlers.
366 345
 **/
367
void wl_do()
368
{
369
	if (wl_timeout)
370
	{
371
		wl_do_timeout();
372
		wl_timeout = 0;
373
	}
346
void wl_do() {
347
  if (wl_timeout) {
348
    wl_do_timeout();
349
    wl_timeout = 0;
350
  }
374 351

  
375
	int len = xbee_get_packet(wl_buf);
376
	if (len < 0)//no packet received
377
		return;
352
  int len = xbee_get_packet(wl_buf);
353
  if (len < 0)//no packet received
354
    return;
378 355

  
379
	if (wl_buf[0] == XBEE_TX_STATUS)
380
	{
381
		if (len != 3)
382
		{
383
			WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
384
			return;
385
		}
356
  if (wl_buf[0] == XBEE_TX_STATUS) {
357
    if (len != 3) {
358
      WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
359
      return;
360
    }
386 361

  
387
		//the first four bits are the packet group
388
		//this only works with under 16 groups
389
		int group = (int)(wl_buf[1] >> 4);
390
		int success = 0;
391
		if (wl_buf[2] == 0)
392
			success = 1;
393
		else
394
		{
395
			WL_DEBUG_PRINT("No response received.\r\n");
396
			if (wl_buf[2] == 2)
397
			{
398
				WL_DEBUG_PRINT("CCA Failure\r\n");
399
			}
400
			if (wl_buf[2] == 3)
401
			{
402
				WL_DEBUG_PRINT("Purged\r\n");
403
			}
404
		}
362
    //the first four bits are the packet group
363
    //this only works with under 16 groups
364
    int group = (int)(wl_buf[1] >> 4);
365
    int success = 0;
366
    if (wl_buf[2] == 0)
367
      success = 1;
368
    else {
369
      WL_DEBUG_PRINT("No response received.\r\n");
370
      if (wl_buf[2] == 2) {
371
	WL_DEBUG_PRINT("CCA Failure\r\n");
372
      }
373
      if (wl_buf[2] == 3) {
374
	WL_DEBUG_PRINT("Purged\r\n");
375
      }
376
    }
405 377

  
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;
411
	}
378
    if (wl_packet_groups[group] != NULL &&
379
	wl_packet_groups[group]->handle_response != NULL)
380
      wl_packet_groups[group]->handle_response(
381
					       (int)wl_buf[1] & 0x0F, success);
382
    return;
383
  }
412 384

  
413
	if (wl_buf[0] == XBEE_RX)
414
	{
415
		if (len < 7)
416
		{
417
			WL_DEBUG_PRINT("Packet is too small.\r\n");
418
			return;
419
		}
385
  if (wl_buf[0] == XBEE_RX) {
386
    if (len < 7) {
387
      WL_DEBUG_PRINT("Packet is too small.\r\n");
388
      return;
389
    }
420 390

  
421
		int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
391
    int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
422 392

  
423
		/*
424
		//unused for now
425
		int signalStrength = wl_buf[3];
426
		//1 for Address broadcast, 2 for PAN broadcast
427
		int options = wl_buf[4];
428
		*/
393
    /*
394
    //unused for now
395
    int signalStrength = wl_buf[3];
396
    //1 for Address broadcast, 2 for PAN broadcast
397
    int options = wl_buf[4];
398
    */
429 399

  
430
		int group = wl_buf[5];
431
		int type = wl_buf[6];
432
		int packetLen = len - 7;
400
    int group = wl_buf[5];
401
    int type = wl_buf[6];
402
    int packetLen = len - 7;
433 403

  
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;
439
	}
404
    if (wl_packet_groups[group] != NULL
405
	&& wl_packet_groups[group]->handle_receive != NULL)
406
      wl_packet_groups[group]->handle_receive(type, source,
407
					      wl_buf + 7, packetLen);
408
    return;
409
  }
440 410

  
441
	WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
442
	return;
411
  WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
412
  return;
443 413
}
444 414

  
445 415

  
446 416
#ifndef ROBOT
447
void wl_set_com_port(char* port)
448
{
449
	xbee_set_com_port(port);
417
void wl_set_com_port(char* port) {
418
  xbee_set_com_port(port);
450 419
}
451 420
#endif
452 421

  

Also available in: Unified diff