diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e21778b..f9b2241 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,7 +14,7 @@ jobs: CMAKE_PREFIX_PATH: /opt/toolchains steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: path: thingsboard diff --git a/README.md b/README.md index ccdbe05..4ffa84a 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 diff --git a/include/thingsboard.h b/include/thingsboard.h index a709ca3..41abfea 100644 --- a/include/thingsboard.h +++ b/include/thingsboard.h @@ -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); /** @@ -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; }; /** diff --git a/src/coap_client.c b/src/coap_client.c index 92904f0..a49ce8a 100644 --- a/src/coap_client.c +++ b/src/coap_client.c @@ -1,8 +1,8 @@ #include "coap_client.h" -#include -#include #include +#include +#include #include LOG_MODULE_REGISTER(coap_client); diff --git a/src/coap_client.h b/src/coap_client.h index 3d1f0dd..52f2369 100644 --- a/src/coap_client.h +++ b/src/coap_client.h @@ -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; diff --git a/src/thingsboard.c b/src/thingsboard.c index c73de44..f837094 100644 --- a/src/thingsboard.c +++ b/src/thingsboard.c @@ -5,10 +5,6 @@ #include #include #include -#include -#include -#include -#include #include "coap_client.h" #include "tb_fota.h" @@ -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__); @@ -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 @@ -67,6 +64,7 @@ static void client_handle_attribute_notification(struct coap_client_request *req if (attribute_cb) { attribute_cb(&attr); } + return 0; } /** @@ -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; @@ -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) @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/tests/compile/prj.conf b/tests/compile/prj.conf index ec686ce..5f692bb 100644 --- a/tests/compile/prj.conf +++ b/tests/compile/prj.conf @@ -1,5 +1,4 @@ CONFIG_ZTEST=y -CONFIG_ZTEST_NEW_API=y CONFIG_ZTEST_STACK_SIZE=2048 CONFIG_TEST_RANDOM_GENERATOR=y diff --git a/tests/compile/src/main.c b/tests/compile/src/main.c index 2cc7d4f..4771830 100644 --- a/tests/compile/src/main.c +++ b/tests/compile/src/main.c @@ -1,30 +1,16 @@ #include #include -#include #include -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); diff --git a/tests/compile/testcase.yaml b/tests/compile/testcase.yaml index 1c3356c..b5d351c 100644 --- a/tests/compile/testcase.yaml +++ b/tests/compile/testcase.yaml @@ -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 diff --git a/west.yml b/west.yml index a0931bc..0f39a1f 100644 --- a/west.yml +++ b/west.yml @@ -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