Golioth Zephyr SDK
net/golioth.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Golioth, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef GOLIOTH_INCLUDE_NET_GOLIOTH_H_
8 #define GOLIOTH_INCLUDE_NET_GOLIOTH_H_
9 
10 #include <net/golioth/lightdb.h>
11 #include <net/golioth/rpc.h>
12 #include <net/golioth/settings.h>
13 #include <stdint.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/net/coap.h>
16 #include <zephyr/net/tls_credentials.h>
17 
24 #define GOLIOTH_COAP_MAX_NON_PAYLOAD_LEN 128
25 
26 #define GOLIOTH_MAX_IDENTITY_LEN 32
27 #define GOLIOTH_EMPTY_PACKET_LEN (16 + GOLIOTH_MAX_IDENTITY_LEN)
28 
29 #define GOLIOTH_MAX_NUM_MESSAGE_CALLBACKS 4
30 
35  GOLIOTH_CONTENT_FORMAT_APP_OCTET_STREAM = COAP_CONTENT_FORMAT_APP_OCTET_STREAM,
36  GOLIOTH_CONTENT_FORMAT_APP_JSON = COAP_CONTENT_FORMAT_APP_JSON,
37  GOLIOTH_CONTENT_FORMAT_APP_CBOR = COAP_CONTENT_FORMAT_APP_CBOR,
38 };
39 
43 struct golioth_tls {
44  sec_tag_t *sec_tag_list;
45  size_t sec_tag_count;
46 };
47 
51 struct golioth_client;
52 typedef void (*golioth_message_callback)(struct golioth_client *client,
53  struct coap_packet *rx,
54  void *user_arg);
55 
61  void *user_arg;
62 };
63 
68  int proto;
69 
70  struct golioth_tls tls;
71 
72  uint8_t *rx_buffer;
73  size_t rx_buffer_len;
74  size_t rx_received;
75 
76  struct coap_packet rx_packet;
77  struct coap_option rx_options[CONFIG_GOLIOTH_COAP_MAX_OPTIONS];
78 
79  struct k_mutex lock;
80  int sock;
81 
82  void (*on_connect)(struct golioth_client *client);
83  void (*on_message)(struct golioth_client *client, struct coap_packet *rx);
84 
85  /* Storage for additional on_message callbacks */
88 
89  struct golioth_rpc rpc;
90  struct k_mutex rpc_mutex;
91 
93 };
94 
96 
103  const uint8_t *data,
104  size_t offset, size_t len,
105  bool last);
106 
111  struct coap_block_context block_ctx;
113  struct coap_reply *reply;
114  uint8_t token[COAP_TOKEN_MAX_LEN];
116 };
117 
125  struct golioth_blockwise_download_ctx *ctx);
126 
127 static inline void golioth_lock(struct golioth_client *client)
128 {
129  k_mutex_lock(&client->lock, K_FOREVER);
130 }
131 
132 static inline void golioth_unlock(struct golioth_client *client)
133 {
134  k_mutex_unlock(&client->lock);
135 }
136 
145 void golioth_init(struct golioth_client *client);
146 
157 bool golioth_is_connected(struct golioth_client *client);
158 
171 int golioth_connect(struct golioth_client *client, const char *host,
172  uint16_t port);
173 
184 int golioth_disconnect(struct golioth_client *client);
185 
202  sec_tag_t *sec_tag_list,
203  size_t sec_tag_count);
204 
216 int golioth_send_coap(struct golioth_client *client,
217  struct coap_packet *packet);
218 
233 int golioth_send_coap_payload(struct golioth_client *client,
234  struct coap_packet *packet,
235  uint8_t *data, uint16_t data_len);
236 
245 int golioth_ping(struct golioth_client *client);
246 
258 int golioth_send_hello(struct golioth_client *client);
259 
271 int golioth_process_rx(struct golioth_client *client);
272 
288  golioth_message_callback callback,
289  void *user_arg);
290 
293 #endif /* GOLIOTH_INCLUDE_NET_GOLIOTH_H_ */
golioth_client::rx_buffer_len
size_t rx_buffer_len
Definition: net/golioth.h:73
golioth_client::rx_received
size_t rx_received
Definition: net/golioth.h:74
settings.h
golioth_client::num_message_callbacks
size_t num_message_callbacks
Definition: net/golioth.h:87
golioth_blockwise_download_ctx::block_ctx
struct coap_block_context block_ctx
Definition: net/golioth.h:111
GOLIOTH_CONTENT_FORMAT_APP_OCTET_STREAM
@ GOLIOTH_CONTENT_FORMAT_APP_OCTET_STREAM
Definition: net/golioth.h:35
golioth_client::rpc_mutex
struct k_mutex rpc_mutex
Definition: net/golioth.h:90
golioth_client::on_message
void(* on_message)(struct golioth_client *client, struct coap_packet *rx)
Definition: net/golioth.h:83
golioth_lock
static void golioth_lock(struct golioth_client *client)
Definition: net/golioth.h:127
golioth_blockwise_download_init
void golioth_blockwise_download_init(struct golioth_client *client, struct golioth_blockwise_download_ctx *ctx)
Initialize blockwise download.
golioth_blockwise_download_ctx
Represents blockwise download transfer from Golioth.
Definition: net/golioth.h:110
golioth_disconnect
int golioth_disconnect(struct golioth_client *client)
Disconnect from Golioth.
golioth_client::message_callbacks
struct golioth_message_callback_reg message_callbacks[GOLIOTH_MAX_NUM_MESSAGE_CALLBACKS]
Definition: net/golioth.h:86
golioth_tls::sec_tag_list
sec_tag_t * sec_tag_list
Definition: net/golioth.h:44
golioth_blockwise_download_ctx::token
uint8_t token[COAP_TOKEN_MAX_LEN]
Definition: net/golioth.h:114
golioth_blockwise_download_received_t
int(* golioth_blockwise_download_received_t)(struct golioth_blockwise_download_ctx *ctx, const uint8_t *data, size_t offset, size_t len, bool last)
Type of the callback being called when a single block of data is received as part of CoAP response.
Definition: net/golioth.h:102
golioth_client::lock
struct k_mutex lock
Definition: net/golioth.h:79
golioth_client::rx_packet
struct coap_packet rx_packet
Definition: net/golioth.h:76
golioth_rpc
Global/shared RPC state data, placed in struct golioth_client.
Definition: rpc.h:107
rpc.h
golioth_unlock
static void golioth_unlock(struct golioth_client *client)
Definition: net/golioth.h:132
golioth_message_callback
void(* golioth_message_callback)(struct golioth_client *client, struct coap_packet *rx, void *user_arg)
Definition: net/golioth.h:52
golioth_client::on_connect
void(* on_connect)(struct golioth_client *client)
Definition: net/golioth.h:82
GOLIOTH_MAX_NUM_MESSAGE_CALLBACKS
#define GOLIOTH_MAX_NUM_MESSAGE_CALLBACKS
Definition: net/golioth.h:29
golioth_message_callback_reg
Data associated with a message callback registration.
Definition: net/golioth.h:59
golioth_message_callback_reg::user_arg
void * user_arg
Definition: net/golioth.h:61
golioth_send_coap
int golioth_send_coap(struct golioth_client *client, struct coap_packet *packet)
Send CoAP packet to Golioth.
lightdb.h
golioth_client::tls
struct golioth_tls tls
Definition: net/golioth.h:70
golioth_connect
int golioth_connect(struct golioth_client *client, const char *host, uint16_t port)
Connect to Golioth.
golioth_client::settings
struct golioth_settings settings
Definition: net/golioth.h:92
golioth_client::sock
int sock
Definition: net/golioth.h:80
golioth_client::proto
int proto
Definition: net/golioth.h:68
golioth_blockwise_download_ctx::client
struct golioth_client * client
Definition: net/golioth.h:112
golioth_tls
(D)TLS credentials of Golioth client.
Definition: net/golioth.h:43
golioth_content_format
golioth_content_format
Set of Content-Format option values for Golioth APIs.
Definition: net/golioth.h:34
golioth_init
void golioth_init(struct golioth_client *client)
Initialize golioth client instance.
golioth_ping
int golioth_ping(struct golioth_client *client)
Send PING message to Golioth.
golioth_settings
Settings state data, placed in struct golioth_client.
Definition: settings.h:110
golioth_client::rpc
struct golioth_rpc rpc
Definition: net/golioth.h:89
golioth_blockwise_download_ctx::reply
struct coap_reply * reply
Definition: net/golioth.h:113
golioth_blockwise_download_ctx::received_cb
golioth_blockwise_download_received_t received_cb
Definition: net/golioth.h:115
golioth_client
Represents a Golioth client instance.
Definition: net/golioth.h:67
GOLIOTH_CONTENT_FORMAT_APP_CBOR
@ GOLIOTH_CONTENT_FORMAT_APP_CBOR
Definition: net/golioth.h:37
golioth_process_rx
int golioth_process_rx(struct golioth_client *client)
Process incoming data from Golioth.
golioth_send_coap_payload
int golioth_send_coap_payload(struct golioth_client *client, struct coap_packet *packet, uint8_t *data, uint16_t data_len)
Send CoAP packet with separate payload to Golioth.
golioth_tls::sec_tag_count
size_t sec_tag_count
Definition: net/golioth.h:45
golioth_client::rx_options
struct coap_option rx_options[CONFIG_GOLIOTH_COAP_MAX_OPTIONS]
Definition: net/golioth.h:77
golioth_send_hello
int golioth_send_hello(struct golioth_client *client)
Send Hello message to Golioth.
golioth_register_message_callback
int golioth_register_message_callback(struct golioth_client *client, golioth_message_callback callback, void *user_arg)
Register a callback to be called when a CoAP message is received.
golioth_message_callback_reg::callback
golioth_message_callback callback
Definition: net/golioth.h:60
golioth_set_proto_coap_dtls
int golioth_set_proto_coap_dtls(struct golioth_client *client, sec_tag_t *sec_tag_list, size_t sec_tag_count)
Set DTLS as transport protocol.
GOLIOTH_CONTENT_FORMAT_APP_JSON
@ GOLIOTH_CONTENT_FORMAT_APP_JSON
Definition: net/golioth.h:36
golioth_client::rx_buffer
uint8_t * rx_buffer
Definition: net/golioth.h:72
golioth_is_connected
bool golioth_is_connected(struct golioth_client *client)
Check if client is connected to Golioth.