1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 
9 #include "esp_bt_main.h"
10 #include "btc/btc_manage.h"
11 
12 #include "btc_sdp.h"
13 #include "esp_sdp_api.h"
14 #include "common/bt_target.h"
15 
16 #if (defined BTC_SDP_INCLUDED && BTC_SDP_INCLUDED == TRUE)
17 
esp_sdp_register_callback(esp_sdp_cb_t callback)18 esp_err_t esp_sdp_register_callback(esp_sdp_cb_t callback)
19 {
20     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
21 
22     if (callback == NULL) {
23         return ESP_FAIL;
24     }
25 
26     btc_profile_cb_set(BTC_PID_SDP, callback);
27     return ESP_OK;
28 }
29 
esp_sdp_init(void)30 esp_err_t esp_sdp_init(void)
31 {
32     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
33 
34     btc_msg_t msg;
35     bt_status_t stat;
36 
37     msg.sig = BTC_SIG_API_CALL;
38     msg.pid = BTC_PID_SDP;
39     msg.act = BTC_SDP_ACT_INIT;
40 
41     /* Switch to BTC context */
42     stat = btc_transfer_context(&msg, NULL, 0, NULL, NULL);
43     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
44 }
45 
esp_sdp_deinit(void)46 esp_err_t esp_sdp_deinit(void)
47 {
48     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
49 
50     btc_msg_t msg;
51     bt_status_t stat;
52 
53     msg.sig = BTC_SIG_API_CALL;
54     msg.pid = BTC_PID_SDP;
55     msg.act = BTC_SDP_ACT_DEINIT;
56 
57     /* Switch to BTC context */
58     stat = btc_transfer_context(&msg, NULL, 0, NULL, NULL);
59     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
60 }
61 
esp_sdp_search_record(esp_bd_addr_t bd_addr,esp_bt_uuid_t uuid)62 esp_err_t esp_sdp_search_record(esp_bd_addr_t bd_addr, esp_bt_uuid_t uuid)
63 {
64     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
65 
66     btc_msg_t msg;
67     bt_status_t stat;
68     btc_sdp_args_t arg;
69 
70     msg.sig = BTC_SIG_API_CALL;
71     msg.pid = BTC_PID_SDP;
72     msg.act = BTC_SDP_ACT_SEARCH;
73 
74     memset(&arg, 0, sizeof(btc_sdp_args_t));
75     memcpy(arg.search.bd_addr, bd_addr, ESP_BD_ADDR_LEN);
76     arg.search.sdp_uuid.len = uuid.len;
77     memcpy(&arg.search.sdp_uuid.uu, &uuid.uuid, sizeof(uuid.uuid));
78 
79     /* Switch to BTC context */
80     stat = btc_transfer_context(&msg, &arg, sizeof(btc_sdp_args_t), NULL, NULL);
81     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
82 }
83 
esp_sdp_create_record(esp_bluetooth_sdp_record_t * record)84 esp_err_t esp_sdp_create_record(esp_bluetooth_sdp_record_t *record)
85 {
86     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
87 
88     if (record == NULL || record->hdr.service_name_length > ESP_SDP_SERVER_NAME_MAX
89             || strlen(record->hdr.service_name)+1 != record->hdr.service_name_length) {
90         LOG_ERROR("Invalid server name!\n");
91         return ESP_ERR_INVALID_ARG;
92     }
93 
94     btc_msg_t msg;
95     bt_status_t stat;
96     btc_sdp_args_t arg;
97 
98     msg.sig = BTC_SIG_API_CALL;
99     msg.pid = BTC_PID_SDP;
100     msg.act = BTC_SDP_ACT_CREATE_RECORD;
101 
102     memset(&arg, 0, sizeof(btc_sdp_args_t));
103     arg.creat_record.record = (bluetooth_sdp_record *)record;
104 
105     /* Switch to BTC context */
106     stat = btc_transfer_context(&msg, &arg, sizeof(btc_sdp_args_t),
107                                     btc_sdp_arg_deep_copy, btc_sdp_arg_deep_free);
108     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
109 }
110 
esp_sdp_remove_record(int record_handle)111 esp_err_t esp_sdp_remove_record(int record_handle)
112 {
113     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
114 
115     btc_msg_t msg;
116     bt_status_t stat;
117     btc_sdp_args_t arg;
118 
119     msg.sig = BTC_SIG_API_CALL;
120     msg.pid = BTC_PID_SDP;
121     msg.act = BTC_SDP_ACT_REMOVE_RECORD;
122 
123     arg.remove_record.record_handle = record_handle;
124 
125     /* Switch to BTC context */
126     stat = btc_transfer_context(&msg, &arg, sizeof(btc_sdp_args_t), NULL, NULL);
127     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
128 }
129 
130 #endif ///defined BTC_SDP_INCLUDED && BTC_SDP_INCLUDED == TRUE
131