Project

General

Profile

Revision 1598

Added by Chris Mar over 10 years ago

implemented wireless init, terminate, register, and unregister

View differences:

branches/wireless/code/projects/libwireless/wireless.h
92 92
#define CCA_FAILURE UINT8_C(3)
93 93

  
94 94
/**@brief packet handler function pointer type **/
95
#define FNPTR void (*func)(uint8_t* data,uint8_t length)
95
#define FUNC func
96 96

  
97
/**@brief packet handler function pointer type **/
98
#define FNPTR void (*FUNC)(uint8_t* data,uint8_t length,uint8_t source)
99

  
100
// TODO: this max may be too big b/c the packet handler array stores 3*MAX_PACKET_GROUPS bytes 
101
/**@brief maximum number of packet groups, size of handler array **/
102
#define MAX_PACKET_GROUPS UINT8_C(255)
103

  
104
/**@brief init_flag when library has not been initialized **/
105
#define INIT_NO UINT8_C(0)
106

  
107
/**@brief init_flag when library has been initialized **/
108
#define INIT_YES UINT8_C(1)
109

  
97 110
/**@} **/ // end defines group
98 111

  
112
/**
113
 * @struct PacketGroupHandler
114
 * A PacketGroupHandler represents a packet group, and is used to
115
 * register a packet group with the wireless library. It contains
116
 * a function pointer to the packet handler function and the priority
117
 * of the packet group.
118
 **/
119
typedef struct {
120
    
121
    // function pointer of handler for this packet group
122
    FNPTR;
99 123

  
124
    // priority for this packet group
125
    uint8_t priority;
126
    
127
} PacketGroupHandler;
128

  
129

  
100 130
/**@defgroup wl_functions Public API Functions 
101 131
 * @{ **/
102 132
 
......
156 186
/**@brief Function to register new packet handlers (for non-default groups only). **/
157 187
int8_t wl_register_handler(uint8_t group, FNPTR, uint8_t priority);
158 188

  
189
/**@brief Function to unregister existing packet handlers (for non-default groups only). **/
190
int8_t wl_unregister_handler(uint8_t group);
191

  
159 192
/**@} **/ //end functions group
160 193

  
161 194
/**@} **/ //end wireless group
branches/wireless/code/projects/libwireless/wl_defs.h
83 83
/** @brief Error code for failed packet group registration **/
84 84
#define WL_ERROR_FAILED_REGISTRATION INT8_C(-5)
85 85

  
86
/** @brief Error code for failed packet group registration **/
87
#define WL_ERROR_FAILED_UNREGISTRATION INT8_C(-6)
88

  
86 89
/** @brief Error in arguments **/
87
#define WL_ERROR_ARGUMENT INT8_C(-6)
90
#define WL_ERROR_ARGUMENT INT8_C(-7)
88 91

  
89 92
/** @brief Error code for send failure **/
90 93
#define WL_ERROR_SEND INT8_C(-10)
branches/wireless/code/projects/libwireless/wireless.c
32 32
 * @author Colony Project, CMU Robotics Club
33 33
 **/
34 34

  
35
#include <string.h>
35 36
#include "wl_defs.h"
36 37
#include "wireless.h"
38
#include "xbee.h"
37 39

  
38

  
39 40
// this is the common wireless file
40 41

  
41 42
/**
......
43 44
 * @{
44 45
 **/
45 46

  
47
static uint8_t init_flag = INIT_NO;
48
static PacketGroupHandler wl_packet_handlers[MAX_PACKET_GROUPS];
49

  
46 50
/**
47 51
 * initialization function:
48 52
 * set up xbee communication and packet handler infrastructure
......
51 55
 **/
52 56
int8_t wl_init(void) {
53 57

  
54
  return 0;
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_lib_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;
55 78
}
56 79

  
57 80
/**
......
62 85
 **/
63 86
int8_t wl_terminate(void) {
64 87

  
65
  return 0;
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
    // TODO need pass termination error from lower level functions
97
    xbee_terminate();
98
    // if(TERMINATE FAILS) {
99
    //     return WL_ERROR_TERMINATION_FAILED
100
    // }
101

  
102
    return WL_SUCCESS;
66 103
}
67 104

  
68 105
/**
......
76 113
 **/
77 114
int8_t wl_register_handler(uint8_t group, FNPTR, uint8_t priority) {
78 115

  
79
  return WL_SUCCESS;
116
    // check to see if wireless library has been initialized
117
    if(init_flag == INIT_NO) {
118
        return WL_ERROR_LIBRARY_NOT_INITD;
119
    }
120

  
121
    // packet group number exceeds available packets groups
122
    if(group >= MAX_PACKET_GROUPS) {
123
        return WL_ERROR_FAILED_REGISTRATION;
124
    }
125

  
126
    // packet group handler already exists, user needs to unregister first
127
    if(wl_packet_handlers[group].FUNC != NULL) {
128
        return WL_ERROR_FAILED_REGISTRATION;
129
    }
130
    
131
    // save packet handler function pointer and priority
132
    wl_packet_handlers[group].FUNC = FUNC;
133
    wl_packet_handlers[group].priority = priority;
134
    return WL_SUCCESS;
80 135
}
81 136

  
137
/**
138
 * function to unregister existing packet handlers (for non-default groups only)
139
 *
140
 * @param group the packet group number of the handler to unregister
141
 *
142
 * @return 0 on success, or error code
143
 **/
144
int8_t wl_unregister_handler(uint8_t group) {
82 145

  
146
    // check to see if wireless library has been initialized
147
    if(init_flag == INIT_NO) {
148
        return WL_ERROR_LIBRARY_NOT_INITD;
149
    }
150

  
151
    // packet group number exceeds available packets groups
152
    if(group >= MAX_PACKET_GROUPS) {
153
        return WL_ERROR_FAILED_UNREGISTRATION;
154
    }
155

  
156
    // packet group handler does not exists
157
    if(wl_packet_handlers[group].FUNC == NULL) {
158
        return WL_ERROR_FAILED_UNREGISTRATION;
159
    }
160

  
161
    // clear packet group information
162
    wl_packet_handlers[group].FUNC = NULL;
163
    wl_packet_handlers[group].priority = NORMAL_PRIORITY;
164
    return WL_SUCCESS;
165
}
166

  
167

  
83 168
/**@} **/ //end defgroup
84 169

  
85 170

  

Also available in: Unified diff