Skip to content

Commit

Permalink
New features & Fix more compatibility problems
Browse files Browse the repository at this point in the history
  • Loading branch information
beegee-tokyo committed Aug 27, 2023
1 parent be3c9d1 commit 9de41dd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 122 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Arduino library for RAKWireless WisBlock Core modules that takes all the LoRaWAN

# Release Notes

## NOT_RELEASED YET New features & Fix more compatibility problems with latest WisToolBox version
## 2.0.12 New features & Fix more compatibility problems with latest WisToolBox version
- Give correct ATC+.... return instead of only AT+... for custom AT commands
- Add AT command for force OTA DFU mode on RAK4631
- Add re-initialize function for LoRaWAN (fix problem with confirmed msg NAK)
Expand Down
139 changes: 86 additions & 53 deletions src/at_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,17 +1268,27 @@ static int at_query_join(void)
*/
static int at_exec_join(char *str)
{
uint8_t bJoin;
uint8_t bJoin = 3;
uint8_t autoJoin;
uint8_t nbtrials;
char *param;
bool has_autojoin = false;
bool has_nbtrials = false;

param = strtok(str, ":");

/* check start or stop join parameter */
bJoin = strtol(param, NULL, 0);
if (param == NULL)
{
bJoin = str[0] == '0' ? 0 : 1;
}
else
{
bJoin = strtol(param, NULL, 0);
}
if (bJoin != 1 && bJoin != 0)
{
API_LOG("AT", "bJoin = %d", bJoin);
return AT_ERRNO_PARA_VAL;
}

Expand All @@ -1289,40 +1299,10 @@ static int at_exec_join(char *str)
autoJoin = strtol(param, NULL, 0);
if (autoJoin != 1 && autoJoin != 0)
{
API_LOG("AT", "autoJoin = %d", autoJoin);
return AT_ERRNO_PARA_VAL;
}
g_lorawan_settings.auto_join = (autoJoin == 1 ? true : false);

if (!g_lorawan_settings.lorawan_enable)
{
if (bJoin == 0)
{
if (!g_lorawan_initialized)
{
init_lora();
}
if (!g_lorawan_settings.auto_join)
{
Radio.Sleep();
}
}

if (bJoin == 1)
{
if (!g_lorawan_initialized)
{
init_lora();
}
else
{
Radio.Rx(0);
}
}

save_settings();
return AT_SUCCESS;
}

has_autojoin = true;
param = strtok(NULL, ":");
if (param != NULL)
{
Expand All @@ -1337,38 +1317,91 @@ static int at_exec_join(char *str)
{
return AT_ERRNO_PARA_VAL;
}
g_lorawan_settings.join_trials = nbtrials;
lmh_setConfRetries(nbtrials);
}
has_nbtrials = true;
}
save_settings();
}
bool need_save = false;
if (has_nbtrials)
{
if (g_lorawan_settings.join_trials != nbtrials)
{
need_save = true;
g_lorawan_settings.join_trials = nbtrials;
lmh_setConfRetries(nbtrials);
}
}

if ((bJoin == 1) && !g_lorawan_initialized) // ==0 stop join, not support, yet
if (has_autojoin)
{
if (g_lorawan_settings.auto_join != autoJoin)
{
// Manual join only works if LoRaWAN was not initialized yet.
// If LoRaWAN was already initialized, a restart is required
init_lorawan();
return AT_SUCCESS;
need_save = true;
g_lorawan_settings.auto_join = autoJoin;
}
}

if ((bJoin == 1) && g_lorawan_initialized && (lmh_join_status_get() != LMH_SET))
if (g_lorawan_settings.lorawan_enable)
{
bool started_join = false;
// If join is 0, mark as not joined and put radio into sleep
if (bJoin == 0)
{
// If if not yet joined, start join
delay(100);
lmh_join();
return AT_SUCCESS;
if (!g_lorawan_settings.auto_join)
{
if (!g_lorawan_initialized)
{
init_lora();
}
g_lpwan_has_joined = false;
Radio.Sleep();
}
}
else // bJoin == 1
{
if (!g_lpwan_has_joined)
{
if (!g_lorawan_initialized)
{
init_lorawan();
}
}
else
{
// Nothing to do, already joined
}
started_join = true;
}

if ((bJoin == 1) && g_lorawan_settings.auto_join)
if (g_lorawan_settings.auto_join)
{
// If auto join is set, restart the device to start join process
delay(100);
api_reset();
return AT_SUCCESS;
if (!started_join)
{
if (!g_lpwan_has_joined)
{
if (!g_lorawan_initialized)
{
init_lorawan();
}
else
{
// Start Join process
lmh_join();
}
}
else
{
// Nothing to do, already joined
}
}
}
}

return AT_ERRNO_PARA_VAL;
if (need_save)
{
save_settings();
}
return AT_SUCCESS;
}

/**
Expand Down
69 changes: 1 addition & 68 deletions src/lorawan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,76 +202,9 @@ int8_t init_lorawan(void)
*/
int8_t re_init_lorawan(void)
{
// Setup the EUIs and Keys
lmh_setDevEui(g_lorawan_settings.node_device_eui);
lmh_setAppEui(g_lorawan_settings.node_app_eui);
lmh_setAppKey(g_lorawan_settings.node_app_key);
lmh_setNwkSKey(g_lorawan_settings.node_nws_key);
lmh_setAppSKey(g_lorawan_settings.node_apps_key);
lmh_setDevAddr(g_lorawan_settings.node_dev_addr);

// Setup the LoRaWan init structure
lora_param_init.adr_enable = g_lorawan_settings.adr_enabled;
lora_param_init.tx_data_rate = g_lorawan_settings.data_rate;
lora_param_init.enable_public_network = g_lorawan_settings.public_network;
lora_param_init.nb_trials = g_lorawan_settings.join_trials;
lora_param_init.tx_power = g_lorawan_settings.tx_power;
lora_param_init.duty_cycle = g_lorawan_settings.duty_cycle_enabled;

API_LOG("LORA", "Initialize LoRaWAN for region %s", region_names[g_lorawan_settings.lora_region]);
// Initialize LoRaWan
if (lmh_init(&lora_callbacks, lora_param_init, g_lorawan_settings.otaa_enabled, (eDeviceClass)g_lorawan_settings.lora_class, (LoRaMacRegion_t)g_lorawan_settings.lora_region) != 0)
{
API_LOG("LORA", "Failed to initialize LoRaWAN");
return -2;
}

// For some regions we might need to define the sub band the gateway is listening to
// This must be called AFTER lmh_init()

// Additional check if the subband from the settings is valid
switch ((LoRaMacRegion_t)g_lorawan_settings.lora_region)
{
case LORAMAC_REGION_AS923:
case LORAMAC_REGION_AS923_2:
case LORAMAC_REGION_AS923_3:
case LORAMAC_REGION_AS923_4:
case LORAMAC_REGION_RU864:
if (g_lorawan_settings.subband_channels > 1)
{
g_lorawan_settings.subband_channels = 1;
}
break;
case LORAMAC_REGION_AU915:
case LORAMAC_REGION_US915:
if (g_lorawan_settings.subband_channels > 9)
{
g_lorawan_settings.subband_channels = 1;
}
break;
case LORAMAC_REGION_CN470:
if (g_lorawan_settings.subband_channels > 12)
{
g_lorawan_settings.subband_channels = 1;
}
break;
case LORAMAC_REGION_CN779:
case LORAMAC_REGION_EU433:
case LORAMAC_REGION_IN865:
case LORAMAC_REGION_EU868:
case LORAMAC_REGION_KR920:
if (g_lorawan_settings.subband_channels > 2)
{
g_lorawan_settings.subband_channels = 1;
}
break;
}
lmh_reset_mac();

if (!lmh_setSubBandChannels(g_lorawan_settings.subband_channels))
{
API_LOG("LORA", "lmh_setSubBandChannels failed. Wrong sub band requested?");
return -3;
}
return 0;
}

Expand Down

0 comments on commit 9de41dd

Please sign in to comment.