Project

General

Profile

Statistics
| Revision:

root / branches / wireless / code / projects / libwireless / wireless.c @ 1615

History | View | Annotate | Download (4.56 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

    
49
/**
50
 * initialization function:
51
 * set up xbee communication and packet handler infrastructure
52
 *
53
 * @return 0 on success, or error code
54
 **/
55
int8_t wl_init(void) {
56

    
57
    // 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
    if(xbee_init() == -1) {
70
        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
}
78

    
79
/**
80
 * termination function:
81
 * end xbee communication, deregister all handlers, etc
82
 *
83
 * @return 0 on success, or error code
84
 **/
85
int8_t wl_terminate(void) {
86

    
87
    // 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
    if(xbee_terminate() != WL_SUCCESS) {
96
        return WL_ERROR_TERMINATION_FAILED;
97
    }
98

    
99
    return WL_SUCCESS;
100
}
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
 * @param priority flag to set the priority of the function handler
108
 *
109
 * @return 0 on success, or error code
110
 **/
111
int8_t wl_register_handler(uint8_t group, FNPTR, uint8_t priority) {
112

    
113
    // 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
    // packet group number exceeds available packets groups   
119
    // or packet group handler already exists
120
    if(group >= MAX_PACKET_GROUPS || group == 0
121
        || wl_packet_handlers[group].FUNC != NULL) {
122
        return WL_ERROR_FAILED_REGISTRATION;
123
    }
124
    
125
    // save packet handler function pointer and priority
126
    wl_packet_handlers[group].FUNC = FUNC;
127
    wl_packet_handlers[group].priority = priority;
128
    return WL_SUCCESS;
129
}
130

    
131
/**
132
 * function to unregister existing packet handlers (for non-default groups only)
133
 *
134
 * @param group the packet group number of the handler to unregister
135
 *
136
 * @return 0 on success, or error code
137
 **/
138
int8_t wl_unregister_handler(uint8_t group) {
139

    
140
    // check to see if wireless library has been initialized
141
    if(init_flag == INIT_NO) {
142
        return WL_ERROR_LIBRARY_NOT_INITD;
143
    }
144

    
145
    // packet group number exceeds available packets groups
146
    if(group >= MAX_PACKET_GROUPS) {
147
        return WL_ERROR_FAILED_UNREGISTRATION;
148
    }
149

    
150
    // packet group handler does not exists
151
    if(wl_packet_handlers[group].FUNC == NULL) {
152
        return WL_ERROR_FAILED_UNREGISTRATION;
153
    }
154

    
155
    // clear packet group information
156
    wl_packet_handlers[group].FUNC = NULL;
157
    wl_packet_handlers[group].priority = NORMAL_PRIORITY;
158
    return WL_SUCCESS;
159
}
160

    
161

    
162
/**@} **/ //end defgroup
163

    
164

    
165
// Define all private functions down here
166

    
167

    
168