Ejemplo n.º 1
0
def parse_events(sock):
    global sensor_list

    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'):

        if debug:
            for report in parsed_packet["advertising_reports"]:
                print "----------------------------------------------------"
                print "Found BLE device:", report['peer_bluetooth_address']
                print "Raw Advertising Packet:"
                print ble.packet_as_hex_string(pkt,
                                               flag_with_spacing=True,
                                               flag_force_capitalize=True)
                print ""
                for k, v in report.items():
                    if k == "payload_binary":
                        continue
                    print "\t%s: %s" % (k, v)
                print ""

        for report in parsed_packet["advertising_reports"]:
            if (ble.verify_beacon_packet(report)):
                sensor = envsensor.SensorBeacon(
                    report["peer_bluetooth_address_s"],
                    ble.classify_beacon_packet(report), GATEWAY,
                    report["payload_binary"])

                index = find_sensor_in_list(sensor, sensor_list)

                if debug:
                    print("\t--- sensor data ---")
                    sensor.debug_print()
                    print ""

                lock = threading.Lock()
                lock.acquire()
                conn = create_engine(
                    'mysql://*****:*****@localhost:3306/miner?charset=utf8')

                if (index != -1):  # BT Address found in sensor_list
                    if sensor.check_diff_seq_num(sensor_list[index]):
                        conn.execute(sensor.sqlInsert())
                        pass
                    sensor.update(sensor_list[index])
                else:  # new SensorBeacon
                    sensor_list.append(sensor)
                    conn.execute(sensor.sqlInsert())
                lock.release()
            else:
                pass
    else:
        pass
    return
Ejemplo n.º 2
0
def parse_events(sock, loop_count=10):
    global sensor_list
    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'):

        if debug:
            for report in parsed_packet["advertising_reports"]:
                print("----------------------------------------------------")
                print("Found BLE device:", report['peer_bluetooth_address'])
                print("Raw Advertising Packet:")
                print(
                    ble.packet_as_hex_string(pkt,
                                             flag_with_spacing=True,
                                             flag_force_capitalize=True))
                print("")
                for k, v in report.items():
                    if k == "payload_binary":
                        continue
                    print("\t%s: %s" % (k, v))
                print("")

        for report in parsed_packet["advertising_reports"]:
            if (ble.verify_beacon_packet(report)):
                sensor = envsensor.SensorBeacon(
                    report["peer_bluetooth_address_s"],
                    ble.classify_beacon_packet(report), device_id,
                    report["payload_binary"])

                index = find_sensor_in_list(sensor, sensor_list)

                if debug:
                    print("\t--- sensor data ---")
                    sensor.debug_print()
                    print("")

                lock = threading.Lock()
                lock.acquire()

                if (index != -1):  # BT Address found in sensor_list
                    if sensor.check_diff_seq_num(sensor_list[index]):
                        handling_data(sensor)
                    sensor.update(sensor_list[index])
                else:  # new SensorBeacon
                    sensor_list.append(sensor)
                    handling_data(sensor)
                lock.release()
            else:
                pass
    else:
        pass
    return
Ejemplo n.º 3
0
def parse_events(sock, loop_count=10):
    global sensor_list

    pkt = sock.recv(255)

    # Raw avertise packet data from Bluez scan
    # Packet Type (1byte) + BT Event ID (1byte) + Packet Length (1byte) +
    # BLE sub-Event ID (1byte) + Number of Advertising reports (1byte) +
    # Report type ID (1byte) + BT Address Type (1byte) + BT Address (6byte) +
    # Data Length (1byte) + Data ((Data Length)byte) + RSSI (1byte)
    #
    # Packet Type = 0x04
    # BT Event ID = EVT_LE_META_EVENT = 0x3E (BLE events)
    # (All LE commands result in a metaevent, specified by BLE sub-Event ID)
    # BLE sub-Event ID = {
    #                       EVT_LE_CONN_COMPLETE = 0x01
    #                       EVT_LE_ADVERTISING_REPORT = 0x02
    #                       EVT_LE_CONN_UPDATE_COMPLETE = 0x03
    #                       EVT_LE_READ_REMOTE_USED_FEATURES_COMPLETE = 0x04
    #                       EVT_LE_LTK_REQUEST = 0x05
    #                     }
    # Number of Advertising reports = 0x01 (normally)
    # Report type ID = {
    #                       LE_ADV_IND = 0x00
    #                       LE_ADV_DIRECT_IND = 0x01
    #                       LE_ADV_SCAN_IND = 0x02
    #                       LE_ADV_NONCONN_IND = 0x03
    #                       LE_ADV_SCAN_RSP = 0x04
    #                   }
    # BT Address Type = {
    #                       LE_PUBLIC_ADDRESS = 0x00
    #                       LE_RANDOM_ADDRESS = 0x01
    #                    }
    # Data Length = 0x00 - 0x1F
    # * Maximum Data Length of an advertising packet = 0x1F

    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'):

        if debug:
            for report in parsed_packet["advertising_reports"]:
                print "----------------------------------------------------"
                print "Found BLE device:", report['peer_bluetooth_address']
                print "Raw Advertising Packet:"
                print ble.packet_as_hex_string(pkt,
                                               flag_with_spacing=True,
                                               flag_force_capitalize=True)
                print ""
                for k, v in report.items():
                    if k == "payload_binary":
                        continue
                    print "\t%s: %s" % (k, v)
                print ""

        for report in parsed_packet["advertising_reports"]:
            if (ble.verify_beacon_packet(report)):
                sensor = envsensor.SensorBeacon(
                    report["peer_bluetooth_address_s"],
                    ble.classify_beacon_packet(report), GATEWAY,
                    report["payload_binary"])

                index = find_sensor_in_list(sensor, sensor_list)

                if debug:
                    print("\t--- sensor data ---")
                    sensor.debug_print()
                    print ""

                lock = threading.Lock()
                lock.acquire()

                if (index != -1):  # BT Address found in sensor_list
                    if sensor.check_diff_seq_num(sensor_list[index]):
                        handling_data(sensor)
                    sensor.update(sensor_list[index])
                else:  # new SensorBeacon
                    sensor_list.append(sensor)
                    handling_data(sensor)
                lock.release()
            else:
                pass
    else:
        pass
    return