Arduino WolkConnect library  3.1.4
WolkConn.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 WolkAbout Technology s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
21 #ifndef WOLK_H
22 #define WOLK_H
23 
24 #include "MQTTClient.h"
25 #include "utility/WolkQueue.h"
26 #include "utility/parser.h"
27 #include "utility/size_definitions.h"
28 #include "utility/actuator_status.h"
29 #include "utility/outbound_message.h"
30 #include "utility/outbound_message_factory.h"
31 #include "utility/dtostrf_fix.h"
32 #include "utility/wolk_utils.h"
33 
34 #include "utility/in_memory_persistence.h"
35 #include "utility/persistence.h"
36 
37 #include "Arduino.h"
38 
39 #include <stdbool.h>
40 #include <stdint.h>
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #define FW_VERSION_MAJOR 3 // number 0 -99
47 #define FW_VERSION_MINOR 1 // number 0 -99
48 #define FW_VERSION_PATCH 4 // number 0 -99
49 
50 
54 typedef enum { PROTOCOL_JSON_SINGLE = 0 } protocol_t;
58 typedef unsigned char WOLK_ERR_T;
62 typedef unsigned char WOLK_BOOL_T;
63 enum WOLK_BOOL_T_values { W_FALSE = 0, W_TRUE = 1 };
64 
72 typedef void (*actuation_handler_t)(const char* reference, const char* value);
78 typedef actuator_status_t (*actuator_status_provider_t)(const char* reference);
79 
88 typedef void (*configuration_handler_t)(char (*reference)[CONFIGURATION_REFERENCE_SIZE],
89  char (*value)[CONFIGURATION_VALUE_SIZE], size_t num_configuration_items);
97 typedef size_t (*configuration_provider_t)(char (*reference)[CONFIGURATION_REFERENCE_SIZE],
98  char (*value)[CONFIGURATION_VALUE_SIZE], size_t max_num_configuration_items);
104 typedef struct _wolk_ctx_t wolk_ctx_t;
105 
106 struct _wolk_ctx_t {
107  int sock;
108  PubSubClient *mqtt_client;
109 
116  char device_key[DEVICE_KEY_SIZE];
117  char device_password[DEVICE_PASSWORD_SIZE];
120  parser_t parser;
121 
122  const char** actuator_references;
123  uint32_t num_actuator_references;
124 
125  bool is_keep_alive_enabled;
126  unsigned long millis_last_ping;
127 
128  bool is_initialized;
129  bool is_connected;
130  bool pong_received;
131 
132  persistence_t persistence;
133 
134  unsigned long epoch_time;
136 };
137 
138 
166  const char* device_key, const char* device_password, PubSubClient *client,
167  const char *server, int port, protocol_t protocol, const char** actuator_references,
168  uint32_t num_actuator_references);
180 WOLK_ERR_T wolk_init_in_memory_persistence(wolk_ctx_t* ctx, void* storage, uint32_t size, bool wrap);
195 WOLK_ERR_T wolk_init_custom_persistence(wolk_ctx_t* ctx, persistence_push_t push, persistence_peek_t peek,
196  persistence_pop_t pop, persistence_is_empty_t is_empty);
197 
198 
218 WOLK_ERR_T wolk_connect (wolk_ctx_t *ctx);
219 
225 WOLK_ERR_T wolk_disconnect (wolk_ctx_t *ctx);
226 
237 WOLK_ERR_T wolk_process (wolk_ctx_t *ctx);
238 
248 WOLK_ERR_T wolk_add_string_sensor_reading(wolk_ctx_t *ctx,const char *reference,const char *value, uint32_t utc_time);
249 
260 WOLK_ERR_T wolk_add_multi_value_string_sensor_reading(wolk_ctx_t* ctx, const char* reference,
261  const char (*values)[READING_SIZE], uint16_t values_size,
262  uint32_t utc_time);
272 WOLK_ERR_T wolk_add_numeric_sensor_reading(wolk_ctx_t *ctx,const char *reference,double value, uint32_t utc_time);
273 
285 WOLK_ERR_T wolk_add_multi_value_numeric_sensor_reading(wolk_ctx_t* ctx, const char* reference, double* values,
286  uint16_t values_size, uint32_t utc_time);
296 WOLK_ERR_T wolk_add_bool_sensor_reading(wolk_ctx_t *ctx,const char *reference,bool value, uint32_t utc_time);
297 
309 WOLK_ERR_T wolk_add_multi_value_bool_sensor_reading(wolk_ctx_t* ctx, const char* reference, bool* values,
310  uint16_t values_size, uint32_t utc_time);
311 
322 WOLK_ERR_T wolk_add_alarm(wolk_ctx_t* ctx, const char* reference, bool state, uint32_t utc_time);
323 
333 WOLK_ERR_T wolk_publish_actuator_status (wolk_ctx_t *ctx,const char *reference);
334 
343 WOLK_ERR_T wolk_publish_configuration(wolk_ctx_t* ctx);
344 
352 WOLK_ERR_T wolk_publish(wolk_ctx_t* ctx);
353 
361 WOLK_ERR_T wolk_disable_keep_alive(wolk_ctx_t* ctx);
362 
371 WOLK_ERR_T wolk_update_epoch(wolk_ctx_t* ctx);
372 
373 
374 #ifdef __cplusplus
375 }
376 #endif
377 
378 #endif
379 
WOLK_ERR_T wolk_add_alarm(wolk_ctx_t *ctx, const char *reference, bool state, uint32_t utc_time)
Add alarm.
WOLK_ERR_T wolk_add_multi_value_string_sensor_reading(wolk_ctx_t *ctx, const char *reference, const char(*values)[READING_SIZE], uint16_t values_size, uint32_t utc_time)
Add multi-value string reading.
WOLK_ERR_T wolk_connect(wolk_ctx_t *ctx)
Connect to WolkAbout IoT Platform.
Definition: WolkConn.h:106
WOLK_ERR_T wolk_init_custom_persistence(wolk_ctx_t *ctx, persistence_push_t push, persistence_peek_t peek, persistence_pop_t pop, persistence_is_empty_t is_empty)
Initializes persistence mechanism with custom implementation.
WOLK_ERR_T wolk_publish(wolk_ctx_t *ctx)
Publish accumulated sensor readings, and alarms.
actuator_status_t(* actuator_status_provider_t)(const char *reference)
Declaration of actuator status.
Definition: WolkConn.h:78
WOLK_ERR_T wolk_add_bool_sensor_reading(wolk_ctx_t *ctx, const char *reference, bool value, uint32_t utc_time)
Add bool reading.
size_t(* configuration_provider_t)(char(*reference)[CONFIGURATION_REFERENCE_SIZE], char(*value)[CONFIGURATION_VALUE_SIZE], size_t max_num_configuration_items)
Declaration of configuration provider.
Definition: WolkConn.h:97
char device_key[DEVICE_KEY_SIZE]
Definition: WolkConn.h:116
WOLK_ERR_T wolk_add_string_sensor_reading(wolk_ctx_t *ctx, const char *reference, const char *value, uint32_t utc_time)
Add string reading.
char device_password[DEVICE_PASSWORD_SIZE]
Definition: WolkConn.h:117
WOLK_ERR_T wolk_process(wolk_ctx_t *ctx)
Must be called periodically to keep the connection to the WolkAbout IoT platform alive, obtain and perform actuation requests. It also serves as a diagnostic tool for the MQTT client, printing it&#39;s state when an error occurs. Client state will be -3 if the connection is lost and -4 if the broker can&#39;t be reached.
WOLK_ERR_T wolk_init(wolk_ctx_t *ctx, actuation_handler_t actuation_handler, actuator_status_provider_t actuator_status_provider, configuration_handler_t configuration_handler, configuration_provider_t configuration_provider, const char *device_key, const char *device_password, PubSubClient *client, const char *server, int port, protocol_t protocol, const char **actuator_references, uint32_t num_actuator_references)
Initializes WolkAbout IoT Platform connector context.
unsigned char WOLK_ERR_T
WOLK_ERR_T Boolean used for error handling in Wolk connection module.
Definition: WolkConn.h:58
actuator_status_provider_t actuator_status_provider
Definition: WolkConn.h:111
protocol_t
Supported protocols, WolkConnect libararies currently support only PROTOCOL_JSON_SINGLE.
Definition: WolkConn.h:54
configuration_handler_t configuration_handler
Definition: WolkConn.h:113
WOLK_ERR_T wolk_init_in_memory_persistence(wolk_ctx_t *ctx, void *storage, uint32_t size, bool wrap)
Initializes persistence mechanism with in-memory implementation.
WOLK_ERR_T wolk_add_multi_value_bool_sensor_reading(wolk_ctx_t *ctx, const char *reference, bool *values, uint16_t values_size, uint32_t utc_time)
Add multi-value bool reading.
WOLK_ERR_T wolk_update_epoch(wolk_ctx_t *ctx)
Requests the epoch time from the platform and awaits reply. If the reply did not arrive within 60 sec...
WOLK_ERR_T wolk_publish_configuration(wolk_ctx_t *ctx)
Obtains configuration via configuration_provider and publishes it. If configuration can not be publis...
WOLK_ERR_T wolk_disable_keep_alive(wolk_ctx_t *ctx)
Disables internal keep alive mechanism.
WOLK_ERR_T wolk_disconnect(wolk_ctx_t *ctx)
Disconnect from WolkAbout IoT Platform.
unsigned char WOLK_BOOL_T
WOLK_ERR_T Boolean used in Wolk connection module.
Definition: WolkConn.h:62
unsigned long epoch_time
Definition: WolkConn.h:134
WOLK_ERR_T wolk_publish_actuator_status(wolk_ctx_t *ctx, const char *reference)
Obtains actuator status via actuator_status_provider_t and publishes it. If actuator status can not b...
actuation_handler_t actuation_handler
Definition: WolkConn.h:110
WOLK_ERR_T wolk_add_multi_value_numeric_sensor_reading(wolk_ctx_t *ctx, const char *reference, double *values, uint16_t values_size, uint32_t utc_time)
Add multi-value numeric reading.
protocol_t protocol
Definition: WolkConn.h:119
WOLK_ERR_T wolk_add_numeric_sensor_reading(wolk_ctx_t *ctx, const char *reference, double value, uint32_t utc_time)
Add numeric reading.
void(* configuration_handler_t)(char(*reference)[CONFIGURATION_REFERENCE_SIZE], char(*value)[CONFIGURATION_VALUE_SIZE], size_t num_configuration_items)
Declaration of configuration handler. Configuration reference and value are the pairs of data on the ...
Definition: WolkConn.h:88
void(* actuation_handler_t)(const char *reference, const char *value)
Declaration of actuator handler. Actuator reference and value are the pairs of data on the same place...
Definition: WolkConn.h:72
configuration_provider_t configuration_provider
Definition: WolkConn.h:114