/*******************************************************************************
* Project:      Amped'RF Bluetooth
* File name:    BTI.h
* Description:  BTI is an interface module to the upper layer stack
*
* Copyright (c) 2010 Amped'RF Inc., AR Technology  All rights reserved.
*******************************************************************************/
#ifndef _AMP_H_
#define _AMP_H_


//------------------------------------------------------------------------------
//  Defines
//------------------------------------------------------------------------------

// Result Codes
#define AMP_RESULT_DONE                         0x0000
#define AMP_RESULT_PENDING                      0x0001
#define AMP_RESULT_PARAMETER_ERROR              0x0002
#define AMP_RESULT_ILLEGAL_OPERATION            0x0003
#define AMP_RESULT_NO_CONNECTION                0x0004
#define AMP_RESULT_NO_SETUP                     0x0005
#define AMP_RESULT_LINE_ERROR                   0x0006
#define AMP_RESULT_NO_RESOURCES                 0x0007
#define AMP_RESULT_SERVICE_EXIST                0x0008
#define AMP_RESULT_PORT_EXIST                   0x0009
#define AMP_RESULT_CONNECTION_ERROR             0x000A
#define AMP_RESULT_CONNECTION_UP                0x000B
#define AMP_RESULT_CONNECTION_DOWN              0x000C
#define AMP_RESULT_CONNECTION_FAIL              0x000D
#define AMP_RESULT_INVALID_TASKID               0x000E
#define AMP_RESULT_SDP_REGISTER_ERROR           0x000F
#define AMP_RESULT_OPP_SERVER_REGISTER_ERROR    0x0010
#define AMP_RESULT_OPP_CLIENT_OPEN_ERROR        0x0011
#define AMP_RESULT_OPP_CLIENT_REGISTER_ERROR    0x0012
#define AMP_RESULT_SUCCESS                      0x0013
#define AMP_RESULT_FAIL                         0x0014
#define AMP_DATA_BUFFER_HEADER_CORRUPTED        0x0015
#define AMP_DATA_LENGTH_ERROR                   0x0016
#define AMP_MTU_LENGTH_ERROR                    0x0017
#define AMP_AUDIO_NOT_SUPPORTED                 0x0018
#define AMP_INVALID_PROFILE                     0x0019
#define AMP_SDAP_CANCEL_DEV_BROWSE              0x001a
#define AMP_RESULT_SDAP_BUSY                    0x001b
#define AMP_MODE_NOT_SUPPORTED                  0x001c
#define AMP_WRONG_BDADDR                        0x001d
#define AMP_RESULT_ILLEGAL_MODE                 0x001e
#define AMP_WRONG_MODE                          0x001f
#define AMP_RESULT_CONNECTION_REJECTED          0x0020

#define AMP_INVALID_HANDLE                      0xFFFF

// Modes
#define AMP_ACTIVE_MODE   0x00
#define AMP_HOLD_MODE     0x01
#define AMP_SNIFF_MODE    0x02
#define AMP_PARK_MODE     0x03
#define AMP_INVALID_MODE  0xff

// Setup Codes
#define	AMP_NON_DISCOVERABLE_MODE      0
#define	AMP_LIMITED_DISCOVERABLE_MODE  1
#define	AMP_GENERAL_DISCOVERABLE_MODE  2

#define	AMP_NON_PAIRABLE_MODE          0
#define	AMP_PAIRABLE_MODE              1

#define	AMP_NON_CONNECTABLE_MODE       0
#define	AMP_CONNECTABLE_MODE           1

#define AMP_SECURITY_MODE_1_NON        1
#define AMP_SECURITY_MODE_2_SERVICE    2
#define AMP_SECURITY_MODE_3_LINK       3
#define AMP_SECURITY_MODE_4            4

#define AMP_SECURITY_SERVICE_AUTHORIZE    0x01
#define AMP_SECURITY_SERVICE_AUTHENTICATE 0x02
#define AMP_SECURITY_SERVICE_ENCRYPT      0x04

#define AMP_SECURITY_FIXED_PIN_TYPE       1
#define AMP_SECURITY_VARIABLE_PIN_TYPE    0

#define AMP_SECURITY_UNIT_KEY_TYPE      0
#define AMP_SECURITY_COMB_KEY_TYPE      1

#define AMP_SECURITY_MIN_ENCRYPTION_KEY_SIZE      0x01
#define AMP_SECURITY_MAX_ENCRYPTION_KEY_SIZE      0x10

// Inquiry
#define AMP_NULL_COD     0
#define AMP_NULL_BDADDR  AMP_NullBdAddr


//Special Task Id
#define AMP_SPECIAL_TASK_ID           0xfffe

#define AMP_Log(Id,Text,pData,DataSize) TS_API_Log(Id, Text, pData, (uint8)DataSize)


#define AMP_SDAP_COD_FILTER_ALL			SDAP_COD_FILTER_ALL	/* Select all devices regardless of
												CoD */
#define AMP_SDAP_COD_FILTER_SVC			SDAP_COD_FILTER_SVC	/* Select devices filtering on the
												Major Service Class */
#define AMP_SDAP_COD_FILTER_MAJ			SDAP_COD_FILTER_MAJ	/* Select devices filtering on
												Major Device Class */
#define AMP_SDAP_COD_FILTER_MAJ_MIN		SDAP_COD_FILTER_MAJ_MIN	/* Select devices filtering on
												Major and Minor Device
												Classes */
#define AMP_SDAP_COD_FILTER_SVC_MAJ		SDAP_COD_FILTER_SVC_MAJ	/* Select devices filtering on
												Major Service Class and Major
												Device Class */
#define AMP_SDAP_COD_FILTER_SVC_MAJ_MIN	SDAP_COD_FILTER_SVC_MAJ_MIN	/* Select devices filtering on
												Major Service Class, Major
												Device Class and Minor Device
												Class */
#define AMP_SDAP_MAX_DEVICES      12

#define AMP_SPP_DATA_HEADER_SIZE 9  // RF COMM(5) + L2CAP(4)


#define AMP_SPP_DATA_MTU_SIZE    (MIN(SDC_LM_DATA_BODY_SIZE,SDC_TO_LM_DATA_BUFFER_SIZE)-\
                                  AMP_SPP_DATA_HEADER_SIZE-3)   // 2 bytes for RFCOMM FCS byte
                                                                // +1 for fudge

//------------------------------------------------------------------------------
//  Types
//------------------------------------------------------------------------------
extern const tBdAddr AMP_NullBdAddr;


typedef struct tRequest1 {
  tOS_TaskId TaskId;
  uint32     Transac;
} tRequest1;

typedef struct tRequest2 {
  tOS_TaskId  TaskId;
  tpOS_Signal Signal;
} tRequest2;


typedef struct tSdapCB {
  uint32                    PendingTransacId;
  tOS_TaskId                SignallingTaskId;
  tOS_TaskId                DataTaskId;
  uint32                    DevBrowseHandle;
  uint32                    NumDevices;
  tRemDev                   DevBrowseResults[AMP_SDAP_MAX_DEVICES];
  uint32                    CurrentDevIndex;
} tSdapCB;

typedef struct tSppCB {
  uint32              PendingTransacId;
  tOS_TaskId          SignallingTaskId;
  tOS_TaskId          DataTaskId;
} tSppCB;

typedef struct tGapCB {
  uint32              PendingTransacId;
  tOS_TaskId          SignallingTaskId;
} tGapCB;

typedef struct tGapSecurityCB {
  tOS_TaskId          SecurityTaskId;
  uint8               PairableMode;
  uint8               SecurityMode;
} tGapSecurityCB;

typedef struct tAMP_Data {
  bool           Started;
  tOS_TaskId     SetupTaskId;
  tRequest1      InquiryRequest;
  tRequest1      PeriodicInquiryRequest;
  tRequest2      RemoteNameRequest;
  tGapCB         GapCB;
  tSppCB         SppCB;
  tSdapCB        SdapCB;
  tGapSecurityCB GapSecurityCB;
  tpOS_MemPool   DataBufPool;
  tpOS_MemPool   TinyBufPool;
  tpOS_MemPool   ShortBufPool;
  tpOS_MemPool   LongBufPool;
} tAMP_Data;


typedef struct tAMP_ChangeConnectionPacketType {
  uint16    Connection_Handle;
  uint16    Packet_Type;
} tAMP_ChangeConnectionPacketType;


//------------------------------------------------------------------------------
//  Public Variables
//------------------------------------------------------------------------------
extern tAMP_Data AMP_Data;  //bcolias

#define CheckMemCorruption()    //CheckMem()
void CheckMem(void);

//------------------------------------------------------------------------------
//  Public Routines
//------------------------------------------------------------------------------
bool   AMP_EqualBdAddr(const tBdAddr *pBdAddr1, const tBdAddr *pBdAddr2);
void   AMP_ReleaseDataBufFromBackPlan(tpOS_MemPool pMemPool);
uint16 AMP_FirstHandle(void);
uint16 AMP_NextHandle(void);
//void   AMP_SetLatency(uint16 Handle, uint16 Latency, tCmdCompleteCallback Callback);

//------------------------------------------------------------------------------
void AMP_PacketType(tOS_Signal *pSignal);



//------------------------------------------------------------------------------
//--- Init Routines ------------------------------------------------------------
//------------------------------------------------------------------------------
void AMP_InitModule(void);
void AMP_ReleaseDataBuffer(tBufferHeader* pHdrBuf);
tBufferHeader *AMP_AllocateGeneralBuffer(uint16 DataSize);

uint32 AMP_API_Gap_Set_Local_Device_Name(char *pName);
uint32 AMP_API_Gap_Set_Pairable_Mode(uint8 PairableMode);

void BdAddrArrayToStruct(tBdAddr *pBdAddr, uint8 BdAddrArray[6]);
void BdAddrStructToArray(uint8 BdAddrArray[6], tBdAddr *pBdAddr);


//------------------------------------------------------------------------------
// GAP prototypes
//------------------------------------------------------------------------------
void Coduint32toArray(uint8 CodArray[3], tCod Cod);
tCod CodArrayToUint32(uint8 CodArray[3]);
void BdAddrStructToArray(uint8 BdAddrArray[6], tBdAddr *pBdAddr);
void BdAddrArrayToStruct(tBdAddr *pBdAddr, uint8 BdAddrArray[6]);
void InitGAP(void);
void AMP_GapReceiveSignal(tOS_Signal *pSignal);
void AMP_GapReadBDAddress(tBdAddr *pBdAddr);
tDeviceDBInformation *ReadRecordInDeviceDBTable(tBdAddr *pBdAddr);


//------------------------------------------------------------------------------
// SDAP prototypes
//------------------------------------------------------------------------------
void InitSDAP(void);
void AMP_SdapReceiveSignal(tOS_Signal *pSignal);


//------------------------------------------------------------------------------
// SPP prototypes
//------------------------------------------------------------------------------
void InitSPP(void);
void AMP_SppReceiveSignal(tOS_Signal *pSignal);
uint32 AMP_API_Spp_Setup(tSIG_AMPSppSetup *pBtiSppSetup);
tBufferHeader *AMP_AllocateSppDataBuffer(uint16 DataSize);




#endif //AMP_H
//------------------------------------------------------------------------------
// End of BTI.h
