root / branches / colonetmk2 / code / projects / libwireless / lib / wl_error_group.c @ 1456
History | View | Annotate | Download (3.7 KB)
| 1 | /**
|
|---|---|
| 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 | #include "wl_error_group.h" |
| 36 | #include "wireless.h" |
| 37 | #include "wl_defs.h" |
| 38 | |
| 39 | #include <string.h> |
| 40 | |
| 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 | //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 | void wl_error_register(void) |
| 67 | {
|
| 68 | wl_register_packet_group(&wl_error_handler); |
| 69 | } |
| 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 | //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 | void wl_error_unregister(void) |
| 80 | {
|
| 81 | wl_unregister_packet_group(&wl_error_handler); |
| 82 | } |
| 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 | 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 | } |
| 115 | |
| 116 | /**
|
| 117 | * Send an error message as a string. |
| 118 | * |
| 119 | * @param str the error message to send |
| 120 | **/ |
| 121 | //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 | 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 | } |
| 128 |