• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: 蓝牙SDK 功能函数头文件(此文件为DEMO,需集成方适配修改)
15  */
16 #ifndef HILINK_BT_FUNCTION_H
17 #define HILINK_BT_FUNCTION_H
18 
19 #include "hilink_bt_api.h"
20 #include "ble_cfg_net_api.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* 广播最大长度 */
27 #define ADV_VALUE_MAX_LEN 31
28 #define HIBEACON_IV_NONCE_LEN 8
29 #define HIBEACON_PSK_LEN 16
30 
31 typedef enum {
32     HILINK_BT_SDK_STATUS_SVC_RUNNING = 0, /* 正常运行 */
33     HILINK_BT_SDK_STATUS_DEINIT, /* 注销 */
34     HILINK_BT_SDK_STATUS_NAME_SET_ABNORM, /* 蓝牙名称设置异常 */
35     HILINK_BT_SDK_STATUS_DISCOVER_MODE_SET_ABNORM, /* 蓝牙可发现模式设置异常 */
36     HILINK_BT_SDK_STATUS_REG_APP_ABNORM, /* 注册BLE应用异常 */
37     HILINK_BT_SDK_STATUS_SVC_CREATE_ABNORM, /* 服务创建异常 */
38     HILINK_BT_SDK_STATUS_CHAR_ADD_ABNORM, /* 属性添加异常 */
39     HILINK_BT_SDK_STATUS_DESC_ADD_ABNORM, /* 描述添加异常 */
40     HILINK_BT_SDK_STATUS_SVC_START_ABNORM, /* 服务启动异常 */
41     HILINK_BT_SDK_STATUS_ADV_PARA_SET_ABNORM, /* 广播参数设置异常 */
42     HILINK_BT_SDK_STATUS_ADV_DATA_SET_ABNORM, /* 广播数据设置异常 */
43     HILINK_BT_SDK_STATUS_ADV_START_ABNORM, /* 广播启动异常 */
44 } HILINK_BT_SdkStatus;
45 
46 /* GATTS char属性取值 */
47 typedef enum {
48     HILINK_BT_CHAR_PROP_WRITE_WITHOUT_RESP = 0x04,
49     HILINK_BT_CHAR_PROP_WRITE = 0x08,
50     HILINK_BT_CHAR_PROP_READ = 0x02,
51     HILINK_BT_CHAR_PROP_NOTIFY = 0x10,
52     HILINK_BT_CHAR_PROP_INDICATE = 0x20
53 } HILINK_BT_CharProperty;
54 
55 /* GATTS char权限取值 */
56 typedef enum {
57     HILINK_BT_CHAR_PERM_READ  = 0x01,
58     HILINK_BT_CHAR_PERM_READ_ENCRYPTED = 0x02,
59     HILINK_BT_CHAR_PERM_READ_ENCRYPTED_MITM = 0x04,
60     HILINK_BT_CHAR_PERM_WRITE = 0x10,
61     HILINK_BT_CHAR_PERM_WRITE_ENCRYPTED = 0x20,
62     HILINK_BT_CHAR_PERM_WRITE_ENCRYPTED_MITM = 0x40,
63     HILINK_BT_CHAR_PERM_WRITE_SIGNED = 0x80,
64     HILINK_BT_CHAR_PERM_WRITE_SIGNED_MITM = 0x100,
65 } HILINK_BT_CharPermission;
66 
67 /* GATTS desc属性取值 */
68 typedef enum {
69     HILINK_BT_DESC_PERM_WRITE = 0x01,
70     HILINK_BT_DESC_PERM_READ = 0x02
71 } HILINK_BT_DescPermission;
72 
73 /* 属性值类型: 整型和属性 */
74 typedef enum {
75     HILINK_BT_CMD_DATA_TYPE_INT,
76     HILINK_BT_CMD_DATA_TYPE_STR,
77 } HILINK_BT_CmdDataType;
78 
79 /* hilink蓝牙应用层数据编码类型 */
80 typedef enum {
81     HILINK_BT_CMD_DATA_MODE_TLV = 0x00, /* TLV格式: 降低报文占用的空间 */
82     HILINK_BT_CMD_DATA_MODE_JSON = 0x01 /* JSON格式: 扩展性更好,默认格式 */
83 } HILINK_BT_CmdDataMode;
84 
85 /* 发送蓝牙SDK状态回调函数类型 */
86 typedef void (*HILINK_BT_SdkEventCallBack)(HILINK_BT_SdkStatus event, const void *param);
87 
88 /* 自定义gatt服务读事件回调 */
89 typedef int (*HILINK_BT_GattReadCallback)(unsigned char *out, unsigned int *outLen);
90 
91 /* 自定义gatt服务写事件回调 */
92 typedef int (*HILINK_BT_GattWriteCallback)(const unsigned char *in, unsigned int inLen);
93 
94 /* 产品功能命令定义结构体 */
95 typedef struct {
96     unsigned char attrIdx;
97     char *attr;
98     HILINK_BT_CmdDataType dataType;
99     int (*putFunc)(const void *data, unsigned int len);
100     int (*getFunc)(void *buf, unsigned int *bufLen, unsigned int len);
101 } HILINK_BT_AttrInfo;
102 
103 /* 产品功能定义结构体 */
104 typedef struct {
105     unsigned char svcIdx;
106     char *service;
107     int (*putFunc)(const void *svc, const unsigned char *in, unsigned int inLen,
108         unsigned char **out, unsigned int *outLen);
109     int (*getFunc)(const void *svc, const unsigned char *in, unsigned int inLen,
110         unsigned char **out, unsigned int *outLen);
111     unsigned char attrNum;
112     HILINK_BT_AttrInfo *attrInfo;
113 } HILINK_BT_SvcInfo;
114 
115 /* 产品Profile定义结构体 */
116 typedef struct {
117     unsigned int svcNum;
118     HILINK_BT_SvcInfo *svcInfo;
119 } HILINK_BT_Profile;
120 
121 /* 蓝牙gatt character描述 */
122 typedef struct {
123     char *descUuid;
124     /* gatt属性描述读写权限:取值由HILINK_BT_DescPermission类型的成员或运算得出 */
125     unsigned int descPermission;
126 } HILINK_BT_GattProfileDesc;
127 
128 /* 蓝牙gatt character */
129 typedef struct {
130     char *charUuid;
131     /* gatt char权限:取值由HILINK_BT_CharPermission类型的成员或运算得出 */
132     unsigned int charPermission;
133     /* gatt char属性:取值由HILINK_BT_CharProperty类型的成员或运算得出 */
134     unsigned int charProperty;
135     HILINK_BT_GattReadCallback readFunc;
136     HILINK_BT_GattWriteCallback writeFunc;
137     HILINK_BT_GattProfileDesc *desc;
138     unsigned char descNum;
139 } HILINK_BT_GattProfileChar;
140 
141 /* 蓝牙gatt 服务 */
142 typedef struct {
143     char *svcUuid;
144     int isPrimary;
145     HILINK_BT_GattProfileChar *character;
146     unsigned char charNum;
147 } HILINK_BT_GattProfileSvc;
148 
149 /* 厂商自定义蓝牙gatt服务列表 */
150 typedef struct {
151     HILINK_BT_GattProfileSvc *service;
152     unsigned char serviceNum;
153 } HILINK_BT_GattServiceList;
154 
155 /* 配置保存回调结构体 */
156 typedef struct {
157     int (*createItem)(const char *name, unsigned int size);
158     int (*readItem)(const char *name, unsigned char *buf, unsigned int len);
159     int (*writeItem)(const char *name, const unsigned char *buf, unsigned int len);
160     int (*deleteItem)(const char *name);
161     int (*destroyConfMgr)(void);
162     int (*getHichainFlashAddr)(unsigned int *start, unsigned int *size);
163 } HILINK_BT_ConfigInterface;
164 
165 /* 获取广播数据结构体 */
166 typedef struct {
167     unsigned int advSvcDataLen;
168     unsigned char advSvcData[ADV_VALUE_MAX_LEN];
169     unsigned int advRspDataLen;
170     unsigned char advRspData[ADV_VALUE_MAX_LEN];
171 } HILINK_BT_AdvertiseData;
172 
173 /* 设置应用层编码模式 */
174 int HILINK_BT_SetEncodeMode(HILINK_BT_CmdDataMode mode);
175 
176 /* 查询应用层编码模式 */
177 HILINK_BT_CmdDataMode HILINK_BT_GetEncodeMode(void);
178 
179 /* 初始化启动HiLink Bluetooth SDK */
180 int HILINK_BT_Init(const HILINK_BT_Profile *profile);
181 
182 /* 初始化蓝牙协议栈 */
183 int HILINK_BT_InitBtStack(void);
184 
185 /* 启动HiLink BT SDK处理,调用HiLink协议栈 */
186 int HILINK_BT_Process(void);
187 
188 /*
189  * 结束HiLink Bluetooth SDK
190  * flag为0:只销毁控制和调度线程,flag为1销毁蓝牙协议栈,该函数不可重入
191  */
192 int HILINK_BT_DeInit(unsigned int flag);
193 
194 /* 添加HiLink服务信息service信息 */
195 int HILINK_BT_AddHiLilnkService(const HILINK_BT_SvcInfo *serviceArray, unsigned int serviceNum);
196 
197 /* 通知服务状态 */
198 int HILINK_BT_ReportServiceState(const void *service, const void *buf, unsigned int len);
199 
200 /* 通知属性状态 */
201 int HILINK_BT_ReportAttrState(const void *svc, const void *attr, const void *buf, unsigned int len);
202 
203 /* 查询蓝牙数据发送接口 */
204 HILINK_BT_SendBtDataCallback HILINK_BT_GetBtDataSendCallback(void);
205 
206 /* 查询蓝牙OTA数据发送接口 */
207 HILINK_BT_SendBtDataCallback HILINK_BT_GetOtaDataSendCallback(void);
208 
209 /* 设置蓝牙SDK事件处理函数 */
210 int HILINK_BT_SetSdkEventCallback(HILINK_BT_SdkEventCallBack callback);
211 
212 /*
213  * 设置BLE最大连接数量
214  * 入参connNum的范围为[1,10]
215  * 最大连接数上限为10,超过10个按10个执行
216  * 最小连接数为1,小于1按1个执行
217  * 若不调用该接口,默认最大连接数为1
218  */
219 void HILINK_BT_SetMaxConnNum(int connNum);
220 
221 /* 查询蓝牙SDK最大连接数量 */
222 int HILINK_BT_GetMaxConnNum(void);
223 
224 /* 添加蓝牙SDK自定义gatt服务 */
225 int HILINK_BT_SetGattProfile(const HILINK_BT_GattServiceList *gattServiceList);
226 
227 /* 注册配置保存回调函数到HiLink Bluetooth SDK,若不调用该函数,则默认使用HiLink Bluetooth SDK保存配置实现 */
228 int HILINK_BT_RegisterConfigInterface(const HILINK_BT_ConfigInterface *interface);
229 
230 /* 启动广播 */
231 int HILINK_BT_StartAdvertise(void);
232 
233 /* 停止广播 */
234 int HILINK_BT_StopAdvertise(void);
235 
236 /* 上报蓝牙反馈数据 */
237 int HILINK_BT_IndicateSvcCharData(const char *svcUuid, const char *charUuid, const char *buf, unsigned int len);
238 
239 /* 获取蓝牙SDK设备相关信息 */
240 HILINK_BT_DevInfo *HILINK_BT_GetDevInfo(void);
241 
242 /* 获取靠近发现中广播数据 */
243 int HILINK_BT_GetAdvertiseData(HILINK_BT_AdvertiseData *advertiseData);
244 
245 /* 获取BLE厂商注册的回调函数 */
246 BLE_CfgNetCb *GetBleCfgNetCallback(void);
247 
248 /* 设置开启Kitframework认证 */
249 void HILINK_BT_EnableKitframework(void);
250 
251 /* 设置关闭Kitframework认证 */
252 void HILINK_BT_DisableKitframework(void);
253 
254 typedef struct {
255     unsigned char ivNonce[HIBEACON_IV_NONCE_LEN];
256     unsigned char psk[HIBEACON_PSK_LEN];
257 } HiBeaconData;
258 
259 typedef int (*HiBeaconDataHandler)(const HiBeaconData *data);
260 
261 /* 注册hibeacon设备配网数据回调 */
262 void HILINK_BT_RegHiBeaconDataHandler(HiBeaconDataHandler handler);
263 
264 #ifdef __cplusplus
265 }
266 #endif
267 #endif
268