Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / lib / wl_basic.c @ 1428

History | View | Annotate | Download (3.48 KB)

1 1379 cmar
/**
2
 * @file wl_basic.c
3
 * @brief High Level Wireless Packet Sending-Receiving Functions
4
 *
5
 * Abstracted wireless functionality for sending and receiving packets
6
 *
7 1380 cmar
 * @author Christopher Mar, Colony Project, CMU Robotics Club
8 1379 cmar
 **/
9
10
#include <wireless.h>
11
#include "wl_basic.h"
12
13
/**
14
 * Wrapper for wl_init().
15 1425 cmar
 * Initializes wireless and registers a packet handler function for Basic Group.
16 1379 cmar
 *
17
 * @param handle_receive function pointer to handler function for Basic Group
18
 * @return 0 on success, -1 on error
19
 **/
20
int wl_basic_init( void(*handle_receive) (char type, int source,
21
                   unsigned char* packet, int length) ) {
22
    int returnme = wl_init();
23
    wl_basic_register_handler(handle_receive);
24 1396 cmar
    current_packet.new_flag = 0;
25 1379 cmar
    return returnme;
26
}
27
28
/**
29 1425 cmar
 * Wrapper for wl_init().
30
 * Initializes wireless and registers the default packet handler for Basig Group.
31 1396 cmar
 *
32
 **/
33
int wl_basic_init_default( ) {
34
    return wl_basic_init(&wl_basic_packet_receive_handler);
35
}
36
37
/**
38 1425 cmar
 * Internal function to register a packet group handler for Basic Group.
39 1379 cmar
 *
40
 * @param handle_receive function pointer to handler function for Basic Group
41
 **/
42
void wl_basic_register_handler( void (*handle_receive) (char type, int source,
43
                        unsigned char* packet, int length)) {
44
    wl_basic_group_handler.groupCode = WL_BASIC_GROUP;
45
    wl_basic_group_handler.timeout_handler = 0;
46
    wl_basic_group_handler.handle_response = 0;
47
    wl_basic_group_handler.handle_receive = handle_receive;
48
    wl_basic_group_handler.unregister = 0;
49
50
    wl_register_packet_group(&wl_basic_group_handler);
51
}
52
53
/**
54 1425 cmar
 * Send a packet to a single robot in Basic Group.
55 1379 cmar
 *
56
 * @param type Packet type
57
 * @param data Packet buffer, data you want to send
58
 * @param len Length of the data buffer in bytes
59 1425 cmar
 * @param dest Robot ID of the destination robot
60 1379 cmar
 **/
61
void wl_basic_send_robot_packet( char type, char* data, int len, int dest ) {
62
    wl_send_robot_to_robot_global_packet(WL_BASIC_GROUP, type, data, len, dest, 0);
63
}
64
65
/**
66 1425 cmar
 * Send a packet to all robots in Basic Group.
67 1379 cmar
 *
68
 * @param type Packet type
69
 * @param data Packet buffer, data you want to send
70
 * @param len Length of the data buffer in bytes
71
 **/
72
void wl_basic_send_global_packet( char type, char* data, int len ) {
73
    wl_send_global_packet(WL_BASIC_GROUP, type, data, len, 0);
74
}
75
76 1396 cmar
/**
77 1425 cmar
 * Default packet handler used if none is specified on init.
78
 * This should not be called directly by any user program.
79 1396 cmar
 *
80
 * @param type Packet type
81 1425 cmar
 * @param source the robot ID of the sending robot
82
 * @param packet Packet buffer, data you received
83 1396 cmar
 * @param length Length of the data buffer in bytes
84
 **/
85
void wl_basic_packet_receive_handler( char type, int source, unsigned char* packet, int length ) {
86
    current_packet.new_flag = 1;
87
    current_packet.type = type;
88
    current_packet.source = source;
89 1425 cmar
    current_packet.data = packet;
90 1396 cmar
    current_packet.length = length;
91
}
92
93
/**
94 1425 cmar
 * Wrapper for wl_do() for use with the Basic Wireless group.
95
 * Can only be called if wl_basic_init_default and your packets
96
 *  are handled with the default packet handler.
97
 * To access packet information, use PacketInfo current_packet.
98 1396 cmar
 *
99 1427 cmar
 * @param length a pointer to int to store the length of the data buffer
100
 *
101 1425 cmar
 * @return pointer to the data of the packet just received, 0 if no new packet
102 1396 cmar
 **/
103 1427 cmar
unsigned char* wl_basic_do_default( int *length ) {
104 1396 cmar
    wl_do();
105
    if (current_packet.new_flag == 1) {
106
        current_packet.new_flag = 0;
107 1427 cmar
        *length = current_packet.length;
108 1425 cmar
        return current_packet.data;
109 1396 cmar
    }
110
    return 0;
111
}