Skip to content

Commit bb5b7b4

Browse files
sgbihuSidLeung
authored andcommitted
Jira 671, Support Connection Interval updating.
Description: This feature enables a sketch to change the Connection Interval, Timeout, and Latency. The feature is applicable to both Central and Peripheral mode. A change in the interval in the midst of a connection will terminate the connection. A re-connection is required. File mods: 1. BLEDeviceManager.cpp: - Application facing Connection Interval updating APIs. 2. BLEDevice.cpp: - The execution of the interval changing command. File addition: 1. connupdateperipheral.ino: - A demo sketch to show how a peripheral can change the Connection Interval.
1 parent 82eb1be commit bb5b7b4

File tree

5 files changed

+218
-33
lines changed

5 files changed

+218
-33
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2017 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
/*
7+
* Sketch: connupdateperipheral.ino
8+
*
9+
* Description:
10+
* This is a Peripheral sketch that is based on the Arduino LED
11+
* control sketch. The purpose of this sketch is to exercise
12+
* the changing of Connection Interval as a Peripheral. The Central
13+
* writes to the Characteristic to turn the LED on and off similar
14+
* to the original sketch. However, when the LED is turned off,
15+
* this sketch will change the connection interval to a new
16+
* value and, thus, forcing a re-connection.
17+
*
18+
* Notes:
19+
*
20+
* - This sketch is based on the Arduino BLE Peripheral LED example.
21+
* Please refer to licensing info at the bottom of this file.
22+
*/
23+
24+
#include <CurieBLE.h>
25+
26+
// LED pin
27+
#define LED_PIN 13
28+
29+
// create service
30+
BLEService ledService("19b10000e8f2537e4f6cd104768a1214");
31+
32+
// create switch characteristic
33+
BLECharCharacteristic switchCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
34+
35+
BLEDescriptor switchDescriptor("2901", "switch");
36+
37+
38+
void bleCentralConnectionParameterUpdateHandler(BLEDevice central) {
39+
Serial1.println("Updated connection parameter");
40+
Serial1.println("-----------------------");
41+
42+
// print address
43+
Serial1.print("Address: ");
44+
Serial1.println(central.address());
45+
46+
Serial1.print("Interval: ");
47+
Serial1.println(central.getConnectionInterval());
48+
Serial1.print("Timeout: ");
49+
Serial1.println(central.getConnectionTimeout());
50+
Serial1.print("Latency: ");
51+
Serial1.println(central.getConnectionLatency());
52+
53+
//Serial1.println();
54+
}
55+
56+
void setup() {
57+
Serial.begin(9600);
58+
Serial1.begin(115200);
59+
60+
// set LED pin to output mode
61+
pinMode(LED_PIN, OUTPUT);
62+
63+
// begin initialization
64+
BLE.begin();
65+
Serial1.println(BLE.address());
66+
67+
// set advertised local name and service UUID
68+
BLE.setLocalName("LED");
69+
BLE.setAdvertisedServiceUuid(ledService.uuid());
70+
71+
switchCharacteristic.addDescriptor(switchDescriptor);
72+
ledService.addCharacteristic(switchCharacteristic);
73+
74+
// add service and characteristic
75+
BLE.addService(ledService);
76+
77+
//Register callbacks
78+
BLE.setEventHandler(BLEConParamUpdate, bleCentralConnectionParameterUpdateHandler);
79+
80+
BLE.advertise();
81+
82+
Serial1.println(F("BLE LED Peripheral"));
83+
}
84+
85+
void loop() {
86+
BLEDevice central = BLE.central();
87+
88+
if (central) {
89+
// central connected to peripheral
90+
Serial1.print(F("Connected to central: "));
91+
Serial1.println(central.address());
92+
static int connection_interval = 15;
93+
94+
while (central.connected()) {
95+
// central still connected to peripheral
96+
if (switchCharacteristic.written()) {
97+
// central wrote new value to characteristic, update LED
98+
if (switchCharacteristic.value()) {
99+
Serial1.println(F("LED on"));
100+
digitalWrite(LED_PIN, HIGH);
101+
connection_interval++;
102+
} else {
103+
Serial1.println(F("LED off"));
104+
digitalWrite(LED_PIN, LOW);
105+
delay(100);
106+
// The peripheral update the connection interval
107+
// If want central update the connection interval
108+
// comment the below line
109+
central.setConnectionInterval(connection_interval, connection_interval);
110+
}
111+
}
112+
}
113+
114+
// central disconnected
115+
Serial1.print(F("Disconnected from central: "));
116+
Serial1.println(central.address());
117+
}
118+
}
119+
120+
/*
121+
Arduino BLE Peripheral LED example
122+
Copyright (c) 2016 Arduino LLC. All right reserved.
123+
124+
This library is free software; you can redistribute it and/or
125+
modify it under the terms of the GNU Lesser General Public
126+
License as published by the Free Software Foundation; either
127+
version 2.1 of the License, or (at your option) any later version.
128+
129+
This library is distributed in the hope that it will be useful,
130+
but WITHOUT ANY WARRANTY; without even the implied warranty of
131+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
132+
Lesser General Public License for more details.
133+
134+
You should have received a copy of the GNU Lesser General Public
135+
License along with this library; if not, write to the Free Software
136+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
137+
*/
138+

libraries/CurieBLE/src/BLEDevice.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "CurieBLE.h"
2020
#include "BLEDevice.h"
2121

22+
#include "./internal/ble_client.h"
23+
2224
#include "./internal/BLEUtils.h"
2325

2426
#include "./internal/BLEProfileManager.h"
@@ -51,6 +53,7 @@ BLEDevice::BLEDevice(const bt_addr_le_t* bleaddress):
5153
BLEDevice()
5254
{
5355
memcpy(&_bt_addr, bleaddress, sizeof(_bt_addr));
56+
BLEDeviceManager::instance()->getConnectionInterval(this, &_conn_param);
5457
}
5558

5659
BLEDevice::BLEDevice(const BLEDevice* bledevice)
@@ -154,11 +157,54 @@ void BLEDevice::setAdvertisingInterval(float advertisingInterval)
154157
BLEDeviceManager::instance()->setAdvertisingInterval(advertisingInterval);
155158
}
156159

160+
void BLEDevice::setConnectionInterval(int minInterval,
161+
int maxInterval,
162+
uint16_t latency,
163+
uint16_t timeout)
164+
{
165+
uint16_t minVal = (uint16_t)MSEC_TO_UNITS(minInterval, UNIT_1_25_MS);
166+
uint16_t maxVal = (uint16_t)MSEC_TO_UNITS(maxInterval, UNIT_1_25_MS);
167+
uint16_t timeoutVal = MSEC_TO_UNITS(timeout, UNIT_10_MS);
168+
_conn_param.interval_min = minVal;
169+
_conn_param.interval_max = maxVal;
170+
_conn_param.timeout = timeoutVal;
171+
_conn_param.latency = latency;
172+
BLEDeviceManager::instance()->setConnectionInterval(this);
173+
}
174+
157175
void BLEDevice::setConnectionInterval(int minimumConnectionInterval,
158176
int maximumConnectionInterval)
159177
{
160-
// TODO: Update the connection interval need more discussion
178+
uint16_t minVal = (uint16_t)MSEC_TO_UNITS(minimumConnectionInterval, UNIT_1_25_MS);
179+
uint16_t maxVal = (uint16_t)MSEC_TO_UNITS(maximumConnectionInterval, UNIT_1_25_MS);
180+
_conn_param.interval_min = minVal;
181+
_conn_param.interval_max = maxVal;
182+
183+
BLEDeviceManager::instance()->setConnectionInterval(this);
184+
}
185+
186+
int BLEDevice::getConnectionInterval()
187+
{
188+
bt_le_conn_param_t conn_param;
189+
190+
BLEDeviceManager::instance()->getConnectionInterval(this, &conn_param);
191+
return UNITS_TO_MSEC((int)conn_param.interval_max, UNIT_1_25_MS);
192+
}
193+
194+
int BLEDevice::getConnectionTimeout()
195+
{
196+
bt_le_conn_param_t conn_param;
197+
198+
BLEDeviceManager::instance()->getConnectionInterval(this, &conn_param);
199+
return UNITS_TO_MSEC(conn_param.timeout, UNIT_10_MS);;
200+
}
161201

202+
int BLEDevice::getConnectionLatency()
203+
{
204+
bt_le_conn_param_t conn_param;
205+
206+
BLEDeviceManager::instance()->getConnectionInterval(this, &conn_param);
207+
return conn_param.latency;
162208
}
163209

164210
bool BLEDevice::setTxPower(int txPower)

libraries/CurieBLE/src/BLEDevice.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ class BLEDevice
232232
*
233233
* @note none
234234
*/
235-
//void setConnectionInterval(int minimumConnectionInterval,
236-
// int maximumConnectionInterval,
237-
// uint16_t latency,
238-
// uint16_t timeout);
235+
void setConnectionInterval(int minimumConnectionInterval,
236+
int maximumConnectionInterval,
237+
uint16_t latency,
238+
uint16_t timeout);
239239

240240
/**
241241
* @brief Set the min and max connection interval and send connection
@@ -252,6 +252,10 @@ class BLEDevice
252252
void setConnectionInterval(int minimumConnectionInterval,
253253
int maximumConnectionInterval);
254254

255+
int getConnectionInterval();
256+
int getConnectionTimeout();
257+
int getConnectionLatency();
258+
255259
/**
256260
* @brief Set TX power of the radio in dBM
257261
*
@@ -689,7 +693,7 @@ class BLEDevice
689693
private:
690694
bt_addr_le_t _bt_addr;
691695

692-
bt_le_conn_param _conn_param;
696+
bt_le_conn_param_t _conn_param;
693697
};
694698

695699
#endif

libraries/CurieBLE/src/internal/BLEDeviceManager.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,31 @@ void BLEDeviceManager::setAdvertisingInterval(float advertisingInterval)
283283
_adv_param.interval_max = interval;
284284
}
285285

286-
void BLEDeviceManager::setConnectionInterval(float minimumConnectionInterval,
287-
float maximumConnectionInterval,
288-
uint16_t latency,
289-
uint16_t timeout)
286+
void BLEDeviceManager::getConnectionInterval(BLEDevice *device,
287+
bt_le_conn_param* conn_param)
290288
{
289+
bt_conn_t* conn = bt_conn_lookup_addr_le(device->bt_le_address());
290+
if (NULL != conn)
291+
{
292+
conn_param->interval_max = conn->le.interval;
293+
conn_param->interval_min = conn->le.interval;
294+
conn_param->latency = conn->le.latency;
295+
conn_param->timeout = conn->le.timeout;
296+
bt_conn_unref(conn);
297+
}
291298
}
292299

293-
void BLEDeviceManager::setConnectionInterval(float minimumConnectionInterval,
294-
float maximumConnectionInterval)
300+
int BLEDeviceManager::setConnectionInterval(BLEDevice *device)
295301
{
296-
302+
bt_conn_t* conn = bt_conn_lookup_addr_le(device->bt_le_address());
303+
int ret = 0;
304+
if (NULL != conn)
305+
{
306+
ret = bt_conn_le_param_update(conn, device->bt_conn_param());
307+
pr_debug(LOG_MODULE_BLE, "%s-ret:%d",__FUNCTION__, ret);
308+
bt_conn_unref(conn);
309+
}
310+
return ret;
297311
}
298312

299313
bool BLEDeviceManager::setTxPower(int txPower)

libraries/CurieBLE/src/internal/BLEDeviceManager.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,9 @@ class BLEDeviceManager
181181
*
182182
* @note none
183183
*/
184-
void setConnectionInterval(float minimumConnectionInterval,
185-
float maximumConnectionInterval,
186-
uint16_t latency,
187-
uint16_t timeout);
188-
189-
/**
190-
* @brief Set the min and max connection interval and send connection
191-
* update request in both BLE peripheral and central
192-
*
193-
* @param[in] intervalmin Minimum Connection Interval (ms)
194-
*
195-
* @param[in] intervalmax Maximum Connection Interval (ms)
196-
*
197-
* @return none
198-
*
199-
* @note none
200-
*/
201-
void setConnectionInterval(float minimumConnectionInterval,
202-
float maximumConnectionInterval);
203-
184+
int setConnectionInterval (BLEDevice *device);
185+
void getConnectionInterval(BLEDevice *device,
186+
bt_le_conn_param* conn_param);
204187
/**
205188
* @brief Set TX power of the radio in dBM
206189
*

0 commit comments

Comments
 (0)