1 /**
2  * @copyright Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <memory.h>
7 #include "MQTTClient.h"
8 
9 #include <stdio.h>
10 #include <signal.h>
11 #include <sys/time.h>
12 
13 #if AOS_COMP_CLI
14 #include "aos/cli.h"
15 #endif
16 
17 /* 三元组 */
18 #define EXAMPLE_PRODUCT_KEY			"a1nEew2TMm8"
19 #define EXAMPLE_DEVICE_NAME			"abcd"
20 #define EXAMPLE_DEVICE_SECRET       "5719c4da9103cea09daaa9d70a513c65"
21 
22 /* declare the external function aiotMqttSign() */
23 extern int aiotMqttSign(const char *productKey, const char *deviceName, const char *deviceSecret, char clientId[150], char username[65], char password[65]);
24 
messageArrived(MessageData * md)25 void messageArrived(MessageData* md)
26 {
27 	MQTTMessage* message = md->message;
28 
29 	printf("%.*s\t", md->topicName->lenstring.len, md->topicName->lenstring.data);
30 	printf("%.*s\n", (int)message->payloadlen, (char*)message->payload);
31 }
32 
33 // void aliyunIotPlatform(char clientId[150], char username[64], char password[65])
34 // {
35 // 	if ((rc = aiotMqttSign(EXAMPLE_PRODUCT_KEY, EXAMPLE_DEVICE_NAME, EXAMPLE_DEVICE_SECRET, clientId, username, password) < 0)) {
36 // 		printf("aiotMqttSign -%0x4x\n", -rc);
37 // 		return -1;
38 // 	}
39 // }
40 
mqtt_comp_example(int argc,char ** argv)41 int mqtt_comp_example(int argc, char** argv)
42 {
43 	int rc = 0;
44 
45 	/* setup the buffer, it must big enough */
46 	unsigned char buf[2000];
47 	unsigned char readbuf[2000];
48 
49 	Network n;
50 	MQTTClient c;
51 
52 	/* connect aliyun iot platform should deal with connect parameters */
53 #if CONFIG_AIOT_SIGN
54 	char *host = EXAMPLE_PRODUCT_KEY".iot-as-mqtt.cn-shanghai.aliyuncs.com";
55 	short port = 443;
56 	const char *subTopic = "/"EXAMPLE_PRODUCT_KEY"/"EXAMPLE_DEVICE_NAME"/user/get";
57 	const char *pubTopic = "/"EXAMPLE_PRODUCT_KEY"/"EXAMPLE_DEVICE_NAME"/user/update";
58 	char clientId[150] = {0};
59 	char username[65] = {0};
60 	char password[65] = {0};
61 
62 	if ((rc = aiotMqttSign(EXAMPLE_PRODUCT_KEY, EXAMPLE_DEVICE_NAME, EXAMPLE_DEVICE_SECRET, clientId, username, password) < 0)) {
63 		printf("aiotMqttSign -%0x4x\n", -rc);
64 		return -1;
65 	}
66 	printf("clientid: %s\n", clientId);
67 	printf("username: %s\n", username);
68 	printf("password: %s\n", password);
69 
70 #else
71 	char *host = "10.101.170.55";  //使用mosquitto搭建的linux服务器
72 	short port = 1883;
73 	const char *subTopic = "test/aiot/sub_topic";
74 	const char *pubTopic = "test/aiot/pub_topic";
75 	char clientId[150] = "haas";
76 	char username[65] = {0};
77 	char password[65] = {0};
78 #endif
79 	/* network init and establish network to aliyun IoT platform */
80 	NetworkInit(&n);
81 
82 	rc = NetworkConnect(&n, host, port);
83 	if(rc != 0){
84 		printf("Network Connect failed:%d\n", rc);
85 		return;
86 	}else{
87 		printf("Network Connect Success!\n");
88 	}
89 
90 	/* init mqtt client */
91 	MQTTClientInit(&c, &n, 1000, buf, sizeof(buf), readbuf, sizeof(readbuf));
92 
93 	/* set the default message handler */
94 	c.defaultMessageHandler = messageArrived;
95 
96 	/* set mqtt connect parameter */
97 	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
98 	data.willFlag = 0;
99 	data.MQTTVersion = 3;
100 	data.clientID.cstring = clientId;
101 	data.username.cstring = username;
102 	data.password.cstring = password;
103 	data.keepAliveInterval = 60;
104 	data.cleansession = 1;
105 	printf("Connecting to %s:%d\n", host, port);
106 
107 	rc = MQTTConnect(&c, &data);
108 	if(rc != 0){
109 		printf("MQTT Connect server failed:%d\n", rc);
110 		return;
111 	}else{
112 		printf("MQTT Connect Success!\n");
113 	}
114 
115 	rc = MQTTSubscribe(&c, subTopic, 1, messageArrived);
116 	if(rc != 0){
117 		printf("MQTT Subscribe failed:%d\n", rc);
118 		return;
119 	}else{
120 		printf("MQTT Subscribe Success! Topic:%s\n",subTopic);
121 	}
122 
123 	int cnt = 0;
124     unsigned int msgid = 0;
125 	while (1)
126 	{
127 		MQTTYield(&c, 5000);
128 
129 		if (++cnt % 5 == 0) {
130 			MQTTMessage msg = {
131 				QOS1,
132 				0,
133 				0,
134 				0,
135 				"Hello world",
136 				strlen("Hello world"),
137 			};
138             msg.id = ++msgid;
139 			rc = MQTTPublish(&c, pubTopic, &msg);
140 			printf("MQTTPublish %d, msgid %d\n", rc, msgid);
141 
142 			if(msgid==10){
143 				break;
144 			}
145 		}
146 	}
147 
148 	printf("MQTT example exit!\n");
149 
150 	MQTTDisconnect(&c);
151 	NetworkDisconnect(&n);
152 
153 	return 0;
154 }
155 
156 
157 #if AOS_COMP_CLI
158 /* reg args: fun, cmd, description*/
159 ALIOS_CLI_CMD_REGISTER(mqtt_comp_example, mqtt_example, mqtt component base example)
160 #endif