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