1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __ESP_SDP_API_H__
8 #define __ESP_SDP_API_H__
9 
10 #include "esp_err.h"
11 #include "esp_bt_defs.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #define ESP_SDP_SERVER_NAME_MAX              32  /*!< Service name max length */
18 #define SDP_OPP_SUPPORTED_FORMATS_MAX_LENGTH 15  /*!< OPP supported format list maximum length */
19 
20 #define ESP_SDP_UUID_MAP_MAS  0x1132 /*!< Message Access Service UUID */
21 #define ESP_SDP_UUID_MAP_MNS  0x1133 /*!< Message Notification Service UUID */
22 #define ESP_SDP_UUID_PBAP_PSE 0x112F /*!< Phone Book Server Equipment UUID */
23 #define ESP_SDP_UUID_PBAP_PCE 0x112E /*!< Phone Book Client Equipment UUID */
24 #define ESP_SDP_UUID_OPP      0x1105 /*!< Object Push Profile UUID */
25 #define ESP_SDP_UUID_SAP      0x112D /*!< SIM Access Profile UUID */
26 #define ESP_SDP_UUID_DIP      0x1200 /*!< Device Identification Profile UUID */
27 
28 #define ESP_SDP_BUILD_BT_UUID16(uuid16_val)                                                                                \
29     (esp_bt_uuid_t) { .len = ESP_UUID_LEN_16, .uuid = {.uuid16 = (uint16_t)(uuid16_val),}, }
30 
31 typedef enum {
32     ESP_SDP_SUCCESS = 0,      /*!< Successful operation. */
33     ESP_SDP_FAILURE,          /*!< Generic failure. */
34     ESP_SDP_NO_RESOURCE,      /*!< No more resource */
35     ESP_SDP_NEED_INIT,        /*!< SDP module shall init first */
36     ESP_SDP_NEED_DEINIT,      /*!< SDP module shall deinit first */
37     ESP_SDP_NO_CREATE_RECORD, /*!< No record created */
38 } esp_sdp_status_t;
39 
40 /**
41  * @brief SDP callback function events
42  */
43 typedef enum {
44     ESP_SDP_INIT_EVT               = 0, /*!< When SDP is initialized, the event comes */
45     ESP_SDP_DEINIT_EVT             = 1, /*!< When SDP is de-initialized, the event comes */
46     ESP_SDP_SEARCH_COMP_EVT        = 2, /*!< When SDP search complete, the event comes */
47     ESP_SDP_CREATE_RECORD_COMP_EVT = 3, /*!< When create SDP records complete, the event comes */
48     ESP_SDP_REMOVE_RECORD_COMP_EVT = 4, /*!< When remove a SDP record complete, the event comes */
49 } esp_sdp_cb_event_t;
50 
51 /**
52  * @brief SDP record type
53  */
54 typedef enum {
55     ESP_SDP_TYPE_RAW,        /*!< Used to carry raw SDP search data for unknown UUIDs */
56     ESP_SDP_TYPE_MAP_MAS,    /*!< Message Access Profile - Server */
57     ESP_SDP_TYPE_MAP_MNS,    /*!< Message Access Profile - Client (Notification Server) */
58     ESP_SDP_TYPE_PBAP_PSE,   /*!< Phone Book Profile - Server */
59     ESP_SDP_TYPE_PBAP_PCE,   /*!< Phone Book Profile - Client */
60     ESP_SDP_TYPE_OPP_SERVER, /*!< Object Push Profile */
61     ESP_SDP_TYPE_SAP_SERVER, /*!< SIM Access Profile */
62 } esp_bluetooth_sdp_types_t;
63 
64 /**
65  * @brief SDP header structure
66  */
67 typedef struct bluetooth_sdp_hdr_overlay {
68     esp_bluetooth_sdp_types_t type; /*!< SDP type */
69     esp_bt_uuid_t uuid;             /*!< UUID type, include uuid and uuid length, only needed to be set for RAW record creation */
70     uint32_t service_name_length;   /*!< Service name length */
71     char *service_name;             /*!< Service name */
72     int32_t rfcomm_channel_number;  /*!< RFCOMM channel number, if not used set to -1*/
73     int32_t l2cap_psm;              /*!< L2CAP psm, if not used set to -1 */
74     int32_t profile_version;        /*!< Profile version */
75     int user1_ptr_len;              /*!< User data1 length, only used for searching RAW record */
76     uint8_t *user1_ptr;             /*!< User data1 pointer to the raw SDP response data, only used for searching RAW record */
77     int user2_ptr_len;              /*!< User data2 length, only used for searching RAW record */
78     uint8_t *user2_ptr;             /*!< User data2 pointer, only used for searching RAW record */
79 } esp_bluetooth_sdp_hdr_overlay_t;
80 
81 /**
82  * @brief Message Access Profile - Server parameters
83  */
84 typedef struct bluetooth_sdp_mas_record {
85     esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */
86     uint32_t mas_instance_id;            /*!< MAS Instance ID */
87     uint32_t supported_features;         /*!< Map supported features */
88     uint32_t supported_message_types;    /*!< Supported message types */
89 } esp_bluetooth_sdp_mas_record_t;
90 
91 /**
92  * @brief Message Access Profile - Client (Notification Server) parameters
93  */
94 typedef struct bluetooth_sdp_mns_record {
95     esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */
96     uint32_t supported_features;         /*!< Supported features */
97 } esp_bluetooth_sdp_mns_record_t;
98 
99 /**
100  * @brief Phone Book Profile - Server parameters
101  */
102 typedef struct bluetooth_sdp_pse_record {
103     esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */
104     uint32_t supported_features;         /*!< PBAP Supported Features */
105     uint32_t supported_repositories;     /*!< Supported Repositories */
106 } esp_bluetooth_sdp_pse_record_t;
107 
108 /**
109  * @brief Phone Book Profile - Client parameters
110  */
111 typedef struct bluetooth_sdp_pce_record {
112     esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */
113 } esp_bluetooth_sdp_pce_record_t;
114 
115 /**
116  * @brief Object Push Profile parameters
117  */
118 typedef struct bluetooth_sdp_ops_record {
119     esp_bluetooth_sdp_hdr_overlay_t hdr;                                  /*!< General info */
120     int supported_formats_list_len;                                       /*!< Supported formats list length */
121     uint8_t supported_formats_list[SDP_OPP_SUPPORTED_FORMATS_MAX_LENGTH]; /*!< Supported formats list */
122 } esp_bluetooth_sdp_ops_record_t;
123 
124 /**
125  * @brief SIM Access Profile parameters
126  */
127 typedef struct bluetooth_sdp_sap_record {
128     esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */
129 } esp_bluetooth_sdp_sap_record_t;
130 
131 /**
132  * @brief SDP record parameters union
133  */
134 typedef union {
135     esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */
136     esp_bluetooth_sdp_mas_record_t mas;  /*!< Message Access Profile - Server */
137     esp_bluetooth_sdp_mns_record_t mns;  /*!< Message Access Profile - Client (Notification Server) */
138     esp_bluetooth_sdp_pse_record_t pse;  /*!< Phone Book Profile - Server */
139     esp_bluetooth_sdp_pce_record_t pce;  /*!< Phone Book Profile - Client */
140     esp_bluetooth_sdp_ops_record_t ops;  /*!< Object Push Profile */
141     esp_bluetooth_sdp_sap_record_t sap;  /*!< SIM Access Profile */
142 } esp_bluetooth_sdp_record_t;
143 
144 /**
145  * @brief SDP callback parameters union
146  */
147 typedef union {
148     /**
149      * @brief ESP_SDP_INIT_EVT
150      */
151     struct sdp_init_evt_param {
152         esp_sdp_status_t status; /*!< Status */
153     } init;                      /*!< SDP callback param of ESP_SDP_INIT_EVT */
154 
155     /**
156      * @brief ESP_SDP_DEINIT_EVT
157      */
158     struct sdp_deinit_evt_param {
159         esp_sdp_status_t status; /*!< Status */
160     } deinit;                    /*!< SDP callback param of ESP_SDP_DEINIT_EVT */
161 
162     /**
163      * @brief ESP_SDP_SEARCH_COMP_EVT
164      */
165     struct sdp_search_evt_param {
166         esp_sdp_status_t status;             /*!< Status */
167         esp_bd_addr_t remote_addr;           /*!< Remote device address */
168         esp_bt_uuid_t sdp_uuid;              /*!< Service uuid */
169         int record_count;                    /*!< Number of SDP records */
170         esp_bluetooth_sdp_record_t *records; /*!< SDP records */
171     } search;                                /*!< SDP callback param of ESP_SDP_SEARCH_COMP_EVT */
172 
173     /**
174      * @brief ESP_SDP_CREATE_RECORD_COMP_EVT
175      */
176     struct sdp_create_record_evt_param {
177         esp_sdp_status_t status; /*!< Status */
178         int record_handle;       /*!< SDP record handle */
179     } create_record;             /*!< SDP callback param of ESP_SDP_CREATE_RECORD_COMP_EVT */
180 
181     /**
182      * @brief ESP_SDP_REMOVE_RECORD_COMP_EVT
183      */
184     struct sdp_remove_record_evt_param {
185         esp_sdp_status_t status; /*!< Status */
186     } remove_record;             /*!< SDP callback param of ESP_SDP_REMOVE_RECORD_COMP_EVT */
187 
188 } esp_sdp_cb_param_t;
189 
190 /**
191  * @brief       SDP callback function type.
192  *
193  * @param       event:      Event type
194  * @param       param:      Point to callback parameter, currently is union type
195  */
196 typedef void (* esp_sdp_cb_t)(esp_sdp_cb_event_t event, esp_sdp_cb_param_t *param);
197 
198 /**
199  * @brief       This function is called to init callbacks with SDP module.
200  *
201  * @param[in]   callback:   pointer to the init callback function.
202  *
203  * @return
204  *              - ESP_OK: success
205  *              - other: failed
206  */
207 esp_err_t esp_sdp_register_callback(esp_sdp_cb_t callback);
208 
209 /**
210  * @brief       This function is called to init SDP module.
211  *              When the operation is completed, the callback function will be called with ESP_SDP_INIT_EVT.
212  *              This function should be called after esp_bluedroid_enable() completes successfully.
213  *
214  * @return
215  *              - ESP_OK: success
216  *              - other: failed
217  */
218 esp_err_t esp_sdp_init(void);
219 
220 /**
221  * @brief       This function is called to de-initialize SDP module.
222  *              The operation will remove all SDP records, then the callback function will be called
223  *              with ESP_SDP_REMOVE_RECORD_COMP_EVT, and the number of ESP_SDP_REMOVE_RECORD_COMP_EVT is
224  *              equal to the number of SDP records.When the operation is completed, the callback function
225  *              will be called with ESP_SDP_DEINIT_EVT. This function should be called after esp_sdp_init()
226  *              completes successfully.
227  *
228  * @return
229  *              - ESP_OK: success
230  *              - other: failed
231  */
232 esp_err_t esp_sdp_deinit(void);
233 
234 /**
235  * @brief       This function is called to performs service discovery for the services provided by the given peer device.
236  *              When the operation is completed, the callback function will be called with ESP_SDP_SEARCH_COMP_EVT.
237  *              This function must be called after esp_sdp_init() successful and before esp_sdp_deinit().
238  *
239  * @param[in]   bd_addr:   Remote device bluetooth device address.
240  * @param[in]   uuid:      Service UUID of the remote device.
241  *
242  * @return
243  *              - ESP_OK: success
244  *              - other: failed
245  */
246 esp_err_t esp_sdp_search_record(esp_bd_addr_t bd_addr, esp_bt_uuid_t uuid);
247 
248 /**
249  * @brief       This function is called to create SDP records.
250  *              When the operation is completed, the callback function will be called with ESP_SDP_CREATE_RECORD_COMP_EVT.
251  *              This function must be called after esp_sdp_init() successful and before esp_sdp_deinit().
252  *
253  * @param[in]   record:   The SDP record to create.
254  *
255  * @return
256  *              - ESP_OK: success
257  *              - other: failed
258  */
259 esp_err_t esp_sdp_create_record(esp_bluetooth_sdp_record_t *record);
260 
261 /**
262  * @brief       This function is called to remove a SDP record.
263  *              When the operation is completed, the callback function will be called with ESP_SDP_REMOVE_RECORD_COMP_EVT.
264  *              This function must be called after esp_sdp_init() successful and before esp_sdp_deinit().
265  *
266  * @param[in]   record_handle:    The SDP record handle.
267  *
268  * @return
269  *              - ESP_OK: success
270  *              - other: failed
271  */
272 esp_err_t esp_sdp_remove_record(int record_handle);
273 
274 #ifdef __cplusplus
275 }
276 #endif
277 
278 #endif ///__ESP_SDP_API_H__
279