Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / libwireless / lib / wl_error_group.c @ 767

History | View | Annotate | Download (3.23 KB)

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