Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New files to support Bluetooth Service on ESP32
Based on Gordons last changes to support easy poritng of Bluetooth for other boards
- Loading branch information
jumjum123
committed
Dec 8, 2017
1 parent
75915ec
commit 4ef6eb0
Showing
18 changed files
with
970 additions
and
1,010 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers | ||
* | ||
* Copyright (C) 2017 Gordon Williams <gw@pur3.co.uk> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* ---------------------------------------------------------------------------- | ||
* ESP32 specific Bluetooth utils | ||
* ---------------------------------------------------------------------------- | ||
*/ | ||
|
||
#include "BLE/esp32_bluetooth_utils.h" | ||
|
||
JsVar *bda2JsVarString(esp_bd_addr_t bda){ | ||
JsVar *s = jsvVarPrintf("%02x:%02x:%02x:%02x:%02x:%02x",bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]); | ||
return s; | ||
} | ||
|
||
static char *gattsEvent2String(esp_gatts_cb_event_t event){ | ||
switch(event){ | ||
case 0: return "REG"; | ||
case 1: return "READ"; | ||
case 2: return "WRITE"; | ||
case 3: return "EXEC_WRITE"; | ||
case 4: return "MTU"; | ||
case 5: return "CONF"; | ||
case 6: return "UNREG"; | ||
case 7: return "CREATE"; | ||
case 8: return "ADD_INCL_SRVC"; | ||
case 9: return "ADD_CHAR"; | ||
case 10: return "ADD_CHAR_DESCR"; | ||
case 11: return "DELETE"; | ||
case 12: return "START"; | ||
case 13: return "STOP"; | ||
case 14: return "CONNECT"; | ||
case 15: return "DISCONNECT"; | ||
case 16: return "OPEN"; | ||
case 17: return "CANCEL_OPEN"; | ||
case 18: return "CLOSE"; | ||
case 19: return "LISTEN"; | ||
case 20: return "CONGEST"; | ||
case 21: return "RESPONSE"; | ||
case 22: return "CREAT_ATTR_TAB"; | ||
case 23: return "CREAT_ATTR_TAB"; | ||
} | ||
return "unknown GattsEvent"; | ||
} | ||
static char *gapEvent2String(esp_gap_ble_cb_event_t event){ | ||
switch(event){ | ||
case 0: return "ADV_DATA_SET_COMPLETE"; | ||
case 1: return "SCAN_RSP_DATA_SET_COMPLETE"; | ||
case 2: return "SCAN_PARAM_SET_COMPLETE"; | ||
case 3: return "SCAN_RESULT"; | ||
case 4: return "ADV_DATA_RAW_SET_COMPLETE"; | ||
case 5: return "SCAN_RSP_DATA_RAW_SET_COMPLETE"; | ||
case 6: return "ADV_START_COMPLETE"; | ||
case 7: return "SCAN_START_COMPLETE"; | ||
case 8: return "AUTH_CMPL"; | ||
case 9: return "KEY"; | ||
case 10: return "SEC_REQ"; | ||
case 11: return "PASSKEY_NOTIF"; | ||
case 12: return "PASSKEY_REQ"; | ||
case 13: return "OOB_REQ"; | ||
case 14: return "LOCAL_IR"; | ||
case 15: return "LOCAL_ER"; | ||
case 16: return "NC_REQ"; | ||
case 17: return "ADV_STOP_COMPLETE"; | ||
case 18: return "SCAN_STOP_COMPLETE"; | ||
case 19: return "SET_STATIC_RAND_ADDR"; | ||
case 20: return "UPDATE_CONN_PARAMS"; | ||
case 21: return "SET_PKT_LENGTH_COMPLETE"; | ||
case 22: return "SET_LOCAL_PRIVACY_COMPLETE"; | ||
case 23: return "REMOVE_BOND_DEV_COMPLETE"; | ||
case 24: return "CLEAR_BOND_DEV_COMPLETE"; | ||
case 25: return "GET_BOND_DEV_COMPLETE_EVT"; | ||
case 26: return "READ_RSSI_COMPLETE"; | ||
} | ||
return "unknown GapEvent"; | ||
} | ||
void jsWarnGattsEvent(esp_gatts_cb_event_t event,esp_gatt_if_t gatts_if){ | ||
jsWarn("Event:ESP_GATTS_%s_EVT gatts_if:%d\n",gattsEvent2String(event), gatts_if); | ||
} | ||
void jsWarnGapEvent(esp_gap_ble_cb_event_t event){ | ||
jsWarn("Event:ESP_GAP_BLE_%s_EVT\n",gapEvent2String(event)); | ||
} | ||
|
||
void jsWarnUUID(esp_bt_uuid_t char_uuid){ | ||
if (char_uuid.len == ESP_UUID_LEN_16) { | ||
jsWarn("- - - Char UUID16: %x", char_uuid.uuid.uuid16); | ||
} else if (char_uuid.len == ESP_UUID_LEN_32) { | ||
jsWarn("- - - Char UUID32: %x", char_uuid.uuid.uuid32); | ||
} else if (char_uuid.len == ESP_UUID_LEN_128) { | ||
jsWarn("- - - Char UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", char_uuid.uuid.uuid128[0], | ||
char_uuid.uuid.uuid128[1], char_uuid.uuid.uuid128[2], char_uuid.uuid.uuid128[3], | ||
char_uuid.uuid.uuid128[4], char_uuid.uuid.uuid128[5], char_uuid.uuid.uuid128[6], | ||
char_uuid.uuid.uuid128[7], char_uuid.uuid.uuid128[8], char_uuid.uuid.uuid128[9], | ||
char_uuid.uuid.uuid128[10], char_uuid.uuid.uuid128[11], char_uuid.uuid.uuid128[12], | ||
char_uuid.uuid.uuid128[13], char_uuid.uuid.uuid128[14], char_uuid.uuid.uuid128[15]); | ||
} else { | ||
jsWarn("- - - Char UNKNOWN LEN %d\n", char_uuid.len); | ||
} | ||
} | ||
|
||
void bleGetHiddenName(char *eventName, char *hiddenName, uint16_t pos){ | ||
strcpy(eventName,hiddenName); | ||
itostr(pos,&eventName[strlen(eventName)],16); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers | ||
* | ||
* Copyright (C) 2017 Gordon Williams <gw@pur3.co.uk> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* ---------------------------------------------------------------------------- | ||
* ESP32 specific Bluetooth utils | ||
* ---------------------------------------------------------------------------- | ||
*/ | ||
#ifndef ESP32_BLUETOOTH_UTILS_H_ | ||
#define ESP32_BLUETOOTH_UTILS_H_ | ||
|
||
#include "jsvar.h" | ||
|
||
#include "esp_bt_defs.h" | ||
#include "esp_gatts_api.h" | ||
#include "esp_gap_ble_api.h" | ||
|
||
#define BLE_WRITE_EVENT JS_EVENT_PREFIX"blewv" | ||
#define BLE_READ_EVENT JS_EVENT_PREFIX"blerv" | ||
#define BLE_CONNECT_EVENT JS_EVENT_PREFIX"connect" | ||
#define BLE_DISCONNECT_EVENT JS_EVENT_PREFIX"disconnect" | ||
|
||
#define BLE_CHAR_VALUE "BLE_CHAR_V" | ||
|
||
JsVar *bda2JsVarString(uint8_t *ble_adv); | ||
|
||
void jsWarnGattsEvent(esp_gatts_cb_event_t event,esp_gatt_if_t gatts_if); | ||
void jsWarnGapEvent(esp_gap_ble_cb_event_t event); | ||
void jsWarnUUID(esp_bt_uuid_t char_uuid); | ||
|
||
void bleGetHiddenName(char *eventName, char *hiddenName, uint16_t pos); | ||
|
||
#endif /* ESP32_BLUETOOTH_UTILS_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/* | ||
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers | ||
* | ||
* Copyright (C) 2017 Gordon Williams <gw@pur3.co.uk> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* ---------------------------------------------------------------------------- | ||
* ESP32 specific GAP functions | ||
* ---------------------------------------------------------------------------- | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "BLE/esp32_gap_func.h" | ||
#include "BLE/esp32_gatts_func.h" | ||
#include "BLE/esp32_bluetooth_utils.h" | ||
|
||
#include "jsutils.h" | ||
#include "jsparse.h" | ||
#include "jsinteractive.h" | ||
#include "bluetooth_utils.h" | ||
|
||
#define adv_config_flag (1 << 0) | ||
#define scan_rsp_config_flag (1 << 1) | ||
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] | ||
#define GAP_SCAN_FUNC "gap_scan_func" | ||
|
||
static uint8_t adv_config_done = 0; | ||
|
||
static esp_ble_adv_params_t adv_params = { | ||
.adv_int_min = 0x20, | ||
.adv_int_max = 0x40, | ||
.adv_type = ADV_TYPE_IND, | ||
.own_addr_type = BLE_ADDR_TYPE_PUBLIC, | ||
//.peer_addr = | ||
//.peer_addr_type = | ||
.channel_map = ADV_CHNL_ALL, | ||
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, | ||
}; | ||
|
||
static esp_ble_scan_params_t ble_scan_params = { | ||
.scan_type = BLE_SCAN_TYPE_ACTIVE, | ||
.own_addr_type = BLE_ADDR_TYPE_PUBLIC, | ||
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL, | ||
.scan_interval = 0x50, | ||
.scan_window = 0x30 | ||
}; | ||
|
||
static esp_ble_adv_data_t adv_data = { | ||
.set_scan_rsp = false, | ||
.include_name = true, | ||
.include_txpower = true, | ||
.min_interval = 0x20, | ||
.max_interval = 0x40, | ||
.appearance = 0x00, | ||
.manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN, | ||
.p_manufacturer_data = NULL, //&test_manufacturer[0], | ||
.service_data_len = 0, | ||
.p_service_data = NULL, | ||
.service_uuid_len = 0, //needs to be set before used | ||
.p_service_uuid = &adv_service_uuid128, | ||
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT), | ||
}; | ||
|
||
static void execScanFunc(esp_ble_gap_cb_param_t *p){ | ||
JsVar *evt = jsvNewObject(); | ||
jsvObjectSetChildAndUnLock(evt, "id", bda2JsVarString(p->scan_rst.bda)); | ||
jsvObjectSetChildAndUnLock(evt, "rssi",jsvNewFromInteger(p->scan_rst.rssi)); | ||
JsVar *data = jsvNewStringOfLength(p->scan_rst.adv_data_len, (char*)p->scan_rst.ble_adv); | ||
if(data){ | ||
JsVar *ab = jsvNewArrayBufferFromString(data,p->scan_rst.adv_data_len); | ||
jsvUnLock(data); | ||
jsvObjectSetChildAndUnLock(evt,"data",ab); | ||
} | ||
jsiQueueObjectCallbacks(execInfo.root, BLE_SCAN_EVENT, &evt,1); | ||
jsvUnLock(evt); | ||
jshHadEvent(); | ||
} | ||
|
||
void gap_event_scan_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param){ | ||
uint8_t *adv_name = NULL; | ||
uint8_t adv_name_len = 0; | ||
esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param; | ||
switch(event){ | ||
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: { | ||
} | ||
break; | ||
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:{ | ||
if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {jsWarn("Scan start failed:d\n",param->scan_start_cmpl.status);} | ||
} | ||
break; | ||
case ESP_GAP_BLE_SCAN_RESULT_EVT:{ | ||
execScanFunc(param); | ||
} | ||
break; | ||
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:{ | ||
if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){jsWarn("Scan stop failed");} | ||
else {jsWarn("Stop scan successfully");} | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param){ | ||
// jsWarnGapEvent(event); | ||
switch (event) { | ||
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:{ | ||
adv_config_done &= (~adv_config_flag); | ||
if (adv_config_done == 0){ | ||
esp_ble_gap_start_advertising(&adv_params); | ||
} | ||
} | ||
break; | ||
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:{ | ||
adv_config_done &= (~scan_rsp_config_flag); | ||
if (adv_config_done == 0){ | ||
esp_ble_gap_start_advertising(&adv_params); | ||
} | ||
} | ||
break; | ||
|
||
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:{ | ||
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { | ||
jsWarn("Advertising start failed\n"); | ||
} | ||
} | ||
break; | ||
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:{ | ||
if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS) { | ||
jsWarn("Advertising stop failed\n"); | ||
} | ||
else { | ||
jsWarn("Stop adv successfully\n"); | ||
} | ||
} | ||
break; | ||
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:{ | ||
jsWarn("update connetion params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d", | ||
param->update_conn_params.status, | ||
param->update_conn_params.min_int, | ||
param->update_conn_params.max_int, | ||
param->update_conn_params.conn_int, | ||
param->update_conn_params.latency, | ||
param->update_conn_params.timeout); | ||
} | ||
break; | ||
default: | ||
gap_event_scan_handler(event,param); | ||
break; | ||
} | ||
} | ||
|
||
void bluetooth_gap_setScan(bool enable){ | ||
esp_err_t status; | ||
status = esp_ble_gap_set_scan_params(&ble_scan_params); | ||
if (status){ jsWarn("gap set scan error code = %x", status);return;} | ||
if(enable == true){ | ||
status = esp_ble_gap_start_scanning(30); | ||
if (status != ESP_OK) jsWarn("esp_ble_gap_start_scanning: rc=%d", status); | ||
} | ||
else{ | ||
status = esp_ble_gap_stop_scanning(); | ||
} | ||
} | ||
|
||
esp_err_t bluetooth_gap_startAdvertizing(bool enable){ | ||
if(enable){ | ||
return esp_ble_gap_start_advertising(&adv_params); | ||
} | ||
else{ | ||
return esp_ble_gap_stop_advertising(); | ||
} | ||
} | ||
|
||
esp_err_t bluetooth_gap_setAdvertizing(){ | ||
adv_data.service_uuid_len = ble_service_cnt * 16; | ||
esp_err_t ret = esp_ble_gap_config_adv_data(&adv_data); | ||
if (ret){ | ||
jsWarn("config adv data failed, error code = %x", ret); | ||
} | ||
return ret; | ||
} | ||
|
||
esp_err_t bluetooth_setDeviceName(uint8_t *deviceName){ | ||
return esp_ble_gap_set_device_name(deviceName); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers | ||
* | ||
* Copyright (C) 2017 Gordon Williams <gw@pur3.co.uk> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* ---------------------------------------------------------------------------- | ||
* ESP32 specific GAP functions | ||
* ---------------------------------------------------------------------------- | ||
*/ | ||
#ifndef GAP_FUNC_H_ | ||
#define GAP_FUNC_H_ | ||
|
||
#include "esp_gap_ble_api.h" | ||
|
||
#include "jsvar.h" | ||
|
||
void bluetooth_gap_setScan(bool enabled); | ||
|
||
esp_err_t bluetooth_setDeviceName(uint8_t *deviceName); | ||
|
||
esp_err_t bluetooth_gap_startAdvertizing(bool enable); | ||
esp_err_t bluetooth_gap_setAdvertizing(); | ||
|
||
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); | ||
|
||
#endif /* GAP_FUNC_H_ */ |
Oops, something went wrong.