/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    lora_at.h
  * @author  MCD Application Team
  * @brief   Header for driver at.c module
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LORA_AT_H__
#define __LORA_AT_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "LmHandler.h"
#include "stm32_adv_trace.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/*
 * AT Command Id errors. Note that they are in sync with ATError_description static array
 * in command.c
 */
typedef enum eATEerror
{
  AT_OK = 0,
  AT_ERROR,
  AT_PARAM_ERROR,
  AT_BUSY_ERROR,
  AT_TEST_PARAM_OVERFLOW,
  AT_NO_NET_JOINED,
  AT_RX_ERROR,
  AT_NO_CLASS_B_ENABLE,
  AT_DUTYCYCLE_RESTRICTED,
  AT_CRYPTO_ERROR,
  AT_MAX,
} ATEerror_t;

/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* External variables --------------------------------------------------------*/
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/* Exported macro ------------------------------------------------------------*/
/* AT printf */
#define AT_PRINTF(...)     do{  UTIL_ADV_TRACE_COND_FSend(VLEVEL_OFF, T_REG_OFF, TS_OFF, __VA_ARGS__);}while(0)
#define AT_PPRINTF(...)    do{ } while( UTIL_ADV_TRACE_OK \
                               != UTIL_ADV_TRACE_COND_FSend(VLEVEL_ALWAYS, T_REG_OFF, TS_OFF, __VA_ARGS__) ) /*Polling Mode*/

//#define Factory_MODE     				0
//#define TEST_Borad_MODE     	 0


/* AT Command strings. Commands start with AT */
/* General commands */
#define AT_VER        "+VER"
#define AT_VL         "+VL"
#define AT_LTIME      "+LTIME"
#define AT_RESET      "Z"

/* Context Store */
#define AT_RFS        "+RFS"
#define AT_CS         "+CS"

/* Keys, IDs and EUIs management commands */
#define AT_JOINEUI    "+APPEUI" /*to match with V1.0.x specification- For V1.1.x "+APPEUI" will be replaced by "+JOINEUI"*/
#define AT_NWKKEY     "+NWKKEY"
#define AT_APPKEY     "+APPKEY"
#define AT_NWKSKEY    "+NWKSKEY"
#define AT_APPSKEY    "+APPSKEY"
#define AT_DADDR      "+DADDR"
#define AT_DEUI       "+DEUI"
#define AT_NWKID      "+NWKID"

/* LoRaWAN join and send data commands */
#define AT_JOIN       "+JOIN"
#define AT_LINKC      "+LINKC"
#define AT_SEND       "+SEND"

/* LoRaWAN network management commands */
#define AT_ADR        "+ADR"
#define AT_DR         "+DR"
#define AT_BAND       "+BAND"
#define AT_CLASS      "+CLASS"
#define AT_DCS        "+DCS"
#define AT_JN1DL      "+JN1DL"
#define AT_JN2DL      "+JN2DL"
#define AT_RX1DL      "+RX1DL"
#define AT_RX2DL      "+RX2DL"
#define AT_RX2DR      "+RX2DR"
#define AT_RX2FQ      "+RX2FQ"
#define AT_TXP        "+TXP"
#define AT_PGSLOT     "+PGSLOT"

/* Radio tests commands */
#define AT_TTONE      "+TTONE"
#define AT_TRSSI      "+TRSSI"
#define AT_TCONF      "+TCONF"
#define AT_TTX        "+TTX"
#define AT_TRX        "+TRX"
#define AT_TTH        "+TTH"
#define AT_TOFF       "+TOFF"

/* Radio access commands */
#define AT_REGW       "+REGW"
#define AT_REGR       "+REGR"

/* LoraWAN Certif command */
#define AT_CERTIF     "+CERTIF"

/* Information command */
#define AT_BAT        "+BAT"

#define AT_CHIPID			"+CHIPID"
#define AT_GPIOTEST   "+GPIOTEST"
#define AT_PINGPONG   "+PINGPONG"

#define AT_MASK   "+MASK"

#if(TEST_Borad_MODE==1)
#define AT_MODE   "+MODE"
#endif

/* USER CODE BEGIN EM */
/*---------------------------------------------------------------------------*/
/*                             LoRaWAN NVM configuration                     */
/*---------------------------------------------------------------------------*/
/**
  * @brief LoRaWAN NVM Flash address
  * @note last 2 sector of a 128kBytes device
  */
#define LORAWAN_NVM_BASE_ADDRESS                    ((void *)0x0801F000UL)

#define PINGPONGMODE_NVM_BASE_ADDRESS               ((void *)0x0801F800UL)


//#ifndef LoraWan_DEFAULT_MASK0
///*enabled channels from channel 15 down to channel 0*/
//int16_t LoraWan_DEFAULT_MASK0=0x0000;  	
//#endif

//#ifndef LoraWan_DEFAULT_MASK1
///*enabled channels from channel 31 down to channel 16*/
//int16_t LoraWan_DEFAULT_MASK1=0x0000;
//#endif

//#ifndef LoraWan_DEFAULT_MASK2
///*enabled channels from channel 47 down to channel 32*/
//int16_t LoraWan_DEFAULT_MASK2=0x0000;
//#endif

//#ifndef LoraWan_DEFAULT_MASK3
///*enabled channels from channel 63 down to channel 48*/
//int16_t LoraWan_DEFAULT_MASK3=0x0000;
//#endif

//#ifndef LoraWan_DEFAULT_MASK4
///*enabled channels from channel 79 down to channel 64*/
//int16_t LoraWan_DEFAULT_MASK4=0x0000;
//#endif

//#ifndef LoraWan_DEFAULT_MASK5
///*enabled channels from channel 95 down to channel 80*/
//int16_t LoraWan_DEFAULT_MASK5=0x00ff;   //The default frequency group for lorawan is 80-87
//#endif

//int16_t LoraWan_DEFAULT_MASK_Channel[6];

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
/**
  * @brief  Return AT_OK in all cases
  * @param  param string of the AT command - unused
  * @retval AT_OK
  */
ATEerror_t AT_return_ok(const char *param);

/**
  * @brief  Return AT_ERROR in all cases
  * @param  param string of the AT command - unused
  * @retval AT_ERROR
  */
ATEerror_t AT_return_error(const char *param);

/* --------------- Application events --------------- */
/**
  * @brief  Event callback on join
  * @param params
  */
void AT_event_join(LmHandlerJoinParams_t *params);

/**
  * @brief  Event callback on received data
  * @param appData
  * @param params
  */
void AT_event_receive(LmHandlerAppData_t *appData, LmHandlerRxParams_t *params);

/**
  * @brief  Event callback on confirmed acknowledge
  * @param  params
  */
void AT_event_confirm(LmHandlerTxParams_t *params);

/**
  * @brief  Event callback on class updated
  * @param  deviceClass
  */
void AT_event_ClassUpdate(DeviceClass_t deviceClass);

/**
  * @brief  Event callback on beacon status updated
  * @param  params
  */
void AT_event_Beacon(LmHandlerBeaconParams_t *params);

void AT_event_OnNvmDataChange(LmHandlerNvmContextStates_t state);

void AT_event_OnStoreContextRequest(void *nvm, uint32_t nvm_size);

void AT_event_OnRestoreContextRequest(void *nvm, uint32_t nvm_size);

/* --------------- General commands --------------- */
/**
  * @brief  Print the version of the AT_Slave FW
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_version_get(const char *param);

/**
  * @brief  Get the verbose level
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_verbose_get(const char *param);

/**
  * @brief  Set the verbose level
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_verbose_set(const char *param);

/**
  * @brief  Get the local time in UTC format
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_LocalTime_get(const char *param);

/**
  * @brief  Trig a reset of the MCU
  * @param  param string of the AT command - unused
  * @retval AT_OK
  */
ATEerror_t AT_reset(const char *param);

/* --------------- Context Store commands --------------- */
/**
  * @brief  Restore factory settings in Eeprom
  * @param  param string of the AT command - unused
  * @retval AT_OK
  */
ATEerror_t AT_restore_factory_settings(const char *param);

/**
  * @brief  Store current settings in Eeprom
  * @param  param string of the AT command - unused
  * @retval AT_OK
  */
ATEerror_t AT_store_context(const char *param);

/* --------------- Keys, IDs and EUIs management commands --------------- */
/**
  * @brief  Print Join Eui
  * @param  param string of the AT command
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_JoinEUI_get(const char *param);

/**
  * @brief  Set Join Eui
  * @param  param string of the AT command
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_JoinEUI_set(const char *param);

/**
  * @brief  Print Network Key
  * @param  param string of the AT command
  * @retval AT_OK
  */
ATEerror_t AT_NwkKey_get(const char *param);

/**
  * @brief  Set Network Key
  * @param  param string of the AT command
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_NwkKey_set(const char *param);

/**
  * @brief  Print Application Key
  * @param  param string of the AT command
  * @retval AT_OK
  */
ATEerror_t AT_AppKey_get(const char *param);

/**
  * @brief  Set Application Key
  * @param  param string of the AT command
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_AppKey_set(const char *param);

/**
  * @brief  Print Network Session Key
  * @param  param string of the AT command
  * @retval AT_OK
  */
ATEerror_t AT_NwkSKey_get(const char *param);

/**
  * @brief  Set Network Session Key
  * @param  param String pointing to provided DevAddr
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_NwkSKey_set(const char *param);

/**
  * @brief  Print Application Session Key
  * @param  param string of the AT command
  * @retval AT_OK
  */
ATEerror_t AT_AppSKey_get(const char *param);

/**
  * @brief  Set Application Session Key
  * @param  param String pointing to provided DevAddr
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_AppSKey_set(const char *param);

/**
  * @brief  Print the DevAddr
  * @param  param String pointing to provided DevAddr
  * @retval AT_OK
  */
ATEerror_t AT_DevAddr_get(const char *param);

/**
  * @brief  Set DevAddr
  * @param  param String pointing to provided DevAddr
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_DevAddr_set(const char *param);

/**
  * @brief  Print Device EUI
  * @param  param string of the AT command - unused
  * @retval AT_OK
  */
ATEerror_t AT_DevEUI_get(const char *param);

/**
  * @brief  Set Device EUI
  * @param  param string of the AT command
  * @retval AT_OK if OK
  */
ATEerror_t AT_DevEUI_set(const char *param);

/**
  * @brief  Print the Network ID
  * @param  param String pointing to provided parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_NetworkID_get(const char *param);

/**
  * @brief  Set the Network ID
  * @param  param String pointing to provided parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_NetworkID_set(const char *param);

/* --------------- LoRaWAN join and send data commands --------------- */
/**
  * @brief  Join a network
  * @param  param String parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Join(const char *param);

/**
  * @brief  Piggyback a Link Check Request to the next uplink
  * @param  param String parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Link_Check(const char *param);

/**
  * @brief  Print last received message
  * @param  param String parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Send(const char *param);

/* --------------- LoRaWAN network management commands --------------- */
/**
  * @brief  Print Adaptive Data Rate setting
  * @param  param String pointing to provided ADR setting
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_ADR_get(const char *param);

/**
  * @brief  Set Adaptive Data Rate setting
  * @param  param String pointing to provided ADR setting
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_ADR_set(const char *param);

/**
  * @brief  Print Data Rate
  * @param  param String pointing to provided rate
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_DataRate_get(const char *param);

/**
  * @brief  Set Data Rate
  * @param  param String pointing to provided rate
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_DataRate_set(const char *param);

/**
  * @brief  Print actual Active Region
  * @param  param string of the AT command - unused
  * @retval AT_OK
  */
ATEerror_t AT_Region_get(const char *param);

/**
  * @brief  Set Active Region
  * @param  param string of the AT command
  * @retval AT_OK if OK
  */
ATEerror_t AT_Region_set(const char *param);

/**
  * @brief  Print the Device Class
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_DeviceClass_get(const char *param);

/**
  * @brief  Set the Device Class
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_DeviceClass_set(const char *param);

/**
  * @brief  Get ETSI Duty Cycle parameter
  * @param  param String pointing to provided param
  * @retval AT_OK
  */
ATEerror_t AT_DutyCycle_get(const char *param);

/**
  * @brief  Set ETSI Duty Cycle parameter
  * @param  param String pointing to provided Duty Cycle value
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_DutyCycle_set(const char *param);

/**
  * @brief  Print the delay between the end of the Tx and the Join Rx Window 1
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_JoinAcceptDelay1_get(const char *param);

/**
  * @brief  Set the delay between the end of the Tx and the Join Rx Window 1
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_JoinAcceptDelay1_set(const char *param);

/**
  * @brief  Print the delay between the end of the Tx and the Join Rx Window 2
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_JoinAcceptDelay2_get(const char *param);

/**
  * @brief  Set the delay between the end of the Tx and the Join Rx Window 2
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_JoinAcceptDelay2_set(const char *param);

/**
  * @brief  Print the delay between the end of the Tx and the Rx Window 1
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx1Delay_get(const char *param);

/**
  * @brief  Set the delay between the end of the Tx and the Rx Window 1
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx1Delay_set(const char *param);

/**
  * @brief  Print the delay between the end of the Tx and the Rx Window 2
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx2Delay_get(const char *param);

/**
  * @brief  Set the delay between the end of the Tx and the Rx Window 2
  * @param  param String pointing to provided param
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx2Delay_set(const char *param);

/**
  * @brief  Print Rx2 window data rate
  * @param  param String pointing to parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx2DataRate_get(const char *param);

/**
  * @brief  Set Rx2 window data rate
  * @param  param String pointing to parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx2DataRate_set(const char *param);

/**
  * @brief  Print Rx2 window frequency
  * @param  param String pointing to parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx2Frequency_get(const char *param);

/**
  * @brief  Set Rx2 window frequency
  * @param  param String pointing to parameter
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_Rx2Frequency_set(const char *param);

/**
  * @brief  Print Transmit Power
  * @param  param String pointing to provided power
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_TransmitPower_get(const char *param);

/**
  * @brief  Set Transmit Power
  * @param  param String pointing to provided power
  * @retval AT_OK if OK, or an appropriate AT_xxx error code
  */
ATEerror_t AT_TransmitPower_set(const char *param);

/**
  * @brief  Get the unicast pingslot periodicity, Data rate and PsFrequency
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_PingSlot_get(const char *param);

/**
  * @brief  Set the unicast pingslot periodicity
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_PingSlot_set(const char *param);

/* --------------- Radio tests commands --------------- */
/**
  * @brief  Start Tx test
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_txTone(const char *param);

/**
  * @brief  Start Rx tone
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_rxRssi(const char *param);

/**
  * @brief  Get Rx or Tx test config
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_get_config(const char *param);

/**
  * @brief  Set Rx or Tx test config
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_set_config(const char *param);

/**
  * @brief  Start Tx LoRa test
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_tx(const char *param);

/**
  * @brief  Start Rx LoRa test
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_rx(const char *param);

/**
  * @brief  Start Tx hopping
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_tx_hopping(const char *param);

/**
  * @brief  stop Rx or Tx test
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_test_stop(const char *param);

/* --------------- Radio access commands --------------- */
/**
  * @brief  Write Radio Register
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_write_register(const char *param);

/**
  * @brief  Read Radio Register
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_read_register(const char *param);

/* --------------- LoraWAN Certif command --------------- */
/**
  * @brief  set the Modem in Certif Mode
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_Certif(const char *param);

/* --------------- Information command --------------- */
/**
  * @brief  Get the battery level
  * @param  param String parameter
  * @retval AT_OK
  */
ATEerror_t AT_bat_get(const char *param);
ATEerror_t AT_CHIPID_GET(const char *param);
ATEerror_t AT_GPIO_TEST(const char *param);
ATEerror_t AT_PINGPONG_TEST(const char *param);
ATEerror_t AT_BANDMASK_SET(const char *param);

#if(TEST_Borad_MODE==1)
ATEerror_t AT_MODE_SET(const char *param);
#endif

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

#ifdef __cplusplus
}
#endif

#endif /* __LORA_AT_H__ */
