Skip to content

Commit 112f08c

Browse files
committed
第一次提交iotsharp的sdk
0 parents  commit 112f08c

File tree

8 files changed

+378
-0
lines changed

8 files changed

+378
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 IoTSharp
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# IoTSharp for RTthread Package
2+
3+
## 1 介绍
4+
5+
IoTSharp 是一个 基于.Net Core 开源的物联网基础平台, 支持 HTTP、MQTT 、CoAp 协议, 属性数据和遥测数据协议简单类型丰富,简单设置即可将数据存储在PostgreSql、MySql、Oracle、SQLServer、Sqlite,是一个用于数据收集、处理、可视化与设备管理的 IoT 平台.
6+
7+
### 1.1 目录结构
8+
9+
| 名称 | 说明 |
10+
| ---- | ---- |
11+
| jiot-c-sdk | sdk源码目录 |
12+
| docs | 文档目录 |
13+
| samples | 示例文件目录 |
14+
| LICENSE | 许可证文件 |
15+
| README.md | 软件包使用说明 |
16+
| SConscript | RT-Thread 默认的构建脚本 |
17+
18+
### 1.2 SDK接口说明
19+
20+
[IoTSharp C SDK 接口文档](https://docs.iotsharp.net/client/c_sdk_api/)
21+
22+
### 1.3 许可证
23+
24+
MIT License 协议许可。
25+
26+
## 2 获取软件包
27+
28+
使用 `iotsharp-c-sdk` 软件包使用 menuconfig 命令打开 Env 配置界面,在 `RT-Thread online packages → IoT - internet of things → IoT Cloud` 中选择 iotsharp-c-sdk 软件包,操作界面如下图所示:
29+
30+
![选中 iotsharp-c-sdk 软件包](https://github.com/jpush/JIoT-rtthread-package/blob/master/docs/figures/select_iotsharp_c_sdk.png?raw=true)
31+
32+
选择合适的配置项后,使用 `pkgs --update` 命令下载软件包并添加到工程中即可。
33+

SConscript

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Import('RTT_ROOT')
2+
from building import *
3+
4+
# get current directory
5+
cwd = GetCurrentDir()
6+
7+
# The set of source files associated with this SConscript file.
8+
src = []
9+
path = []
10+
11+
src += Glob('iotsharp-c-sdk/src/jclient/*.c')
12+
src += Glob('iotsharp-c-sdk/src/sisclient/*.c')
13+
src += Glob('iotsharp-c-sdk/src/mqtt/MQTTClient-C/src/*.c')
14+
src += Glob('iotsharp-c-sdk/src/mqtt/MQTTPacket/src/*.c')
15+
src += Glob('iotsharp-c-sdk/common/*.c')
16+
src += Glob('iotsharp-c-sdk/public/net/tcp/*.c')
17+
src += Glob('iotsharp-c-sdk/platform/os/rt-thread/*.c')
18+
19+
path += [cwd + '/iotsharp-c-sdk/common']
20+
path += [cwd + '/iotsharp-c-sdk/include/jclient']
21+
path += [cwd + '/iotsharp-c-sdk/include/mqtt']
22+
path += [cwd + '/iotsharp-c-sdk/include/sisclient']
23+
path += [cwd + '/iotsharp-c-sdk/platform/os/rt-thread']
24+
path += [cwd + '/iotsharp-c-sdk/src/mqtt/MQTTPacket/src']
25+
path += [cwd + '/iotsharp-c-sdk/public/net/tcp']
26+
27+
if GetDepend(['PKG_USING_IOTSHARP_EXAMPLES']):
28+
src += Glob('samples/*.c')
29+
30+
31+
32+
group = DefineGroup('iotsharp-c-sdk', src, depend = [''], CPPPATH = path)
33+
34+
Return('group')

docs/c_sdk_api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C SDK 接口说明文档
2+
3+
## 设备信息结构体
26 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef iotsharp_CLIENT_H
2+
#define iotsharp_CLIENT_H
3+
/*********************************************************************************
4+
* 文件名称: iotsharp_client.h
5+
* 作 者: 麦壳饼
6+
* 版 本:
7+
**********************************************************************************/
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
#define SDK_VERSION "v1.0.0"
13+
14+
#ifndef SDK_PLATFORM
15+
#define SDK_PLATFORM "unknown"
16+
#endif
17+
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+
23+
#endif
24+
25+
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdint.h>
4+
5+
#include <rtthread.h>
6+
7+
#define DBG_ENABLE
8+
#define DBG_SECTION_NAME "iotsharp"
9+
#define DBG_LEVEL DBG_LOG
10+
#define DBG_COLOR
11+
#include <rtdbg.h>
12+
13+
#include "paho_mqtt.h"
14+
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"
33+
#define MQTT_WILLMSG "Goodbye!"
34+
35+
/* define MQTT client context */
36+
static MQTTClient client;
37+
static int is_started = 0;
38+
39+
static void mqtt_sub_callback(MQTTClient *c, MessageData *msg_data)
40+
{
41+
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
42+
LOG_D("mqtt sub callback: %.*s %.*s",
43+
msg_data->topicName->lenstring.len,
44+
msg_data->topicName->lenstring.data,
45+
msg_data->message->payloadlen,
46+
(char *)msg_data->message->payload);
47+
}
48+
49+
static void mqtt_sub_default_callback(MQTTClient *c, MessageData *msg_data)
50+
{
51+
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
52+
LOG_D("mqtt sub default callback: %.*s %.*s",
53+
msg_data->topicName->lenstring.len,
54+
msg_data->topicName->lenstring.data,
55+
msg_data->message->payloadlen,
56+
(char *)msg_data->message->payload);
57+
}
58+
59+
static void mqtt_connect_callback(MQTTClient *c)
60+
{
61+
LOG_D("inter mqtt_connect_callback!");
62+
}
63+
64+
static void mqtt_online_callback(MQTTClient *c)
65+
{
66+
LOG_D("inter mqtt_online_callback!");
67+
}
68+
69+
static void mqtt_offline_callback(MQTTClient *c)
70+
{
71+
LOG_D("inter mqtt_offline_callback!");
72+
}
73+
74+
static int mqtt_start(int argc, char **argv)
75+
{
76+
/* init condata param by using MQTTPacket_connectData_initializer */
77+
MQTTPacket_connectData condata = MQTTPacket_connectData_initializer;
78+
static char cid[20] = { 0 };
79+
80+
if (argc != 1)
81+
{
82+
rt_kprintf("mqtt_start --start a mqtt worker thread.\n");
83+
return -1;
84+
}
85+
86+
if (is_started)
87+
{
88+
LOG_E("mqtt client is already connected.");
89+
return -1;
90+
}
91+
/* config MQTT context param */
92+
{
93+
client.isconnected = 0;
94+
client.uri = MQTT_URI;
95+
96+
/* generate the random client ID */
97+
rt_snprintf(cid, sizeof(cid), "rtthread%d", rt_tick_get());
98+
/* config connect param */
99+
memcpy(&client.condata, &condata, sizeof(condata));
100+
client.condata.clientID.cstring = cid;
101+
client.condata.keepAliveInterval = 30;
102+
client.condata.cleansession = 1;
103+
client.condata.username.cstring = MQTT_USERNAME;
104+
client.condata.password.cstring = MQTT_PASSWORD;
105+
106+
/* config MQTT will param. */
107+
client.condata.willFlag = 1;
108+
client.condata.will.qos = 1;
109+
client.condata.will.retained = 0;
110+
client.condata.will.topicName.cstring = MQTT_PUBTOPIC;
111+
client.condata.will.message.cstring = MQTT_WILLMSG;
112+
113+
/* malloc buffer. */
114+
client.buf_size = client.readbuf_size = 1024;
115+
client.buf = rt_calloc(1, client.buf_size);
116+
client.readbuf = rt_calloc(1, client.readbuf_size);
117+
if (!(client.buf && client.readbuf))
118+
{
119+
LOG_E("no memory for MQTT client buffer!");
120+
return -1;
121+
}
122+
123+
/* set event callback function */
124+
client.connect_callback = mqtt_connect_callback;
125+
client.online_callback = mqtt_online_callback;
126+
client.offline_callback = mqtt_offline_callback;
127+
128+
/* 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;
132+
133+
/* set default subscribe event callback */
134+
client.defaultMessageHandler = mqtt_sub_default_callback;
135+
}
136+
137+
/* run mqtt client */
138+
paho_mqtt_start(&client);
139+
is_started = 1;
140+
141+
return 0;
142+
}
143+
144+
static int mqtt_stop(int argc, char **argv)
145+
{
146+
if (argc != 1)
147+
{
148+
rt_kprintf("mqtt_stop --stop mqtt worker thread and free mqtt client object.\n");
149+
}
150+
151+
is_started = 0;
152+
153+
return paho_mqtt_stop(&client);
154+
}
155+
156+
static int mqtt_publish(int argc, char **argv)
157+
{
158+
if (is_started == 0)
159+
{
160+
LOG_E("mqtt client is not connected.");
161+
return -1;
162+
}
163+
164+
if (argc == 2)
165+
{
166+
paho_mqtt_publish(&client, QOS1, MQTT_PUBTOPIC, argv[1]);
167+
}
168+
else if (argc == 3)
169+
{
170+
paho_mqtt_publish(&client, QOS1, argv[1], argv[2]);
171+
}
172+
else
173+
{
174+
rt_kprintf("mqtt_publish <topic> [message] --mqtt publish message to specified topic.\n");
175+
return -1;
176+
}
177+
178+
return 0;
179+
}
180+
181+
static void mqtt_new_sub_callback(MQTTClient *client, MessageData *msg_data)
182+
{
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);
189+
}
190+
191+
static int mqtt_subscribe(int argc, char **argv)
192+
{
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);
206+
}
207+
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+
}
221+
222+
return paho_mqtt_unsubscribe(&client, argv[1]);
223+
}
224+
225+
#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);
231+
#endif /* FINSH_USING_MSH */
232+

samples/demo.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
#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
9+
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
22+
23+
24+
25+
#ifdef PKG_USING_IOTSHARP-C-SDK
26+
#include <finsh.h>
27+
28+
MSH_CMD_EXPORT_ALIAS(start_demo_test, start_demo_test,Example: start_demo_test);
29+
#endif
30+

0 commit comments

Comments
 (0)