/*******************************************************************************
* Project:      Amped'RF Bluetooth
* File name:    App_Ex1.c
* Description:  SDK Example 1
*
* Copyright (c) 2016 Amped'RF Technology Inc, All rights reserved.
*******************************************************************************/
#ifdef SDK
#include "SDK_Common.h"
#include "App_Ex.h"
#if APP_EXAMPLE == 1


// Defines
#define STRMAX 128


/*******************************************************************************
// Structures & Defines
*******************************************************************************/
typedef struct {
    bool    Connected;
    uint16  PortHandle;
    tBdAddr BdAddr;
} tRemoteDevice;



/*******************************************************************************
//   Globals
*******************************************************************************/
tRemoteDevice   Remote={0};



/*******************************************************************************
* SDK_ReceiveSignal
* This handles signals for AT and the SDK application. All AT signals are first
* sent here. If they are ignored the signals are passed to the AT layer. If
* RetCode is zero, the signal will be handled by AT. If it is non-zero, further
* processing will be ignored by AT.
********************************************************************************/
int SDK_ReceiveSignal(tOS_Signal *pSignal)
{
    int  RetCode=SIGNAL_UNHANDLED;
    uint16 SigNo = OS_GetSignalNo(pSignal);
    tSIG_AMPSppRecv         * pAMPSppRecv;
    tSIG_AMPSppConnectCon   * pSppConnect;
    char *p, msg[STRMAX+16];
    int  len;

    switch (SigNo)
    {
    case SIG_AT_DEVICE_UP:
        sprintf(msg, "Amped RF DevKit Version %s %s [App_Ex%d.c]\r\n",
                SDK_VERSION, SDK_BUILD, APP_EXAMPLE);
        GKI_putstring(msg);
        APP_InitTask();
        OS_TimerRegisterRequest(0, APP_TMRID_STARTED, 2000, SIG_APP_STARTUP_TIMEOUT);
        break;

    case SIG_APP_STARTUP_TIMEOUT:
        TM_print("Timeout\r\n");
        TM_printflush();
        RetCode = SIGNAL_HANDLED;
        break;

    case SIG_AMP_SPP_RECV:
        pAMPSppRecv = (tSIG_AMPSppRecv*)OS_GetSignalDataPtr(pSignal);
        TM_print("BytesRead: %2d [", pAMPSppRecv->BytesRead);
        for (int i=0; i<pAMPSppRecv->BytesRead; i++) {
            int ch = pAMPSppRecv->pDataHdr->pData[i];
            TM_print("%c", (ch >=' ' && ch<='~') ? ch : '.');
        }
        OS_Release(pAMPSppRecv->pDataHdr);
        TM_printlf("]");
        TM_printflush();
        //==================================================
        len = MIN(STRMAX, pAMPSppRecv->BytesRead);
        p = msg;
        strcpy(msg, "Received: ");
        p += strlen(p);
        strncpy(p, (char *)pAMPSppRecv->pDataHdr->pData, len);
        p += strlen(p);
        *p = 0;
        OS_SendDataToHandle(Remote.PortHandle, (uint8 *)msg, strlen(msg));
        RetCode = SIGNAL_HANDLED;
        break;

    case SIG_AMP_SPP_LISTEN_CON:
    case SIG_AMP_SPP_CONNECT_CON:
        pSppConnect       = (tSIG_AMPSppConnectCon*)OS_GetSignalDataPtr(pSignal);
        Remote.Connected  = true;
        Remote.PortHandle = pSppConnect->Handle;
        Remote.BdAddr     = pSppConnect->BdAddr;
        break;

    case SIG_AMP_SPP_DISCONNECT_IND:
    case SIG_AMP_SPP_DISCONNECT_CON:
    case SIG_AMP_GAP_LL_CONN_DOWN:
        Remote.Connected    = false;
        break;

    default:
        //sprintf(msg, "Signal %s\r\n", SDK_GetSignalName(SigNo));
        //GKI_putstring(msg);
        break;
    }


    //---------------------------------------------------------------
    // RetCode == 0 Unhandled Signal
    // RetCode == 1 Signal was handled in this function
    //---------------------------------------------------------------
    if (RetCode==SIGNAL_HANDLED) {
        OS_ReleaseSignal(pSignal);
    }
    return RetCode;
}




/*******************************************************************************
* Function Name  : APP_InitTask
* Description    : Initialize App layer
********************************************************************************/
void APP_InitTask(void)
{
    // Initialize your variables here
}



#endif // APP_EXAMPLE
#endif // SDK
