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 | ||||||
|  * |  * | ||||||
| @ -430,12 +424,17 @@ 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 | ||||||
|  | |||||||
| @ -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,13 +31,11 @@ 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. | ||||||
|      */ |      */ | ||||||
| @ -58,7 +56,6 @@ typedef enum | |||||||
|         TTN_SUCCESSFUL_RECEIVE = 2 |         TTN_SUCCESSFUL_RECEIVE = 2 | ||||||
|     } ttn_response_code_t; |     } ttn_response_code_t; | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief RX/TX window |      * @brief RX/TX window | ||||||
|      */ |      */ | ||||||
| @ -82,7 +79,6 @@ typedef enum | |||||||
|         TTN_WINDOW_RX2 = 3 |         TTN_WINDOW_RX2 = 3 | ||||||
|     } ttn_rx_tx_window_t; |     } ttn_rx_tx_window_t; | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Spreading Factor |      * @brief Spreading Factor | ||||||
|      */ |      */ | ||||||
| @ -122,7 +118,6 @@ typedef enum | |||||||
|         TTN_SF12 = 7 |         TTN_SF12 = 7 | ||||||
|     } ttn_spreading_factor_t; |     } ttn_spreading_factor_t; | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Bandwidth |      * @brief Bandwidth | ||||||
|      */ |      */ | ||||||
| @ -146,7 +141,6 @@ typedef enum | |||||||
|         TTN_BW_500 = 3 |         TTN_BW_500 = 3 | ||||||
|     } ttn_bandwidth_t; |     } ttn_bandwidth_t; | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Data Rate |      * @brief Data Rate | ||||||
|      * |      * | ||||||
| @ -403,7 +397,6 @@ typedef enum | |||||||
|         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 | ||||||
|      */ |      */ | ||||||
| @ -423,7 +416,6 @@ typedef struct | |||||||
|         uint32_t frequency; |         uint32_t frequency; | ||||||
|     } ttn_rf_settings_t; |     } ttn_rf_settings_t; | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @brief Callback for recieved messages |      * @brief Callback for recieved messages | ||||||
|      * |      * | ||||||
| @ -433,7 +425,6 @@ typedef struct | |||||||
|      */ |      */ | ||||||
|     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. | ||||||
|      * |      * | ||||||
| @ -457,12 +448,15 @@ 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. | ||||||
| @ -570,7 +564,8 @@ 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); | ||||||
|  |  | ||||||
| @ -701,15 +696,12 @@ ttn_rf_settings_t ttn_rx2_settings(void); | |||||||
|      */ |      */ | ||||||
|     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 | ||||||
|  | |||||||
							
								
								
									
										38
									
								
								src/ttn.c
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								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,7 +49,8 @@ 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; | ||||||
| @ -74,7 +75,6 @@ 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) | ||||||
| { | { | ||||||
| #if defined(TTN_IS_DISABLED) | #if defined(TTN_IS_DISABLED) | ||||||
| @ -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,14 +374,12 @@ 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) | ||||||
| { | { | ||||||
| @ -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,16 +451,12 @@ 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); | ||||||
|  | |||||||
| @ -10,27 +10,25 @@ | |||||||
|  * 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; | ||||||
| @ -71,12 +69,12 @@ 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); | ||||||
|     } |     } | ||||||
| @ -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,7 +132,6 @@ 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 | ||||||
|  |  | ||||||
| @ -144,7 +140,8 @@ 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) | ||||||
| @ -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,42 +177,21 @@ 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) | ||||||
|     { |     { | ||||||
| @ -254,7 +224,6 @@ 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) | ||||||
| { | { | ||||||
| @ -278,90 +247,46 @@ void printEvtJoined(TTNLogMessage* log) | |||||||
|     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"; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @ -375,7 +300,8 @@ static const char* HEX_DIGITS = "0123456789ABCDEF"; | |||||||
| 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,18 +13,16 @@ | |||||||
| #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. | ||||||
|      * |      * | ||||||
| @ -43,12 +41,10 @@ extern "C" { | |||||||
|     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 | ||||||
| @ -68,7 +68,6 @@ 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. | ||||||
| @ -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) | ||||||
| @ -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) | ||||||
|     { |     { | ||||||
| @ -534,7 +530,6 @@ 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) | ||||||
|  | |||||||
| @ -16,12 +16,12 @@ | |||||||
| #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); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user