Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to NRF SDK v2.7.0 #6

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
CMAKE_PREFIX_PATH: /opt/toolchains
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
path: thingsboard

Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ manifest:
projects:
- name: thingsboard-sdk
remote: gcx
revision: v2.5.0
revision: v2.7.0
path: modules/thingsboard
import: true
self:
Expand Down Expand Up @@ -48,13 +48,20 @@ static void handle_attr_update(struct thingsboard_attr *attr) {
int main() {
fw_id.fw_title = "my-app";
fw_id.fw_version = fw_version_str;
fw_id.device_name = device_name;

thingsboard_init(handle_attr_update, &fw_id);
}
```

### Connecting
The module takes care of modem enabling, socket creating and connecting to the server automatically. You just have to configure URL and port of the Thingsboard CoAP server, using `config COAP_CLIENT_NUM_MSGS` and `config COAP_CLIENT_MSG_LEN`. CoAP reliability can be fine-tuned using `config COAP_NUM_RETRIES` and the Zephyr-internal `config COAP_INIT_ACK_TIMEOUT_MS`. Using NB-IoT, 15000 is a good starting value for the latter.

The module takes care of socket creating and connecting to the server automatically. You just have
to configure URL and port of the Thingsboard CoAP server, using `config COAP_CLIENT_NUM_MSGS` and
`config COAP_CLIENT_MSG_LEN`.

CoAP reliability can be fine-tuned using `config COAP_NUM_RETRIES` and the Zephyr-internal
`config COAP_INIT_ACK_TIMEOUT_MS`. Using NB-IoT, 15000 is a good starting value for the latter.

### Sending telemetry to cloud
```c
Expand Down
7 changes: 5 additions & 2 deletions include/thingsboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ time_t thingsboard_time(void);
* Same as thingsboard_time, but the return value is not truncated to
* seconds. Please be aware that no special care is taken to guarantee
* the accuracy of the time. Due to network latency, the time will
* be off in the order of multiple seconds.
*/
* be off in the order of multiple seconds.
*/
time_t thingsboard_time_msec(void);

/**
Expand All @@ -52,6 +52,9 @@ struct tb_fw_id {
* FOTA page.
*/
const char *fw_version;

/** Name of your device. for example the ICCID of the SIM-Card. */
const char *device_name;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/coap_client.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "coap_client.h"

#include <zephyr/net/socket.h>
#include <zephyr/random/rand32.h>
#include <stdio.h>
#include <zephyr/net/socket.h>
#include <zephyr/random/random.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(coap_client);
Expand Down
2 changes: 1 addition & 1 deletion src/coap_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

struct coap_client_request;

typedef void (*coap_reply_handler_t)(struct coap_client_request *req, struct coap_packet *resp);
typedef int (*coap_reply_handler_t)(struct coap_client_request *req, struct coap_packet *resp);

struct coap_client_request {
struct coap_packet pkt;
Expand Down
83 changes: 11 additions & 72 deletions src/thingsboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#include <zephyr/kernel.h>
#include <zephyr/net/coap.h>
#include <thingsboard_attr_parser.h>
#include <modem/at_cmd_parser.h>
#include <modem/at_params.h>
#include <modem/lte_lc.h>
#include <modem/modem_info.h>

#include "coap_client.h"
#include "tb_fota.h"
Expand Down Expand Up @@ -36,7 +32,8 @@ K_WORK_DELAYABLE_DEFINE(work_time, time_worker);

static const char *access_token;

static void client_handle_attribute_notification(struct coap_client_request *req, struct coap_packet *response)
static int client_handle_attribute_notification(struct coap_client_request *req,
struct coap_packet *response)
{
LOG_INF("%s", __func__);

Expand All @@ -50,14 +47,14 @@ static void client_handle_attribute_notification(struct coap_client_request *req
payload = (uint8_t*)coap_packet_get_payload(response, &payload_len);
if (!payload_len) {
LOG_WRN("Received empty attributes");
return;
return payload_len;
}
LOG_HEXDUMP_DBG(payload, payload_len, "Received attributes");

err = thingsboard_attr_from_json(payload, payload_len, &attr);
if (err < 0) {
LOG_ERR("Parsing attributes failed");
return;
return err;
}

#ifdef CONFIG_THINGSBOARD_FOTA
Expand All @@ -67,6 +64,7 @@ static void client_handle_attribute_notification(struct coap_client_request *req
if (attribute_cb) {
attribute_cb(&attr);
}
return 0;
}

/**
Expand Down Expand Up @@ -99,7 +97,8 @@ static int timestamp_from_buf(int64_t *value, const void *buf, size_t sz) {
return 0;
}

static void client_handle_time_response(struct coap_client_request *req, struct coap_packet *response)
static int client_handle_time_response(struct coap_client_request *req,
struct coap_packet *response)
{
int64_t ts = 0;
const uint8_t *payload;
Expand All @@ -111,19 +110,20 @@ static void client_handle_time_response(struct coap_client_request *req, struct
payload = coap_packet_get_payload(response, &payload_len);
if (!payload_len) {
LOG_WRN("Received empty timestamp");
return;
return payload_len;
}

err = timestamp_from_buf(&ts, payload, payload_len);
if (err) {
LOG_ERR("Parsing of time response failed");
return;
return err;
}

tb_time.tb_time = ts;
tb_time.own_time = k_uptime_get();

k_sem_give(&time_sem);
return 0;
}

static int client_subscribe_to_attributes(void)
Expand Down Expand Up @@ -190,24 +190,6 @@ static void time_worker(struct k_work *work) {
k_work_schedule(&work_time, K_MSEC(TIME_RETRY_INTERVAL));
}

static void modem_configure(void)
{
#if defined(CONFIG_LTE_LINK_CONTROL)
if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
/* Do nothing, modem is already turned on
* and connected.
*/
} else {
int err;

LOG_INF("LTE Link Connecting ...");
err = lte_lc_init_and_connect();
__ASSERT(err == 0, "LTE link could not be established.");
LOG_INF("LTE Link Connected!");
}
#endif /* defined(CONFIG_LTE_LINK_CONTROL) */
}

int thingsboard_send_telemetry(const void *payload, size_t sz) {
int err;

Expand All @@ -221,38 +203,6 @@ int thingsboard_send_telemetry(const void *payload, size_t sz) {
return 0;
}

static void print_modem_info(void) {
char info_name[50];
char modem_info[50];

enum modem_info infos[] = {
MODEM_INFO_FW_VERSION,
MODEM_INFO_UICC,
MODEM_INFO_IMSI,
MODEM_INFO_ICCID,
MODEM_INFO_APN,
MODEM_INFO_IP_ADDRESS
};

int ret;

for (size_t i = 0; i < ARRAY_SIZE(infos); i++) {
ret = modem_info_string_get(infos[i],
modem_info,
sizeof(modem_info));
if (ret < 0) {
return;
}
ret = modem_info_name_get(infos[i],
info_name);
if (ret < 0 || ret > sizeof(info_name)) {
return;
}
info_name[ret] = '\0';
LOG_INF("Value of %s is %s", info_name, modem_info);
}
}

static void start_client(void);

static const struct tb_fw_id *current_fw;
Expand All @@ -274,20 +224,13 @@ static void prov_callback(const char *token) {

static void start_client(void) {
int err;
char name[30];

LOG_INF("%s", __func__);

if (!access_token) {
LOG_INF("Access token missing, requesting provisioning");

err = modem_info_string_get(MODEM_INFO_ICCID, name, sizeof(name));
if (err < 0) {
LOG_ERR("Could not fetch ICCID");
return;
}

err = thingsboard_provision_device(name, prov_callback);
err = thingsboard_provision_device(current_fw->device_name, prov_callback);
if (err) {
LOG_ERR("Could not provision device");
return;
Expand Down Expand Up @@ -315,10 +258,6 @@ int thingsboard_init(attr_write_callback_t cb, const struct tb_fw_id *fw_id) {

current_fw = fw_id;

modem_configure();

print_modem_info();

if (coap_client_init(start_client) != 0) {
LOG_ERR("Failed to initialize CoAP client");
return -1;
Expand Down
1 change: 0 additions & 1 deletion tests/compile/prj.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_ZTEST_STACK_SIZE=2048
CONFIG_TEST_RANDOM_GENERATOR=y

Expand Down
20 changes: 3 additions & 17 deletions tests/compile/src/main.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
#include <thingsboard.h>
#include <zephyr/ztest.h>

#include <modem/modem_info.h>
#include <zephyr/sys/reboot.h>

void sys_reboot(int type)
{
while(1) {}
}

int modem_info_string_get(enum modem_info info, char *buf,
const size_t buf_size) {
return 0;
}

int modem_info_name_get(enum modem_info info, char *name) {
return 0;
}

static void attr_write_callback(struct thingsboard_attr *attr) {

}

static const struct tb_fw_id fw_id = {
.fw_title = "tb_test",
.fw_version = "1"
.fw_title = "tb_test",
.fw_version = "1",
.device_name = "123456789",
};

ZTEST_SUITE(tb_compile, NULL, NULL, NULL, NULL, NULL);
Expand Down
14 changes: 12 additions & 2 deletions tests/compile/testcase.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
common:
platform_allow:
- qemu_cortex_m3
- native_posix
tests:
thingsboard.compile:
platform_allow: qemu_cortex_m3
thingsboard.compile: {}
thingsboard.compile_provisioning:
extra_configs:
- CONFIG_THINGSBOARD_USE_PROVISIONING=y
- CONFIG_SETTINGS=y
thingsboard.compile_fota:
extra_configs:
- CONFIG_THINGSBOARD_FOTA=y
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ manifest:
- name: nrf
remote: github
repo-path: nrfconnect/sdk-nrf
revision: v2.5.0
revision: v2.7.0
path: nrf
import: true

Expand Down