move getPeerName to BLEConnection

This commit is contained in:
hathach committed 2019-04-05 12:59:50 +07:00
1 parent a5bc1f71e6
commit 5f06fd2fd9
15 files changed
+42 -21

No files matched your search

+1 -2
View File
@@ -21,9 +21,8 @@
- Bluefruit.setConnectCallback()/setDisconnectCallback() are replaced by BLEPeriph's setConnectCallback()/setDisconnectCallback()
- Introduce BLEConnection class (Bluefruit.Connection(conn)) to mange both peripheral and central connections
- Added setRssiCallback(), monitorRssi(), getRssi(), stopRssi() for tracking rssi of a connection. `rssi_poll` and `rssi_callback` are added as example sketches
- Remove BLEGap, API functions are taken by Bluefruit, BLEPeriph, BLECentral
- Remove BLEGap, API functions are taken by Bluefruit, BLEPeriph, BLECentral, BLEConnection
- Gap.setAddr()/getAddr() are replaced by Bluefruit.setAddr()/getAddr()
- Gap.getPeerName() is replaced by Bluefruit.getPeerName()
- Gap.requestPairing() is replaced by Bluefruit.requestPairing(), conn_handle parameter is also added
- Most of other functions of BLEGap are replaced by BLEConnection's one
- BLECharacteristic
@@ -156,7 +156,7 @@ void connect_callback(uint16_t conn_handle)
prph_info_t* peer = &prphs[id];
peer->conn_handle = conn_handle;
Bluefruit.getPeerName(conn_handle, peer->name, sizeof(peer->name)-1);
Bluefruit.Connection(conn_handle)->getPeerName(peer->name, sizeof(peer->name)-1);
Serial.print("Connected to ");
Serial.println(peer->name);
@@ -121,8 +121,11 @@ void loop()
*------------------------------------------------------------------*/
void prph_connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char peer_name[32] = { 0 };
Bluefruit.getPeerName(conn_handle, peer_name, sizeof(peer_name));
connection->getPeerName(peer_name, sizeof(peer_name));
Serial.print("[Prph] Connected to ");
Serial.println(peer_name);
@@ -170,8 +173,11 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
void cent_connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char peer_name[32] = { 0 };
Bluefruit.getPeerName(conn_handle, peer_name, sizeof(peer_name));
connection->getPeerName(peer_name, sizeof(peer_name));
Serial.print("[Cent] Connected to ");
Serial.println(peer_name);;
@@ -123,8 +123,11 @@ void loop()
// 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 };
Bluefruit.getPeerName(conn_handle, central_name, sizeof(central_name));
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
@@ -161,8 +161,11 @@ void setupHRM(void)
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
Bluefruit.getPeerName(conn_handle, central_name, sizeof(central_name));
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
@@ -151,8 +151,11 @@ void setupHTM(void)
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
Bluefruit.getPeerName(conn_handle, central_name, sizeof(central_name));
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
@@ -133,8 +133,11 @@ void startAdv(void)
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
Bluefruit.getPeerName(conn_handle, central_name, sizeof(central_name));
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
@@ -113,8 +113,11 @@ void startAdv(void)
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
Bluefruit.getPeerName(conn_handle, central_name, sizeof(central_name));
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
@@ -90,12 +90,12 @@ void connect_callback(uint16_t conn_handle)
Serial.println("Connected");
// Get the reference to current connection
BLEConnection* conn = Bluefruit.Connection(conn_handle);
BLEConnection* connection = Bluefruit.Connection(conn_handle);
// Start monitoring rssi of this connection
// This function should be called in connect callback
// Input argument is value difference (to current rssi) that triggers callback
conn->monitorRssi(10);
connection->monitorRssi(10);
}
void rssi_changed_callback(uint16_t conn_hdl, int8_t rssi)
@@ -84,11 +84,11 @@ void loop()
uint16_t conn_hdl = Bluefruit.connHandle();
// Get the reference to current connected connection
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
BLEConnection* connection = Bluefruit.Connection(conn_hdl);
// get the RSSI value of this connection
// monitorRssi() must be called previously (in callback)
int8_t rssi = conn->getRssi();
int8_t rssi = connection->getRssi();
Serial.printf("Rssi = %d", rssi);
Serial.println();
@@ -104,6 +104,11 @@ ble_gap_addr_t BLEConnection::getPeerAddr (void)
return _peer_addr;
}
uint16_t BLEConnection::getPeerName(char* buf, uint16_t bufsize)
{
return Bluefruit.Gatt.readCharByUuid(_conn_hdl, BLEUuid(BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME), buf, bufsize);
}
static inline bool is_tx_power_valid(int8_t power)
{
#if defined(NRF52832_XXAA)
@@ -76,7 +76,9 @@ class BLEConnection
uint8_t getRole(void);
uint16_t getMtu (void);
uint16_t getConnInterval(void);
ble_gap_addr_t getPeerAddr(void);
uint16_t getPeerName(char* buf, uint16_t bufsize);
bool disconnect(void);
@@ -606,11 +606,6 @@ bool AdafruitBluefruit::disconnect(uint16_t conn_hdl)
return true; // not connected still return true
}
uint16_t AdafruitBluefruit::getPeerName(uint16_t conn_hdl, char* buf, uint16_t bufsize)
{
return Gatt.readCharByUuid(conn_hdl, BLEUuid(BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME), buf, bufsize);
}
void AdafruitBluefruit::setEventCallback ( void (*fp) (ble_evt_t*) )
{
_event_cb = fp;
-1
View File
@@ -176,7 +176,6 @@ class AdafruitBluefruit
bool disconnect (uint16_t conn_hdl);
bool requestPairing (uint16_t conn_hdl);
uint16_t getPeerName (uint16_t conn_hdl, char* buf, uint16_t bufsize);
uint16_t getMaxMtu(uint8_t role);
BLEConnection* Connection(uint16_t conn_hdl);
@@ -108,7 +108,7 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_keys_t* bk
//------------- save device name -------------//
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
Bluefruit.getPeerName(conn_hdl, devname, CFG_MAX_DEVNAME_LEN);
Bluefruit.Connection(conn_hdl)->getPeerName(devname, CFG_MAX_DEVNAME_LEN);
// If couldn't get devname then use peer mac address
if ( !devname[0] )