Internet Middleware API Reference  Rev 1.0
MQTTMediatek.h
1 /* Copyright Statement:
2  *
3  * (C) 2005-2016 MediaTek Inc. All rights reserved.
4  *
5  * This software/firmware and related documentation ("MediaTek Software") are
6  * protected under relevant copyright laws. The information contained herein
7  * is confidential and proprietary to MediaTek Inc. ("MediaTek") and/or its licensors.
8  * Without the prior written permission of MediaTek and/or its licensors,
9  * any reproduction, modification, use or disclosure of MediaTek Software,
10  * and information contained herein, in whole or in part, shall be strictly prohibited.
11  * You may only use, reproduce, modify, or distribute (as applicable) MediaTek Software
12  * if you have agreed to and been bound by the applicable license agreement with
13  * MediaTek ("License Agreement") and been granted explicit permission to do so within
14  * the License Agreement ("Permitted User"). If you are not a Permitted User,
15  * please cease any access or use of MediaTek Software immediately.
16  * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
17  * THAT MEDIATEK SOFTWARE RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES
18  * ARE PROVIDED TO RECEIVER ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
19  * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
21  * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
22  * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
23  * SUPPLIED WITH MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
24  * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
25  * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
26  * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
27  * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
28  * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
29  * CUMULATIVE LIABILITY WITH RESPECT TO MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
30  * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE MEDIATEK SOFTWARE AT ISSUE,
31  * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
32  * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
33  */
34 
35 #ifndef __MQTT_MEDIATEK__
36 #define __MQTT_MEDIATEK__
37 
38 #include "mbedtls/compat-1.3.h"
39 #include "mbedtls/net.h"
40 #include "mbedtls/ssl.h"
41 #include "mbedtls/certs.h"
42 
43 #include "syslog.h"
44 
45 typedef struct Network Network;
46 
73 struct Network {
74  int my_socket;
75  int (*mqttread)(Network *, unsigned char *, int, int);
76  int (*mqttwrite)(Network *, unsigned char *, int, int);
77  void (*disconnect)(Network *);
78  mbedtls_ssl_context ssl;
79  mbedtls_net_context fd;
80  mbedtls_ssl_config conf;
81  mbedtls_x509_crt cacertl;
82  mbedtls_x509_crt clicert;
83  mbedtls_pk_context pkey;
84 };
85 
96 void NewNetwork(Network *n);
97 
98 
107 int ConnectNetwork(Network *n, char *addr, char *port);
108 
109 
126 int TLSConnectNetwork(Network *n, const char *addr, const char *port,
127  const char *ca_crt, size_t ca_crt_len,
128  const char *client_crt, size_t client_crt_len,
129  const char *client_key, size_t client_key_len,
130  const char *client_pwd, size_t client_pwd_len);
131 
132 
138 /* The following content is used inside the MQTT module. */
139 typedef struct Timer Timer;
140 struct Timer {
141  unsigned int systick_period;
142  unsigned int end_time;
143 };
144 
145 char expired(Timer *timer);
146 void countdown_ms(Timer *timer, unsigned int timeout);
147 void countdown(Timer *timer, unsigned int timeout);
148 unsigned int left_ms(Timer *timer);
149 void InitTimer(Timer *timer);
150 
152 #ifndef MTK_DEBUG_LEVEL_NONE
153 #define MTK_MQTT_DEBUG 1
154 #else
155 #define MTK_MQTT_DEBUG 0
156 #endif
157 
158 #if MTK_MQTT_DEBUG
159 #define MQTT_DBG(fmt,arg...) LOG_I(MQTT_CLIENT, "[MQTT_CLIENT]: "fmt,##arg)
160 #define MQTT_WARN(fmt,arg...) LOG_W(MQTT_CLIENT, "[MQTT_CLIENT]: "fmt,##arg)
161 #define MQTT_ERR(fmt,arg...) LOG_E(MQTT_CLIENT ,"[MQTT_CLIENT]: "fmt,##arg)
162 #else
163 #define MQTT_DBG(x, ...)
164 #define MQTT_WARN(x, ...)
165 #define MQTT_ERR(x, ...)
166 #endif
167 
168 #endif /* __MQTT_MEDIATEK__ */
int(* mqttwrite)(Network *, unsigned char *, int, int)
Send data to server function pointer.
Definition: MQTTMediatek.h:76
void NewNetwork(Network *n)
This function initializes the network structure.
int(* mqttread)(Network *, unsigned char *, int, int)
Read data from server function pointer.
Definition: MQTTMediatek.h:75
mbedtls_x509_crt clicert
mbed TLS Client certification.
Definition: MQTTMediatek.h:82
void(* disconnect)(Network *)
Disconnect the network function pointer.
Definition: MQTTMediatek.h:77
int TLSConnectNetwork(Network *n, const char *addr, const char *port, const char *ca_crt, size_t ca_crt_len, const char *client_crt, size_t client_crt_len, const char *client_key, size_t client_key_len, const char *client_pwd, size_t client_pwd_len)
This function connects to a MQTT server with TLS, and returns a value that indicates whether the conn...
Definition: MQTTMediatek.h:140
mbedtls_x509_crt cacertl
mbed TLS CA certification.
Definition: MQTTMediatek.h:81
int my_socket
Connect the socket handle.
Definition: MQTTMediatek.h:74
mbedtls_ssl_config conf
mbed TLS configuration context.
Definition: MQTTMediatek.h:80
int ConnectNetwork(Network *n, char *addr, char *port)
This function connects to the MQTT server without TLS, and returns a value that indicates whether the...
mbedtls_net_context fd
mbed TLS network context.
Definition: MQTTMediatek.h:79
mbedtls_ssl_context ssl
mbed TLS control context.
Definition: MQTTMediatek.h:78
mbedtls_pk_context pkey
mbed TLS Client key.
Definition: MQTTMediatek.h:83
The structure of MQTT network connection used in the MQTT library.
Definition: MQTTMediatek.h:73