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,
|
2016-11-24 18:10:15 +00:00
|
|
|
* and the client will get the BAS value. It calls the API bta
|
2016-10-07 03:16:13 +00:00
|
|
|
* layer provides.
|
|
|
|
*
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "controller.h"
|
|
|
|
|
|
|
|
#include "bt_trace.h"
|
|
|
|
#include "bt_types.h"
|
|
|
|
|
2016-10-24 09:08:37 +00:00
|
|
|
#include "blufi.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-16 14:11:59 +00:00
|
|
|
#include "esp_blufi_api.h"
|
2016-11-06 18:16:00 +00:00
|
|
|
|
2016-11-16 14:11:59 +00:00
|
|
|
extern void wifi_set_blue_config(char *ssid, char *passwd);
|
2016-10-24 09:08:37 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
#define HEADER_SSID "ssid"
|
|
|
|
#define HEADER_PASSWD "passwd"
|
|
|
|
#define HEADER_CONFIRM "confirm"
|
2016-10-07 03:16:13 +00:00
|
|
|
|
2016-11-16 14:11:59 +00:00
|
|
|
static char tmp_ssid[32 + 1];
|
|
|
|
static char tmp_passwd[64 + 1];
|
|
|
|
|
|
|
|
static void blufi_data_recv(uint8_t *data, int len)
|
2016-10-07 03:16:13 +00:00
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
char *p = NULL;
|
|
|
|
LOG_DEBUG("the data is:%s\n", data);
|
|
|
|
|
2016-12-07 06:11:40 +00:00
|
|
|
p = strstr((char *)data, HEADER_SSID);
|
2016-11-24 18:10:15 +00:00
|
|
|
if (p) {
|
|
|
|
LOG_ERROR("SSID: %s\n", p + strlen(HEADER_SSID) + 1);
|
|
|
|
strcpy(tmp_ssid, p + strlen(HEADER_SSID) + 1);
|
|
|
|
}
|
2016-12-07 06:11:40 +00:00
|
|
|
p = strstr((char *)data, HEADER_PASSWD);
|
2016-11-24 18:10:15 +00:00
|
|
|
if (p) {
|
|
|
|
LOG_ERROR("PASSWORD: %s\n", p + strlen(HEADER_PASSWD) + 1);
|
|
|
|
strcpy(tmp_passwd, p + strlen(HEADER_PASSWD) + 1);
|
|
|
|
}
|
2016-12-07 06:11:40 +00:00
|
|
|
p = strstr((char *)data, HEADER_CONFIRM);
|
2016-11-24 18:10:15 +00:00
|
|
|
if (p) {
|
|
|
|
LOG_ERROR("CONFIRM\n");
|
|
|
|
wifi_set_blue_config(tmp_ssid, tmp_passwd);
|
|
|
|
}
|
|
|
|
|
2016-10-07 03:16:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 14:11:59 +00:00
|
|
|
static void blufi_callback(uint32_t event, void *param)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
/* actually, should post to blufi_task handle the procedure,
|
|
|
|
* now, as a demo, we do simplely */
|
|
|
|
switch (event) {
|
|
|
|
case ESP_BLUFI_EVENT_INIT_FINISH:
|
|
|
|
LOG_ERROR("blufi init finish\n");
|
|
|
|
break;
|
|
|
|
case ESP_BLUFI_EVENT_RECV_DATA: {
|
|
|
|
LOG_DEBUG("blufi recv data\n");
|
|
|
|
esp_blufi_cb_param_t *blufi_param = (esp_blufi_cb_param_t *)param;
|
|
|
|
blufi_data_recv(blufi_param->recv_data.data, blufi_param->recv_data.data_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-11-16 14:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t blufi_startup_in_blufi_task(void *arg)
|
2016-10-24 09:08:37 +00:00
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
/*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);
|
2016-11-16 14:11:59 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
esp_blufi_register_callback(blufi_callback);
|
|
|
|
esp_blufi_profile_init();
|
2016-10-24 09:08:37 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return ESP_OK;
|
2016-10-24 09:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-16 14:11:59 +00:00
|
|
|
static void blufi_startup(void)
|
2016-10-24 09:08:37 +00:00
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
blufi_transfer_context(blufi_startup_in_blufi_task, NULL);
|
2016-10-24 09:08:37 +00:00
|
|
|
}
|
|
|
|
|
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-24 18:10:15 +00:00
|
|
|
esp_err_t err;
|
2016-11-06 18:16:00 +00:00
|
|
|
|
2016-10-26 07:57:40 +00:00
|
|
|
BTM_SetTraceLevel(BT_TRACE_LEVEL_ERROR);
|
2016-10-07 03:16:13 +00:00
|
|
|
|
2016-11-16 08:26:02 +00:00
|
|
|
err = esp_enable_bluetooth();
|
2016-11-24 18:10:15 +00:00
|
|
|
if (err) {
|
|
|
|
LOG_ERROR("%s failed\n", __func__);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
blufi_startup();
|
2016-10-24 09:08:37 +00:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-11-06 18:16:00 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return err;
|
2016-11-06 18:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t blufi_disable(void *arg)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
esp_err_t err;
|
2016-11-06 18:16:00 +00:00
|
|
|
|
|
|
|
err = esp_disable_bluetooth();
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (arg) {
|
|
|
|
((void (*)(void))arg)();
|
|
|
|
}
|
2016-11-06 18:16:00 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return err;
|
2016-10-07 03:16:13 +00:00
|
|
|
}
|