[go: up one dir, main page]

Skip to content

Commit

Permalink
第一次提交iotsharp的sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
maikebing committed May 8, 2022
0 parents commit 112f08c
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 IoTSharp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# IoTSharp for RTthread Package

## 1 介绍

IoTSharp 是一个 基于.Net Core 开源的物联网基础平台, 支持 HTTP、MQTT 、CoAp 协议, 属性数据和遥测数据协议简单类型丰富,简单设置即可将数据存储在PostgreSql、MySql、Oracle、SQLServer、Sqlite,是一个用于数据收集、处理、可视化与设备管理的 IoT 平台.

### 1.1 目录结构

| 名称 | 说明 |
| ---- | ---- |
| jiot-c-sdk | sdk源码目录 |
| docs | 文档目录 |
| samples | 示例文件目录 |
| LICENSE | 许可证文件 |
| README.md | 软件包使用说明 |
| SConscript | RT-Thread 默认的构建脚本 |

### 1.2 SDK接口说明

[IoTSharp C SDK 接口文档](https://docs.iotsharp.net/client/c_sdk_api/)

### 1.3 许可证

MIT License 协议许可。

## 2 获取软件包

使用 `iotsharp-c-sdk` 软件包使用 menuconfig 命令打开 Env 配置界面,在 `RT-Thread online packages → IoT - internet of things → IoT Cloud` 中选择 iotsharp-c-sdk 软件包,操作界面如下图所示:

![选中 iotsharp-c-sdk 软件包](https://github.com/jpush/JIoT-rtthread-package/blob/master/docs/figures/select_iotsharp_c_sdk.png?raw=true)

选择合适的配置项后,使用 `pkgs --update` 命令下载软件包并添加到工程中即可。

34 changes: 34 additions & 0 deletions SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Import('RTT_ROOT')
from building import *

# get current directory
cwd = GetCurrentDir()

# The set of source files associated with this SConscript file.
src = []
path = []

src += Glob('iotsharp-c-sdk/src/jclient/*.c')
src += Glob('iotsharp-c-sdk/src/sisclient/*.c')
src += Glob('iotsharp-c-sdk/src/mqtt/MQTTClient-C/src/*.c')
src += Glob('iotsharp-c-sdk/src/mqtt/MQTTPacket/src/*.c')
src += Glob('iotsharp-c-sdk/common/*.c')
src += Glob('iotsharp-c-sdk/public/net/tcp/*.c')
src += Glob('iotsharp-c-sdk/platform/os/rt-thread/*.c')

path += [cwd + '/iotsharp-c-sdk/common']
path += [cwd + '/iotsharp-c-sdk/include/jclient']
path += [cwd + '/iotsharp-c-sdk/include/mqtt']
path += [cwd + '/iotsharp-c-sdk/include/sisclient']
path += [cwd + '/iotsharp-c-sdk/platform/os/rt-thread']
path += [cwd + '/iotsharp-c-sdk/src/mqtt/MQTTPacket/src']
path += [cwd + '/iotsharp-c-sdk/public/net/tcp']

if GetDepend(['PKG_USING_IOTSHARP_EXAMPLES']):
src += Glob('samples/*.c')



group = DefineGroup('iotsharp-c-sdk', src, depend = [''], CPPPATH = path)

Return('group')
3 changes: 3 additions & 0 deletions docs/c_sdk_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# C SDK 接口说明文档

## 设备信息结构体
Binary file added docs/figures/select_sharp_c_sdk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions iotsharp-c-sdk/include/iotsharp_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef iotsharp_CLIENT_H
#define iotsharp_CLIENT_H
/*********************************************************************************
* 文件名称: iotsharp_client.h
* 作 者: 麦壳饼
* 版 本:
**********************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#define SDK_VERSION "v1.0.0"

#ifndef SDK_PLATFORM
#define SDK_PLATFORM "unknown"
#endif

#define NO_LOG_LEVL 0 //不输出日志
#define ERROR_LOG_LEVL 1 //输出错误日志
#define INFO_LOG_LEVL 2 //输出运行时日志和错误日志
#define DEBUG_LOG_LEVL 3 //输出调试日志、运行时日志和错误日志

#endif


232 changes: 232 additions & 0 deletions iotsharp-c-sdk/src/iotsharp_client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include <rtthread.h>

#define DBG_ENABLE
#define DBG_SECTION_NAME "iotsharp"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>

#include "paho_mqtt.h"

/**
* MQTT URI farmat:
* domain mode
* tcp://iot.eclipse.org:1883
*
* ipv4 mode
* tcp://192.168.10.1:1883
* ssl://192.168.10.1:1884
*
* ipv6 mode
* tcp://[fe80::20c:29ff:fe9a:a07e]:1883
* ssl://[fe80::20c:29ff:fe9a:a07e]:1884
*/
#define MQTT_URI "tcp://iot.eclipse.org:1883"
#define MQTT_USERNAME "admin"
#define MQTT_PASSWORD "admin"
#define MQTT_SUBTOPIC "/mqtt/test"
#define MQTT_PUBTOPIC "/mqtt/test"
#define MQTT_WILLMSG "Goodbye!"

/* define MQTT client context */
static MQTTClient client;
static int is_started = 0;

static void mqtt_sub_callback(MQTTClient *c, MessageData *msg_data)
{
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
LOG_D("mqtt sub callback: %.*s %.*s",
msg_data->topicName->lenstring.len,
msg_data->topicName->lenstring.data,
msg_data->message->payloadlen,
(char *)msg_data->message->payload);
}

static void mqtt_sub_default_callback(MQTTClient *c, MessageData *msg_data)
{
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
LOG_D("mqtt sub default callback: %.*s %.*s",
msg_data->topicName->lenstring.len,
msg_data->topicName->lenstring.data,
msg_data->message->payloadlen,
(char *)msg_data->message->payload);
}

static void mqtt_connect_callback(MQTTClient *c)
{
LOG_D("inter mqtt_connect_callback!");
}

static void mqtt_online_callback(MQTTClient *c)
{
LOG_D("inter mqtt_online_callback!");
}

static void mqtt_offline_callback(MQTTClient *c)
{
LOG_D("inter mqtt_offline_callback!");
}

static int mqtt_start(int argc, char **argv)
{
/* init condata param by using MQTTPacket_connectData_initializer */
MQTTPacket_connectData condata = MQTTPacket_connectData_initializer;
static char cid[20] = { 0 };

if (argc != 1)
{
rt_kprintf("mqtt_start --start a mqtt worker thread.\n");
return -1;
}

if (is_started)
{
LOG_E("mqtt client is already connected.");
return -1;
}
/* config MQTT context param */
{
client.isconnected = 0;
client.uri = MQTT_URI;

/* generate the random client ID */
rt_snprintf(cid, sizeof(cid), "rtthread%d", rt_tick_get());
/* config connect param */
memcpy(&client.condata, &condata, sizeof(condata));
client.condata.clientID.cstring = cid;
client.condata.keepAliveInterval = 30;
client.condata.cleansession = 1;
client.condata.username.cstring = MQTT_USERNAME;
client.condata.password.cstring = MQTT_PASSWORD;

/* config MQTT will param. */
client.condata.willFlag = 1;
client.condata.will.qos = 1;
client.condata.will.retained = 0;
client.condata.will.topicName.cstring = MQTT_PUBTOPIC;
client.condata.will.message.cstring = MQTT_WILLMSG;

/* malloc buffer. */
client.buf_size = client.readbuf_size = 1024;
client.buf = rt_calloc(1, client.buf_size);
client.readbuf = rt_calloc(1, client.readbuf_size);
if (!(client.buf && client.readbuf))
{
LOG_E("no memory for MQTT client buffer!");
return -1;
}

/* set event callback function */
client.connect_callback = mqtt_connect_callback;
client.online_callback = mqtt_online_callback;
client.offline_callback = mqtt_offline_callback;

/* set subscribe table and event callback */
client.messageHandlers[0].topicFilter = rt_strdup(MQTT_SUBTOPIC);
client.messageHandlers[0].callback = mqtt_sub_callback;
client.messageHandlers[0].qos = QOS1;

/* set default subscribe event callback */
client.defaultMessageHandler = mqtt_sub_default_callback;
}

/* run mqtt client */
paho_mqtt_start(&client);
is_started = 1;

return 0;
}

static int mqtt_stop(int argc, char **argv)
{
if (argc != 1)
{
rt_kprintf("mqtt_stop --stop mqtt worker thread and free mqtt client object.\n");
}

is_started = 0;

return paho_mqtt_stop(&client);
}

static int mqtt_publish(int argc, char **argv)
{
if (is_started == 0)
{
LOG_E("mqtt client is not connected.");
return -1;
}

if (argc == 2)
{
paho_mqtt_publish(&client, QOS1, MQTT_PUBTOPIC, argv[1]);
}
else if (argc == 3)
{
paho_mqtt_publish(&client, QOS1, argv[1], argv[2]);
}
else
{
rt_kprintf("mqtt_publish <topic> [message] --mqtt publish message to specified topic.\n");
return -1;
}

return 0;
}

static void mqtt_new_sub_callback(MQTTClient *client, MessageData *msg_data)
{
*((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
LOG_D("mqtt new subscribe callback: %.*s %.*s",
msg_data->topicName->lenstring.len,
msg_data->topicName->lenstring.data,
msg_data->message->payloadlen,
(char *)msg_data->message->payload);
}

static int mqtt_subscribe(int argc, char **argv)
{
if (argc != 2)
{
rt_kprintf("mqtt_subscribe [topic] --send an mqtt subscribe packet and wait for suback before returning.\n");
return -1;
}

if (is_started == 0)
{
LOG_E("mqtt client is not connected.");
return -1;
}

return paho_mqtt_subscribe(&client, QOS1, argv[1], mqtt_new_sub_callback);
}

static int mqtt_unsubscribe(int argc, char **argv)
{
if (argc != 2)
{
rt_kprintf("mqtt_unsubscribe [topic] --send an mqtt unsubscribe packet and wait for suback before returning.\n");
return -1;
}

if (is_started == 0)
{
LOG_E("mqtt client is not connected.");
return -1;
}

return paho_mqtt_unsubscribe(&client, argv[1]);
}

#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(mqtt_start, startup mqtt client);
MSH_CMD_EXPORT(mqtt_stop, stop mqtt client);
MSH_CMD_EXPORT(mqtt_publish, mqtt publish message to specified topic);
MSH_CMD_EXPORT(mqtt_subscribe, mqtt subscribe topic);
MSH_CMD_EXPORT(mqtt_unsubscribe, mqtt unsubscribe topic);
#endif /* FINSH_USING_MSH */

30 changes: 30 additions & 0 deletions samples/demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <time.h>
#include <string.h>
#ifdef PKG_USING_IOTSHARP_SERVER
#define DEVICE_SERVER PKG_USING_IOTSHARP_DEVICE_SERVER
#else
#define DEVICE_SERVER ""
#endif


#ifdef PKG_USING_IOTSHARP_DEVICE_NAME
#define DEVICE_NAME PKG_USING_IOTSHARP_DEVICE_NAME
#else
#define DEVICE_NAME ""
#endif

#ifdef PKG_USING_IOTSHARP_DEVICE_TOKEN
#define DEVICE_TOKEN PKG_USING_IOTSHARP_DEVICE_TOKEN
#else
#define DEVICE_TOKEN ""
#endif



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

MSH_CMD_EXPORT_ALIAS(start_demo_test, start_demo_test,Example: start_demo_test);
#endif

0 comments on commit 112f08c

Please sign in to comment.