2016-11-04 18:48:16 +00:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-10-07 03:16:13 +00:00
|
|
|
/***************************************************************
|
|
|
|
*
|
|
|
|
* This file is for gatt server device. It instantiates BATTERY
|
|
|
|
* sevice. It can be scanned and connected by central device,
|
|
|
|
* and the client will get the BAS value. It calls the API bta
|
|
|
|
* layer provides.
|
|
|
|
*
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "bta_api.h"
|
|
|
|
#include "bta_gatt_api.h"
|
|
|
|
#include "controller.h"
|
|
|
|
|
|
|
|
#include "gatt_int.h"
|
|
|
|
#include "bt_trace.h"
|
|
|
|
#include "btm_api.h"
|
|
|
|
#include "bt_types.h"
|
|
|
|
|
2016-10-24 09:08:37 +00:00
|
|
|
#include "blufi_prf.h"
|
|
|
|
|
|
|
|
#include "blufi.h"
|
|
|
|
#include "blufi_adv.h"
|
|
|
|
|
2016-11-15 12:56:15 +00:00
|
|
|
#include "esp_bt_defs.h"
|
2016-11-16 03:28:41 +00:00
|
|
|
#include "esp_bt_main.h"
|
2016-11-06 18:16:00 +00:00
|
|
|
|
2016-10-24 12:07:19 +00:00
|
|
|
static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_data);
|
2016-10-24 09:08:37 +00:00
|
|
|
|
|
|
|
struct dm_evt {
|
|
|
|
tBTA_DM_SEC_EVT event;
|
|
|
|
tBTA_DM_SEC* p_data;
|
|
|
|
};
|
|
|
|
|
2016-10-07 03:16:13 +00:00
|
|
|
#define HEADER_SSID "ssid"
|
|
|
|
#define HEADER_PASSWD "passwd"
|
|
|
|
#define HEADER_CONFIRM "confirm"
|
|
|
|
extern void wifi_set_blue_config(char *ssid, char *passwd);
|
|
|
|
static char tmp_ssid[33];
|
|
|
|
static char tmp_passwd[33];
|
|
|
|
|
2016-10-24 12:07:19 +00:00
|
|
|
static void BlufiDataCallBack(UINT8 app_id, UINT8 event, UINT8 len, UINT8 *p_data)
|
2016-10-07 03:16:13 +00:00
|
|
|
{
|
|
|
|
char *p = NULL;
|
2016-10-26 07:57:40 +00:00
|
|
|
LOG_DEBUG("the data is:%s\n", p_data);
|
2016-10-07 03:16:13 +00:00
|
|
|
#if 0
|
|
|
|
switch(event)
|
|
|
|
{
|
|
|
|
case RECEIVE_NET_PASSWD_EVT:
|
|
|
|
LOG_ERROR("Received the network passwork");
|
|
|
|
break;
|
|
|
|
case RECEIVE_NET_SSD_EVT:
|
|
|
|
LOG_ERROR("Received the network SSID");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
p = strstr(p_data, HEADER_SSID);
|
|
|
|
if (p) {
|
2016-10-26 07:57:40 +00:00
|
|
|
LOG_ERROR("SSID: %s\n", p+strlen(HEADER_SSID)+1);
|
2016-10-07 03:16:13 +00:00
|
|
|
strcpy(tmp_ssid, p+strlen(HEADER_SSID)+1);
|
|
|
|
}
|
|
|
|
p = strstr(p_data, HEADER_PASSWD);
|
|
|
|
if (p) {
|
2016-10-26 07:57:40 +00:00
|
|
|
LOG_ERROR("PASSWORD: %s\n", p+strlen(HEADER_PASSWD)+1);
|
2016-10-07 03:16:13 +00:00
|
|
|
strcpy(tmp_passwd, p+strlen(HEADER_PASSWD)+1);
|
|
|
|
}
|
|
|
|
p = strstr(p_data, HEADER_CONFIRM);
|
|
|
|
if (p) {
|
2016-10-26 07:57:40 +00:00
|
|
|
LOG_ERROR("CONFIRM\n");
|
2016-10-07 03:16:13 +00:00
|
|
|
wifi_set_blue_config(tmp_ssid, tmp_passwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-06 18:16:00 +00:00
|
|
|
static esp_err_t blufi_dm_upstreams_evt(void *arg)
|
2016-10-24 09:08:37 +00:00
|
|
|
{
|
|
|
|
struct dm_evt *evt = (struct dm_evt *)arg;
|
|
|
|
|
|
|
|
tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)evt->p_data;
|
|
|
|
switch (evt->event) {
|
|
|
|
case BTA_DM_ENABLE_EVT: {
|
|
|
|
/*set connectable,discoverable, pairable and paired only modes of local device*/
|
|
|
|
tBTA_DM_DISC disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE;
|
|
|
|
tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE;
|
|
|
|
BTA_DmSetVisibility(disc_mode, conn_mode, (uint8_t)BTA_DM_NON_PAIRABLE, (uint8_t)BTA_DM_CONN_ALL );
|
|
|
|
|
|
|
|
#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
|
|
|
|
/* Enable local privacy */
|
|
|
|
//BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
|
|
|
|
do {
|
|
|
|
const controller_t *controller = controller_get_interface();
|
|
|
|
char bdstr[18];
|
|
|
|
bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr));
|
2016-10-26 07:57:40 +00:00
|
|
|
LOG_DEBUG("BDA is: %s\n", bdstr);
|
2016-10-24 09:08:37 +00:00
|
|
|
} while (0);
|
|
|
|
#endif
|
2016-10-24 12:07:19 +00:00
|
|
|
blufi_profile_init(BlufiDataCallBack);
|
2016-10-24 09:08:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GKI_freebuf(evt);
|
|
|
|
|
2016-11-06 18:16:00 +00:00
|
|
|
return ESP_OK;
|
2016-10-24 09:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void blufi_bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data)
|
|
|
|
{
|
|
|
|
struct dm_evt *evt;
|
|
|
|
|
2016-10-26 07:57:40 +00:00
|
|
|
LOG_DEBUG("%s: %d\n", __func__, (uint16_t)event);
|
2016-10-24 09:08:37 +00:00
|
|
|
|
|
|
|
evt = (struct dm_evt *)GKI_getbuf(sizeof(struct dm_evt));
|
|
|
|
if (evt == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
evt->event = event;
|
|
|
|
evt->p_data = p_data;
|
|
|
|
|
|
|
|
blufi_transfer_context(blufi_dm_upstreams_evt, evt);
|
|
|
|
}
|
|
|
|
|
2016-11-06 18:16:00 +00:00
|
|
|
esp_err_t blufi_enable(void *arg)
|
2016-10-07 03:16:13 +00:00
|
|
|
{
|
2016-11-06 18:16:00 +00:00
|
|
|
esp_err_t err;
|
|
|
|
|
2016-10-26 07:57:40 +00:00
|
|
|
BTM_SetTraceLevel(BT_TRACE_LEVEL_ERROR);
|
2016-10-07 03:16:13 +00:00
|
|
|
|
2016-11-06 18:16:00 +00:00
|
|
|
err = esp_enable_bluetooth(blufi_bte_dm_evt);
|
2016-10-24 09:08:37 +00:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-11-06 18:16:00 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t blufi_disable(void *arg)
|
|
|
|
{
|
|
|
|
esp_err_t err;
|
|
|
|
|
|
|
|
err = esp_disable_bluetooth();
|
|
|
|
|
|
|
|
if (arg) {
|
|
|
|
((void (*)(void))arg)();
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2016-10-07 03:16:13 +00:00
|
|
|
}
|