77#include " sdkconfig.h"
88#include " BleConnectionStatus.h"
99#include " BleGamepad.h"
10+ #include " NimBLELog.h"
1011#include " BleGamepadConfiguration.h"
1112
1213#include < stdexcept>
@@ -231,6 +232,7 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
231232 // REPORT_COUNT
232233 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95 ;
233234 tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getDesktopSpecialButtonCount ();
235+
234236 if (configuration.getIncludeStart ())
235237 {
236238 // USAGE (Start)
@@ -273,15 +275,15 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
273275 // USAGE (Home)
274276 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x0A ;
275277 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x23 ;
276- tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02 ;
278+ tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02 ;
277279 }
278280
279281 if (configuration.getIncludeBack ())
280282 {
281283 // USAGE (Back)
282284 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x0A ;
283285 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x24 ;
284- tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02 ;
286+ tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02 ;
285287 }
286288
287289 if (configuration.getIncludeVolumeInc ())
@@ -343,9 +345,9 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
343345 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x16 ;
344346 tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte (configuration.getAxesMin ());
345347 tempHidReportDescriptor[hidReportDescriptorSize++] = highByte (configuration.getAxesMin ());
346- // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; // Use these two lines for 0 min
348+ // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; // Use these two lines for 0 min
347349 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
348- // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; // Use these two lines for -32767 min
350+ // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; // Use these two lines for -32767 min
349351 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x80;
350352
351353 // LOGICAL_MAXIMUM (+32767)
@@ -354,7 +356,7 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
354356 tempHidReportDescriptor[hidReportDescriptorSize++] = highByte (configuration.getAxesMax ());
355357 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; // Use these two lines for 255 max
356358 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
357- // tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; // Use these two lines for +32767 max
359+ // tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; // Use these two lines for +32767 max
358360 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x7F;
359361
360362 // REPORT_SIZE (16)
@@ -447,7 +449,7 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
447449 tempHidReportDescriptor[hidReportDescriptorSize++] = highByte (configuration.getSimulationMin ());
448450 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00; // Use these two lines for 0 min
449451 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
450- // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; // Use these two lines for -32767 min
452+ // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01; // Use these two lines for -32767 min
451453 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x80;
452454
453455 // LOGICAL_MAXIMUM (+32767)
@@ -456,7 +458,7 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
456458 tempHidReportDescriptor[hidReportDescriptorSize++] = highByte (configuration.getSimulationMax ());
457459 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; // Use these two lines for 255 max
458460 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
459- // tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; // Use these two lines for +32767 max
461+ // tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF; // Use these two lines for +32767 max
460462 // tempHidReportDescriptor[hidReportDescriptorSize++] = 0x7F;
461463
462464 // REPORT_SIZE (16)
@@ -570,7 +572,8 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
570572 tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0 ;
571573 }
572574
573- if (configuration.getEnableOutputReport ()){
575+ if (configuration.getEnableOutputReport ())
576+ {
574577 // Usage Page (Vendor Defined 0xFF00)
575578 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x06 ;
576579 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00 ;
@@ -597,11 +600,14 @@ void BleGamepad::begin(BleGamepadConfiguration *config)
597600 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75 ;
598601 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x08 ;
599602
600- if (configuration.getOutputReportLength ()<=0xFF ){
603+ if (configuration.getOutputReportLength ()<=0xFF )
604+ {
601605 // Report Count (0~255 bytes)
602606 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95 ;
603607 tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getOutputReportLength ();
604- }else {
608+ }
609+ else
610+ {
605611 // Report Count (0~65535 bytes)
606612 tempHidReportDescriptor[hidReportDescriptorSize++] = 0x96 ;
607613 tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte (configuration.getOutputReportLength ());
@@ -891,6 +897,7 @@ uint8_t BleGamepad::specialButtonBitPosition(uint8_t b)
891897 if (b >= POSSIBLESPECIALBUTTONS)
892898 throw std::invalid_argument (" Index out of range" );
893899 uint8_t bit = 0 ;
900+
894901 for (int i = 0 ; i < b; i++)
895902 {
896903 if (configuration.getWhichSpecialButtons ()[i])
@@ -1423,41 +1430,45 @@ uint8_t* BleGamepad::getOutputBuffer()
14231430
14241431bool BleGamepad::deleteAllBonds (bool resetBoard)
14251432{
1426- bool success = false ;
1433+ bool success = false ;
14271434
1428- NimBLEDevice::deleteAllBonds ();
1429- success = true ;
1430- delay (500 );
1431-
1432- if (resetBoard)
1433- {
1434- ESP.restart ();
1435- }
1436-
1437- return success; // Returns false if all bonds are not deleted
1435+ NimBLEDevice::deleteAllBonds ();
1436+ NIMBLE_LOGD (LOG_TAG, " deleteAllBonds - All bonds deleted" );
1437+ success = true ;
1438+ delay (500 );
1439+
1440+ if (resetBoard)
1441+ {
1442+ NIMBLE_LOGD (LOG_TAG, " deleteAllBonds - Reboot ESP32" );
1443+ ESP.restart ();
1444+ }
1445+
1446+ return success; // Returns false if all bonds are not deleted
14381447}
14391448
14401449bool BleGamepad::deleteBond (bool resetBoard)
14411450{
1442- bool success = false ;
1451+ bool success = false ;
14431452
1444- NimBLEServer* server = NimBLEDevice::getServer ();
1453+ NimBLEServer* server = NimBLEDevice::getServer ();
14451454
1446- if (server)
1447- {
1448- NimBLEConnInfo info = server->getPeerInfo (0 );
1449- NimBLEAddress address = info.getAddress ();
1455+ if (server)
1456+ {
1457+ NimBLEConnInfo info = server->getPeerInfo (0 );
1458+ NimBLEAddress address = info.getAddress ();
14501459
1451- success = NimBLEDevice::deleteBond (address);
1460+ success = NimBLEDevice::deleteBond (address);
1461+ NIMBLE_LOGD (LOG_TAG, " deleteBond - Bond for %s deleted" , std::string (address).c_str ());
14521462
1453- delay (500 );
1463+ delay (500 );
14541464
1455- if (resetBoard)
1456- {
1457- ESP.restart ();
1458- }
1459- }
1460- return success; // Returns false if current bond is not deleted
1465+ if (resetBoard)
1466+ {
1467+ NIMBLE_LOGD (LOG_TAG, " deleteBond - Reboot ESP32" );
1468+ ESP.restart ();
1469+ }
1470+ }
1471+ return success; // Returns false if current bond is not deleted
14611472}
14621473
14631474bool BleGamepad::enterPairingMode ()
@@ -1466,58 +1477,93 @@ bool BleGamepad::enterPairingMode()
14661477
14671478 if (server)
14681479 {
1469- // Serial.println("Entered pairing mode");
1480+ NIMBLE_LOGD (LOG_TAG, " enterPairingMode - Pairing mode entered " );
14701481
1471- // Get current connection information and address
1472- NimBLEConnInfo currentConnInfo = server->getPeerInfo (0 );
1473- NimBLEAddress currentAddress = currentConnInfo.getAddress ();
1474- // Serial.print("Current Address is: ");
1475- // Serial.println(currentAddress.toString().c_str());
1482+ // Get current connection information and address
1483+ NimBLEConnInfo currentConnInfo = server->getPeerInfo (0 );
1484+ NimBLEAddress currentAddress = currentConnInfo.getAddress ();
1485+ NIMBLE_LOGD (LOG_TAG, " enterPairingMode - Connected Address: %s" , std::string (currentAddress).c_str ());
14761486
1477- // Disconnect from current connection
1478- for (uint16_t connHandle : server->getPeerDevices ())
1479- {
1487+ // Disconnect from current connection
1488+ for (uint16_t connHandle : server->getPeerDevices ())
1489+ {
14801490 server->disconnect (connHandle); // Disconnect the client
1481- // Serial.println(" Disconnected from client");
1482- delay (1000 );
1491+ NIMBLE_LOGD (LOG_TAG, " enterPairingMode - Disconnected from client" );
1492+ delay (500 );
14831493 }
14841494
1485- bool connectedToOldDevice = true ;
1495+ bool connectedToOldDevice = true ;
14861496
1487- // While connected to old device, keep allowing to connect new new devices
1488- while (connectedToOldDevice)
1489- {
1490- // Serial.println("While loop entered");
1491- delay (10 ); // Needs a delay to work - do not remove!
1497+ // While connected to old device, keep allowing to connect new new devices
1498+ NIMBLE_LOGD (LOG_TAG, " enterPairingMode - Advertising for clients... " );
1499+ while (connectedToOldDevice)
1500+ {
1501+ delay (10 ); // Needs a delay to work - do not remove!
14921502
14931503 if (this ->isConnected ())
14941504 {
1495- NimBLEConnInfo newConnInfo = server->getPeerInfo (0 );
1496- NimBLEAddress newAddress = newConnInfo.getAddress ();
1497-
1498- // Serial.print("Current Address is: ");
1499- // Serial.println(newAddress.toString().c_str());
1505+ NimBLEConnInfo newConnInfo = server->getPeerInfo (0 );
1506+ NimBLEAddress newAddress = newConnInfo.getAddress ();
15001507
1501- // Block specific MAC address
1502- if (newAddress == currentAddress)
1503- {
1504- // Serial.println("Addresses match - blocking");
1505- server->disconnect (newConnInfo.getConnHandle ());
1506- delay (500 );
1507- }
1508- else
1509- {
1510- // Serial.println("New device connected");
1511- connectedToOldDevice = false ;
1512- return true ;
1513- }
1508+ // Block specific MAC address
1509+ if (newAddress == currentAddress)
1510+ {
1511+ NIMBLE_LOGD (LOG_TAG, " enterPairingMode - Connected to previous client, so disconnect and continue advertising for new client" );
1512+ server->disconnect (newConnInfo.getConnHandle ());
1513+ delay (500 );
1514+ }
1515+ else
1516+ {
1517+ NIMBLE_LOGD (LOG_TAG, " enterPairingMode - Connected to new client" );
1518+ NIMBLE_LOGD (LOG_TAG, " enterPairingMode - Exit pairing mode" );
1519+ connectedToOldDevice = false ;
1520+ return true ;
1521+ }
15141522 }
15151523 }
15161524 return false ; // Might want to adjust this function to stay in pairing mode for a while, and then return false after a while if no other device pairs with it
15171525 }
15181526 return false ;
15191527}
15201528
1529+ NimBLEAddress BleGamepad::getAddress ()
1530+ {
1531+ NimBLEServer* server = NimBLEDevice::getServer ();
1532+
1533+ if (server)
1534+ {
1535+
1536+ // Get current connection information and address
1537+ NimBLEConnInfo currentConnInfo = server->getPeerInfo (0 );
1538+ NimBLEAddress currentAddress = currentConnInfo.getAddress ();
1539+ return currentAddress;
1540+ }
1541+ NimBLEAddress blankAddress;
1542+ return blankAddress;
1543+ }
1544+
1545+ String BleGamepad::getStringAddress ()
1546+ {
1547+ NimBLEServer* server = NimBLEDevice::getServer ();
1548+
1549+ if (server)
1550+ {
1551+
1552+ // Get current connection information and address
1553+ NimBLEConnInfo currentConnInfo = server->getPeerInfo (0 );
1554+ NimBLEAddress currentAddress = currentConnInfo.getAddress ();
1555+ return currentAddress.toString ().c_str ();
1556+ }
1557+ NimBLEAddress blankAddress;
1558+ return blankAddress.toString ().c_str ();
1559+ }
1560+
1561+ NimBLEConnInfo BleGamepad::getPeerInfo ()
1562+ {
1563+ NimBLEServer* server = NimBLEDevice::getServer ();
1564+ NimBLEConnInfo currentConnInfo = server->getPeerInfo (0 );
1565+ return currentConnInfo;
1566+ }
15211567
15221568void BleGamepad::taskServer (void *pvParameter)
15231569{
@@ -1526,6 +1572,7 @@ void BleGamepad::taskServer(void *pvParameter)
15261572 NimBLEDevice::init (BleGamepadInstance->deviceName );
15271573 NimBLEServer *pServer = NimBLEDevice::createServer ();
15281574 pServer->setCallbacks (BleGamepadInstance->connectionStatus );
1575+ pServer->advertiseOnDisconnect (true );
15291576
15301577 BleGamepadInstance->hid = new NimBLEHIDDevice (pServer);
15311578
@@ -1599,6 +1646,6 @@ void BleGamepad::taskServer(void *pvParameter)
15991646 pAdvertising->start ();
16001647 BleGamepadInstance->hid ->setBatteryLevel (BleGamepadInstance->batteryLevel );
16011648
1602- ESP_LOGD (LOG_TAG, " Advertising started!" );
1649+ NIMBLE_LOGD (LOG_TAG, " Advertising started!" );
16031650 vTaskDelay (portMAX_DELAY); // delay(portMAX_DELAY);
16041651}
0 commit comments