This repository was archived by the owner on Oct 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
add function for RPC calls #22
Open
twendtland
wants to merge
7
commits into
main
Choose a base branch
from
feature/rpc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b2a445e
add function for RPC calls
twendtland 1c124d8
update README
twendtland 9c0df56
add RPC sample
twendtland e9c2e9d
use RPC function for time
twendtland be85436
drop
twendtland 06dd351
drop
twendtland ff223a7
drop
twendtland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| cmake_minimum_required(VERSION 3.20.0) | ||
| find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
| project(rpc-sample) | ||
|
|
||
| target_sources(app PRIVATE src/main.c) | ||
|
|
||
| target_compile_options(app PRIVATE | ||
| -Wall | ||
| -Werror | ||
| -Wno-unused-parameter | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| CONFIG_LOG=y | ||
| CONFIG_SHELL=y | ||
| CONFIG_SHELL_MINIMAL=y | ||
| CONFIG_COAP_INIT_ACK_TIMEOUT_MS=4000 | ||
|
|
||
| # Cellular connectivity | ||
| CONFIG_NRF_MODEM_LIB=y | ||
| CONFIG_NRF_MODEM_LIB_LOG_FW_VERSION_UUID=y | ||
| CONFIG_LTE_LINK_CONTROL=y | ||
| CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT=y | ||
| CONFIG_LTE_MODE_PREFERENCE_LTE_M=y | ||
| CONFIG_LTE_PSM_REQ=y | ||
| CONFIG_LTE_EDRX_REQ=y | ||
| CONFIG_NET_SOCKETS_OFFLOAD=y | ||
|
|
||
| # Requirements for Thingsboard SDK | ||
| CONFIG_COAP=y | ||
| CONFIG_NETWORKING=y | ||
| CONFIG_NET_IPV4=y | ||
| CONFIG_NET_SOCKETS=y | ||
| CONFIG_JSON_LIBRARY=y | ||
| CONFIG_FLASH=y | ||
| CONFIG_FLASH_MAP=y | ||
| CONFIG_NVS=y | ||
| CONFIG_SETTINGS=y | ||
|
|
||
| # Thingsboard SDK | ||
| CONFIG_THINGSBOARD=y | ||
| CONFIG_THINGSBOARD_USE_PROVISIONING=n | ||
| # Set server name and access token | ||
| # CONFIG_COAP_SERVER_HOSTNAME="" | ||
| # CONFIG_THINGSBOARD_ACCESS_TOKEN="" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #include <thingsboard.h> | ||
|
|
||
| #include <modem/lte_lc.h> | ||
| #include <modem/nrf_modem_lib.h> | ||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/logging/log.h> | ||
| #include <zephyr/settings/settings.h> | ||
| #include <zephyr/shell/shell.h> | ||
|
|
||
| #include <string.h> | ||
|
|
||
| LOG_MODULE_REGISTER(main); | ||
|
|
||
| static struct tb_fw_id fw_id = { | ||
| .fw_title = "rpc-sample", .fw_version = "v1.0.0", .device_name = "sample-device"}; | ||
|
|
||
| int main(void) | ||
| { | ||
| int err = 0; | ||
|
|
||
| LOG_INF("Initializing modem library"); | ||
| err = nrf_modem_lib_init(); | ||
| if (err) { | ||
| LOG_ERR("Failed to initialize the modem library, error (%d): %s", err, | ||
| strerror(-err)); | ||
| return err; | ||
| } | ||
|
|
||
| err = lte_lc_func_mode_set(LTE_LC_FUNC_MODE_ACTIVATE_LTE); | ||
| if (err) { | ||
| LOG_ERR("Failed to activate LTE"); | ||
| return err; | ||
| } | ||
|
|
||
| LOG_INF("Connecting to LTE network"); | ||
| err = lte_lc_connect(); | ||
| if (err) { | ||
| LOG_ERR("Could not establish LTE connection, error (%d): %s", err, strerror(-err)); | ||
| return err; | ||
| } | ||
| LOG_INF("LTE connection established"); | ||
|
|
||
| LOG_INF("Connecting to Thingsboards"); | ||
| err = thingsboard_init(NULL, &fw_id); | ||
| if (err) { | ||
| LOG_ERR("Could not initialize thingsboard connection, error (%d) :%s", err, | ||
| strerror(-err)); | ||
| return err; | ||
| } | ||
| } | ||
|
|
||
| static void rpc_callback(const uint8_t *data, size_t len) | ||
| { | ||
| LOG_HEXDUMP_INF(data, len, "RPC repsonse: "); | ||
| } | ||
|
|
||
| static int cmd_do_rpc(const struct shell *shell, size_t argc, char **argv) | ||
| { | ||
| int err; | ||
|
|
||
| const char *params = "{\"name\":\"gcx\"}"; | ||
| err = thingsboard_rpc("hello", rpc_callback, params); | ||
| if (err) { | ||
| LOG_ERR("Could not send RPC, error (%d): %s", err, strerror(-err)); | ||
| return err; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| SHELL_CMD_REGISTER(rpc, NULL, "Send a RPC request", cmd_do_rpc); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The callback will be overwritten by another one if the user calls
thingsboad_rpcmultiple times without waiting for the response. When the response arrives the wrong callback would be called.Avoiding multiple RPCs in flight with a semaphore approach that you mentioned yesterday would probably solve this. Or just reset
rpc_cbtoNULLafter it was called and return with an error fromthingsboad_rpc()ifrpc_cbis notNULL?A nicer solution would be to allow simultaneous RPC calls by using a memory slab to store multiple callbacks (the same way coap_client.c manages multiple requests).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, the limitation is obvious. I will think about a solution, being able to do multiple RPC calls simultaneously seems like something that's not often used, but could come in handy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be fine with having this limitations as it is probably okay for most use cases. We should just document this and somehow enforce the "only one rpc at a time" rule to avoid weird behaviour (like wrong callback called) for the user.