/*******************************************************************************
* Project:      Amped'RF Bluetooth
* File name:    App_Main.h
* Description:  Main application header.
*
* Copyright (c) 2016 Amped'RF Inc., AR Technology  All rights reserved.
*******************************************************************************/
#ifndef _APP_MAIN_H_
#define _APP_MAIN_H_


//--------------------------------------------------------------------------------
// Example Apps
//  1   App_Ex1.c   Listen for a remote device connection, then echo back
//                  messages sent from the remote device.
//  2   App_Ex2.c   Automatically Connect and Disconnect
//--------------------------------------------------------------------------------
#define APP_EXAMPLE     2
//--------------------------------------------------------------------------------


/*****************************************************************************************
*  This define is used when an Apple CP chip v2.0B is connected and requires the low power
*  mode internal to the chip.
****************************************************************************************/
//#define APPLE_CP_SLEEP  // turn this on to force the Apple CP to sleep when not needed
#ifdef APPLE_CP_SLEEP
#define APPLE_CP_RESET_PIN 13
#define FORCE_SLEEP 5
#endif

/*****************************************************************************************
*  This define is used when an Apple CP chip is powered using a transistor switch
****************************************************************************************/
#define APPLE_CP_PWR_CONTROL
#ifdef APPLE_CP_PWR_CONTROL
#define APPLE_CP_PWR_PIN 6
#endif

#define TM_TMRID_APP_BASE   0xC000

enum {
    APP_TMRID_HELLO = TM_TMRID_APP_BASE,
    APP_TMRID_SEND_HCI_MESSAGE,
    APP_TMRID_DEBOUNCE,
    APP_TMRID_BOND,
    APP_TMRID_DISCOVERY,
    APP_TMRID_CONNECT_INTERVAL,
    APP_TMRID_DISCONNECT_INTERVAL,
    APP_TMRID_STARTED,
    APP_TMRID_MESSAGE_TEST,
    APP_TMRID_APPLE_CP_SLEEP_DELAY,
    APP_TMRID_APPLE_CP_PWR_DELAY
};

#define APP_DISCOVERY_INTERVAL    10000  // 10 seconds
#define APP_CONNECT_INTERVAL      10000  // 10 seconds
#define APP_MESSAGE_TEST_INTERVAL  8000  // 8 seconds
#define APP_APPLE_CP_SLEEP_DELAY  10000  // 10 seconds to allow authentication to complete
#define APP_APPLE_CP_PWR_DELAY    10000  // 10 seconds to allow authentication to complete


#define SIG_APP_STARTED  40001


enum {
    SIG_APP_HELLO = SIG_APP_BASE,
    SIG_APP_STARTUP_TIMEOUT,
    SIG_SEND_HCI_MESSAGE,
    SIG_APP_GPIO_INTERRUPT,
    SIG_APP_GPIO_DEBOUNCE,
    SIG_APP_ANOTHER,
    SIG_APP_MESSAGE,
    SIG_APP_BOND,
    SIG_APP_DISCOVERY_TIMEOUT,
    SIG_APP_CONNECT_INTERVAL_TIMEOUT,
    SIG_APP_DISCONNECT_INTERVAL_TIMEOUT,
    SIG_APP_MESSAGE_TEST,//SDK_MESSAGE_TEST
    SIG_APP_APPLE_SLEEP_TIMEOUT,
    SIG_APP_APPLE_PWR_TIMEOUT,
    SIG_APP_WAKEUP,
};

enum {
    SIG_SEND_IAP_DISCONNECT_REQ = SIG_CUSTOM_BASE,
    SIG_SEND_IAP_RECONNECT_REQ,
    SIG_SEND_DISCOVERY_REQ,
};



/************************************
*    Application Variable IDs       *
*************************************/
#define VAR_ID_SDK_BASE     0x5001  // Base ID for SDK
enum {
    VAR_ID_DeviceTable = VAR_ID_SDK_BASE,
    VAR_ID_SDK_END
};




//-------------------------------------
//   Types and structures
//-------------------------------------
#define GENERIC_SPP_PROFILE  0
#define LAN_ACCESS_PROFILE   1
#define DUN_PROFILE          2
#define FAX_PROFILE          3
#define IAP_PROFILE          4
#define AUDIO_SINK_PROFILE   5

#define  NON_IOS_DEVICE     0
#define  IOS_DEVICE         1
#define  MAX_DEVICE_RECORDS 10
#define  PHONE_NUMBER_LEN   10


typedef struct tApp_DeviceRecord {
    tBdAddr   DeviceAddress;
    uint8     PhoneNumber[PHONE_NUMBER_LEN];  // stored as ascii coded decimal digits
    uint8     DeviceType;
} tApp_DeviceRecord;

typedef struct tApp_DeviceTable {
    uint8              RecordsInUse;
    tApp_DeviceRecord  DeviceRecord[MAX_DEVICE_RECORDS];
} tApp_DeviceTable;

typedef struct tApp_HCI_Message {
    uint32  Transac;    // This field should exist for all messages
                        // but may be freely used or ignored.
    uint16 Opcode;      // HCI Opcode
    uint8  Length;      // HCI Param length
    uint8  Params[32];  // HCI Params
} tApp_HCI_Message;

typedef struct tSIG_Wakeup {
    uint32  Transac;    // This field should exist for all messages
                        // but may be freely used or ignored.
    uint32 OldPioState; // Old state of PIO lines
    uint32 NewPioState; // New state of PIO lines
} tSIG_Wakeup;


//-------------------------------------
// Prototypes
//-------------------------------------
void    APP_InitTask(void);
tBdAddr App_GetBdAddressFromHandle(uint16 Handle);


#endif // _APP_MAIN_H_

