static void getNextBlock(uint32_t currentAddress)
{
   unsigned char *msg =  NULL;
   size_t size;


   createDataReq_u32(&msg, currentAddress, StartDescriptorAddress + OFFSET_TO_POINTER, &size);
   /* Clear any pending flags and flush queue*/
   wifiEngineState.cmdReplyPending = 0;
   wei_clear_cmd_queue();
   wei_send_cmd_raw((char *)msg, size);

   /* Start transmission of next 256 bytes */
   createDataReq_u32(&msg, START_TRANSMISSION, StartTxBmDesc , &size);
   /* Clear any pending flags and flush queue*/
   wifiEngineState.cmdReplyPending = 0;
   wei_clear_cmd_queue();
   wei_send_cmd_raw((char *)msg, size);

   /* Request signal host attention */
   createDataReq_u32(&msg, START_TRANSMISSION, signalHostAttentionAddress, &size);
   /* Clear any pending flags and flush queue*/
   wifiEngineState.cmdReplyPending = 0;
   wei_clear_cmd_queue();
   wei_send_cmd_raw((char *)msg, size);
}
static void sendCommitSuicideRequest(void)
{
   hic_message_context_t  msg_ref;
   char *cmd; 
   int size;   
   
   /* Send commit suicide request */
   Mlme_CreateMessageContext(msg_ref);
   if (Mlme_CreateCommitSuicideReq(&msg_ref))
   {
      /* Clear any pending flags and flush queue*/
      wifiEngineState.cmdReplyPending = 0;
      wei_clear_cmd_queue();
      if (! packer_HIC_Pack(&msg_ref))
      {
         return;
      }       
      cmd = msg_ref.packed;
      size = msg_ref.packed_size;
      msg_ref.packed = NULL; 
      
      if (wei_send_cmd_raw(cmd, size)!= WIFI_ENGINE_SUCCESS)
      {
         DE_TRACE_STATIC(TR_WARN, "Failed to send HIC_CTRL_COMMIT_SUICIDE_REQ\n");
      }        
   }
   else
   {
      DE_TRACE_STATIC(TR_WARN, "Failed to create suicide request\n");
   }
   
   Mlme_ReleaseMessageContext(msg_ref);
    
}
static void sendScbErrorRequest(void)
{
   hic_message_context_t  msg_ref;
   char *dst_str;
   char *cmd;
   int size;
   
   DE_TRACE_STATIC(TR_ALWAYS,"sendScbErrorRequest()\n");

   core_dump_state = W4_SCB_ERROR_CFM;

   /* Send scb error request */
   Mlme_CreateMessageContext(msg_ref);
   dst_str = (char*)DriverEnvironment_Nonpaged_Malloc(sizeof(SCB_ERROR_KEY_STRING)); 
   if (Mlme_CreateScbErrorReq(&msg_ref, dst_str))
   {
      /* Clear any pending flags and flush queue*/
      wifiEngineState.cmdReplyPending = 0;
      wei_clear_cmd_queue();
      if (! packer_HIC_Pack(&msg_ref))
      {
         return ;
      }      
      cmd = msg_ref.packed;
      size = msg_ref.packed_size;
      msg_ref.packed = NULL;      
      
      if (wei_send_cmd_raw(cmd, size) != WIFI_ENGINE_SUCCESS)
      {
         DE_TRACE_STATIC(TR_WARN, "Failed to send HIC_CTRL_SCB_ERROR_REQ\n");
      }   
   }
   DriverEnvironment_Nonpaged_Free(dst_str);
   Mlme_ReleaseMessageContext(msg_ref);
}
/*!
 * @brief Check if any data or command has been queued.
 *
 *
 * @return Nothing
 */
void WiFiEngine_PsCheckQueues(void)
{
#if (DE_CCX == CFG_INCLUDED)
    if(connected)
    {
        DE_TRACE_STATIC(TR_PS, "NIKS: ccx_handle_wakeup called from wakeup_ind->WiFiEngine_PsCheckQueues\n");
        ccx_handle_wakeup();
    }
#endif

   if((wifiEngineState.ps_queue_cnt == 0) && QUEUE_EMPTY(&cmd_queue))
   {
      /* No command or data queued */
      DE_TRACE_STATIC(TR_PS, "Data and command queue empty\n");
   }
   else
   {
      if(wifiEngineState.ps_queue_cnt > 0)
      {
         DE_TRACE_STATIC(TR_PS, "wifiEngineState.ps_queue_cnt > 0\n");
         if(connected)
         {       
            wifiEngineState.dataPathState = DATA_PATH_OPENED; 
            DriverEnvironment_handle_driver_wakeup();       
         }
         else
         {
            DE_TRACE_STATIC(TR_PS, "WifiEngine main state not driverConnected ignore queue count\n");
            DE_TRACE_INT(TR_PS, "WifiEngine main state: %d \n", wifiEngineState.main_state );
         }
      }     
      if(!QUEUE_EMPTY(&cmd_queue))
      {
         DE_TRACE_STATIC(TR_PS, "!QUEUE_EMPTY(&cmd_queue)\n");
         /* Send a queued cmd if one is ready */
         wei_send_cmd_raw(NULL, 0);
      }     
   }
}
/*!
 * @brief Handles packet received when core dump is enabled.
 *
 * @param pkt Received packet.
 *
 * @return 
 * - Always returns WIFI_ENGINE_SUCCESS
 */
void WiFiEngine_HandleCoreDumpPkt(char* pkt)
{
   size_t size;
   unsigned char *msg =  NULL;
   
   if (!WES_TEST_FLAG(WES_FLAG_HW_PRESENT)) 
      return;
   
   switch(core_dump_state)
   {
      case W4_SCB_ERROR_CFM:
      {
         unsigned char tx_desc[TX_DESC_SIZE];
         hic_message_context_t msg_ref;
         uint8_t  messageId;
         uint8_t  messageType;
         Blob_t blob;
         Mlme_CreateMessageContext(msg_ref);

         INIT_BLOB(&blob, pkt, 1500); /* XXX */
         /* Remove HIC header and add type/id info to msg_ref */
         packer_HIC_Unpack(&msg_ref, &blob); 
         messageId = msg_ref.msg_id;
         messageType = msg_ref.msg_type;
         msg_ref.packed = NULL;
         Mlme_ReleaseMessageContext(msg_ref);

         if((messageType == HIC_MESSAGE_TYPE_CTRL) && (messageId == HIC_CTRL_SCB_ERROR_CFM))
            {
               /* Handle confirm message */         
               if(handleSCBErrorCfm(pkt) == 0) 
               {

                  /* SCB_ERROR_CFM has error code 0, this means that firmware.
                   * is executing "normally" (no scb error has occured).
                   * However a command timeout has occured so it would be nice
                   * to force firmware in scb error and try to get a core dump.
                   */
                  DE_TRACE_STATIC(TR_ALWAYS,"Cancel timer\n");
                  DriverEnvironment_CancelTimer(wifiEngineState.cmd_timer_id);
                  DE_TRACE_STATIC(TR_ALWAYS,"Suicide requested\n");  
                  sendCommitSuicideRequest();
                  wifiEngineState.core_dump_state = WEI_CORE_DUMP_DISABLED;
                  return;
               }


            if(m_ext_ctx) 
            {
               /* Coredump already started */
               DriverEnvironment_Core_Dump_Abort(
                                          registry.network.basic.enableCoredump, 
                                          registry.network.basic.restartTargetAfterCoredump,
                                          m_objId,
                                          m_errCode, 
                                          &m_ext_ctx);
            } 
            else 
            {
               DriverEnvironment_indicate(WE_IND_CORE_DUMP_START, NULL, 0);
            }
            DriverEnvironment_Core_Dump_Started(
                                          registry.network.basic.enableCoredump, 
                                          registry.network.basic.restartTargetAfterCoredump,
                                          m_objId,
                                          m_errCode, 
                                          dump_total_size(dump_address_table),
                                          dump_total_size(dump_address_table),
                                          &m_ext_ctx);

            /* Turn off traces */
            //trace_mask &= ~(TR_CMD | TR_NOISE | TR_PS | TR_HIGH_RES | TR_DATA );
            trace_mask = 0;

            DriverEnvironment_Enable_Boot();

            /* This is to prevent command timeout logic to re-start the timer */
            WES_SET_FLAG(WES_FLAG_CMD_TIMEOUT_RUNNING);

            /* Initiate physical bm-descriptor to point to tx-descriptors */
            createDataReq_u32(&msg, StartDescriptorAddress, 
                              StartTxBmDesc + OFFSET_TO_POINTER_BM_DESC, &size);
            /* Clear any pending flags and flush queue*/
            wifiEngineState.cmdReplyPending = 0;
            wei_clear_cmd_queue();
            
            wei_send_cmd_raw((char *)msg, size);

            /* Initiate targets tx-descriptor to be used */
            createTxDescriptor(tx_desc);
            createDataReq(&msg, tx_desc, sizeof(tx_desc),
                          StartDescriptorAddress, &size);
            /* Clear any pending flags and flush queue*/
            wifiEngineState.cmdReplyPending = 0;
            wei_clear_cmd_queue();
            wei_send_cmd_raw((char *)msg, size);

            core_dump_state = W4_MEMORY_DUMP;
            /*
             * if m_ext_ctx==NULL we are not interested in the coredump and can restart the target directly
             * Not sure of how to do this in a safe way so will go ahead with the coredump anyway for now and fix this later 
             */
            if(m_ext_ctx==NULL) { 
               trace_mask = old_trace_mask; 
               DriverEnvironment_Core_Dump_Complete(
                                                FALSE,
                                                registry.network.basic.restartTargetAfterCoredump,
                                                m_objId,
                                                m_errCode, 
                                                &m_ext_ctx);
               DriverEnvironment_indicate(WE_IND_CORE_DUMP_COMPLETE, NULL, 0);
               DriverEnvironment_CancelTimer(wifiEngineState.cmd_timer_id);
               WES_CLEAR_FLAG(WES_FLAG_CMD_TIMEOUT_RUNNING);
               return;
            }

            currentRegion = dump_address_table;
            currentAddress = dump_region_start(currentRegion);

            getNextBlock(currentAddress);
         }
         else
         {
            DE_TRACE_INT2(TR_ALWAYS, "unexpected message %u.%u\n",
                          messageType,
                          messageId); 
         }
      }
      break;

      case W4_MEMORY_DUMP:
      {
         uint32_t len;
         /* Store recevied packet in a file */
         
         len = HIC_MESSAGE_LENGTH_GET(pkt);
         /* Move beyond size */ 
         pkt += 2;
         len -= 2;
         
         /* Write dumpfile header (for nrx600 coredumps) */
         if (dump_address_table == dump_address_table_nrx600
             && m_ext_ctx
             && currentAddress == dump_region_start(currentRegion)) {
            DriverEnvironment_Core_Dump_Write(m_ext_ctx, 
                                              (char *)currentRegion, 
                                              sizeof(*currentRegion));
         }
         
         len = DE_MIN(len, dump_region_end(currentRegion) - currentAddress);
         /* Copy to buffer */
         if (m_ext_ctx) {
            DriverEnvironment_Core_Dump_Write(m_ext_ctx, pkt, len);
         }

         currentAddress += len;

         if(currentAddress >= dump_region_end(currentRegion)) {
            /* end of region, skip forward */
            currentRegion++;
            currentAddress = dump_region_start(currentRegion);
            if(dump_region_size(currentRegion) == 0) {
               /* Complete - Start store data on a file and restart driver */
               trace_mask = old_trace_mask;
               DE_TRACE_STATIC(TR_ALWAYS,"Coredump complete - cancel timer\n");
               DriverEnvironment_CancelTimer(wifiEngineState.cmd_timer_id);
               WES_CLEAR_FLAG(WES_FLAG_CMD_TIMEOUT_RUNNING);
               DriverEnvironment_Core_Dump_Complete(registry.network.basic.enableCoredump && m_ext_ctx != NULL, /* see driverenv.h */
                                                    registry.network.basic.restartTargetAfterCoredump,
                                                    m_objId,
                                                    m_errCode, 
                                                    &m_ext_ctx);
               DriverEnvironment_indicate(WE_IND_CORE_DUMP_COMPLETE, NULL, 0);
               return;
            }
         }
         getNextBlock(currentAddress);
      }
      break;

      default:
         DE_BUG_ON(TRUE, "Invalid core dump state\n");
         break;
   }
}