11// ble_sensor_mqtt_pub.c
22// gcc -o ble_sensor_mqtt_pub ble_sensor_mqtt_pub.c -l bluetooth -l paho-mqtt3c
3- // 202012131603
3+ // 202102030607
44//
55// decode BLE temperature sensor temperature and humidity data from BLE advertising packets
66// and publish to MQTT
1818//
1919
2020#define VERSION_MAJOR 2
21- #define VERSION_MINOR 12
21+ #define VERSION_MINOR 13
2222// why is it so hard to get the base name of the program withOUT the .c extension!!!!!!!
2323
2424#define PROGRAM_NAME "ble_sensor_mqtt_pub"
@@ -256,7 +256,7 @@ void connlost(void *context, char *cause)
256256
257257 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d MQTT Server Connection lost" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
258258 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
259- syslog (LOG_ERR , log_message );
259+ syslog (LOG_ERR , "%s" , log_message );
260260 fprintf (stderr , "MQTT Server Connection lost, cause: %s\n" , cause );
261261 exit (1 );
262262}
@@ -344,7 +344,7 @@ int main(int argc, char *argv[])
344344
345345 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Couldn't enumerate HCI devices: %s" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , strerror (errno ));
346346 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
347- syslog (LOG_ERR , log_message );
347+ syslog (LOG_ERR , "%s" , log_message );
348348 fprintf (stderr , "Couldn't enumerate HCI devices: %s" , strerror (errno ));
349349 exit (1 );
350350 }
@@ -357,7 +357,7 @@ int main(int argc, char *argv[])
357357 {
358358 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Start program with four arguments, the bluetooth adapter number, scan type (0=passive, 1=active), BLE scan Window (0 for default), BLE scan Interval (0 for default)\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
359359 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
360- syslog (LOG_ERR , log_message );
360+ syslog (LOG_ERR , "%s" , log_message );
361361 fprintf (stderr , "Start program with four arguments, the bluetooth adapter number, scan type (0=passive, 1=active), BLE scan Window (0 for default), BLE scan Interval (0 for default)\n" );
362362 exit (1 );
363363 }
@@ -397,7 +397,7 @@ int main(int argc, char *argv[])
397397 {
398398 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Enter bluetooth adapter number between 0 and %u !!\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , hci_devs_num - 1 );
399399 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
400- syslog (LOG_ERR , log_message );
400+ syslog (LOG_ERR , "%s" , log_message );
401401 fprintf (stderr , "Enter bluetooth adapter number between 0 and %u !!\n" , hci_devs_num - 1 );
402402 exit (1 );
403403 }
@@ -411,7 +411,7 @@ int main(int argc, char *argv[])
411411 openlog (PROGRAM_NAME , LOG_CONS | LOG_PID | LOG_NDELAY , LOG_LOCAL1 );
412412 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Starting." , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
413413 send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
414- syslog (LOG_INFO , log_message );
414+ syslog (LOG_INFO , "%s" , log_message );
415415
416416 // maximum number of sensors
417417 #define MAXIMUM_UNITS 40
@@ -473,7 +473,7 @@ int main(int argc, char *argv[])
473473 {
474474 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Too many devices in configuration file, limit is : %d\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , MAXIMUM_UNITS );
475475 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
476- syslog (LOG_ERR , log_message );
476+ syslog (LOG_ERR , "%s" , log_message );
477477 fprintf (stderr , "Too many devices in configuration file, limit is : %d\n" , MAXIMUM_UNITS );
478478 exit (1 );
479479
@@ -528,7 +528,7 @@ int main(int argc, char *argv[])
528528 {
529529 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d No blank rows in configuration file are allowed\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
530530 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
531- syslog (LOG_ERR , log_message );
531+ syslog (LOG_ERR , "%s" , log_message );
532532 fprintf (stderr , "No blank rows in configuration file are allowed\n" );
533533 exit (1 );
534534 }
@@ -563,7 +563,7 @@ int main(int argc, char *argv[])
563563 {
564564 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d failed to connect to MQTT server" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
565565 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
566- syslog (LOG_ERR , log_message );
566+ syslog (LOG_ERR , "%s" , log_message );
567567 fprintf (stderr , "Failed to connect to MQTT server, return code %d\n" , rc );
568568 exit (1 );
569569 }
@@ -574,13 +574,13 @@ int main(int argc, char *argv[])
574574
575575 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Bluetooth Adapter : %u has MAC address : %s\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , bluetooth_adapter_number , bluetooth_adapter_mac );
576576 send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
577- syslog (LOG_INFO , log_message );
577+ syslog (LOG_INFO , "%s" , log_message );
578578 fprintf (stdout , "Bluetooth Adapter : %u has MAC address : %s\n" , bluetooth_adapter_number , bluetooth_adapter_mac );
579579
580580 if ( bluetooth_device < 0 ) {
581581 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d failed to open HCI device" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
582582 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
583- syslog (LOG_ERR , log_message );
583+ syslog (LOG_ERR , "%s" , log_message );
584584 fprintf (stderr , "Failed to open HCI device, return code %d\n" , bluetooth_device );
585585 exit (1 );
586586 }
@@ -589,17 +589,17 @@ int main(int argc, char *argv[])
589589
590590 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Advertising scan type (0=passive, 1=active): %u\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , ble_scan_type );
591591 send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
592- syslog (LOG_INFO , log_message );
592+ syslog (LOG_INFO , "%s" , log_message );
593593 fprintf (stdout , "Advertising scan type (0=passive, 1=active): %u\n" , ble_scan_type );
594594
595595 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Advertising scan window : %u %.1f ms\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , ble_scan_window , ble_scan_window * 0.625 );
596596 send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
597- syslog (LOG_INFO , log_message );
597+ syslog (LOG_INFO , "%s" , log_message );
598598 fprintf (stdout , "Advertising scan window : %4u, %4.1f ms\n" , ble_scan_window , ble_scan_window * 0.625 );
599599
600600 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Advertising scan interval : %u %.1f ms\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , ble_scan_interval , ble_scan_interval * 0.625 );
601601 send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
602- syslog (LOG_INFO , log_message );
602+ syslog (LOG_INFO , "%s" , log_message );
603603 fprintf (stdout , "Advertising scan interval : %4u, %4.1f ms\n" , ble_scan_interval , ble_scan_interval * 0.625 );
604604
605605
@@ -621,7 +621,7 @@ int main(int argc, char *argv[])
621621 hci_close_dev (bluetooth_device );
622622 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to set scan parameters data" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
623623 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
624- syslog (LOG_ERR , log_message );
624+ syslog (LOG_ERR , "%s" , log_message );
625625 fprintf (stderr , "Failed to set scan parameters data, you must run this program as ROOT, return code %d\n" , ret );
626626 exit (1 );
627627 }
@@ -639,7 +639,7 @@ int main(int argc, char *argv[])
639639 hci_close_dev (bluetooth_device );
640640 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to set event mask" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
641641 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
642- syslog (LOG_ERR , log_message );
642+ syslog (LOG_ERR , "%s" , log_message );
643643 fprintf (stderr , "Failed to set event mask, return code %d\n" , ret );
644644 exit (1 );
645645 }
@@ -658,7 +658,7 @@ int main(int argc, char *argv[])
658658 hci_close_dev (bluetooth_device );
659659 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to enable scan" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
660660 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
661- syslog (LOG_ERR , log_message );
661+ syslog (LOG_ERR , "%s" , log_message );
662662 fprintf (stderr , "Failed to enable scan, return code %d\n" , ret );
663663 exit (1 );
664664 }
@@ -674,14 +674,14 @@ int main(int argc, char *argv[])
674674 hci_close_dev (bluetooth_device );
675675 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Could not set socket options" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
676676 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
677- syslog (LOG_ERR , log_message );
677+ syslog (LOG_ERR , "%s" , log_message );
678678 fprintf (stderr , "Could not set socket options, return code %d\n" , ret );
679679 exit (1 );
680680 }
681681
682682 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Scanning...." , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
683683 send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
684- syslog (LOG_INFO , log_message );
684+ syslog (LOG_INFO , "%s" , log_message );
685685// fprintf(stdout, "%s v%2d.%02d\n", PROGRAM_NAME, VERSION_MAJOR, VERSION_MINOR);
686686 fprintf (stdout , "Scanning....\n" );
687687 fflush (stdout );
@@ -801,7 +801,7 @@ int main(int argc, char *argv[])
801801 fprintf (stderr , "MQTT payload too long: %d\n" , payload_length );
802802 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d MQTT payload too long: %d\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , payload_length );
803803 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
804- syslog (LOG_ERR , log_message );
804+ syslog (LOG_ERR , "%s" , log_message );
805805 exit (1 );
806806 }
807807
@@ -1763,24 +1763,22 @@ int main(int argc, char *argv[])
17631763 // get rssi
17641764 int rssi_int = (signed char ) (int8_t )adv_info -> data [adv_info -> length ];
17651765
1766- if (logging_level == LOG_DEBUG )
1767- {
1768- fprintf (stdout , "=========\n" );
1769- fprintf (stdout , "Current local time and date: %s" , asctime (time_packet_received ) );
1770- fprintf (stdout , "mac address = %s location = %s device type = %d " , addr , device_units_location [mac_index ], device_units_type [mac_index ]);
1771- fprintf (stdout , "advertising_packet_type = %03d\n" , advertising_packet_type );
1772- // print whole packet
1773- printf ("==>0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 \n" );
1774- printf ("==>0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1775- printf ("==> 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2\n" );
1776- printf ("==> 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1777- printf ("==>" );
1778- for (n = 0 ; n < bluetooth_adv_packet_length ; n ++ )
1779- printf ("%02X" ,(unsigned char )ble_adv_buf [n ]);
1780- printf ("\n" );
1781- printf ("==>__________ad________________________mmmmmmmmmmmmtttthhbbzbzbccrr\n" );
1782- fprintf (stdout , "rssi = %03d\n" , rssi_int );
1783- }
1766+
1767+ fprintf (stdout , "=========\n" );
1768+ fprintf (stdout , "Current local time and date: %s" , asctime (time_packet_received ) );
1769+ fprintf (stdout , "mac address = %s location = %s device type = %d " , addr , device_units_location [mac_index ], device_units_type [mac_index ]);
1770+ fprintf (stdout , "advertising_packet_type = %03d\n" , advertising_packet_type );
1771+ // print whole packet
1772+ printf ("==>0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 \n" );
1773+ printf ("==>0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1774+ printf ("==> 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2\n" );
1775+ printf ("==> 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1776+ printf ("==>" );
1777+ for (n = 0 ; n < bluetooth_adv_packet_length ; n ++ )
1778+ printf ("%02X" ,(unsigned char )ble_adv_buf [n ]);
1779+ printf ("\n" );
1780+ printf ("==>__________ad________________________mmmmmmmmmmmmtttthhbbzbzbccrr\n" );
1781+ fprintf (stdout , "rssi = %03d\n" , rssi_int );
17841782 }
17851783 fflush (stdout );
17861784 }
@@ -1799,7 +1797,7 @@ int main(int argc, char *argv[])
17991797 fprintf (stdout , "\n<ctrl>-c signal received, exiting.\n" );
18001798 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d <ctrl>-c signal received, exiting." , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
18011799 send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
1802- syslog (LOG_INFO , log_message );
1800+ syslog (LOG_INFO , "%s" , log_message );
18031801
18041802
18051803 // Disable scanning.
@@ -1812,7 +1810,7 @@ int main(int argc, char *argv[])
18121810 hci_close_dev (bluetooth_device );
18131811 snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to disable scan" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
18141812 send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
1815- syslog (LOG_ERR , log_message );
1813+ syslog (LOG_ERR , "%s" , log_message );
18161814 fprintf (stdout , "Failed to disable scan, return code %d\n" , ret );
18171815 exit (1 );
18181816 }
0 commit comments