root / branches / wireless / code / projects / libwireless / wireless.c @ 1614
History | View | Annotate | Download (4.63 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2009 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
/**
|
27 |
* @file wireless.c
|
28 |
* @brief Wireless library
|
29 |
*
|
30 |
* Implementation of high level wireless communication.
|
31 |
*
|
32 |
* @author Colony Project, CMU Robotics Club
|
33 |
**/
|
34 |
|
35 |
#include <string.h> |
36 |
#include "wl_defs.h" |
37 |
#include "wireless.h" |
38 |
#include "xbee.h" |
39 |
|
40 |
// this is the common wireless file
|
41 |
|
42 |
/**
|
43 |
* @addtogroup wireless Wireless
|
44 |
* @{
|
45 |
**/
|
46 |
|
47 |
static uint8_t init_flag = INIT_NO;
|
48 |
static PacketGroupHandler wl_packet_handlers[MAX_PACKET_GROUPS];
|
49 |
|
50 |
/**
|
51 |
* initialization function:
|
52 |
* set up xbee communication and packet handler infrastructure
|
53 |
*
|
54 |
* @return 0 on success, or error code
|
55 |
**/
|
56 |
int8_t wl_init(void) {
|
57 |
|
58 |
// check for duplicate initialization
|
59 |
if(init_flag == INIT_YES) {
|
60 |
return WL_ERROR_INIT_ALREADY_INITD;
|
61 |
} |
62 |
|
63 |
// set flag for checking duplicate initializations
|
64 |
init_flag = INIT_YES; |
65 |
|
66 |
// reset packet handler array
|
67 |
memset(wl_packet_handlers, sizeof(PacketGroupHandler)*MAX_PACKET_GROUPS, 0); |
68 |
|
69 |
// initialize xbee
|
70 |
if(xbee_init() == -1) { |
71 |
return WL_ERROR_INIT_FAILED;
|
72 |
} |
73 |
|
74 |
// do we need a timer handler?
|
75 |
//rtc_init(HALF_SECOND, &timer_handler);
|
76 |
|
77 |
return WL_SUCCESS;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* termination function:
|
82 |
* end xbee communication, deregister all handlers, etc
|
83 |
*
|
84 |
* @return 0 on success, or error code
|
85 |
**/
|
86 |
int8_t wl_terminate(void) {
|
87 |
|
88 |
// check to see if wireless library has been initialized
|
89 |
if(init_flag == INIT_NO) {
|
90 |
return WL_ERROR_LIBRARY_NOT_INITD;
|
91 |
} |
92 |
|
93 |
// reset packet handler array
|
94 |
memset(wl_packet_handlers, sizeof(PacketGroupHandler)*MAX_PACKET_GROUPS, 0); |
95 |
|
96 |
if(xbee_terminate() != WL_SUCCESS) {
|
97 |
return WL_ERROR_TERMINATION_FAILED;
|
98 |
} |
99 |
|
100 |
return WL_SUCCESS;
|
101 |
} |
102 |
|
103 |
/**
|
104 |
* function to register new packet handlers (for non-default groups only)
|
105 |
*
|
106 |
* @param group the packet group number of the packets to handle with this function
|
107 |
* @param func the function pointer to the user-specified packet handler
|
108 |
* @param priority flag to set the priority of the function handler
|
109 |
*
|
110 |
* @return 0 on success, or error code
|
111 |
**/
|
112 |
int8_t wl_register_handler(uint8_t group, FNPTR, uint8_t priority) { |
113 |
|
114 |
// check to see if wireless library has been initialized
|
115 |
if(init_flag == INIT_NO) {
|
116 |
return WL_ERROR_LIBRARY_NOT_INITD;
|
117 |
} |
118 |
|
119 |
// packet group number exceeds available packets groups
|
120 |
// or packet group handler already exists
|
121 |
if(group >= MAX_PACKET_GROUPS || group == 0 |
122 |
|| wl_packet_handlers[group].FUNC != NULL) {
|
123 |
return WL_ERROR_FAILED_REGISTRATION;
|
124 |
} |
125 |
|
126 |
// save packet handler function pointer and priority
|
127 |
wl_packet_handlers[group].FUNC = FUNC; |
128 |
wl_packet_handlers[group].priority = priority; |
129 |
return WL_SUCCESS;
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* function to unregister existing packet handlers (for non-default groups only)
|
134 |
*
|
135 |
* @param group the packet group number of the handler to unregister
|
136 |
*
|
137 |
* @return 0 on success, or error code
|
138 |
**/
|
139 |
int8_t wl_unregister_handler(uint8_t group) { |
140 |
|
141 |
// check to see if wireless library has been initialized
|
142 |
if(init_flag == INIT_NO) {
|
143 |
return WL_ERROR_LIBRARY_NOT_INITD;
|
144 |
} |
145 |
|
146 |
// packet group number exceeds available packets groups
|
147 |
if(group >= MAX_PACKET_GROUPS) {
|
148 |
return WL_ERROR_FAILED_UNREGISTRATION;
|
149 |
} |
150 |
|
151 |
// packet group handler does not exists
|
152 |
if(wl_packet_handlers[group].FUNC == NULL) { |
153 |
return WL_ERROR_FAILED_UNREGISTRATION;
|
154 |
} |
155 |
|
156 |
// clear packet group information
|
157 |
wl_packet_handlers[group].FUNC = NULL;
|
158 |
wl_packet_handlers[group].priority = NORMAL_PRIORITY; |
159 |
return WL_SUCCESS;
|
160 |
} |
161 |
|
162 |
|
163 |
/**@} **/ //end defgroup |
164 |
|
165 |
|
166 |
// Define all private functions down here
|
167 |
|
168 |
|
169 |
|