Skip to content

Commit 07bb42a

Browse files
RanMaoyiJaylinYu
authored andcommitted
feat(C): Add paho TLS example
Signed-off-by: Moi Ran <[email protected]>
1 parent c102afd commit 07bb42a

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

mqtt-client-C-paho/main_tls.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "stdlib.h"
2+
#include "string.h"
3+
#include "unistd.h"
4+
#include "MQTTClient.h"
5+
6+
#define ADDRESS "ssl://127.0.0.1:8883"
7+
#define USERNAME "emqx"
8+
#define PASSWORD "public"
9+
#define CLIENTID "c-client"
10+
#define QOS 0
11+
#define TOPIC "emqx/c-test"
12+
#define TIMEOUT 10000L
13+
#define CLIENTCERT "/etc/emqx/certs/client-cert.pem"
14+
#define CACERT "/etc/emqx/certs/cacert.pem"
15+
#define PRIVATEKEY "/etc/emqx/certs/client-key.pem"
16+
17+
void publish(MQTTClient client, char *topic, char *payload) {
18+
MQTTClient_message message = MQTTClient_message_initializer;
19+
message.payload = payload;
20+
message.payloadlen = strlen(payload);
21+
message.qos = QOS;
22+
message.retained = 0;
23+
MQTTClient_deliveryToken token;
24+
MQTTClient_publishMessage(client, topic, &message, &token);
25+
MQTTClient_waitForCompletion(client, token, TIMEOUT);
26+
printf("Send `%s` to topic `%s` \n", payload, TOPIC);
27+
}
28+
29+
int on_message(void *context, char *topicName, int topicLen, MQTTClient_message *message) { char *payload = message->payload;
30+
printf("Received `%s` from `%s` topic \n", payload, topicName);
31+
MQTTClient_freeMessage(&message);
32+
MQTTClient_free(topicName);
33+
return 1;
34+
}
35+
36+
int main(int argc, char *argv[]) {
37+
int rc;
38+
MQTTClient client;
39+
40+
MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
41+
MQTTClient_create(&client, ADDRESS, CLIENTID, 0, NULL);
42+
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
43+
conn_opts.username = USERNAME;
44+
conn_opts.password = PASSWORD;
45+
46+
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
47+
/*
48+
* Set the value of 'verify' to 0, which means
49+
* that the domain should not be checked.
50+
* Otherwise, if the domain does not match,
51+
* an error will occur.
52+
*/
53+
ssl_opts.verify = 0;
54+
ssl_opts.trustStore = CACERT;
55+
/*
56+
* Two-Way SSL Authentiacation needed.
57+
* If you want to use One-Way SSL Authentication,
58+
* please comment out keyStore and privateKey.
59+
*/
60+
ssl_opts.privateKey = PRIVATEKEY;
61+
ssl_opts.keyStore = CLIENTCERT;
62+
/*
63+
* Notice: In higher versions of openssl
64+
* the SSL/TLS version is depended on openssl itself.
65+
* (Paho will use TLS_Client_method(), more details please see openssl docs)
66+
*/
67+
conn_opts.ssl = &ssl_opts;
68+
69+
MQTTClient_setCallbacks(client, NULL, NULL, on_message, NULL);
70+
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
71+
printf("Failed to connect, return code %d\n", rc);
72+
exit(-1);
73+
} else {
74+
printf("Connected to MQTT Broker!\n");
75+
}
76+
// subscribe topic
77+
MQTTClient_subscribe(client, TOPIC, QOS);
78+
char payload[16];
79+
for (int i = 0; i < 100; i += 1) {
80+
// publish message to broker
81+
snprintf(payload, 16, "message-%d", i);
82+
publish(client, TOPIC, payload);
83+
sleep(1);
84+
}
85+
MQTTClient_disconnect(client, TIMEOUT);
86+
MQTTClient_destroy(&client);
87+
return rc;
88+
}

0 commit comments

Comments
 (0)