root / trunk / code / projects / libwireless / lib / wl_basic.c @ 1452
History | View | Annotate | Download (3.5 KB)
| 1 | /**
|
|---|---|
| 2 | * @file wl_basic.c |
| 3 | * @brief High Level Wireless Packet Sending-Receiving Functions |
| 4 | * |
| 5 | * Abstracted wireless functionality for sending and receiving packets |
| 6 | * |
| 7 | * @author Christopher Mar, Colony Project, CMU Robotics Club |
| 8 | **/ |
| 9 | |
| 10 | #include "wl_basic.h" |
| 11 | |
| 12 | /**
|
| 13 | * Wrapper for wl_init(). |
| 14 | * Initializes wireless and registers a packet handler function for Basic Group. |
| 15 | * |
| 16 | * @param handle_receive function pointer to handler function for Basic Group |
| 17 | * @return 0 on success, -1 on error |
| 18 | **/ |
| 19 | int wl_basic_init( void(*handle_receive) (char type, int source, |
| 20 | unsigned char* packet, int length) ) { |
| 21 | int returnme = wl_init();
|
| 22 | wl_basic_register_handler(handle_receive); |
| 23 | current_packet.new_flag = 0;
|
| 24 | return returnme;
|
| 25 | } |
| 26 | |
| 27 | /**
|
| 28 | * Wrapper for wl_init(). |
| 29 | * Initializes wireless and registers the default packet handler for Basig Group. |
| 30 | * |
| 31 | **/ |
| 32 | int wl_basic_init_default( ) {
|
| 33 | return wl_basic_init(&wl_basic_packet_receive_handler);
|
| 34 | } |
| 35 | |
| 36 | /**
|
| 37 | * Internal function to register a packet group handler for Basic Group. |
| 38 | * |
| 39 | * @param handle_receive function pointer to handler function for Basic Group |
| 40 | **/ |
| 41 | void wl_basic_register_handler( void (*handle_receive) (char type, int source, |
| 42 | unsigned char* packet, int length)) { |
| 43 | wl_basic_group_handler.groupCode = WL_BASIC_GROUP; |
| 44 | wl_basic_group_handler.timeout_handler = 0;
|
| 45 | wl_basic_group_handler.handle_response = 0;
|
| 46 | wl_basic_group_handler.handle_receive = handle_receive; |
| 47 | wl_basic_group_handler.unregister = 0;
|
| 48 | |
| 49 | wl_register_packet_group(&wl_basic_group_handler); |
| 50 | } |
| 51 | |
| 52 | /**
|
| 53 | * Send a packet to a single robot in Basic Group. |
| 54 | * |
| 55 | * @param type Packet type |
| 56 | * @param data Packet buffer, data you want to send |
| 57 | * @param len Length of the data buffer in bytes |
| 58 | * @param dest Robot ID of the destination robot |
| 59 | **/ |
| 60 | void wl_basic_send_robot_packet( char type, char* data, int len, int dest ) { |
| 61 | wl_send_robot_to_robot_global_packet(WL_BASIC_GROUP, type, data, len, dest, 0);
|
| 62 | } |
| 63 | |
| 64 | /**
|
| 65 | * Send a packet to all robots in Basic Group. |
| 66 | * |
| 67 | * @param type Packet type |
| 68 | * @param data Packet buffer, data you want to send |
| 69 | * @param len Length of the data buffer in bytes |
| 70 | **/ |
| 71 | void wl_basic_send_global_packet( char type, char* data, int len ) { |
| 72 | wl_send_global_packet(WL_BASIC_GROUP, type, data, len, 0);
|
| 73 | } |
| 74 | |
| 75 | /**
|
| 76 | * Default packet handler used if none is specified on init. |
| 77 | * This should not be called directly by any user program. |
| 78 | * |
| 79 | * @param type Packet type |
| 80 | * @param source the robot ID of the sending robot |
| 81 | * @param packet Packet buffer, data you received |
| 82 | * @param length Length of the data buffer in bytes |
| 83 | **/ |
| 84 | void wl_basic_packet_receive_handler( char type, int source, unsigned char* packet, int length ) { |
| 85 | current_packet.new_flag = 1;
|
| 86 | current_packet.type = type; |
| 87 | current_packet.source = source; |
| 88 | current_packet.data = packet; |
| 89 | current_packet.length = length; |
| 90 | } |
| 91 | |
| 92 | /**
|
| 93 | * Wrapper for wl_do() for use with the Basic Wireless group. |
| 94 | * Can only be called if wl_basic_init_default and your packets |
| 95 | * are handled with the default packet handler. |
| 96 | * To access packet information, use PacketInfo current_packet. |
| 97 | * |
| 98 | * @param length a pointer to int to store the length of the data buffer |
| 99 | * |
| 100 | * @return pointer to the data of the packet just received, 0 if no new packet |
| 101 | **/ |
| 102 | unsigned char* wl_basic_do_default( int *length ) { |
| 103 | wl_do(); |
| 104 | if (current_packet.new_flag == 1) { |
| 105 | current_packet.new_flag = 0;
|
| 106 | *length = current_packet.length; |
| 107 | return current_packet.data;
|
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 |