root / branches / autonomous_recharging / code / lib / src / libwireless / wl_error_group.c @ 743
History | View | Annotate | Download (3.23 KB)
1 | 743 | bcoltin | /**
|
---|---|---|---|
2 | * Copyright (c) 2007 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 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 | 18 | 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 | {WL_ERROR_GROUP, NULL,
|
||
49 | wl_error_response_receive, wl_error_handle_receive, NULL};
|
||
50 | |||
51 | 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 | } |
||
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 | void wl_error_register(void) |
||
65 | { |
||
66 | wl_register_packet_group(&wl_error_handler); |
||
67 | } |
||
68 | |||
69 | /**
|
||
70 | * Unregister this packet group with the wireless library.
|
||
71 | * This function must be called after wl_error_register.
|
||
72 | *
|
||
73 | * @see wl_error_register
|
||
74 | **/
|
||
75 | void wl_error_unregister(void) |
||
76 | { |
||
77 | wl_unregister_packet_group(&wl_error_handler); |
||
78 | } |
||
79 | |||
80 | /**
|
||
81 | * Handles receiving an error packet.
|
||
82 | *
|
||
83 | * @param type the packet type
|
||
84 | * @param source the 16-bit address of the packet's sender
|
||
85 | * @param packet the packet data
|
||
86 | * @param length the length in bytes of the packet
|
||
87 | **/
|
||
88 | void wl_error_handle_receive(char type, int source, unsigned char* packet, |
||
89 | int length)
|
||
90 | { |
||
91 | switch (type)
|
||
92 | { |
||
93 | case WL_ERROR_STRING_TYPE:
|
||
94 | if (packet[length - 1] != 0) |
||
95 | { |
||
96 | WL_DEBUG_PRINT( |
||
97 | "Error packet string should be null terminated.\r\n");
|
||
98 | break;
|
||
99 | } |
||
100 | WL_DEBUG_PRINT("Error packet received from robot ");
|
||
101 | WL_DEBUG_PRINT_INT(source); |
||
102 | WL_DEBUG_PRINT(":\r\n");
|
||
103 | WL_DEBUG_PRINT((char*)packet);
|
||
104 | WL_DEBUG_PRINT("\r\n");
|
||
105 | break;
|
||
106 | default:
|
||
107 | WL_DEBUG_PRINT("Error packet of unknown type received.\r\n");
|
||
108 | break;
|
||
109 | } |
||
110 | } |
||
111 | |||
112 | /**
|
||
113 | * Send an error message as a string.
|
||
114 | *
|
||
115 | * @param str the error message to send
|
||
116 | **/
|
||
117 | void wl_error_send_string(char* str) |
||
118 | { |
||
119 | wl_send_global_packet(WL_ERROR_GROUP, WL_ERROR_STRING_TYPE, |
||
120 | str, strlen(str) + 1, 1); |
||
121 | } |