Skip to content

Commit

Permalink
Add more info to message.
Browse files Browse the repository at this point in the history
Cleanup modem callbacks.

Signed-off-by: Achim Kraus <[email protected]>
  • Loading branch information
boaks committed Aug 1, 2022
1 parent 759c9fe commit 9215ba4
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 191 deletions.
28 changes: 28 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,31 @@ if (COPY_PREBUILDS)
file(COPY_FILE ${CMAKE_BINARY_DIR}/zephyr/merged.hex ${CMAKE_BINARY_DIR}/../prebuild/${BOARD}_full.hex)
file(COPY_FILE ${CMAKE_BINARY_DIR}/zephyr/app_signed.hex ${CMAKE_BINARY_DIR}/../prebuild/${BOARD}_app_signed.hex)
endif()

if (CONFIG_COAP_SEND_INTERVAL)
set(BUILD_SUMUP1 "Build with send interval ${CONFIG_COAP_SEND_INTERVAL} s.")
elseif (CONFIG_COAP_WAKEUP_SEND_INTERVAL)
set(BUILD_SUMUP1 "Build with wakeup send interval ${CONFIG_COAP_WAKEUP_SEND_INTERVAL} s.")
else()
set(BUILD_SUMUP1 "Build without send interval.")
endif()

if (CONFIG_LOCATION_ENABLE)
set(BUILD_SUMUP2 "Build with GNSS.")
else()
set(BUILD_SUMUP2 "Build without GNSS.")
endif()

if (CONFIG_BME680)
set(BUILD_SUMUP3 "Build with BME680.")
elseif (CONFIG_BME680_BSEC)
set(BUILD_SUMUP3 "Build with BME680 BSEC.")
else()
set(BUILD_SUMUP3 "Build without BME680.")
endif()

add_custom_command(TARGET app POST_BUILD
COMMAND echo "${BUILD_SUMUP1}"
COMMAND echo "${BUILD_SUMUP2}"
COMMAND echo "${BUILD_SUMUP3}"
)
38 changes: 32 additions & 6 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,52 @@
menu "CoAP tinyDTLS Settings"

config COAP_SERVER_HOSTNAME
string "CoAP server hostname"
string "CoAP server hostname."
default "californium.eclipseprojects.io"

config COAP_SERVER_ADDRESS_STATIC
string "CoAP server IP address (alternative, if DNS is not available)"
string "CoAP server IP address (alternative, if DNS is not available)."
default "20.47.97.44"

config COAP_SERVER_PORT
int "CoAP server port number (DTLS)"
int "CoAP server port number (DTLS)."
default "5684"

config DTLS_PSK_IDENTITY
string "CoAP/DTLS PSK identity"
string "CoAP/DTLS PSK identity."
default "cali.${imei}"
help
"${imei}" is replaced by the modem's IMEI.
This only work with Californium's PlugTestServer,
which implements "wildcard" identities to
make it easier to connect.
Don't use this on production or other security
relevant use-cases.

config DTLS_PSK_SECRET
string "CoAP/DTLS PSK secret"
string "CoAP/DTLS PSK secret."
default ".fornium"
help
This only work with Californium's PlugTestServer,
and the "cali.*" wildcard identity.
Don't use this on production or other security
relevant use-cases.

config COAP_SEND_INTERVAL
int "CoAP send interval in seconds. 0 disable"
default 0

config COAP_WAKEUP_SEND_INTERVAL
int "Wakeup CoAP send interval in seconds. 0 disable."
default 3500
depends on UDP_PSM_ENABLE
help
Using PSM (Power Sleeping Mode) the modem frequently wakesup to refresh
the network registration. This interval is used to exchange messages
piggybacked on such an wakeup. The interval doesn't trigger such a wakeup,
so it's usully chosen a little smaller than the PSM interval.
Ignored, if COAP_SEND_INTERVAL is enabled.

config COAP_RESOURCE
string "CoAP resource - defaults to Californium's echo resource"
default "echo"
Expand All @@ -49,6 +72,9 @@ config COAP_QUERY_KEEP_ENABLE
bool "Enable CoAP query - keep"
default y

endmenu

menu "Modem Settings"
config UDP_PSM_ENABLE
bool "Enable LTE Power Saving Mode"
default y
Expand All @@ -58,7 +84,7 @@ config UDP_RAI_ENABLE
default y

config MODEM_SAVE_CONFIG_THRESHOLD
int "Modem save configuration threshold in seconds. Default 60s"
int "Modem save configuration threshold in seconds. 0s, don't save, 1s always save. Default 60s"
default 60
help
If the initial network connect takes more than the threshold,
Expand Down
11 changes: 6 additions & 5 deletions prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_MAIN_STACK_SIZE=6000
CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_HW_STACK_PROTECTION=y

CONFIG_REBOOT=y

Expand All @@ -35,7 +34,7 @@ CONFIG_REBOOT=y

# Logging
CONFIG_LOG=y
CONFIG_LOG2_MODE_DEFERRED=y
CONFIG_LOG_MODE_DEFERRED=y
CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP=y

# Network
Expand Down Expand Up @@ -76,21 +75,23 @@ CONFIG_LTE_PSM_REQ_RPTAU="00100001"
# 000xxxxx : 10 minutes
# CONFIG_LTE_PSM_REQ_RPTAU="00000001"

# 000xxxxx : 2 seconds
# 000xxxxx : 2 seconds
CONFIG_LTE_PSM_REQ_RAT="00000100"
# 00000000 : immediately
# CONFIG_LTE_PSM_REQ_RAT="00000000"

## RAI
CONFIG_UDP_RAI_ENABLE=y
CONFIG_LTE_RAI_REQ_VALUE="3"

CONFIG_LTE_LOCK_BANDS=n

#CONFIG_LTE_LOCK_PLMN=y
# CONFIG_LTE_LOCK_PLMN=y
# Germany:
# - Telekom: 26201
# - Vodafone: 26202
# - Telefonica: 26203
#CONFIG_LTE_LOCK_PLMN_STRING="26201"
# CONFIG_LTE_LOCK_PLMN_STRING="26203"

## CoAP
CONFIG_COAP=y
Expand Down
9 changes: 7 additions & 2 deletions src/coap_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ int coap_client_prepare_post(void)
uint8_t *token = (uint8_t *)&coap_current_token;
struct coap_packet request;
struct lte_lc_psm_cfg psm;
uint32_t psm_delays;

#ifdef CONFIG_COAP_QUERY_DELAY_ENABLE
static int query_delay = 0;
Expand Down Expand Up @@ -223,8 +224,8 @@ int coap_client_prepare_post(void)

index += snprintf(buf + index, sizeof(buf) - index, "\n");
start = index;
if (modem_get_psm_status(&psm) == 0) {
index += snprintf(buf + index, sizeof(buf) - index, "PSM: %d/%d [s]", psm.tau,psm.active_time);
if (modem_get_psm_status(&psm, &psm_delays) == 0) {
index += snprintf(buf + index, sizeof(buf) - index, "PSM: TAU %d [s], Act %d [s], Delays %d", psm.tau, psm.active_time, psm_delays);
}
err = modem_get_release_time();
if (err > 0) {
Expand All @@ -234,6 +235,10 @@ int coap_client_prepare_post(void)
index += snprintf(buf + index, sizeof(buf) - index, "Released: %d ms", err);
}
dtls_info("%s", buf + start);
start = index + 1;
index += snprintf(buf + index, sizeof(buf) - index, "\nStat: ");
index += modem_read_statistic(buf + index, sizeof(buf) - index);
dtls_info("%s", buf + start);

#ifdef CONFIG_LOCATION_ENABLE
err = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/coap_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "dtls.h"

#define CLIENT_VERSION "v0.3"
#define CLIENT_VERSION "v0.4"

#define COAP_MAX_RETRANSMISSION 3
#define BAT_LEVEL_SLOTS 10
Expand Down
108 changes: 59 additions & 49 deletions src/dtls_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include "coap_client.h"
#include "dtls.h"
#include "dtls_client.h"
#include "dtls_credentials.h"
#include "dtls_debug.h"
#include "global.h"
Expand Down Expand Up @@ -70,19 +69,20 @@ static int timeout = 0;

/* the wakeup send interval is only effecitve on PSM wakeup */
/* the granularity of this time is therefore the PSM time */
#define NETWORK_WAKEUP_SEND_INTERVAL_S (3600 * 1 - 100) /* approx. 1h */

//#define NETWORK_WAKEUP_SEND_INTERVAL_S (3600 * 1 - 100) /* approx. 1h */

#if (defined CONFIG_LTE_MODE_PREFERENCE_NBIOT_PLMN_PRIO || defined CONFIG_LTE_MODE_PREFERENCE_LTE_M_PLMN_PRIO)
#define NETWORK_TIMEOUT_S 360
#else
#define NETWORK_TIMEOUT_S 360
#define NETWORK_TIMEOUT_S 240
#endif

#define RTT_SLOTS 9
#define RTT_INTERVAL 2000
static unsigned int rtts[RTT_SLOTS + 2] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20};

K_SEM_DEFINE(dtls_trigger_msg, 0, 1);
K_SEM_DEFINE(dtls_trigger_msg, 1, 1);
K_SEM_DEFINE(dtls_trigger_search, 0, 1);

static void reboot()
Expand Down Expand Up @@ -141,7 +141,7 @@ static void reopen_socket(struct dtls_context_t *ctx)
{
int err;
dtls_warn("> reconnect modem");
modem_set_power_modes(0);
modem_set_rai(0);
err = modem_start(K_SECONDS(NETWORK_TIMEOUT_S));
if (err) {
reconnect();
Expand All @@ -153,9 +153,47 @@ static void reopen_socket(struct dtls_context_t *ctx)
dtls_warn("> reopen UDP socket failed, %d, errno %d (%s), reboot", *fd, errno, strerror(errno));
reboot();
}
modem_set_power_modes(1);
modem_set_rai(1);
}
static void dtls_trigger(void)
{
if (request_state == NONE) {
k_sem_give(&dtls_trigger_msg);
}
}

static void dtls_manual_trigger(void)
{
ui_led_op(LED_COLOR_RED, LED_CLEAR);
k_sem_give(&dtls_trigger_search);
dtls_trigger();
}

#if CONFIG_COAP_SEND_INTERVAL > 0
static void dtls_timer_trigger_fn(struct k_work *work)
{
dtls_trigger();
}

static K_WORK_DELAYABLE_DEFINE(dtls_timer_trigger_work, dtls_timer_trigger_fn);

#elif CONFIG_COAP_WAKEUP_SEND_INTERVAL > 0
static void dtls_wakeup_trigger(void)
{
static unsigned long wakeup_next_sent = 0;
unsigned long now = (unsigned long)k_uptime_get();

if (request_state == NONE) {
if (wakeup_next_sent <= now) {
dtls_trigger();
} else {
return;
}
}
wakeup_next_sent = now + ((CONFIG_COAP_WAKEUP_SEND_INTERVAL)*MSEC_PER_SEC);
}
#endif

static void dtls_coap_success(void)
{
long time1 = connected_time - connect_time;
Expand Down Expand Up @@ -207,6 +245,9 @@ static void dtls_coap_success(void)
dtls_info("vbat: %u, %u, %u, %u, %u", bat_level[0], bat_level[1], bat_level[2], bat_level[3], bat_level[4]);
dtls_info(" %u, %u, %u, %u, %u", bat_level[5], bat_level[6], bat_level[7], bat_level[8], bat_level[9]);
}
#if CONFIG_COAP_SEND_INTERVAL > 0
k_work_schedule(&dtls_timer_trigger_work, K_SECONDS(CONFIG_COAP_SEND_INTERVAL));
#endif
}

static void dtls_coap_failure(void)
Expand All @@ -227,6 +268,9 @@ static void dtls_coap_failure(void)
ui_led_op(LED_COLOR_BLUE, LED_CLEAR);
dtls_info("%u/%ldms/%ldms: failure", lte_connections, time1, time2);
transmissions[COAP_MAX_RETRANSMISSION + 1]++;
#if CONFIG_COAP_SEND_INTERVAL > 0
k_work_schedule(&dtls_timer_trigger_work, K_SECONDS(CONFIG_COAP_SEND_INTERVAL));
#endif
}

static int
Expand Down Expand Up @@ -359,14 +403,14 @@ dtls_handle_event(struct dtls_context_t *ctx, session_t *session,
request_state = NONE;
ui_led_op(LED_COLOR_RED, LED_CLEAR);
ui_led_op(LED_COLOR_GREEN, LED_CLEAR);
modem_set_power_modes(1);
modem_set_rai(1);
} else if (DTLS_EVENT_CONNECT == code) {
dtls_info("dtls connect ...");
dtls_connected = 0;
ui_led_op(LED_COLOR_BLUE, LED_CLEAR);
ui_led_op(LED_COLOR_RED, LED_SET);
ui_led_op(LED_COLOR_GREEN, LED_SET);
modem_set_power_modes(0);
modem_set_rai(0);
}
return 0;
}
Expand Down Expand Up @@ -415,7 +459,7 @@ static dtls_handler_t cb = {
.event = dtls_handle_event,
};

void dtls_lte_connected(enum dtls_lte_connect_type type, int connected)
static void dtls_lte_connected(enum dtls_lte_connect_type type, bool connected)
{
if (type == LTE_CONNECT_NETWORK) {
network_connected = connected;
Expand Down Expand Up @@ -507,44 +551,6 @@ static int dtls_init_destination(session_t *destination)
return 0;
}

static void dtls_trigger(void)
{
if (request_state == NONE) {
k_sem_give(&dtls_trigger_msg);
}
}

static void dtls_manual_trigger(void)
{
ui_led_op(LED_COLOR_RED, LED_CLEAR);
k_sem_give(&dtls_trigger_search);
dtls_trigger();
}

static void dtls_wakeup_trigger(void)
{
static unsigned long wakeup_next_sent = 0;
unsigned long now = (unsigned long)k_uptime_get();

if (request_state == NONE) {
if (wakeup_next_sent <= now) {
dtls_trigger();
} else {
return;
}
}
wakeup_next_sent = now + ((NETWORK_WAKEUP_SEND_INTERVAL_S)*MSEC_PER_SEC);
}

#if CONFIG_COAP_SEND_INTERVAL > 0
static void dtls_timer_trigger_fn(struct k_work *work)
{
dtls_trigger();
}

static K_WORK_DELAYABLE_DEFINE(dtls_timer_trigger_work, dtls_timer_trigger_fn);
#endif

#ifdef CONFIG_ADXL362_MOTION_DETECTION
static void accelerometer_handler(const struct accelerometer_evt *const evt)
{
Expand Down Expand Up @@ -584,7 +590,7 @@ int dtls_loop(void)
reboot();
}

modem_set_power_modes(0);
modem_set_rai(0);
imei_len = modem_at_cmd("AT+CGSN", imei, sizeof(imei), NULL);
dtls_credentials_init_psk(0 < imei_len ? imei : NULL);
dtls_credentials_init_handler(&cb);
Expand Down Expand Up @@ -748,7 +754,11 @@ void main(void)

ui_init(dtls_manual_trigger);

modem_init(dtls_wakeup_trigger);
#if CONFIG_COAP_WAKEUP_SEND_INTERVAL > 0 && CONFIG_COAP_SEND_INTERVAL == 0
modem_init(dtls_wakeup_trigger, dtls_lte_connected);
#else
modem_init(NULL, dtls_lte_connected);
#endif

#ifdef CONFIG_LOCATION_ENABLE
#ifdef CONFIG_LOCATION_ENABLE_TRIGGER_MESSAGE
Expand Down
Loading

0 comments on commit 9215ba4

Please sign in to comment.