print "-- open bluetooth device" sock = ble.bluez.hci_open_dev(conf.BT_DEV_ID) if debug: print "-- ble thread started" except Exception as e: print "error accessing bluetooth device: ", str(conf.BT_DEV_ID) print str(e) sys.exit(1) # set ble scan parameters try: if debug: print "-- set ble scan parameters" # ble.hci_le_set_scan_parameters(sock) ble.hci_le_set_scan_parameters( sock, scan_type=ble.LE_SCAN_PASSIVE, own_bdaddr_type=ble.LE_PUBLIC_ADDRESS) if debug: print "-- set ble scan parameters : success" except Exception as e: print "failed to set scan parameter!!" print str(e) sys.exit(1) # start ble scan try: if debug: print "-- enable ble scan" ble.hci_le_enable_scan(sock) if debug: print "-- ble scan started"
def evsensor(): try: # process command line arguments debug = False args = arg_parse() if args.debug: debug = True # reset bluetooth functionality try: if debug: print "-- reseting bluetooth device" ble.reset_hci() if debug: print "-- reseting bluetooth device : success" except Exception as e: print "error enabling bluetooth device" print str(e) sys.exit(1) # initialize bluetooth socket try: if debug: print "-- open bluetooth device" sock = ble.bluez.hci_open_dev(conf.BT_DEV_ID) if debug: print "-- ble thread started" except Exception as e: print "error accessing bluetooth device: ", str(conf.BT_DEV_ID) print str(e) sys.exit(1) # set ble scan parameters try: if debug: print "-- set ble scan parameters" ble.hci_le_set_scan_parameters(sock) if debug: print "-- set ble scan parameters : success" except Exception as e: print "failed to set scan parameter!!" print str(e) sys.exit(1) # start ble scan try: if debug: print "-- enable ble scan" ble.hci_le_enable_scan(sock) if debug: print "-- ble scan started" except Exception as e: print "failed to activate scan!!" print str(e) sys.exit(1) global flag_scanning_started flag_scanning_started = True print("envsensor_observer : complete initialization") print "" # preserve old filter setting old_filter = sock.getsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, 14) # perform a device inquiry on bluetooth device #0 # The inquiry should last 8 * 1.28 = 10.24 seconds # before the inquiry is performed, bluez should flush its cache of # previously discovered devices flt = ble.bluez.hci_filter_new() ble.bluez.hci_filter_all_events(flt) ble.bluez.hci_filter_set_ptype(flt, ble.bluez.HCI_EVENT_PKT) sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, flt) # activate timer for sensor status evaluation timer = threading.Timer(conf.CHECK_SENSOR_STATE_INTERVAL_SECONDS, eval_sensor_state) timer.setDaemon(True) timer.start() global flag_update_sensor_status while True: # parse ble event parse_events(sock) if flag_update_sensor_status: print_sensor_state() flag_update_sensor_status = False except Exception as e: print "Exception: " + str(e) import traceback traceback.print_exc() sys.exit(1) finally: if flag_scanning_started: # restore old filter setting sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, old_filter) ble.hci_le_disable_scan(sock) print "Exit"
def parse_events(sock, loop_count=100): flt = bluez.hci_filter_new() bluez.hci_filter_all_events(flt) bluez.hci_filter_set_ptype(flt, bluez.HCI_EVENT_PKT) sock.setsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, flt) for i in range(loop_count): pkt = sock.recv(255) parsed_packet = ble.hci_le_parse_response_packet(pkt) if ( "bluetooth_le_subevent_name" in parsed_packet and parsed_packet["bluetooth_le_subevent_name"] == "EVT_LE_ADVERTISING_REPORT" ): for report in parsed_packet["advertising_reports"]: print "Found BLE device:", report["peer_bluetooth_address"] for k, v in report.items(): print "\t%s: %s" % (k, v) dev_id = 0 try: sock = bluez.hci_open_dev(dev_id) except: print "Error accessing bluetooth device", dev_id sys.exit(1) ble.hci_le_set_scan_parameters(sock) ble.hci_le_enable_scan(sock) parse_events(sock, 30) ble.hci_le_disable_scan(sock)
try: if debug: print "-- open bluetooth device" sock = ble.bluez.hci_open_dev(conf.BT_DEV_ID) if debug: print "-- ble thread started" except Exception as e: print "error accessing bluetooth device: ", str(conf.BT_DEV_ID) print str(e) sys.exit(1) # set ble scan parameters try: if debug: print "-- set ble scan parameters" ble.hci_le_set_scan_parameters(sock) if debug: print "-- set ble scan parameters : success" except Exception as e: print "failed to set scan parameter!!" print str(e) sys.exit(1) # start ble scan try: if debug: print "-- enable ble scan" ble.hci_le_enable_scan(sock) if debug: print "-- ble scan started" except Exception as e:
def evsensor(): # print('called') sys.path.append("..") import util try: # process command line arguments debug = False args = arg_parse() if args.debug: debug = False # reset bluetooth functionality try: if debug: print "-- reseting bluetooth device" ble.reset_hci() if debug: print "-- reseting bluetooth device : success" except Exception as e: util.Logprint(4, "error enabling bluetooth device") # print "error enabling bluetooth device" # print str(e) sys.exit(1) # initialize bluetooth socket try: if debug: print "-- open bluetooth device" sock = ble.bluez.hci_open_dev(conf.BT_DEV_ID) if debug: print "-- ble thread started" except Exception as e: util.Logprint(4, "error accessing bluetooth device") # print "error accessing bluetooth device: ", str(conf.BT_DEV_ID) # print str(e) sys.exit(1) # set ble scan parameters try: if debug: print "-- set ble scan parameters" ble.hci_le_set_scan_parameters(sock) if debug: print "-- set ble scan parameters : success" except Exception as e: util.Logprint(4, "failed to set scan parameter!!") # print "failed to set scan parameter!!" # print str(e) sys.exit(1) # start ble scan try: if debug: print "-- enable ble scan" ble.hci_le_enable_scan(sock) if debug: print "-- ble scan started" except Exception as e: util.Logprint(4, "failed to activate scan!!") # print "failed to activate scan!!" # print str(e) sys.exit(1) global flag_scanning_started flag_scanning_started = True util.Logprint(1, "envsensor_observer : complete initialization") # print ("envsensor_observer : complete initialization") # print "" # preserve old filter setting old_filter = sock.getsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, 14) # perform a device inquiry on bluetooth device #0 # The inquiry should last 8 * 1.28 = 10.24 seconds # before the inquiry is performed, bluez should flush its cache of # previously discovered devices flt = ble.bluez.hci_filter_new() ble.bluez.hci_filter_all_events(flt) ble.bluez.hci_filter_set_ptype(flt, ble.bluez.HCI_EVENT_PKT) sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, flt) # activate timer for sensor status evaluation with open('/home/nvidia/Horus/config.cnf') as f: cnf = json.load(f) timer = threading.Timer( int(cnf['check_sensor_state_interval_seconds']), eval_sensor_state) timer.setDaemon(True) timer.start() global flag_update_sensor_status while True: # parse ble event parse_events(sock) if flag_update_sensor_status: # print_sensor_state() flag_update_sensor_status = False except Exception as e: # print "Exception: " + str(e) import traceback traceback.print_exc() sys.exit(1) finally: if flag_scanning_started: # restore old filter setting sock.setsockopt(ble.bluez.SOL_HCI, ble.bluez.HCI_FILTER, old_filter) ble.hci_le_disable_scan(sock)