mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-27 15:35:30 +00:00
daemon/binding/python: event factory, makefile
This commit is contained in:
parent
760c66926a
commit
b1f6df3b39
12
platform/daemon/binding/python/Makefile
Normal file
12
platform/daemon/binding/python/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
BTSTACK_ROOT=../../../../
|
||||
|
||||
all: btstack/command_builder.py btstack/event_factory.py
|
||||
|
||||
btstack/command_builder.py: ${BTSTACK_ROOT}/tool/python_generator.py ${BTSTACK_ROOT}/src/bluetooth.h ${BTSTACK_ROOT}/src/btstack_defines.h
|
||||
${BTSTACK_ROOT}/tool/python_generator.py
|
||||
|
||||
btstack/event_factory.py: ${BTSTACK_ROOT}/tool/python_generator.py ${BTSTACK_ROOT}/src/bluetooth.h ${BTSTACK_ROOT}/src/btstack_defines.h
|
||||
${BTSTACK_ROOT}/tool/python_generator.py
|
||||
|
||||
clean:
|
||||
rm -f btstack/command_builder.py btstack/event_factory.py
|
@ -47,10 +47,10 @@ class BTstackClient(btstack.command_builder.CommandBuilder):
|
||||
# receive packet header: packet type, channel, len
|
||||
header = self.btstack_server_socket.recv(6)
|
||||
(packet_type, channel, length) = struct.unpack("<HHH", header)
|
||||
# print_hex(header)
|
||||
payload = self.btstack_server_socket.recv(length)
|
||||
# print_hex(payload)
|
||||
if packet_type == 1:
|
||||
# print_hex(header+payload)
|
||||
if packet_type == btstack.btstack_types.Packet.HCI_EVENT_PACKET:
|
||||
event = btstack.event_factory.event_for_payload(payload)
|
||||
# self.packet_handler(event)
|
||||
print(event)
|
||||
# print(event)
|
||||
if not self.packet_handler == None:
|
||||
self.packet_handler(event)
|
||||
|
@ -1,10 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from btstack import btstack_server, btstack_client
|
||||
from btstack import btstack_server, btstack_client, event_factory
|
||||
|
||||
def packet_handler(packet):
|
||||
print("received packet")
|
||||
print(packet)
|
||||
global btstack_client
|
||||
if isinstance(packet, event_factory.BTstackEventState):
|
||||
print("BTstack state: %u" % packet.get_state())
|
||||
if packet.get_state() == 2:
|
||||
print('BTstack up and running, starting scan')
|
||||
btstack_client.gap_le_scan_start()
|
||||
if isinstance(packet, event_factory.GAPEventAdvertisingReport):
|
||||
print(packet)
|
||||
|
||||
# Conrtrol for BTstack Server
|
||||
btstack_server = btstack_server.BTstackServer()
|
||||
@ -22,5 +28,4 @@ btstack_client = btstack_client.BTstackClient()
|
||||
btstack_client.connect()
|
||||
btstack_client.register_packet_handler(packet_handler)
|
||||
btstack_client.btstack_set_power_mode(1)
|
||||
btstack_client.gap_le_scan_start()
|
||||
btstack_client.run()
|
||||
|
@ -70,9 +70,9 @@ def event_for_payload(payload):
|
||||
# LE Subevent
|
||||
if event_type == 0x3e:
|
||||
subevent_type = payload[2]
|
||||
event_class = le_event_class_for_type[subevent_type]
|
||||
event_class = le_event_class_for_type.get(subevent_type, event_class)
|
||||
else:
|
||||
event_class = event_class_for_type[event_type]
|
||||
event_class = event_class_for_type.get(event_type, event_class)
|
||||
return event_class(payload)
|
||||
'''
|
||||
event_factory_event = \
|
||||
@ -83,7 +83,12 @@ event_factory_subevent = \
|
||||
'''
|
||||
|
||||
event_header = '''
|
||||
import struct
|
||||
import btstack.btstack_types
|
||||
|
||||
def hex_string(bytes):
|
||||
return " ".join([('%02x' % a) for a in bytes])
|
||||
|
||||
'''
|
||||
|
||||
event_template = \
|
||||
@ -103,13 +108,11 @@ event_getter = \
|
||||
{1}
|
||||
'''
|
||||
|
||||
event_getter_data = \
|
||||
'''# len = self.get_{length_name}()
|
||||
return self.payload[{offset},{offset}+len]
|
||||
event_getter_data = '''return self.payload[{offset}:{offset}+self.get_{length_name}()]
|
||||
'''
|
||||
|
||||
event_getter_data_fixed = \
|
||||
'''return self.payload[{offset},{offset}+{size}]
|
||||
'''return self.payload[{offset}:{offset}+{size}]
|
||||
'''
|
||||
|
||||
event_to_string = \
|
||||
@ -242,11 +245,11 @@ def create_event(fout, event_name, format, args):
|
||||
param_read = {
|
||||
'1' : 'return self.payload[{offset}]',
|
||||
'J' : 'return self.payload[{offset}]',
|
||||
'2' : 'return struct.unpack("<H", self.payload[{offset}, {offset}+2])',
|
||||
'H' : 'return struct.unpack("<H", self.payload[{offset}, {offset}+2])',
|
||||
'L' : 'return struct.unpack("<H", self.payload[{offset}, {offset}+2])',
|
||||
'2' : 'return struct.unpack("<H", self.payload[{offset} : {offset}+2])',
|
||||
'H' : 'return struct.unpack("<H", self.payload[{offset} : {offset}+2])',
|
||||
'L' : 'return struct.unpack("<H", self.payload[{offset} : {offset}+2])',
|
||||
'3' : 'return btstack.btstack_types.unpack24(self.payload[{offset}:3])',
|
||||
'4' : 'return struct.unpack("<I", self.payload[{offset}, {offset}+4])',
|
||||
'4' : 'return struct.unpack("<I", self.payload[{offset} : {offset}+4])',
|
||||
'B' : 'return btstack.btstack_types.BD_ADDR(self.payload[{offset}:6])',
|
||||
'X' : 'return btstack.btstack_types.GATTService(self.payload[{offset}:20])',
|
||||
'Y' : 'return btstack.btstack_types.GATTCharacteristic(self.payload[{offset}:24])',
|
||||
|
Loading…
x
Reference in New Issue
Block a user