Skip to content

Commit fd56796

Browse files
committed
调整客户端
1 parent 8db932c commit fd56796

File tree

3 files changed

+81
-115
lines changed

3 files changed

+81
-115
lines changed
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#ifndef iotsharp_CLIENT_H
2-
#define iotsharp_CLIENT_H
1+
#ifndef IOTSHARP_CLIENT_H
2+
#define IOTSHARP_CLIENT_H
33
/*********************************************************************************
4-
* 文件名称: iotsharp_client.h
5-
* 作 者: 麦壳饼
6-
* 版 本:
4+
* 文件名称: iotsharp_client.h
5+
* 作 者: 麦壳饼
6+
* 版 本:
77
**********************************************************************************/
88

99
#include <stdio.h>
@@ -15,11 +15,20 @@
1515
#define SDK_PLATFORM "unknown"
1616
#endif
1717

18-
#define NO_LOG_LEVL 0 //不输出日志
19-
#define ERROR_LOG_LEVL 1 //输出错误日志
20-
#define INFO_LOG_LEVL 2 //输出运行时日志和错误日志
21-
#define DEBUG_LOG_LEVL 3 //输出调试日志、运行时日志和错误日志
22-
18+
#define NO_LOG_LEVL 0 //不输出日志
19+
#define ERROR_LOG_LEVL 1 //输出错误日志
20+
#define INFO_LOG_LEVL 2 //输出运行时日志和错误日志
21+
#define DEBUG_LOG_LEVL 3 //输出调试日志、运行时日志和错误日志
22+
#define DATATYPE_LOG_LEVL 3 //输出调试日志、运行时日志和错误日志
23+
#define DEBUG_LOG_LEVL 3 //输出调试日志、运行时日志和错误日志
24+
25+
int iotsharp_start(void);
26+
int iotsharp_stop(void);
27+
int iotsharp_upload_telemetry_for_gateway(char* _devname,char* playload);//本身是网关为子设备上传遥测数据。
28+
int iotsharp_upload_telemetry_to_device(char* playload);//上传遥测数据
29+
int iotsharp_upload_attribute_for_gateway(char* _devname,char* playload);//本身是网关为子设备上传属性数据
30+
int iotsharp_upload_attribute_to_device(char* playload);//上传属性
31+
2332
#endif
2433

2534

iotsharp-c-sdk/src/iotsharp_client.c

Lines changed: 62 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,17 @@
1212

1313
#include "paho_mqtt.h"
1414

15-
/**
16-
* MQTT URI farmat:
17-
* domain mode
18-
* tcp://iot.eclipse.org:1883
19-
*
20-
* ipv4 mode
21-
* tcp://192.168.10.1:1883
22-
* ssl://192.168.10.1:1884
23-
*
24-
* ipv6 mode
25-
* tcp://[fe80::20c:29ff:fe9a:a07e]:1883
26-
* ssl://[fe80::20c:29ff:fe9a:a07e]:1884
27-
*/
28-
#define MQTT_URI "tcp://iot.eclipse.org:1883"
29-
#define MQTT_USERNAME "admin"
30-
#define MQTT_PASSWORD "admin"
31-
#define MQTT_SUBTOPIC "/mqtt/test"
32-
#define MQTT_PUBTOPIC "/mqtt/test"
15+
#define MQTT_URI PKG_USING_IOTSHARP_DEVICE_SERVER
16+
#define MQTT_USERNAME PKG_USING_IOTSHARP_DEVICE_NAME
17+
#define MQTT_PASSWORD PKG_USING_IOTSHARP_DEVICE_SECRET
18+
3319
#define MQTT_WILLMSG "Goodbye!"
3420

3521
/* define MQTT client context */
3622
static MQTTClient client;
3723
static int is_started = 0;
3824

39-
static void mqtt_sub_callback(MQTTClient *c, MessageData *msg_data)
25+
void mqtt_sub_callback(MQTTClient *c, MessageData *msg_data)
4026
{
4127
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
4228
LOG_D("mqtt sub callback: %.*s %.*s",
@@ -46,7 +32,8 @@ static void mqtt_sub_callback(MQTTClient *c, MessageData *msg_data)
4632
(char *)msg_data->message->payload);
4733
}
4834

49-
static void mqtt_sub_default_callback(MQTTClient *c, MessageData *msg_data)
35+
36+
void mqtt_sub_default_callback(MQTTClient *c, MessageData *msg_data)
5037
{
5138
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
5239
LOG_D("mqtt sub default callback: %.*s %.*s",
@@ -59,29 +46,47 @@ static void mqtt_sub_default_callback(MQTTClient *c, MessageData *msg_data)
5946
static void mqtt_connect_callback(MQTTClient *c)
6047
{
6148
LOG_D("inter mqtt_connect_callback!");
49+
50+
}
51+
static void mqtt_new_sub_callback(MQTTClient *client, MessageData *msg_data)
52+
{
53+
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
54+
LOG_D("mqtt new subscribe callback: %.*s %.*s",
55+
msg_data->topicName->lenstring.len,
56+
msg_data->topicName->lenstring.data,
57+
msg_data->message->payloadlen,
58+
(char *)msg_data->message->payload);
6259
}
6360

6461
static void mqtt_online_callback(MQTTClient *c)
6562
{
6663
LOG_D("inter mqtt_online_callback!");
64+
char _rpc_topic[200] = { 0 };
65+
char _attup_topic[200] = { 0 };
66+
sprintf(_rpc_topic, "devices/"PKG_USING_IOTSHARP_DEVICE_NAME"/rpc/request/+/+");
67+
sprintf(_attup_topic, "devices/"PKG_USING_IOTSHARP_DEVICE_NAME"/attributes/update/");
68+
paho_mqtt_subscribe(&client, QOS1, _rpc_topic, mqtt_new_sub_callback);
69+
paho_mqtt_subscribe(&client, QOS1, _rpc_topic, mqtt_new_sub_callback);
70+
6771
}
6872

6973
static void mqtt_offline_callback(MQTTClient *c)
7074
{
7175
LOG_D("inter mqtt_offline_callback!");
76+
char _rpc_topic[200] = { 0 };
77+
char _attup_topic[200] = { 0 };
78+
sprintf(_rpc_topic, "devices/"PKG_USING_IOTSHARP_DEVICE_NAME"/rpc/request/+/+");
79+
sprintf(_attup_topic, "devices/"PKG_USING_IOTSHARP_DEVICE_NAME"/attributes/update/");
80+
paho_mqtt_unsubscribe(&client, _rpc_topic);
81+
paho_mqtt_unsubscribe(&client, _rpc_topic);
7282
}
7383

74-
static int mqtt_start(int argc, char **argv)
84+
int iotsharp_start(void)
7585
{
7686
/* init condata param by using MQTTPacket_connectData_initializer */
7787
MQTTPacket_connectData condata = MQTTPacket_connectData_initializer;
7888
static char cid[20] = { 0 };
7989

80-
if (argc != 1)
81-
{
82-
rt_kprintf("mqtt_start --start a mqtt worker thread.\n");
83-
return -1;
84-
}
8590

8691
if (is_started)
8792
{
@@ -94,7 +99,7 @@ static int mqtt_start(int argc, char **argv)
9499
client.uri = MQTT_URI;
95100

96101
/* generate the random client ID */
97-
rt_snprintf(cid, sizeof(cid), "rtthread%d", rt_tick_get());
102+
rt_snprintf(cid, sizeof(cid), "iotsharp%d", rt_tick_get());
98103
/* config connect param */
99104
memcpy(&client.condata, &condata, sizeof(condata));
100105
client.condata.clientID.cstring = cid;
@@ -107,7 +112,7 @@ static int mqtt_start(int argc, char **argv)
107112
client.condata.willFlag = 1;
108113
client.condata.will.qos = 1;
109114
client.condata.will.retained = 0;
110-
client.condata.will.topicName.cstring = MQTT_PUBTOPIC;
115+
client.condata.will.topicName.cstring = "/device/me/disconnect";
111116
client.condata.will.message.cstring = MQTT_WILLMSG;
112117

113118
/* malloc buffer. */
@@ -126,9 +131,9 @@ static int mqtt_start(int argc, char **argv)
126131
client.offline_callback = mqtt_offline_callback;
127132

128133
/* set subscribe table and event callback */
129-
client.messageHandlers[0].topicFilter = rt_strdup(MQTT_SUBTOPIC);
130-
client.messageHandlers[0].callback = mqtt_sub_callback;
131-
client.messageHandlers[0].qos = QOS1;
134+
// client.messageHandlers[0].topicFilter = rt_strdup(MQTT_SUBTOPIC);
135+
//client.messageHandlers[0].callback = mqtt_sub_callback;
136+
// client.messageHandlers[0].qos = QOS1;
132137

133138
/* set default subscribe event callback */
134139
client.defaultMessageHandler = mqtt_sub_default_callback;
@@ -141,92 +146,62 @@ static int mqtt_start(int argc, char **argv)
141146
return 0;
142147
}
143148

144-
static int mqtt_stop(int argc, char **argv)
149+
int iotsahrp_stop(void)
145150
{
146-
if (argc != 1)
147-
{
148-
rt_kprintf("mqtt_stop --stop mqtt worker thread and free mqtt client object.\n");
149-
}
150-
151151
is_started = 0;
152152

153153
return paho_mqtt_stop(&client);
154154
}
155155

156-
static int mqtt_publish(int argc, char **argv)
156+
157+
static int mqtt_publish_for_gateway(char* subdevicename, int datatype, char *playload)
157158
{
159+
char _telemetry_topic[200] = { 0 };
160+
char attributes_topic[200] = { 0 };
161+
sprintf(_telemetry_topic, "devices/%s/telemetry",subdevicename);
162+
sprintf(attributes_topic, "devices/%s/attributes",subdevicename);
158163
if (is_started == 0)
159164
{
160165
LOG_E("mqtt client is not connected.");
161166
return -1;
162167
}
163168

164-
if (argc == 2)
169+
if (datatype == 1)
165170
{
166-
paho_mqtt_publish(&client, QOS1, MQTT_PUBTOPIC, argv[1]);
171+
paho_mqtt_publish(&client, QOS1, _telemetry_topic, playload);
167172
}
168-
else if (argc == 3)
173+
else if (datatype== 2)
169174
{
170-
paho_mqtt_publish(&client, QOS1, argv[1], argv[2]);
175+
paho_mqtt_publish(&client, QOS1, attributes_topic, playload);
171176
}
172177
else
173178
{
174-
rt_kprintf("mqtt_publish <topic> [message] --mqtt publish message to specified topic.\n");
179+
rt_kprintf("publish message to specified datatype.\n");
175180
return -1;
176181
}
177-
178182
return 0;
179183
}
180-
181-
static void mqtt_new_sub_callback(MQTTClient *client, MessageData *msg_data)
184+
int iotsharp_upload_telemetry_for_gateway(char* _devname,char* playload)
185+
{
186+
return mqtt_publish_for_gateway(_devname,1,playload);
187+
}
188+
int iotsharp_upload_telemetry_to_device(char* playload)
189+
{
190+
return mqtt_publish_for_gateway("me",1,playload);
191+
}
192+
int iotsharp_upload_attribute_for_gateway(char* _devname,char* playload)
182193
{
183-
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
184-
LOG_D("mqtt new subscribe callback: %.*s %.*s",
185-
msg_data->topicName->lenstring.len,
186-
msg_data->topicName->lenstring.data,
187-
msg_data->message->payloadlen,
188-
(char *)msg_data->message->payload);
194+
return mqtt_publish_for_gateway(_devname,2,playload);
189195
}
190-
191-
static int mqtt_subscribe(int argc, char **argv)
196+
int iotsharp_upload_attribute_to_device(char* playload)
192197
{
193-
if (argc != 2)
194-
{
195-
rt_kprintf("mqtt_subscribe [topic] --send an mqtt subscribe packet and wait for suback before returning.\n");
196-
return -1;
197-
}
198-
199-
if (is_started == 0)
200-
{
201-
LOG_E("mqtt client is not connected.");
202-
return -1;
203-
}
204-
205-
return paho_mqtt_subscribe(&client, QOS1, argv[1], mqtt_new_sub_callback);
198+
return mqtt_publish_for_gateway("me",2,playload);
206199
}
207200

208-
static int mqtt_unsubscribe(int argc, char **argv)
209-
{
210-
if (argc != 2)
211-
{
212-
rt_kprintf("mqtt_unsubscribe [topic] --send an mqtt unsubscribe packet and wait for suback before returning.\n");
213-
return -1;
214-
}
215-
216-
if (is_started == 0)
217-
{
218-
LOG_E("mqtt client is not connected.");
219-
return -1;
220-
}
221201

222-
return paho_mqtt_unsubscribe(&client, argv[1]);
223-
}
224202

225203
#ifdef FINSH_USING_MSH
226-
MSH_CMD_EXPORT(mqtt_start, startup mqtt client);
227-
MSH_CMD_EXPORT(mqtt_stop, stop mqtt client);
228-
MSH_CMD_EXPORT(mqtt_publish, mqtt publish message to specified topic);
229-
MSH_CMD_EXPORT(mqtt_subscribe, mqtt subscribe topic);
230-
MSH_CMD_EXPORT(mqtt_unsubscribe, mqtt unsubscribe topic);
204+
205+
231206
#endif /* FINSH_USING_MSH */
232207

samples/demo.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
#include <stdio.h>
22
#include <time.h>
33
#include <string.h>
4-
#ifdef PKG_USING_IOTSHARP_SERVER
5-
#define DEVICE_SERVER PKG_USING_IOTSHARP_DEVICE_SERVER
6-
#else
7-
#define DEVICE_SERVER ""
8-
#endif
94

10-
11-
#ifdef PKG_USING_IOTSHARP_DEVICE_NAME
12-
#define DEVICE_NAME PKG_USING_IOTSHARP_DEVICE_NAME
13-
#else
14-
#define DEVICE_NAME ""
15-
#endif
16-
17-
#ifdef PKG_USING_IOTSHARP_DEVICE_TOKEN
18-
#define DEVICE_TOKEN PKG_USING_IOTSHARP_DEVICE_TOKEN
19-
#else
20-
#define DEVICE_TOKEN ""
21-
#endif
225

236

247

258
#ifdef PKG_USING_IOTSHARP-C-SDK
269
#include <finsh.h>
2710

28-
MSH_CMD_EXPORT_ALIAS(start_demo_test, start_demo_test,Example: start_demo_test);
2911
#endif
3012

0 commit comments

Comments
 (0)