TizenRT Public API  v2.0 M2
mqtt_api.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
29 #ifndef __MQTT_API_H__
30 #define __MQTT_API_H__
31 
32 /****************************************************************************
33  * Included Files
34  ****************************************************************************/
35 #include <tinyara/config.h>
36 
37 #ifdef __cplusplus
38 #define EXTERN extern "C"
39 extern "C" {
40 #else
41 #define EXTERN extern
42 #endif
43 
44 /****************************************************************************
45  * Pre-processor Definitions
46  ****************************************************************************/
47 #define MQTT_DEFAULT_BROKER_PORT 1883
48 #define MQTT_SECURITY_BROKER_PORT 8883
49 #define MQTT_DEFAULT_KEEP_ALIVE_TIME 60
50 #define MQTT_PROTOCOL_VERSION_31 3
51 #define MQTT_PROTOCOL_VERSION_311 4
52 
53 /****************************************************************************
54  * Enumeration
55  ****************************************************************************/
60  MQTT_CLIENT_STATE_NOT_CONNECTED = 0,
61  MQTT_CLIENT_STATE_CONNECTED,
62  MQTT_CLIENT_STATE_CONNECT_REQUEST,
63  MQTT_CLIENT_STATE_DISCONNECT_REQUEST,
64 };
65 
70  MQTT_CONN_ACCEPTED = 0,
71  MQTT_CONN_REFUSED_UNACCEPTABLE_PROTOCOL_VER,
72  MQTT_CONN_REFUSED_ID_REJECTED,
73  MQTT_CONN_REFUSED_BROKER_UNAVAILABLE,
74  MQTT_CONN_REFUSED_BAD_USER_NAME_OR_PASSWORD,
75  MQTT_CONN_REFUSED_NOT_AUTHORIZED,
76 };
77 
78 /****************************************************************************
79  * Public Types
80  ****************************************************************************/
84 struct _mqtt_msg_s {
85  int msg_id;
86  char *topic;
87  void *payload;
89  int qos;
90  int retain;
91 };
92 
93 typedef struct _mqtt_msg_s mqtt_msg_t;
94 
99  const unsigned char *ca_cert; /* CA certificate, common between client and MQTT Broker */
100  const unsigned char *cert; /* Client certificate */
101  const unsigned char *key; /* Client private key */
102  int ca_cert_len; /* the length of CA certificate */
103  int cert_len; /* the length of Client certificate */
104  int key_len; /* the length of key */
105 };
106 
107 typedef struct _mqtt_tls_param_s mqtt_tls_param_t;
108 
113  char *client_id;
114  char *user_name;
115  char *password;
118  bool debug;
119  mqtt_tls_param_t *tls;
121  void (*on_connect)(void *client, int result);
123  void (*on_disconnect)(void *client, int result);
125  void (*on_publish)(void *client, int msg_id);
127  void (*on_message)(void *client, mqtt_msg_t *msg);
129  void (*on_subscribe)(void *client, int msg_id, int qos_count, const int *granted_qos);
131  void (*on_unsubscribe)(void *client, int msg_id);
134  void *user_data;
135 };
136 
138 
144  void *mosq;
146  int state;
147 };
148 
149 typedef struct _mqtt_client_s mqtt_client_t;
150 
151 /****************************************************************************
152  * Public Function Prototypes
153  ****************************************************************************/
163 
172 int mqtt_deinit_client(mqtt_client_t *handle);
173 
185 int mqtt_connect(mqtt_client_t *handle, char *addr, int port, int keep_alive);
186 
195 int mqtt_disconnect(mqtt_client_t *handle);
196 
210 int mqtt_publish(mqtt_client_t *handle, char *topic, char *data, uint32_t data_len, uint8_t qos, uint8_t retain);
211 
222 int mqtt_subscribe(mqtt_client_t *handle, char *topic, uint8_t qos);
223 
233 int mqtt_unsubscribe(mqtt_client_t *handle, char *topic);
234 
235 #undef EXTERN
236 #ifdef __cplusplus
237 }
238 #endif
239 
240 #endif /* __MQTT_API_H__ */
241 // end of MQTT group
int mqtt_disconnect(mqtt_client_t *handle)
mqtt_disconnect() disconnects from a MQTT broker
Structure of MQTT client configuration.
Definition: mqtt_api.h:112
int mqtt_deinit_client(mqtt_client_t *handle)
mqtt_deinit_client() de-initializes MQTT client
void * mosq
Definition: mqtt_api.h:144
int mqtt_connect(mqtt_client_t *handle, char *addr, int port, int keep_alive)
mqtt_connect() connects to a MQTT broker
mqtt_client_t * mqtt_init_client(mqtt_client_config_t *config)
mqtt_init_client() initializes MQTT client
mqtt_client_config_t * config
Definition: mqtt_api.h:145
mqtt_connection_result_e
This enumeration describes the state of the MQTT connection result.
Definition: mqtt_api.h:69
void * payload
Definition: mqtt_api.h:87
int payload_len
Definition: mqtt_api.h:88
Structure of MQTT message.
Definition: mqtt_api.h:84
char * topic
Definition: mqtt_api.h:86
Structure of MQTT client object.
Definition: mqtt_api.h:142
mqtt_client_state_e
This enumeration describes the state of the MQTT client.
Definition: mqtt_api.h:59
int msg_id
Definition: mqtt_api.h:85
int mqtt_publish(mqtt_client_t *handle, char *topic, char *data, uint32_t data_len, uint8_t qos, uint8_t retain)
mqtt_publish() pusblishes message to a MQTT broker on the given topic
int mqtt_subscribe(mqtt_client_t *handle, char *topic, uint8_t qos)
mqtt_subscribe() subscribes for the specified topic with MQTT broker
int mqtt_unsubscribe(mqtt_client_t *handle, char *topic)
mqtt_unsubscribe() unsubscribes from the specified topic
Structure of MQTT security information.
Definition: mqtt_api.h:98
int retain
Definition: mqtt_api.h:90
mqtt_tls_param_t * tls
Definition: mqtt_api.h:119