Golioth Zephyr SDK
SDK for development of Zephyr OS powered devices with Golioth
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 <kernel.h>
11 #include <net/coap.h>
12 #include <net/tls_credentials.h>
13 #include <stdint.h>
14 
21 #define GOLIOTH_COAP_MAX_NON_PAYLOAD_LEN 128
22 
23 #define GOLIOTH_MAX_IDENTITY_LEN 32
24 #define GOLIOTH_EMPTY_PACKET_LEN (16 + GOLIOTH_MAX_IDENTITY_LEN)
25 
26 #define GOLIOTH_LIGHTDB_PATH(x) ".d/" x
27 #define GOLIOTH_LIGHTDB_STREAM_PATH(x) ".s/" x
28 
33  const uint8_t *identity;
34  size_t identity_len;
35 };
36 
40 struct golioth_tls {
41  sec_tag_t *sec_tag_list;
42  size_t sec_tag_count;
43 };
44 
49  int proto;
50 
51  union {
53  struct golioth_tls tls;
54  };
55 
56  uint8_t *rx_buffer;
57  size_t rx_buffer_len;
58  size_t rx_received;
59 
60  struct coap_packet rx_packet;
61  struct coap_option rx_options[CONFIG_GOLIOTH_COAP_MAX_OPTIONS];
62 
63  struct k_mutex lock;
64  int sock;
65 
66  void (*on_connect)(struct golioth_client *client);
67  void (*on_message)(struct golioth_client *client,
68  struct coap_packet *rx);
69 };
70 
72 
79  const uint8_t *data,
80  size_t offset, size_t len,
81  bool last);
82 
87  struct coap_block_context block_ctx;
89  const char *path;
91 };
92 
94 
101  const uint8_t *data,
102  size_t offset, size_t len,
103  bool last);
104 
109  struct coap_block_context block_ctx;
111  struct coap_reply *reply;
112  uint8_t token[COAP_TOKEN_MAX_LEN];
114 };
115 
123  struct golioth_blockwise_download_ctx *ctx);
124 
125 static inline void golioth_lock(struct golioth_client *client)
126 {
127  k_mutex_lock(&client->lock, K_FOREVER);
128 }
129 
130 static inline void golioth_unlock(struct golioth_client *client)
131 {
132  k_mutex_unlock(&client->lock);
133 }
134 
143 void golioth_init(struct golioth_client *client);
144 
157 int golioth_connect(struct golioth_client *client, const char *host,
158  uint16_t port);
159 
170 int golioth_disconnect(struct golioth_client *client);
171 
185  uint8_t *identity, size_t identity_len);
186 
203  sec_tag_t *sec_tag_list,
204  size_t sec_tag_count);
205 
218  struct coap_packet *packet);
219 
235  struct coap_packet *packet,
236  uint8_t *data, uint16_t data_len);
237 
246 int golioth_ping(struct golioth_client *client);
247 
259 int golioth_send_hello(struct golioth_client *client);
260 
276 int golioth_lightdb_get(struct golioth_client *client, const uint8_t *path,
277  enum coap_content_format format,
278  struct coap_reply *reply, coap_reply_t reply_cb);
279 
294 int golioth_lightdb_set(struct golioth_client *client, const uint8_t *path,
295  enum coap_content_format format,
296  uint8_t *data, uint16_t data_len);
297 
309 int golioth_lightdb_delete(struct golioth_client *client, const uint8_t *path);
310 
326 int golioth_lightdb_observe(struct golioth_client *client, const uint8_t *path,
327  enum coap_content_format format,
328  struct coap_reply *reply, coap_reply_t reply_cb);
329 
345  struct golioth_blockwise_observe_ctx *ctx,
346  const char *path, struct coap_reply *reply,
348 
360 int golioth_process_rx(struct golioth_client *client);
361 
364 #endif /* GOLIOTH_INCLUDE_NET_GOLIOTH_H_ */
int golioth_observe_blockwise(struct golioth_client *client, struct golioth_blockwise_observe_ctx *ctx, const char *path, struct coap_reply *reply, golioth_blockwise_observe_received_t received_cb)
Observe resource with blockwise updates.
int golioth_lightdb_set(struct golioth_client *client, const uint8_t *path, enum coap_content_format format, uint8_t *data, uint16_t data_len)
Set value to Golioth's Light DB.
void golioth_blockwise_download_init(struct golioth_client *client, struct golioth_blockwise_download_ctx *ctx)
Initialize blockwise download.
int golioth_ping(struct golioth_client *client)
Send PING message to Golioth.
int golioth_set_proto_coap_udp(struct golioth_client *client, uint8_t *identity, size_t identity_len)
Set UDP as transport protocol.
int golioth_process_rx(struct golioth_client *client)
Process incoming data from Golioth.
int golioth_send_coap(struct golioth_client *client, struct coap_packet *packet)
Send CoAP packet to Golioth.
int golioth_disconnect(struct golioth_client *client)
Disconnect from Golioth.
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: golioth.h:100
int golioth_lightdb_observe(struct golioth_client *client, const uint8_t *path, enum coap_content_format format, struct coap_reply *reply, coap_reply_t reply_cb)
Observe value in Golioth's Light DB.
int golioth_lightdb_delete(struct golioth_client *client, const uint8_t *path)
Delete value in Golioth's Light DB.
int golioth_send_hello(struct golioth_client *client)
Send Hello message to Golioth.
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.
int golioth_lightdb_get(struct golioth_client *client, const uint8_t *path, enum coap_content_format format, struct coap_reply *reply, coap_reply_t reply_cb)
Get value from Golioth's Light DB.
int(* golioth_blockwise_observe_received_t)(struct golioth_blockwise_observe_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 observe not...
Definition: golioth.h:78
int golioth_connect(struct golioth_client *client, const char *host, uint16_t port)
Connect to Golioth.
void golioth_init(struct golioth_client *client)
Initialize golioth client instance.
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.
Represents blockwise download transfer from Golioth.
Definition: golioth.h:108
struct coap_block_context block_ctx
Definition: golioth.h:109
golioth_blockwise_download_received_t received_cb
Definition: golioth.h:113
uint8_t token[COAP_TOKEN_MAX_LEN]
Definition: golioth.h:112
struct coap_reply * reply
Definition: golioth.h:111
struct golioth_client * client
Definition: golioth.h:110
Represents a Golioth blockwise observe context.
Definition: golioth.h:86
golioth_blockwise_observe_received_t received_cb
Definition: golioth.h:90
const char * path
Definition: golioth.h:89
struct coap_block_context block_ctx
Definition: golioth.h:87
struct golioth_client * client
Definition: golioth.h:88
Represents a Golioth client instance.
Definition: golioth.h:48
struct k_mutex lock
Definition: golioth.h:63
uint8_t * rx_buffer
Definition: golioth.h:56
size_t rx_received
Definition: golioth.h:58
int sock
Definition: golioth.h:64
struct golioth_unsecure unsecure
Definition: golioth.h:52
struct coap_packet rx_packet
Definition: golioth.h:60
void(* on_connect)(struct golioth_client *client)
Definition: golioth.h:66
struct coap_option rx_options[CONFIG_GOLIOTH_COAP_MAX_OPTIONS]
Definition: golioth.h:61
void(* on_message)(struct golioth_client *client, struct coap_packet *rx)
Definition: golioth.h:67
int proto
Definition: golioth.h:49
size_t rx_buffer_len
Definition: golioth.h:57
struct golioth_tls tls
Definition: golioth.h:53
(D)TLS credentials of Golioth client.
Definition: golioth.h:40
size_t sec_tag_count
Definition: golioth.h:42
sec_tag_t * sec_tag_list
Definition: golioth.h:41
UDP (unsecure) credentials (identity only) of Golioth client.
Definition: golioth.h:32
const uint8_t * identity
Definition: golioth.h:33
size_t identity_len
Definition: golioth.h:34