DOTA using WIP


#1

Hi Everybody.
Here is a Sample of DOTA with the WIP stack
Hope This is useful
I have placed alot of TRACES and comments in the code.
Please do not e-mail me for support on this code.
Ask your questions on this forum
Best regards
Walter

/*
Created by Walter Senekal
					 walter@senekal.net
This code is provided Royalty free
and AS-IS
This code is only for evaluation purpose
and not for production.
PLEASE DO NOT E-MAIL FOR SUPPORT ON 
THIS CODE.I WILL NOT ANSWER THEM
Please ask your questions on the FORUM
(I provided my e-mail if somebody 
wants to discuss some bugs or suggestions on the code)					 
*/
#include "adl_global.h"
#include "wip.h"

#if __OAT_API_VERSION__ = 400
  const u16 wm_apmCustomStackSize = 4096;
#else 
  u32 wm_apmCustomStack[1024];
  const u16 wm_apmCustomStackSize = sizeof(wm_apmCustomStack);
#endif 
/* Variables */
u8 ftp_State = 0;
u8 GPRS_State = 0;
s32 DOTA_Cell_Handle = 0;
s32 ADEventHandle = 0;
ascii SimPin[4];
static wip_bearer_t gprs_bearer = NULL;
wip_channel_t ftp_WipHandle, ftp_Data_Channel;
ascii dotafile[64];
//TRACE LEVELS
#define lvl_Common	9
#define lvl_Sim		10
#define lvl_WIP		11
#define lvl_AD		12

// Destination address
#define STR_SIZE        64   // Common string size
#define STR_SIZE_SRV    128  // Server parameters string size
struct
{
  u16 Port; // FTP server port
  u8 Mode; // FTP server mode (active/passive)
  ascii Type; // FTP server type (binary/ascii)
  ascii Server[STR_SIZE_SRV]; // FTP server address
  ascii Login[STR_SIZE]; // FTP account login
  ascii Password[STR_SIZE]; // FTP account password
  ascii FileName1[STR_SIZE_SRV]; // FTP server file name
  ascii FileName2[STR_SIZE_SRV]; // FTP server file name
} ftp_DestAddr;

struct
{
  ascii APN_Name[STR_SIZE];
  ascii Username[STR_SIZE];
  ascii Password[STR_SIZE];
} APN_Details;

void adl_main(adl_InitType_e InitType);
void initvariables();
void Sim_Handler(u8 Event);
void GPRS_Attach();
static void gprs_bearer_handler(wip_bearer_t br, s8 event, void *ctx);
void ConnectFTP();
void ftp_SessionHandler(wip_event_t *ev, void *ctx);
void ftp_DataHandler(wip_event_t *ev, void *ctx);
//void ftp_Write(void);
void ftp_Read(void);
//void FTPGetMyFile(wip_event_t *ev, void *ctx);
void ADEvHandler(adl_adEvent_e Event, u32 Progress);
void DotaCellHandleReturnValue();
void CmdDOTAHandler1(adl_atCmdPreParser_t *params);
void CmdDOTAHandler2(adl_atCmdPreParser_t *params);
void CmdFORMATHandler(adl_atCmdPreParser_t *params);
void CmdINSTALLHandler(adl_atCmdPreParser_t *params);
void CmdCOMPACTHandler(adl_atCmdPreParser_t *params);
/***************************************************************************/
/*  Local functions                                                        */
/***************************************************************************/
void adl_main(adl_InitType_e InitType)
{
  s8 S8RetVal = 0;
  s32 s32RetVal = 0;
  TRACE((lvl_Common, "adl_main :Start InitType: %d", InitType));
  GPRS_State = 0;
  //This Check is only for RTE because it does not always return the correct InitType
  if (InitType < 0)
  {
    InitType = 0;
  }
  // Initialize TCP/IP stack
  S8RetVal = wip_netInit();
  TRACE((lvl_AD, "wip_netInit Ret= %d", S8RetVal));
  // Initialize My Variables
  initvariables();
  //Subscribe To Sim card For Notifications
  s32RetVal = adl_simSubscribe(Sim_Handler, SimPin);
  TRACE((lvl_Sim, "adl_simSubscribe Ret= %d", s32RetVal));
  //Subscribe To A&D Events
  ADEventHandle = adl_adEventSubscribe(ADEvHandler);





  TRACE((lvl_AD, "adl_adEventSubscribe Ret= %d", ADEventHandle));
  adl_atCmdSubscribe("AT+DOTA1", CmdDOTAHandler1, ADL_CMD_TYPE_ACT);
  adl_atCmdSubscribe("AT+DOTA2", CmdDOTAHandler2, ADL_CMD_TYPE_ACT);
  adl_atCmdSubscribe("AT+FORMAT", CmdFORMATHandler, ADL_CMD_TYPE_ACT);
  adl_atCmdSubscribe("AT+INSTALL", CmdINSTALLHandler, ADL_CMD_TYPE_ACT);
  adl_atCmdSubscribe("AT+COMPACT", CmdCOMPACTHandler, ADL_CMD_TYPE_ACT);
  adl_atSendResponse(ADL_AT_RSP, "\r\nApp Version:2\r\n");
  switch (InitType)
  {
    case ADL_INIT_POWER_ON:
      // Normal power on
      TRACE((lvl_Common, "ADL_INIT_POWER_ON"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nApp Start Normally\r\n");
      adl_atSendResponse(ADL_AT_RSP, "\r\nPlease Wait For Sim To Init...\r\n");
      //Create a Cell For The Dota File
      DOTA_Cell_Handle = adl_adSubscribe(0, ADL_AD_SIZE_UNDEF);
      TRACE((lvl_AD, "DOTA_Cell_Subscribe Ret= %d", DOTA_Cell_Handle));
      if (DOTA_Cell_Handle < 0)
      {
        //If Cell Is Not Subscribed WHY???
        DotaCellHandleReturnValue();
      }
      break;
    case ADL_INIT_REBOOT_FROM_EXCEPTION:
      // Reboot after an embedded application exception
      TRACE((lvl_Common, "ADL_INIT_REBOOT_FROM_EXCEPTION"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nModem Start After Exception\r\n");
      break;
    case ADL_INIT_DOWNLOAD_SUCCESS:
      // Reboot after a successful download process
      TRACE((lvl_Common, "ADL_INIT_DOWNLOAD_SUCCESS"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nModem Start After DOTA OK\r\n");
      //Format A&D to clear Dota File
      s32RetVal = adl_adFormat(ADEventHandle);
      TRACE((lvl_AD, "adl_adFormat Ret= %d", s32RetVal));
      break;
    case ADL_INIT_DOWNLOAD_ERROR:
      // Reboot after a Failed download process
      TRACE((lvl_Common, "ADL_INIT_DOWNLOAD_ERROR"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nModem Start After DOTA KO\r\n");
      //Format A&D to clear Dota File
      ////s32RetVal = adl_adFormat(ADEventHandle);
      ////TRACE((lvl_AD, "adl_adFormat Ret= %d", s32RetVal));
      break;
  }
  TRACE((lvl_Common, "adl_main :Exit"));
}

void DotaCellHandleReturnValue()
{
  //TODO: Handle Cell Subscribe Failures
  TRACE((lvl_AD, "DotaCellHandleReturnValue :Start"));
  switch (DOTA_Cell_Handle)
  {
    case ADL_RET_ERR_ALREADY_SUBSCRIBED:
      TRACE((lvl_AD, "ADL_RET_ERR_ALREADY_SUBSCRIBED"));
      //if the cell is already subscribed
      break;
    case ADL_RET_ERR_PARAM:
      //on parameter error
      TRACE((lvl_AD, "ADL_RET_ERR_PARAM"));
      break;
    case ADL_RET_ERR_BAD_STATE:
      //if an undefined size cell is already subscribed and not finalized
      TRACE((lvl_AD, "ADL_RET_ERR_BAD_STATE"));
      break;
    case ADL_AD_RET_ERR_NOT_AVAILABLE:
      //if A&D space is not available
      TRACE((lvl_AD, "ADL_AD_RET_ERR_NOT_AVAILABLE"));
      break;
    case ADL_AD_RET_ERR_OVERFLOW:
      //if there is not enough space for allocation
      TRACE((lvl_AD, "ADL_AD_RET_ERR_OVERFLOW"));
      break;
    case ADL_RET_ERR_SERVICE_LOCKED:
      //if called from a low level interruption handler
      TRACE((lvl_AD, "ADL_RET_ERR_SERVICE_LOCKED"));
      break;
  }
  TRACE((lvl_AD, "DotaCellHandleReturnValue :Exit"));
}

void ADEvHandler(adl_adEvent_e Event, u32 Progress)
{
  TRACE((lvl_AD, "ADEvHandler :Start Event: %d", Event));
  switch (Event)
  {
    case ADL_AD_EVENT_FORMAT_INIT:
      // Format process initialization
      TRACE((lvl_AD, "ADL_AD_EVENT_FORMAT_INIT"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nFormat Starting...\r\n");
      break;
    case ADL_AD_EVENT_FORMAT_PROGRESS:
      // Format process progress
      TRACE((lvl_AD, "ADL_AD_EVENT_FORMAT_PROGRESS"));
      break;
    case ADL_AD_EVENT_FORMAT_DONE:
      // Format process Done
      TRACE((lvl_AD, "ADL_AD_EVENT_FORMAT_DONE"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nFormat Completed\r\n");
      //A&D Is Empty Create your A&D objects
      //NOTE! if you create undefined size then you can only create one
      DOTA_Cell_Handle = adl_adSubscribe(0, ADL_AD_SIZE_UNDEF);
      TRACE((lvl_AD, "DOTA_Cell_Handle=%d", DOTA_Cell_Handle));
      if (DOTA_Cell_Handle < 0)
      {
        //If Cell Is Not Subscribed WHY???
        DotaCellHandleReturnValue();
      }
      break;
    case ADL_AD_EVENT_RECOMPACT_INIT:
      // Re-compaction process initialization
      TRACE((lvl_AD, "ADL_AD_EVENT_RECOMPACT_INIT"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nRecompact Starting...\r\n");
      break;
    case ADL_AD_EVENT_RECOMPACT_PROGRESS:
      // Re-compaction process progress
      TRACE((lvl_AD, "ADL_AD_EVENT_RECOMPACT_PROGRESS"));
      break;
    case ADL_AD_EVENT_RECOMPACT_DONE:
      // Re-compaction process Done
      TRACE((lvl_AD, "ADL_AD_EVENT_RECOMPACT_DONE"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nRecompact Completed\r\n");
      DOTA_Cell_Handle = adl_adSubscribe(0, ADL_AD_SIZE_UNDEF);
      TRACE((lvl_AD, "DOTA_Cell_Handle=%d", DOTA_Cell_Handle));
      if (DOTA_Cell_Handle < 0)
      {
        //If Cell Is Not Subscribed WHY???
        DotaCellHandleReturnValue();
      }
      break;
    case ADL_AD_EVENT_INSTALL:
      TRACE((lvl_AD, "ADL_AD_EVENT_INSTALL"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nInstall AD\r\n");
      break;
  }
  TRACE((lvl_AD, "ADEvHandler :Exit"));
}

void Sim_Handler(u8 Event)
{
  TRACE((lvl_Sim, "Sim_Handler :Start with Event: %d", Event));
  switch (Event)
  {
    case ADL_SIM_EVENT_FULL_INIT:
      //When Sim is fully Init then Continue
      adl_atSendResponse(ADL_AT_RSP, "\r\nSim Init Complete\r\n");
      GPRS_Attach();
      break;
    case ADL_SIM_EVENT_PIN_WAIT:
      TRACE((lvl_Sim, "ADL_SIM_EVENT_PIN_WAIT"));
      break;
    case ADL_SIM_EVENT_PIN_OK:
      TRACE((lvl_Sim, "ADL_SIM_EVENT_PIN_OK"));
      break;
    case ADL_SIM_EVENT_INSERTED:
      TRACE((lvl_Sim, "ADL_SIM_EVENT_INSERTED"));
      break;
    case ADL_SIM_EVENT_REMOVED:
      TRACE((lvl_Sim, "ADL_SIM_EVENT_REMOVED"));
      break;
    case ADL_SIM_EVENT_PIN_ERROR:
      TRACE((lvl_Sim, "ADL_SIM_EVENT_PIN_ERROR"));
      break;
    case ADL_SIM_EVENT_PIN_NO_ATTEMPT:
      TRACE((lvl_Sim, "ADL_SIM_EVENT_PIN_NO_ATTEMPT"));
      break;
    case ADL_SIM_EVENT_LAST:
      TRACE((lvl_Sim, "ADL_SIM_EVENT_LAST"));
      break;
  }
  TRACE((lvl_Sim, "Sim_Handler :Exit"));
}

void GPRS_Attach()
{
  s8 s8RetVal;
  TRACE((lvl_WIP, "GPRS_Attach :Start"));
  adl_atSendResponse(ADL_AT_RSP, "\r\nConnectiong To GPRS...\r\n");
  //Create a Bearer e.g GPRS,CSD Call,UART
  s8RetVal = wip_bearerOpen(&gprs_bearer, "GPRS", gprs_bearer_handler, NULL);
  TRACE((lvl_WIP, "wip_bearerOpen Retval: %d", s8RetVal));
  //Provide GPRS(in this case)Login Details
  s8RetVal = wip_bearerSetOpts(gprs_bearer, WIP_BOPT_GPRS_APN,
    APN_Details.APN_Name, WIP_BOPT_LOGIN, APN_Details.Username,
    WIP_BOPT_PASSWORD, APN_Details.Password, WIP_BOPT_END);
  TRACE((lvl_WIP, "wip_bearerSetOpts Retval: %d", s8RetVal));
  //Connect the Bearer(GPRS Attach)
  s8RetVal = wip_bearerStart(gprs_bearer);
  TRACE((lvl_WIP, "wip_bearerStart Retval: %d", s8RetVal));
  TRACE((lvl_WIP, "GPRS_Attach :Exit"));
}

static void gprs_bearer_handler(wip_bearer_t br, s8 event, void *ctx)
{
  ascii RspStr[128];
  ascii ipStr[16], dns1Str[16], dns2Str[16];
  wip_in_addr_t ip, dns1, dns2;
  TRACE((lvl_WIP, "gprs_bearer_handler :Start with Event: %d", event));
  //Make Sure Correct Bearer is connecting
  if (br != gprs_bearer)
    return ;
  switch (event)
  {
    case WIP_BEV_IP_CONNECTED:
      //Get Bearer Info
      TRACE((lvl_WIP, "WIP_BEV_IP_CONNECTED"));
      wip_bearerGetOpts(br, WIP_BOPT_IP_ADDR, &ip, WIP_BOPT_IP_DNS1, &dns1,
        WIP_BOPT_IP_DNS2, &dns2, WIP_BOPT_END);
      wip_inet_ntoa(ip, ipStr, sizeof(ipStr));
      wip_inet_ntoa(dns1, dns1Str, sizeof(dns1Str));
      wip_inet_ntoa(dns2, dns2Str, sizeof(dns2Str));
      wm_sprintf(RspStr, "\r\nGPRS: CONNECTED IP=%s DNS1=%s DNS2=%s\r\n", ipStr,
        dns1Str, dns2Str);
      TRACE((lvl_WIP, RspStr));
      adl_atSendResponse(ADL_AT_UNS, RspStr);
      adl_atSendResponse(ADL_AT_RSP, "\r\nGPRS Connect Ready For DOTA\r\n");
      GPRS_State = 1;

      break;
    case WIP_BEV_IP_DISCONNECTED:
      TRACE((lvl_WIP, "WIP_BEV_IP_DISCONNECTED"));
      adl_atSendResponse(ADL_AT_UNS, "\r\n+WIPSTART: DISCONNECTED\r\n");
      GPRS_State = 0;
      //ReAttach
      GPRS_Attach();
      break;
    case WIP_BEV_STOPPED:
      TRACE((lvl_WIP, "WIP_BEV_STOPPED"));
      wip_bearerClose(br);
      GPRS_State = 0;
      gprs_bearer = NULL;
      //ReAttach
      GPRS_Attach();
      break;
    case WIP_BEV_CONN_FAILED:
      TRACE((lvl_WIP, "WIP_BEV_CONN_FAILED"));
      adl_atSendResponse(ADL_AT_UNS, "\r\n+WIPSTART: FAILED\r\n");
      wip_bearerClose(br);
      gprs_bearer = NULL;
      //ReAttach
      GPRS_State = 0;
      GPRS_Attach();
      break;
  }
  TRACE((lvl_WIP, "gprs_bearer_handler :Exit"));
}

void ConnectFTP()
{
  TRACE((lvl_WIP, "ConnectFTP :Start"));
  // Check for current state
  if (!ftp_State)
  {
    // Get parameters
    s8 sReturn = OK;
    //ftp_Mode = FTP_MODE_GET;
    //		adl_atSendResponse( ADL_AT_RSP,"\r\nEnter ConnectFTP\r\n");  
    ftp_WipHandle = wip_FTPCreateOpts(ftp_DestAddr.Server, ftp_SessionHandler,
      NULL, WIP_COPT_USER, ftp_DestAddr.Login, WIP_COPT_PASSWORD,
      ftp_DestAddr.Password, WIP_COPT_PASSIVE, ftp_DestAddr.Mode, WIP_COPT_TYPE,
      ftp_DestAddr.Type, WIP_COPT_PEER_PORT, ftp_DestAddr.Port, WIP_COPT_END);
    if (ftp_WipHandle == NULL)
    {
      // FTP error
      TRACE((lvl_WIP, "FTP Has Not Started"));
      adl_atSendStdResponseExt(ADL_AT_RSP, ADL_STR_CME_ERROR, 3);
      ftp_State = 0;
    }
    else
    {
      // FTP started
      TRACE((lvl_WIP, "FTP Has Started Ok"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nFTP STARTED OK\r\n");
      ftp_State = 1;
    }
  }
  else
  {
    // Already running
    adl_atSendStdResponseExt(ADL_AT_RSP, ADL_STR_CME_ERROR, 515);
  }
  TRACE((lvl_WIP, "ConnectFTP :Exit"));
}

void ftp_SessionHandler(wip_event_t *ev, void *ctx)
{
  TRACE((lvl_WIP, "ftp_SessionHandler :Start with Event %d", ev->kind));
  switch (ev->kind)
  {
    case WIP_CEV_OPEN:
      //Download The Dota File
      //TODO Make sure Cell is empty first
      TRACE((lvl_WIP, "WIP_CEV_OPEN"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nDownloading New App\r\n");
      ftp_Data_Channel = wip_getFile(ftp_WipHandle, dotafile, ftp_DataHandler,
        NULL);
      break;
    case WIP_CEV_DONE:
      //Called if you close the Connection
      TRACE((lvl_WIP, "WIP_CEV_DONE"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nFTP Connection Closed by APP\r\n");
      break;
    case WIP_CEV_PEER_CLOSE:
      //Called if Remote Connection Closes the Connection
      TRACE((lvl_WIP, "WIP_CEV_PEER_CLOSE"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nFTP Connection Closed by PEER\r\n");
    case WIP_CEV_ERROR:
      // End of transfer
      TRACE((lvl_WIP, "WIP_CEV_ERROR"));
      adl_atSendResponse(ADL_AT_RSP, "\r\nFTP Connection Closed \r\n");
      ftp_State = 0;
      //Close the FTP Connection
      wip_close(ftp_WipHandle);
      ftp_WipHandle = NULL;
      break;
  }
  TRACE((lvl_WIP, "ftp_SessionHandler :Exit"));
}

void ftp_DataHandler(wip_event_t *ev, void *ctx)
{
  s32 s32RetVal = 0;
  TRACE((lvl_WIP, "ftp_DataHandler :Start with Event %d", ev->kind));
  wip_debug("FTP data event: %d\n", ev->kind);
  switch (ev->kind)
  {
    case WIP_CEV_OPEN:
      TRACE((lvl_WIP, "WIP_CEV_OPEN"));
      break;
    case WIP_CEV_READ:
      TRACE((lvl_WIP, "WIP_CEV_READ"));
      // Read FTP data
      adl_atSendResponse(ADL_AT_RSP, "$");
      ftp_Read();
      break;
    case WIP_CEV_WRITE:
      TRACE((lvl_WIP, "WIP_CEV_WRITE"));
      // Write FTP Data
      break;
    case WIP_CEV_PEER_CLOSE:
      TRACE((lvl_WIP, "WIP_CEV_PEER_CLOSE"));
      //all Data Downloaded
      adl_atSendResponse(ADL_AT_RSP, "\r\nFTP Download Completed\r\n");
      //Finalize Cell with Undefined Size before you can use it
      s32RetVal = adl_adFinalise(DOTA_Cell_Handle);
      TRACE((lvl_WIP, "adl_adFinalise return Code %d", s32RetVal));

    case WIP_CEV_ERROR:
      TRACE((lvl_WIP, "WIP_CEV_ERROR"));
      // Close data channel
      wip_close(ftp_Data_Channel);
      // Close session channel
      TRACE((lvl_WIP, "ErrorNumber: %d", ev->content.error.errnum));
      if (ev->content.error.errnum == WIP_CERR_ABORTED)
      {
        TRACE((1, "ftp download aborted"));
        adl_atSendResponse(ADL_AT_RSP, "\r\nFTP Download Aborted\r\n");
      }
      if (ftp_WipHandle != NULL)
      {
        wip_close(ftp_WipHandle);
        ftp_WipHandle = NULL;
      }
      break;
  }
  TRACE((lvl_WIP, "ftp_DataHandler :Exit"));
}

void ftp_Read(void)
{
  int len = 0x00;
  u8 buf[256] = 
  {
    NULL
  };
  s32 RetVal32 = 0x00;
  TRACE((lvl_WIP, "ftp_Read :Start"));
  TRACE((1, "Read Data From FTP"));
  //Keep reading until you read 0
  while ((len = wip_read(ftp_Data_Channel, buf, sizeof(buf))) > 0)
  {
    //Write Data To A&D
    TRACE((lvl_WIP, "Bytes Read:%d", len));

    //New Data is added to end of Current data in Cell
    RetVal32 = adl_adWrite(DOTA_Cell_Handle, len, (void*)buf);
    TRACE((lvl_WIP, "adl_adWrite RetCode:%d", RetVal32));
    if (RetVal32 != 0)
    {
      adl_atSendResponse(ADL_AT_RSP, 
        "\r\nSomething is wrong with A&D Space,\r\nPlease format and Retry\r\n")
        ;
      wip_close(ftp_WipHandle);
    }
  }
  TRACE((lvl_WIP, "ftp_Read :Exit"));
}

void CmdDOTAHandler1(adl_atCmdPreParser_t *params)
{
  TRACE((lvl_Common, "CmdDOTAHandler1 :Start With Event %d", params->Type));
  if (GPRS_State == 1)
  {
    //Connect To FTP To Download New Application

    wm_sprintf(dotafile, ftp_DestAddr.FileName1);
    adl_atSendResponse(ADL_AT_RSP, "\r\nOK\r\n");
    ConnectFTP();
  }
  else
  {
    //Connect To GPRS First
    adl_atSendResponse(ADL_AT_RSP, "\r\nFirst Connecting to GPRS\r\n");
    adl_atSendResponse(ADL_AT_RSP, "Please Wait Before Retry\r\n");
    adl_atSendResponse(ADL_AT_RSP, "Error\r\n");
    GPRS_Attach();
  }
  TRACE((lvl_Common, "CmdDOTAHandler1 :Exit"));
}

void CmdDOTAHandler2(adl_atCmdPreParser_t *params)
{
  TRACE((lvl_Common, "CmdDOTAHandler2 :Start With Event %d", params->Type));
  if (GPRS_State == 1)
  {
    //Connect To FTP To Download New Application
    wm_sprintf(dotafile, ftp_DestAddr.FileName2);
    adl_atSendResponse(ADL_AT_RSP, "\r\nOK\r\n");
    ConnectFTP();
  }
  else
  {
    //Connect To GPRS First
    adl_atSendResponse(ADL_AT_RSP, "\r\nFirst Connecting to GPRS\r\n");
    adl_atSendResponse(ADL_AT_RSP, "Please Wait Before Retry\r\n");
    adl_atSendResponse(ADL_AT_RSP, "Error\r\n");
    GPRS_Attach();
  }
  TRACE((lvl_Common, "CmdDOTAHandler2 :Exit"));
}

void CmdFORMATHandler(adl_atCmdPreParser_t *params)
{
  s32 s32RetVal;
  TRACE((lvl_Common, "CmdFORMATHandler :Start With Event %d", params->Type));
  //Format A&D to clear Dota File
  adl_adFinalise(DOTA_Cell_Handle);
  adl_adUnsubscribe(DOTA_Cell_Handle);
  s32RetVal = adl_adFormat(ADEventHandle);
  TRACE((lvl_AD, "adl_adFormat Ret= %d", s32RetVal));
  if (s32RetVal == 0)
  {
    adl_atSendResponse(ADL_AT_RSP, "\r\nOK\r\n");
  }
  else
  {
    adl_atSendResponse(ADL_AT_RSP, "Error");
  }
  //  OK                                             */
  //ADL_RET_ERR_UNKNOWN_HDL //if the handle is unknown           */
  //ADL_RET_ERR_NOT_SUBSCRIBED //if the service is not subscribed*/
  //ADL_AD_RET_ERR_NOT_AVAILABLE //if A&D space is not available */
  //ADL_RET_ERR_BAD_STATE //if there is at least one currently   */
  //subscribed cell or if a recompact/format process is running */
  //ADL_RET_ERR_SERVICE_LOCKED //if called from a low level
  TRACE((lvl_Common, "CmdFORMATHandler :Exit"));
}

void CmdINSTALLHandler(adl_atCmdPreParser_t *params)
{
  s32 s32RetVal;
  TRACE((lvl_Common, "CmdINSTALLHandler :Start With Event %d", params->Type));
  //Install The New Application
  s32RetVal = adl_adInstall(DOTA_Cell_Handle);
  if (s32RetVal != OK)
  {
    adl_atSendResponse(ADL_AT_RSP, "Error\r\n");
  }
  else
  {
    adl_atSendResponse(ADL_AT_RSP, "OK\r\n");
  }
  TRACE((lvl_Common, "CmdINSTALLHandler :Exit"));
}

void CmdCOMPACTHandler(adl_atCmdPreParser_t *params)
{
  s32 s32RetVal;
  TRACE((lvl_Common, "CmdCOMPACTHandler :Start With Event %d", params->Type));
  adl_adFinalise(DOTA_Cell_Handle);
  adl_adUnsubscribe(DOTA_Cell_Handle);
  s32RetVal = adl_adRecompact(ADEventHandle);
  TRACE((lvl_AD, "adl_adRecompact Ret= %d", s32RetVal));
  if (s32RetVal == 0)
  {
    adl_atSendResponse(ADL_AT_RSP, "\r\nOK\r\n");
  }
  else
  {
    adl_atSendResponse(ADL_AT_RSP, "Error");
  }
  TRACE((lvl_Common, "CmdCOMPACTHandler :Exit"));
}

void initvariables()
{
  TRACE((lvl_WIP, "initvariables :Start"));
  ftp_DestAddr.Port = 21;
  ftp_DestAddr.Mode = 1;
  ftp_DestAddr.Type = 'A';
  wm_sprintf(ftp_DestAddr.Server, "your.own.ftp.server");
  wm_sprintf(ftp_DestAddr.Login, "UserName");
  wm_sprintf(ftp_DestAddr.Password, "Password");
  wm_sprintf(ftp_DestAddr.FileName1, "file1.wpb.dwl");
  wm_sprintf(ftp_DestAddr.FileName2, "file2.dwl");
  wm_sprintf(APN_Details.APN_Name, "APN_Name");
  wm_sprintf(APN_Details.Username, "APN_username");
  wm_sprintf(APN_Details.Password, "APN_password");
  TRACE((lvl_WIP, "initvariables :Exit"));
}