Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-mqtt.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <sol-buffer.h>
22 #include <sol-certificate.h>
23 #include <sol-common-buildopts.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <inttypes.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
64 typedef enum {
72 
80 
87 } sol_mqtt_qos;
88 
99 
104 
109 
114 
119 };
120 
132 struct sol_mqtt;
133 typedef struct sol_mqtt sol_mqtt;
134 
141 typedef struct sol_mqtt_message {
142 #ifndef SOL_NO_API_VERSION
143 #define SOL_MQTT_MESSAGE_API_VERSION (1)
144 
147  uint16_t api_version;
148 #endif
149 
153  char *topic;
154 
159 
163  int id;
164 
169 
173  bool retain;
175 
179 typedef struct sol_mqtt_handlers {
180 #ifndef SOL_NO_API_VERSION
181 #define SOL_MQTT_HANDLERS_API_VERSION (1)
182 
185  uint16_t api_version;
186 #endif
187 
198  void (*connect)(void *data, struct sol_mqtt *mqtt);
199 
211  void (*disconnect) (void *data, struct sol_mqtt *mqtt);
212 
223  void (*publish) (void *data, struct sol_mqtt *mqtt);
224 
241  void (*message) (void *data, struct sol_mqtt *mqtt, const struct sol_mqtt_message *message);
242 
253  void (*subscribe) (void *data, struct sol_mqtt *mqtt);
254 
265  void (*unsubscribe) (void *data, struct sol_mqtt *mqtt);
267 
271 typedef struct sol_mqtt_config {
272 #ifndef SOL_NO_API_VERSION
273 #define SOL_MQTT_CONFIG_API_VERSION (1)
274 
277  uint16_t api_version;
278 #endif
279 
285 
289  uint16_t port;
290 
295  time_t keep_alive;
296 
301  const char *client_id;
302 
306  const char *username;
307 
311  const char *password;
312 
316  const struct sol_mqtt_message *will;
317 
321  const struct sol_cert *ca_cert;
322 
326  const struct sol_cert *client_cert;
327 
331  const struct sol_cert *private_key;
332 
336  const void *data;
337 
341  const char *host;
342 
348 
356 struct sol_mqtt *sol_mqtt_connect(const struct sol_mqtt_config *config);
357 
365 int sol_mqtt_reconnect(struct sol_mqtt *mqtt);
366 
377 void sol_mqtt_disconnect(struct sol_mqtt *mqtt);
378 
388 int sol_mqtt_get_connection_status(const struct sol_mqtt *mqtt);
389 
398 int sol_mqtt_publish(const struct sol_mqtt *mqtt, struct sol_mqtt_message *message);
399 
409 int sol_mqtt_subscribe(const struct sol_mqtt *mqtt, const char *topic, sol_mqtt_qos qos);
410 
411 #ifndef SOL_NO_API_VERSION
412 
419 #define SOL_MQTT_MESSAGE_CHECK_API_VERSION(msg_, ...) \
420  if (SOL_UNLIKELY((msg_)->api_version != \
421  SOL_MQTT_MESSAGE_API_VERSION)) { \
422  SOL_ERR("Unexpected API version (message is %" PRIu16 ", expected %" PRIu16 ")", \
423  (msg_)->api_version, SOL_MQTT_MESSAGE_API_VERSION); \
424  return __VA_ARGS__; \
425  }
426 #else
427 #define SOL_MQTT_MESSAGE_CHECK_API_VERSION(msg_, ...)
428 #endif
429 
434 #ifdef __cplusplus
435 }
436 #endif
const struct sol_mqtt_handlers handlers
Handlers to be used with this connection.
Definition: sol-mqtt.h:346
const char * client_id
NULL terminated string that should be used as client ID.
Definition: sol-mqtt.h:301
struct sol_mqtt_message sol_mqtt_message
MQTT Message.
const struct sol_mqtt_message * will
A message that the broker should send when the client disconnects.
Definition: sol-mqtt.h:316
bool clean_session
If set, the broker will drop all messages and subscriptions when the client disconnects.
Definition: sol-mqtt.h:284
Broker unavailable at provided host.
Definition: sol-mqtt.h:118
uint16_t api_version
Should always be set to SOL_MQTT_MESSAGE_API_VERSION.
Definition: sol-mqtt.h:147
uint16_t api_version
Should always be set to SOL_MQTT_CONFIG_API_VERSION.
Definition: sol-mqtt.h:277
char * topic
The topic which the message was/will be posted to.
Definition: sol-mqtt.h:153
void(* subscribe)(void *data, struct sol_mqtt *mqtt)
On subscribe callback.
Definition: sol-mqtt.h:253
Client ID rejected by the broker.
Definition: sol-mqtt.h:113
int sol_mqtt_reconnect(struct sol_mqtt *mqtt)
Reestablish the connection to the MQTT broker.
struct sol_cert sol_cert
Certificate handler.
Definition: sol-certificate.h:48
int sol_mqtt_publish(const struct sol_mqtt *mqtt, struct sol_mqtt_message *message)
Send the Broker a message to be published in a given topic.
MQTT callback handlers.
Definition: sol-mqtt.h:179
const char * password
NULL terminated string with the password.
Definition: sol-mqtt.h:311
bool retain
If true, the message will be retained by the broker.
Definition: sol-mqtt.h:173
sol_mqtt_conn_status
Connection status.
Definition: sol-mqtt.h:94
These are routines that Soletta provides for its buffer implementation.
static struct sol_mqtt_message message
Definition: mqtt-publish.c:35
struct sol_mqtt_config sol_mqtt_config
Server Configuration.
sol_mqtt_qos qos
The message Quality of service.
Definition: sol-mqtt.h:168
struct sol_buffer * payload
The message payload.
Definition: sol-mqtt.h:158
const struct sol_cert * client_cert
Client certificate for SSL connections.
Definition: sol-mqtt.h:326
void(* connect)(void *data, struct sol_mqtt *mqtt)
On connect callback.
Definition: sol-mqtt.h:198
const char * host
The host address of the MQTT broker.
Definition: sol-mqtt.h:341
int id
The message Id.
Definition: sol-mqtt.h:163
uint16_t port
The host port to connect.
Definition: sol-mqtt.h:289
struct sol_mqtt * sol_mqtt_connect(const struct sol_mqtt_config *config)
Connect to a MQTT broker.
void sol_mqtt_disconnect(struct sol_mqtt *mqtt)
Disconnect from MQTT Broker.
void(* publish)(void *data, struct sol_mqtt *mqtt)
On publish callback.
Definition: sol-mqtt.h:223
int sol_mqtt_subscribe(const struct sol_mqtt *mqtt, const char *topic, sol_mqtt_qos qos)
Ask the Broker to be subscribed to a given topic.
const struct sol_cert * private_key
Private key for SSL connections.
Definition: sol-mqtt.h:331
Disconnected due to unexpected reasons.
Definition: sol-mqtt.h:98
MQTT protocol rejected by the broker.
Definition: sol-mqtt.h:108
These routines are used for Soletta's certificate handling.
sol_mqtt_qos
MQTT QOS level for message delivery.
Definition: sol-mqtt.h:64
This is the highest quality of service, for use when neither loss nor duplication of messages are acc...
Definition: sol-mqtt.h:86
const void * data
User data provided to the callbacks.
Definition: sol-mqtt.h:336
struct sol_mqtt_handlers sol_mqtt_handlers
MQTT callback handlers.
const struct sol_cert * ca_cert
CA Certificate for SSL connections.
Definition: sol-mqtt.h:321
void(* disconnect)(void *data, struct sol_mqtt *mqtt)
On disconnect callback.
Definition: sol-mqtt.h:211
time_t keep_alive
Time interval between PING messages that should be sent by the broker to the client in miliseconds...
Definition: sol-mqtt.h:295
int sol_mqtt_get_connection_status(const struct sol_mqtt *mqtt)
Get connection status.
static char * topic
Definition: mqtt-publish.c:34
void(* message)(void *data, struct sol_mqtt *mqtt, const struct sol_mqtt_message *message)
On message callback.
Definition: sol-mqtt.h:241
void(* unsubscribe)(void *data, struct sol_mqtt *mqtt)
On unsubscribe callback.
Definition: sol-mqtt.h:265
This quality of service ensures that the message arrives at the receiver at least once...
Definition: sol-mqtt.h:79
The message is delivered according to the capabilities of the underlying network. ...
Definition: sol-mqtt.h:71
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
Successfully connected to the broker.
Definition: sol-mqtt.h:103
struct sol_mqtt sol_mqtt
MQTT Object.
Definition: sol-mqtt.h:133
MQTT Message.
Definition: sol-mqtt.h:141
Server Configuration.
Definition: sol-mqtt.h:271
uint16_t api_version
Should always be set to SOL_MQTT_HANDLERS_API_VERSION.
Definition: sol-mqtt.h:185
const char * username
NULL terminated string with the username.
Definition: sol-mqtt.h:306