
 

#ifndef COMMONSHIM_H
#define COMMONSHIM_H


#include <stdbool.h>
#include <stdint.h>

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "ti_154stack_config.h"
#include "api_mac.h"

#include <ti/drivers/UART2.h>   //print out options for UART2
//#include <ti/display/Display.h>
#include "ti_drivers_config.h"


#ifdef OSAL_PORT2TIRTOS
#ifndef FREERTOS_SUPPORT
#include <ti/sysbios/knl/Task.h>
#endif
#endif

#ifdef FEATURE_SECURE_COMMISSIONING
#include "sm_ti154.h"
#endif

#ifdef __cplusplus
extern "C"
{
#endif


/******************************************************************************
 Constants and definitions
 *****************************************************************************/
#ifdef IEEE_COEX_TEST
#define APS_RETRY_INTERVAL       (50)
#define APS_INTERVAL             (10)
#define APS_MAX_FRAME_RETRIES    (2)
#endif // IEEE_COEX_TEST
/*! Event ID - Start the device in the network */
#define COLLECTOR_START_EVT               0x0001
/*! Event ID - Tracking Timeout Event */
#define COLLECTOR_TRACKING_TIMEOUT_EVT    0x0002
/*! Event ID - Generate Configs Event */
#define COLLECTOR_CONFIG_EVT              0x0004
#define COLLECTOR_BROADCAST_TIMEOUT_EVT   0x0008
#ifdef FH_LOW_LATENCY_BROADCAST
#define COLLECTOR_BROADCAST_CAMCMD_EVT    0x1000
#define COLLECTOR_BROADCAST_SUCCESS_EVT   0x2000
#define COLLECTOR_BROADCAST_CAMCMDACK_EVT 0x4000
#endif // FH_LOW_LATENCY_BROADCAST

/*! Event ID - Start Provisioning Event */
#define COLLECTOR_PROV_EVT                0x0010
/*! Event ID - Open Network Event */
#define COLLECTOR_OPEN_NWK_EVT            0x0040
/*! Event ID - Close Network Event */
#define COLLECTOR_CLOSE_NWK_EVT           0x0080

#ifdef DMM_OAD
/*! Event ID - Pause 154 Collector */
#define COLLECTOR_PAUSE_EVT               0x0100
/*! Event ID - Resume 154 Collector */
#define COLLECTOR_RESUME_EVT              0x0200
#endif

#ifdef LPSTK
/* LPSTK configuration frame control */
#define CONFIG_FRAME_CONTROL (Smsgs_dataFields_tempSensor | \
                              Smsgs_dataFields_lightSensor | \
                              Smsgs_dataFields_humiditySensor | \
                              Smsgs_dataFields_msgStats | \
                              Smsgs_dataFields_configSettings | \
                              Smsgs_dataFields_hallEffectSensor | \
                              Smsgs_dataFields_accelSensor)
#else
/* Default configuration frame control */
#define CONFIG_FRAME_CONTROL (Smsgs_dataFields_tempSensor | \
                              Smsgs_dataFields_lightSensor | \
                              Smsgs_dataFields_humiditySensor | \
                              Smsgs_dataFields_msgStats | \
                              Smsgs_dataFields_configSettings)
#endif


#define MAX_PACKET_SIZE 256
/* Internal buffer for string formatting. 
 * 128 bytes is usually plenty for a single debug line. */
#define DEBUG_BUF_SIZE 128


static char debugBuf[DEBUG_BUF_SIZE];

typedef enum
{
    START,
    PRIMITIVE_TYPE1,
    PRIMITIVE_TYPE2,
    PARAMETERS_SIZE,
    WAIT_DATA,
} ReceiveState;

typedef enum
{
    SCAN_CFM = 1,
    START_CFM = 2,
    ASSOCIATE_CFM = 3,
    ASSOCIATE_IND = 4,
    COMM_STATUS_IND = 5,
    DATA_CFM = 6,
    DATA_IND = 7,
    SET_CFM = 8,
    GET_CFM = 9,
    ORPHAN_IND = 10,
    BEACON_NOTIFY_IND = 11,
    SENSOR_IND = 20
} PrimitiveType;

typedef enum : uint8_t
{
  MAC_MLME_REQ_ASSOCIATE = 0,
  MAC_MLME_REQ_DISASSOCIATE = 1,
  MAC_MLME_REQ_GET = 2,
  MAC_MLME_REQ_GTS = 3,
  MAC_MLME_REQ_RESET = 4,
  MAC_MLME_REQ_RX_ENABLE = 5,
  MAC_MLME_REQ_SCAN = 6,
  MAC_MLME_REQ_SET = 7,
  MAC_MLME_REQ_START = 8,
  MAC_MLME_REQ_SYNC = 9,
  MAC_MLME_REQ_POLL = 10,
  MAC_MLME_RSP_ASSOCIATE = 11,
  MAC_MLME_RSP_ORPHAN = 12,
  MAC_MLME_REQ_VS_EXTADDR = 13
} MlmeReqRspType;

typedef enum : uint8_t
{
  MAC_MCPS_REQ_DATA = 0
} McpsReqRspType;

typedef enum
{
    NONE,
    MLME_PRIMITIVE,
    MCPS_PRIMITIVE
} PrimitiveGroup;

ReceiveState m_state;
PrimitiveGroup m_primitiveGroup;
uint8_t m_rxBuffer[MAX_PACKET_SIZE]; //!< Represents the reception buffer of a single packet
uint32_t m_rxByteCount;
uint16_t m_primitiveMaxSize;
uint8_t m_mlmeReqRspType;
uint8_t m_mcpsReqRspType;


/*! Collector Status Values */
typedef enum
{
    /*! Success */
    Collector_status_success = 0,
    /*! Device Not Found */
    Collector_status_deviceNotFound = 1,
    /*! Collector isn't in the correct state to send a message */
    Collector_status_invalid_state = 2
} Collector_status_t;

/* Beacon order for non beacon network */
#define NON_BEACON_ORDER      15

/* Number of superframe periods to hold a indirect packet at collector for
Sensor to poll and get the frame*/
#define BCN_MODE_INDIRECT_PERSISTENT_TIME 3

#define MIN_PERSISTENCE_TIME_USEC 2000000

#ifndef MAX
#define MAX(n,m)   (((n) < (m)) ? (m) : (n))
#endif

#ifndef MIN
#define MIN(n,m)   (((n) < (m)) ? (n) : (m))
#endif


#if (CONFIG_PHY_ID == APIMAC_CUSTOM_PHY_ID)
/* MAC Indirevt Persistent Timeout */
#define INDIRECT_PERSISTENT_TIME (MIN((MAX((5 * 1000 * CONFIG_POLLING_INTERVAL / 2), MIN_PERSISTENCE_TIME_USEC)/ \
                                  (BASE_SUPER_FRAME_DURATION * \
                                   SYMBOL_DURATION_CUSTOM)), 65535))

#elif  ((CONFIG_PHY_ID >= APIMAC_MRFSK_STD_PHY_ID_BEGIN) && (CONFIG_PHY_ID <= APIMAC_MRFSK_GENERIC_PHY_ID_BEGIN))

/* MAC Indirect Persistent Timeout */
#define INDIRECT_PERSISTENT_TIME (MIN((MAX((5 * 1000 * CONFIG_POLLING_INTERVAL / 2), MIN_PERSISTENCE_TIME_USEC)/ \
                                  (BASE_SUPER_FRAME_DURATION * \
                                   SYMBOL_DURATION_50_kbps)), 65535))

#elif ((CONFIG_PHY_ID >= APIMAC_200KBPS_915MHZ_PHY_132) && (CONFIG_PHY_ID <= APIMAC_200KBPS_868MHZ_PHY_133)) || \
       (CONFIG_PHY_ID == APIMAC_200KBPS_920MHZ_PHY_136)

/* MAC Indirect Persistent Timeout */
#define INDIRECT_PERSISTENT_TIME (MIN((MAX((5 * 1000 * CONFIG_POLLING_INTERVAL / 2),MIN_PERSISTENCE_TIME_USEC) / \
                                  (BASE_SUPER_FRAME_DURATION * \
                                   SYMBOL_DURATION_200_kbps)), 65535))

#elif (CONFIG_PHY_ID == APIMAC_250KBPS_IEEE_PHY_0)

/* MAC Indirect Persistent Timeout */
#ifdef FEATURE_SECURE_COMMISSIONING
/* Increase the persistence time by a factor of 10 */
#define INDIRECT_PERSISTENT_TIME (MIN((MAX((5* 10 * 1000 * CONFIG_POLLING_INTERVAL / 2), MIN_PERSISTENCE_TIME_USEC)/ \
                                  (BASE_SUPER_FRAME_DURATION * \
                                   SYMBOL_DURATION_250_kbps)), 65535))
#else
#define INDIRECT_PERSISTENT_TIME (MIN((MAX((5 * 1000 * CONFIG_POLLING_INTERVAL / 2), MIN_PERSISTENCE_TIME_USEC)/ \
                                  (BASE_SUPER_FRAME_DURATION * \
                                   SYMBOL_DURATION_250_kbps)), 65535))
#endif

#else
/* MAC Indirect Persistent Timeout */
#define INDIRECT_PERSISTENT_TIME (MIN((MAX((5 * 1000 * CONFIG_POLLING_INTERVAL / 2), MIN_PERSISTENCE_TIME_USEC)/ \
                                  (BASE_SUPER_FRAME_DURATION * \
                                    SYMBOL_DURATION_LRM)), 65535))
#endif

/******************************************************************************
 Structures
 *****************************************************************************/



/******************************************************************************
 Global Variables
 *****************************************************************************/

/*! Collector events flags */
extern uint16_t Collector_events;


/******************************************************************************
 Function Prototypes
 *****************************************************************************/



#ifdef OSAL_PORT2TIRTOS
extern void InitializeCommonShim(uint8_t _macTaskId);
#else
extern void InitializeCommonShim(void);
#endif

void DebugPrint(const char *format, ...);
void SendBytes(uint8_t bytes[], uint8_t size);
void ProcessRxData();
void ReceiveByte(UART2_Handle handle, void *buffer, size_t count, void *userArg, int_fast16_t status);

extern void ProcessData(void);
void ScanRequest(void);
void OrphanResponse(void);
void OrphanIndication(ApiMac_mlmeOrphanInd_t *params);
void ScanConfirm(ApiMac_mlmeScanCnf_t *params);


/**
 * Triggers the execution of a MLME-START.request primitive.
 * Parameters used in this primitive are used from the Rx buffer.
 */
void StartRequest();


void StartConfirm(ApiMac_mlmeStartCnf_t *params);

void SetRequest();

void GetRequest();




#ifdef POWER_MEAS
/*! Generate data of fixed size for power measurement testing */
extern void generateIndirectRampMsg(void);
#endif

#ifdef __cplusplus
}
#endif

#endif /* COMMONSHIM_H */
