/*
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: Bjoern Lichtblau

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
*/

#include <io.h>
#include "ScatterWeb.Event.h"
#include "ScatterWeb.Scanner.h"
#include "../../../../System/src/ScatterWeb.System.h"

void Process_interceptRadioHandler() {

}

void Process_radioHandler() {
  if ( !(rxPacket.to == BROADCAST || rxPacket.to == Configuration.id) ) {
    Process_interceptRadioHandler();
  }
  else {
    switch(rxPacket.type){
      case ACK_PACKET: break;
      case SYNCH_PACKET: break;
      case SCAN_PACKET: Scanner_scanHandler(); break;
      case SCAN_REPLY_PACKET: Scanner_replyHandler(); break;  
      case MESSAGING_REQ_PACKET: Messaging_requestHandler(); break;
      case MESSAGING_ANS_PACKET: Messaging_answerHandler(); break;
      case PING_PACKET: 
         if (rxPacket.to == Configuration.id) {
           packet_t packet;
           packet.to = rxPacket.from;    packet.type = PONG_PACKET;
           packet.header_length = 0;     packet.header = NULL;
           packet.data_length = 0;       packet.data = NULL;
           Net_send(&packet, NULL);
         }
         break;
      case PONG_PACKET: 
          if (rxPacket.to == Configuration.id) {
            Comm_print("PONG from %i\r\n", rxPacket.from);   
          }
          break;
      default: break;
    }
  }
}

void Process_init(){
  System_registerCallback(C_RADIO, Process_radioHandler);
  Event_init();
  Scanner_init();
  Comm_log(LOW, "| Application %s initialized\r\n\r\n", imageData.versionName);    
}


/// \name Software header. See programming.h for description.
/// Theses variables are defined by the linker script, they are needed in the software header
extern int __image_start;
extern int __image_end;

extern UINT16 prog_reprogram();
__attribute__((section(".imagedata"))) const imageData_t imageData = {
    (UINT16)&__image_start, (UINT16)&__image_end,

  1, // ESB
    (UINT16)prog_reprogram,
    "ESB: V2.10",     // Must be 20 chars max (with \0 21 alltogether).
    __DATE__ " " __TIME__   // Only filled when programming.o is rebuilt!
};
