支持BLE设备接入“腾讯连连”小程序

This commit is contained in:
zekwang
2020-12-01 20:11:07 +08:00
parent 2a06226767
commit a6ff5bb42b
151 changed files with 47376 additions and 7 deletions

View File

@@ -0,0 +1,541 @@
/*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "ble_qiot_template.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "ble_qiot_export.h"
#include "ble_qiot_common.h"
#include "ble_qiot_param_check.h"
extern void property_power_switch(const char *data, uint16_t len);
extern void action_led_blink(int ms);
extern void report_reply_blink(void);
static uint8_t sg_test_power_switch = false;
static int ble_property_power_switch_set(const char *data, uint16_t len)
{
ble_qiot_log_d("set property power_switch %d", *(uint8_t *)data);
property_power_switch(data, len);
sg_test_power_switch = data[0];
return 0;
}
static int ble_property_power_switch_get(char *data, uint16_t buf_len)
{
ble_qiot_log_d("get property power_switch %d", sg_test_power_switch);
data[0] = sg_test_power_switch;
return sizeof(uint8_t);
}
static uint16_t sg_test_color = 0;
static int ble_property_color_set(const char *data, uint16_t len)
{
uint16_t color_value = 0;
memcpy(&color_value, data, sizeof(uint16_t));
color_value = NTOHS(color_value);
ble_qiot_log_d("set property color %d", color_value);
sg_test_color = color_value;
return 0;
}
static int ble_property_color_get(char *data, uint16_t buf_len)
{
uint16_t color_value = 0;
ble_qiot_log_d("get property color %d", color_value);
color_value = HTONS(sg_test_color);
memcpy(data, &color_value, sizeof(uint16_t));
return sizeof(uint16_t);
}
static int sg_test_brightness = 0;
static int ble_property_brightness_set(const char *data, uint16_t len)
{
int brightness_value = 0;
memcpy(&brightness_value, data, sizeof(int));
brightness_value = NTOHL(brightness_value);
if ((brightness_value < BLE_QIOT_PROPERTY_BRIGHTNESS_MIN) ||
(brightness_value > BLE_QIOT_PROPERTY_BRIGHTNESS_MAX)) {
ble_qiot_log_e("invalid brightness value %d", brightness_value);
return -1;
}
ble_qiot_log_d("set property brightness %d", brightness_value);
sg_test_brightness = brightness_value;
return 0;
}
static int ble_property_brightness_get(char *data, uint16_t buf_len)
{
int brightness_value = 0;
ble_qiot_log_d("get property brightness %d", sg_test_brightness);
brightness_value = HTONL(sg_test_brightness);
memcpy(data, &brightness_value, sizeof(int));
return sizeof(uint32_t);
}
static char sg_test_name[100 + 1] = "default name";
static int ble_property_name_set(const char *data, uint16_t len)
{
ble_qiot_log_d("set property name %.*s", len, data);
if (len > sizeof(sg_test_name) - 1) {
ble_qiot_log_d("too long name");
return -1;
}
memset(sg_test_name, 0, sizeof(sg_test_name));
memcpy(sg_test_name, data, len);
return 0;
}
static int ble_property_name_get(char *data, uint16_t buf_len)
{
int i = 0;
ble_qiot_log_d("get property name %s", sg_test_name);
if (0 == strncmp("default name", sg_test_name, sizeof("default name") - 1)) {
for (i = 0; i < 26 * 3; i++) {
data[i] = 'a' + (i % 26);
}
return i;
} else {
memcpy(data, sg_test_name, strlen(sg_test_name));
return strlen(sg_test_name);
}
}
static ble_property_t sg_ble_property_array[BLE_QIOT_PROPERTY_ID_BUTT] = {
{ble_property_power_switch_set, ble_property_power_switch_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_BOOL},
{ble_property_color_set, ble_property_color_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_ENUM},
{ble_property_brightness_set, ble_property_brightness_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_INT},
{ble_property_name_set, ble_property_name_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_STRING},
};
static bool ble_check_space_enough_by_type(uint8_t type, uint16_t left_size)
{
switch (type) {
case BLE_QIOT_DATA_TYPE_BOOL:
return left_size >= sizeof(uint8_t);
case BLE_QIOT_DATA_TYPE_INT:
case BLE_QIOT_DATA_TYPE_FLOAT:
case BLE_QIOT_DATA_TYPE_TIME:
return left_size >= sizeof(uint32_t);
case BLE_QIOT_DATA_TYPE_ENUM:
return left_size >= sizeof(uint16_t);
default:
// string length is unknow, default true
return true;
}
}
static uint16_t ble_check_ret_value_by_type(uint8_t type, uint16_t buf_len, uint16_t ret_val)
{
switch (type) {
case BLE_QIOT_DATA_TYPE_BOOL:
return ret_val <= sizeof(uint8_t);
case BLE_QIOT_DATA_TYPE_INT:
case BLE_QIOT_DATA_TYPE_FLOAT:
case BLE_QIOT_DATA_TYPE_TIME:
return ret_val <= sizeof(uint32_t);
case BLE_QIOT_DATA_TYPE_ENUM:
return ret_val <= sizeof(uint16_t);
default:
// string length is unknow, default true
return ret_val <= buf_len;
}
}
uint8_t ble_get_property_type_by_id(uint8_t id)
{
if (id >= BLE_QIOT_PROPERTY_ID_BUTT) {
ble_qiot_log_e("invalid property id %d", id);
return BLE_QIOT_DATA_TYPE_BUTT;
}
return sg_ble_property_array[id].type;
}
int ble_user_property_set_data(const e_ble_tlv *tlv)
{
POINTER_SANITY_CHECK(tlv, BLE_QIOT_RS_ERR_PARA);
if (tlv->id >= BLE_QIOT_PROPERTY_ID_BUTT) {
ble_qiot_log_e("invalid property id %d", tlv->id);
return BLE_QIOT_RS_ERR;
}
if (NULL != sg_ble_property_array[tlv->id].set_cb) {
if (0 != sg_ble_property_array[tlv->id].set_cb(tlv->val, tlv->len)) {
ble_qiot_log_e("set property id %d failed", tlv->id);
return BLE_QIOT_RS_ERR;
} else {
return BLE_QIOT_RS_OK;
}
}
ble_qiot_log_e("invalid set callback, id %d", tlv->id);
return BLE_QIOT_RS_ERR;
}
int ble_user_property_get_data_by_id(uint8_t id, char *buf, uint16_t buf_len)
{
int ret_len = 0;
POINTER_SANITY_CHECK(buf, BLE_QIOT_RS_ERR_PARA);
if (id >= BLE_QIOT_PROPERTY_ID_BUTT) {
ble_qiot_log_e("invalid property id %d", id);
return -1;
}
if (NULL != sg_ble_property_array[id].get_cb) {
if (!ble_check_space_enough_by_type(sg_ble_property_array[id].type, buf_len)) {
ble_qiot_log_e("not enough space get property id %d data", id);
return -1;
}
ret_len = sg_ble_property_array[id].get_cb(buf, buf_len);
if (ret_len < 0) {
ble_qiot_log_e("get property id %d data failed", id);
return -1;
} else {
if (ble_check_ret_value_by_type(sg_ble_property_array[id].type, buf_len, ret_len)) {
return ret_len;
} else {
ble_qiot_log_e("property id %d length invalid, type %d", id, sg_ble_property_array[id].type);
return -1;
}
}
}
ble_qiot_log_e("invalid callback, property id %d", id);
return 0;
}
int ble_user_property_report_reply_handle(uint8_t result)
{
ble_qiot_log_d("report reply result %d", result);
if (0 == result) {
report_reply_blink();
}
return BLE_QIOT_RS_OK;
}
static int ble_event_get_status_report_status(char *buf, uint16_t buf_len)
{
buf[0] = 1;
return 1;
}
static int ble_event_get_status_report_message(char *buf, uint16_t buf_len)
{
int i = 0;
for (i = 0; i < 26 * 3; i++) {
buf[i] = 'a' + (i % 26);
}
return i;
}
static ble_event_param sg_ble_event_status_report_array[BLE_QIOT_EVENT_STATUS_REPORT_PARAM_ID_BUTT] = {
{ble_event_get_status_report_status, BLE_QIOT_DATA_TYPE_BOOL},
{ble_event_get_status_report_message, BLE_QIOT_DATA_TYPE_STRING},
};
static int ble_event_get_low_voltage_voltage(char *data, uint16_t buf_len)
{
float tmp = 1.0;
memcpy(data, &tmp, sizeof(float));
return sizeof(float);
}
static ble_event_param sg_ble_event_low_voltage_array[BLE_QIOT_EVENT_LOW_VOLTAGE_PARAM_ID_BUTT] = {
{ble_event_get_low_voltage_voltage, BLE_QIOT_DATA_TYPE_FLOAT},
};
static int ble_event_get_hardware_fault_name(char *data, uint16_t buf_len)
{
memcpy(data, "hardware_fault", sizeof("hardware_fault") - 1);
return sizeof("hardware_fault") - 1;
}
static int ble_event_get_hardware_fault_error_code(char *data, uint16_t buf_len)
{
int error_code = HTONL(1024);
memcpy(data, &error_code, sizeof(int));
return sizeof(int);
}
static ble_event_param sg_ble_event_hardware_fault_array[BLE_QIOT_EVENT_HARDWARE_FAULT_PARAM_ID_BUTT] = {
{ble_event_get_hardware_fault_name, BLE_QIOT_DATA_TYPE_STRING},
{ble_event_get_hardware_fault_error_code, BLE_QIOT_DATA_TYPE_INT},
};
static ble_event_t sg_ble_event_array[BLE_QIOT_EVENT_ID_BUTT] = {
{sg_ble_event_status_report_array, sizeof(sg_ble_event_status_report_array) / sizeof(ble_event_param)},
{sg_ble_event_low_voltage_array, sizeof(sg_ble_event_low_voltage_array) / sizeof(ble_event_param)},
{sg_ble_event_hardware_fault_array, sizeof(sg_ble_event_hardware_fault_array) / sizeof(ble_event_param)},
};
int ble_event_get_id_array_size(uint8_t event_id)
{
if (event_id >= BLE_QIOT_EVENT_ID_BUTT) {
ble_qiot_log_e("invalid event id %d", event_id);
return -1;
}
return sg_ble_event_array[event_id].array_size;
}
uint8_t ble_event_get_param_id_type(uint8_t event_id, uint8_t param_id)
{
if (event_id >= BLE_QIOT_EVENT_ID_BUTT) {
ble_qiot_log_e("invalid event id %d", event_id);
return BLE_QIOT_DATA_TYPE_BUTT;
}
if (param_id >= sg_ble_event_array[event_id].array_size) {
ble_qiot_log_e("invalid param id %d", param_id);
return BLE_QIOT_DATA_TYPE_BUTT;
}
return sg_ble_event_array[event_id].event_array[param_id].type;
}
int ble_event_get_data_by_id(uint8_t event_id, uint8_t param_id, char *out_buf, uint16_t buf_len)
{
int ret_len = 0;
if (event_id >= BLE_QIOT_EVENT_ID_BUTT) {
ble_qiot_log_e("invalid event id %d", event_id);
return -1;
}
if (param_id >= sg_ble_event_array[event_id].array_size) {
ble_qiot_log_e("invalid param id %d", param_id);
return -1;
}
if (NULL == sg_ble_event_array[event_id].event_array[param_id].get_cb) {
ble_qiot_log_e("invalid callback, event id %d, param id %d", event_id, param_id);
return 0;
}
if (!ble_check_space_enough_by_type(sg_ble_event_array[event_id].event_array[param_id].type, buf_len)) {
ble_qiot_log_e("not enough space get data, event id %d, param id %d", event_id, param_id);
return -1;
}
ret_len = sg_ble_event_array[event_id].event_array[param_id].get_cb(out_buf, buf_len);
if (ret_len < 0) {
ble_qiot_log_e("get event data failed, event id %d, param id %d", event_id, param_id);
return -1;
} else {
if (ble_check_ret_value_by_type(sg_ble_event_array[event_id].event_array[param_id].type, buf_len, ret_len)) {
return ret_len;
} else {
ble_qiot_log_e("evnet data length invalid, event id %d, param id %d, type %d", event_id, param_id,
sg_ble_event_array[event_id].event_array[param_id].type);
return -1;
}
}
}
int ble_user_event_reply_handle(uint8_t event_id, uint8_t result)
{
ble_qiot_log_d("event id %d, reply result %d", event_id, result);
return BLE_QIOT_RS_OK;
}
static int ble_action_handle_loop_input_cb(e_ble_tlv *input_param_array, uint8_t input_array_size,
uint8_t *output_id_array)
{
int result = 0;
if (NULL == input_param_array || NULL == output_id_array) {
ble_qiot_log_e("invalid param");
return -1;
}
report_reply_blink();
for (int i = 0; i < input_array_size; i++) {
ble_qiot_log_d("id %d", input_param_array[i].id);
}
memcpy(&result, input_param_array[0].val, sizeof(int));
result = NTOHL(result);
ble_qiot_log_d("id %d, val %d", input_param_array[0].id, result);
action_led_blink(result);
output_id_array[BLE_QIOT_ACTION_LOOP_OUTPUT_ID_RESULT] = true;
return 0;
}
static int ble_action_handle_loop_output_cb(uint8_t output_id, char *buf, uint16_t buf_len)
{
int data_len = 0;
int i = 0;
switch (output_id) {
case BLE_QIOT_ACTION_LOOP_OUTPUT_ID_RESULT:
for (i = 0; i < 26 * 3; i++) {
buf[i] = 'a' + (i % 26);
}
data_len = i;
break;
default:
break;
}
return data_len;
}
static uint8_t sg_ble_action_loop_input_type_array[BLE_QIOT_ACTION_LOOP_INPUT_ID_BUTT] = {
BLE_QIOT_DATA_TYPE_INT,
};
static uint8_t sg_ble_action_loop_output_type_array[BLE_QIOT_ACTION_LOOP_OUTPUT_ID_BUTT] = {
BLE_QIOT_DATA_TYPE_STRING,
};
static ble_action_t sg_ble_action_array[BLE_QIOT_ACTION_ID_BUTT] = {
{ble_action_handle_loop_input_cb, ble_action_handle_loop_output_cb, sg_ble_action_loop_input_type_array,
sg_ble_action_loop_output_type_array, sizeof(sg_ble_action_loop_input_type_array) / sizeof(uint8_t),
sizeof(sg_ble_action_loop_output_type_array) / sizeof(uint8_t)},
};
uint8_t ble_action_get_intput_type_by_id(uint8_t action_id, uint8_t input_id)
{
if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
ble_qiot_log_e("invalid action id %d", action_id);
return BLE_QIOT_DATA_TYPE_BUTT;
}
if (input_id >= sg_ble_event_array[action_id].array_size) {
ble_qiot_log_e("invalid input id %d", input_id);
return BLE_QIOT_DATA_TYPE_BUTT;
}
return sg_ble_action_array[action_id].input_type_array[input_id];
}
uint8_t ble_action_get_output_type_by_id(uint8_t action_id, uint8_t output_id)
{
if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
ble_qiot_log_e("invalid action id %d", action_id);
return BLE_QIOT_DATA_TYPE_BUTT;
}
if (output_id >= sg_ble_event_array[action_id].array_size) {
ble_qiot_log_e("invalid output id %d", output_id);
return BLE_QIOT_DATA_TYPE_BUTT;
}
return sg_ble_action_array[action_id].output_type_array[output_id];
}
int ble_action_get_input_id_size(uint8_t action_id)
{
if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
ble_qiot_log_e("invalid action id %d", action_id);
return -1;
}
return sg_ble_action_array[action_id].input_id_size;
}
int ble_action_get_output_id_size(uint8_t action_id)
{
if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
ble_qiot_log_e("invalid action id %d", action_id);
return -1;
}
return sg_ble_action_array[action_id].output_id_size;
}
int ble_action_user_handle_input_param(uint8_t action_id, e_ble_tlv *input_param_array, uint8_t input_array_size,
uint8_t *output_id_array)
{
if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
ble_qiot_log_e("invalid action id %d", action_id);
return -1;
}
if (NULL != sg_ble_action_array[action_id].input_cb) {
if (0 != sg_ble_action_array[action_id].input_cb(input_param_array, input_array_size, output_id_array)) {
ble_qiot_log_e("input handle error");
return -1;
}
}
return 0;
}
int ble_action_user_handle_output_param(uint8_t action_id, uint8_t output_id, char *buf, uint16_t buf_len)
{
int ret_len = 0;
if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
ble_qiot_log_e("invalid action id %d", action_id);
return -1;
}
if (NULL == sg_ble_action_array[action_id].output_cb) {
ble_qiot_log_e("invalid callback, action id %d", action_id);
return 0;
}
if (!ble_check_space_enough_by_type(sg_ble_action_array[action_id].output_type_array[output_id], buf_len)) {
ble_qiot_log_e("not enough space get data, action id %d, output id %d", action_id, output_id);
return -1;
}
ret_len = sg_ble_action_array[action_id].output_cb(output_id, buf, buf_len);
if (ret_len < 0) {
ble_qiot_log_e("get action data failed, action id %d, output id %d", action_id, output_id);
return -1;
} else {
if (ble_check_ret_value_by_type(sg_ble_action_array[action_id].output_type_array[output_id], buf_len,
ret_len)) {
return ret_len;
} else {
ble_qiot_log_e("action data length invalid, action id %d, output id %d", action_id, output_id);
return -1;
}
}
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,285 @@
/*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef BLE_QIOT_TEMPLATE_H_
#define BLE_QIOT_TEMPLATE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
// data type in template, corresponding to type in json file
enum {
BLE_QIOT_DATA_TYPE_BOOL = 0,
BLE_QIOT_DATA_TYPE_INT,
BLE_QIOT_DATA_TYPE_STRING,
BLE_QIOT_DATA_TYPE_FLOAT,
BLE_QIOT_DATA_TYPE_ENUM,
BLE_QIOT_DATA_TYPE_TIME,
BLE_QIOT_DATA_TYPE_BUTT,
};
// message type, reference data template
enum {
BLE_QIOT_MSG_TYPE_PROPERTY = 0,
BLE_QIOT_MSG_TYPE_EVENT,
BLE_QIOT_MSG_TYPE_ACTION,
BLE_QIOT_MSG_TYPE_BUTT,
};
// define property authority, not used
enum {
BLE_QIOT_PROPERTY_AUTH_RW = 0,
BLE_QIOT_PROPERTY_AUTH_READ,
BLE_QIOT_PROPERTY_AUTH_BUTT,
};
// define reply result
enum {
BLE_QIOT_REPLY_SUCCESS = 0,
BLE_QIOT_REPLY_FAIL,
BLE_QIOT_REPLY_DATA_ERR,
BLE_QIOT_REPLY_BUTT,
};
// define message flow direction
enum {
BLE_QIOT_EFFECT_REQUEST = 0,
BLE_QIOT_EFFECT_REPLY,
BLE_QIOT_EFFECT_BUTT,
};
// define message type that from server to device
enum {
BLE_QIOT_DATA_DOWN_REPORT_REPLY = 0,
BLE_QIOT_DATA_DOWN_CONTROL,
BLE_QIOT_DATA_DOWN_GET_STATUS_REPLY,
BLE_QIOT_DATA_DOWN_ACTION,
BLE_QIOT_DATA_DOWN_EVENT_REPLY,
};
// define message type that from device to server
enum {
BLE_QIOT_EVENT_UP_PROPERTY_REPORT = 0,
BLE_QIOT_EVENT_UP_CONTROL_REPLY,
BLE_QIOT_EVENT_UP_GET_STATUS,
BLE_QIOT_EVENT_UP_EVENT_POST,
BLE_QIOT_EVENT_UP_ACTION_REPLY,
BLE_QIOT_EVENT_UP_BIND_SIGN_RET,
BLE_QIOT_EVENT_UP_CONN_SIGN_RET,
BLE_QIOT_EVENT_UP_UNBIND_SIGN_RET,
BLE_QIOT_EVENT_UP_REPORT_MTU,
BLE_QIOT_EVENT_UP_BUTT,
};
// msg header define, bit 7-6 is msg type, bit 5 means request or reply, bit 4 - 0 is id
#define BLE_QIOT_PARSE_MSG_HEAD_TYPE(_C) (((_C)&0XFF) >> 6)
#define BLE_QIOT_PARSE_MSG_HEAD_EFFECT(_C) ((((_C)&0XFF) & 0X20) ? BLE_QIOT_EFFECT_REPLY : BLE_QIOT_EFFECT_REQUEST)
#define BLE_QIOT_PARSE_MSG_HEAD_ID(_C) ((_C)&0X1F)
#define BLE_QIOT_PACKAGE_MSG_HEAD(_TYPE, _REPLY, _ID) \
(((_TYPE) << 6) | (((_REPLY) == BLE_QIOT_EFFECT_REPLY) << 5) | ((_ID)&0X1F))
// tlv header define, bit 7 - 5 is type, bit 4 - 0 depends on type of data template
#define BLE_QIOT_PARSE_TLV_HEAD_TYPE(_C) (((_C)&0XFF) >> 5)
#define BLE_QIOT_PARSE_TLV_HEAD_ID(_C) ((_C)&0X1F)
#define BLE_QIOT_PACKAGE_TLV_HEAD(_TYPE, _ID) (((_TYPE) << 5) | ((_ID)&0X1F))
// define property id
enum {
BLE_QIOT_PROPERTY_ID_POWER_SWITCH = 0,
BLE_QIOT_PROPERTY_ID_COLOR,
BLE_QIOT_PROPERTY_ID_BRIGHTNESS,
BLE_QIOT_PROPERTY_ID_NAME,
BLE_QIOT_PROPERTY_ID_BUTT,
};
// define property color enum
enum {
BLE_QIOT_PROPERTY_COLOR_RED = 0,
BLE_QIOT_PROPERTY_COLOR_GREEN = 1,
BLE_QIOT_PROPERTY_COLOR_BLUE = 2,
BLE_QIOT_PROPERTY_COLOR_BUTT = 3,
};
// define brightness attributes
#define BLE_QIOT_PROPERTY_BRIGHTNESS_STEP (1)
#define BLE_QIOT_PROPERTY_BRIGHTNESS_MIN (0)
#define BLE_QIOT_PROPERTY_BRIGHTNESS_MAX (100)
#define BLE_QIOT_PROPERTY_BRIGHTNESS_START (1)
// define name length limit
#define BLE_QIOT_PROPERTY_NAME_LEN_MIN (0)
#define BLE_QIOT_PROPERTY_NAME_LEN_MAX (640)
// define property set handle return 0 if success, other is error
// sdk call the function that inform the server data to the device
typedef int (*property_set_cb)(const char *data, uint16_t len);
// define property get handle. return the data length obtained, -1 is error, 0 is no data
// sdk call the function fetch user data and send to the server, the data should wrapped by user adn skd just transmit
typedef int (*property_get_cb)(char *buf, uint16_t buf_len);
// each property have a struct ble_property_t, make up a array named sg_ble_property_array
typedef struct {
property_set_cb set_cb; // set callback
property_get_cb get_cb; // get callback
uint8_t authority; // property authority
uint8_t type; // data type
} ble_property_t;
// define event id
enum {
BLE_QIOT_EVENT_ID_STATUS_REPORT = 0,
BLE_QIOT_EVENT_ID_LOW_VOLTAGE,
BLE_QIOT_EVENT_ID_HARDWARE_FAULT,
BLE_QIOT_EVENT_ID_BUTT,
};
// define param id for event status_report
enum {
BLE_QIOT_EVENT_STATUS_REPORT_PARAM_ID_STATUS = 0,
BLE_QIOT_EVENT_STATUS_REPORT_PARAM_ID_MESSAGE,
BLE_QIOT_EVENT_STATUS_REPORT_PARAM_ID_BUTT,
};
// define range for param message
#define BLE_QIOT_EVENT_STATUS_REPORT_MESSAGE_LEN_MIN (0)
#define BLE_QIOT_EVENT_STATUS_REPORT_MESSAGE_LEN_MAX (64)
// define param id for event low_voltage
enum {
BLE_QIOT_EVENT_LOW_VOLTAGE_PARAM_ID_VOLTAGE = 0,
BLE_QIOT_EVENT_LOW_VOLTAGE_PARAM_ID_BUTT,
};
// define param voltage attributes
#define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_STEP (1)
#define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_MIN (0.0)
#define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_MAX (24.0)
#define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_START (1)
// define param id for event hardware_fault
enum {
BLE_QIOT_EVENT_HARDWARE_FAULT_PARAM_ID_NAME = 0,
BLE_QIOT_EVENT_HARDWARE_FAULT_PARAM_ID_ERROR_CODE,
BLE_QIOT_EVENT_HARDWARE_FAULT_PARAM_ID_BUTT,
};
// define range for param name
#define BLE_QIOT_EVENT_HARDWARE_FAULT_NAME_LEN_MIN (0)
#define BLE_QIOT_EVENT_HARDWARE_FAULT_NAME_LEN_MAX (64)
// define param error_code attributes
#define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_STEP (1)
#define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_MIN (0)
#define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_MAX (2000)
#define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_START (1)
// define event get handle. return the data length obtained, -1 is error, 0 is no data
// sdk call the function fetch user data and send to the server, the data should wrapped by user adn skd just transmit
typedef int (*event_get_cb)(char *buf, uint16_t buf_len);
// each param have a struct ble_event_param, make up a array for the event
typedef struct {
event_get_cb get_cb; // get param data callback
uint8_t type; // param type
} ble_event_param;
// a array named sg_ble_event_array is composed by all the event array
typedef struct {
ble_event_param *event_array; // array of params data
uint8_t array_size; // array size
} ble_event_t;
// define action id
enum {
BLE_QIOT_ACTION_ID_LOOP = 0,
BLE_QIOT_ACTION_ID_BUTT,
};
// define input id for action loop
enum {
BLE_QIOT_ACTION_LOOP_INPUT_ID_INTERVAL = 0,
BLE_QIOT_ACTION_LOOP_INPUT_ID_BUTT,
};
// define output id for action loop
enum {
BLE_QIOT_ACTION_LOOP_OUTPUT_ID_RESULT = 0,
BLE_QIOT_ACTION_LOOP_OUTPUT_ID_BUTT,
};
#define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_MIN (0)
#define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_MAX (100)
#define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_START (0)
#define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_STEP (1)
// define output id result attributes
#define BLE_QIOT_ACTION_OUTPUT_LOOP_RESULT_LEN_MIN (0)
#define BLE_QIOT_ACTION_OUTPUT_LOOP_RESULT_LEN_MAX (320)
// define max input id and output id in all of input id and output id above
#define BLE_QIOT_ACTION_INPUT_ID_BUTT 1
#define BLE_QIOT_ACTION_OUTPUT_ID_BUTT 1
// define tlv struct
typedef struct {
uint8_t type;
uint8_t id;
uint16_t len;
char * val;
} e_ble_tlv;
// define action input handle, return 0 is success, other is error.
// input_param_array carry the data from server, include input id, data length ,data val
// input_array_size means how many input id
// output_id_array filling with output id numbers that need obtained, sdk will traverse it and call the
// action_output_handle to obtained data
typedef int (*action_input_handle)(e_ble_tlv *input_param_array, uint8_t input_array_size, uint8_t *output_id_array);
// define action output handle, return length of the data, 0 is no data, -1 is error
// output_id means which id data should be obtained
typedef int (*action_output_handle)(uint8_t output_id, char *buf, uint16_t buf_len);
// each action have a struct ble_action_t, make up a array named sg_ble_action_array
typedef struct {
action_input_handle input_cb; // handle input data
action_output_handle output_cb; // get output data in the callback
uint8_t * input_type_array; // type array for input id
uint8_t * output_type_array; // type array for output id
uint8_t input_id_size; // numbers of input id
uint8_t output_id_size; // numbers of output id
} ble_action_t;
// property module
uint8_t ble_get_property_type_by_id(uint8_t id);
int ble_user_property_set_data(const e_ble_tlv *tlv);
int ble_user_property_get_data_by_id(uint8_t id, char *buf, uint16_t buf_len);
int ble_user_property_report_reply_handle(uint8_t result);
// event module
int ble_event_get_id_array_size(uint8_t event_id);
uint8_t ble_event_get_param_id_type(uint8_t event_id, uint8_t param_id);
int ble_event_get_data_by_id(uint8_t event_id, uint8_t param_id, char *out_buf, uint16_t buf_len);
int ble_user_event_reply_handle(uint8_t event_id, uint8_t result);
// action module
uint8_t ble_action_get_intput_type_by_id(uint8_t action_id, uint8_t input_id);
uint8_t ble_action_get_output_type_by_id(uint8_t action_id, uint8_t output_id);
int ble_action_get_input_id_size(uint8_t action_id);
int ble_action_get_output_id_size(uint8_t action_id);
int ble_action_user_handle_input_param(uint8_t action_id, e_ble_tlv *input_param_array, uint8_t input_array_size,
uint8_t *output_id_array);
int ble_action_user_handle_output_param(uint8_t action_id, uint8_t output_id, char *buf, uint16_t buf_len);
#ifdef __cplusplus
}
#endif
#endif // BLE_QIOT_TEMPLATE_H_

View File

@@ -0,0 +1,183 @@
{
"version": "1.0",
"profile": {
"ProductId": "DHZX03IQAZ",
"CategoryId": "141"
},
"properties": [
{
"id": "power_switch",
"name": "电灯开关",
"desc": "控制电灯开灭",
"required": true,
"mode": "rw",
"define": {
"type": "bool",
"mapping": {
"0": "关",
"1": "开"
}
}
},
{
"id": "color",
"name": "颜色",
"desc": "灯光颜色",
"mode": "rw",
"define": {
"type": "enum",
"mapping": {
"0": "Red",
"1": "Green",
"2": "Blue"
}
}
},
{
"id": "brightness",
"name": "亮度",
"desc": "灯光亮度",
"mode": "rw",
"define": {
"type": "int",
"unit": "%",
"step": "1",
"min": "0",
"max": "100",
"start": "1"
}
},
{
"id": "name",
"name": "灯位置名称",
"desc": "灯位置名称:书房、客厅等",
"mode": "rw",
"define": {
"type": "string",
"min": "0",
"max": "640"
},
"required": false
}
],
"events": [
{
"id": "status_report",
"name": "DeviceStatus",
"desc": "Report the device status",
"type": "info",
"required": false,
"params": [
{
"id": "status",
"name": "running_state",
"desc": "Report current device running state",
"define": {
"type": "bool",
"mapping": {
"0": "normal",
"1": "fault"
}
}
},
{
"id": "message",
"name": "Message",
"desc": "Some extra message",
"define": {
"type": "string",
"min": "0",
"max": "64"
}
}
]
},
{
"id": "low_voltage",
"name": "LowVoltage",
"desc": "Alert for device voltage is low",
"type": "alert",
"required": false,
"params": [
{
"id": "voltage",
"name": "Voltage",
"desc": "Current voltage",
"define": {
"type": "float",
"unit": "V",
"step": "1",
"min": "0.0",
"max": "24.0",
"start": "1"
}
}
]
},
{
"id": "hardware_fault",
"name": "Hardware_fault",
"desc": "Report hardware fault",
"type": "fault",
"required": false,
"params": [
{
"id": "name",
"name": "Name",
"desc": "Name like: memory,tf card, censors ...",
"define": {
"type": "string",
"min": "0",
"max": "64"
}
},
{
"id": "error_code",
"name": "Error_Code",
"desc": "Error code for fault",
"define": {
"type": "int",
"unit": "",
"step": "1",
"min": "0",
"max": "2000",
"start": "1"
}
}
]
}
],
"actions": [
{
"id": "loop",
"name": "loop",
"desc": "",
"input": [
{
"id": "interval",
"name": "interval",
"define": {
"type": "int",
"min": "0",
"max": "100",
"start": "0",
"step": "1",
"unit": ""
}
}
],
"output": [
{
"id": "result",
"name": "result",
"define": {
"type": "string",
"min": "0",
"max": "320"
}
}
],
"required": false
}
]
}