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 <net/golioth/stream.h>
14 #include <stdint.h>
15 #include <zephyr/kernel.h>
16 #include <zephyr/net/coap.h>
17 #include <zephyr/net/tls_credentials.h>
18 
25 #define GOLIOTH_COAP_MAX_NON_PAYLOAD_LEN 128
26 
27 #define GOLIOTH_MAX_IDENTITY_LEN 32
28 #define GOLIOTH_EMPTY_PACKET_LEN (16 + GOLIOTH_MAX_IDENTITY_LEN)
29 
34  GOLIOTH_CONTENT_FORMAT_APP_OCTET_STREAM = COAP_CONTENT_FORMAT_APP_OCTET_STREAM,
35  GOLIOTH_CONTENT_FORMAT_APP_JSON = COAP_CONTENT_FORMAT_APP_JSON,
36  GOLIOTH_CONTENT_FORMAT_APP_CBOR = COAP_CONTENT_FORMAT_APP_CBOR,
37 };
38 
42 struct golioth_tls {
43  sec_tag_t *sec_tag_list;
44  size_t sec_tag_count;
45 };
46 
51  int proto;
52 
53  struct golioth_tls tls;
54 
55  uint8_t *rx_buffer;
56  size_t rx_buffer_len;
57  size_t rx_received;
58 
59  struct coap_packet rx_packet;
60 
61  struct k_mutex lock;
62  int sock;
63 
64  sys_dlist_t coap_reqs;
66  struct k_mutex coap_reqs_lock;
67 
68  void (*on_connect)(struct golioth_client *client);
69 
70  void (*wakeup)(struct golioth_client *client);
71 
72  struct golioth_rpc rpc;
74 };
75 
76 static inline void golioth_lock(struct golioth_client *client)
77 {
78  k_mutex_lock(&client->lock, K_FOREVER);
79 }
80 
81 static inline void golioth_unlock(struct golioth_client *client)
82 {
83  k_mutex_unlock(&client->lock);
84 }
85 
94 void golioth_init(struct golioth_client *client);
95 
107 
120 int golioth_connect(struct golioth_client *client, const char *host,
121  uint16_t port);
122 
133 int golioth_disconnect(struct golioth_client *client);
134 
151  sec_tag_t *sec_tag_list,
152  size_t sec_tag_count);
153 
166  struct coap_packet *packet);
167 
183  struct coap_packet *packet,
184  uint8_t *data, uint16_t data_len);
185 
194 int golioth_ping(struct golioth_client *client);
195 
207 int golioth_send_hello(struct golioth_client *client);
208 
220 int golioth_process_rx(struct golioth_client *client);
221 
231 void golioth_poll_prepare(struct golioth_client *client, int64_t now,
232  int *fd, int64_t *timeout);
233 
236 #endif /* GOLIOTH_INCLUDE_NET_GOLIOTH_H_ */
int golioth_ping(struct golioth_client *client)
Send PING message to Golioth.
static void golioth_lock(struct golioth_client *client)
Definition: net/golioth.h:76
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.
static void golioth_unlock(struct golioth_client *client)
Definition: net/golioth.h:81
int golioth_disconnect(struct golioth_client *client)
Disconnect from Golioth.
void golioth_poll_prepare(struct golioth_client *client, int64_t now, int *fd, int64_t *timeout)
Prepare for poll() system call on transport socket.
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.
bool golioth_is_connected(struct golioth_client *client)
Check if client is connected to Golioth.
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.
golioth_content_format
Set of Content-Format option values for Golioth APIs.
Definition: net/golioth.h:33
@ GOLIOTH_CONTENT_FORMAT_APP_CBOR
Definition: net/golioth.h:36
@ GOLIOTH_CONTENT_FORMAT_APP_JSON
Definition: net/golioth.h:35
@ GOLIOTH_CONTENT_FORMAT_APP_OCTET_STREAM
Definition: net/golioth.h:34
Represents a Golioth client instance.
Definition: net/golioth.h:50
struct golioth_settings settings
Definition: net/golioth.h:73
struct k_mutex lock
Definition: net/golioth.h:61
void(* wakeup)(struct golioth_client *client)
Definition: net/golioth.h:70
struct k_mutex coap_reqs_lock
Definition: net/golioth.h:66
sys_dlist_t coap_reqs
Definition: net/golioth.h:64
uint8_t * rx_buffer
Definition: net/golioth.h:55
size_t rx_received
Definition: net/golioth.h:57
bool coap_reqs_connected
Definition: net/golioth.h:65
struct coap_packet rx_packet
Definition: net/golioth.h:59
void(* on_connect)(struct golioth_client *client)
Definition: net/golioth.h:68
size_t rx_buffer_len
Definition: net/golioth.h:56
struct golioth_rpc rpc
Definition: net/golioth.h:72
struct golioth_tls tls
Definition: net/golioth.h:53
Global/shared RPC state data, placed in struct golioth_client.
Definition: rpc.h:118
Settings state data, placed in struct golioth_client.
Definition: settings.h:110
(D)TLS credentials of Golioth client.
Definition: net/golioth.h:42
size_t sec_tag_count
Definition: net/golioth.h:44
sec_tag_t * sec_tag_list
Definition: net/golioth.h:43