@@ -226,6 +226,7 @@ std::filesystem::path SVGIndexFilename;
226
226
int LogFileTime (60 );
227
227
int MinutesAverage (5 );
228
228
int DaysBetweenDataDownload (0 );
229
+ int MaxMinutesBetweenBluetoothAdvertisments (0 );
229
230
// The following details were taken from https://github.com/oetiker/mrtg
230
231
const size_t DAY_COUNT (600 ); /* 400 samples is 33.33 hours */
231
232
const size_t WEEK_COUNT (600 ); /* 400 samples is 8.33 days */
@@ -3615,16 +3616,18 @@ void BlueZ_HCI_MainLoop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
3615
3616
if (bMonitorLoggingDirectory)
3616
3617
MonitorLoggedData ();
3617
3618
}
3618
- const int MaxMinutesBetweenBluetoothAdvertisments (3 );
3619
- if (difftime (TimeNow, TimeAdvertisment) > MaxMinutesBetweenBluetoothAdvertisments * 60 ) // Hack to force scanning restart regularly
3619
+ if (MaxMinutesBetweenBluetoothAdvertisments > 0 )
3620
3620
{
3621
- if (ConsoleVerbosity > 0 )
3622
- std::cout << " [" << getTimeISO8601 (true ) << " ] No recent Bluetooth LE Advertisments! (> " << MaxMinutesBetweenBluetoothAdvertisments << " Minutes)" << std::endl;
3623
- btRVal = bt_LEScan (BlueToothDevice_Handle, true , BT_WhiteList, HCI_Passive_Scanning);
3624
- if (btRVal < 0 )
3621
+ if (difftime (TimeNow, TimeAdvertisment) > MaxMinutesBetweenBluetoothAdvertisments * 60 ) // Hack to force scanning restart regularly
3625
3622
{
3626
- bRun = false ; // rely on inetd to restart entire process
3627
- ExitValue = EXIT_FAILURE;
3623
+ if (ConsoleVerbosity > 0 )
3624
+ std::cout << " [" << getTimeISO8601 (true ) << " ] No recent Bluetooth LE Advertisments! (> " << MaxMinutesBetweenBluetoothAdvertisments << " Minutes)" << std::endl;
3625
+ btRVal = bt_LEScan (BlueToothDevice_Handle, true , BT_WhiteList, HCI_Passive_Scanning);
3626
+ if (btRVal < 0 )
3627
+ {
3628
+ bRun = false ; // rely on inetd to restart entire process
3629
+ ExitValue = EXIT_FAILURE;
3630
+ }
3628
3631
}
3629
3632
}
3630
3633
}
@@ -5356,10 +5359,10 @@ time_t ConnectAndDownload(DBusConnection* dbus_conn, const char* adapter_path, c
5356
5359
return (TimeDownloadStart);
5357
5360
}
5358
5361
// ///////////////////////////////////////////////////////////////////////////
5359
- int BlueZ_DBus_Mainloop (std::string& ControllerAddress, std::set<bdaddr_t >& BT_WhiteList, bool bMonitorLoggingDirectory)
5362
+ int BlueZ_DBus_Mainloop (std::string& ControllerAddress, std::set<bdaddr_t >& BT_WhiteList, int & ExitValue, bool bMonitorLoggingDirectory)
5360
5363
{
5361
5364
int rVal (0 );
5362
- time_t TimeStart (0 ), TimeLog (0 ), TimeSVG (0 );
5365
+ time_t TimeStart (0 ), TimeLog (0 ), TimeSVG (0 ), TimeAdvertisment ( 0 ) ;
5363
5366
std::ostringstream ssOutput;
5364
5367
// Main loop
5365
5368
bRun = true ;
@@ -5474,6 +5477,7 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
5474
5477
{
5475
5478
const std::string dbus_msg_Member (dbus_message_get_member (dbus_msg)); // https://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaf5c6b705c53db07a5ae2c6b76f230cf9
5476
5479
bdaddr_t localBTAddress ({ 0 });
5480
+ TimeAdvertisment = TimeNow;
5477
5481
if (!dbus_msg_Member.compare (" InterfacesAdded" ))
5478
5482
bluez_dbus_msg_InterfacesAdded (dbus_msg, localBTAddress, BT_WhiteList, TimeNow);
5479
5483
else if (!dbus_msg_Member.compare (" PropertiesChanged" ))
@@ -5547,6 +5551,20 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set<bdaddr_t>& BT_W
5547
5551
for (auto & [UUID, Path] : PathUUID)
5548
5552
std::cout << " [-------------------] [" << ba2string (btAddress) << " ] " << UUID << " " << Path << std::endl;
5549
5553
}
5554
+ if (MaxMinutesBetweenBluetoothAdvertisments > 0 )
5555
+ {
5556
+ if (difftime (TimeNow, TimeAdvertisment) > MaxMinutesBetweenBluetoothAdvertisments * 60 ) // Hack to force scanning restart regularly
5557
+ {
5558
+ if (ConsoleVerbosity > 0 )
5559
+ std::cout << " [" << getTimeISO8601 (true ) << " ] No recent Bluetooth LE Advertisments! (> " << MaxMinutesBetweenBluetoothAdvertisments << " Minutes)" << std::endl;
5560
+ // btRVal = bt_LEScan(BlueToothDevice_Handle, true, BT_WhiteList, HCI_Passive_Scanning);
5561
+ // if (btRVal < 0)
5562
+ // {
5563
+ bRun = false ; // rely on inetd to restart entire process
5564
+ ExitValue = EXIT_FAILURE;
5565
+ // }
5566
+ }
5567
+ }
5550
5568
#ifdef DEBUG
5551
5569
} while (bRun && difftime (TimeNow, TimeStart) < 300 ); // Maintain DBus connection for no more than 5 minutes
5552
5570
#else
@@ -5619,13 +5637,14 @@ static void usage(int argc, char **argv)
5619
5637
std::cout << " -d | --download days Periodically attempt to connect and download stored data" << std::endl;
5620
5638
std::cout << " -n | --no-bluetooth Monitor Logging Directory and process logs without Bluetooth Scanning" << std::endl;
5621
5639
std::cout << " -M | --monitor Monitor Logging Directory" << std::endl;
5622
- #ifdef _BLUEZ_HCI_
5640
+ std::cout << " -r | --restart Maximum minutes between Bluetooth advertisments [" << MaxMinutesBetweenBluetoothAdvertisments << " ]" << std::endl;
5641
+ #ifdef _BLUEZ_HCI_
5623
5642
std::cout << " -H | --HCI Prefer deprecated BlueZ HCI interface instead of DBus" << std::endl;
5624
5643
std::cout << " -p | --passive Bluetooth LE Passive Scanning" << std::endl;
5625
5644
#endif // _BLUEZ_HCI_
5626
5645
std::cout << std::endl;
5627
5646
}
5628
- static const char short_options[] = " hl:t:v:m:o:C:a:f:s:i:T:cb:x:d::pnHM " ;
5647
+ static const char short_options[] = " hl:t:v:m:o:C:a:f:s:i:T:cb:x:d::pnHMR: " ;
5629
5648
static const struct option long_options[] = {
5630
5649
{ " help" , no_argument, NULL , ' h' },
5631
5650
{ " log" , required_argument, NULL , ' l' },
@@ -5647,6 +5666,7 @@ static const struct option long_options[] = {
5647
5666
{ " no-bluetooth" ,no_argument, NULL , ' n' },
5648
5667
{ " HCI" , no_argument, NULL , ' H' },
5649
5668
{ " monitor" ,no_argument, NULL , ' M' },
5669
+ { " restart" ,required_argument, NULL , ' R' },
5650
5670
{ 0 , 0 , 0 , 0 }
5651
5671
};
5652
5672
// ///////////////////////////////////////////////////////////////////////////
@@ -5767,6 +5787,11 @@ int main(int argc, char **argv)
5767
5787
case ' M' :
5768
5788
bMonitorLoggingDirectory = true ;
5769
5789
break ;
5790
+ case ' R' :
5791
+ try { MaxMinutesBetweenBluetoothAdvertisments = std::stoi (optarg); }
5792
+ catch (const std::invalid_argument& ia) { std::cerr << " Invalid argument: " << ia.what () << std::endl; exit (EXIT_FAILURE); }
5793
+ catch (const std::out_of_range& oor) { std::cerr << " Out of Range error: " << oor.what () << std::endl; exit (EXIT_FAILURE); }
5794
+ break ;
5770
5795
default :
5771
5796
usage (argc, argv);
5772
5797
exit (EXIT_FAILURE);
@@ -5841,7 +5866,7 @@ int main(int argc, char **argv)
5841
5866
SignalHandlerPointer previousHandlerSIGHUP = std::signal (SIGHUP, SignalHandlerSIGHUP); // Install Hangup signal handler
5842
5867
// /////////////////////////////////////////////////////////////////////////////////////////////
5843
5868
if (!bUse_HCI_Interface) // BlueZ over DBus is the recommended method of Bluetooth
5844
- bUse_HCI_Interface = (0 != BlueZ_DBus_Mainloop (ControllerAddress, BT_WhiteList, bMonitorLoggingDirectory));
5869
+ bUse_HCI_Interface = (0 != BlueZ_DBus_Mainloop (ControllerAddress, BT_WhiteList, ExitValue, bMonitorLoggingDirectory));
5845
5870
#ifdef _BLUEZ_HCI_
5846
5871
if (bUse_HCI_Interface) // The HCI interface for bluetooth is deprecated, with BlueZ over DBus being preferred
5847
5872
BlueZ_HCI_MainLoop (ControllerAddress, BT_WhiteList, ExitValue, bMonitorLoggingDirectory, bUse_HCI_Passive);
0 commit comments