8000 Merge pull request #315 from chegewara/master · chegewara/esp32-snippets@15b6fc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15b6fc0

Browse files
authored
Merge pull request nkolban#315 from chegewara/master
Added arduino security examples
2 parents f28769c + 8f0d707 commit 15b6fc0

19 files changed

+1610
-289
lines changed

cpp_utils/HIDKeyboardTypes.h

Lines changed: 402 additions & 0 deletions
Large diffs are not rendered by default.

cpp_utils/Makefile.arduino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ BLE_FILES= \
2828
BLEDevice.h \
2929
BLEExceptions.cpp \
3030
BLEExceptions.h \
31+
BLEHIDDevice.cpp \
32+
BLEHIDDevice.h \
3133
BLERemoteCharacteristic.cpp \
3234
BLERemoteCharacteristic.h \
3335
BLERemoteDescriptor.cpp \
@@ -41,6 +43,8 @@ BLE_FILES= \
4143
BLEService.cpp \
4244
BLEService.h \
4345
BLEServiceMap.cpp \
46+
BLESecurity.cpp \
47+
BLESecurity.h \
4448
BLEUtils.cpp \
4549
BLEUtils.h \
4650
BLEUUID.cpp \
@@ -50,7 +54,9 @@ BLE_FILES= \
5054
FreeRTOS.h \
5155
FreeRTOS.cpp \
5256
GeneralUtils.h \
53-
GeneralUtils.cpp
57+
GeneralUtils.cpp \
58+
HIDTypes.h \
59+
HIDKeyboardTypes.h
5460

5561
ARDUINO_LIBS=$(HOME)/Arduino/libraries
5662

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* A BLE client example that is rich in capabilities.
3+
*/
4+
5+
#include "BLEDevice.h"
6+
//#include "BLEScan.h"
7+
8+
// The remote service we wish to connect to.
9+
static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
10+
// The characteristic of the remote service we are interested in.
11+
static BLEUUID charUUID("0d563a58-196a-48ce-ace2-dfec78acc814");
12+
13+
static BLEAddress *pServerAddress;
14+
static boolean doConnect = false;
15+
static boolean connected = false;
16+
static BLERemoteCharacteristic* pRemoteCharacteristic;
17+
18+
static void notifyCallback(
19+
BLERemoteCharacteristic* pBLERemoteCharacteristic,
20+
uint8_t* pData,
21+
size_t length,
22+
bool isNotify) {
23+
Serial.print("Notify callback for characteristic ");
24+
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
25+
Serial.print(" of data length ");
26+
Serial.println(length);
27+
}
28+
29+
bool connectToServer(BLEAddress pAddress) {
30+
Serial.print("Forming a connection to ");
31+
Serial.println(pAddress.toString().c_str());
32+
33+
/*
34+
* Here we have implemented simplest security. This kind security does not provide authentication
35+
*/
36+
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
37+
BLEClient* pClient = BLEDevice::createClient();
38+
Serial.println(" - Created client");
39+
40+
// Connect to the remove BLE Server.
41+
pClient->connect(pAddress);
42+
Serial.println(" - Connected to server");
43+
44+
// Obtain a reference to the service we are after in the remote BLE server.
45+
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
46+
if (pRemoteService == nullptr) {
47+
Serial.print("Failed to find our service UUID: ");
48+
Serial.println(serviceUUID.toString().c_str());
49+
return false;
50+
}
51+
Serial.println(" - Found our service");
52+
53+
54+
// Obtain a reference to the characteristic in the service of the remote BLE server.
55+
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
56+
if (pRemoteCharacteristic == nullptr) {
57+
Serial.print("Failed to find our characteristic UUID: ");
58+
Serial.println(charUUID.toString().c_str());
59+
return false;
60+
}
61+
Serial.println(" - Found our characteristic");
62+
63+
// Read the value of the characteristic.
64+
std::string value = pRemoteCharacteristic->readValue();
65+
Serial.print("The characteristic value was: ");
66+
Serial.println(value.c_str());
67+
68+
pRemoteCharacteristic->registerForNotify(notifyCallback);
69+
}
70+
/**
71+
* Scan for BLE servers and find the first one that advertises the service we are looking for.
72+
*/
73+
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
74+
/**
75+
* Called for each advertising BLE server.
76+
*/
77+
void onResult(BLEAdvertisedDevice advertisedDevice) {
78+
Serial.print("BLE Advertised Device found: ");
79+
Serial.println(advertisedDevice.toString().c_str());
80+
81+
// We have found a device, let us now see if it contains the service we are looking for.
82+
if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
83+
84+
//
85+
Serial.print("Found our device! address: ");
86+
advertisedDevice.getScan()->stop();
87+
88+
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
89+
doConnect = true;
90+
91+
} // Found our server
92+
} // onResult
93+
}; // MyAdvertisedDeviceCallbacks
94+
95+
96+
void setup() {
97+
Serial.begin(115200);
98+
Serial.println("Starting Arduino BLE Client application...");
99+
BLEDevice::init("");
100+
101+
// Retrieve a Scanner and set the callback we want to use to be informed when we
102+
// have detected a new device. Specify that we want active scanning and start the
103+
// scan to run for 30 seconds.
104+
BLEScan* pBLEScan = BLEDevice::getScan();
105+
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
106+
pBLEScan->setActiveScan(true);
107+
pBLEScan->start(30);
108+
} // End of setup.
109+
110+
111+
// This is the Arduino main loop function.
112+
void loop() {
113+
114+
// If the flag "doConnect" is true then we have scanned for and found the desired
115+
// BLE Server with which we wish to connect. Now we connect to it. Once we are
116+
// connected we set the connected flag to be true.
117+
if (doConnect == true) {
118+
if (connectToServer(*pServerAddress)) {
119+
Serial.println("We are now connected to the BLE Server.");
120+
connected = true;
121+
} else {
122+
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
123+
}
124+
doConnect = false;
125+
}
126+
127+
// If we are connected to a peer BLE Server, update the characteristic each time we are reached
128+
// with the current time since boot.
129+
if (connected) {
130+
String newValue = "Time since boot: " + String(millis()/1000);
131+
Serial.println("Setting new characteristic value to \"" + newValue + "\"");
132+
133+
// Set the characteristic's value to be the array of bytes that is actually a string.
134+
pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
135+
}
136+
137+
delay(1000); // Delay a second between loops.
138+
} // End of loop
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* A BLE client example that is rich in capabilities.
3+
*/
4+
5+
#include "BLEDevice.h"
6+
//#include "BLEScan.h"
7+
8+
// The remote service we wish to connect to.
9+
static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
10+
// The characteristic of the remote service we are interested in.
11+
static BLEUUID charUUID("0d563a58-196a-48ce-ace2-dfec78acc814");
12+
13+
static BLEAddress *pServerAddress;
14+
static boolean doConnect = false;
15+
static boolean connected = false;
16+
static BLERemoteCharacteristic* pRemoteCharacteristic;
17+
18+
static void notifyCallback(
19+
BLERemoteCharacteristic* pBLERemoteCharacteristic,
20+
uint8_t* pData,
21+
size_t length,
22+
bool isNotify) {
23+
Serial.print("Notify callback for characteristic ");
24+
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
25+
Serial.print(" of data length ");
26+
Serial.println(length);
27+
}
28+
29+
bool connectToServer(BLEAddress pAddress) {
30+
Serial.print("Forming a connection to ");
31+
Serial.println(pAddress.toString().c_str());
32+
33+
/*
34+
* Here we have implemented simplest security. This kind security does not provide authentication
35+
*/
36+
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
37+
BLEClient* pClient = BLEDevice::createClient();
38+
Serial.println(" - Created client");
39+
40+
// Connect to the remove BLE Server.
41+
pClient->connect(pAddress);
42+
Serial.println(" - Connected to server");
43+
44+
// Obtain a reference to the service we are after in the remote BLE server.
45+
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
46+
if (pRemoteService == nullptr) {
47+
Serial.print("Failed to find our service UUID: ");
48+
Serial.println(serviceUUID.toString().c_str());
49+
return false;
50+
}
51+
Serial.println(" - Found our service");
52+
53+
54+
// Obtain a reference to the characteristic in the service of the remote BLE server.
55+
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
56+
if (pRemoteCharacteristic == nullptr) {
57+
Serial.print("Failed to find our characteristic UUID: ");
58+
Serial.println(charUUID.toString().c_str());
59+
return false;
60+
}
61+
Serial.println(" - Found our characteristic");
62+
63+
// Read the value of the characteristic.
64+
std::string value = pRemoteCharacteristic->readValue();
65+
Serial.print("The characteristic value was: ");
66+
Serial.println(value.c_str());
67+
68+
pRemoteCharacteristic->registerForNotify(notifyCallback);
69+
}
70+
/**
71+
* Scan for BLE servers and find the first one that advertises the service we are looking for.
72+
*/
73+
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
74+
/**
75+
* Called for each advertising BLE server.
76+
*/
77+
void onResult(BLEAdvertisedDevice advertisedDevice) {
78+
Serial.print("BLE Advertised Device found: ");
79+
Serial.println(advertisedDevice.toString().c_str());
80+
81+
// We have found a device, let us now see if it contains the service we are looking for.
82+
if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
83+
84+
//
85+
Serial.print("Found our device! address: ");
86+
advertisedDevice.getScan()->stop();
87+
88+
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
89+
doConnect = true;
90+
91+
} // Found our server
92+
} // onResult
93+
}; // MyAdvertisedDeviceCallbacks
94+
95+
96+
void setup() {
97+
Serial.begin(115200);
98+
Serial.println("Starting Arduino BLE Client application...");
99+
BLEDevice::init("");
100+
101+
// Retrieve a Scanner and set the callback we want to use to be informed when we
102+
// have detected a new device. Specify that we want active scanning and start the
103+
// scan to run for 30 seconds.
104+
BLEScan* pBLEScan = BLEDevice::getScan();
105+
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
106+
pBLEScan->setActiveScan(true);
107+
pBLEScan->start(30);
108+
} // End of setup.
109+
110+
111+
// This is the Arduino main loop function.
112+
void loop() {
113+
114+
// If the flag "doConnect" is true then we have scanned for and found the desired
115+
// BLE Server with which we wish to connect. Now we connect to it. Once we are
116+
// connected we set the connected flag to be true.
117+
if (doConnect == true) {
118+
if (connectToServer(*pServerAddress)) {
119+
Serial.println("We are now connected to the BLE Server.");
120+
connected = true;
121+
} else {
122+
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
123+
}
124+
doConnect = false;
125+
}
126+
127+
// If we are connected to a peer BLE Server, update the characteristic each time we are reached
128+
// with the current time since boot.
129+
if (connected) {
130+
String newValue = "Time since boot: " + String(millis()/1000);
131+
Serial.println("Setting new characteristic value to \"" + newValue + "\"");
132+
133+
// Set the characteristic's value to be the array of bytes that is actually a string.
134+
pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
135+
}
136+
137+
delay(1000); // Delay a second between loops.
138+
} // End of loop

0 commit comments

Comments
 (0)
0