• 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示例代码(此文件为DEMO,需集成方适配修改)
15  */
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <string.h>
21 
22 #include "hilink_bt_api.h"
23 #include "hilink_bt_function.h"
24 #include "ble_cfg_net_api.h"
25 #include "hilink_device.h"
26 
27 #include "ohos_bt_gatt.h"
28 #include "ohos_bt_gatt_server.h"
29 #include "cmsis_os2.h"
30 #include "hilink_socket_adapter.h"
31 
32 /*
33  * 设备基本信息根据设备实际情况填写
34  * 与hilink sdk相同定义,双模组模式只需一份,已提供给第三方厂家,暂不按编程规范整改
35  */
36 #define DEVICE_SN "113GFF01239F0066"
37 #define PRODUCT_ID "2LWU"
38 #define DEVICE_MODEL "IH6SC800S3"
39 #define DEVICE_TYPE "008"
40 #define MANUAFACTURER "04D"
41 #define DEVICE_MAC "607D0968B970"
42 #define DEVICE_HIVERSION "1.0.0"
43 #define DEVICE_PROT_TYPE 1
44 #define DEVICE_TYPE_NAME "WiFiCamera"
45 #define MANUAFACTURER_NAME "HW"
46 
47 /* 蓝牙sdk单独使用的定义 */
48 #define SUB_PRODUCT_ID "00"
49 #define ADV_TX_POWER 0xF8
50 #define BLE_ADV_TIME 60
51 
52 /* 厂商自定义蓝牙广播,设备型号信息 */
53 #define BT_CUSTOM_INFO "12345678"
54 #define DEVICE_MANU_ID "017"
55 
56 #define min_len(a, b) (((a) < (b)) ? (a) : (b))
57 
58 static HILINK_BT_DevInfo g_btDevInfo;
59 
60 /*
61  * 功能: 获取设备sn号
62  * 参数[in]: len sn的最大长度, 39字节
63  * 参数[out]: sn 设备sn
64  * 注意: sn指针的字符串长度为0时将使用设备mac地址作为sn
65  */
HILINK_GetDeviceSn(unsigned int len,char * sn)66 void HILINK_GetDeviceSn(unsigned int len, char *sn)
67 {
68     if (sn == NULL) {
69         return;
70     }
71     const char *ptr = DEVICE_SN;
72     int tmp = min_len(len, sizeof(DEVICE_SN));
73     for (int i = 0; i < tmp; i++) {
74         sn[i] = ptr[i];
75     }
76     return;
77 }
78 
79 /*
80  * 获取设备的子型号,长度固定两个字节
81  * subProdId为保存子型号的缓冲区,len为缓冲区的长度
82  * 如果产品定义有子型号,则填入两字节子型号,并以'\0'结束, 返回0
83  * 没有定义子型号,则返回-1
84  * 该接口需设备开发者实现
85  */
HILINK_GetSubProdId(char * subProdId,int len)86 int HILINK_GetSubProdId(char *subProdId, int len)
87 {
88     if (subProdId == NULL) {
89         return -1;
90     }
91     const char *ptr = SUB_PRODUCT_ID;
92     int tmp = min_len((unsigned int)len, sizeof(SUB_PRODUCT_ID));
93     for (int i = 0; i < tmp; i++) {
94         subProdId[i] = ptr[i];
95     }
96     return 0;
97 }
98 
99 /*
100  * 获取设备表面的最强点信号发射功率强度,最强点位置的确定以及功率测试方
101  * 法,参照hilink认证蓝牙靠近发现功率设置及测试方法指导文档,power为出参,
102  * 单位dbm,下一次发送广播时生效
103  */
HILINK_BT_GetDevSurfacePower(char * power)104 int HILINK_BT_GetDevSurfacePower(char *power)
105 {
106     if (power == NULL) {
107         return -1;
108     }
109     *power = ADV_TX_POWER;
110     return 0;
111 }
112 
113 /* 获取蓝牙SDK设备相关信息 */
HILINK_BT_GetDevInfo(void)114 HILINK_BT_DevInfo *HILINK_BT_GetDevInfo(void)
115 {
116     printf("HILINK_BT_GetDevInfo\n");
117     g_btDevInfo.manuName = MANUAFACTURER;
118     g_btDevInfo.devName = DEVICE_TYPE_NAME;
119     g_btDevInfo.productId = PRODUCT_ID;
120     g_btDevInfo.mac = DEVICE_MAC;
121     g_btDevInfo.model = DEVICE_MODEL;
122     g_btDevInfo.devType = DEVICE_TYPE;
123     g_btDevInfo.hiv = DEVICE_HIVERSION;
124     g_btDevInfo.protType = DEVICE_PROT_TYPE;
125     g_btDevInfo.sn = DEVICE_SN;
126     return &g_btDevInfo;
127 }
128 /*
129  * 若厂商发送广播类型为BLE_ADV_CUSTOM时才需适配此接口
130  * 获取厂商定制信息,由厂家实现
131  * 返回0表示获取成功,返回其他表示获取失败
132  */
HILINK_GetCustomInfo(char * customInfo,unsigned int len)133 int HILINK_GetCustomInfo(char *customInfo, unsigned int len)
134 {
135     if (customInfo == NULL || len == 0) {
136         return -1;
137     }
138 
139     const char *ptr = BT_CUSTOM_INFO;
140     int tmp = min_len(len, strlen(BT_CUSTOM_INFO));
141     for (int i = 0; i < tmp; i++) {
142         customInfo[i] = ptr[i];
143     }
144 
145     return 0;
146 }
147 
148 /*
149  * 若厂商发送广播类型为BLE_ADV_CUSTOM时才需适配此接口
150  * 获取厂家ID,由厂家实现
151  * 返回0表示获取成功,返回其他表示获取失败
152  */
HILINK_GetManuId(char * manuId,unsigned int len)153 int HILINK_GetManuId(char *manuId, unsigned int len)
154 {
155     if (manuId == NULL || len == 0) {
156         return -1;
157     }
158 
159     const char *ptr = DEVICE_MANU_ID;
160     int tmp = min_len(len, strlen(DEVICE_MANU_ID));
161     for (int i = 0; i < tmp; i++) {
162         manuId[i] = ptr[i];
163     }
164 
165     return 0;
166 }
167 
168 /*
169  * 获取蓝牙mac地址,由厂家实现
170  * 返回0表示获取成功,返回其他表示获取失败
171  */
HILINK_BT_GetMacAddr(unsigned char * mac,unsigned int len)172 int HILINK_BT_GetMacAddr(unsigned char *mac, unsigned int len)
173 {
174     (void)mac;
175     (void)len;
176     return 0;
177 }
178 
179 /* 填写固件、软件和硬件版本号,APP显示版本号以及OTA版本检查与此相关 */
getDeviceVersion(char ** firmwareVer,char ** softwareVer,char ** hardwareVer)180 int getDeviceVersion(char* *firmwareVer, char* *softwareVer, char* *hardwareVer)
181 {
182     *firmwareVer = "1.0.0";
183     *softwareVer = "1.1.0";
184     *hardwareVer = "1.1.1";
185     return 0;
186 }
187 
HILINK_BT_StateChangeHandler(HILINK_BT_SdkStatus event,const void * param)188 static void HILINK_BT_StateChangeHandler(HILINK_BT_SdkStatus event, const void *param)
189 {
190     (void)param;
191     /* ble sdk初始化完成后,发送广播让设备被手机发现 */
192     if (event == HILINK_BT_SDK_STATUS_SVC_RUNNING) {
193         /* 设置蓝牙广播格式,包括靠近发现、碰一碰等,下一次发送广播生效 */
194         BLE_SetAdvType(BLE_ADV_DEFAULT);
195 
196         /* BLE配网广播控制:参数代表广播时间,0:停止;0xFFFFFFFF:一直广播,其他:广播指定时间后停止,单位秒 */
197         (void)BLE_CfgNetAdvCtrl(BLE_ADV_TIME);
198     }
199 }
200 
201 static BLE_ConfPara g_isBlePair = {
202     .isBlePair = 0,
203 };
204 
205 static BLE_InitPara g_bleInitParam = {
206     .confPara = &g_isBlePair,
207     /* advInfo为空表示使用ble sdk默认广播参数及数据 */
208     .advInfo  = NULL,
209     .gattList = NULL,
210 };
211 
212 /* APP下发自定义指令时调用此函数,需处理自定义数据,返回0表示处理成功 */
BleRcvCustomData(unsigned char * buff,unsigned int len)213 static int BleRcvCustomData(unsigned char *buff, unsigned int len)
214 {
215     printf("custom data, len: %u, data: %s\r\n", len, buff);
216     /* 处理自定义数据 */
217     return 0;
218 }
219 
220 static BLE_CfgNetCb g_bleCfgNetCb = {
221     .rcvCustomDataCb = BleRcvCustomData,
222 };
223 
hilink_ble_main(void)224 int hilink_ble_main(void)
225 {
226     /* 设备按需设置,例如接入蓝牙网关时,设置广播类型标志及心跳间隔 */
227     unsigned char mpp[] = {0x02, 0x3c, 0x00};
228     int ret = BLE_SetAdvNameMpp(mpp, sizeof(mpp));
229     if (ret != 0) {
230         printf("set adv name mpp failed\r\n");
231         return -1;
232     }
233 
234     ret = HILINK_SetNetConfigMode(HILINK_NETCONFIG_OTHER);
235     if (ret != 0) {
236         printf("SetNetConfigMode failed\r\n");
237         return -1;
238     }
239 
240     /* 注册SDK状态接收函数,可在初始化完成后发送广播 */
241     ret = HILINK_BT_SetSdkEventCallback(HILINK_BT_StateChangeHandler);
242     if (ret != 0) {
243         printf("set event callback failed\r\n");
244         return -1;
245     }
246 
247     /* 初始化ble sdk */
248     ret = BLE_CfgNetInit(&g_bleInitParam, &g_bleCfgNetCb);
249     if (ret != 0) {
250         printf("ble sdk init fail\r\n");
251         return -1;
252     }
253 
254     /* 修改任务属性 */
255     HILINK_SdkAttr *sdkAttr = HILINK_GetSdkAttr();
256     if (sdkAttr == NULL) {
257         printf("sdkAttr is null");
258         return -1;
259     }
260     sdkAttr->monitorTaskStackSize = 0x400;  /* 示例代码 推荐栈大小为0x400 */
261     HILINK_SetSdkAttr(*sdkAttr);
262 
263 #ifdef CONFIG_SUPPORT_HILINK_INDIE_UPGRADE
264     if (HILINK_RegisterErrnoCallback(get_os_errno) != 0) {
265         printf("reg errno cb err\r\n");
266     }
267 #endif
268 
269     /* 启动Hilink SDK */
270     if (HILINK_Main() != 0) {
271         printf("HILINK_Main start error");
272     }
273 
274     return 0;
275 }