//*****************************************************************************
//
//! @file
//! @brief ZEP
//
//*****************************************************************************


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ZEP_H
#define __ZEP_H

/* Includes ------------------------------------------------------------------*/

// STELARISWARE
#include "lwip/opt.h"
#include "sniffer.h"

// PROJECT INCLUDES
#include "radio/packet.h"


#include <string.h>
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

#define ZEP_DEFAULT_PORT   17754

/*  ZEP Preamble Code */
#define ZEP_PREAMBLE       0x5845         //hton("EX")

static char ZEP_RESERVED[]={0,0,0,0,0,0,0,0,0,0};

/*  ZEP Header lengths. */
#define ZEP_V1_HEADER_LEN   16
#define ZEP_V2_HEADER_LEN   30          //32
#define ZEP_V2_ACK_LEN      8

#define ZEP_V2_TYPE_DATA    1
#define ZEP_V2_TYPE_ACK     2

#define ZEP_LENGTH_MASK     0x7F

/////////////////////////////////////////////////////////////////////////

//jedna děleno frekvence uP (1/8000000), výsledek v sekundách
//TODO: změnit dle frekvence uP

#define CPU_CYCLE_TIME  0.000000125
#define TIMER_MAXTIME   536.870912

//for 50MHz System clock
//#define CPU_CYCLE_TIME  0.00000002
//#define TIMER_MAXTIME   85.89934592

//
//original 4294967296.0, vyděleno 1000000000.0
//hodnota pro správné zobrazení ve Wireshark
//
#define NTP_FLOAT_DENOM 4.294967296 

//#define NTP_BASE_N 4294967296.0
#define NTP_BASE_N 4294.967296

//konstanta pro přičtení k sekundám do Wiresharku
#define NTP_BASE_S 2208988800ul

/* Exported macro ------------------------------------------------------------*/

/* Exported structs ----------------------------------------------------------*/
 
typedef struct {
	unsigned int     secs;      //original time_t	secs;
	unsigned int	nsecs;
} nstime_t;

#ifdef PACK_STRUCT_USE_INCLUDES
#  include "arch/bpstruct.h"
#endif


PACK_STRUCT_BEGIN
struct nstime_t{
//    u16_t     preamble;
PACK_STRUCT_FIELD( u32_t      secs );
PACK_STRUCT_FIELD( u32_t      nsecs);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END

PACK_STRUCT_BEGIN
struct zep_head{
PACK_STRUCT_FIELD( u16_t     preamble);
PACK_STRUCT_FIELD( u8_t      version);
PACK_STRUCT_FIELD( u8_t      type);
PACK_STRUCT_FIELD( u8_t      channel_id);
PACK_STRUCT_FIELD( u16_t     device_id);
PACK_STRUCT_FIELD( u8_t      lqi_mode);   //boolean
PACK_STRUCT_FIELD( u8_t      lqi);
PACK_STRUCT_FIELD( struct nstime_t  ntp_time);
PACK_STRUCT_FIELD( u32_t     seqno);
PACK_STRUCT_FIELD( u8_t      reserved[10]);
PACK_STRUCT_FIELD( u8_t      length);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END

#ifdef PACK_STRUCT_USE_INCLUDES
#  include "arch/epstruct.h"
#endif

/* Exported functions ------------------------------------------------------- */
extern void zep_init(unsigned long *ptrz, tSnifferSettings *ptrs);
extern void zep_send(TtrxPacket *trxPacketPtr);
//extern void zep_send_test(void);

#endif /* __ZEP_H */


