mirror of
				https://github.com/manuelbl/ttn-esp32.git
				synced 2025-10-31 18:50:33 +01:00 
			
		
		
		
	Adapted code formatting
This commit is contained in:
		
							
								
								
									
										3
									
								
								.clang-format
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.clang-format
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | |||||||
|  | BasedOnStyle: Microsoft | ||||||
|  | IndentWidth: 4 | ||||||
|  | ColumnLimit: 120 | ||||||
| @ -41,7 +41,6 @@ enum TTNResponseCode | |||||||
|     kTTNSuccessfulReceive = TTN_SUCCESSFUL_RECEIVE |     kTTNSuccessfulReceive = TTN_SUCCESSFUL_RECEIVE | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief RX/TX window |  * @brief RX/TX window | ||||||
|  */ |  */ | ||||||
| @ -65,7 +64,6 @@ enum TTNRxTxWindow | |||||||
|     kTTNRx2Window = TTN_WINDOW_RX2 |     kTTNRx2Window = TTN_WINDOW_RX2 | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief Spreading Factor |  * @brief Spreading Factor | ||||||
|  */ |  */ | ||||||
| @ -105,7 +103,6 @@ enum TTNSpreadingFactor | |||||||
|     kTTNSF12 = TTN_SF12 |     kTTNSF12 = TTN_SF12 | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief Bandwidth |  * @brief Bandwidth | ||||||
|  */ |  */ | ||||||
| @ -129,7 +126,6 @@ enum TTNBandwidth | |||||||
|     kTTNBW500 = TTN_BW_500 |     kTTNBW500 = TTN_BW_500 | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief Data Rate |  * @brief Data Rate | ||||||
|  * |  * | ||||||
| @ -386,7 +382,6 @@ enum TTNDataRate | |||||||
|     kTTNDRJoinDdefault |     kTTNDRJoinDdefault | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief RF settings for TX or RX |  * @brief RF settings for TX or RX | ||||||
|  */ |  */ | ||||||
| @ -406,7 +401,6 @@ struct TTNRFSettings | |||||||
|     uint32_t frequency; |     uint32_t frequency; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief Callback for recieved messages |  * @brief Callback for recieved messages | ||||||
|  * |  * | ||||||
| @ -414,7 +408,7 @@ struct TTNRFSettings | |||||||
|  * @param length   number of received bytes |  * @param length   number of received bytes | ||||||
|  * @param port     port the message was received on |  * @param port     port the message was received on | ||||||
|  */ |  */ | ||||||
| typedef void (*TTNMessageCallback)(const uint8_t* payload, size_t length, ttn_port_t port); | typedef void (*TTNMessageCallback)(const uint8_t *payload, size_t length, ttn_port_t port); | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief TTN device |  * @brief TTN device | ||||||
| @ -426,16 +420,21 @@ typedef void (*TTNMessageCallback)(const uint8_t* payload, size_t length, ttn_po | |||||||
|  */ |  */ | ||||||
| class TheThingsNetwork | class TheThingsNetwork | ||||||
| { | { | ||||||
| public: |   public: | ||||||
|     /** |     /** | ||||||
|      * @brief Constructs a new The Things Network device instance. |      * @brief Constructs a new The Things Network device instance. | ||||||
|      */ |      */ | ||||||
|     TheThingsNetwork() { ttn_init(); } |     TheThingsNetwork() | ||||||
|  |     { | ||||||
|  |         ttn_init(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Destroys the The Things Network device instance. |      * @brief Destroys the The Things Network device instance. | ||||||
|      */ |      */ | ||||||
|     ~TheThingsNetwork() { } |     ~TheThingsNetwork() | ||||||
|  |     { | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Resets the LoRaWAN radio. |      * @brief Resets the LoRaWAN radio. | ||||||
| @ -443,7 +442,10 @@ public: | |||||||
|      * To restart communication, @ref join() must be called. |      * To restart communication, @ref join() must be called. | ||||||
|      * Clears neither the provisioned keys nor the configured pins. |      * Clears neither the provisioned keys nor the configured pins. | ||||||
|      */ |      */ | ||||||
|     void reset() { ttn_reset(); } |     void reset() | ||||||
|  |     { | ||||||
|  |         ttn_reset(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Configures the pins used to communicate with the LoRaWAN radio chip. |      * @brief Configures the pins used to communicate with the LoRaWAN radio chip. | ||||||
| @ -453,8 +455,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @param spi_host  The SPI bus/peripherial to use (`SPI_HOST`, `HSPI_HOST` or `VSPI_HOST`). |      * @param spi_host  The SPI bus/peripherial to use (`SPI_HOST`, `HSPI_HOST` or `VSPI_HOST`). | ||||||
|      * @param nss       The GPIO pin number connected to the radio chip's NSS pin (serving as the SPI chip select) |      * @param nss       The GPIO pin number connected to the radio chip's NSS pin (serving as the SPI chip select) | ||||||
|      * @param rxtx      The GPIO pin number connected to the radio chip's RXTX pin (@ref TTN_NOT_CONNECTED if not connected) |      * @param rxtx      The GPIO pin number connected to the radio chip's RXTX pin (@ref TTN_NOT_CONNECTED if not | ||||||
|      * @param rst       The GPIO pin number connected to the radio chip's RST pin (@ref TTN_NOT_CONNECTED if not connected) |      * connected) | ||||||
|  |      * @param rst       The GPIO pin number connected to the radio chip's RST pin (@ref TTN_NOT_CONNECTED if not | ||||||
|  |      * connected) | ||||||
|      * @param dio0      The GPIO pin number connected to the radio chip's DIO0 pin |      * @param dio0      The GPIO pin number connected to the radio chip's DIO0 pin | ||||||
|      * @param dio1      The GPIO pin number connected to the radio chip's DIO1 pin |      * @param dio1      The GPIO pin number connected to the radio chip's DIO1 pin | ||||||
|      */ |      */ | ||||||
| @ -475,7 +479,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @param band band (0 for all bands, or value between 1 and 8) |      * @param band band (0 for all bands, or value between 1 and 8) | ||||||
|      */ |      */ | ||||||
|     void setSubband(int band) { ttn_set_subband(band); } |     void setSubband(int band) | ||||||
|  |     { | ||||||
|  |         ttn_set_subband(band); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Sets the credentials needed to activate the device via OTAA, without activating it. |      * @brief Sets the credentials needed to activate the device via OTAA, without activating it. | ||||||
| @ -490,7 +497,10 @@ public: | |||||||
|      * @param appKey  AppKey of the device (32 character string with hexadecimal data) |      * @param appKey  AppKey of the device (32 character string with hexadecimal data) | ||||||
|      * @return `true` if the provisioning was successful, `false`  if the provisioning failed |      * @return `true` if the provisioning was successful, `false`  if the provisioning failed | ||||||
|      */ |      */ | ||||||
|     bool provision(const char *devEui, const char *appEui, const char *appKey) { return ttn_provision(devEui, appEui, appKey); } |     bool provision(const char *devEui, const char *appEui, const char *appKey) | ||||||
|  |     { | ||||||
|  |         return ttn_provision(devEui, appEui, appKey); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Sets the information needed to activate the device via OTAA, using the MAC to generate the DevEUI |      * @brief Sets the information needed to activate the device via OTAA, using the MAC to generate the DevEUI | ||||||
| @ -514,14 +524,20 @@ public: | |||||||
|      * @param appKey  AppKey of the device (32 character string with hexadecimal data) |      * @param appKey  AppKey of the device (32 character string with hexadecimal data) | ||||||
|      * @return `true` if the provisioning was successful, `false`  if the provisioning failed |      * @return `true` if the provisioning was successful, `false`  if the provisioning failed | ||||||
|      */ |      */ | ||||||
|     bool provisionWithMAC(const char *appEui, const char *appKey) { return ttn_provision_with_mac(appEui, appKey); } |     bool provisionWithMAC(const char *appEui, const char *appKey) | ||||||
|  |     { | ||||||
|  |         return ttn_provision_with_mac(appEui, appKey); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Starts task listening on configured UART for AT commands. |      * @brief Starts task listening on configured UART for AT commands. | ||||||
|      * |      * | ||||||
|      * Run `make menuconfig` to configure it. |      * Run `make menuconfig` to configure it. | ||||||
|      */ |      */ | ||||||
|     void startProvisioningTask() { ttn_start_provisioning_task(); } |     void startProvisioningTask() | ||||||
|  |     { | ||||||
|  |         ttn_start_provisioning_task(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Waits until the DevEUI, AppEUI/JoinEUI and AppKey have been provisioned |      * @brief Waits until the DevEUI, AppEUI/JoinEUI and AppKey have been provisioned | ||||||
| @ -531,7 +547,10 @@ public: | |||||||
|      * or call of @ref join(const char*, const char*, const char*), this function |      * or call of @ref join(const char*, const char*, const char*), this function | ||||||
|      * immediately returns. |      * immediately returns. | ||||||
|      */ |      */ | ||||||
|     void waitForProvisioning() { ttn_wait_for_provisioning(); } |     void waitForProvisioning() | ||||||
|  |     { | ||||||
|  |         ttn_wait_for_provisioning(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Activates the device via OTAA. |      * @brief Activates the device via OTAA. | ||||||
| @ -543,7 +562,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @return `true` if the activation was succesful, `false` if the activation failed |      * @return `true` if the activation was succesful, `false` if the activation failed | ||||||
|      */ |      */ | ||||||
|     bool join() { return ttn_join_provisioned(); } |     bool join() | ||||||
|  |     { | ||||||
|  |         return ttn_join_provisioned(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Sets the DevEUI, AppEUI/JoinEUI and AppKey and activate the device via OTAA. |      * @brief Sets the DevEUI, AppEUI/JoinEUI and AppKey and activate the device via OTAA. | ||||||
| @ -557,7 +579,10 @@ public: | |||||||
|      * @param appKey  AppKey of the device (32 character string with hexadecimal data) |      * @param appKey  AppKey of the device (32 character string with hexadecimal data) | ||||||
|      * @return `true` if the activation was succesful, `false` if the activation failed |      * @return `true` if the activation was succesful, `false` if the activation failed | ||||||
|      */ |      */ | ||||||
|     bool join(const char *devEui, const char *appEui, const char *appKey) { return ttn_join(devEui, appEui, appKey); } |     bool join(const char *devEui, const char *appEui, const char *appKey) | ||||||
|  |     { | ||||||
|  |         return ttn_join(devEui, appEui, appKey); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Transmits a message |      * @brief Transmits a message | ||||||
| @ -570,7 +595,8 @@ public: | |||||||
|      * @param length   number of bytes to be transmitted |      * @param length   number of bytes to be transmitted | ||||||
|      * @param port     port (defaults to 1) |      * @param port     port (defaults to 1) | ||||||
|      * @param confirm  flag indicating if a confirmation should be requested. Defaults to `false` |      * @param confirm  flag indicating if a confirmation should be requested. Defaults to `false` | ||||||
|      * @return @ref kTTNSuccessfulTransmission for successful transmission, @ref kTTNErrorTransmissionFailed for failed transmission, @ref kTTNErrorUnexpected for unexpected error |      * @return @ref kTTNSuccessfulTransmission for successful transmission, @ref kTTNErrorTransmissionFailed for failed | ||||||
|  |      * transmission, @ref kTTNErrorUnexpected for unexpected error | ||||||
|      */ |      */ | ||||||
|     TTNResponseCode transmitMessage(const uint8_t *payload, size_t length, ttn_port_t port = 1, bool confirm = false) |     TTNResponseCode transmitMessage(const uint8_t *payload, size_t length, ttn_port_t port = 1, bool confirm = false) | ||||||
|     { |     { | ||||||
| @ -591,7 +617,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @param callback  the callback function |      * @param callback  the callback function | ||||||
|      */ |      */ | ||||||
|     void onMessage(TTNMessageCallback callback) { ttn_on_message(callback); } |     void onMessage(TTNMessageCallback callback) | ||||||
|  |     { | ||||||
|  |         ttn_on_message(callback); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Checks if DevEUI, AppEUI/JoinEUI and AppKey have been stored in non-volatile storage |      * @brief Checks if DevEUI, AppEUI/JoinEUI and AppKey have been stored in non-volatile storage | ||||||
| @ -599,7 +628,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @return `true` if they are stored, complete and of the correct size, `false` otherwise |      * @return `true` if they are stored, complete and of the correct size, `false` otherwise | ||||||
|      */ |      */ | ||||||
|     bool isProvisioned() { return ttn_is_provisioned(); } |     bool isProvisioned() | ||||||
|  |     { | ||||||
|  |         return ttn_is_provisioned(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Sets the RSSI calibration value for LBT (Listen Before Talk). |      * @brief Sets the RSSI calibration value for LBT (Listen Before Talk). | ||||||
| @ -610,14 +642,20 @@ public: | |||||||
|      * |      * | ||||||
|      * @param rssiCal RSSI calibration value, in dB |      * @param rssiCal RSSI calibration value, in dB | ||||||
|      */ |      */ | ||||||
|     void setRSSICal(int8_t rssiCal) { ttn_set_rssi_cal(rssiCal); } |     void setRSSICal(int8_t rssiCal) | ||||||
|  |     { | ||||||
|  |         ttn_set_rssi_cal(rssiCal); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Returns whether Adaptive Data Rate (ADR) is enabled. |      * Returns whether Adaptive Data Rate (ADR) is enabled. | ||||||
|      * |      * | ||||||
|      * @return `true` if enabled, `false` if disabled |      * @return `true` if enabled, `false` if disabled | ||||||
|      */ |      */ | ||||||
|     bool adrEnabled() { return ttn_adr_enabled(); } |     bool adrEnabled() | ||||||
|  |     { | ||||||
|  |         return ttn_adr_enabled(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Enables or disabled Adaptive Data Rate (ADR). |      * @brief Enables or disabled Adaptive Data Rate (ADR). | ||||||
| @ -627,7 +665,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @param enabled `true` to enable, `false` to disable |      * @param enabled `true` to enable, `false` to disable | ||||||
|      */ |      */ | ||||||
|     void setAdrEnabled(bool enabled) { ttn_set_adr_enabled(enabled); } |     void setAdrEnabled(bool enabled) | ||||||
|  |     { | ||||||
|  |         ttn_set_adr_enabled(enabled); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Sets the transmission data rate (i.e. the data rate for uplink messages). |      * @brief Sets the transmission data rate (i.e. the data rate for uplink messages). | ||||||
| @ -637,7 +678,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @param data_rate data rate (use constants of enum @ref TTNDataRate) |      * @param data_rate data rate (use constants of enum @ref TTNDataRate) | ||||||
|      */ |      */ | ||||||
|     void setDataRate(TTNDataRate data_rate) { ttn_set_data_rate(static_cast<ttn_data_rate_t>(data_rate)); } |     void setDataRate(TTNDataRate data_rate) | ||||||
|  |     { | ||||||
|  |         ttn_set_data_rate(static_cast<ttn_data_rate_t>(data_rate)); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Sets the maximum power for transmission |      * @brief Sets the maximum power for transmission | ||||||
| @ -648,7 +692,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @param tx_pow power, in dBm |      * @param tx_pow power, in dBm | ||||||
|      */ |      */ | ||||||
|     void setMaxTxPower(int tx_pow) { ttn_set_max_tx_pow(tx_pow); } |     void setMaxTxPower(int tx_pow) | ||||||
|  |     { | ||||||
|  |         ttn_set_max_tx_pow(tx_pow); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Stops all activies and shuts down the RF module and the background tasks. |      * @brief Stops all activies and shuts down the RF module and the background tasks. | ||||||
| @ -656,20 +703,29 @@ public: | |||||||
|      * To restart communication, @ref startup() and @ref join() must be called. |      * To restart communication, @ref startup() and @ref join() must be called. | ||||||
|      * it neither clears the provisioned keys nor the configured pins. |      * it neither clears the provisioned keys nor the configured pins. | ||||||
|      */ |      */ | ||||||
|     void shutdown() { ttn_shutdown(); } |     void shutdown() | ||||||
|  |     { | ||||||
|  |         ttn_shutdown(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Restarts the background tasks and RF module. |      * @brief Restarts the background tasks and RF module. | ||||||
|      * |      * | ||||||
|      * This member function must only be called after a call to shutdowna(). |      * This member function must only be called after a call to shutdowna(). | ||||||
|      */ |      */ | ||||||
|     void startup() { ttn_startup(); } |     void startup() | ||||||
|  |     { | ||||||
|  |         ttn_startup(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Gets current RX/TX window |      * @brief Gets current RX/TX window | ||||||
|      * @return window |      * @return window | ||||||
|      */ |      */ | ||||||
|     TTNRxTxWindow rxTxWindow() { return static_cast<TTNRxTxWindow>(ttn_rx_tx_window()); } |     TTNRxTxWindow rxTxWindow() | ||||||
|  |     { | ||||||
|  |         return static_cast<TTNRxTxWindow>(ttn_rx_tx_window()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Gets the RF settings for the specified window |      * @brief Gets the RF settings for the specified window | ||||||
| @ -681,19 +737,28 @@ public: | |||||||
|      * @brief Gets the RF settings of the last (or ongoing) transmission. |      * @brief Gets the RF settings of the last (or ongoing) transmission. | ||||||
|      * @return RF settings |      * @return RF settings | ||||||
|      */ |      */ | ||||||
|     TTNRFSettings txSettings() { return getRFSettings(kTTNTxWindow); } |     TTNRFSettings txSettings() | ||||||
|  |     { | ||||||
|  |         return getRFSettings(kTTNTxWindow); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 1. |      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 1. | ||||||
|      * @return RF settings |      * @return RF settings | ||||||
|      */ |      */ | ||||||
|     TTNRFSettings rx1Settings() { return getRFSettings(kTTNRx1Window); } |     TTNRFSettings rx1Settings() | ||||||
|  |     { | ||||||
|  |         return getRFSettings(kTTNRx1Window); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 2. |      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 2. | ||||||
|      * @return RF settings |      * @return RF settings | ||||||
|      */ |      */ | ||||||
|     TTNRFSettings rx2Settings() { return getRFSettings(kTTNRx2Window); } |     TTNRFSettings rx2Settings() | ||||||
|  |     { | ||||||
|  |         return getRFSettings(kTTNRx2Window); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Gets the received signal strength indicator (RSSI). |      * @brief Gets the received signal strength indicator (RSSI). | ||||||
| @ -702,7 +767,10 @@ public: | |||||||
|      * |      * | ||||||
|      * @return RSSI, in dBm |      * @return RSSI, in dBm | ||||||
|      */ |      */ | ||||||
|     int rssi() { return ttn_rssi(); } |     int rssi() | ||||||
|  |     { | ||||||
|  |         return ttn_rssi(); | ||||||
|  |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  | |||||||
							
								
								
									
										190
									
								
								include/ttn.h
									
									
									
									
									
								
							
							
						
						
									
										190
									
								
								include/ttn.h
									
									
									
									
									
								
							| @ -17,12 +17,12 @@ | |||||||
| #ifndef TTN_C_H | #ifndef TTN_C_H | ||||||
| #define TTN_C_H | #define TTN_C_H | ||||||
|  |  | ||||||
| #include <stdint.h> |  | ||||||
| #include "driver/spi_master.h" | #include "driver/spi_master.h" | ||||||
|  | #include <stdint.h> | ||||||
|  |  | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
| extern "C" { | extern "C" | ||||||
|  | { | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @ -31,23 +31,21 @@ extern "C" { | |||||||
|  * @{ |  * @{ | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief Constant for indicating that a pin is not connected |  * @brief Constant for indicating that a pin is not connected | ||||||
|  */ |  */ | ||||||
| #define TTN_NOT_CONNECTED 0xff | #define TTN_NOT_CONNECTED 0xff | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief Integer data type for specifiying the port of an uplink or downlink message. |      * @brief Integer data type for specifiying the port of an uplink or downlink message. | ||||||
|      */ |      */ | ||||||
| typedef uint8_t ttn_port_t; |     typedef uint8_t ttn_port_t; | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Response codes |      * @brief Response codes | ||||||
|      */ |      */ | ||||||
| typedef enum |     typedef enum | ||||||
| { |     { | ||||||
|         /** @brief Transmission failed error */ |         /** @brief Transmission failed error */ | ||||||
|         TTN_ERROR_TRANSMISSION_FAILED = -1, |         TTN_ERROR_TRANSMISSION_FAILED = -1, | ||||||
|         /** @brief Unexpected or internal error */ |         /** @brief Unexpected or internal error */ | ||||||
| @ -56,14 +54,13 @@ typedef enum | |||||||
|         TTN_SUCCESSFUL_TRANSMISSION = 1, |         TTN_SUCCESSFUL_TRANSMISSION = 1, | ||||||
|         /** @brief Successful receipt of a downlink message */ |         /** @brief Successful receipt of a downlink message */ | ||||||
|         TTN_SUCCESSFUL_RECEIVE = 2 |         TTN_SUCCESSFUL_RECEIVE = 2 | ||||||
| } ttn_response_code_t; |     } ttn_response_code_t; | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief RX/TX window |      * @brief RX/TX window | ||||||
|      */ |      */ | ||||||
| typedef enum |     typedef enum | ||||||
| { |     { | ||||||
|         /** |         /** | ||||||
|          * @brief Outside RX/TX window |          * @brief Outside RX/TX window | ||||||
|          */ |          */ | ||||||
| @ -80,14 +77,13 @@ typedef enum | |||||||
|          * @brief Reception window 2 |          * @brief Reception window 2 | ||||||
|          */ |          */ | ||||||
|         TTN_WINDOW_RX2 = 3 |         TTN_WINDOW_RX2 = 3 | ||||||
| } ttn_rx_tx_window_t; |     } ttn_rx_tx_window_t; | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief Spreading Factor |      * @brief Spreading Factor | ||||||
|      */ |      */ | ||||||
| typedef enum |     typedef enum | ||||||
| { |     { | ||||||
|         /** |         /** | ||||||
|          * @brief Unused / undefined spreading factor |          * @brief Unused / undefined spreading factor | ||||||
|          */ |          */ | ||||||
| @ -120,14 +116,13 @@ typedef enum | |||||||
|          * @brief Spreading Factor 12 (SF12) |          * @brief Spreading Factor 12 (SF12) | ||||||
|          */ |          */ | ||||||
|         TTN_SF12 = 7 |         TTN_SF12 = 7 | ||||||
| } ttn_spreading_factor_t; |     } ttn_spreading_factor_t; | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief Bandwidth |      * @brief Bandwidth | ||||||
|      */ |      */ | ||||||
| typedef enum |     typedef enum | ||||||
| { |     { | ||||||
|         /** |         /** | ||||||
|          * @brief Undefined/unused bandwidth |          * @brief Undefined/unused bandwidth | ||||||
|          */ |          */ | ||||||
| @ -144,17 +139,16 @@ typedef enum | |||||||
|          * @brief Bandwidth of 500 kHz |          * @brief Bandwidth of 500 kHz | ||||||
|          */ |          */ | ||||||
|         TTN_BW_500 = 3 |         TTN_BW_500 = 3 | ||||||
| } ttn_bandwidth_t; |     } ttn_bandwidth_t; | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief Data Rate |      * @brief Data Rate | ||||||
|      * |      * | ||||||
|      * Note that the spreading factor, bandwidth, bit rate and maximum message |      * Note that the spreading factor, bandwidth, bit rate and maximum message | ||||||
|      * size associated with each data rate depends on the region. |      * size associated with each data rate depends on the region. | ||||||
|      */ |      */ | ||||||
| typedef enum |     typedef enum | ||||||
| { |     { | ||||||
|         /** |         /** | ||||||
|          * @brief Data rate for region AS923 using SF12 and 125 kHz bandwidth. |          * @brief Data rate for region AS923 using SF12 and 125 kHz bandwidth. | ||||||
|          */ |          */ | ||||||
| @ -401,14 +395,13 @@ typedef enum | |||||||
|          * @brief Default data rate for joining. |          * @brief Default data rate for joining. | ||||||
|          */ |          */ | ||||||
|         TTN_DR_JOIN_DEFAULT = 255 |         TTN_DR_JOIN_DEFAULT = 255 | ||||||
| } ttn_data_rate_t; |     } ttn_data_rate_t; | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief RF settings for TX or RX |      * @brief RF settings for TX or RX | ||||||
|      */ |      */ | ||||||
| typedef struct |     typedef struct | ||||||
| { |     { | ||||||
|         /** |         /** | ||||||
|          * @brief Spreading Factor (SF) |          * @brief Spreading Factor (SF) | ||||||
|          */ |          */ | ||||||
| @ -421,35 +414,33 @@ typedef struct | |||||||
|          * @brief Frequency, in Hz |          * @brief Frequency, in Hz | ||||||
|          */ |          */ | ||||||
|         uint32_t frequency; |         uint32_t frequency; | ||||||
| } ttn_rf_settings_t; |     } ttn_rf_settings_t; | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief Callback for recieved messages |      * @brief Callback for recieved messages | ||||||
|      * |      * | ||||||
|      * @param payload  pointer to the received bytes |      * @param payload  pointer to the received bytes | ||||||
|      * @param length   number of received bytes |      * @param length   number of received bytes | ||||||
|      * @param port     port the message was received on |      * @param port     port the message was received on | ||||||
|      */ |      */ | ||||||
| typedef void (*ttn_message_cb)(const uint8_t* payload, size_t length, ttn_port_t port); |     typedef void (*ttn_message_cb)(const uint8_t *payload, size_t length, ttn_port_t port); | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief Initializes The Things Network device instance. |      * @brief Initializes The Things Network device instance. | ||||||
|      * |      * | ||||||
|      * Call this function once at the start of the program and before all other TTN functions. |      * Call this function once at the start of the program and before all other TTN functions. | ||||||
|      */ |      */ | ||||||
| void ttn_init(void); |     void ttn_init(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Resets the LoRaWAN radio. |      * @brief Resets the LoRaWAN radio. | ||||||
|      * |      * | ||||||
|      * To restart communication, @ref ttn_join() or @ref ttn_join_provisioned() must be called. |      * To restart communication, @ref ttn_join() or @ref ttn_join_provisioned() must be called. | ||||||
|      * It neither clears the provisioned keys nor the configured pins. |      * It neither clears the provisioned keys nor the configured pins. | ||||||
|      */ |      */ | ||||||
| void ttn_reset(void); |     void ttn_reset(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Configures the pins used to communicate with the LoRaWAN radio chip. |      * @brief Configures the pins used to communicate with the LoRaWAN radio chip. | ||||||
|      * |      * | ||||||
|      * Before calling this member function, the SPI bus needs to be configured using `spi_bus_initialize()`. |      * Before calling this member function, the SPI bus needs to be configured using `spi_bus_initialize()`. | ||||||
| @ -457,14 +448,17 @@ void ttn_reset(void); | |||||||
|      * |      * | ||||||
|      * @param spi_host  The SPI bus/peripherial to use (`SPI_HOST`, `HSPI_HOST` or `VSPI_HOST`). |      * @param spi_host  The SPI bus/peripherial to use (`SPI_HOST`, `HSPI_HOST` or `VSPI_HOST`). | ||||||
|      * @param nss       The GPIO pin number connected to the radio chip's NSS pin (serving as the SPI chip select) |      * @param nss       The GPIO pin number connected to the radio chip's NSS pin (serving as the SPI chip select) | ||||||
|  * @param rxtx      The GPIO pin number connected to the radio chip's RXTX pin (@ref TTN_NOT_CONNECTED if not connected) |      * @param rxtx      The GPIO pin number connected to the radio chip's RXTX pin (@ref TTN_NOT_CONNECTED if not | ||||||
|  * @param rst       The GPIO pin number connected to the radio chip's RST pin (@ref TTN_NOT_CONNECTED if not connected) |      * connected) | ||||||
|  |      * @param rst       The GPIO pin number connected to the radio chip's RST pin (@ref TTN_NOT_CONNECTED if not | ||||||
|  |      * connected) | ||||||
|      * @param dio0      The GPIO pin number connected to the radio chip's DIO0 pin |      * @param dio0      The GPIO pin number connected to the radio chip's DIO0 pin | ||||||
|      * @param dio1      The GPIO pin number connected to the radio chip's DIO1 pin |      * @param dio1      The GPIO pin number connected to the radio chip's DIO1 pin | ||||||
|      */ |      */ | ||||||
| void ttn_configure_pins(spi_host_device_t spi_host, uint8_t nss, uint8_t rxtx, uint8_t rst, uint8_t dio0, uint8_t dio1); |     void ttn_configure_pins(spi_host_device_t spi_host, uint8_t nss, uint8_t rxtx, uint8_t rst, uint8_t dio0, | ||||||
|  |                             uint8_t dio1); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the frequency sub-band to be used. |      * @brief Sets the frequency sub-band to be used. | ||||||
|      * |      * | ||||||
|      * For regions with sub-bands (USA, Australia), sets the sub-band to be used for uplink communication. |      * For regions with sub-bands (USA, Australia), sets the sub-band to be used for uplink communication. | ||||||
| @ -476,9 +470,9 @@ void ttn_configure_pins(spi_host_device_t spi_host, uint8_t nss, uint8_t rxtx, u | |||||||
|      * |      * | ||||||
|      * @param band band (0 for all bands, or value between 1 and 8) |      * @param band band (0 for all bands, or value between 1 and 8) | ||||||
|      */ |      */ | ||||||
| void ttn_set_subband(int band); |     void ttn_set_subband(int band); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the credentials needed to activate the device via OTAA, without activating it. |      * @brief Sets the credentials needed to activate the device via OTAA, without activating it. | ||||||
|      * |      * | ||||||
|      * The provided DevEUI, AppEUI/JoinEUI and AppKey are saved in non-volatile memory. Before |      * The provided DevEUI, AppEUI/JoinEUI and AppKey are saved in non-volatile memory. Before | ||||||
| @ -491,9 +485,9 @@ void ttn_set_subband(int band); | |||||||
|      * @param app_key  AppKey of the device (32 character string with hexadecimal data) |      * @param app_key  AppKey of the device (32 character string with hexadecimal data) | ||||||
|      * @return `true` if the provisioning was successful, `false`  if the provisioning failed |      * @return `true` if the provisioning was successful, `false`  if the provisioning failed | ||||||
|      */ |      */ | ||||||
| bool ttn_provision(const char *dev_eui, const char *app_eui, const char *app_key); |     bool ttn_provision(const char *dev_eui, const char *app_eui, const char *app_key); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the information needed to activate the device via OTAA, using the MAC to generate the DevEUI |      * @brief Sets the information needed to activate the device via OTAA, using the MAC to generate the DevEUI | ||||||
|      * and without activating it. |      * and without activating it. | ||||||
|      * |      * | ||||||
| @ -515,25 +509,25 @@ bool ttn_provision(const char *dev_eui, const char *app_eui, const char *app_key | |||||||
|      * @param app_key  AppKey of the device (32 character string with hexadecimal data) |      * @param app_key  AppKey of the device (32 character string with hexadecimal data) | ||||||
|      * @return `true` if the provisioning was successful, `false`  if the provisioning failed |      * @return `true` if the provisioning was successful, `false`  if the provisioning failed | ||||||
|      */ |      */ | ||||||
| bool ttn_provision_with_mac(const char *app_eui, const char *app_key); |     bool ttn_provision_with_mac(const char *app_eui, const char *app_key); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Starts task listening on configured UART for AT commands. |      * @brief Starts task listening on configured UART for AT commands. | ||||||
|      * |      * | ||||||
|      * Run `make menuconfig` to configure it. |      * Run `make menuconfig` to configure it. | ||||||
|      */ |      */ | ||||||
| void ttn_start_provisioning_task(void); |     void ttn_start_provisioning_task(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Waits until the DevEUI, AppEUI/JoinEUI and AppKey have been provisioned |      * @brief Waits until the DevEUI, AppEUI/JoinEUI and AppKey have been provisioned | ||||||
|      * by the provisioning task. |      * by the provisioning task. | ||||||
|      * |      * | ||||||
|      * If the device has already been provisioned (stored data in NVS, call of provision() |      * If the device has already been provisioned (stored data in NVS, call of provision() | ||||||
|      * or call of @ref ttn_join(), this function immediately returns. |      * or call of @ref ttn_join(), this function immediately returns. | ||||||
|      */ |      */ | ||||||
| void ttn_wait_for_provisioning(void); |     void ttn_wait_for_provisioning(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Activates the device via OTAA. |      * @brief Activates the device via OTAA. | ||||||
|      * |      * | ||||||
|      * The DevEUI, AppEUI/JoinEUI and AppKey must have already been provisioned by a call to provision(). |      * The DevEUI, AppEUI/JoinEUI and AppKey must have already been provisioned by a call to provision(). | ||||||
| @ -543,9 +537,9 @@ void ttn_wait_for_provisioning(void); | |||||||
|      * |      * | ||||||
|      * @return `true` if the activation was succesful, `false` if the activation failed |      * @return `true` if the activation was succesful, `false` if the activation failed | ||||||
|      */ |      */ | ||||||
| bool ttn_join_provisioned(void); |     bool ttn_join_provisioned(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the DevEUI, AppEUI/JoinEUI and AppKey and activate the device via OTAA. |      * @brief Sets the DevEUI, AppEUI/JoinEUI and AppKey and activate the device via OTAA. | ||||||
|      * |      * | ||||||
|      * The DevEUI, AppEUI/JoinEUI and AppKey are NOT saved in non-volatile memory. |      * The DevEUI, AppEUI/JoinEUI and AppKey are NOT saved in non-volatile memory. | ||||||
| @ -557,9 +551,9 @@ bool ttn_join_provisioned(void); | |||||||
|      * @param app_key  AppKey of the device (32 character string with hexadecimal data) |      * @param app_key  AppKey of the device (32 character string with hexadecimal data) | ||||||
|      * @return `true` if the activation was succesful, `false` if the activation failed |      * @return `true` if the activation was succesful, `false` if the activation failed | ||||||
|      */ |      */ | ||||||
| bool ttn_join(const char *dev_eui, const char *app_eui, const char *app_key); |     bool ttn_join(const char *dev_eui, const char *app_eui, const char *app_key); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Transmits a message |      * @brief Transmits a message | ||||||
|      * |      * | ||||||
|      * The function blocks until the message could be transmitted and a message has been received |      * The function blocks until the message could be transmitted and a message has been received | ||||||
| @ -570,11 +564,12 @@ bool ttn_join(const char *dev_eui, const char *app_eui, const char *app_key); | |||||||
|      * @param length   number of bytes to be transmitted |      * @param length   number of bytes to be transmitted | ||||||
|      * @param port     port (use 1 as default) |      * @param port     port (use 1 as default) | ||||||
|      * @param confirm  flag indicating if a confirmation should be requested (use `false` as default) |      * @param confirm  flag indicating if a confirmation should be requested (use `false` as default) | ||||||
|  * @return @ref TTN_SUCCESSFUL_TRANSMISSION for successful transmission, @ref TTN_ERROR_TRANSMISSION_FAILED for failed transmission, @ref TTN_ERROR_UNEXPECTED for unexpected error |      * @return @ref TTN_SUCCESSFUL_TRANSMISSION for successful transmission, @ref TTN_ERROR_TRANSMISSION_FAILED for | ||||||
|  |      * failed transmission, @ref TTN_ERROR_UNEXPECTED for unexpected error | ||||||
|      */ |      */ | ||||||
| ttn_response_code_t ttn_transmit_message(const uint8_t *payload, size_t length, ttn_port_t port, bool confirm); |     ttn_response_code_t ttn_transmit_message(const uint8_t *payload, size_t length, ttn_port_t port, bool confirm); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the function to be called when a message is received |      * @brief Sets the function to be called when a message is received | ||||||
|      * |      * | ||||||
|      * When a message is received, the specified function is called. The |      * When a message is received, the specified function is called. The | ||||||
| @ -588,17 +583,17 @@ ttn_response_code_t ttn_transmit_message(const uint8_t *payload, size_t length, | |||||||
|      * |      * | ||||||
|      * @param callback  the callback function |      * @param callback  the callback function | ||||||
|      */ |      */ | ||||||
| void ttn_on_message(ttn_message_cb callback); |     void ttn_on_message(ttn_message_cb callback); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Checks if DevEUI, AppEUI/JoinEUI and AppKey have been stored in non-volatile storage |      * @brief Checks if DevEUI, AppEUI/JoinEUI and AppKey have been stored in non-volatile storage | ||||||
|      * or have been provided as by a call to @ref ttn_join(). |      * or have been provided as by a call to @ref ttn_join(). | ||||||
|      * |      * | ||||||
|      * @return `true` if they are stored, complete and of the correct size, `false` otherwise |      * @return `true` if they are stored, complete and of the correct size, `false` otherwise | ||||||
|      */ |      */ | ||||||
| bool ttn_is_provisioned(void); |     bool ttn_is_provisioned(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the RSSI calibration value for LBT (Listen Before Talk). |      * @brief Sets the RSSI calibration value for LBT (Listen Before Talk). | ||||||
|      * |      * | ||||||
|      * This value is added to RSSI measured prior to decision. It must include the guardband. |      * This value is added to RSSI measured prior to decision. It must include the guardband. | ||||||
| @ -607,16 +602,16 @@ bool ttn_is_provisioned(void); | |||||||
|      * |      * | ||||||
|      * @param rssi_cal RSSI calibration value, in dB |      * @param rssi_cal RSSI calibration value, in dB | ||||||
|      */ |      */ | ||||||
| void ttn_set_rssi_cal(int8_t rssi_cal); |     void ttn_set_rssi_cal(int8_t rssi_cal); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * Returns whether Adaptive Data Rate (ADR) is enabled. |      * Returns whether Adaptive Data Rate (ADR) is enabled. | ||||||
|      * |      * | ||||||
|      * @return `true` if enabled, `false` if disabled |      * @return `true` if enabled, `false` if disabled | ||||||
|      */ |      */ | ||||||
| bool ttn_adr_enabled(void); |     bool ttn_adr_enabled(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Enables or disabled Adaptive Data Rate (ADR). |      * @brief Enables or disabled Adaptive Data Rate (ADR). | ||||||
|      * |      * | ||||||
|      * ADR is enabled by default. It optimizes data rate, airtime and energy consumption |      * ADR is enabled by default. It optimizes data rate, airtime and energy consumption | ||||||
| @ -624,9 +619,9 @@ bool ttn_adr_enabled(void); | |||||||
|      * |      * | ||||||
|      * @param enabled `true` to enable, `false` to disable |      * @param enabled `true` to enable, `false` to disable | ||||||
|      */ |      */ | ||||||
| void ttn_set_adr_enabled(bool enabled); |     void ttn_set_adr_enabled(bool enabled); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the transmission data rate (i.e. the data rate for uplink messages). |      * @brief Sets the transmission data rate (i.e. the data rate for uplink messages). | ||||||
|      * |      * | ||||||
|      * If ADR is enabled, it's is used as the initial data rate and later adjusted depending |      * If ADR is enabled, it's is used as the initial data rate and later adjusted depending | ||||||
| @ -634,9 +629,9 @@ void ttn_set_adr_enabled(bool enabled); | |||||||
|      * |      * | ||||||
|      * @param data_rate data rate (use constants of enum @ref ttn_data_rate_t) |      * @param data_rate data rate (use constants of enum @ref ttn_data_rate_t) | ||||||
|      */ |      */ | ||||||
| void ttn_set_data_rate(ttn_data_rate_t data_rate); |     void ttn_set_data_rate(ttn_data_rate_t data_rate); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Sets the maximum power for transmission |      * @brief Sets the maximum power for transmission | ||||||
|      * |      * | ||||||
|      * The power is specified in dBm and sets the power emitted by the radio. |      * The power is specified in dBm and sets the power emitted by the radio. | ||||||
| @ -645,71 +640,68 @@ void ttn_set_data_rate(ttn_data_rate_t data_rate); | |||||||
|      * |      * | ||||||
|      * @param tx_pow power, in dBm |      * @param tx_pow power, in dBm | ||||||
|      */ |      */ | ||||||
| void ttn_set_max_tx_pow(int tx_pow); |     void ttn_set_max_tx_pow(int tx_pow); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Stops all activies and shuts down the RF module and the background tasks. |      * @brief Stops all activies and shuts down the RF module and the background tasks. | ||||||
|      * |      * | ||||||
|      * To restart communication, @ref ttn_startup() and @ref ttn_join() must be called. |      * To restart communication, @ref ttn_startup() and @ref ttn_join() must be called. | ||||||
|      * it neither clears the provisioned keys nor the configured pins. |      * it neither clears the provisioned keys nor the configured pins. | ||||||
|      */ |      */ | ||||||
| void ttn_shutdown(void); |     void ttn_shutdown(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Restarts the background tasks and RF module. |      * @brief Restarts the background tasks and RF module. | ||||||
|      * |      * | ||||||
|      * This member function must only be called after a call to shutdowna(). |      * This member function must only be called after a call to shutdowna(). | ||||||
|      */ |      */ | ||||||
| void ttn_startup(void); |     void ttn_startup(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Gets current RX/TX window |      * @brief Gets current RX/TX window | ||||||
|      * @return window |      * @return window | ||||||
|      */ |      */ | ||||||
| ttn_rx_tx_window_t ttn_rx_tx_window(void); |     ttn_rx_tx_window_t ttn_rx_tx_window(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Gets the RF settings for the specified window |      * @brief Gets the RF settings for the specified window | ||||||
|      * @param window RX/TX window (valid values are @ref TTN_WINDOW_TX, @ref TTN_WINDOW_RX1 and @ref TTN_WINDOW_RX2 |      * @param window RX/TX window (valid values are @ref TTN_WINDOW_TX, @ref TTN_WINDOW_RX1 and @ref TTN_WINDOW_RX2 | ||||||
|      */ |      */ | ||||||
| ttn_rf_settings_t ttn_get_rf_settings(ttn_rx_tx_window_t window); |     ttn_rf_settings_t ttn_get_rf_settings(ttn_rx_tx_window_t window); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Gets the RF settings of the last (or ongoing) transmission. |      * @brief Gets the RF settings of the last (or ongoing) transmission. | ||||||
|      * @return RF settings |      * @return RF settings | ||||||
|      */ |      */ | ||||||
| ttn_rf_settings_t ttn_tx_settings(void); |     ttn_rf_settings_t ttn_tx_settings(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 1. |      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 1. | ||||||
|      * @return RF settings |      * @return RF settings | ||||||
|      */ |      */ | ||||||
| ttn_rf_settings_t ttn_rx1_settings(void); |     ttn_rf_settings_t ttn_rx1_settings(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 2. |      * @brief Gets the RF settings of the last (or ongoing) reception of RX window 2. | ||||||
|      * @return RF settings |      * @return RF settings | ||||||
|      */ |      */ | ||||||
| ttn_rf_settings_t ttn_rx2_settings(void); |     ttn_rf_settings_t ttn_rx2_settings(void); | ||||||
|  |  | ||||||
| /** |     /** | ||||||
|      * @brief Gets the received signal strength indicator (RSSI). |      * @brief Gets the received signal strength indicator (RSSI). | ||||||
|      * |      * | ||||||
|      * RSSI is the measured signal strength of the last recevied message (incl. join responses). |      * RSSI is the measured signal strength of the last recevied message (incl. join responses). | ||||||
|      * |      * | ||||||
|      * @return RSSI, in dBm |      * @return RSSI, in dBm | ||||||
|      */ |      */ | ||||||
| int ttn_rssi(); |     int ttn_rssi(); | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @} |      * @} | ||||||
|      */ |      */ | ||||||
|  |  | ||||||
|  |  | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -12,7 +12,6 @@ | |||||||
|  |  | ||||||
| #include "TheThingsNetwork.h" | #include "TheThingsNetwork.h" | ||||||
|  |  | ||||||
|  |  | ||||||
| TTNRFSettings TheThingsNetwork::getRFSettings(TTNRxTxWindow window) | TTNRFSettings TheThingsNetwork::getRFSettings(TTNRxTxWindow window) | ||||||
| { | { | ||||||
|     ttn_rf_settings_t settings = ttn_get_rf_settings(static_cast<ttn_rx_tx_window_t>(window)); |     ttn_rf_settings_t settings = ttn_get_rf_settings(static_cast<ttn_rx_tx_window_t>(window)); | ||||||
|  | |||||||
| @ -17,7 +17,8 @@ | |||||||
| #elif defined(CONFIG_TTN_LORA_FREQ_US_915) | #elif defined(CONFIG_TTN_LORA_FREQ_US_915) | ||||||
| #define CFG_us915 1 | #define CFG_us915 1 | ||||||
| #elif defined(CONFIG_TTN_LORA_FREQ_AU_921) | #elif defined(CONFIG_TTN_LORA_FREQ_AU_921) | ||||||
| #   warning "CONFIG_TTN_LORA_FREQ_AU_921 was deprecated in favour of CONFIG_TTN_LORA_FREQ_AU_921. Support for CONFIG_TTN_LORA_FREQ_AU_921 will be removed in the future." | #warning                                                                                                               \ | ||||||
|  |     "CONFIG_TTN_LORA_FREQ_AU_921 was deprecated in favour of CONFIG_TTN_LORA_FREQ_AU_921. Support for CONFIG_TTN_LORA_FREQ_AU_921 will be removed in the future." | ||||||
| #define CFG_au915 1 | #define CFG_au915 1 | ||||||
| #elif defined(CONFIG_TTN_LORA_FREQ_AU_915) | #elif defined(CONFIG_TTN_LORA_FREQ_AU_915) | ||||||
| #define CFG_au915 1 | #define CFG_au915 1 | ||||||
| @ -50,7 +51,6 @@ | |||||||
| #endif | #endif | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| // 16 μs per tick | // 16 μs per tick | ||||||
| // LMIC requires ticks to be 15.5μs - 100 μs long | // LMIC requires ticks to be 15.5μs - 100 μs long | ||||||
| #define US_PER_OSTICK 16 | #define US_PER_OSTICK 16 | ||||||
|  | |||||||
							
								
								
									
										56
									
								
								src/ttn.c
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								src/ttn.c
									
									
									
									
									
								
							| @ -10,15 +10,14 @@ | |||||||
|  * High-level C API for ttn-esp32. |  * High-level C API for ttn-esp32. | ||||||
|  *******************************************************************************/ |  *******************************************************************************/ | ||||||
|  |  | ||||||
| #include "lmic/lmic.h" |  | ||||||
| #include "ttn.h" | #include "ttn.h" | ||||||
| #include "ttn_provisioning.h" |  | ||||||
| #include "ttn_logging.h" |  | ||||||
| #include "hal/hal_esp32.h" |  | ||||||
| #include "freertos/FreeRTOS.h" |  | ||||||
| #include "esp_event.h" | #include "esp_event.h" | ||||||
| #include "esp_log.h" | #include "esp_log.h" | ||||||
|  | #include "freertos/FreeRTOS.h" | ||||||
|  | #include "hal/hal_esp32.h" | ||||||
|  | #include "lmic/lmic.h" | ||||||
|  | #include "ttn_logging.h" | ||||||
|  | #include "ttn_provisioning.h" | ||||||
|  |  | ||||||
| #define TAG "ttn" | #define TAG "ttn" | ||||||
|  |  | ||||||
| @ -37,7 +36,8 @@ typedef enum | |||||||
| /** | /** | ||||||
|  * @brief Event type |  * @brief Event type | ||||||
|  */ |  */ | ||||||
| typedef enum { | typedef enum | ||||||
|  | { | ||||||
|     TTN_EVENT_NONE, |     TTN_EVENT_NONE, | ||||||
|     TTN_EVNT_JOIN_COMPLETED, |     TTN_EVNT_JOIN_COMPLETED, | ||||||
|     TTN_EVENT_JOIN_FAILED, |     TTN_EVENT_JOIN_FAILED, | ||||||
| @ -49,10 +49,11 @@ typedef enum { | |||||||
| /** | /** | ||||||
|  * @brief Event message sent from LMIC task to waiting client task |  * @brief Event message sent from LMIC task to waiting client task | ||||||
|  */ |  */ | ||||||
| typedef struct { | typedef struct | ||||||
|  | { | ||||||
|     ttn_event_t event; |     ttn_event_t event; | ||||||
|     uint8_t port; |     uint8_t port; | ||||||
|     const uint8_t* message; |     const uint8_t *message; | ||||||
|     size_t message_size; |     size_t message_size; | ||||||
| } ttn_lmic_event_t; | } ttn_lmic_event_t; | ||||||
|  |  | ||||||
| @ -68,12 +69,11 @@ static int max_tx_power = DEFAULT_MAX_TX_POWER; | |||||||
|  |  | ||||||
| static bool join_core(void); | static bool join_core(void); | ||||||
| static void config_rf_params(void); | static void config_rf_params(void); | ||||||
| static void event_callback(void* user_data, ev_t event); | static void event_callback(void *user_data, ev_t event); | ||||||
| static void message_received_callback(void *user_data, uint8_t port, const uint8_t *message, size_t message_size); | static void message_received_callback(void *user_data, uint8_t port, const uint8_t *message, size_t message_size); | ||||||
| static void message_transmitted_callback(void *user_data, int success); | static void message_transmitted_callback(void *user_data, int success); | ||||||
| static void save_rf_settings(ttn_rf_settings_t* rf_settings); | static void save_rf_settings(ttn_rf_settings_t *rf_settings); | ||||||
| static void clear_rf_settings(ttn_rf_settings_t* rf_settings); | static void clear_rf_settings(ttn_rf_settings_t *rf_settings); | ||||||
|  |  | ||||||
|  |  | ||||||
| void ttn_init(void) | void ttn_init(void) | ||||||
| { | { | ||||||
| @ -152,7 +152,6 @@ bool ttn_provision_with_mac(const char *app_eui, const char *app_key) | |||||||
|     return ttn_provisioning_save_keys(); |     return ttn_provisioning_save_keys(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| void ttn_start_provisioning_task(void) | void ttn_start_provisioning_task(void) | ||||||
| { | { | ||||||
| #if defined(TTN_HAS_AT_COMMANDS) | #if defined(TTN_HAS_AT_COMMANDS) | ||||||
| @ -289,7 +288,6 @@ void ttn_on_message(ttn_message_cb callback) | |||||||
|     message_callback = callback; |     message_callback = callback; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| bool ttn_is_provisioned(void) | bool ttn_is_provisioned(void) | ||||||
| { | { | ||||||
|     if (ttn_provisioning_have_keys()) |     if (ttn_provisioning_have_keys()) | ||||||
| @ -376,19 +374,17 @@ int ttn_rssi(void) | |||||||
|     return LMIC.rssi; |     return LMIC.rssi; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // --- Callbacks --- | // --- Callbacks --- | ||||||
|  |  | ||||||
| #if CONFIG_LOG_DEFAULT_LEVEL >= 3 || LMIC_ENABLE_event_logging | #if CONFIG_LOG_DEFAULT_LEVEL >= 3 || LMIC_ENABLE_event_logging | ||||||
| static const char *event_names[] = { LMIC_EVENT_NAME_TABLE__INIT }; | static const char *event_names[] = {LMIC_EVENT_NAME_TABLE__INIT}; | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| // Called by LMIC when an LMIC event (join, join failed, reset etc.) occurs | // Called by LMIC when an LMIC event (join, join failed, reset etc.) occurs | ||||||
| void event_callback(void* user_data, ev_t event) | void event_callback(void *user_data, ev_t event) | ||||||
| { | { | ||||||
|     // update monitoring information |     // update monitoring information | ||||||
|     switch(event) |     switch (event) | ||||||
|     { |     { | ||||||
|     case EV_TXSTART: |     case EV_TXSTART: | ||||||
|         current_rx_tx_window = TTN_WINDOW_TX; |         current_rx_tx_window = TTN_WINDOW_TX; | ||||||
| @ -438,9 +434,7 @@ void event_callback(void* user_data, ev_t event) | |||||||
|     if (ttn_event == TTN_EVENT_NONE) |     if (ttn_event == TTN_EVENT_NONE) | ||||||
|         return; |         return; | ||||||
|  |  | ||||||
|     ttn_lmic_event_t result = { |     ttn_lmic_event_t result = {.event = ttn_event}; | ||||||
|         .event = ttn_event |  | ||||||
|     }; |  | ||||||
|     waiting_reason = TTN_WAITING_NONE; |     waiting_reason = TTN_WAITING_NONE; | ||||||
|     xQueueSend(lmic_event_queue, &result, pdMS_TO_TICKS(100)); |     xQueueSend(lmic_event_queue, &result, pdMS_TO_TICKS(100)); | ||||||
| } | } | ||||||
| @ -449,11 +443,7 @@ void event_callback(void* user_data, ev_t event) | |||||||
| void message_received_callback(void *user_data, uint8_t port, const uint8_t *message, size_t message_size) | void message_received_callback(void *user_data, uint8_t port, const uint8_t *message, size_t message_size) | ||||||
| { | { | ||||||
|     ttn_lmic_event_t result = { |     ttn_lmic_event_t result = { | ||||||
|         .event = TTN_EVENT_MESSAGE_RECEIVED, |         .event = TTN_EVENT_MESSAGE_RECEIVED, .port = port, .message = message, .message_size = message_size}; | ||||||
|         .port = port, |  | ||||||
|         .message = message, |  | ||||||
|         .message_size = message_size |  | ||||||
|     }; |  | ||||||
|     xQueueSend(lmic_event_queue, &result, pdMS_TO_TICKS(100)); |     xQueueSend(lmic_event_queue, &result, pdMS_TO_TICKS(100)); | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -461,24 +451,20 @@ void message_received_callback(void *user_data, uint8_t port, const uint8_t *mes | |||||||
| void message_transmitted_callback(void *user_data, int success) | void message_transmitted_callback(void *user_data, int success) | ||||||
| { | { | ||||||
|     waiting_reason = TTN_WAITING_NONE; |     waiting_reason = TTN_WAITING_NONE; | ||||||
|     ttn_lmic_event_t result = { |     ttn_lmic_event_t result = {.event = success ? TTN_EVENT_TRANSMISSION_COMPLETED : TTN_EVENT_TRANSMISSION_FAILED}; | ||||||
|         .event = success ? TTN_EVENT_TRANSMISSION_COMPLETED : TTN_EVENT_TRANSMISSION_FAILED |  | ||||||
|     }; |  | ||||||
|     xQueueSend(lmic_event_queue, &result, pdMS_TO_TICKS(100)); |     xQueueSend(lmic_event_queue, &result, pdMS_TO_TICKS(100)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // --- Helpers | // --- Helpers | ||||||
|  |  | ||||||
|  | void save_rf_settings(ttn_rf_settings_t *rf_settings) | ||||||
| void save_rf_settings(ttn_rf_settings_t* rf_settings) |  | ||||||
| { | { | ||||||
|     rf_settings->spreading_factor = (ttn_spreading_factor_t)(getSf(LMIC.rps) + 1); |     rf_settings->spreading_factor = (ttn_spreading_factor_t)(getSf(LMIC.rps) + 1); | ||||||
|     rf_settings->bandwidth = (ttn_bandwidth_t)(getBw(LMIC.rps) + 1); |     rf_settings->bandwidth = (ttn_bandwidth_t)(getBw(LMIC.rps) + 1); | ||||||
|     rf_settings->frequency = LMIC.freq; |     rf_settings->frequency = LMIC.freq; | ||||||
| } | } | ||||||
|  |  | ||||||
| void clear_rf_settings(ttn_rf_settings_t* rf_settings) | void clear_rf_settings(ttn_rf_settings_t *rf_settings) | ||||||
| { | { | ||||||
|     memset(rf_settings, 0, sizeof(*rf_settings)); |     memset(rf_settings, 0, sizeof(*rf_settings)); | ||||||
| } | } | ||||||
|  | |||||||
| @ -10,28 +10,26 @@ | |||||||
|  * Circular buffer for detailed logging without affecting LMIC timing. |  * Circular buffer for detailed logging without affecting LMIC timing. | ||||||
|  *******************************************************************************/ |  *******************************************************************************/ | ||||||
|  |  | ||||||
|  |  | ||||||
| #if LMIC_ENABLE_event_logging | #if LMIC_ENABLE_event_logging | ||||||
|  |  | ||||||
| #include "ttn_logging.h" | #include "ttn_logging.h" | ||||||
|  | #include "esp_log.h" | ||||||
| #include "freertos/FreeRTOS.h" | #include "freertos/FreeRTOS.h" | ||||||
| #include "freertos/task.h" | #include "freertos/task.h" | ||||||
| #include "esp_log.h" |  | ||||||
| #include "lmic/lmic.h" | #include "lmic/lmic.h" | ||||||
| #include <string.h> | #include <string.h> | ||||||
|  |  | ||||||
|  |  | ||||||
| #define NUM_RINGBUF_MSG 50 | #define NUM_RINGBUF_MSG 50 | ||||||
| #define TAG "lmic" | #define TAG "lmic" | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief Message structure used in ring buffer |  * @brief Message structure used in ring buffer | ||||||
|  * |  * | ||||||
|  * The structure is sent from the LMIC task to the logging task. |  * The structure is sent from the LMIC task to the logging task. | ||||||
|  */ |  */ | ||||||
| typedef struct { | typedef struct | ||||||
|     const char* message; | { | ||||||
|  |     const char *message; | ||||||
|     uint32_t datum; |     uint32_t datum; | ||||||
|     ev_t event; |     ev_t event; | ||||||
|     ostime_t time; |     ostime_t time; | ||||||
| @ -49,34 +47,34 @@ typedef struct { | |||||||
|     u1_t saveIrqFlags; |     u1_t saveIrqFlags; | ||||||
| } TTNLogMessage; | } TTNLogMessage; | ||||||
|  |  | ||||||
| static void loggingTask(void* param); | static void loggingTask(void *param); | ||||||
| static void logFatal(const char* const file, const uint16_t line); | static void logFatal(const char *const file, const uint16_t line); | ||||||
|  |  | ||||||
| static void printMessage(TTNLogMessage* log); | static void printMessage(TTNLogMessage *log); | ||||||
| static void printFatalError(TTNLogMessage* log); | static void printFatalError(TTNLogMessage *log); | ||||||
| static void printEvent(TTNLogMessage* log); | static void printEvent(TTNLogMessage *log); | ||||||
| static void printEvtJoined(TTNLogMessage* log); | static void printEvtJoined(TTNLogMessage *log); | ||||||
| static void printEvtJoinFailed(TTNLogMessage* log); | static void printEvtJoinFailed(TTNLogMessage *log); | ||||||
| static void printEvtTxComplete(TTNLogMessage* log); | static void printEvtTxComplete(TTNLogMessage *log); | ||||||
| static void printEvtTxStart(TTNLogMessage* log); | static void printEvtTxStart(TTNLogMessage *log); | ||||||
| static void printEvtRxStart(TTNLogMessage* log); | static void printEvtRxStart(TTNLogMessage *log); | ||||||
| static void printEvtJoinTxComplete(TTNLogMessage* log); | static void printEvtJoinTxComplete(TTNLogMessage *log); | ||||||
| static void bin2hex(const uint8_t* bin, unsigned len, char* buf, char sep); | static void bin2hex(const uint8_t *bin, unsigned len, char *buf, char sep); | ||||||
|  |  | ||||||
| // Constants for formatting LORA values | // Constants for formatting LORA values | ||||||
| static const char* const SF_NAMES[] = { "FSK", "SF7", "SF8", "SF9", "SF10", "SF11", "SF12", "SFrfu" }; | static const char *const SF_NAMES[] = {"FSK", "SF7", "SF8", "SF9", "SF10", "SF11", "SF12", "SFrfu"}; | ||||||
| static const char* const BW_NAMES[] = { "BW125", "BW250", "BW500", "BWrfu" }; | static const char *const BW_NAMES[] = {"BW125", "BW250", "BW500", "BWrfu"}; | ||||||
| static const char* const CR_NAMES[] = { "CR 4/5", "CR 4/6", "CR 4/7", "CR 4/8" }; | static const char *const CR_NAMES[] = {"CR 4/5", "CR 4/6", "CR 4/7", "CR 4/8"}; | ||||||
| static const char* const CRC_NAMES[] = { "NoCrc", "Crc" }; | static const char *const CRC_NAMES[] = {"NoCrc", "Crc"}; | ||||||
|  |  | ||||||
| static RingbufHandle_t ringBuffer; | static RingbufHandle_t ringBuffer; | ||||||
|  |  | ||||||
|  |  | ||||||
| // Initialize logging | // Initialize logging | ||||||
| void ttn_log_init(void) | void ttn_log_init(void) | ||||||
| { | { | ||||||
|     ringBuffer = xRingbufferCreate(NUM_RINGBUF_MSG * sizeof(TTNLogMessage), RINGBUF_TYPE_NOSPLIT); |     ringBuffer = xRingbufferCreate(NUM_RINGBUF_MSG * sizeof(TTNLogMessage), RINGBUF_TYPE_NOSPLIT); | ||||||
|     if (ringBuffer == NULL) { |     if (ringBuffer == NULL) | ||||||
|  |     { | ||||||
|         ESP_LOGE(TAG, "Failed to create ring buffer"); |         ESP_LOGE(TAG, "Failed to create ring buffer"); | ||||||
|         ASSERT(0); |         ASSERT(0); | ||||||
|     } |     } | ||||||
| @ -86,7 +84,7 @@ void ttn_log_init(void) | |||||||
| } | } | ||||||
|  |  | ||||||
| // Record a logging event for later output | // Record a logging event for later output | ||||||
| void ttn_log_event(int event, const char* message, uint32_t datum) | void ttn_log_event(int event, const char *message, uint32_t datum) | ||||||
| { | { | ||||||
|     if (ringBuffer == NULL) |     if (ringBuffer == NULL) | ||||||
|         return; |         return; | ||||||
| @ -101,8 +99,8 @@ void ttn_log_event(int event, const char* message, uint32_t datum) | |||||||
|         .event = (ev_t)event, |         .event = (ev_t)event, | ||||||
|         .freq = LMIC.freq, |         .freq = LMIC.freq, | ||||||
|         .opmode = LMIC.opmode, |         .opmode = LMIC.opmode, | ||||||
|         .fcntDn = (u2_t) LMIC.seqnoDn, |         .fcntDn = (u2_t)LMIC.seqnoDn, | ||||||
|         .fcntUp = (u2_t) LMIC.seqnoUp, |         .fcntUp = (u2_t)LMIC.seqnoUp, | ||||||
|         .rxsyms = LMIC.rxsyms, |         .rxsyms = LMIC.rxsyms, | ||||||
|         .rps = LMIC.rps, |         .rps = LMIC.rps, | ||||||
|         .txChnl = LMIC.txChnl, |         .txChnl = LMIC.txChnl, | ||||||
| @ -115,7 +113,7 @@ void ttn_log_event(int event, const char* message, uint32_t datum) | |||||||
| } | } | ||||||
|  |  | ||||||
| // record a fatal event (failed assert) for later output | // record a fatal event (failed assert) for later output | ||||||
| void logFatal(const char* const file, const uint16_t line) | void logFatal(const char *const file, const uint16_t line) | ||||||
| { | { | ||||||
|     ttn_log_event(-3, file, line); |     ttn_log_event(-3, file, line); | ||||||
| } | } | ||||||
| @ -125,7 +123,6 @@ void logFatal(const char* const file, const uint16_t line) | |||||||
| void LMICOS_logEvent(const char *pMessage) | void LMICOS_logEvent(const char *pMessage) | ||||||
| { | { | ||||||
|     ttn_log_event(-1, pMessage, 0); |     ttn_log_event(-1, pMessage, 0); | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // Record an information message with an integer value for later output | // Record an information message with an integer value for later output | ||||||
| @ -135,18 +132,18 @@ void LMICOS_logEventUint32(const char *pMessage, uint32_t datum) | |||||||
|     ttn_log_event(-2, pMessage, datum); |     ttn_log_event(-2, pMessage, datum); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // --------------------------------------------------------------------------- | // --------------------------------------------------------------------------- | ||||||
| // Log output | // Log output | ||||||
|  |  | ||||||
| // Tasks that receiveds the recorded messages, formats and outputs them. | // Tasks that receiveds the recorded messages, formats and outputs them. | ||||||
| void loggingTask(void* param) | void loggingTask(void *param) | ||||||
| { | { | ||||||
|     RingbufHandle_t ringBuffer = (RingbufHandle_t)param; |     RingbufHandle_t ringBuffer = (RingbufHandle_t)param; | ||||||
|  |  | ||||||
|     while (true) { |     while (true) | ||||||
|  |     { | ||||||
|         size_t size; |         size_t size; | ||||||
|         TTNLogMessage* log = (TTNLogMessage*) xRingbufferReceive(ringBuffer, &size, portMAX_DELAY); |         TTNLogMessage *log = (TTNLogMessage *)xRingbufferReceive(ringBuffer, &size, portMAX_DELAY); | ||||||
|         if (log == NULL) |         if (log == NULL) | ||||||
|             continue; |             continue; | ||||||
|  |  | ||||||
| @ -156,24 +153,18 @@ void loggingTask(void* param) | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // Format and output a log message | // Format and output a log message | ||||||
| void printMessage(TTNLogMessage* log) | void printMessage(TTNLogMessage *log) | ||||||
| { | { | ||||||
|     switch((int)log->event) |     switch ((int)log->event) | ||||||
|     { |     { | ||||||
|     case -1: |     case -1: | ||||||
|             ESP_LOGI(TAG, "%u (%d ms) - %s: opmode=%x", |         ESP_LOGI(TAG, "%u (%d ms) - %s: opmode=%x", log->time, osticks2ms(log->time), log->message, log->opmode); | ||||||
|                 log->time, osticks2ms(log->time), |  | ||||||
|                 log->message, log->opmode |  | ||||||
|             ); |  | ||||||
|         break; |         break; | ||||||
|  |  | ||||||
|     case -2: |     case -2: | ||||||
|             ESP_LOGI(TAG, "%u (%d ms) - %s: datum=0x%x, opmode=%x)", |         ESP_LOGI(TAG, "%u (%d ms) - %s: datum=0x%x, opmode=%x)", log->time, osticks2ms(log->time), log->message, | ||||||
|                 log->time, osticks2ms(log->time), |                  log->datum, log->opmode); | ||||||
|                 log->message, log->datum, log->opmode |  | ||||||
|             ); |  | ||||||
|         break; |         break; | ||||||
|  |  | ||||||
|     case -3: |     case -3: | ||||||
| @ -186,44 +177,23 @@ void printMessage(TTNLogMessage* log) | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void printFatalError(TTNLogMessage *log) | ||||||
| void printFatalError(TTNLogMessage* log) |  | ||||||
| { | { | ||||||
|     ESP_LOGE(TAG, "%u (%d ms) - %s, %d", |     ESP_LOGE(TAG, "%u (%d ms) - %s, %d", log->time, osticks2ms(log->time), log->message, log->datum); | ||||||
|         log->time, osticks2ms(log->time), |     ESP_LOGE(TAG, "- freq=%d.%d, txend=%u, avail=%u, ch=%u", log->freq / 1000000, (log->freq % 1000000) / 100000, | ||||||
|         log->message, log->datum |              log->txend, log->globalDutyAvail, (unsigned)log->txChnl); | ||||||
|     ); |  | ||||||
|     ESP_LOGE(TAG, "- freq=%d.%d, txend=%u, avail=%u, ch=%u", |  | ||||||
|         log->freq / 1000000, (log->freq % 1000000) / 100000, |  | ||||||
|         log->txend, log->globalDutyAvail, |  | ||||||
|         (unsigned)log->txChnl |  | ||||||
|     ); |  | ||||||
|     rps_t rps = log->rps; |     rps_t rps = log->rps; | ||||||
|     ESP_LOGE(TAG, "- rps=0x%02x (%s, %s, %s, %s, IH=%d)", |     ESP_LOGE(TAG, "- rps=0x%02x (%s, %s, %s, %s, IH=%d)", rps, SF_NAMES[getSf(rps)], BW_NAMES[getBw(rps)], | ||||||
|         rps, |              CR_NAMES[getCr(rps)], CRC_NAMES[getNocrc(rps)], getIh(rps)); | ||||||
|         SF_NAMES[getSf(rps)], |     ESP_LOGE(TAG, "- opmode=%x, txrxFlags=0x%02x%s, saveIrqFlags=0x%02x", log->opmode, log->txrxFlags, | ||||||
|         BW_NAMES[getBw(rps)], |              (log->txrxFlags & TXRX_ACK) != 0 ? "; received ack" : "", log->saveIrqFlags); | ||||||
|         CR_NAMES[getCr(rps)], |  | ||||||
|         CRC_NAMES[getNocrc(rps)], |  | ||||||
|         getIh(rps) |  | ||||||
|     ); |  | ||||||
|     ESP_LOGE(TAG, "- opmode=%x, txrxFlags=0x%02x%s, saveIrqFlags=0x%02x", |  | ||||||
|         log->opmode, |  | ||||||
|         log->txrxFlags, |  | ||||||
|         (log->txrxFlags & TXRX_ACK) != 0 ? "; received ack" : "", |  | ||||||
|         log->saveIrqFlags |  | ||||||
|     ); |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void printEvent(TTNLogMessage *log) | ||||||
| void printEvent(TTNLogMessage* log) |  | ||||||
| { | { | ||||||
|     ESP_LOGI(TAG, "%u (%d ms) - %s", |     ESP_LOGI(TAG, "%u (%d ms) - %s", log->time, osticks2ms(log->time), log->message); | ||||||
|         log->time, osticks2ms(log->time), |  | ||||||
|         log->message |  | ||||||
|     ); |  | ||||||
|  |  | ||||||
|     switch((int)log->event) |     switch ((int)log->event) | ||||||
|     { |     { | ||||||
|     case EV_JOINED: |     case EV_JOINED: | ||||||
|         printEvtJoined(log); |         printEvtJoined(log); | ||||||
| @ -254,9 +224,8 @@ void printEvent(TTNLogMessage* log) | |||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // Format and output the detail of a successful network join | // Format and output the detail of a successful network join | ||||||
| void printEvtJoined(TTNLogMessage* log) | void printEvtJoined(TTNLogMessage *log) | ||||||
| { | { | ||||||
|     ESP_LOGI(TAG, "- ch=%d", (unsigned)log->txChnl); |     ESP_LOGI(TAG, "- ch=%d", (unsigned)log->txChnl); | ||||||
|  |  | ||||||
| @ -271,98 +240,54 @@ void printEvtJoined(TTNLogMessage* log) | |||||||
|     ESP_LOGI(TAG, "- devaddr: %08x", devaddr); |     ESP_LOGI(TAG, "- devaddr: %08x", devaddr); | ||||||
|  |  | ||||||
|     char hexBuf[48]; |     char hexBuf[48]; | ||||||
|     bin2hex((uint8_t*)&artKey, sizeof(artKey), hexBuf, '-'); |     bin2hex((uint8_t *)&artKey, sizeof(artKey), hexBuf, '-'); | ||||||
|     ESP_LOGI(TAG, "- artKey: %s", hexBuf); |     ESP_LOGI(TAG, "- artKey: %s", hexBuf); | ||||||
|  |  | ||||||
|     bin2hex((uint8_t*)&nwkKey, sizeof(nwkKey), hexBuf, '-'); |     bin2hex((uint8_t *)&nwkKey, sizeof(nwkKey), hexBuf, '-'); | ||||||
|     ESP_LOGI(TAG, "- nwkKey: %s", hexBuf); |     ESP_LOGI(TAG, "- nwkKey: %s", hexBuf); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // Format and output the detail of a failed network join | // Format and output the detail of a failed network join | ||||||
| void printEvtJoinFailed(TTNLogMessage* log) | void printEvtJoinFailed(TTNLogMessage *log) | ||||||
| { | { | ||||||
|     rps_t rps = log->rps; |     rps_t rps = log->rps; | ||||||
|     ESP_LOGE(TAG, "- freq=%d.%d, opmode=%x, rps=0x%02x (%s, %s, %s, %s, IH=%d)", |     ESP_LOGE(TAG, "- freq=%d.%d, opmode=%x, rps=0x%02x (%s, %s, %s, %s, IH=%d)", log->freq / 1000000, | ||||||
|         log->freq / 1000000, (log->freq % 1000000) / 100000, |              (log->freq % 1000000) / 100000, log->opmode, rps, SF_NAMES[getSf(rps)], BW_NAMES[getBw(rps)], | ||||||
|         log->opmode, |              CR_NAMES[getCr(rps)], CRC_NAMES[getNocrc(rps)], getIh(rps)); | ||||||
|         rps, |  | ||||||
|         SF_NAMES[getSf(rps)], |  | ||||||
|         BW_NAMES[getBw(rps)], |  | ||||||
|         CR_NAMES[getCr(rps)], |  | ||||||
|         CRC_NAMES[getNocrc(rps)], |  | ||||||
|         getIh(rps) |  | ||||||
|     ); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void printEvtTxComplete(TTNLogMessage* log) | void printEvtTxComplete(TTNLogMessage *log) | ||||||
| { | { | ||||||
|     rps_t rps = log->rps; |     rps_t rps = log->rps; | ||||||
|     ESP_LOGI(TAG, "- ch=%d, rps=0x%02x (%s, %s, %s, %s, IH=%d)", |     ESP_LOGI(TAG, "- ch=%d, rps=0x%02x (%s, %s, %s, %s, IH=%d)", (unsigned)log->txChnl, rps, SF_NAMES[getSf(rps)], | ||||||
|         (unsigned)log->txChnl, |              BW_NAMES[getBw(rps)], CR_NAMES[getCr(rps)], CRC_NAMES[getNocrc(rps)], getIh(rps)); | ||||||
|         rps, |     ESP_LOGI(TAG, "- txrxFlags=0x%02x%s, FcntUp=%04x, FcntDn=%04x, txend=%u", log->txrxFlags, | ||||||
|         SF_NAMES[getSf(rps)], |              (log->txrxFlags & TXRX_ACK) != 0 ? "; received ack" : "", log->fcntUp, log->fcntDn, log->txend); | ||||||
|         BW_NAMES[getBw(rps)], |  | ||||||
|         CR_NAMES[getCr(rps)], |  | ||||||
|         CRC_NAMES[getNocrc(rps)], |  | ||||||
|         getIh(rps) |  | ||||||
|     ); |  | ||||||
|     ESP_LOGI(TAG, "- txrxFlags=0x%02x%s, FcntUp=%04x, FcntDn=%04x, txend=%u", |  | ||||||
|         log->txrxFlags, |  | ||||||
|         (log->txrxFlags & TXRX_ACK) != 0 ? "; received ack" : "", |  | ||||||
|         log->fcntUp, log->fcntDn, |  | ||||||
|         log->txend |  | ||||||
|     ); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void printEvtTxStart(TTNLogMessage* log) | void printEvtTxStart(TTNLogMessage *log) | ||||||
| { | { | ||||||
|     rps_t rps = log->rps; |     rps_t rps = log->rps; | ||||||
|     ESP_LOGI(TAG, "- ch=%d, rps=0x%02x (%s, %s, %s, %s, IH=%d)", |     ESP_LOGI(TAG, "- ch=%d, rps=0x%02x (%s, %s, %s, %s, IH=%d)", (unsigned)log->txChnl, rps, SF_NAMES[getSf(rps)], | ||||||
|         (unsigned)log->txChnl, |              BW_NAMES[getBw(rps)], CR_NAMES[getCr(rps)], CRC_NAMES[getNocrc(rps)], getIh(rps)); | ||||||
|         rps, |     ESP_LOGI(TAG, "- datarate=%u, opmode=%x, txend=%u", log->datarate, log->opmode, log->txend); | ||||||
|         SF_NAMES[getSf(rps)], |  | ||||||
|         BW_NAMES[getBw(rps)], |  | ||||||
|         CR_NAMES[getCr(rps)], |  | ||||||
|         CRC_NAMES[getNocrc(rps)], |  | ||||||
|         getIh(rps) |  | ||||||
|     ); |  | ||||||
|     ESP_LOGI(TAG, "- datarate=%u, opmode=%x, txend=%u", |  | ||||||
|         log->datarate, |  | ||||||
|         log->opmode, |  | ||||||
|         log->txend |  | ||||||
|     ); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void printEvtRxStart(TTNLogMessage* log) | void printEvtRxStart(TTNLogMessage *log) | ||||||
| { | { | ||||||
|     rps_t rps = log->rps; |     rps_t rps = log->rps; | ||||||
|     ESP_LOGI(TAG, "- freq=%d.%d, rps=0x%02x (%s, %s, %s, %s, IH=%d)", |     ESP_LOGI(TAG, "- freq=%d.%d, rps=0x%02x (%s, %s, %s, %s, IH=%d)", log->freq / 1000000, | ||||||
|         log->freq / 1000000, (log->freq % 1000000) / 100000, |              (log->freq % 1000000) / 100000, rps, SF_NAMES[getSf(rps)], BW_NAMES[getBw(rps)], CR_NAMES[getCr(rps)], | ||||||
|         rps, |              CRC_NAMES[getNocrc(rps)], getIh(rps)); | ||||||
|         SF_NAMES[getSf(rps)], |     ESP_LOGI(TAG, "- delta=%dms, rxsysm=%u", osticks2ms(log->time - log->txend), log->rxsyms); | ||||||
|         BW_NAMES[getBw(rps)], |  | ||||||
|         CR_NAMES[getCr(rps)], |  | ||||||
|         CRC_NAMES[getNocrc(rps)], |  | ||||||
|         getIh(rps) |  | ||||||
|     ); |  | ||||||
|     ESP_LOGI(TAG, "- delta=%dms, rxsysm=%u", |  | ||||||
|         osticks2ms(log->time - log->txend), |  | ||||||
|         log->rxsyms |  | ||||||
|     ); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void printEvtJoinTxComplete(TTNLogMessage* log) | void printEvtJoinTxComplete(TTNLogMessage *log) | ||||||
| { | { | ||||||
|     ESP_LOGI(TAG, "- saveIrqFlags=0x%02x", |     ESP_LOGI(TAG, "- saveIrqFlags=0x%02x", log->saveIrqFlags); | ||||||
|         log->saveIrqFlags |  | ||||||
|     ); |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | static const char *HEX_DIGITS = "0123456789ABCDEF"; | ||||||
|  |  | ||||||
|  |  | ||||||
| static const char* HEX_DIGITS = "0123456789ABCDEF"; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief Convert binary data to hexadecimal representation. |  * @brief Convert binary data to hexadecimal representation. | ||||||
| @ -372,10 +297,11 @@ static const char* HEX_DIGITS = "0123456789ABCDEF"; | |||||||
|  * @param buf buffer for hexadecimal result |  * @param buf buffer for hexadecimal result | ||||||
|  * @param sep separator used between bytes (or 0 for none) |  * @param sep separator used between bytes (or 0 for none) | ||||||
|  */ |  */ | ||||||
| void bin2hex(const uint8_t* bin, unsigned len, char* buf, char sep) | void bin2hex(const uint8_t *bin, unsigned len, char *buf, char sep) | ||||||
| { | { | ||||||
|     int tgt = 0; |     int tgt = 0; | ||||||
|     for (int i = 0; i < len; i++) { |     for (int i = 0; i < len; i++) | ||||||
|  |     { | ||||||
|         if (sep != 0 && i != 0) |         if (sep != 0 && i != 0) | ||||||
|             buf[tgt++] = sep; |             buf[tgt++] = sep; | ||||||
|         buf[tgt++] = HEX_DIGITS[bin[i] >> 4]; |         buf[tgt++] = HEX_DIGITS[bin[i] >> 4]; | ||||||
| @ -384,5 +310,4 @@ void bin2hex(const uint8_t* bin, unsigned len, char* buf, char sep) | |||||||
|     buf[tgt] = 0; |     buf[tgt] = 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -13,19 +13,17 @@ | |||||||
| #ifndef TTN_LOGGING_H | #ifndef TTN_LOGGING_H | ||||||
| #define TTN_LOGGING_H | #define TTN_LOGGING_H | ||||||
|  |  | ||||||
|  |  | ||||||
| #if LMIC_ENABLE_event_logging | #if LMIC_ENABLE_event_logging | ||||||
|  |  | ||||||
| #include <freertos/FreeRTOS.h> | #include <freertos/FreeRTOS.h> | ||||||
| #include <freertos/ringbuf.h> | #include <freertos/ringbuf.h> | ||||||
|  |  | ||||||
|  |  | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
| extern "C" { | extern "C" | ||||||
|  | { | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |     /** | ||||||
| /** |  | ||||||
|      * @brief Logging functions. |      * @brief Logging functions. | ||||||
|      * |      * | ||||||
|      * Logs internal information from LMIC in an asynchrnous fashion in order |      * Logs internal information from LMIC in an asynchrnous fashion in order | ||||||
| @ -40,15 +38,13 @@ extern "C" { | |||||||
|      * `LMIC_ENABLE_event_logging` to 1. |      * `LMIC_ENABLE_event_logging` to 1. | ||||||
|      */ |      */ | ||||||
|  |  | ||||||
| void ttn_log_init(void); |     void ttn_log_init(void); | ||||||
| void ttn_log_event(int event, const char* message, uint32_t datum); |     void ttn_log_event(int event, const char *message, uint32_t datum); | ||||||
|  |  | ||||||
|  |  | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -11,14 +11,14 @@ | |||||||
|  *******************************************************************************/ |  *******************************************************************************/ | ||||||
|  |  | ||||||
| #include "ttn_provisioning.h" | #include "ttn_provisioning.h" | ||||||
| #include "freertos/FreeRTOS.h" |  | ||||||
| #include "driver/uart.h" | #include "driver/uart.h" | ||||||
| #include "esp_event.h" | #include "esp_event.h" | ||||||
| #include "esp_log.h" | #include "esp_log.h" | ||||||
| #include "esp_system.h" | #include "esp_system.h" | ||||||
| #include "nvs_flash.h" | #include "freertos/FreeRTOS.h" | ||||||
| #include "lmic/lmic.h" |  | ||||||
| #include "hal/hal_esp32.h" | #include "hal/hal_esp32.h" | ||||||
|  | #include "lmic/lmic.h" | ||||||
|  | #include "nvs_flash.h" | ||||||
|  |  | ||||||
| #if defined(TTN_HAS_AT_COMMANDS) | #if defined(TTN_HAS_AT_COMMANDS) | ||||||
| #define UART_NUM CONFIG_TTN_PROVISION_UART_NUM | #define UART_NUM CONFIG_TTN_PROVISION_UART_NUM | ||||||
| @ -32,7 +32,7 @@ | |||||||
| #define NVS_FLASH_KEY_APP_KEY "appKey" | #define NVS_FLASH_KEY_APP_KEY "appKey" | ||||||
|  |  | ||||||
| #if defined(TTN_HAS_AT_COMMANDS) | #if defined(TTN_HAS_AT_COMMANDS) | ||||||
| static void provisioning_task(void* pvParameter); | static void provisioning_task(void *pvParameter); | ||||||
| static void add_line_data(int num_bytes); | static void add_line_data(int num_bytes); | ||||||
| static void detect_line_end(int start_at); | static void detect_line_end(int start_at); | ||||||
| static void process_line(void); | static void process_line(void); | ||||||
| @ -43,16 +43,16 @@ static void config_uart(void); | |||||||
| #endif | #endif | ||||||
|  |  | ||||||
| static bool decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const char *app_key); | static bool decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const char *app_key); | ||||||
| static bool read_nvs_value(nvs_handle handle, const char* key, uint8_t* data, size_t expected_length, bool silent); | static bool read_nvs_value(nvs_handle handle, const char *key, uint8_t *data, size_t expected_length, bool silent); | ||||||
| static bool write_nvs_value(nvs_handle handle, const char* key, const uint8_t* data, size_t len); | static bool write_nvs_value(nvs_handle handle, const char *key, const uint8_t *data, size_t len); | ||||||
|  |  | ||||||
| static bool hex_str_to_bin(const char *hex, uint8_t *buf, int len); | static bool hex_str_to_bin(const char *hex, uint8_t *buf, int len); | ||||||
| static int hex_tuple_to_byte(const char *hex); | static int hex_tuple_to_byte(const char *hex); | ||||||
| static int hex_digit_to_val(char ch); | static int hex_digit_to_val(char ch); | ||||||
| static void bin_to_hex_str(const uint8_t* buf, int len, char* hex); | static void bin_to_hex_str(const uint8_t *buf, int len, char *hex); | ||||||
| static char val_to_hex_digit(int val); | static char val_to_hex_digit(int val); | ||||||
| static void swap_bytes(uint8_t* buf, int len); | static void swap_bytes(uint8_t *buf, int len); | ||||||
| static bool is_all_zeros(const uint8_t* buf, int len); | static bool is_all_zeros(const uint8_t *buf, int len); | ||||||
|  |  | ||||||
| static uint8_t global_dev_eui[8]; | static uint8_t global_dev_eui[8]; | ||||||
| static uint8_t global_app_eui[8]; | static uint8_t global_app_eui[8]; | ||||||
| @ -62,26 +62,25 @@ static bool have_keys = false; | |||||||
|  |  | ||||||
| #if defined(TTN_HAS_AT_COMMANDS) | #if defined(TTN_HAS_AT_COMMANDS) | ||||||
| static QueueHandle_t uart_queue; | static QueueHandle_t uart_queue; | ||||||
| static char* line_buf; | static char *line_buf; | ||||||
| static int line_length; | static int line_length; | ||||||
| static uint8_t last_line_end_char; | static uint8_t last_line_end_char; | ||||||
| static bool quit_task; | static bool quit_task; | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| // --- LMIC callbacks | // --- LMIC callbacks | ||||||
|  |  | ||||||
| // This EUI must be in little-endian format, so least-significant-byte first. | // This EUI must be in little-endian format, so least-significant-byte first. | ||||||
| // When copying an EUI from ttnctl output, this means to reverse the bytes. | // When copying an EUI from ttnctl output, this means to reverse the bytes. | ||||||
| // For TTN issued EUIs the last bytes should be 0xD5, 0xB3, 0x70. | // For TTN issued EUIs the last bytes should be 0xD5, 0xB3, 0x70. | ||||||
| // The order is swapped in provisioning_decode_keys(). | // The order is swapped in provisioning_decode_keys(). | ||||||
| void os_getArtEui (u1_t* buf) | void os_getArtEui(u1_t *buf) | ||||||
| { | { | ||||||
|     memcpy(buf, global_app_eui, 8); |     memcpy(buf, global_app_eui, 8); | ||||||
| } | } | ||||||
|  |  | ||||||
| // This should also be in little endian format, see above. | // This should also be in little endian format, see above. | ||||||
| void os_getDevEui (u1_t* buf) | void os_getDevEui(u1_t *buf) | ||||||
| { | { | ||||||
|     memcpy(buf, global_dev_eui, 8); |     memcpy(buf, global_dev_eui, 8); | ||||||
| } | } | ||||||
| @ -89,7 +88,7 @@ void os_getDevEui (u1_t* buf) | |||||||
| // This key should be in big endian format (or, since it is not really a number | // This key should be in big endian format (or, since it is not really a number | ||||||
| // but a block of memory, endianness does not really apply). In practice, a key | // but a block of memory, endianness does not really apply). In practice, a key | ||||||
| // taken from ttnctl can be copied as-is. | // taken from ttnctl can be copied as-is. | ||||||
| void os_getDevKey (u1_t* buf) | void os_getDevKey(u1_t *buf) | ||||||
| { | { | ||||||
|     memcpy(buf, global_app_key, 16); |     memcpy(buf, global_app_key, 16); | ||||||
| } | } | ||||||
| @ -100,7 +99,6 @@ void ttn_provisioning_init(void) | |||||||
| { | { | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // --- Provisioning task | // --- Provisioning task | ||||||
|  |  | ||||||
| #if defined(TTN_HAS_AT_COMMANDS) | #if defined(TTN_HAS_AT_COMMANDS) | ||||||
| @ -117,9 +115,9 @@ void ttn_provisioning_start_task(void) | |||||||
|     xTaskCreate(provisioning_task, "ttn_provision", 2048, NULL, 1, NULL); |     xTaskCreate(provisioning_task, "ttn_provision", 2048, NULL, 1, NULL); | ||||||
| } | } | ||||||
|  |  | ||||||
| void provisioning_task(void* pvParameter) | void provisioning_task(void *pvParameter) | ||||||
| { | { | ||||||
|     line_buf = (char*)malloc(MAX_LINE_LENGTH + 1); |     line_buf = (char *)malloc(MAX_LINE_LENGTH + 1); | ||||||
|     line_length = 0; |     line_length = 0; | ||||||
|  |  | ||||||
|     uart_event_t event; |     uart_event_t event; | ||||||
| @ -161,7 +159,7 @@ top: | |||||||
|     if (line_length + n > MAX_LINE_LENGTH) |     if (line_length + n > MAX_LINE_LENGTH) | ||||||
|         n = MAX_LINE_LENGTH - line_length; |         n = MAX_LINE_LENGTH - line_length; | ||||||
|  |  | ||||||
|     uart_read_bytes(UART_NUM, (uint8_t*)line_buf + line_length, n, portMAX_DELAY); |     uart_read_bytes(UART_NUM, (uint8_t *)line_buf + line_length, n, portMAX_DELAY); | ||||||
|     int start_at = line_length; |     int start_at = line_length; | ||||||
|     line_length += n; |     line_length += n; | ||||||
|  |  | ||||||
| @ -271,7 +269,8 @@ void process_line(void) | |||||||
|         ESP_ERROR_CHECK(err); |         ESP_ERROR_CHECK(err); | ||||||
|  |  | ||||||
|         bin_to_hex_str(mac, 6, hexbuf); |         bin_to_hex_str(mac, 6, hexbuf); | ||||||
|         for (int i = 0; i < 12; i += 2) { |         for (int i = 0; i < 12; i += 2) | ||||||
|  |         { | ||||||
|             if (i > 0) |             if (i > 0) | ||||||
|                 uart_write_bytes(UART_NUM, ":", 1); |                 uart_write_bytes(UART_NUM, ":", 1); | ||||||
|             uart_write_bytes(UART_NUM, hexbuf + i, 2); |             uart_write_bytes(UART_NUM, hexbuf + i, 2); | ||||||
| @ -287,7 +286,8 @@ void process_line(void) | |||||||
|         ESP_ERROR_CHECK(err); |         ESP_ERROR_CHECK(err); | ||||||
|  |  | ||||||
|         bin_to_hex_str(mac, 6, hexbuf); |         bin_to_hex_str(mac, 6, hexbuf); | ||||||
|         for (int i = 0; i < 12; i += 2) { |         for (int i = 0; i < 12; i += 2) | ||||||
|  |         { | ||||||
|             uart_write_bytes(UART_NUM, hexbuf + i, 2); |             uart_write_bytes(UART_NUM, hexbuf + i, 2); | ||||||
|             if (i == 4) |             if (i == 4) | ||||||
|                 uart_write_bytes(UART_NUM, "FFFE", 4); |                 uart_write_bytes(UART_NUM, "FFFE", 4); | ||||||
| @ -316,30 +316,27 @@ void process_line(void) | |||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| #if defined(TTN_CONFIG_UART) | #if defined(TTN_CONFIG_UART) | ||||||
|  |  | ||||||
| void config_uart(void) | void config_uart(void) | ||||||
| { | { | ||||||
|     esp_err_t err; |     esp_err_t err; | ||||||
|  |  | ||||||
|     uart_config_t uart_config = { |     uart_config_t uart_config = {.baud_rate = CONFIG_TTN_PROVISION_UART_BAUDRATE, | ||||||
|         .baud_rate = CONFIG_TTN_PROVISION_UART_BAUDRATE, |  | ||||||
|                                  .data_bits = UART_DATA_8_BITS, |                                  .data_bits = UART_DATA_8_BITS, | ||||||
|                                  .parity = UART_PARITY_DISABLE, |                                  .parity = UART_PARITY_DISABLE, | ||||||
|                                  .stop_bits = UART_STOP_BITS_1, |                                  .stop_bits = UART_STOP_BITS_1, | ||||||
|         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE |                                  .flow_ctrl = UART_HW_FLOWCTRL_DISABLE}; | ||||||
|     }; |  | ||||||
|     err = uart_param_config(UART_NUM, &uart_config); |     err = uart_param_config(UART_NUM, &uart_config); | ||||||
|     ESP_ERROR_CHECK(err); |     ESP_ERROR_CHECK(err); | ||||||
|  |  | ||||||
|     err = uart_set_pin(UART_NUM, CONFIG_TTN_PROVISION_UART_TX_GPIO, CONFIG_TTN_PROVISION_UART_RX_GPIO, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); |     err = uart_set_pin(UART_NUM, CONFIG_TTN_PROVISION_UART_TX_GPIO, CONFIG_TTN_PROVISION_UART_RX_GPIO, | ||||||
|  |                        UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); | ||||||
|     ESP_ERROR_CHECK(err); |     ESP_ERROR_CHECK(err); | ||||||
| } | } | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| // --- Key handling | // --- Key handling | ||||||
|  |  | ||||||
| bool ttn_provisioning_have_keys(void) | bool ttn_provisioning_have_keys(void) | ||||||
| @ -404,13 +401,12 @@ bool decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const c | |||||||
|     memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui)); |     memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui)); | ||||||
|     memcpy(global_app_key, buf_app_key, sizeof(global_app_key)); |     memcpy(global_app_key, buf_app_key, sizeof(global_app_key)); | ||||||
|  |  | ||||||
|     have_keys = !is_all_zeros(global_dev_eui, sizeof(global_dev_eui)) |     have_keys = | ||||||
|         && !is_all_zeros(global_app_key, sizeof(global_app_key)); |         !is_all_zeros(global_dev_eui, sizeof(global_dev_eui)) && !is_all_zeros(global_app_key, sizeof(global_app_key)); | ||||||
|  |  | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // --- Non-volatile storage | // --- Non-volatile storage | ||||||
|  |  | ||||||
| bool ttn_provisioning_save_keys() | bool ttn_provisioning_save_keys() | ||||||
| @ -480,8 +476,8 @@ bool ttn_provisioning_restore_keys(bool silent) | |||||||
|     memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui)); |     memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui)); | ||||||
|     memcpy(global_app_key, buf_app_key, sizeof(global_app_key)); |     memcpy(global_app_key, buf_app_key, sizeof(global_app_key)); | ||||||
|  |  | ||||||
|     have_keys = !is_all_zeros(global_dev_eui, sizeof(global_dev_eui)) |     have_keys = | ||||||
|         && !is_all_zeros(global_app_key, sizeof(global_app_key)); |         !is_all_zeros(global_dev_eui, sizeof(global_dev_eui)) && !is_all_zeros(global_app_key, sizeof(global_app_key)); | ||||||
|  |  | ||||||
|     if (have_keys) |     if (have_keys) | ||||||
|     { |     { | ||||||
| @ -497,7 +493,7 @@ done: | |||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool read_nvs_value(nvs_handle handle, const char* key, uint8_t* data, size_t expected_length, bool silent) | bool read_nvs_value(nvs_handle handle, const char *key, uint8_t *data, size_t expected_length, bool silent) | ||||||
| { | { | ||||||
|     size_t size = expected_length; |     size_t size = expected_length; | ||||||
|     esp_err_t res = nvs_get_blob(handle, key, data, &size); |     esp_err_t res = nvs_get_blob(handle, key, data, &size); | ||||||
| @ -522,7 +518,7 @@ bool read_nvs_value(nvs_handle handle, const char* key, uint8_t* data, size_t ex | |||||||
|     return false; |     return false; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool write_nvs_value(nvs_handle handle, const char* key, const uint8_t* data, size_t len) | bool write_nvs_value(nvs_handle handle, const char *key, const uint8_t *data, size_t len) | ||||||
| { | { | ||||||
|     uint8_t buf[16]; |     uint8_t buf[16]; | ||||||
|     if (read_nvs_value(handle, key, buf, len, true) && memcmp(buf, data, len) == 0) |     if (read_nvs_value(handle, key, buf, len, true) && memcmp(buf, data, len) == 0) | ||||||
| @ -534,12 +530,11 @@ bool write_nvs_value(nvs_handle handle, const char* key, const uint8_t* data, si | |||||||
|     return res == ESP_OK; |     return res == ESP_OK; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // --- Helper functions --- | // --- Helper functions --- | ||||||
|  |  | ||||||
| bool hex_str_to_bin(const char *hex, uint8_t *buf, int len) | bool hex_str_to_bin(const char *hex, uint8_t *buf, int len) | ||||||
| { | { | ||||||
|     const char* ptr = hex; |     const char *ptr = hex; | ||||||
|     for (int i = 0; i < len; i++) |     for (int i = 0; i < len; i++) | ||||||
|     { |     { | ||||||
|         int val = hex_tuple_to_byte(ptr); |         int val = hex_tuple_to_byte(ptr); | ||||||
| @ -573,7 +568,7 @@ int hex_digit_to_val(char ch) | |||||||
|     return -1; |     return -1; | ||||||
| } | } | ||||||
|  |  | ||||||
| void bin_to_hex_str(const uint8_t* buf, int len, char* hex) | void bin_to_hex_str(const uint8_t *buf, int len, char *hex) | ||||||
| { | { | ||||||
|     for (int i = 0; i < len; i++) |     for (int i = 0; i < len; i++) | ||||||
|     { |     { | ||||||
| @ -590,10 +585,10 @@ char val_to_hex_digit(int val) | |||||||
|     return "0123456789ABCDEF"[val]; |     return "0123456789ABCDEF"[val]; | ||||||
| } | } | ||||||
|  |  | ||||||
| void swap_bytes(uint8_t* buf, int len) | void swap_bytes(uint8_t *buf, int len) | ||||||
| { | { | ||||||
|     uint8_t* p1 = buf; |     uint8_t *p1 = buf; | ||||||
|     uint8_t* p2 = buf + len - 1; |     uint8_t *p2 = buf + len - 1; | ||||||
|     while (p1 < p2) |     while (p1 < p2) | ||||||
|     { |     { | ||||||
|         uint8_t t = *p1; |         uint8_t t = *p1; | ||||||
| @ -604,7 +599,7 @@ void swap_bytes(uint8_t* buf, int len) | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| bool is_all_zeros(const uint8_t* buf, int len) | bool is_all_zeros(const uint8_t *buf, int len) | ||||||
| { | { | ||||||
|     for (int i = 0; i < len; i++) |     for (int i = 0; i < len; i++) | ||||||
|         if (buf[i] != 0) |         if (buf[i] != 0) | ||||||
|  | |||||||
| @ -16,20 +16,20 @@ | |||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
|  |  | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
| extern "C" { | extern "C" | ||||||
|  | { | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| void ttn_provisioning_init(void); |     void ttn_provisioning_init(void); | ||||||
|  |  | ||||||
|  |     bool ttn_provisioning_have_keys(void); | ||||||
| bool ttn_provisioning_have_keys(void); |     bool ttn_provisioning_decode_keys(const char *dev_eui, const char *app_eui, const char *app_key); | ||||||
| bool ttn_provisioning_decode_keys(const char *dev_eui, const char *app_eui, const char *app_key); |     bool ttn_provisioning_from_mac(const char *app_eui, const char *app_key); | ||||||
| bool ttn_provisioning_from_mac(const char *app_eui, const char *app_key); |     bool ttn_provisioning_save_keys(void); | ||||||
| bool ttn_provisioning_save_keys(void); |     bool ttn_provisioning_restore_keys(bool silent); | ||||||
| bool ttn_provisioning_restore_keys(bool silent); |  | ||||||
|  |  | ||||||
| #if defined(TTN_HAS_AT_COMMANDS) | #if defined(TTN_HAS_AT_COMMANDS) | ||||||
| void ttn_provisioning_start_task(void); |     void ttn_provisioning_start_task(void); | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user