adding pairing display example

This commit is contained in:
hathach committed 2020-02-05 14:57:42 +07:00
1 parent 2c02b37d91
commit ee29722b69
5 files changed
+367 -34

No files matched your search

@@ -0,0 +1,273 @@
/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <bluefruit.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
/* This sketch demonstrates Pairing process using static Passkey aka PIN.
* This sketch is essentially the same as bleuart.ino except the BLE Uart
* service requires Security Mode with Man-In-The-Middle protection i.e
* using 6 digits PIN for pairing.
*/
/* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App.
* Following TFT Display are supported
* - https://www.adafruit.com/product/3315
* - https://www.adafruit.com/product/3651
* - https://www.adafruit.com/product/4367
*/
#define TFT_NONE 0
#define TFT_35_FEATHERWING 1
#define TFT_24_FEATHERWING 2
#define TFT_GIZMO 3
// [Configurable] Please select one of above supported Display to match your hardware setup
#define TFT_IN_USE TFT_NONE
#if defined(ARDUINO_NRF52832_FEATHER)
// Feather nRF52832
#define TFT_DC 11
#define TFT_CS 31
#elif defined(ARDUINO_NRF52840_CIRCUITPLAY)
// Circuit Playground Bluefruit for use with TFT 1.5" GIZMO
#define TFT_DC 1
#define TFT_CS 0
#define TFT_BACKLIGHT A3
#else
// Default for others
#define TFT_DC 10
#define TFT_CS 9
#endif
#if TFT_IN_USE == TFT_35_FEATHERWING
#include "Adafruit_HX8357.h"
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);
#elif TFT_IN_USE == TFT_24_FEATHERWING
#include <Adafruit_ILI9341.h>
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#elif TFT_IN_USE == TFT_GIZMO
#include "Adafruit_ST7789.h"
Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, TFT_CS, TFT_DC, -1);
#endif
#define COLOR_WHITE 0xFFFF
#define COLOR_BLACK 0x0000
#define COLOR_YELLOW 0xFFE0
#define COLOR_GREEN 0x07E0
#define COLOR_RED 0xF800
// BLE Service
BLEUart bleuart; // uart over ble
void setup()
{
Serial.begin(115200);
#if TFT_IN_USE == TFT_GIZMO
tft.init(240, 240);
tft.setRotation(2);
pinMode(TFT_BACKLIGHT, OUTPUT);
digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on
#elif TFT_IN_USE != TFT_NONE
tft.begin();
#endif
Serial.println("Bluefruit52 BLEUART Example");
Serial.println("---------------------------\n");
// Setup the BLE LED to be enabled on CONNECT
// Note: This is actually the default behavior, but provided
// here in case you want to control this LED manually via PIN 19
Bluefruit.autoConnLed(true);
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin();
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName("Bluefruit52");
// Serial.println("Setting pairing PIN to: " PAIRING_PIN);
Bluefruit.setPairingDisplayCallback(pairing_display_callback);
Bluefruit.setPairingCompleteCallback(pairing_complete_callback);
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// Configure and Start BLE Uart Service
// Set Permission to access BLE Uart is to require man-in-the-middle protection
// This will cause central to perform pairing with static PIN we set above
Serial.println("Configure BLE Uart to require man-in-the-middle protection for PIN pairing");
bleuart.setPermission(SECMODE_ENC_WITH_MITM);
bleuart.begin();
// Set up and start advertising
startAdv();
Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
Serial.println("Your phone should pop-up PIN input");
Serial.println("Once connected, enter character(s) that you wish to send");
#if TFT_IN_USE != TFT_NONE
tft.fillScreen(COLOR_BLACK);
tft.setTextColor(COLOR_WHITE);
tft.setTextSize(2);
tft.println("Advertising ... ");
#endif
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart);
// Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet
Bluefruit.ScanResponse.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void loop()
{
// Forward data from HW Serial to BLEUART
while (Serial.available())
{
// Delay to wait for enough input, since we have a limited transmission buffer
delay(2);
uint8_t buf[64];
int count = Serial.readBytes(buf, sizeof(buf));
bleuart.write( buf, count );
}
// Forward from BLEUART to HW Serial
while ( bleuart.available() )
{
uint8_t ch;
ch = (uint8_t) bleuart.read();
Serial.write(ch);
}
}
void pairing_display_callback(uint16_t conn_handle, uint8_t const passkey[6])
{
Serial.println("Enter this code on your phone to pair with Bluefruit:");
Serial.printf(" %.6s\n", passkey);
#if TFT_IN_USE != TFT_NONE
tft.printf("Enter this code on your phone to pair with Bluefruit:\n\n");
tft.setTextColor(COLOR_YELLOW);
tft.setTextSize(4);
tft.printf(" %.6s\n", passkey);
tft.setTextColor(COLOR_WHITE);
tft.setTextSize(2);
#endif
}
void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
{
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
{
Serial.println("Succeeded");
}else
{
Serial.println("Failed");
}
#if TFT_IN_USE != TFT_NONE
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
{
tft.setTextColor(COLOR_GREEN);
tft.println("Succeeded");
}else
{
tft.setTextColor(COLOR_RED);
tft.println("Failed");
}
tft.setTextColor(COLOR_WHITE);
tft.setTextSize(2);
#endif
}
// callback invoked when central connects
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
#if TFT_IN_USE != TFT_NONE
tft.fillScreen(COLOR_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.println("Connected");
#endif
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle connection where this event happens
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.println();
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
#if TFT_IN_USE != TFT_NONE
tft.println("Advertising ...");
#endif
}
@@ -37,7 +37,7 @@ void setup()
Serial.println("---------------------------\n");
// Setup the BLE LED to be enabled on CONNECT
// Note: This is actually the default behaviour, but provided
// Note: This is actually the default behavior, but provided
// here in case you want to control this LED manually via PIN 19
Bluefruit.autoConnLed(true);
+21 -22
View File
@@ -304,7 +304,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
//--------------------------------------------------------------------+
/* First-time Pairing
* Connect -> SEC_PARAMS_REQUEST -> CONN_SEC_UPDATE -> AUTH_STATUS
* Connect -> SEC_PARAMS_REQUEST -> PASSKEY_DISPLAY -> CONN_SEC_UPDATE -> AUTH_STATUS
* 1. Either we or peer initiate the process
* 2. Peer ask for Secure Parameter ( I/O Caps ) BLE_GAP_EVT_SEC_PARAMS_REQUEST
* 3. Pair Key exchange ( PIN code)
@@ -331,15 +331,10 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
* - Central supplies its parameters
* - We replies with our security parameters
*/
// ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
COMMENT_OUT(
// Change security parameter according to authentication type
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
{
sec_para.mitm = 1;
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
}
)
ble_gap_sec_params_t const* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
(void) peer;
LOG_LV2("PAIR", "Peer Params: bond = %d, mitm = %d, lesc = %d, io_caps = %d",
peer->bond, peer->mitm, peer->lesc, peer->io_caps);
ble_gap_sec_keyset_t keyset =
{
@@ -372,6 +367,8 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
// Pairing process completed
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
LOG_LV2("PAIR", "Auth Status = 0x%02x (BLE_GAP_SEC_STATUS)", status->auth_status);
// Pairing succeeded --> save encryption keys ( Bonding )
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
{
@@ -379,9 +376,6 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
_ediv = _bond_keys->own_enc.master_id.ediv;
bond_save_keys(_role, _conn_hdl, _bond_keys);
}else
{
PRINT_HEX(status->auth_status);
}
rtos_free(_bond_keys);
@@ -411,15 +405,20 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
}
break;
case BLE_GAP_EVT_PASSKEY_DISPLAY:
{
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
// PRINT_INT(passkey_display->match_request);
// PRINT_BUFFER(passkey_display->passkey, 6);
// sd_ble_gap_auth_key_reply
}
break;
// case BLE_GAP_EVT_PASSKEY_DISPLAY:
// {
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
// LOG_LV2("PAIR", "Passkey = %.6s, match request = %d", passkey_display->passkey, passkey_display->match_request);
//
// if ( Bluefruit._pair_display_cb ) ada_callback(passkey_display->passkey, 6, )
//
// if (passkey_display->match_request)
// {
// // Match request require to report the match
// // sd_ble_gap_auth_key_reply();
// }
// }
// break;
case BLE_GAP_EVT_CONN_SEC_UPDATE:
{
+59 -6
View File
@@ -175,6 +175,7 @@ AdafruitBluefruit::AdafruitBluefruit(void)
_event_cb = NULL;
_rssi_cb = NULL;
_pair_display_cb = NULL;
_sec_param = ((ble_gap_sec_params_t)
{
@@ -621,7 +622,7 @@ bool AdafruitBluefruit::disconnect(uint16_t conn_hdl)
return true; // not connected still return true
}
void AdafruitBluefruit::setEventCallback ( void (*fp) (ble_evt_t*) )
void AdafruitBluefruit::setEventCallback (event_cb_t fp)
{
_event_cb = fp;
}
@@ -647,7 +648,7 @@ BLEConnection* AdafruitBluefruit::Connection(uint16_t conn_hdl)
return (conn_hdl < BLE_MAX_CONNECTION) ? _connection[conn_hdl] : NULL;
}
void AdafruitBluefruit::setRssiCallback(rssi_callback_t fp)
void AdafruitBluefruit::setRssiCallback(rssi_cb_t fp)
{
_rssi_cb = fp;
}
@@ -659,9 +660,9 @@ bool AdafruitBluefruit::setPIN(const char* pin)
// back to open mode
if (pin == NULL)
{
_sec_param.lesc = 0;
_sec_param.mitm = 0;
_sec_param.bond = 1;
_sec_param.mitm = 0;
_sec_param.lesc = 0;
_sec_param.io_caps = BLE_GAP_IO_CAPS_NONE;
}else
{
@@ -671,9 +672,9 @@ bool AdafruitBluefruit::setPIN(const char* pin)
// - Legacy SC
// - IO cap: Display
// - MITM is on
_sec_param.lesc = 0;
_sec_param.mitm = 1;
_sec_param.bond = 1;
_sec_param.mitm = 1;
_sec_param.lesc = 0;
_sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
ble_opt_t opt;
@@ -684,6 +685,34 @@ bool AdafruitBluefruit::setPIN(const char* pin)
return true;
}
// Pairing using LESC with peripheral display
bool AdafruitBluefruit::setPairingDisplayCallback(pair_display_cb_t fp)
{
_pair_display_cb = fp;
if ( fp == NULL )
{
// TODO callback clear
}else
{
_sec_param.bond = 1;
_sec_param.mitm = 1;
_sec_param.lesc = 0;
_sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
ble_opt_t opt;
opt.gap_opt.passkey.p_passkey = NULL; // generate Passkey randomly
VERIFY_STATUS( sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opt), false);
}
return true;
}
void AdafruitBluefruit::setPairingCompleteCallback(pair_complete_cb_t fp)
{
_pair_complete_cb = fp;
}
/*------------------------------------------------------------------*/
/* Thread & SoftDevice Event handler
*------------------------------------------------------------------*/
@@ -868,6 +897,30 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
}
break;
case BLE_GAP_EVT_PASSKEY_DISPLAY:
{
ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
LOG_LV2("PAIR", "Passkey = %.6s, match request = %d", passkey_display->passkey, passkey_display->match_request);
// Invoke display callback
if ( _pair_display_cb ) ada_callback(passkey_display->passkey, 6, _pair_display_cb, conn_hdl, passkey_display->passkey);
if (passkey_display->match_request)
{
// Match request require to report the match
// sd_ble_gap_auth_key_reply();
}
}
break;
case BLE_GAP_EVT_AUTH_STATUS:
{
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
if (_pair_complete_cb) ada_callback(NULL, 0, _pair_complete_cb, conn_hdl, status->auth_status);
}
break;
case BLE_EVT_USER_MEM_REQUEST:
// We will handle Long Write sequence (RW Authorize PREP_WRITE_REQ)
sd_ble_user_mem_reply(conn_hdl, NULL);
+13 -5
View File
@@ -95,7 +95,10 @@ extern "C"
class AdafruitBluefruit
{
public:
typedef void (*rssi_callback_t) (uint16_t conn_hdl, int8_t rssi);
typedef void (*event_cb_t) (ble_evt_t* evt);
typedef void (*rssi_cb_t) (uint16_t conn_hdl, int8_t rssi);
typedef void (*pair_display_cb_t ) (uint16_t conn_hdl, uint8_t const passkey[6]);
typedef void (*pair_complete_cb_t) (uint16_t conn_hdl, uint8_t auth_status);
AdafruitBluefruit(void); // Constructor
@@ -173,12 +176,14 @@ class AdafruitBluefruit
//--------------------------------------------------------------------+
ble_gap_sec_params_t getSecureParam(void) { return _sec_param; }
bool setPIN(const char* pin); // Static Passkey
bool setPairingDisplayCallback(pair_display_cb_t fp);
void setPairingCompleteCallback(pair_complete_cb_t fp);
/*------------------------------------------------------------------*/
/* Callbacks
*------------------------------------------------------------------*/
void setRssiCallback(rssi_callback_t fp);
void setEventCallback( void (*fp) (ble_evt_t*) );
void setRssiCallback(rssi_cb_t fp);
void setEventCallback(event_cb_t fp);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
@@ -224,8 +229,11 @@ class AdafruitBluefruit
BLEConnection* _connection[BLE_MAX_CONNECTION];
rssi_callback_t _rssi_cb;
void (*_event_cb) (ble_evt_t*);
//------------- Callbacks -------------//
rssi_cb_t _rssi_cb;
event_cb_t _event_cb;
pair_display_cb_t _pair_display_cb;
pair_complete_cb_t _pair_complete_cb;
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY