1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
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 #include <string.h>
16 
17 #include "esp_bt_main.h"
18 #include "btc/btc_manage.h"
19 
20 #include "btc_spp.h"
21 #include "esp_spp_api.h"
22 #include "common/bt_target.h"
23 
24 #if (defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE)
25 
26 static const uint8_t UUID_SPP[16] = {0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00,
27                                     0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
28                                     };
29 static tSDP_UUID sdp_uuid;
esp_spp_register_callback(esp_spp_cb_t * callback)30 esp_err_t esp_spp_register_callback(esp_spp_cb_t *callback)
31 {
32     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
33 
34     if (callback == NULL) {
35         return ESP_FAIL;
36     }
37 
38     btc_profile_cb_set(BTC_PID_SPP, callback);
39     return ESP_OK;
40 }
41 
42 
esp_spp_init(esp_spp_mode_t mode)43 esp_err_t esp_spp_init(esp_spp_mode_t mode)
44 {
45     btc_msg_t msg;
46     btc_spp_args_t arg;
47     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
48 
49     msg.sig = BTC_SIG_API_CALL;
50     msg.pid = BTC_PID_SPP;
51     msg.act = BTC_SPP_ACT_INIT;
52 
53     arg.init.mode = mode;
54     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
55 }
56 
esp_spp_deinit(void)57 esp_err_t esp_spp_deinit(void)
58 {
59     btc_msg_t msg;
60     btc_spp_args_t arg;
61     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
62 
63     msg.sig = BTC_SIG_API_CALL;
64     msg.pid = BTC_PID_SPP;
65     msg.act = BTC_SPP_ACT_UNINIT;
66 
67     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
68 }
69 
70 
esp_spp_start_discovery(esp_bd_addr_t bd_addr)71 esp_err_t esp_spp_start_discovery(esp_bd_addr_t bd_addr)
72 {
73     sdp_uuid.len = 16;
74     memcpy(sdp_uuid.uu.uuid128, UUID_SPP, sizeof(sdp_uuid.uu.uuid128));
75 
76     btc_msg_t msg;
77     btc_spp_args_t arg;
78     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
79 
80     msg.sig = BTC_SIG_API_CALL;
81     msg.pid = BTC_PID_SPP;
82     msg.act = BTC_SPP_ACT_START_DISCOVERY;
83 
84     memcpy(arg.start_discovery.bd_addr, bd_addr, ESP_BD_ADDR_LEN);
85     arg.start_discovery.num_uuid = 1;
86     arg.start_discovery.p_uuid_list = &sdp_uuid;
87 
88     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), btc_spp_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
89 }
90 
esp_spp_connect(esp_spp_sec_t sec_mask,esp_spp_role_t role,uint8_t remote_scn,esp_bd_addr_t peer_bd_addr)91 esp_err_t esp_spp_connect(esp_spp_sec_t sec_mask,
92                           esp_spp_role_t role, uint8_t remote_scn, esp_bd_addr_t peer_bd_addr)
93 {
94     btc_msg_t msg;
95     btc_spp_args_t arg;
96     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
97 
98     if (sec_mask != ESP_SPP_SEC_NONE && sec_mask != ESP_SPP_SEC_AUTHORIZE && sec_mask != ESP_SPP_SEC_AUTHENTICATE) {
99         LOG_WARN("Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only\n");
100     }
101 
102     msg.sig = BTC_SIG_API_CALL;
103     msg.pid = BTC_PID_SPP;
104     msg.act = BTC_SPP_ACT_CONNECT;
105 
106     arg.connect.sec_mask = sec_mask;
107     arg.connect.role = role;
108     arg.connect.remote_scn = remote_scn;
109     memcpy(arg.connect.peer_bd_addr, peer_bd_addr, ESP_BD_ADDR_LEN);
110 
111     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
112 }
113 
esp_spp_disconnect(uint32_t handle)114 esp_err_t esp_spp_disconnect(uint32_t handle)
115 {
116     btc_msg_t msg;
117     btc_spp_args_t arg;
118     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
119 
120     msg.sig = BTC_SIG_API_CALL;
121     msg.pid = BTC_PID_SPP;
122     msg.act = BTC_SPP_ACT_DISCONNECT;
123 
124     arg.disconnect.handle = handle;
125 
126     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
127 }
128 
esp_spp_start_srv(esp_spp_sec_t sec_mask,esp_spp_role_t role,uint8_t local_scn,const char * name)129 esp_err_t esp_spp_start_srv(esp_spp_sec_t sec_mask,
130                             esp_spp_role_t role, uint8_t local_scn, const char *name)
131 {
132     btc_msg_t msg;
133     btc_spp_args_t arg;
134     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
135 
136     if (name == NULL || strlen(name) > ESP_SPP_SERVER_NAME_MAX) {
137         LOG_ERROR("Invalid server name!\n");
138         return ESP_ERR_INVALID_ARG;
139     }
140 
141     if (sec_mask != ESP_SPP_SEC_NONE && sec_mask != ESP_SPP_SEC_AUTHORIZE && sec_mask != ESP_SPP_SEC_AUTHENTICATE) {
142         LOG_WARN("Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only\n");
143     }
144 
145     msg.sig = BTC_SIG_API_CALL;
146     msg.pid = BTC_PID_SPP;
147     msg.act = BTC_SPP_ACT_START_SRV;
148 
149     arg.start_srv.sec_mask = sec_mask;
150     arg.start_srv.role = role;
151     arg.start_srv.local_scn = local_scn;
152     arg.start_srv.max_session = ESP_SPP_MAX_SESSION;
153     strcpy(arg.start_srv.name, name);
154 
155     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
156 }
157 
esp_spp_stop_srv(void)158 esp_err_t esp_spp_stop_srv(void)
159 {
160     btc_msg_t msg;
161     btc_spp_args_t arg;
162     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
163 
164     msg.sig = BTC_SIG_API_CALL;
165     msg.pid = BTC_PID_SPP;
166     msg.act = BTC_SPP_ACT_STOP_SRV;
167     arg.stop_srv.scn = BTC_SPP_INVALID_SCN;
168 
169     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
170 }
171 
esp_spp_stop_srv_scn(uint8_t scn)172 esp_err_t esp_spp_stop_srv_scn(uint8_t scn)
173 {
174     btc_msg_t msg;
175     btc_spp_args_t arg;
176     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
177 
178     if ((scn == 0) || (scn >= PORT_MAX_RFC_PORTS)) {
179         LOG_ERROR("Invalid SCN!\n");
180         return ESP_ERR_INVALID_ARG;
181     }
182 
183     msg.sig = BTC_SIG_API_CALL;
184     msg.pid = BTC_PID_SPP;
185     msg.act = BTC_SPP_ACT_STOP_SRV;
186     arg.stop_srv.scn = scn;
187 
188     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
189 }
190 
191 
esp_spp_write(uint32_t handle,int len,uint8_t * p_data)192 esp_err_t esp_spp_write(uint32_t handle, int len, uint8_t *p_data)
193 {
194     btc_msg_t msg;
195     btc_spp_args_t arg;
196     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
197 
198     msg.sig = BTC_SIG_API_CALL;
199     msg.pid = BTC_PID_SPP;
200     msg.act = BTC_SPP_ACT_WRITE;
201 
202     arg.write.handle = handle;
203     arg.write.len = len;
204     arg.write.p_data = p_data;
205 
206     return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), btc_spp_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
207 }
208 
esp_spp_vfs_register(void)209 esp_err_t esp_spp_vfs_register(void)
210 {
211     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
212 
213     return btc_spp_vfs_register();
214 }
215 
216 #endif ///defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE
217