/**
* Copyright (c) 2007 Colony Project
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/
/**
* @file wireless.h
* @brief Contains definitions for the wireless library.
*
* Contains functions for the wireless library.
*
* @author Brian Coltin, Colony Project, CMU Robotics Club
**/
#ifndef WIRELESS_H
#define WIRELESS_H
//Note: If this is raised above 16, we will need to do
//something about frame numbers for TX Status packets.
/**
* The maximum number of packet groups.
**/
//TODO: a PacketGroupHandler is at least 10 bytes (I don't know if function pointers are 2 bytes
// or 4 bytes). That means that in the c file, your array of packet groups is at least 160 bytes.
// Normally that might be fine (the robot's avr chips have 4k SRAM), but austin's chip only has
// 1k SRAM, so if this number can be reduced or if the size of the struct could be reduced, that would be a plus.
#define WL_MAX_PACKET_GROUPS 16
/**
* @defgroup wireless Wireless
* @brief Wireless definitions.
*
* Contains functions and definitions for dealing with wireless functionality.
*
* The wireless library provides a modular method for dealing with
* wireless packets, by allowing packet groups to be registered.
* A packet group is a collection of packets which share a packet
* group code. Each packet in the group also has a type. A packet
* group code and type are sent with each packet. When a packet
* with a group code registered in the wireless library is
* received, the corresponding event handler is called. The
* event handler uses the packet type and other information
* stored in the packet to respond.
*
* This architecture allows different wireless functionality to be
* defined and handled separately, making it simpler and more
* efficient to take advantage of the XBee's wireless functionality.
*
* @{
**/
/**
* @struct PacketGroupHandler
* A PacketGroupHandler represents a packet group, and is used to
* register a packet group with the wireless library. It contains
* handlers for various events which can occur related to a packet
* group.
**/
//TODO: the order of member variables in this struct should be changed in case the compile packs the struct
// In order to achieve the best packing, the variables should be listed in order of decreasing memory size.
// Thus, pointers should be first, followed by int, followed by char.
typedef struct
{
/**
* The group code for this packet group. This number
* must be unique. The maximum number of packet groups
* is defined by WL_MAX_PACKET_GROUPS.
**/
//TODO: if this number must be less than or equal to WL_MAX_PACKET_GROUPS, don't you only need
// one byte for it and it can be made an unsigned char?
unsigned int groupCode;
/**
* Called every half second (not in interrupt,
* but in wl_do).
**/
void (*timeout_handler) (void);
/**
* Called when a transmit status packet is received
* from the XBee where the first four bits of the frame
* are the group code.
*
* @param frame the last four bits of the frame
* @param received is true if we received an ack, 0 if
* we did not.
**/
void (*handle_response) (int frame, int received);
/**
* Called when we receive a packet from this group.
*
* @param type the packet type
* @param source the 16-bit address of the XBee this
* packet was sent from
* @param packet the packet received
* @param length the length of the packet
**/
void (*handle_receive) (char type, int source, unsigned char* packet, int length);
/**
* Called for any cleanup when the network is turned off.
**/
void (*unregister) (void);
} PacketGroupHandler;
/**@brief Initialize the wireless library **/
int wl_init(void);
/**@brief Uninitialize the wireless library **/
void wl_terminate(void);
/**@brief Perform wireless library functionality **/
void wl_do(void);
/**@brief Register a packet group with the wireless library **/
void wl_register_packet_group(PacketGroupHandler* h);
/**@brief Unregister a packet group with the wireless library **/
void wl_unregister_packet_group(PacketGroupHandler* h);
/**@brief Send a packet to a specific robot in any PAN **/
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame);
/**@brief Send a packet to a specific robot in our PAN **/
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame);
/**@brief Send a packet to all robots **/
int wl_send_global_packet(char group, char type, char* data, int len, char frame);
/**@brief Send a packet to all robots in our PAN **/
void wl_send_pan_packet(char group, char type, char* data, int len, char frame);
/**@brief Set the PAN we are using **/
int wl_set_pan(int pan);
/**@brief Get the PAN we are using **/
int wl_get_pan(void);
/**@brief Set the channel we are using **/
int wl_set_channel(int channel);
/**@brief Get the channel we are using **/
int wl_get_channel(void);
/**@brief Get the 16-bit address of the XBee module **/
int wl_get_xbee_id(void);
/**@brief Set the com port on a computer, undefined on the robot.**/
void wl_set_com_port(char* port);
/** @} **/ // end defgroup
#endif