Project

General

Profile

Statistics
| Revision:

root / branches / wireless / code / projects / libwireless / wireless_receive.c @ 1589

History | View | Annotate | Download (4 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_receive.c
28
 * @brief Wireless library receive functions
29
 *
30
 * Implementation of high level wireless communication.
31
 * This is the receive functions portion of the library.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

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

    
41
// the receive functions
42

    
43
/**
44
 * @addtogroup wireless Wireless
45
 * @{
46
 **/
47

    
48
extern uint8_t xbee_basic_buf[PACKET_BUFFER_SIZE];  // buffer for basic-group packets
49
extern uint8_t xbee_other_buf[PACKET_BUFFER_SIZE];  // buffer for non-basic-group packets
50
extern uint8_t basic_buf_first;        // beginning of first packet in basic buffer
51
extern uint8_t basic_buf_last;        // end of last packet in basic buffer
52
extern uint8_t other_buf_first; // beginning of first packet in other buffer
53
extern uint8_t other_buf_last;  // end of last packet in other buffer
54

    
55
/**
56
 * the main receive function (similar to wl_do)
57
 * 
58
 * when called, this function will receive the next packet on the default packet group
59
 * it will also dispatch registered packet handler functions for any other packet groups it has received
60
 *
61
 * @param data an already-initialized array to store the default group packet data in
62
 * @param length the length of the initialized data array
63
 *
64
 * @return the length of the used portion of data array or error (<0)
65
 **/
66
int8_t wl_get(char *data, uint8_t length) {
67
  int8_t return_code = wl_dispatch(); // run dispatch of other packet groups
68
  if (return_code != 0)
69
    return return_code; // error, so end early
70
  return wl_get_basic(data, length); // get a basic packet
71
}
72

    
73
/**
74
 * function to receive only packets on the default group
75
 * this function is only meant to receive packets sent using wl_send_basic()
76
 * 
77
 * @param data an already-initialized array to store the default group packet data in
78
 * @param length the length of the initialized data array
79
 * 
80
 * @return the length of the used portion of data array or error (<0)
81
 **/
82
int8_t wl_get_basic(char *data, uint8_t length) {
83
    uint8_t buf_pos = basic_buf_first;        // start at beginning of first (oldest) basic packet
84
    uint8_t data_length = xbee_basic_buf[buf_pos];  // get packet length
85
    uint8_t packet_num;        // number of packet (may eventually be used to weed out duplicates)
86

    
87
    if (data_length > length)        // not enough room for packet in destination
88
        return WL_ERROR_TOO_SMALL;
89
    
90
    buf_pos++;
91
    packet_num = xbee_basic_buf[buf_pos];   // get packet number
92
    buf_pos++;
93
    buf_pos++;        // ignore the group code. it will always be the same.
94

    
95
    memcpy(data, xbee_basic_buf + buf_pos, data_length);    // get the data
96
    basic_buf_first += data_length; // "free up" this packet's buffer space
97

    
98
    return 0;
99
}
100

    
101
/*
102
 * function to dispatch all registered packet handlers for received packets on non-default groups
103
 *
104
 * @return error codes (TBD)
105
 */
106
int8_t wl_dispatch(void) {
107

    
108
  return 0;
109
}
110

    
111

    
112
/**@} **/ //end defgroup
113

    
114

    
115
// Define all private functions down here
116

    
117

    
118