Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / lib / wl_error_group.c @ 1452

History | View | Annotate | Download (3.67 KB)

1 242 bcoltin
/**
2
 * Copyright (c) 2007 Colony Project
3 717 jknichel
 *
4 242 bcoltin
 * 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 717 jknichel
 *
13 242 bcoltin
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15 717 jknichel
 *
16 242 bcoltin
 * 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 wl_error_group.h
28
 * @brief Error Packets
29
 *
30
 * A wireless group for sending error packets.
31
 *
32
 * @author Brian Coltin, Colony Project, CMU Robotics Club
33
 **/
34
35 17 bcoltin
#include "wl_error_group.h"
36 1452 dsschult
#include "wireless.h"
37
#include "wl_defs.h"
38 17 bcoltin
39
#include <string.h>
40
41
42 1452 dsschult
43 17 bcoltin
void wl_error_response_receive(int frame, int received);
44
void wl_error_handle_receive(char type, int source, unsigned char* packet,
45 346 bcoltin
                                                        int length);
46 17 bcoltin
47
PacketGroupHandler wl_error_handler =
48 346 bcoltin
                {WL_ERROR_GROUP, NULL,
49
                wl_error_response_receive, wl_error_handle_receive, NULL};
50 17 bcoltin
51 346 bcoltin
void wl_error_response_receive(int frame, int received)
52
{
53
        WL_DEBUG_PRINT("Response received.\r\n");
54
        if (!received)
55
        {
56
                WL_DEBUG_PRINT("FAILED.\r\n");
57
        }
58 17 bcoltin
}
59
/**
60
 * Register this packet group with the wireless library.
61
 * This function must be called before any other wl_error
62
 * function.
63
 **/
64 717 jknichel
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
65
// it reduces code size or not should be done to be sure.
66 346 bcoltin
void wl_error_register(void)
67
{
68
        wl_register_packet_group(&wl_error_handler);
69 17 bcoltin
}
70
71
/**
72
 * Unregister this packet group with the wireless library.
73
 * This function must be called after wl_error_register.
74
 *
75
 * @see wl_error_register
76
 **/
77 717 jknichel
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
78
// it reduces code size or not should be done to be sure.
79 346 bcoltin
void wl_error_unregister(void)
80
{
81
        wl_unregister_packet_group(&wl_error_handler);
82 17 bcoltin
}
83
84
/**
85
 * Handles receiving an error packet.
86
 *
87
 * @param type the packet type
88
 * @param source the 16-bit address of the packet's sender
89
 * @param packet the packet data
90
 * @param length the length in bytes of the packet
91
 **/
92
void wl_error_handle_receive(char type, int source, unsigned char* packet,
93 346 bcoltin
                                                        int length)
94
{
95
        switch (type)
96
        {
97
                case WL_ERROR_STRING_TYPE:
98
                        if (packet[length - 1] != 0)
99
                        {
100
                                WL_DEBUG_PRINT(
101
                                        "Error packet string should be null terminated.\r\n");
102
                                break;
103
                        }
104
                        WL_DEBUG_PRINT("Error packet received from robot ");
105
                        WL_DEBUG_PRINT_INT(source);
106
                        WL_DEBUG_PRINT(":\r\n");
107
                        WL_DEBUG_PRINT((char*)packet);
108
                        WL_DEBUG_PRINT("\r\n");
109
                        break;
110
                default:
111
                        WL_DEBUG_PRINT("Error packet of unknown type received.\r\n");
112
                        break;
113
        }
114 17 bcoltin
}
115
116
/**
117
 * Send an error message as a string.
118 717 jknichel
 *
119 17 bcoltin
 * @param str the error message to send
120
 **/
121 717 jknichel
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
122
// it reduces code size or not should be done to be sure.
123 346 bcoltin
void wl_error_send_string(char* str)
124
{
125
        wl_send_global_packet(WL_ERROR_GROUP, WL_ERROR_STRING_TYPE,
126
                str, strlen(str) + 1, 1);
127 17 bcoltin
}