1 /*****************************************************************************
2  * Copyright (c) 2019, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @brief bluetooth functions.
30  * @file n32wb452_ble_api.h
31  * @author Nations
32  * @version v1.0.1
33  * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
34  */
35 
36 /* Define to prevent recursive inclusion -------------------------------------*/
37 #ifndef __N32WB452_BLE_API_H__
38 #define __N32WB452_BLE_API_H__
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #include "n32wb452.h"
45 
46 #define BLE_SERVICE_USED        1       //用到的蓝牙服务数量
47 #define BLE_CHARACTER_USED      1       //用到的特征字数量
48 
49 /**
50  * @brief 返回值类型定义
51  */
52 typedef enum bt_ret_enum_t
53 {
54     BT_RET_SUCCESS = 0,             //操作成功
55     BT_RET_OPERATED_FAILDED_ERR,    //操作失败
56     BT_RET_PARAM_NULL_ERR,          //参数为空错误
57     BT_RET_PARAM_ERR,               //参数错误
58 }bt_ret_enum;
59 
60 
61 /**
62  * @brief 定义蓝牙事件
63  */
64 typedef enum bt_event_enum_t
65 {
66     BT_EVENT_VERSION,               //返回蓝牙软件版本,如'V1000'表示Ver1.000
67     BT_EVENT_CONNECTED,             //表示蓝牙已连接
68     BT_EVENT_DISCONNECTD,           //表示蓝牙已断开
69     BT_EVENT_RCV_DATA,              //表示蓝牙模块接收到了主机端下发的数据
70 }bt_event_enum;
71 
72 /**
73  * @brief 特征字权限
74  */
75 typedef enum bt_character_perm_t
76 {
77     BT_RD_PERM          = 0x00001,      //主机只读
78     BT_WRITE_PERM       = 0x00002,      //主机可写,设备端无response
79     BT_WRITE_REQ_PERM   = 0x00004,      //主机可写,设备端有response
80     BT_NTF_PERM         = 0x00008,      //设备通知
81 }bt_character_perm;
82 
83 
84 typedef void (*bt_event_callback_handler_t)(bt_event_enum event, const uint8_t * data, uint32_t size, uint32_t character_uuid);
85 
86 
87 /**
88  * @brief 蓝牙控制参数配置
89  */
90 typedef struct bt_control_param_t
91 {
92     uint32_t multi_mode:1;  //0:disable connect multiple,1:enable connect multiple
93     uint32_t reserved:31;  //
94 }bt_control_param;
95 
96 /**
97  * @brief 蓝牙服务定义
98  */
99 typedef struct bt_svc_param_t
100 {
101     uint16_t svc_uuid;  /// 16 bits UUID,从0xFEC1开始
102 
103     struct bt_character{
104         uint16_t uuid;
105         uint16_t permission;//reference to bt_character_perm
106     }character[BLE_CHARACTER_USED];
107 }bt_svc_param;
108 
109 
110 
111 /**
112  * @brief 蓝牙相关属性配置
113  */
114 typedef struct bt_attr_param_t
115 {
116     bt_control_param ctrl_param;    //蓝牙控制参数
117     uint8_t device_name[32];        //蓝牙设备名.4字符以上有效。如全为0x00或无效名称,则使用自带默认名称
118     uint8_t scan_rsp_data[31];      //广播应答数据,实际有效长度30
119     uint8_t scan_rsp_data_len;      //广播应答数据
120     uint8_t device_addr[20];        //格式如:"11:22:33:44:55:66"。如配置为0则为默认地址
121 
122     //adv data
123     bt_svc_param service[BLE_SERVICE_USED];
124 }bt_attr_param;
125 
126 
127 /***************************************************************************************
128 declaraction: int32_t bt_ware_init(bt_event_pcallback pcallback)
129 function    : 初始化蓝牙模块
130 parameter   : 蓝牙初始化数据
131 parameter   : 蓝牙事件回调函数
132 return      : NONE
133 ***************************************************************************************/
134 int32_t bt_ware_init(bt_attr_param *pinit, bt_event_callback_handler_t pcallback);
135 
136 
137 /***************************************************************************************
138 declaraction: void bt_tick_count(void)
139 function    : 蓝牙模块需要的计时函数(默认定时:5ms)
140 parameter   : void
141 return      : NONE
142 ***************************************************************************************/
143 //void bt_tick_count(void);
144 
145 
146 /***************************************************************************************
147 declaraction: void bt_handler(void)
148 function    : 蓝牙中断处理
149 parameter   : void
150 return      : NONE
151 ***************************************************************************************/
152 void bt_handler(void);
153 
154 /***************************************************************************************
155 declaraction: void bt_run_thread(void)
156 function    : 蓝牙运行的主线程,用于蓝牙接收、发送、通知等处理
157 parameter   : void
158 return      : NONE
159 ***************************************************************************************/
160 void bt_run_thread(void);
161 
162 
163 /***************************************************************************************
164 declaraction: uint32_t bt_rcv_data(uint8_t *data, uint32_t size, uint32_t character)
165 function    : 根据BT_EVENT_RCV_DATA事件,读取相应服务特征字的数据
166 parameter   : uint8_t *data 接收数据buf
167 parameter   : uint32_t size buf大小(最大不超过512Bytes)
168 parameter   : uint32_t character 发生数据通知对应的特征字ID
169 return      : 返回实际读取大小
170 ***************************************************************************************/
171 uint32_t bt_rcv_data(uint8_t *data, uint32_t size, uint32_t character);
172 
173 
174 /***************************************************************************************
175 declaraction: uint32_t bt_rcv_data(const uint8_t *data, uint32_t size, uint32_t character)
176 function    : 发送数据到对应的特征字
177 parameter   : uint8_t *data 发送数据buf
178 parameter   : uint32_t 发送数据大小(最大不超过512Bytes)
179 parameter   : uint32_t character 对应特征字ID
180 return      : 返回值0
181 ***************************************************************************************/
182 uint32_t bt_snd_data(const uint8_t *data, uint32_t size, uint32_t character);
183 
184 
185 /***************************************************************************************
186 declaraction: void bt_disconnect(void)
187 function    : slave主动断开蓝牙连接
188 parameter   : NULL
189 return      : NULL
190 ***************************************************************************************/
191 void bt_disconnect(void);
192 
193 /***************************************************************************************
194 declaraction: void ble_status_monitor(void)
195 function    : 蓝牙状态监控
196 parameter   : NULL
197 return      : NULL
198 ***************************************************************************************/
199 void ble_status_monitor(void);
200 
201 /*==============================================================================
202 BT_Init
203 <Description>
204     This function is to initialize bluetooth stack
205 ==============================================================================*/
206 void BT_init(void);
207 
208 
209 /*==============================================================================
210 is_bt_busy
211 
212 <Description>
213     This function is to get the bt kernel processing is ongoing or not.
214     if not onging, then allow to sleep, otherwise not allow to sleep.
215 
216 <return>
217     0:  bt kernel processing is not ongoing
218     1:  bt kernel processing is ongoing
219 ==============================================================================*/
220 bool is_bt_busy(void);
221 
222 
223 /*==============================================================================
224 BT_handle
225 
226 <Description>
227     This function is schedule the BT tasks
228 ==============================================================================*/
229 void BT_handle(void);
230 
231 /*==============================================================================
232 BT_get_version
233 
234 <Description>
235     This function is to get version  of BT API.
236 
237 <parameter out>
238     version:   store the data of the BT API version, size 10 byte;
239 ==============================================================================*/
240 void BT_get_version(uint8_t * version);
241 
242 extern bt_event_callback_handler_t g_pcallback;
243 extern uint32_t g_connnet_start;
244 
245 /*==============================================================================
246 ble_get_drv_version
247 
248 
249 <Description>
250     This function is to get the version of the ble driver.
251 
252 <parameter out>
253     version:   store the data of the ble driver version, size 6 byte:"V1.0.0";
254 ==============================================================================*/
255 extern void ble_get_drv_version(char *version);
256 
257 #if defined __cplusplus
258 }
259 #endif
260 #endif // __N32WB452_BLE_API_H__
261 
262 
263