Simplify conditional compilation; add region Japan

This commit is contained in:
Manuel Bleichenbacher 2018-10-27 19:19:55 +02:00
parent 99420c6a75
commit f03b5c51fd
6 changed files with 62 additions and 25 deletions

23
Kconfig
View File

@ -14,16 +14,19 @@ config TTN_LORA_FREQ_EU_868
bool "Europe (868 MHz)"
config TTN_LORA_FREQ_US_915
bool "North America (915 Mhz)"
bool "North America (915 MHz)"
config TTN_LORA_FREQ_AU_921
bool "Australia (921 Mhz)"
bool "Australia (921 MHz)"
config TTN_LORA_FREQ_AS_923
bool "Asia (923 Mhz)"
bool "Asia (923 MHz)"
config TTN_LORA_FREQ_AS_923_JP
bool "Asia, region Japan (923 MHz)"
config TTN_LORA_FREQ_IN_866
bool "India (866 Mhz)"
bool "India (866 MHz)"
endchoice
@ -79,22 +82,22 @@ config TTN_BG_TASK_PRIO
choice TTN_PROVISION_UART
prompt "UART for provisioning"
prompt "AT commands"
default TTN_PROVISION_UART_DEFAULT
help
Select whether to use UART for listening for provisioning commands.
Select whether to listen on UART for AT commands.
- Default is to use UART0 on pins GPIO1(TX) and GPIO3(RX).
- If "Custom" is selected, UART0 or UART1 can be chosen,
and any pins can be selected.
- If "None" is selected, the feature is not available.
- If "None" is selected, AT commands are not available.
config TTN_PROVISION_UART_DEFAULT
bool "Default: UART0, TX=GPIO1, RX=GPIO3, 115,200 baud"
bool "Enabled - default settings: UART0, TX=GPIO1, RX=GPIO3, 115,200 baud"
config TTN_PROVISION_UART_CUSTOM
bool "Custom"
bool "Enabled - custom UART settings"
config TTN_PROVISION_UART_NONE
bool "None"
bool "Disabled"
endchoice
choice TTN_PROVISION_UART_NUM

View File

@ -20,6 +20,9 @@
#define CFG_au921 1
#elif defined(CONFIG_TTN_LORA_FREQ_AS_923)
#define CFG_as923 1
#elif defined(CONFIG_TTN_LORA_FREQ_AS_923_JP)
#define CFG_as923 1
#define LMIC_COUNTRY_CODE LMIC_COUNTRY_CODE_JP
#elif defined(CONFIG_TTN_LORA_FREQ_IN_866)
#define CFG_in866 1
#else
@ -55,6 +58,14 @@
#error TTN timer must be configured using 'make menuconfig'
#endif
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#define TTN_HAS_AT_COMMANDS 1
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
#define TTN_CONFIG_UART 1
#endif
#endif
// 16 μs per tick
// LMIC requires ticks to be 15.5μs - 100 μs long
#define US_PER_OSTICK 16

View File

@ -130,7 +130,7 @@ public:
bool provisionWithMAC(const char *appEui, const char *appKey);
/**
* @brief Start task that listens on configured UART for provisioning commands.
* @brief Start task that listens on configured UART for AT commands.
*
* Run 'make menuconfig' to configure it.
*/

View File

@ -20,7 +20,7 @@
#include "lmic/lmic.h"
#include "hal/hal_esp32.h"
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#if defined(TTN_HAS_AT_COMMANDS)
const uart_port_t UART_NUM = (uart_port_t) CONFIG_TTN_PROVISION_UART_NUM;
const int MAX_LINE_LENGTH = 128;
#endif
@ -35,7 +35,10 @@ static uint8_t global_dev_eui[8];
static uint8_t global_app_eui[8];
static uint8_t global_app_key[16];
#if defined(TTN_HAS_AT_COMMANDS)
void ttn_provisioning_task_caller(void* pvParameter);
#endif
// --- LMIC callbacks
@ -67,7 +70,7 @@ void os_getDevKey (u1_t* buf)
TTNProvisioning::TTNProvisioning()
: have_keys(false)
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#if defined(TTN_HAS_AT_COMMANDS)
, uart_queue(nullptr), line_buf(nullptr), line_length(0), last_line_end_char(0), quit_task(false)
#endif
{
@ -76,9 +79,11 @@ TTNProvisioning::TTNProvisioning()
// --- Provisioning task
#if defined(TTN_HAS_AT_COMMANDS)
void TTNProvisioning::startTask()
{
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
#if defined(TTN_CONFIG_UART)
configUART();
#endif
@ -287,7 +292,10 @@ void TTNProvisioning::processLine()
uart_write_bytes(UART_NUM, is_ok ? "OK\r\n" : "ERROR\r\n", is_ok ? 4 : 7);
}
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
#endif
#if defined(TTN_CONFIG_UART)
void TTNProvisioning::configUART()
{

View File

@ -22,23 +22,29 @@ class TTNProvisioning
public:
TTNProvisioning();
void startTask();
bool haveKeys();
bool decodeKeys(const char *dev_eui, const char *app_eui, const char *app_key);
bool fromMAC(const char *app_eui, const char *app_key);
bool saveKeys();
bool restoreKeys(bool silent);
#if defined(TTN_HAS_AT_COMMANDS)
void startTask();
#endif
private:
void provisioningTask();
bool decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const char *app_key);
void addLineData(int numBytes);
void detectLineEnd(int start_at);
void processLine();
bool readNvsValue(nvs_handle handle, const char* key, uint8_t* data, size_t expected_length, bool silent);
bool writeNvsValue(nvs_handle handle, const char* key, const uint8_t* data, size_t len);
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
#if defined(TTN_HAS_AT_COMMANDS)
void provisioningTask();
void addLineData(int numBytes);
void detectLineEnd(int start_at);
void processLine();
#endif
#if defined(TTN_CONFIG_UART)
void configUART();
#endif
@ -53,15 +59,15 @@ private:
private:
bool have_keys = false;
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#if defined(TTN_HAS_AT_COMMANDS)
QueueHandle_t uart_queue;
char* line_buf;
int line_length;
uint8_t last_line_end_char;
bool quit_task;
#endif
friend void ttn_provisioning_task_caller(void* pvParameter);
#endif
};
#endif

View File

@ -93,16 +93,21 @@ bool TheThingsNetwork::provisionWithMAC(const char *appEui, const char *appKey)
return provisioning.saveKeys();
}
void TheThingsNetwork::startProvisioningTask()
{
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#if defined(TTN_HAS_AT_COMMANDS)
provisioning.startTask();
#else
ESP_LOGE(TAG, "AT commands are disabled. Change the configuration using 'make menuconfig'");
ASSERT(0);
esp_restart();
#endif
}
void TheThingsNetwork::waitForProvisioning()
{
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#if defined(TTN_HAS_AT_COMMANDS)
if (isProvisioned())
{
ESP_LOGI(TAG, "Device is already provisioned");
@ -113,6 +118,10 @@ void TheThingsNetwork::waitForProvisioning()
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Device successfully provisioned");
#else
ESP_LOGE(TAG, "AT commands are disabled. Change the configuration using 'make menuconfig'");
ASSERT(0);
esp_restart();
#endif
}