/**
LoRaWAN specific functions
**/


void lorawan_confirm_class_handler(DeviceClass_t Class)
{
  Serial.printf("switch to class %c done\n", "ABC"[Class]);
  // Informs the server that switch has occurred ASAP
  m_lora_app_json.buffsize = 0;
  m_lora_app_json.port = gAppPort;
  lmh_send(&m_lora_app_json, g_CurrentConfirm);
}


/**@brief LoRa function for handling HasJoined event.
 */
void lorawan_has_joined_handler(void)
{

  Serial.println("OTAA Mode, Network Joined!");
 // showDisplay("OTAA Mode:\nNetwork Joined!",1);
 // delay(1000);
 // showASA();

 

  lmh_error_status ret = lmh_class_request(g_CurrentClass);
  if (ret == LMH_SUCCESS)
  {
    delay(1000);
    TimerSetValue(&appTimer, LORAWAN_APP_INTERVAL);
    TimerStart(&appTimer);
    
  }else{
    Serial.println("LMH FAIL");

  }
}



/**@brief LoRa function for handling OTAA join failed
*/
static void lorawan_join_failed_handler(void)
{
  Serial.println("OTAA join failed!");
  //Serial.println("Check your EUI's and Keys's!");
  //Serial.println("Check if a Gateway is in range!");
  showDisplay("Failed to join OTAA network");
}
/**@brief Function for handling LoRaWan received data from Gateway
 *
 * @param[in] app_data  Pointer to rx data
 */
void lorawan_rx_handler(lmh_app_data_t *app_data)
{
  Serial.printf("LoRa Packet received on port %d, size:%d, rssi:%d, snr:%d, data:%s\n\r",
          app_data->port, app_data->buffsize, app_data->rssi, app_data->snr, app_data->buffer);
}



/**@brief Function for handling user timerout event.
 */
void tx_lora_periodic_handler(void)
{
  TimerSetValue(&appTimer, LORAWAN_APP_INTERVAL);
  TimerStart(&appTimer);
  send_lora_json_status();
}


/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module. This creates and starts application timers.
 */
uint32_t timers_init(void)
{
  TimerInit(&appTimer, tx_lora_periodic_handler);
  return 0;
}
