22/* * NimBLE_Server Demo:
33 *
44 * Demonstrates many of the available features of the NimBLE server library.
5- *
5+ *
66 * Created: on March 22 2020
77 * Author: H2zero
8- *
8+ *
99*/
1010#include " NimBLEDevice.h"
1111#include " NimBLELog.h"
@@ -17,23 +17,23 @@ extern "C" {void app_main(void);}
1717static NimBLEServer* pServer;
1818
1919/* * None of these are required as they will be handled by the library with defaults. **
20- ** Remove as you see fit for your needs */
20+ ** Remove as you see fit for your needs */
2121class ServerCallbacks : public NimBLEServerCallbacks {
2222 void onConnect (NimBLEServer* pServer) {
2323 printf (" Client connected\n " );
2424 NimBLEDevice::startAdvertising ();
2525 };
26- /* * Alternative onConnect() method to extract details of the connection.
26+ /* * Alternative onConnect() method to extract details of the connection.
2727 * See: src/ble_gap.h for the details of the ble_gap_conn_desc struct.
28- */
28+ */
2929 void onConnect (NimBLEServer* pServer, ble_gap_conn_desc* desc) {
3030 printf (" Client address: %s\n " , NimBLEAddress (desc->peer_ota_addr ).toString ().c_str ());
3131 /* * We can use the connection handle here to ask for different connection parameters.
3232 * Args: connection handle, min connection interval, max connection interval
3333 * latency, supervision timeout.
3434 * Units; Min/Max Intervals: 1.25 millisecond increments.
3535 * Latency: number of intervals allowed to skip.
36- * Timeout: 10 millisecond increments, try for 3x interval time for best results.
36+ * Timeout: 10 millisecond increments, try for 3x interval time for best results.
3737 */
3838 pServer->updateConnParams (desc->conn_handle , 24 , 48 , 0 , 18 );
3939 };
@@ -44,25 +44,25 @@ class ServerCallbacks: public NimBLEServerCallbacks {
4444 void onMTUChange (uint16_t MTU, ble_gap_conn_desc* desc) {
4545 printf (" MTU updated: %u for connection ID: %u\n " , MTU, desc->conn_handle );
4646 };
47-
47+
4848/* ******************** Security handled here **********************
4949****** Note: these are the same return values as defaults ********/
5050 uint32_t onPassKeyRequest (){
5151 printf (" Server Passkey Request\n " );
52- /* * This should return a random 6 digit number for security
52+ /* * This should return a random 6 digit number for security
5353 * or make your own static passkey as done here.
5454 */
55- return 123456 ;
55+ return 123456 ;
5656 };
5757
5858 bool onConfirmPIN (uint32_t pass_key){
5959 printf (" The passkey YES/NO number: %d\n " , pass_key);
6060 /* * Return false if passkeys don't match. */
61- return true ;
61+ return true ;
6262 };
6363
6464 void onAuthenticationComplete (ble_gap_conn_desc* desc){
65- /* * Check that encryption was successful, if not we disconnect the client */
65+ /* * Check that encryption was successful, if not we disconnect the client */
6666 if (!desc->sec_state .encrypted ) {
6767 /* * NOTE: createServer returns the current server reference unless one is not already created */
6868 NimBLEDevice::createServer ()->disconnect (desc->conn_handle );
@@ -76,17 +76,17 @@ class ServerCallbacks: public NimBLEServerCallbacks {
7676/* * Handler class for characteristic actions */
7777class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
7878 void onRead (NimBLECharacteristic* pCharacteristic){
79- printf (" %s : onRead(), value: %s\n " ,
79+ printf (" %s : onRead(), value: %s\n " ,
8080 pCharacteristic->getUUID ().toString ().c_str (),
8181 pCharacteristic->getValue ().c_str ());
8282 };
8383
8484 void onWrite (NimBLECharacteristic* pCharacteristic) {
85- printf (" %s : onWrite(), value: %s\n " ,
85+ printf (" %s : onWrite(), value: %s\n " ,
8686 pCharacteristic->getUUID ().toString ().c_str (),
8787 pCharacteristic->getValue ().c_str ());
8888 };
89- /* * Called before notification or indication is sent,
89+ /* * Called before notification or indication is sent,
9090 * the value can be changed here before sending if desired.
9191 */
9292 void onNotify (NimBLECharacteristic* pCharacteristic) {
@@ -104,11 +104,11 @@ class CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
104104 NimBLEUtils::returnCodeToString (code));
105105 };
106106};
107-
108- /* * Handler class for descriptor actions */
107+
108+ /* * Handler class for descriptor actions */
109109class DescriptorCallbacks : public NimBLEDescriptorCallbacks {
110110 void onWrite (NimBLEDescriptor* pDescriptor) {
111- std::string dscVal (( char *) pDescriptor->getValue (), pDescriptor-> getLength () );
111+ std::string dscVal = pDescriptor->getValue ();
112112 printf (" Descriptor witten value: %s\n " , dscVal.c_str ());
113113 };
114114
@@ -118,7 +118,7 @@ class DescriptorCallbacks : public NimBLEDescriptorCallbacks {
118118};
119119
120120
121- /* * Define callback instances globally to use for multiple Charateristics \ Descriptors */
121+ /* * Define callback instances globally to use for multiple Charateristics \ Descriptors */
122122static DescriptorCallbacks dscCallbacks;
123123static CharacteristicCallbacks chrCallbacks;
124124
@@ -135,7 +135,7 @@ void notifyTask(void * parameter){
135135 }
136136 vTaskDelay (2000 /portTICK_PERIOD_MS);
137137 }
138-
138+
139139 vTaskDelete (NULL );
140140}
141141
@@ -155,10 +155,10 @@ void app_main(void) {
155155
156156 /* * 2 different ways to set security - both calls achieve the same result.
157157 * no bonding, no man in the middle protection, secure connections.
158- *
159- * These are the default values, only shown here for demonstration.
160- */
161- // NimBLEDevice::setSecurityAuth(false, false, true);
158+ *
159+ * These are the default values, only shown here for demonstration.
160+ */
161+ // NimBLEDevice::setSecurityAuth(false, false, true);
162162 NimBLEDevice::setSecurityAuth (/* BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM |*/ BLE_SM_PAIR_AUTHREQ_SC);
163163
164164 pServer = NimBLEDevice::createServer ();
@@ -173,7 +173,7 @@ void app_main(void) {
173173 NIMBLE_PROPERTY::READ_ENC | // only allow reading if paired / encrypted
174174 NIMBLE_PROPERTY::WRITE_ENC // only allow writing if paired / encrypted
175175 );
176-
176+
177177 pBeefCharacteristic->setValue (" Burger" );
178178 pBeefCharacteristic->setCallbacks (&chrCallbacks);
179179
@@ -182,10 +182,10 @@ void app_main(void) {
182182 * and sizes. However we must cast the returned reference to the correct type as the method
183183 * only returns a pointer to the base NimBLEDescriptor class.
184184 */
185- NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor (" 2904" );
185+ NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor (" 2904" );
186186 pBeef2904->setFormat (NimBLE2904::FORMAT_UTF8);
187187 pBeef2904->setCallbacks (&dscCallbacks);
188-
188+
189189
190190 NimBLEService* pBaadService = pServer->createService (" BAAD" );
191191 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic (
@@ -201,15 +201,15 @@ void app_main(void) {
201201 /* * Custom descriptor: Arguments are UUID, Properties, max length in bytes of the value */
202202 NimBLEDescriptor* pC01Ddsc = pFoodCharacteristic->createDescriptor (
203203 " C01D" ,
204- NIMBLE_PROPERTY::READ |
204+ NIMBLE_PROPERTY::READ |
205205 NIMBLE_PROPERTY::WRITE|
206206 NIMBLE_PROPERTY::WRITE_ENC, // only allow writing if paired / encrypted
207207 20
208208 );
209209 pC01Ddsc->setValue (" Send it back!" );
210210 pC01Ddsc->setCallbacks (&dscCallbacks);
211211
212- /* * Start the services when finished creating all Characteristics and Descriptors */
212+ /* * Start the services when finished creating all Characteristics and Descriptors */
213213 pDeadService->start ();
214214 pBaadService->start ();
215215
@@ -224,6 +224,6 @@ void app_main(void) {
224224 pAdvertising->start ();
225225
226226 printf (" Advertising Started\n " );
227-
227+
228228 xTaskCreate (notifyTask, " notifyTask" , 5000 , NULL , 1 , NULL );
229229}
0 commit comments