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  struct coap_option rx_options[CONFIG_GOLIOTH_COAP_MAX_OPTIONS];
61 
62  struct k_mutex lock;
63  int sock;
64 
65  sys_dlist_t coap_reqs;
67  struct k_mutex coap_reqs_lock;
68 
69  void (*on_connect)(struct golioth_client *client);
70 
71  void (*wakeup)(struct golioth_client *client);
72 
73  struct golioth_rpc rpc;
74  struct k_mutex rpc_mutex;
75 
77 };
78 
79 static inline void golioth_lock(struct golioth_client *client)
80 {
81  k_mutex_lock(&client->lock, K_FOREVER);
82 }
83 
84 static inline void golioth_unlock(struct golioth_client *client)
85 {
86  k_mutex_unlock(&client->lock);
87 }
88 
97 void golioth_init(struct golioth_client *client);
98 
110 
123 int golioth_connect(struct golioth_client *client, const char *host,
124  uint16_t port);
125 
136 int golioth_disconnect(struct golioth_client *client);
137 
154  sec_tag_t *sec_tag_list,
155  size_t sec_tag_count);
156 
169  struct coap_packet *packet);
170 
186  struct coap_packet *packet,
187  uint8_t *data, uint16_t data_len);
188 
197 int golioth_ping(struct golioth_client *client);
198 
210 int golioth_send_hello(struct golioth_client *client);
211 
223 int golioth_process_rx(struct golioth_client *client);
224 
234 void golioth_poll_prepare(struct golioth_client *client, int64_t now,
235  int *fd, int64_t *timeout);
236 
239 #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:79
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:84
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:76
struct k_mutex lock
Definition: net/golioth.h:62
void(* wakeup)(struct golioth_client *client)
Definition: net/golioth.h:71
struct k_mutex coap_reqs_lock
Definition: net/golioth.h:67
sys_dlist_t coap_reqs
Definition: net/golioth.h:65
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:66
struct k_mutex rpc_mutex
Definition: net/golioth.h:74
struct coap_packet rx_packet
Definition: net/golioth.h:59
void(* on_connect)(struct golioth_client *client)
Definition: net/golioth.h:69
struct coap_option rx_options[CONFIG_GOLIOTH_COAP_MAX_OPTIONS]
Definition: net/golioth.h:60
size_t rx_buffer_len
Definition: net/golioth.h:56
struct golioth_rpc rpc
Definition: net/golioth.h:73
struct golioth_tls tls
Definition: net/golioth.h:53
Global/shared RPC state data, placed in struct golioth_client.
Definition: rpc.h:107
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