root / branches / wireless / code / projects / libwireless / wireless.c @ 1612
History | View | Annotate | Download (4.73 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 |
/* holds pointers to all normal-priority packet handlers
|
48 |
group handler located at index of group number
|
49 |
group numbers with handlers undefined are NULL
|
50 |
*/
|
51 |
void (*handlers[WL_NUM_PACKET_GROUPS])(void); |
52 |
|
53 |
/**
|
54 |
* initialization function:
|
55 |
* set up xbee communication and packet handler infrastructure
|
56 |
*
|
57 |
* @return 0 on success, or error code
|
58 |
**/
|
59 |
int8_t wl_init(void) {
|
60 |
|
61 |
// check for duplicate initialization
|
62 |
if(init_flag == INIT_YES) {
|
63 |
return WL_ERROR_INIT_ALREADY_INITD;
|
64 |
} |
65 |
|
66 |
// set flag for checking duplicate initializations
|
67 |
init_flag = INIT_YES; |
68 |
|
69 |
// reset packet handler array
|
70 |
memset(wl_packet_handlers, sizeof(PacketGroupHandler)*MAX_PACKET_GROUPS, 0); |
71 |
|
72 |
// initialize xbee
|
73 |
if(xbee_init() == -1) { |
74 |
return WL_ERROR_INIT_FAILED;
|
75 |
} |
76 |
|
77 |
// do we need a timer handler?
|
78 |
//rtc_init(HALF_SECOND, &timer_handler);
|
79 |
|
80 |
return WL_SUCCESS;
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* termination function:
|
85 |
* end xbee communication, deregister all handlers, etc
|
86 |
*
|
87 |
* @return 0 on success, or error code
|
88 |
**/
|
89 |
int8_t wl_terminate(void) {
|
90 |
|
91 |
// check to see if wireless library has been initialized
|
92 |
if(init_flag == INIT_NO) {
|
93 |
return WL_ERROR_LIBRARY_NOT_INITD;
|
94 |
} |
95 |
|
96 |
// reset packet handler array
|
97 |
memset(wl_packet_handlers, sizeof(PacketGroupHandler)*MAX_PACKET_GROUPS, 0); |
98 |
|
99 |
if(xbee_terminate() != WL_SUCCESS) {
|
100 |
return WL_ERROR_TERMINATION_FAILED;
|
101 |
} |
102 |
|
103 |
return WL_SUCCESS;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* function to register new packet handlers (for non-default groups only)
|
108 |
*
|
109 |
* @param group the packet group number of the packets to handle with this function
|
110 |
* @param func the function pointer to the user-specified packet handler
|
111 |
* @param priority flag to set the priority of the function handler
|
112 |
*
|
113 |
* @return 0 on success, or error code
|
114 |
**/
|
115 |
int8_t wl_register_handler(uint8_t group, FNPTR, uint8_t priority) { |
116 |
|
117 |
// check to see if wireless library has been initialized
|
118 |
if(init_flag == INIT_NO) {
|
119 |
return WL_ERROR_LIBRARY_NOT_INITD;
|
120 |
} |
121 |
|
122 |
// packet group number exceeds available packets groups
|
123 |
// or packet group handler already exists
|
124 |
if(group >= MAX_PACKET_GROUPS || group == 0 |
125 |
|| wl_packet_handlers[group].FUNC != NULL) {
|
126 |
return WL_ERROR_FAILED_REGISTRATION;
|
127 |
} |
128 |
|
129 |
// save packet handler function pointer and priority
|
130 |
wl_packet_handlers[group].FUNC = FUNC; |
131 |
wl_packet_handlers[group].priority = priority; |
132 |
return WL_SUCCESS;
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* function to unregister existing packet handlers (for non-default groups only)
|
137 |
*
|
138 |
* @param group the packet group number of the handler to unregister
|
139 |
*
|
140 |
* @return 0 on success, or error code
|
141 |
**/
|
142 |
int8_t wl_unregister_handler(uint8_t group) { |
143 |
|
144 |
// check to see if wireless library has been initialized
|
145 |
if(init_flag == INIT_NO) {
|
146 |
return WL_ERROR_LIBRARY_NOT_INITD;
|
147 |
} |
148 |
|
149 |
// packet group number exceeds available packets groups
|
150 |
if(group >= MAX_PACKET_GROUPS) {
|
151 |
return WL_ERROR_FAILED_UNREGISTRATION;
|
152 |
} |
153 |
|
154 |
// packet group handler does not exists
|
155 |
if(wl_packet_handlers[group].FUNC == NULL) { |
156 |
return WL_ERROR_FAILED_UNREGISTRATION;
|
157 |
} |
158 |
|
159 |
// clear packet group information
|
160 |
wl_packet_handlers[group].FUNC = NULL;
|
161 |
wl_packet_handlers[group].priority = NORMAL_PRIORITY; |
162 |
return WL_SUCCESS;
|
163 |
} |
164 |
|
165 |
|
166 |
/**@} **/ //end defgroup |
167 |
|
168 |
|
169 |
// Define all private functions down here
|
170 |
|
171 |
|
172 |
|