在逗猫棒的基础上实现蓝牙转USB功能,目前功能已经实现,还需裁剪掉之前逗猫棒的业务程序

This commit is contained in:
2026-07-24 11:41:08 +08:00
commit b3f06697a6
1569 changed files with 515355 additions and 0 deletions
@@ -0,0 +1,45 @@
/*
* 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 QCLOUD_BLE_QIOT_COMMON_H
#define QCLOUD_BLE_QIOT_COMMON_H
#ifdef __cplusplus
extern "C" {
#endif
#define BLE_QIOT_PRODUCT_ID_LEN (10) // fixed length of product id
#define BLE_QIOT_DEVICE_NAME_LEN (48) // max length of device name
#define BLE_QIOT_PSK_LEN (24) // fixed length of secret key
#define BLE_QIOT_MAC_LEN (6) // fixed length of mac
#define SWAP_32(x) \
((((x)&0xFF000000) >> 24) | (((x)&0x00FF0000) >> 8) | (((x)&0x0000FF00) << 8) | (((x)&0x000000FF) << 24))
#define SWAP_16(x) ((((x)&0xFF00) >> 8) | (((x)&0x00FF) << 8))
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define HTONL(x) SWAP_32(x)
#define HTONS(x) SWAP_16(x)
#define NTOHL(x) SWAP_32(x)
#define NTOHS(x) SWAP_16(x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define HTONL(x) (x)
#define HTONS(x) (x)
#define NTOHL(x) (x)
#define NTOHS(x) (x)
#else
#error "undefined byte order"
#endif
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_COMMON_H
@@ -0,0 +1,120 @@
/*
* 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 QCLOUD_BLE_QIOT_CONFIG_H
#define QCLOUD_BLE_QIOT_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
#include <printf.h>
#include <stdint.h>
#define BLE_QIOT_SDK_VERSION "1.5.0" // sdk version
#define BLE_QIOT_SDK_DEBUG 0 // sdk debug
// the device broadcast is controlled by the user, but we provide a mechanism to help the device save more power.
// if you want broadcast is triggered by something like press a button instead of all the time, and the broadcast
// stopped automatically in a few minutes if the device is not bind, define BLE_QIOT_BUTTON_BROADCAST is 1 and
// BLE_QIOT_BIND_TIMEOUT is the period that broadcast stopped.
// if the device in the bound state, broadcast dose not stop automatically.
#define BLE_QIOT_BUTTON_BROADCAST 0
#if BLE_QIOT_BUTTON_BROADCAST
#define BLE_QIOT_BIND_TIMEOUT (2 * 60 * 1000) // unit: ms
#endif //BLE_QIOT_BUTTON_BROADCAST
// in some BLE stack the default ATT_MTU is 23, set BLE_QIOT_REMOTE_SET_MTU is 1 if you want to reset the mtu by the
// Tencent Lianlian. Tencent Lianlian will set the mtu get from function ble_get_user_data_mtu_size()
#define BLE_QIOT_REMOTE_SET_MTU (1)
// the following definition will affect the stack that LLSync usedthe minimum value tested is 2048 bytes
// the max length of llsync event data, depends on the length of user data reported to Tencent Lianlian at a time
#define BLE_QIOT_EVENT_MAX_SIZE (128)
// the minimum between BLE_QIOT_EVENT_MAX_SIZE and mtu
#define BLE_QIOT_EVENT_BUF_SIZE (23)
// some data like integer need to be transmitted in a certain byte order, defined it according to your device
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __ORDER_BIG_ENDIAN__ 4321
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
// in some BLE stack ble_qiot_log_hex() maybe not work, user can use there own hexdump function
#define BLE_QIOT_USER_DEFINE_HEXDUMP 0
#if BLE_QIOT_USER_DEFINE_HEXDUMP
// add your code here like this
// #define ble_qiot_log_hex(level, hex_name, data, data_len) \
// do { \
// MY_RAW_LOG("\r\nble qiot dump: %s, length: %d\r\n", hex_name, data_len); \
// MY_RAW_HEXDUMP_(data, data_len); \
// } while(0)
// or use your own hexdump function with same definition
// void ble_qiot_log_hex(e_ble_qiot_log_level level, const char *hex_name, const char *data, uint32_t data_len);
#endif // BLE_QIOT_USER_DEFINE_HEXDUMP
// Macro for logging a formatted string, the function must printf raw string without any color, prefix, newline or
// timestamp
#define BLE_QIOT_LOG_PRINT(...) printf(__VA_ARGS__)
#define BLE_QIOT_LLSYNC_STANDARD 1 // support llsync standard
#if BLE_QIOT_LLSYNC_STANDARD
// some users hope to confirm on the device before the binding, set BLE_QIOT_SECURE_BIND is 1 to enable the secure
// binding and enable secure bind in iot-explorer console. When the server is bound, the device callback ble_secure_bind_user_cb()
// will be triggered, the user agree or refuse connect by ble_secure_bind_user_confirm(). If the device does not respond
// and the connection timeout, or the user cancel the connection in Tencent Lianlian, a notify will received in function
// ble_secure_bind_user_notify().
#define BLE_QIOT_SECURE_BIND 0
#if BLE_QIOT_SECURE_BIND
#define BLE_QIOT_BIND_WAIT_TIME 60
#endif //BLE_QIOT_SECURE_BIND
// some sdk info needs to stored on the device and the address is up to you
#define BLE_QIOT_RECORD_FLASH_ADDR 5
// define user develop version, pick from "a-zA-Z0-9.-_" and length limits 132 bytes.
// must be consistent with the firmware version that user write in the iot-explorer console
// refer https://cloud.tencent.com/document/product/1081/40296
#define BLE_QIOT_USER_DEVELOPER_VERSION "0.0.1"
#define BLE_QIOT_SUPPORT_OTA 1 // 1 is support ota, others not
#if BLE_QIOT_SUPPORT_OTA
#define BLE_QIOT_SUPPORT_RESUMING 0 // 1 is support resuming, others not
#if BLE_QIOT_SUPPORT_RESUMING
// storage ota info in the flash if support resuming ota file
#define BLE_QIOT_OTA_INFO_FLASH_ADDR 10
#endif //BLE_QIOT_SUPPORT_RESUMING
#define BLE_QIOT_TOTAL_PACKAGES 0x12 // the total package numbers in a loop
#define BLE_QIOT_PACKAGE_LENGTH 0xb0 // the user data length in package, ble_get_user_data_mtu_size() - 3 is the max
#define BLE_QIOT_RETRY_TIMEOUT 0x20 // the max interval between two packages, unit: second
// the time spent for device reboot, the server waiting the device version reported after upgrade. unit: second
#define BLE_QIOT_REBOOT_TIME 20
#define BLE_QIOT_PACKAGE_INTERVAL 0xa // the interval between two packages send by the server
// the package from the server will storage in the buffer, write the buffer to the flash at one time when the buffer
// overflow. reduce the flash write can speed up file download, we suggest the BLE_QIOT_OTA_BUF_SIZE is multiples
// of BLE_QIOT_PACKAGE_LENGTH and equal flash page size
#define BLE_QIOT_OTA_BUF_SIZE (512 * 4)
#endif //BLE_QIOT_SUPPORT_OTA
#endif //BLE_QIOT_LLSYNC_STANDARD
#define BLE_QIOT_LLSYNC_CONFIG_NET (!BLE_QIOT_LLSYNC_STANDARD) // support llsync configure network
#if (1 == BLE_QIOT_LLSYNC_STANDARD) && (1 == BLE_QIOT_LLSYNC_CONFIG_NET)
#error "llsync standard and llsync configure network is incompatible"
#endif
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_CONFIG_H
@@ -0,0 +1,32 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2018-2020 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 QCLOUD_BLE_QIOT_CRC_H
#define QCLOUD_BLE_QIOT_CRC_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <printf.h>
#include <stdint.h>
void ble_qiot_crc32_init();
uint32_t ble_qiot_crc32(uint32_t crc, const uint8_t *buf, int len);
#if defined(__cplusplus)
}
#endif
#endif // QCLOUD_BLE_QIOT_HMAC_H
@@ -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 QCLOUD_BLE_QIOT_EXPORT_H
#define QCLOUD_BLE_QIOT_EXPORT_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ble_qiot_config.h"
#define TENCENT_COMPANY_IDENTIFIER 0xFEE7 // Tencent Company ID, another is 0xFEBA
#define IOT_BLE_UUID_BASE \
{ \
0xe2, 0xa4, 0x1b, 0x54, 0x93, 0xe4, 0x6a, 0xb5, 0x20, 0x4e, 0xd0, 0x65, 0x00, 0x00, 0x00, 0x00 \
}
// llsync services uuid
#if BLE_QIOT_LLSYNC_STANDARD
#define IOT_BLE_UUID_SERVICE 0xFFE0
#endif //BLE_QIOT_LLSYNC_STANDARD
#if BLE_QIOT_LLSYNC_CONFIG_NET
#define IOT_BLE_UUID_SERVICE 0xFFF0
#endif //BLE_QIOT_LLSYNC_CONFIG_NET
// characteristics uuid
#define IOT_BLE_UUID_DEVICE_INFO 0xFFE1 // used to connection and identity authentication
#define IOT_BLE_UUID_EVENT 0xFFE3 // used to send data to the server from device
#if BLE_QIOT_LLSYNC_STANDARD
#define IOT_BLE_UUID_DATA 0xFFE2 // used to send data to the device from server
#define IOT_BLE_UUID_OTA 0xFFE4 // used to send ota data to the device from server
#endif //BLE_QIOT_LLSYNC_STANDARD
typedef enum {
GATT_CHAR_BROADCAST = (1 << 0), // Broadcasting of the value permitted.
GATT_CHAR_READ = (1 << 1), // Reading the value permitted.
GATT_CHAR_WRITE_WO_RESP = (1 << 2), // Writing the value with Write Command permitted.
GATT_CHAR_WRITE = (1 << 3), // Writing the value with Write Request permitted.
GATT_CHAR_NOTIFY = (1 << 4), // Notification of the value permitted.
GATT_CHAR_INDICATE = (1 << 5), // Indications of the value permitted.
GATT_CHAR_AUTH_SIGNED_WR = (1 << 6), // Writing the value with Signed Write Command permitted.
} char_props_s;
// the callback function prototype definition for the characteristics
typedef void (*ble_on_write_cb)(const uint8_t *buf, uint16_t len);
// the characteristics attributes
typedef struct {
uint16_t uuid16;
uint8_t gatt_char_props;
ble_on_write_cb on_write;
} qiot_char_s;
// the service attributes
typedef struct {
uint16_t service_uuid16;
uint8_t service_uuid128[16];
uint16_t gatt_max_mtu;
qiot_char_s device_info;
qiot_char_s data;
qiot_char_s event;
qiot_char_s ota;
} qiot_service_init_s;
typedef enum {
BLE_QIOT_RS_OK = 0, // success
BLE_QIOT_RS_ERR = -1, // normal error
BLE_QIOT_RS_ERR_FLASH = -2, // flash error
BLE_QIOT_RS_ERR_PARA = -3, // parameters error
BLE_QIOT_RS_VALID_SIGN_ERR = -4,
} ble_qiot_ret_status_t;
/**
* @brief get llsync services context
*
* @return llsync services struct
*/
const qiot_service_init_s *ble_get_qiot_services(void);
/**
* @brief llsync sdck initialize
* @note you should called it before any other sdk api
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_qiot_explorer_init(void);
/**
* @brief report mtu of the device to the server
* @note report mtu to the server to set the mtu
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_report_device_info(void);
/**
* @brief sync the device mtu to The Tencent Lianlian.
* @note if BLE_QIOT_REMOTE_SET_MTU is 1, The Tencent Lianlian will set the mtu get from function
* ble_get_user_data_mtu_size(), but The Tencent Lianlian can not know the effective value even if the setting is
* successful, so llsync need sync the mtu again. The user call this function in the BLE mtu callback.
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_sync_mtu(uint16_t llsync_mtu);
/**
* @brief start llsync advertising
* @note broadcast data according to the device bind state, reference to llsync protocol
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_qiot_advertising_start(void);
/**
* @brief stop advertising
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_qiot_advertising_stop(void);
/**
* @brief device info write callbcak, call the function when characteristic IOT_BLE_UUID_DEVICE_INFO received data
* @param buf a pointer point to the data
* @param len data length
* @return none
*/
void ble_device_info_write_cb(const uint8_t *buf, uint16_t len);
/**
* @brief gap event connect call-back, when gap get ble connect event, use this function
* tell qiot ble sdk
* @return none
*/
void ble_gap_connect_cb(void);
/**
* @brief gap event disconnect call-back, when gap get ble disconnect event, use this function
* tell qiot ble sdk
* @return none
*/
void ble_gap_disconnect_cb(void);
#ifdef BLE_QIOT_LLSYNC_STANDARD
/**
* @brief get property of the device from the server
* @note the property will be received from IOT_BLE_UUID_DATA if success
* @return BLE_QIOT_RS_OK is success, other is error. if success, the data from server will come to
*/
ble_qiot_ret_status_t ble_event_get_status(void);
/**
* @brief report property of the device to the server
* @note the reply will be received from IOT_BLE_UUID_DATA if success
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_report_property(void);
/**
* @brief post event to the server
* @param event_id id of the event
* @note the reply will be received from IOT_BLE_UUID_DATA if success
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_post(uint8_t event_id);
/**
* @brief data write callback, call the function when characteristic IOT_BLE_UUID_DATA received data
* @param buf a pointer point to the data
* @param len data length
* @return none
*/
void ble_lldata_write_cb(const uint8_t *buf, uint16_t len);
/**
* @brief ota data write callback, call the function when characteristic IOT_BLE_UUID_OTA received data
* @param buf a pointer point to the data
* @param len data length
* @return none
*/
void ble_ota_write_cb(const uint8_t *buf, uint16_t len);
typedef enum {
BLE_QIOT_SECURE_BIND_CONFIRM = 0,
BLE_QIOT_SECURE_BIND_REJECT = 1,
} ble_qiot_secure_bind_t;
/**
* @brief user choose whether to connect
* @note call the function when the user choose connect or not
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_secure_bind_user_confirm(ble_qiot_secure_bind_t choose);
// inform user the ota start
typedef void (*ble_ota_start_callback)(void);
enum {
BLE_QIOT_OTA_SUCCESS = 0, // ota success
BLE_QIOT_OTA_ERR_CRC = 1, // ota failed because crc error
BLE_QIOT_OTA_ERR_TIMEOUT = 2, // ota failed because download timeout
BLE_QIOT_OTA_DISCONNECT = 3, // ota failed because ble disconnect
BLE_QIOT_OTA_ERR_FILE = 4, // ota failed because the file mismatch the device
};
// inform user the ota stop and the result
typedef void (*ble_ota_stop_callback)(uint8_t result);
// llsync only valid the file crc, also allow the user valid the file by their way
typedef ble_qiot_ret_status_t (*ble_ota_valid_file_callback)(uint32_t file_size, char *file_version);
/**
* @brief register ota callback
* @param start_cb called before ota start, set null if not used
* @param stop_cb called after ota stop, set null if not used
* @param valid_file_cb called after the crc valid, set null if not used
* @return none
*/
void ble_ota_callback_reg(ble_ota_start_callback start_cb, ble_ota_stop_callback stop_cb,
ble_ota_valid_file_callback valid_file_cb);
#endif //BLE_QIOT_LLSYNC_STANDARD
#if BLE_QIOT_LLSYNC_CONFIG_NET
typedef enum {
BLE_WIFI_MODE_NULL = 0, // invalid mode
BLE_WIFI_MODE_STA = 1, // station
BLE_WIFI_MODE_AP = 2, // ap
} BLE_WIFI_MODE;
typedef enum {
BLE_WIFI_STATE_CONNECT = 0, // wifi connect
BLE_WIFI_STATE_OTHER = 1, // other state
} BLE_WIFI_STATE;
/**
* @brief report wifi-mode setting result
* @param result setting result, 0 is success, other failed
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_report_wifi_mode(uint8_t result);
/**
* @brief report wifi-info setting result
* @param result setting result, 0 is success, other failed
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_report_wifi_info(uint8_t result);
/**
* @brief report wifi connection status
* @param mode current wifi mode, reference BLE_WIFI_MODE
* @param state current wifi state, reference BLE_WIFI_STATE
* @param ssid_len length of ssid
* @param ssid the ssid currently connected
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_report_wifi_connect(BLE_WIFI_MODE mode, BLE_WIFI_STATE state, uint8_t ssid_len,
const char *ssid);
/**
* @brief report wifi-token setting result
* @param result setting result, 0 is success, other failed
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_report_wifi_token(uint8_t result);
/**
* @brief report wifi-log
* @param log log message
* @param log_size length of log
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_event_report_wifi_log(const uint8_t *log, uint16_t log_size);
#endif //BLE_QIOT_LLSYNC_CONFIG_NET
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_EXPORT_H
@@ -0,0 +1,32 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2018-2020 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 QCLOUD_BLE_QIOT_HMAC_H
#define QCLOUD_BLE_QIOT_HMAC_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <string.h>
#define SHA1_DIGEST_SIZE 20
void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len);
#if defined(__cplusplus)
}
#endif
#endif // QCLOUD_BLE_QIOT_HMAC_H
@@ -0,0 +1,254 @@
/*
* 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 QCLOUD_BLE_QIOT_IMPORT_H
#define QCLOUD_BLE_QIOT_IMPORT_H
#if defined(__cplusplus)
extern "C" {
#endif
#include "ble_qiot_export.h"
// 16 bits service UUIDs list, use advertising type 0x02 or 0x03
typedef struct {
uint8_t uuid_num;
uint16_t *uuids;
} uuid_list_s;
// advertise manufacture specific data, use advertising type 0xFF
typedef struct {
uint16_t company_identifier;
uint8_t *adv_data;
uint8_t adv_data_len;
} manufacturer_data_s;
typedef struct {
uuid_list_s uuid_info;
manufacturer_data_s manufacturer_info;
} adv_info_s;
/**
* @brief get mac address
* @param mac the buf storage mac, 6 bytes permanent
* @return 0 is success, other is error
*/
int ble_get_mac(char *mac);
/**
* @brief add llsync services to ble stack
* @param qiot_service_init_s llsync service
* @return none
*/
void ble_services_add(const qiot_service_init_s *p_service);
/**
* @brief start llsync advertising
* @param adv a pointer point to advertising data
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_advertising_start(adv_info_s *adv);
/**
* @brief stop advertising
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_advertising_stop(void);
/**
* @brief get the ATT_MTU user want to used
* @return the value
*/
uint16_t ble_get_user_data_mtu_size(void);
/**
* @brief send a notification to host, use characteristic IOT_BLE_UUID_EVENT
* @param buf a pointer point to indication information
* @param len indication information length
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_send_notify(uint8_t *buf, uint8_t len);
// timer type
enum {
BLE_TIMER_ONE_SHOT_TYPE = 0,
BLE_TIMER_PERIOD_TYPE,
BLE_TIMER_BUTT,
};
typedef void *ble_timer_t;
// timer callback prototype
typedef void (*ble_timer_cb)(void *param);
/**
* @brief create a timer
* @param type timer type
* @param timeout_handle timer callback
* @return timer identifier is return, NULL is error
*/
ble_timer_t ble_timer_create(uint8_t type, ble_timer_cb timeout_handle);
/**
* @brief start a timer
* @param timer_id Timer identifier
* @param period timer period(unit: ms)
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_timer_start(ble_timer_t timer_id, uint32_t period);
/**
* @brief stop a timer
* @param timer_id Timer identifier
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_timer_stop(ble_timer_t timer_id);
/**
* @brief delete a timer
* @param timer_id Timer identifier
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_timer_delete(ble_timer_t timer_id);
#ifdef BLE_QIOT_LLSYNC_STANDARD
/**
* @brief get device product id
* @param product_id the buf storage product id, 10 bytes permanent
* @return 0 is success, other is error
*/
int ble_get_product_id(char *product_id);
/**
* @brief get device name
* @param device_name the buf storage device name, the max length of the device name is 48 bytes
* @return length of device name, 0 is error
*/
int ble_get_device_name(char *device_name);
/**
* @brief get device secret
* @param psk the buf storage secret, 24 bytes permanent
* @return 0 is success, other is error
*/
int ble_get_psk(char *psk);
/**
* @brief write data to flash
* @param flash_addr write address in flash
* @param write_buf point to write buf
* @param write_len length of data to write
* @return write_len is success, other is error
*/
int ble_write_flash(uint32_t flash_addr, const char *write_buf, uint16_t write_len);
/**
* @brief read data from flash
* @param flash_addr read address from flash
* @param read_buf point to read buf
* @param read_len length of data to read
* @return read_len is success, other is error
*/
int ble_read_flash(uint32_t flash_addr, char *read_buf, uint16_t read_len);
/**
* @brief secure binding user callback
* @note when the secure binding is activated, the function notify user the connecting is coming, the function is
* no-block
* @return None
*/
void ble_secure_bind_user_cb(void);
enum {
BLE_SECURE_BIND_CANCEL = 0, // user cancel in Tencent Lianlian
BLE_SECURE_BIND_TIMEOUT = 1, // the binding timeout
};
/**
* @brief secure binding user notify
* @note notify user when the secure bind timeout or cancel
* @return None
*/
void ble_secure_bind_user_notify(uint8_t result);
enum {
BLE_OTA_ENABLE = 1, // ota enable
BLE_OTA_DISABLE_LOW_POWER = 2, // the device low power can not upgrade
BLE_OTA_DISABLE_LOW_VERSION = 3, // disable upgrade low version
};
/**
* @brief get ota is enable
* @param version the ota file version
* @return BLE_OTA_ENABLE is enable, others disable
* @note can not be blocking, the reason defined by users
*/
uint8_t ble_ota_is_enable(const char *version, u32 file_size, u32 file_crc);
/**
* @brief get the address the ota file will be saved
* @return the flash address
*/
uint32_t ble_ota_get_download_addr(void);
/**
* @brief write data to flash
* @param flash_addr write address in flash
* @param write_buf point to write buf
* @param write_len length of data to write
* @return write_len is success, other is error
*/
int ble_ota_write_flash(uint32_t flash_addr, const char *write_buf, uint16_t write_len);
#endif //BLE_QIOT_LLSYNC_STANDARD
#if BLE_QIOT_LLSYNC_CONFIG_NET
/**
* @brief set wifi mode
* @param mode the target mode
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_combo_wifi_mode_set(BLE_WIFI_MODE mode);
/**
* @brief set wifi info
* @param ssid wifi ssid
* @param ssid_len the length of ssid
* @param passwd wifi password
* @param passwd_len the length of password
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_combo_wifi_info_set(const char *ssid, uint8_t ssid_len, const char *passwd, uint8_t passwd_len);
/**
* @brief connect wifi
* @param none
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_combo_wifi_connect();
/**
* @brief set wifi token
* @param token token message
* @param token_len length of token
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_combo_wifi_token_set(const char *token, uint16_t token_len);
/**
* @brief get wifi log
* @param none
* @return BLE_QIOT_RS_OK is success, other is error
*/
ble_qiot_ret_status_t ble_combo_wifi_log_get(void);
#endif //BLE_QIOT_LLSYNC_CONFIG_NET
#if defined(__cplusplus)
}
#endif
#endif // QCLOUD_BLE_QIOT_IMPORT_H
@@ -0,0 +1,44 @@
/*
* 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 QCLOUD_BLE_QIOT_LLSYNC_DATA_H
#define QCLOUD_BLE_QIOT_LLSYNC_DATA_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <ble_qiot_export.h>
#define BLE_QIOT_CONTROL_DATA_TYPE (0x00)
#define BLE_QIOT_GET_STATUS_REPLY_DATA_TYPE (0x22)
#define BLE_QIOT_GET_STATUS_REPLY_HEADER_LEN (4)
#define BLE_QIOT_DATA_FIXED_HEADER_LEN (3)
// handle property request
ble_qiot_ret_status_t ble_lldata_property_request_handle(const char *in_buf, int buf_len);
// handle property reply
ble_qiot_ret_status_t ble_lldata_property_reply_handle(uint8_t type, const char *in_buf, int buf_len);
// handle event data
ble_qiot_ret_status_t ble_lldata_event_handle(uint8_t id, const char *in_buf, int len);
// handle action data
ble_qiot_ret_status_t ble_lldata_action_handle(uint8_t id, const char *in_buf, int len);
// get report data
ble_qiot_ret_status_t ble_user_property_get_report_data(void);
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_LLSYNC_DATA_H
@@ -0,0 +1,173 @@
/*
* 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 QCLOUD_BLE_QIOT_LLSYNC_DEVICE_H
#define QCLOUD_BLE_QIOT_LLSYNC_DEVICE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
//#include <stdbool.h>
#include "ble_qiot_common.h"
#include "ble_qiot_export.h"
#include "ble_qiot_hmac.h"
#include "ble_qiot_llsync_event.h"
#define LLSYNC_BIND_STATE_MASK 0x03
#define LLSYNC_PROTO_VER_BIT 0x04
#define LLSYNC_PROTOCOL_VERSION_MASK 0xF0
#define LLSYNC_MTU_SET_MASK 0x8000
#define LLSYNC_MTU_SET_RESULT_ERR 0xFFFF // some error when setting mtu
#define BLE_QIOT_LLSYNC_PROTOCOL_VERSION (2) // llsync protocol version, equal or less than 15
#define ATT_DEFAULT_MTU 23 // default att mtu
#define ATT_MTU_TO_LLSYNC_MTU(_att_mtu) ((_att_mtu)-3)
#define BLE_LOCAL_PSK_LEN 4
#define BLE_BIND_IDENTIFY_STR_LEN 8
#define BLE_EXPIRATION_TIME 60 // timestamp expiration value
#define BLE_UNBIND_REQUEST_STR "UnbindRequest"
#define BLE_UNBIND_REQUEST_STR_LEN (sizeof("UnbindRequest") - 1)
#define BLE_UNBIND_RESPONSE "UnbindResponse"
#define BLE_UNBIND_RESPONSE_STR_LEN (sizeof("UnbindResponse") - 1)
typedef enum {
E_DEV_MSG_SYNC_TIME = 0, // sync info before bind
E_DEV_MSG_CONN_VALID, // connect request
E_DEV_MSG_BIND_SUCC, // inform bind success
E_DEV_MSG_BIND_FAIL, // inform bind failed
E_DEV_MSG_UNBIND, // unbind request
E_DEV_MSG_CONN_SUCC, // inform connect result
E_DEV_MSG_CONN_FAIL,
E_DEV_MSG_UNBIND_SUCC, // inform unbind result
E_DEV_MSG_UNBIND_FAIL,
E_DEV_MSG_SET_MTU_RESULT, // inform set mtu result
E_DEV_MSG_BIND_TIMEOUT, // inform bind timeout
E_DEV_MSG_GET_DEV_INFO = 0xE0, // configure network start
E_DEV_MSG_SET_WIFI_MODE,
E_DEV_MSG_SET_WIFI_INFO,
E_DEV_MSG_SET_WIFI_CONNECT,
E_DEV_MSG_SET_WIFI_TOKEN,
E_DEV_MSG_GET_DEV_LOG,
E_DEV_MSG_MSG_BUTT,
} e_dev_info_msg_type;
typedef enum {
E_LLSYNC_BIND_IDLE = 0, // no bind
E_LLSYNC_BIND_WAIT, // wait bind, return idle state if no bind in the period
E_LLSYNC_BIND_SUCC, // bound
} e_llsync_bind_state;
typedef enum {
E_LLSYNC_DISCONNECTED = 0,
E_LLSYNC_CONNECTED,
} e_llsync_connection_state;
typedef enum {
E_BLE_DISCONNECTED = 0,
E_BLE_CONNECTED,
} e_ble_connection_state;
typedef struct ble_device_info_t_ {
char product_id[BLE_QIOT_PRODUCT_ID_LEN];
char device_name[BLE_QIOT_DEVICE_NAME_LEN + 1];
char psk[BLE_QIOT_PSK_LEN];
char mac[BLE_QIOT_MAC_LEN];
} ble_device_info;
typedef struct ble_core_data_ {
uint8_t bind_state;
char local_psk[BLE_LOCAL_PSK_LEN];
char identify_str[BLE_BIND_IDENTIFY_STR_LEN];
} ble_core_data;
// write to uuid FEE1 before bind
typedef struct ble_bind_data_t_ {
int nonce;
int timestamp;
} ble_bind_data;
// connect data struct
typedef struct ble_conn_data_t_ {
int timestamp;
char sign_info[SHA1_DIGEST_SIZE];
} ble_conn_data;
// unbind data struct
typedef struct ble_unbind_data_t_ {
char sign_info[SHA1_DIGEST_SIZE];
} ble_unbind_data;
typedef struct {
bool have_data; // start received package
uint8_t type; // event type
uint16_t buf_len; // the length of data
char buf[BLE_QIOT_EVENT_MAX_SIZE];
} ble_event_slice_t;
// read sdk data from flash
ble_qiot_ret_status_t ble_init_flash_data(void);
// set llsync bind state
void llsync_bind_state_set(e_llsync_bind_state new_state);
// get llsync bind state
e_llsync_bind_state llsync_bind_state_get(void);
// set llsync connection state
void llsync_connection_state_set(e_llsync_connection_state new_state);
// set ble connection state
void ble_connection_state_set(e_ble_connection_state new_state);
// get llsync connection state
bool llsync_is_connected(void);
// get ble connection state
bool ble_is_connected(void);
// get broadcast data
int ble_get_my_broadcast_data(char *out_buf, int buf_len);
// get bind authcode, return authcode length;
// out_buf length must greater than SHA1_DIGEST_SIZE + BLE_QIOT_DEVICE_NAME_LEN
int ble_bind_get_authcode(const char *bind_data, uint16_t data_len, char *out_buf, uint16_t buf_len);
// write bind result to flash, return 0 is success
ble_qiot_ret_status_t ble_bind_write_result(const char *result, uint16_t len);
// write unbind result to flash, return 0 is success
ble_qiot_ret_status_t ble_unbind_write_result(void);
// get connect authcode, return authcode length;
// out_buf length must greater than SHA1_DIGEST_SIZE + BLE_QIOT_DEVICE_NAME_LEN
int ble_conn_get_authcode(const char *conn_data, uint16_t data_len, char *out_buf, uint16_t buf_len);
// get connect authcode, return authcode length;
// out_buf length must greater than SHA1_DIGEST_SIZE + BLE_UNBIND_RESPONSE_STR_LEN
int ble_unbind_get_authcode(const char *unbind_data, uint16_t data_len, char *out_buf, uint16_t buf_len);
// inform device the result of mtu setting
int ble_inform_mtu_result(const char *result, uint16_t data_len);
// get llsync mtu
uint16_t llsync_mtu_get(void);
// update llsync mtu
void llsync_mtu_update(uint16_t sync_mtu);
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_LLSYNC_DEVICE_H
@@ -0,0 +1,56 @@
/*
* 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 QCLOUD_BLE_QIOT_LLSYNC_EVENT_H
#define QCLOUD_BLE_QIOT_LLSYNC_EVENT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "ble_qiot_config.h"
#include "ble_qiot_export.h"
enum {
BLE_QIOT_EVENT_NO_SLICE = 0,
BLE_QIOT_EVENT_SLICE_HEAD = 1,
BLE_QIOT_EVENT_SLICE_BODY = 2,
BLE_QIOT_EVENT_SLICE_FOOT = 3,
};
// 1 byte type + 2 bytes payload-length
#define BLE_QIOT_EVENT_FIXED_HEADER_LEN (3)
// the bit 15 - 14 is slice flag, bit 13 - 0 is tlv length
#define BLE_QIOT_IS_SLICE_PACKAGE(_C) ((_C)&0XC0)
#define BLE_QIOT_IS_SLICE_HEADER(_C) (((_C)&0XC0) == 0X40)
#define BLE_QIOT_IS_SLICE_BODY(_C) (((_C)&0XC0) == 0X80)
#define BLE_QIOT_IS_SLICE_TAIL(_C) (((_C)&0XC0) == 0XC0)
#define BLE_QIOT_STRING_TYPE_LEN 2 // string/struct type length
#define BLE_QIOT_MIN_STRING_TYPE_LEN (BLE_QIOT_STRING_TYPE_LEN + 1) // at least 2 bytes length and 1 byte payload
#define BLE_QIOT_NOT_SUPPORT_WARN " not support, please check the data template"
ble_qiot_ret_status_t ble_event_notify(uint8_t type, uint8_t *header, uint8_t header_len, const char *buf,
uint16_t buf_len);
ble_qiot_ret_status_t ble_event_notify2(uint8_t type, uint8_t length_flag, uint8_t *header, uint8_t header_len,
const char *buf, uint16_t buf_len);
ble_qiot_ret_status_t ble_event_sync_wait_time(void);
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_LLSYNC_EVENT_H
@@ -0,0 +1,124 @@
/*
* 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 QCLOUD_BLE_QIOT_LLSYNC_OTA_H
#define QCLOUD_BLE_QIOT_LLSYNC_OTA_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "ble_qiot_config.h"
#include "ble_qiot_export.h"
#define BLE_QIOT_GET_OTA_REQUEST_HEADER_LEN 3 // the ota request header len
#define BLE_QIOT_OTA_DATA_HEADER_LEN 3 // the ota data header len
// ota feature
#define BLE_QIOT_OTA_ENABLE (1 << 0)
#define BLE_QIOT_OTA_RESUME_ENABLE (1 << 1)
// ota file valid result
#define BLE_QIOT_OTA_VALID_SUCCESS (1 << 7)
#define BLE_QIOT_OTA_VALID_FAIL (0 << 7)
#define BLE_QIOT_OTA_MAX_VERSION_STR (32) // max ota version length
#define BLE_QIOT_OTA_PAGE_VALID_VAL 0x5A // ota info valid flag
#define BLE_QIOT_OTA_FIRST_TIMEOUT (1)
#define BLE_QIOT_OTA_MAX_RETRY_COUNT 5 // disconnect if retry times more than BLE_QIOT_OTA_MAX_RETRY_COUNT
// ota control bits
#define BLE_QIOT_OTA_REQUEST_BIT (1 << 0)
#define BLE_QIOT_OTA_RECV_END_BIT (1 << 1)
#define BLE_QIOT_OTA_RECV_DATA_BIT (1 << 2)
#define BLE_QIOT_OTA_FIRST_RETRY_BIT (1 << 3)
#define BLE_QIOT_OTA_DO_VALID_BIT (1 << 4)
#define BLE_QIOT_OTA_HAVE_DATA_BIT (1 << 5)
// the reason of ota file error
enum {
BLE_QIOT_OTA_CRC_ERROR = 0,
BLE_QIOT_OTA_READ_FLASH_ERROR = 1,
BLE_QIOT_OTA_FILE_ERROR = 2,
};
// ota data type
enum {
BLE_QIOT_OTA_MSG_REQUEST = 0,
BLE_QIOT_OTA_MSG_DATA = 1,
BLE_QIOT_OTA_MSG_END = 2,
BLE_QIOT_OTA_MSG_BUTT,
};
// ota request reply
typedef struct ble_ota_reply_info_ {
uint8_t package_nums; // package numbers in a loop
uint8_t package_size; // each package size
uint8_t retry_timeout; // data retry
uint8_t reboot_timeout; // max time of device reboot
uint32_t last_file_size; // the file already received
uint8_t package_interval; // package send interval on the server
uint8_t rsv[3];
} ble_ota_reply_info;
typedef struct ble_ota_file_info_ {
uint32_t file_size;
uint32_t file_crc;
uint8_t file_version[BLE_QIOT_OTA_MAX_VERSION_STR];
} ble_ota_file_info;
// ota info saved in flash if support resuming
typedef struct ble_ota_info_record_ {
uint8_t valid_flag;
uint8_t rsv[3];
uint32_t last_file_size; // the file size already write in flash
uint32_t last_address; // the address file saved
ble_ota_file_info download_file_info;
} ble_ota_info_record;
// ota user callback
typedef struct ble_ota_user_callback_ {
ble_ota_start_callback start_cb;
ble_ota_stop_callback stop_cb;
ble_ota_valid_file_callback valid_file_cb;
} ble_ota_user_callback;
// ota request package: 1 byte type + 2 bytes length + 4 bytes size + 4 bytes crc + 1 byte version length + 1 ~ 32 bytes
// version
typedef struct {
bool have_data; // start received package
uint8_t type; // event type
uint16_t buf_len; // the length of data
char buf[BLE_QIOT_GET_OTA_REQUEST_HEADER_LEN + 4 + 4 + 1 + BLE_QIOT_OTA_MAX_VERSION_STR];
} ble_ota_request_slice_t;
// ota reply info
typedef struct ble_ota_reply_t_ {
uint32_t file_size;
uint8_t req;
} ble_ota_reply_t;
ble_qiot_ret_status_t ble_ota_request_handle(const char *in_buf, int buf_len);
ble_qiot_ret_status_t ble_ota_data_handle(const char *in_buf, int buf_len);
ble_qiot_ret_status_t ble_ota_file_end_handle(void);
void ble_ota_stop(void);
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_LLSYNC_OTA_H
@@ -0,0 +1,103 @@
/*
* 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 QCLOUD_BLE_QIOT_LOG_H
#define QCLOUD_BLE_QIOT_LOG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ble_qiot_config.h"
typedef enum {
BLE_QIOT_LOG_LEVEL_NONE = 0,
BLE_QIOT_LOG_LEVEL_ERR,
BLE_QIOT_LOG_LEVEL_WARN,
BLE_QIOT_LOG_LEVEL_INFO,
BLE_QIOT_LOG_LEVEL_DEBUG,
BLE_QIOT_LOG_LEVEL_ALL,
} e_ble_qiot_log_level;
// log new line feed type
#define LINE_NONE
#define LINE_LF "\n"
#define LINE_CR "\r"
#define LINE_CRLF "\r\n"
// log new line feed type config
#define LOG_LINE_FEED_TYPE LINE_CRLF
extern e_ble_qiot_log_level g_log_level;
#ifndef ble_qiot_log_d
#define ble_qiot_log_d(fmt, args...) \
do { \
if (g_log_level < BLE_QIOT_LOG_LEVEL_DEBUG) \
break; \
BLE_QIOT_LOG_PRINT("qiot debug: " fmt LOG_LINE_FEED_TYPE, ##args); \
} while (0)
#endif
#ifndef ble_qiot_log_i
#define ble_qiot_log_i(fmt, args...) \
do { \
if (g_log_level < BLE_QIOT_LOG_LEVEL_INFO) \
break; \
BLE_QIOT_LOG_PRINT("qiot info: " fmt LOG_LINE_FEED_TYPE, ##args); \
} while (0)
#endif
#ifndef ble_qiot_log_w
#define ble_qiot_log_w(fmt, args...) \
do { \
if (g_log_level < BLE_QIOT_LOG_LEVEL_WARN) \
break; \
BLE_QIOT_LOG_PRINT("qiot warn(%s|%d): " fmt LOG_LINE_FEED_TYPE, __FILE__, __LINE__, ##args); \
} while (0)
#endif
#ifndef ble_qiot_log_e
#define ble_qiot_log_e(fmt, args...) \
do { \
if (g_log_level < BLE_QIOT_LOG_LEVEL_ERR) \
break; \
BLE_QIOT_LOG_PRINT("qiot err(%s|%d): " fmt LOG_LINE_FEED_TYPE, __FILE__, __LINE__, ##args); \
} while (0)
#endif
#ifndef ble_qiot_log
#define ble_qiot_log(level, fmt, args...) \
do { \
if (g_log_level < level) \
break; \
BLE_QIOT_LOG_PRINT("qiot log(%s|%d): " fmt LOG_LINE_FEED_TYPE, __FILE__, __LINE__, ##args); \
} while (0)
#endif
// this function only use for ble_qiot_log_hex
#ifndef ble_qiot_log_raw
#define ble_qiot_log_raw(fmt, args...) \
do { \
BLE_QIOT_LOG_PRINT(fmt, ##args); \
} while (0)
#endif
void ble_qiot_set_log_level(e_ble_qiot_log_level level);
#if !BLE_QIOT_USER_DEFINE_HEXDUMP
void ble_qiot_log_hex(e_ble_qiot_log_level level, const char *hex_name, const char *data, uint32_t data_len);
#endif // BLE_QIOT_USER_DEFINE_HEXDUMP
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_QIOT_LOG_H
@@ -0,0 +1,94 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2018-2020 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 QCLOUD_BLE_QIOT_MD5_H
#define QCLOUD_BLE_QIOT_MD5_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MD5_DIGEST_SIZE 16
typedef struct {
unsigned int total[2]; /*!< number of bytes processed */
unsigned int state[4]; /*!< intermediate digest state */
unsigned char buffer[64]; /*!< data block being processed */
} iot_md5_context;
/**
* @brief init MD5 context
*
* @param ctx MD5 context
*/
void utils_md5_init(iot_md5_context *ctx);
/**
* @brief free MD5 context
*
* @param ctx MD5 context
*/
void utils_md5_free(iot_md5_context *ctx);
/**
* @brief clone MD5 context
*
* @param dst destination MD5 context
* @param src source MD5 context
*/
void utils_md5_clone(iot_md5_context *dst, const iot_md5_context *src);
/**
* @brief start MD5 calculation
*
* @param ctx MD5 context
*/
void utils_md5_starts(iot_md5_context *ctx);
/**
* @brief MD5 update
*
* @param ctx MD5 context
* @param input input data
* @param ilen data length
*/
void utils_md5_update(iot_md5_context *ctx, const unsigned char *input, unsigned int ilen);
/**
* @brief finish MD5 calculation
*
* @param ctx MD5 context
* @param output MD5 result
*/
void utils_md5_finish(iot_md5_context *ctx, unsigned char output[16]);
/* MD5 internal process */
void utils_md5_process(iot_md5_context *ctx, const unsigned char data[64]);
/**
* @brief Output = MD5( input buffer )
*
* @param input data input
* @param ilen data lenght
* @param output MD5 result
*/
void utils_md5(const unsigned char *input, unsigned int ilen, unsigned char output[16]);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,93 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2018-2020 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 QCLOUD_BLE_IOT_PARAM_CHECK_H_
#define QCLOUD_BLE_IOT_PARAM_CHECK_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "ble_qiot_log.h"
#define NUMBERIC_SANITY_CHECK(num, err) \
do { \
if (0 == (num)) { \
ble_qiot_log_e("Invalid argument, numeric 0"); \
return (err); \
} \
} while (0)
#define NUMBERIC_SANITY_CHECK_RTN(num) \
do { \
if (0 == (num)) { \
ble_qiot_log_e("Invalid argument, numeric 0"); \
return; \
} \
} while (0)
#define BUFF_LEN_SANITY_CHECK(num, min, err) \
do { \
if ((min) > (num)) { \
ble_qiot_log_e("Invalid argument, %d <= %d", min, num); \
return (err); \
} \
} while (0)
#define POINTER_SANITY_CHECK(ptr, err) \
do { \
if (NULL == (ptr)) { \
ble_qiot_log_e("Invalid argument, %s = %p", #ptr, ptr); \
return (err); \
} \
} while (0)
#define POINTER_SANITY_CHECK_RTN(ptr) \
do { \
if (NULL == (ptr)) { \
ble_qiot_log_e("Invalid argument, %s = %p", #ptr, ptr); \
return; \
} \
} while (0)
#define STRING_PTR_SANITY_CHECK(ptr, err) \
do { \
if (NULL == (ptr)) { \
ble_qiot_log_e("Invalid argument, %s = %p", #ptr, (ptr)); \
return (err); \
} \
if (0 == strlen((ptr))) { \
ble_qiot_log_e("Invalid argument, %s = '%s'", #ptr, (ptr)); \
return (err); \
} \
} while (0)
#define STRING_PTR_SANITY_CHECK_RTN(ptr) \
do { \
if (NULL == (ptr)) { \
ble_qiot_log_e("Invalid argument, %s = %p", #ptr, (ptr)); \
return; \
} \
if (0 == strlen((ptr))) { \
ble_qiot_log_e("Invalid argument, %s = '%s'", #ptr, (ptr)); \
return; \
} \
} while (0)
#if defined(__cplusplus)
}
#endif
#endif /* QCLOUD_BLE_IOT_PARAM_CHECK_H_ */
@@ -0,0 +1,97 @@
/*
* 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 QCLOUD_BLE_IOT_SERVICE_H_
#define QCLOUD_BLE_IOT_SERVICE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "ble_qiot_export.h"
#include "ble_qiot_llsync_data.h"
#include "ble_qiot_template.h"
// 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 reply result
enum {
BLE_QIOT_REPLY_SUCCESS = 0,
BLE_QIOT_REPLY_FAIL,
BLE_QIOT_REPLY_DATA_ERR,
BLE_QIOT_REPLY_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_REPLY_OTA_REPORT,
BLE_QIOT_EVENT_UP_REPLY_OTA_DATA,
BLE_QIOT_EVENT_UP_REPORT_CHECK_RESULT,
BLE_QIOT_EVENT_UP_SYNC_MTU,
BLE_QIOT_EVENT_UP_SYNC_WAIT_TIME,
BLE_QIOT_EVENT_UP_WIFI_MODE = 0xE0,
BLE_QIOT_EVENT_UP_WIFI_INFO,
BLE_QIOT_EVENT_UP_WIFI_CONNECT,
BLE_QIOT_EVENT_UP_WIFI_TOKEN,
BLE_QIOT_EVENT_UP_WIFI_LOG,
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)
// 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)
// handle llsync device info
// return 0 is success, other is error
int ble_device_info_msg_handle(const char *in_buf, int in_len);
// lldata message from remote
// return 0 is success, other is error
int ble_lldata_msg_handle(const char *in_buf, int in_len);
// ota message from remote
// return 0 is success, other is error
int ble_ota_msg_handle(const char *buf, uint16_t len);
#ifdef __cplusplus
}
#endif
#endif // QCLOUD_BLE_IOT_SERVICE_H_
@@ -0,0 +1,93 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2018-2020 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 QCLOUD_BLE_LLSYNC_BLE_QIOT_SHA1_H
#define QCLOUD_BLE_LLSYNC_BLE_QIOT_SHA1_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
/**
* \brief SHA-1 context structure
*/
typedef struct {
uint32_t total[2]; /*!< number of bytes processed */
uint32_t state[5]; /*!< intermediate digest state */
unsigned char buffer[64]; /*!< data block being processed */
} iot_sha1_context;
/**
* \brief Initialize SHA-1 context
*
* \param ctx SHA-1 context to be initialized
*/
void utils_sha1_init(iot_sha1_context *ctx);
/**
* \brief Clear SHA-1 context
*
* \param ctx SHA-1 context to be cleared
*/
void utils_sha1_free(iot_sha1_context *ctx);
/**
* \brief Clone (the state of) a SHA-1 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void utils_sha1_clone(iot_sha1_context *dst, const iot_sha1_context *src);
/**
* \brief SHA-1 context setup
*
* \param ctx context to be initialized
*/
void utils_sha1_starts(iot_sha1_context *ctx);
/**
* \brief SHA-1 process buffer
*
* \param ctx SHA-1 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void utils_sha1_update(iot_sha1_context *ctx, const unsigned char *input, size_t ilen);
/**
* \brief SHA-1 final digest
*
* \param ctx SHA-1 context
* \param output SHA-1 checksum result
*/
void utils_sha1_finish(iot_sha1_context *ctx, unsigned char output[20]);
/* Internal use */
void utils_sha1_process(iot_sha1_context *ctx, const unsigned char data[64]);
/**
* \brief Output = SHA-1( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-1 checksum result
*/
void utils_sha1(const unsigned char *input, size_t ilen, unsigned char output[20]);
#endif // QCLOUD_BLE_LLSYNC_BLE_QIOT_SHA1_H
@@ -0,0 +1,119 @@
/*
* 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>
//#include <stdbool.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_STRUCT,
BLE_QIOT_DATA_TYPE_BUTT,
};
// message type, reference data template
enum {
BLE_QIOT_PROPERTY_AUTH_RW = 0,
BLE_QIOT_PROPERTY_AUTH_READ,
BLE_QIOT_PROPERTY_AUTH_BUTT,
};
// define message flow direction
enum {
BLE_QIOT_EFFECT_REQUEST = 0,
BLE_QIOT_EFFECT_REPLY,
BLE_QIOT_EFFECT_BUTT,
};
#define BLE_QIOT_PACKAGE_MSG_HEAD(_TYPE, _REPLY, _ID) (((_TYPE) << 6) | (((_REPLY) == BLE_QIOT_EFFECT_REPLY) << 5) | ((_ID) & 0X1F))
#define BLE_QIOT_PACKAGE_TLV_HEAD(_TYPE, _ID) (((_TYPE) << 5) | ((_ID) & 0X1F))
// define tlv struct
typedef struct {
uint8_t type;
uint8_t id;
uint16_t len;
char *val;
} e_ble_tlv;
#define BLE_QIOT_INCLUDE_PROPERTY
// define property id
enum {
BLE_QIOT_PROPERTY_ID_POWER_SWITCH = 0,
BLE_QIOT_PROPERTY_ID_BUTT,
};
// 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;
// property module
#ifdef BLE_QIOT_INCLUDE_PROPERTY
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);
int ble_lldata_parse_tlv(const char *buf, int buf_len, e_ble_tlv *tlv);
int ble_user_property_struct_handle(const char *in_buf, uint16_t buf_len, ble_property_t *struct_arr, uint8_t arr_size);
int ble_user_property_struct_get_data(char *in_buf, uint16_t buf_len, ble_property_t *struct_arr, uint8_t arr_size);
void ll_sync_led_switch(void);
void ll_sync_unbind(void);
void llsync_device_state_sync(void);
#endif
// event module
#ifdef BLE_QIOT_INCLUDE_EVENT
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);
#endif
// action module
#ifdef BLE_QIOT_INCLUDE_ACTION
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);
#endif
#ifdef __cplusplus
}
#endif
#endif //BLE_QIOT_TEMPLATE_H_
@@ -0,0 +1,36 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2018-2020 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 QCLOUD_BLE_IOT_UTILS_BASE64_H_
#define QCLOUD_BLE_IOT_UTILS_BASE64_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdint.h>
#include "ble_qiot_export.h"
ble_qiot_ret_status_t qcloud_iot_utils_base64encode(unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen);
ble_qiot_ret_status_t qcloud_iot_utils_base64decode(unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen);
#ifdef __cplusplus
}
#endif
#endif /* QCLOUD_BLE_IOT_UTILS_BASE64_H_ */