root / branches / wireless / code / projects / libwireless / wireless.c @ 1737
History | View | Annotate | Download (4.59 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 | 1577 | dsschult | /**
|
43 | * @addtogroup wireless Wireless
|
||
44 | * @{
|
||
45 | **/
|
||
46 | |||
47 | 1614 | cmar | static uint8_t init_flag = INIT_NO;
|
48 | 1598 | cmar | |
49 | 1577 | dsschult | /**
|
50 | * initialization function:
|
||
51 | * set up xbee communication and packet handler infrastructure
|
||
52 | *
|
||
53 | 1581 | dsschult | * @return 0 on success, or error code
|
54 | 1577 | dsschult | **/
|
55 | 1576 | dsschult | int8_t wl_init(void) {
|
56 | |||
57 | 1598 | cmar | // check for duplicate initialization
|
58 | if(init_flag == INIT_YES) {
|
||
59 | return WL_ERROR_INIT_ALREADY_INITD;
|
||
60 | } |
||
61 | |||
62 | // set flag for checking duplicate initializations
|
||
63 | init_flag = INIT_YES; |
||
64 | |||
65 | // reset packet handler array
|
||
66 | memset(wl_packet_handlers, sizeof(PacketGroupHandler)*MAX_PACKET_GROUPS, 0); |
||
67 | |||
68 | // initialize xbee
|
||
69 | 1608 | dsschult | if(xbee_init() == -1) { |
70 | 1598 | cmar | return WL_ERROR_INIT_FAILED;
|
71 | } |
||
72 | |||
73 | // do we need a timer handler?
|
||
74 | //rtc_init(HALF_SECOND, &timer_handler);
|
||
75 | |||
76 | return WL_SUCCESS;
|
||
77 | 1576 | dsschult | } |
78 | |||
79 | 1577 | dsschult | /**
|
80 | * termination function:
|
||
81 | * end xbee communication, deregister all handlers, etc
|
||
82 | *
|
||
83 | 1581 | dsschult | * @return 0 on success, or error code
|
84 | 1577 | dsschult | **/
|
85 | int8_t wl_terminate(void) {
|
||
86 | |||
87 | 1598 | cmar | // check to see if wireless library has been initialized
|
88 | if(init_flag == INIT_NO) {
|
||
89 | return WL_ERROR_LIBRARY_NOT_INITD;
|
||
90 | } |
||
91 | |||
92 | // reset packet handler array
|
||
93 | memset(wl_packet_handlers, sizeof(PacketGroupHandler)*MAX_PACKET_GROUPS, 0); |
||
94 | |||
95 | 1604 | dsschult | if(xbee_terminate() != WL_SUCCESS) {
|
96 | 1608 | dsschult | return WL_ERROR_TERMINATION_FAILED;
|
97 | 1604 | dsschult | } |
98 | 1598 | cmar | |
99 | return WL_SUCCESS;
|
||
100 | 1577 | dsschult | } |
101 | |||
102 | /**
|
||
103 | * function to register new packet handlers (for non-default groups only)
|
||
104 | *
|
||
105 | * @param group the packet group number of the packets to handle with this function
|
||
106 | * @param func the function pointer to the user-specified packet handler
|
||
107 | 1581 | dsschult | * @param priority flag to set the priority of the function handler
|
108 | 1577 | dsschult | *
|
109 | 1581 | dsschult | * @return 0 on success, or error code
|
110 | 1577 | dsschult | **/
|
111 | 1590 | dsschult | int8_t wl_register_handler(uint8_t group, FNPTR, uint8_t priority) { |
112 | 1576 | dsschult | |
113 | 1598 | cmar | // check to see if wireless library has been initialized
|
114 | if(init_flag == INIT_NO) {
|
||
115 | return WL_ERROR_LIBRARY_NOT_INITD;
|
||
116 | } |
||
117 | |||
118 | 1604 | dsschult | // packet group number exceeds available packets groups
|
119 | // or packet group handler already exists
|
||
120 | if(group >= MAX_PACKET_GROUPS || group == 0 |
||
121 | 1663 | bwasserm | || wl_packet_handlers[group].FUNC != NULL
|
122 | || FUNC == NULL) {
|
||
123 | 1598 | cmar | 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 | 1576 | dsschult | } |
131 | |||
132 | 1598 | cmar | /**
|
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 | 1576 | dsschult | |
141 | 1598 | cmar | // 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 | 1577 | dsschult | /**@} **/ //end defgroup |
164 | |||
165 | |||
166 | // Define all private functions down here
|
||
167 | |||
168 |