/*
Copyright 2003/2004, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

This software is provided by FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB or contributors be liable
for any direct, indirect, incidental, special, exemplary, or
consequential damages (including, but not limited to, procurement of
substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.

This implementation was developed by the CST group at the FUB.
Contributors: Marco Hutta

For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2003/2004/2005
*/

#ifndef ScatterWeb_Net
#define ScatterWeb_Net

#include "ScatterWeb.System.h"

// Predefined Addresses
#define BROADCAST             (0xFFFF) // Broadcast Address.
#define UNKNOWN               (0x0000) // Illegal Address.

// Packet Types
#define ACK_PACKET            (0x00)   // Only for internal use by radio software.
#define SYNCH_PACKET          (0x01)   // INTERNAL synch packet.
#define PING_PACKET           (0x02)   // Simple ping.
#define PONG_PACKET           (0x03)   // Simple pong.
#define MESSAGING_REQ_PACKET  (0x04)   // Terminal request containing the terminal command.
#define MESSAGING_ANS_PACKET  (0x05)   // Terminal answer.
#define SCAN_PACKET           (0x06)   // Request for SCAN_REPLY.
#define SCAN_REPLY_PACKET     (0x07)   // Answer with information about device type and current software.
#define SENSOR_PACKET         (0x08)   // Predefined for broadcasting sensor values.
#define SMS_PACKET            (0x09)   // To send a sms over a mobile
#define PICTURE_REQ_PACKET    (0x0A)   // Requesting a Picture.
#define PICTURE_ANS_PACKET    (0x0B)   // Answer to PICTURE_REQ (containing failure message or picture data).

#define IS_RADIO_RECEIVING (cdCounter)            // Currently receiving?
#define IS_RADIO_SENDING   ((P5OUT & 0xC0)==0x40) // Switched to TX?

typedef struct {
  UINT16 to;            // Destination.
  UINT16 from;          // Source (automatically set by radioSend).
  UINT8 type;           // Packet type.
  UINT8 num;            // Packet number.
  UINT8* header;        // Pointer to packet extra header. 
  UINT16 header_length; // Extra header length, 0 means no extra header (in radioReceive always 0)
  UINT8* data;          // Pointer to packet data.
  UINT16 data_length;   // Length of data.
} packet_t;

// struct for received packet
extern volatile packet_t rxPacket;
// struct for sended packet
extern packet_t sentPacket;
// number of retries done (retries needed to deliver a packet).
extern volatile UINT8 txRetries;
// indicate if radio free or not
extern volatile UINT8 cdCounter;

/*
 * Function to init radio
 * 
 * @return bool     True always
 *  
 */
bool Net_init();

/*
 * Function to enable radio
 * 
 * @return bool     True always  
 *  
 */
bool Net_on();

/*
 * Function to disable radio
 * 
 * @return bool     True always  
 *  
 */
bool Net_off();

/*
 * Function to set the transceiver power
 * 
 * @param value     value of power
 * 
 * @return bool     True if value are between 0 and 100, otherwise False  
 *   
 */
bool Net_setTxPower(UINT8 value);

/*
 * Function to send a packet over radio
 *
 * @param packet    packet to send
 * @param callback  eventHandler
 * 
 * @return char     1  if packet send successful
 *                  -1 if packet send not successful
 *                  -2 if packet->type == ACK_PACKET
 *    
 */
char Net_send(packet_t* packet, fp_char_t callback);

/*
 * Function to handle receiving
 * 
 * @return bool     True always    
 *  
 */
bool Net_rxHandler();

/*
 * Function to handle sending
 * 
 * @return bool     True always    
 *  
 */
bool Net_txHandler();

#endif
