/*
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://www.scatterweb.net 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 FLASH_HEADER          (0x11)   // Initial request to a target to accept a new image.
#define FLASH_HEADER_REPLY    (0x12)   // Acknowledge for HEADER (and FIN for multiple DEVICES), Deny if busy, not programmable or other error.
#define FLASH_DATA            (0x14)   // Software image data chunk.
#define FLASH_REQ             (0x15)   // End of transmission packet. Acknowledge to FIN. Request if a data chunk is still missing to FIN.
#define ROUTING_INFO          (0x20)   // For broadcasting routing state.
#define ROUTING_REQUEST       (0x22)   // Request childhood at a new parent.
#define ROUTING_ACCEPT        (0x23)   // Accept a new child.
#define ROUTING_DENY          (0x24)   // Deny a new child.
#define ROUTING_NOCHILD       (0x25)   // To inform a parent of a wrong childhood.
#define ROUTING_ROOTPACKET    (0x26)   // Packets destinated to the root.
#define ROUTING_TREEPACKET    (0x27)   // Packets flooded through the tree by the root.

#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;
// state of receiving state machine (public for debugging).
extern volatile UINT8 txState;

/*
 * Initializes the radio
 * 
 * @return bool     True always
 *  
 */
bool Net_init();

/*
 * Enables the radio
 * 
 * @return bool     True always  
 *  
 */
bool Net_on();

/*
 * Disables the radio
 * 
 * @return bool     True always  
 *  
 */
bool Net_off();

/*
 * Sets the transceiver power
 * 
 * @return bool     True if value are between 0 and 100, otherwise False  
 *   
 */
bool Net_setTxPower(UINT8 value, UINT8 write);

/*
 * Sends a packet over radio
 *
 * @return char     True if packet send successful, otherwise False
 *    
 */
bool Net_send(packet_t* packet, fp_char_t callback);

/* 
 * This members support the ScatterWeb infrastructure
 * and are not intended to be used directly from your code.
 */
bool Net_rxHandler();
bool Net_txHandler();
char Net_esend(packet_t* packet, fp_char_t callback);

#endif
