• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
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  */
15 
16 #ifndef _MSM_BLE_API_H_
17 #define _MSM_BLE_API_H_
18 
19 #include <stdint.h>
20 #include "sonata_att.h"
21 #include "sonata_gatt_api.h"
22 
23 #define BLE_APP_FRAME_DATA_MAX_LENGTH    300
24 
25 /*
26  * MACRO DEFINITIONS
27  ****************************************************************************************
28  */
29 
30 /// GATT read perm auth     (0x0202)
31 #define PRD_AU   (SONATA_PERM(RD, ENABLE)        | SONATA_PERM(RP, AUTH))
32 /// GATT write perm  auth   (0x0808)
33 #define PWR_AU   (SONATA_PERM(WRITE_REQ, ENABLE) | SONATA_PERM(WP, AUTH))
34 /// GATT ind perm  auth     (0X2020)
35 #define PIND_AU  (SONATA_PERM(IND, ENABLE)       | SONATA_PERM(IP, AUTH))
36 /// GATT notify perm  auth  (0x1080)
37 #define PNTF_AU  (SONATA_PERM(NTF, ENABLE)       | SONATA_PERM(NP, AUTH))
38 
39 #define PWC_AU   (SONATA_PERM(WRITE_COMMAND, ENABLE) | SONATA_PERM(WP, AUTH))
40 
41 /// GATT read perm unauth   (0x0201)
42 #define PRD_UA   (SONATA_PERM(RD, ENABLE)        | SONATA_PERM(RP, UNAUTH))
43 /// GATT write perm unauth  (0x0804)
44 #define PWR_UA   (SONATA_PERM(WRITE_REQ, ENABLE) | SONATA_PERM(WP, UNAUTH))
45 /// GATT ind perm  unauth   (0X2010)
46 #define PIND_UA  (SONATA_PERM(IND, ENABLE)       | SONATA_PERM(IP, UNAUTH))
47 /// GATT notify perm unauth (0x1040)
48 #define PNTF_UA  (SONATA_PERM(NTF, ENABLE)       | SONATA_PERM(NP, UNAUTH))
49 
50 /// GATT write perm unauth  (0x0404)
51 #define PWC_UA   (SONATA_PERM(WRITE_COMMAND, ENABLE) | SONATA_PERM(WP, UNAUTH))
52 
53 /// GATT read perm no auth  (0x0200)
54 #define PRD_NA   (SONATA_PERM(RD, ENABLE)        | SONATA_PERM(RP, NO_AUTH))
55 /// GATT write perm no auth (0x0800)
56 #define PWR_NA   (SONATA_PERM(WRITE_REQ, ENABLE) | SONATA_PERM(WP, NO_AUTH))
57 /// GATT ind perm  no auth  (0X2000)
58 #define PIND_NA  (SONATA_PERM(IND, ENABLE)       | SONATA_PERM(IP, NO_AUTH))
59 /// GATT notify perm no auth(0x1000)
60 #define PNTF_NA  (SONATA_PERM(NTF, ENABLE)       | SONATA_PERM(NP, NO_AUTH))
61 
62 #define PWC_NA   (SONATA_PERM(WRITE_COMMAND, ENABLE) | SONATA_PERM(WP, NO_AUTH))
63 
64 /// extended Descriptor Present (0X8000)
65 #define PEXT     (SONATA_PERM(EXT,ENABLE))
66 /// enable Read Indication (1 = Value not present in Database) (0x8000)
67 #define PRI      (SONATA_PERM(RI, ENABLE))
68 
69 /** * @brief       Minimum advertising interval.The range is from 0x0020 to 0x4000. */
70 #define BLE_MCRC_MIN_INTERVAL              192 // (0x01E0)
71 /* 0.3s *//** * @brief       Maximum advertising interval.The range is from 0x0020 to 0x4000 and should be greater than the minimum advertising interval. */
72 #define BLE_MCRC_MAX_INTERVAL              192 // (0x01E0)
73 /* 0.3s *//** * @brief       Advertising channel map */
74 #define MS_BLE_CHANNEL_NUM               (7)
75 /** * @brief       Advertising filter policy */
76 #define MS_BLE_FILTER_POLICY             (0)
77 
78 #define MAX_SERVICE_NUM 10
79 
80 enum BLE_ADV_STATE {
81     /// Advertising activity does not exists
82     BLE_ADV_STATE_IDLE = 0,
83     /// Creating advertising activity
84     BLE_ADV_STATE_CREATING,
85     /// Setting advertising data
86     BLE_ADV_STATE_SETTING_ADV_DATA,
87     /// Setting scan response data
88     BLE_ADV_STATE_SETTING_SCAN_RSP_DATA,
89     /// Advertising activity created
90     BLE_ADV_STATE_CREATED,
91     /// Starting advertising activity
92     BLE_ADV_STATE_STARTING,
93     /// Advertising activity started
94     BLE_ADV_STATE_STARTED,
95     /// Stopping advertising activity
96     BLE_ADV_STATE_STOPPING,
97 };
98 
99 typedef enum {
100     BLE_ENABLE_ADV,
101     BLE_DISABLE_ADV,
102 } ble_adv_enable;
103 
104 typedef enum {
105     BLE_GATTC_INDICATE,
106     BLE_GATTC_NOTIFY,
107 } ble_att_op_t;
108 
109 typedef enum {
110     BLE_STATUS_SUCCESS,
111     BLE_STATUS_INVALID_PARAM,
112     BLE_STATUS_DISCONNETED,
113     BLE_STATUS_FAILED,
114 } ble_status_t;
115 
116 typedef struct BLE_ADV_PARAM_T {
117     uint16_t advertising_interval_min;
118     uint16_t advertising_interval_max;
119     uint8_t advertising_type;
120     uint8_t own_address_type;
121     uint8_t advertising_channel_map;
122     uint8_t advertising_filter_policy;
123 } ble_adv_param_t;
124 
125 typedef struct BLE_ADV_DATA_T {
126     uint8_t ble_advdata[32];     /* !< ?????? */
127     uint8_t ble_advdataLen;     /* !< ?????? */
128 
129 } ble_adv_data_t;
130 
131 typedef struct BLE_SCAN_DATA_T {
132     uint8_t ble_respdata[32];     /* !< ?????????? */
133     uint8_t ble_respdataLen;     /* !< ?????????? */
134 
135 } ble_scan_data_t;
136 
137 typedef struct BLE_CMD_DISCONNECT_T {
138     uint8_t link_id;
139     uint8_t reason;
140 
141 } ble_cmd_disconnect_t;
142 
143 typedef struct BLE_VALUE_NOTIFICATION_INDICATION_T {
144     uint8_t att_operation;
145     uint16_t att_handle;
146     uint16_t att_length;
147     uint8_t  att_value[BLE_APP_FRAME_DATA_MAX_LENGTH];
148 } ble_value_notification_indication_t;
149 
150 typedef struct BLE_ADV_ENABLE_T {
151     uint8_t advertising_enable;
152 } ble_adv_enable_t;
153 
154 typedef enum MS_BLE_STACK_EVENT_T {
155     MS_BLE_STACK_EVENT_STACK_READY = 0,
156     MS_BLE_STACK_EVENT_STACK_FAIL,
157     MS_BLE_STACK_EVENT_ADV_ON,                  /* !< ???????????г?? */
158     MS_BLE_STACK_EVENT_ADV_OFF,                /* !< ???????????г?? */
159     MS_BLE_STACK_EVENT_DISCONNECT, /* !< ?????? */
160     MS_BLE_STACK_EVENT_CONNECTED,  /* !< ?????? */
161 } ms_ble_stack_event_t;
162 
163 typedef void (*ble_gatt_service_att_wirte_cb)(uint8_t *data, uint16_t size);
164 typedef void (*ble_gatt_service_att_read_cb)(uint8_t *data,  uint16_t *size);
165 typedef void (*ble_gatt_service_att_ind_cb)(uint8_t *data,  uint16_t size);
166 
167 typedef struct ble_gatt_att_opr {
168     ble_gatt_service_att_wirte_cb write_request;
169     ble_gatt_service_att_read_cb  read_request;
170     ble_gatt_service_att_ind_cb  ind_cb;
171 } ble_gatt_att_opr_t;
172 
173 typedef struct ble_gatt_att_reg {
174     sonata_gatt_att_desc_t att_desc;
175     ble_gatt_att_opr_t  att_opr;
176 } ble_gatt_att_reg_t;
177 
178 typedef void (*cb_fun)(ms_ble_stack_event_t);
179 typedef void (*app_ble_scan_callback_t)(uint8_t *data, uint16_t len);
180 
181 extern cb_fun ble_cb_fun;
182 
183 void ble_set_callback(cb_fun cb);
184 int ble_gatt_add_svc_helper(uint16_t *start_hdl, uint8_t nb_att, ble_gatt_att_reg_t *atts);
185 void ble_gatt_data_send(uint16_t local_handle, uint16_t idx, uint16_t length, uint8_t *p_value);
186 uint8_t ble_get_connect_status(void);
187 uint8_t ble_disconnect_request(void);
188 uint8_t ble_get_adv_status(void);
189 void ble_start_advertising(void);
190 void ble_set_max_mtu(uint16_t mtu);
191 uint16_t ble_get_mtu(void);
192 void ble_stop_adv(void);
193 void ble_gatt_add_srv_rsp(uint16_t handle);
194 void ble_gatt_read_request_handler(uint8_t connection_id, uint16_t handle, uint16_t *p_length, uint8_t **p_value);
195 void ble_gatt_write_request_handler(uint8_t connection_id, uint16_t handle, uint16_t length, uint8_t *p_value);
196 int ble_scan_start(app_ble_scan_callback_t cb);
197 int ble_scan_stop(void);
198 int ms_add_test_service(void);
199 
200 #endif
201