Skip to content

Commit

Permalink
lib: nrf_cloud_coap: add nrf_cloud_coap_bytes_send
Browse files Browse the repository at this point in the history
This patch adds a function to nrf cloud coap that allows
sending of app-specific raw bytes data.

Signed-off-by: Maximilian Deubel <maximilian.deubel@nordicsemi.no>
  • Loading branch information
maxd-nordic committed Aug 4, 2023
1 parent f6f3468 commit 4fbee10
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
12 changes: 12 additions & 0 deletions include/net/nrf_cloud_coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ int nrf_cloud_coap_pgps_url_get(struct nrf_cloud_rest_pgps_request const *const
*/
int nrf_cloud_coap_sensor_send(const char *app_id, double value, int64_t ts_ms);

/**
* @brief Send raw bytes to nRF Cloud.
* @param[in] app_id The app_id identifying the type of data.
* @param[in] buf buffer with binary string.
* @param[in] buf_len length of buf in bytes.
* @param[in] ts_ms Timestamp the data was measured, or NRF_CLOUD_NO_TIMESTAMP.
*
* @retval 0 If successful.
* Otherwise, a (negative) error code is returned.
*/
int nrf_cloud_coap_bytes_send(const char *app_id, uint8_t *buf, size_t buf_len, int64_t ts_ms);

/**
* @brief Send the device location in the @ref nrf_cloud_gnss_data PVT field to nRF Cloud.
*
Expand Down
4 changes: 4 additions & 0 deletions subsys/net/lib/nrf_cloud/Kconfig.nrf_cloud_coap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ config NON_RESP_RETRIES
result in up to this number of retransmissions of the request followed
by waits for a response.

config NRF_CLOUD_COAP_BYTES_MESSAGE_MAX_LEN
int "Maximum length of message encoded in nrf_cloud_coap_bytes_send"
default 64

if WIFI

config NRF_CLOUD_COAP_SEND_SSIDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pvt = {

message_out = {
appId => tstr,
data => tstr/float/int/pvt,
data => bstr/tstr/float/int/pvt,
? ts => uint .size 8
}

Expand Down
2 changes: 2 additions & 0 deletions subsys/net/lib/nrf_cloud/coap/include/msg_encode_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ struct message_out_ts {
struct message_out {
struct zcbor_string _message_out_appId;
union {
struct zcbor_string _message_out_data_bstr;
struct zcbor_string _message_out_data_tstr;
double _message_out_data_float;
int32_t _message_out_data_int;
struct pvt _message_out_data__pvt;
};
enum {
_message_out_data_bstr,
_message_out_data_tstr,
_message_out_data_float,
_message_out_data_int,
Expand Down
29 changes: 17 additions & 12 deletions subsys/net/lib/nrf_cloud/coap/src/msg_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,27 @@ static bool encode_message_out(zcbor_state_t *state, const struct message_out *i
(((((zcbor_uint32_put(state, (1)))) &&
(zcbor_tstr_encode(state, (&(*input)._message_out_appId)))) &&
(((zcbor_uint32_put(state, (2)))) &&
(((*input)._message_out_data_choice == _message_out_data_tstr)
? ((zcbor_tstr_encode(state, (&(*input)._message_out_data_tstr))))
: (((*input)._message_out_data_choice == _message_out_data_float)
? ((zcbor_float64_encode(
state, (&(*input)._message_out_data_float))))
(((*input)._message_out_data_choice == _message_out_data_bstr)
? ((zcbor_bstr_encode(state, (&(*input)._message_out_data_bstr))))
: (((*input)._message_out_data_choice == _message_out_data_tstr)
? ((zcbor_tstr_encode(state,
(&(*input)._message_out_data_tstr))))
: (((*input)._message_out_data_choice ==
_message_out_data_int)
? ((zcbor_int32_encode(
_message_out_data_float)
? ((zcbor_float64_encode(
state,
(&(*input)._message_out_data_int))))
(&(*input)._message_out_data_float))))
: (((*input)._message_out_data_choice ==
_message_out_data__pvt)
? ((encode_pvt(
_message_out_data_int)
? ((zcbor_int32_encode(
state,
(&(*input)._message_out_data__pvt))))
: false))))) &&
(&(*input)._message_out_data_int))))

Check warning on line 140 in subsys/net/lib/nrf_cloud/coap/src/msg_encode.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

subsys/net/lib/nrf_cloud/coap/src/msg_encode.c:140 line length of 107 exceeds 100 columns
: (((*input)._message_out_data_choice ==

Check warning on line 141 in subsys/net/lib/nrf_cloud/coap/src/msg_encode.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

subsys/net/lib/nrf_cloud/coap/src/msg_encode.c:141 line length of 101 exceeds 100 columns
_message_out_data__pvt)
? ((encode_pvt(
state,
(&(*input)._message_out_data__pvt))))

Check warning on line 145 in subsys/net/lib/nrf_cloud/coap/src/msg_encode.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

subsys/net/lib/nrf_cloud/coap/src/msg_encode.c:145 line length of 119 exceeds 100 columns
: false)))))) &&
zcbor_present_encode(&((*input)._message_out_ts_present),
(zcbor_encoder_t *)encode_repeated_message_out_ts, state,
(&(*input)._message_out_ts))) ||
Expand Down
38 changes: 38 additions & 0 deletions subsys/net/lib/nrf_cloud/coap/src/nrf_cloud_coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "nrf_cloud_codec_internal.h"
#include "nrf_cloud_mem.h"
#include "coap_codec.h"
#include "msg_encode.h"

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(nrf_cloud_coap, CONFIG_NRF_CLOUD_COAP_LOG_LEVEL);
Expand Down Expand Up @@ -160,6 +161,43 @@ int nrf_cloud_coap_pgps_url_get(struct nrf_cloud_rest_pgps_request const *const
}
#endif /* CONFIG_NRF_CLOUD_PGPS */

int nrf_cloud_coap_bytes_send(const char *app_id, uint8_t *buf, size_t buf_len, int64_t ts_ms)
{
__ASSERT_NO_MSG(app_id != NULL);
if (buf_len > CONFIG_NRF_CLOUD_COAP_BYTES_MESSAGE_MAX_LEN) {
return -EINVAL;
}
if (!nrf_cloud_coap_is_connected()) {
return -EACCES;
}
int64_t ts = (ts_ms == NRF_CLOUD_NO_TIMESTAMP) ? get_ts() : ts_ms;
static uint8_t buffer[CONFIG_NRF_CLOUD_COAP_BYTES_MESSAGE_MAX_LEN];
int err;
size_t out_len;
struct message_out input = {0};

input._message_out_appId.value = app_id;
input._message_out_appId.len = strlen(app_id);
input._message_out_data_choice = _message_out_data_bstr;
input._message_out_data_bstr.value = buf;
input._message_out_data_bstr.len = buf_len;
input._message_out_ts._message_out_ts = ts;
input._message_out_ts_present = true;

err = cbor_encode_message_out(buffer, sizeof(buffer), &input, &out_len);

if (err) {
LOG_ERR("Error %d encoding message", err);
return -EINVAL;
}
err = nrf_cloud_coap_post("msg/d2c", NULL, buffer, out_len,
COAP_CONTENT_FORMAT_APP_CBOR, false, NULL, NULL);
if (err) {
LOG_ERR("Failed to send POST request: %d", err);
}
return err;
}

int nrf_cloud_coap_sensor_send(const char *app_id, double value, int64_t ts_ms)
{
__ASSERT_NO_MSG(app_id != NULL);
Expand Down

0 comments on commit 4fbee10

Please sign in to comment.