2021-12-06 01:59:55 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-01-05 03:48:29 -05:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "esp_bt_main.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "btc/btc_manage.h"
|
2018-01-05 03:48:29 -05:00
|
|
|
|
|
|
|
#include "btc_spp.h"
|
|
|
|
#include "esp_spp_api.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_target.h"
|
2018-01-05 03:48:29 -05:00
|
|
|
|
|
|
|
#if (defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE)
|
|
|
|
|
2018-11-06 02:34:38 -05:00
|
|
|
static const uint8_t UUID_SPP[16] = {0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00,
|
|
|
|
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
|
|
|
|
};
|
2018-01-05 03:48:29 -05:00
|
|
|
static tSDP_UUID sdp_uuid;
|
2022-05-10 04:01:15 -04:00
|
|
|
|
|
|
|
esp_err_t esp_spp_register_callback(esp_spp_cb_t callback)
|
2018-01-05 03:48:29 -05:00
|
|
|
{
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
|
|
|
if (callback == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
btc_profile_cb_set(BTC_PID_SPP, callback);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t esp_spp_init(esp_spp_mode_t mode)
|
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_spp_args_t arg;
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_INIT;
|
|
|
|
|
2018-04-27 23:36:22 -04:00
|
|
|
arg.init.mode = mode;
|
2018-01-05 03:48:29 -05:00
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t esp_spp_deinit(void)
|
2018-01-05 03:48:29 -05:00
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_spp_args_t arg;
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_UNINIT;
|
|
|
|
|
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t esp_spp_start_discovery(esp_bd_addr_t bd_addr)
|
|
|
|
{
|
|
|
|
sdp_uuid.len = 16;
|
|
|
|
memcpy(sdp_uuid.uu.uuid128, UUID_SPP, sizeof(sdp_uuid.uu.uuid128));
|
|
|
|
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_spp_args_t arg;
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_START_DISCOVERY;
|
|
|
|
|
|
|
|
memcpy(arg.start_discovery.bd_addr, bd_addr, ESP_BD_ADDR_LEN);
|
|
|
|
arg.start_discovery.num_uuid = 1;
|
|
|
|
arg.start_discovery.p_uuid_list = &sdp_uuid;
|
|
|
|
|
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), btc_spp_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t 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)
|
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_spp_args_t arg;
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
2019-12-25 07:03:16 -05:00
|
|
|
if (sec_mask != ESP_SPP_SEC_NONE && sec_mask != ESP_SPP_SEC_AUTHORIZE && sec_mask != ESP_SPP_SEC_AUTHENTICATE) {
|
|
|
|
LOG_WARN("Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only\n");
|
2019-11-17 00:42:09 -05:00
|
|
|
}
|
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_CONNECT;
|
|
|
|
|
|
|
|
arg.connect.sec_mask = sec_mask;
|
|
|
|
arg.connect.role = role;
|
|
|
|
arg.connect.remote_scn = remote_scn;
|
|
|
|
memcpy(arg.connect.peer_bd_addr, peer_bd_addr, ESP_BD_ADDR_LEN);
|
|
|
|
|
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_spp_disconnect(uint32_t handle)
|
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_spp_args_t arg;
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_DISCONNECT;
|
|
|
|
|
|
|
|
arg.disconnect.handle = handle;
|
|
|
|
|
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_spp_start_srv(esp_spp_sec_t sec_mask,
|
|
|
|
esp_spp_role_t role, uint8_t local_scn, const char *name)
|
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_spp_args_t arg;
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
2020-11-03 02:18:08 -05:00
|
|
|
if (name == NULL || strlen(name) > ESP_SPP_SERVER_NAME_MAX) {
|
|
|
|
LOG_ERROR("Invalid server name!\n");
|
2018-01-05 03:48:29 -05:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-12-25 07:03:16 -05:00
|
|
|
if (sec_mask != ESP_SPP_SEC_NONE && sec_mask != ESP_SPP_SEC_AUTHORIZE && sec_mask != ESP_SPP_SEC_AUTHENTICATE) {
|
|
|
|
LOG_WARN("Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only\n");
|
2019-11-17 00:42:09 -05:00
|
|
|
}
|
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_START_SRV;
|
|
|
|
|
|
|
|
arg.start_srv.sec_mask = sec_mask;
|
|
|
|
arg.start_srv.role = role;
|
|
|
|
arg.start_srv.local_scn = local_scn;
|
|
|
|
arg.start_srv.max_session = ESP_SPP_MAX_SESSION;
|
|
|
|
strcpy(arg.start_srv.name, name);
|
|
|
|
|
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
|
|
|
}
|
|
|
|
|
2020-07-15 03:49:37 -04:00
|
|
|
esp_err_t esp_spp_stop_srv(void)
|
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
2020-11-03 02:18:08 -05:00
|
|
|
btc_spp_args_t arg;
|
2020-07-15 03:49:37 -04:00
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_STOP_SRV;
|
2020-11-03 02:18:08 -05:00
|
|
|
arg.stop_srv.scn = BTC_SPP_INVALID_SCN;
|
|
|
|
|
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_spp_stop_srv_scn(uint8_t scn)
|
|
|
|
{
|
|
|
|
btc_msg_t msg;
|
|
|
|
btc_spp_args_t arg;
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
2020-07-15 03:49:37 -04:00
|
|
|
|
2020-11-03 02:18:08 -05:00
|
|
|
if ((scn == 0) || (scn >= PORT_MAX_RFC_PORTS)) {
|
|
|
|
LOG_ERROR("Invalid SCN!\n");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.sig = BTC_SIG_API_CALL;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTC_SPP_ACT_STOP_SRV;
|
|
|
|
arg.stop_srv.scn = scn;
|
|
|
|
|
|
|
|
return (btc_transfer_context(&msg, &arg, sizeof(btc_spp_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
2020-07-15 03:49:37 -04:00
|
|
|
}
|
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
|
|
|
|
esp_err_t esp_spp_write(uint32_t handle, int len, uint8_t *p_data)
|
|
|
|
{
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
2022-10-18 02:21:39 -04:00
|
|
|
if (len <= 0 || p_data == NULL) {
|
|
|
|
LOG_ERROR("Invalid data or len!\n");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2022-10-24 08:03:53 -04:00
|
|
|
return spp_send_data_to_btc(handle, len, p_data, ESP_SPP_MODE_CB);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t esp_spp_vfs_register(void)
|
2018-04-27 23:36:22 -04:00
|
|
|
{
|
|
|
|
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
|
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
return btc_spp_vfs_register();
|
2018-04-27 23:36:22 -04:00
|
|
|
}
|
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
#endif ///defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE
|