void COMWIRELESS_RfStatemachine(void)
{
  otError error;
  if(THSM_WirelessState_previous != THSM_WirelessState)
  {
    printf("\n\r State : %d ..",THSM_WirelessState);    
    THSM_WirelessState_previous = THSM_WirelessState;
  }
  if(THSM_knxiot_running_b == TRUE)
  {
      pres_state = otJoinerGetState(COMWIRELESS_ot_instance);
      if(prev_state != pres_state)
      {
        printf("\n Joiner status: %d", pres_state);
        prev_state = pres_state;
      }
      pres_state1 = otPlatRadioGetState(COMWIRELESS_ot_instance);
      if(prev_state1 != pres_state1)
      {
        printf("\n Radio status: %d", pres_state1);
        prev_state1 = pres_state1;
      }
      //if (otTaskletsArePending(COMWIRELESS_ot_instance))
      // {
      //    otTaskletsProcess(COMWIRELESS_ot_instance);
      //    otSysProcessDrivers(COMWIRELESS_ot_instance);
      // }
       //else
       //{
       //   THSM_WirelessState = THSM_SLEEP_e;
       //}

      // Enter sleep state if no more tasks are pending
      //if (!otTaskletsArePending(COMWIRELESS_ot_instance))
      //{
      //if ((EOSM_teached_gateway_fU8 == TRUE) && (EOSM_start_teach_mode_fU8 == FALSE))
      //{
      //COMWIRELESS_SetReadyToSleepEOSM(TRUE);
      //}
      //}
      //else
      //{
      //COMWIRELESS_SetReadyToSleepEOSM(FALSE);
      //}

      // KNXIoT main function
     // CoapMainProcess(NULL);
  }
    switch(THSM_WirelessState)
    {
      case THSM_IDLE_e:       //joined previously Y-pair success and  N-wait for key press and INITIALIZE_COMM.
      {
        if(CSA_GetStartWirelessStateMachine_U8() == TRUE)
        {
         //if(COMWIRELESS_ot_instance != NULL)                                  //If communication is already joined and has instance already
         //{
         //   //THSM_WirelessState = INITIALIZE_COMM;
         //}
         //else 
         if((THSM_key_pressed_for_joining_b) && (MCDC_GetReadyToSleep_U8() == TRUE))
          {
            THSM_knxiot_running_b = TRUE;
            THSM_key_pressed_for_joining_b = FALSE;
            if (!THSM_initialize_otcomm_b )      //THSM_initialize_otcomm_b ensures thread initalization is done only once
            {
                THSM_initialize_otcomm_b = TRUE;
                COMWIRELESS_InitCommunication();               
            }   
            if (OT_JOINER_STATE_IDLE == otJoinerGetState(COMWIRELESS_ot_instance))
            {
                if (!THSM_teached_gateway_b)
                {
                    /*enable recieve state of radio to check if there are anything to update on wakeup*/
                    COMWIRELESS_ReceiveEnable();
                    // Start pairing                    
                    THSM_WirelessState = THSM_START_JOINING_e;
                }
            }         
          }
          else if(THSM_teached_gateway_b)
          {
            /*enable recieve state of radio to check if there are anything to update on wakeup*/
            COMWIRELESS_ReceiveEnable();
            THSM_wakeupIdle = TRUE;
            THSM_WirelessState = THSM_PREPARE_WAKEUP_e;                   //GO to continous communication after pairing\
            THSM_process_timeout_b = FALSE;
            THSM_data_request_executed_b = FALSE;
            TIMEAPP_StartSingleShotTimerWithCallback(TIMEAPI_DEBUG_e, THSM_PROCESS_TIMEOUT_dU16, THSM_RfProcessCallback, (void *)NULL);
           // printf("\n\r wake up...."); 
          }
          else
          {
            THSM_WirelessState = THSM_SLEEP_e;
          }
        }
      break;
      }

      case INITIALIZE_COMM:       //COMWIRELESS_ot_instance != NULL
      {

        THSM_WirelessState = THSM_PAIR_SUCCESS_e;
      break;
      }

      case THSM_START_JOINING_e://
      {
            //if (otTaskletsArePending(COMWIRELESS_ot_instance))
            // {
                otTaskletsProcess(COMWIRELESS_ot_instance);
                otSysProcessDrivers(COMWIRELESS_ot_instance); 
                CoapMainProcess(NULL);
            // }
            if ((THSM_joining_in_progress_b == FALSE) && (THSM_joining_try_done_b == FALSE))
            {
                if (OT_ERROR_NONE == otJoinerStart(COMWIRELESS_ot_instance, "SEMENS", NULL, NULL, NULL, NULL, NULL, JoinerCallback, NULL))
                {
                    THSM_joining_try_done_b = FALSE;
                    THSM_joining_in_progress_b = TRUE;
                    THSM_start_pairing_b = TRUE;
                    #ifdef DEBUG
                    ConsolePrint("\n\r Start joining ...\n\r"); 
                    #endif
                    //LED Start glowing
                    GENAPP_GpioAppSetLedR(TRUE);
                }

            }
            else
            {
              if (THSM_joining_try_done_b == TRUE)
              {                
                if(THSM_joining_successful_b)
                {
                  THSM_WirelessState = THSM_PAIR_SUCCESS_e;
                }
                else
                {
                  THSM_WirelessState = THSM_PAIR_UNSUCCESS_e;
                }
              }
            }
      
      break;
      }

      case THSM_PAIR_SUCCESS_e:
      {
            //if (otTaskletsArePending(COMWIRELESS_ot_instance))
            // {
                otTaskletsProcess(COMWIRELESS_ot_instance);
                otSysProcessDrivers(COMWIRELESS_ot_instance); 
                CoapMainProcess(NULL);
             //}             
              if(THSM_teached_gateway_b == FALSE)
              {
                  GENAPP_GpioSetLedRTemporarily(THSM_LED_TOGGLE_DURATION_dU16,THSM_SUCCESSFUL_JOIN_BLINK_dU8);//LED Blink for 5 times
                  // Pairing is complete -> enable Thread interface
                  error = otThreadSetEnabled(COMWIRELESS_ot_instance, true);
                  ASSERT(error == OT_ERROR_NONE);
                  #ifdef DEBUG
                  ConsolePrint("\nThread interface has been enabled.");
                  #endif
                  THSM_process_timeout_b = FALSE;
                  THSM_teached_gateway_b = TRUE;
                  TIMEAPP_StartSingleShotTimerWithCallback(TIMEAPI_RF_TIMEOUT_e, 10000, THSM_RfProcessCallback, (void *)NULL);
              }            
              else
              {
                  if((THSM_process_timeout_b == TRUE))
                {
                      //COMWIRELESS_SetTimeoutOccured(FALSE);
                     //THSM_data_request_executed_b = TRUE;
                     #ifdef DEBUG
                      ConsolePrint("\n\rSet new PollPeriod and disable RX when IDLE ...\n\r");
                      #endif

                      otLinkModeConfig mode;
                      memset(&mode, 0, sizeof(mode));
                      mode.mRxOnWhenIdle = false; // Join network as SED.
                      mode.mSecureDataRequests = true;

                      error = otThreadSetLinkMode(COMWIRELESS_ot_instance, mode);
                      ASSERT(error == OT_ERROR_NONE);

                      //error = otLinkSendDataRequest(COMWIRELESS_ot_instance);
                      //ASSERT(error == OT_ERROR_NONE);

                      //error = otLinkSetPollPeriod(COMWIRELESS_ot_instance, COMWIRELESS_GetWakeUpTime_U16()*1000);
                      //ASSERT(error == OT_ERROR_NONE);

                      otThreadSetChildTimeout(COMWIRELESS_ot_instance, 300);
                      THSM_WirelessState = THSM_PREPARE_WAKEUP_e;

                    }
                //THSM_WirelessState = THSM_PREPARE_WAKEUP_e;
              }

             // TIMEAPP_StartSingleShotTimerWithCallback(TIMEAPI_RF_TIMEOUT_e, 10000, COMWIRELESS_RfTimeoutCallback, (void *)NULL);        
      break;
      }

      case THSM_PAIR_UNSUCCESS_e:
      {      
        otPlatRadioSleep(COMWIRELESS_ot_instance);
        //THSM_start_pairing_b = FALSE;
        //COMWIRELESS_SetReadyToSleepTHSM(TRUE);
        GENAPP_GpioAppSetLedR(FALSE);                         //LED stop glowing 
        TIMEAPP_StopSingleShotTimer(TIMEAPI_LED_e);
        THSM_knxiot_running_b = FALSE;
        THSM_WirelessState = THSM_SLEEP_e;
      break;
      }

      case THSM_PREPARE_WAKEUP_e:
      {
      if (THSM_data_request_executed_b == FALSE)
          {
            error = otLinkSendDataRequest(COMWIRELESS_ot_instance);
            THSM_data_request_executed_b = TRUE;
            THSM_WirelessState = THSM_WAKEUP_e;
          }
      break;
      }

      case THSM_WAKEUP_e:
      {        
          otTaskletsProcess(COMWIRELESS_ot_instance);
          otSysProcessDrivers(COMWIRELESS_ot_instance); 
          CoapMainProcess(NULL);
         // if ((!otTaskletsArePending(COMWIRELESS_ot_instance)) && (THSM_wakeupIdle == TRUE) && (THSM_process_timeout_b == TRUE))
         // if ((!otTaskletsArePending(COMWIRELESS_ot_instance)) && (THSM_process_timeout_b == TRUE))
          if ((otPlatRadioGetState(COMWIRELESS_ot_instance) == 1) && (THSM_process_timeout_b == TRUE))
          {
              THSM_wakeupIdle = FALSE;
              THSM_WirelessState = THSM_SLEEP_e;
          }              
          
          //if((COMWIRELESS_GetTimeoutOccured_U8() == TRUE))
          //{
          //      COMWIRELESS_SetTimeoutOccured(FALSE);
          //     //THSM_data_request_executed_b = TRUE;
          //     #ifdef DEBUG
          //      ConsolePrint("\n\rSet new PollPeriod and disable RX when IDLE ...\n\r");
          //      #endif

          //      otLinkModeConfig mode;
          //      memset(&mode, 0, sizeof(mode));
          //      mode.mRxOnWhenIdle = false; // Join network as SED.
          //      mode.mSecureDataRequests = true;

          //      error = otThreadSetLinkMode(COMWIRELESS_ot_instance, mode);
          //      ASSERT(error == OT_ERROR_NONE);

          //      //error = otLinkSendDataRequest(COMWIRELESS_ot_instance);
          //      //ASSERT(error == OT_ERROR_NONE);

          //      //error = otLinkSetPollPeriod(COMWIRELESS_ot_instance, COMWIRELESS_GetWakeUpTime_U16()*1000);
          //      //ASSERT(error == OT_ERROR_NONE);

          //      otThreadSetChildTimeout(COMWIRELESS_ot_instance, 300);
          //      THSM_WirelessState = THSM_SLEEP_e;

          //    }
              //if (!otTaskletsArePending(COMWIRELESS_ot_instance))
              //{
              //    THSM_WirelessState = THSM_SLEEP_e;
              //}
         
      break;
      }

      case THSM_SLEEP_e:
      {
        otLinkModeConfig mode;
        memset(&mode, 0, sizeof(mode));
        mode.mRxOnWhenIdle = false;
        mode.mSecureDataRequests = true;

        otError error;
        error = otThreadSetLinkMode(COMWIRELESS_ot_instance, mode);
        ASSERT(error == OT_ERROR_NONE);
        /*radio sleep*/
        COMWIRELESS_RadioSleep();
        COMWIRELESS_SetReadyToSleepTHSM(TRUE);
        //if (CSA_GetStartWirelessStateMachine_U8() == TRUE)
        //{
          
          THSM_WirelessState = THSM_IDLE_e;
       // }
      break;
      }

      default:
      {
      THSM_WirelessState = THSM_IDLE_e;
      }
      break;
    }
    
}