root / trunk / code / projects / libwireless / lib / wl_error_group.c @ 1460
History | View | Annotate | Download (3.21 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 | 346 | bcoltin | void wl_error_register(void) |
65 | { |
||
66 | wl_register_packet_group(&wl_error_handler); |
||
67 | 17 | bcoltin | } |
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 | 346 | bcoltin | void wl_error_unregister(void) |
76 | { |
||
77 | wl_unregister_packet_group(&wl_error_handler); |
||
78 | 17 | bcoltin | } |
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 | 346 | bcoltin | 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 | 17 | bcoltin | } |
111 | |||
112 | /**
|
||
113 | * Send an error message as a string.
|
||
114 | 717 | jknichel | *
|
115 | 17 | bcoltin | * @param str the error message to send
|
116 | **/
|
||
117 | 346 | bcoltin | 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 | 17 | bcoltin | } |