Merge branch 'bugfix/ci_ble_wifi_example_test' into 'master'

Fix for ble and wifi example test

Closes IDFCI-543

See merge request espressif/esp-idf!13223
This commit is contained in:
Angus Gratton 2021-05-29 06:09:40 +00:00
commit 2ef16a3861
10 changed files with 953 additions and 982 deletions

View File

@ -17,9 +17,14 @@
from __future__ import print_function
import os
import re
import subprocess
import uuid
import threading
import traceback
try:
import Queue
except ImportError:
import queue as Queue
import ttfw_idf
from ble import lib_ble_client
@ -30,21 +35,75 @@ from tiny_test_fw import Utility
# > make print_flash_cmd | tail -n 1 > build/download.config
def blecent_client_task(dut):
interface = 'hci0'
adv_host_name = 'BleCentTestApp'
adv_type = 'peripheral'
adv_uuid = '1811'
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
return
'''
Blecent application run:
Create GATT data
Register GATT Application
Create Advertising data
Register advertisement
Start advertising
'''
# Create Gatt Application
# Register GATT Application
ble_client_obj.register_gatt_app()
# Register Advertisement
# Check read/write/subscribe is received from device
ble_client_obj.register_adv(adv_host_name, adv_type, adv_uuid)
# Check dut responses
dut.expect('Connection established', timeout=30)
dut.expect('Service discovery complete; status=0', timeout=30)
dut.expect('GATT procedure initiated: read;', timeout=30)
dut.expect('Read complete; status=0', timeout=30)
dut.expect('GATT procedure initiated: write;', timeout=30)
dut.expect('Write complete; status=0', timeout=30)
dut.expect('GATT procedure initiated: write;', timeout=30)
dut.expect('Subscribe complete; status=0', timeout=30)
dut.expect('received notification;', timeout=30)
class BleCentThread(threading.Thread):
def __init__(self, dut, exceptions_queue):
threading.Thread.__init__(self)
self.dut = dut
self.exceptions_queue = exceptions_queue
def run(self):
try:
blecent_client_task(self.dut)
except RuntimeError:
self.exceptions_queue.put(traceback.format_exc(), block=False)
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT')
def test_example_app_ble_central(env, extra_data):
"""
Steps:
1. Discover Bluetooth Adapter and Power On
2. Connect BLE Device
3. Start Notifications
4. Updated value is retrieved
5. Stop Notifications
"""
# Remove cached bluetooth devices of any previous connections
subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig', 'hci0', 'reset'])
interface = 'hci0'
adv_host_name = 'BleCentTestApp'
adv_iface_index = 0
adv_type = 'peripheral'
adv_uuid = '1811'
subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig','hci0','reset'])
# Acquire DUT
dut = env.get_dut('blecent', 'examples/bluetooth/nimble/blecent', dut_class=ttfw_idf.ESP32DUT)
@ -58,52 +117,23 @@ def test_example_app_ble_central(env, extra_data):
dut.start_app()
dut.reset()
device_addr = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
exceptions_queue = Queue.Queue()
# Starting a py-client in a separate thread
blehr_thread_obj = BleCentThread(dut, exceptions_queue)
blehr_thread_obj.start()
blehr_thread_obj.join()
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface)
if not ble_client_obj:
raise RuntimeError('Get DBus-Bluez object failed !!')
exception_msg = None
while True:
try:
exception_msg = exceptions_queue.get(block=False)
except Queue.Empty:
break
else:
Utility.console_log('\n' + exception_msg)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
raise RuntimeError('Adapter Power On failed !!')
# Write device address to dut
dut.expect('BLE Host Task Started', timeout=60)
dut.write(device_addr + '\n')
'''
Blecent application run:
Create GATT data
Register GATT Application
Create Advertising data
Register advertisement
Start advertising
'''
ble_client_obj.start_advertising(adv_host_name, adv_iface_index, adv_type, adv_uuid)
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
# Check dut responses
dut.expect('Connection established', timeout=60)
dut.expect('Service discovery complete; status=0', timeout=60)
print('Service discovery passed\n\tService Discovery Status: 0')
dut.expect('GATT procedure initiated: read;', timeout=60)
dut.expect('Read complete; status=0', timeout=60)
print('Read passed\n\tSupportedNewAlertCategoryCharacteristic\n\tRead Status: 0')
dut.expect('GATT procedure initiated: write;', timeout=60)
dut.expect('Write complete; status=0', timeout=60)
print('Write passed\n\tAlertNotificationControlPointCharacteristic\n\tWrite Status: 0')
dut.expect('GATT procedure initiated: write;', timeout=60)
dut.expect('Subscribe complete; status=0', timeout=60)
print('Subscribe passed\n\tClientCharacteristicConfigurationDescriptor\n\tSubscribe Status: 0')
if exception_msg:
raise Exception('Blecent thread did not run successfully')
if __name__ == '__main__':

View File

@ -36,37 +36,35 @@ from tiny_test_fw import Utility
# > make print_flash_cmd | tail -n 1 > build/download.config
def blehr_client_task(hr_obj, dut_addr):
def blehr_client_task(hr_obj, dut, dut_addr):
interface = 'hci0'
ble_devname = 'blehr_sensor_1.0'
hr_srv_uuid = '180d'
hr_char_uuid = '2a37'
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface, devname=ble_devname, devaddr=dut_addr)
if not ble_client_obj:
raise RuntimeError('Failed to get DBus-Bluez object')
ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
raise RuntimeError('Adapter Power On failed !!')
return
# Connect BLE Device
is_connected = ble_client_obj.connect()
is_connected = ble_client_obj.connect(
devname=ble_devname,
devaddr=dut_addr)
if not is_connected:
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
raise RuntimeError('Connection to device ' + str(ble_devname) + ' failed !!')
return
# Read Services
services_ret = ble_client_obj.get_services()
if services_ret:
Utility.console_log('\nServices\n')
Utility.console_log(str(services_ret))
else:
# Get services of the connected device
services = ble_client_obj.get_services()
if not services:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Read Services failed')
return
# Get characteristics of the connected device
ble_client_obj.get_chars()
'''
Blehr application run:
@ -74,26 +72,48 @@ def blehr_client_task(hr_obj, dut_addr):
Retrieve updated value
Stop Notifications
'''
blehr_ret = ble_client_obj.hr_update_simulation(hr_srv_uuid, hr_char_uuid)
if blehr_ret:
Utility.console_log('Success: blehr example test passed')
# Get service if exists
service = ble_client_obj.get_service_if_exists(hr_srv_uuid)
if service:
# Get characteristic if exists
char = ble_client_obj.get_char_if_exists(hr_char_uuid)
if not char:
ble_client_obj.disconnect()
return
else:
raise RuntimeError('Failure: blehr example test failed')
ble_client_obj.disconnect()
return
# Start Notify
# Read updated value
# Stop Notify
notify = ble_client_obj.start_notify(char)
if not notify:
ble_client_obj.disconnect()
return
# Check dut responses
dut.expect('subscribe event; cur_notify=1', timeout=30)
dut.expect('subscribe event; cur_notify=0', timeout=30)
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
# Check dut responses
dut.expect('disconnect;', timeout=30)
class BleHRThread(threading.Thread):
def __init__(self, dut_addr, exceptions_queue):
def __init__(self, dut, dut_addr, exceptions_queue):
threading.Thread.__init__(self)
self.dut = dut
self.dut_addr = dut_addr
self.exceptions_queue = exceptions_queue
def run(self):
try:
blehr_client_task(self, self.dut_addr)
except Exception:
blehr_client_task(self, self.dut, self.dut_addr)
except RuntimeError:
self.exceptions_queue.put(traceback.format_exc(), block=False)
@ -107,8 +127,9 @@ def test_example_app_ble_hr(env, extra_data):
4. Updated value is retrieved
5. Stop Notifications
"""
subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig','hci0','reset'])
# Remove cached bluetooth devices of any previous connections
subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig', 'hci0', 'reset'])
# Acquire DUT
dut = env.get_dut('blehr', 'examples/bluetooth/nimble/blehr', dut_class=ttfw_idf.ESP32DUT)
@ -127,7 +148,7 @@ def test_example_app_ble_hr(env, extra_data):
dut_addr = dut.expect(re.compile(r'Device Address: ([a-fA-F0-9:]+)'), timeout=30)[0]
exceptions_queue = Queue.Queue()
# Starting a py-client in a separate thread
blehr_thread_obj = BleHRThread(dut_addr, exceptions_queue)
blehr_thread_obj = BleHRThread(dut, dut_addr, exceptions_queue)
blehr_thread_obj.start()
blehr_thread_obj.join()
@ -141,12 +162,7 @@ def test_example_app_ble_hr(env, extra_data):
Utility.console_log('\n' + exception_msg)
if exception_msg:
raise Exception('Thread did not run successfully')
# Check dut responses
dut.expect('subscribe event; cur_notify=1', timeout=30)
dut.expect('subscribe event; cur_notify=0', timeout=30)
dut.expect('disconnect;', timeout=30)
raise Exception('Blehr thread did not run successfully')
if __name__ == '__main__':

View File

@ -37,73 +37,57 @@ from tiny_test_fw import Utility
# > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
def bleprph_client_task(prph_obj, dut, dut_addr):
def bleprph_client_task(dut, dut_addr):
interface = 'hci0'
ble_devname = 'nimble-bleprph'
srv_uuid = '2f12'
write_value = b'A'
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface, devname=ble_devname, devaddr=dut_addr)
if not ble_client_obj:
raise RuntimeError('Failed to get DBus-Bluez object')
ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
raise RuntimeError('Adapter Power On failed !!')
return
# Connect BLE Device
is_connected = ble_client_obj.connect()
is_connected = ble_client_obj.connect(
devname=ble_devname,
devaddr=dut_addr)
if not is_connected:
# Call disconnect to perform cleanup operations before exiting application
return
# Get services of the connected device
services = ble_client_obj.get_services()
if not services:
ble_client_obj.disconnect()
raise RuntimeError('Connection to device ' + ble_devname + ' failed !!')
return
# Verify service uuid exists
service_exists = ble_client_obj.get_service_if_exists(srv_uuid)
if not service_exists:
ble_client_obj.disconnect()
return
# Get characteristics of the connected device
ble_client_obj.get_chars()
# Read properties of characteristics (uuid, value and permissions)
ble_client_obj.read_chars()
# Write new value to characteristic having read and write permission
# and display updated value
write_char = ble_client_obj.write_chars(write_value)
if not write_char:
ble_client_obj.disconnect()
return
# Disconnect device
ble_client_obj.disconnect()
# Check dut responses
dut.expect('GAP procedure initiated: advertise;', timeout=30)
# Read Services
services_ret = ble_client_obj.get_services(srv_uuid)
if services_ret:
Utility.console_log('\nServices\n')
Utility.console_log(str(services_ret))
else:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Read Services failed')
# Read Characteristics
chars_ret = {}
chars_ret = ble_client_obj.read_chars()
if chars_ret:
Utility.console_log('\nCharacteristics retrieved')
for path, props in chars_ret.items():
Utility.console_log('\n\tCharacteristic: ' + str(path))
Utility.console_log('\tCharacteristic UUID: ' + str(props[2]))
Utility.console_log('\tValue: ' + str(props[0]))
Utility.console_log('\tProperties: : ' + str(props[1]))
else:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Read Characteristics failed')
'''
Write Characteristics
- write 'A' to characteristic with write permission
'''
chars_ret_on_write = {}
chars_ret_on_write = ble_client_obj.write_chars(b'A')
if chars_ret_on_write:
Utility.console_log('\nCharacteristics after write operation')
for path, props in chars_ret_on_write.items():
Utility.console_log('\n\tCharacteristic:' + str(path))
Utility.console_log('\tCharacteristic UUID: ' + str(props[2]))
Utility.console_log('\tValue:' + str(props[0]))
Utility.console_log('\tProperties: : ' + str(props[1]))
else:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Write Characteristics failed')
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
dut.expect('connection established; status=0', timeout=30)
dut.expect('disconnect;', timeout=30)
class BlePrphThread(threading.Thread):
@ -115,8 +99,8 @@ class BlePrphThread(threading.Thread):
def run(self):
try:
bleprph_client_task(self, self.dut, self.dut_addr)
except Exception:
bleprph_client_task(self.dut, self.dut_addr)
except RuntimeError:
self.exceptions_queue.put(traceback.format_exc(), block=False)
@ -130,8 +114,9 @@ def test_example_app_ble_peripheral(env, extra_data):
4. Read Characteristics
5. Write Characteristics
"""
subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig','hci0','reset'])
# Remove cached bluetooth devices of any previous connections
subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig', 'hci0', 'reset'])
# Acquire DUT
dut = env.get_dut('bleprph', 'examples/bluetooth/nimble/bleprph', dut_class=ttfw_idf.ESP32DUT)
@ -149,8 +134,11 @@ def test_example_app_ble_peripheral(env, extra_data):
# Get device address from dut
dut_addr = dut.expect(re.compile(r'Device Address: ([a-fA-F0-9:]+)'), timeout=30)[0]
exceptions_queue = Queue.Queue()
# Check dut responses
dut.expect('GAP procedure initiated: advertise;', timeout=30)
# Starting a py-client in a separate thread
exceptions_queue = Queue.Queue()
bleprph_thread_obj = BlePrphThread(dut, dut_addr, exceptions_queue)
bleprph_thread_obj.start()
bleprph_thread_obj.join()
@ -165,11 +153,7 @@ def test_example_app_ble_peripheral(env, extra_data):
Utility.console_log('\n' + exception_msg)
if exception_msg:
raise Exception('Thread did not run successfully')
# Check dut responses
dut.expect('connection established; status=0', timeout=30)
dut.expect('disconnect;', timeout=30)
raise Exception('BlePrph thread did not run successfully')
if __name__ == '__main__':

View File

@ -20,8 +20,10 @@ import os
import re
import esp_prov
import tiny_test_fw
import ttfw_idf
import wifi_tools
from tiny_test_fw import Utility
# Have esp_prov throw exception
esp_prov.config_throw_except = True
@ -54,10 +56,21 @@ def test_examples_provisioning_softap(env, extra_data):
try:
ctrl = wifi_tools.wpa_cli(iface, reset_on_exit=True)
print('Connecting to DUT SoftAP...')
ip = ctrl.connect(ssid, password)
got_ip = dut1.expect(re.compile(r'DHCP server assigned IP to a station, IP is: (\d+.\d+.\d+.\d+)'), timeout=60)[0]
if ip != got_ip:
raise RuntimeError('SoftAP connected to another host! ' + ip + '!=' + got_ip)
try:
ip = ctrl.connect(ssid, password)
except RuntimeError as err:
Utility.console_log('error: {}'.format(err))
try:
got_ip = dut1.expect(re.compile(r'DHCP server assigned IP to a station, IP is: (\d+.\d+.\d+.\d+)'), timeout=60)
Utility.console_log('got_ip: {}'.format(got_ip))
got_ip = got_ip[0]
if ip != got_ip:
raise RuntimeError('SoftAP connected to another host! {} != {}'.format(ip, got_ip))
except tiny_test_fw.DUT.ExpectTimeout:
# print what is happening on dut side
Utility.console_log('in exception tiny_test_fw.DUT.ExpectTimeout')
Utility.console_log(dut1.read())
raise
print('Connected to DUT SoftAP')
print('Starting Provisioning')
@ -68,7 +81,7 @@ def test_examples_provisioning_softap(env, extra_data):
provmode = 'softap'
ap_ssid = 'myssid'
ap_password = 'mypassword'
softap_endpoint = ip.split('.')[0] + '.' + ip.split('.')[1] + '.' + ip.split('.')[2] + '.1:80'
softap_endpoint = '{}.{}.{}.1:80'.format(ip.split('.')[0], ip.split('.')[1], ip.split('.')[2])
print('Getting security')
security = esp_prov.get_security(secver, pop, verbose)

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,6 @@ except ImportError as e:
print('Run `pip install -r $IDF_PATH/tools/ble/requirements.txt` for resolving the issue')
raise
ADV_OBJ = False
DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties'
LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1'
@ -75,10 +74,8 @@ class Advertisement(dbus.service.Object):
in_signature='s',
out_signature='a{sv}')
def GetAll(self, interface):
global ADV_OBJ
if interface != LE_ADVERTISEMENT_IFACE:
raise InvalidArgsException()
ADV_OBJ = True
return self.get_properties()[LE_ADVERTISEMENT_IFACE]
@dbus.service.method(LE_ADVERTISEMENT_IFACE,

View File

@ -32,13 +32,6 @@ except ImportError as e:
print('Run `pip install -r $IDF_PATH/tools/ble/requirements.txt` for resolving the issue')
raise
alert_status_char_obj = None
GATT_APP_OBJ = False
CHAR_READ = False
CHAR_WRITE = False
CHAR_SUBSCRIBE = False
DBUS_OM_IFACE = 'org.freedesktop.DBus.ObjectManager'
DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties'
GATT_MANAGER_IFACE = 'org.bluez.GattManager1'
@ -47,6 +40,21 @@ GATT_CHRC_IFACE = 'org.bluez.GattCharacteristic1'
GATT_DESC_IFACE = 'org.bluez.GattDescriptor1'
SERVICE_UUIDS = {
'ALERT_NOTIF_SVC_UUID': '00001811-0000-1000-8000-00805f9b34fb'
}
CHAR_UUIDS = {
'SUPPORT_NEW_ALERT_UUID': '00002A47-0000-1000-8000-00805f9b34fb',
'ALERT_NOTIF_UUID': '00002A44-0000-1000-8000-00805f9b34fb',
'UNREAD_ALERT_STATUS_UUID': '00002A45-0000-1000-8000-00805f9b34fb'
}
DESCR_UUIDS = {
'CCCD_UUID': '00002902-0000-1000-8000-00805f9b34fb'
}
class InvalidArgsException(dbus.exceptions.DBusException):
_dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs'
@ -63,22 +71,16 @@ class Application(dbus.service.Object):
def __init__(self, bus, path):
self.path = path
self.services = []
srv_obj = AlertNotificationService(bus, '0001')
self.add_service(srv_obj)
dbus.service.Object.__init__(self, bus, self.path)
def __del__(self):
pass
def get_path(self):
return dbus.ObjectPath(self.path)
def add_service(self, service):
self.services.append(service)
@dbus.service.method(DBUS_OM_IFACE, out_signature='a{oa{sa{sv}}}')
def GetManagedObjects(self):
global GATT_APP_OBJ
response = {}
for service in self.services:
@ -90,13 +92,25 @@ class Application(dbus.service.Object):
for desc in descs:
response[desc.get_path()] = desc.get_properties()
GATT_APP_OBJ = True
return response
def get_path(self):
return dbus.ObjectPath(self.path)
def Release(self):
pass
class AlertNotificationApp(Application):
'''
Alert Notification Application
'''
def __init__(self, bus, path):
Application.__init__(self, bus, path)
self.service = AlertNotificationService(bus, '0001')
self.add_service(self.service)
class Service(dbus.service.Object):
"""
org.bluez.GattService1 interface implementation
@ -191,7 +205,6 @@ class Characteristic(dbus.service.Object):
def GetAll(self, interface):
if interface != GATT_CHRC_IFACE:
raise InvalidArgsException()
return self.get_properties()[GATT_CHRC_IFACE]
@dbus.service.method(GATT_CHRC_IFACE, in_signature='a{sv}', out_signature='ay')
@ -217,7 +230,8 @@ class Characteristic(dbus.service.Object):
@dbus.service.signal(DBUS_PROP_IFACE,
signature='sa{sv}as')
def PropertiesChanged(self, interface, changed, invalidated):
print('\nProperties Changed')
pass
# print('\nProperties Changed')
class Descriptor(dbus.service.Object):
@ -250,7 +264,6 @@ class Descriptor(dbus.service.Object):
def GetAll(self, interface):
if interface != GATT_DESC_IFACE:
raise InvalidArgsException()
return self.get_properties()[GATT_DESC_IFACE]
@dbus.service.method(GATT_DESC_IFACE, in_signature='a{sv}', out_signature='ay')
@ -263,53 +276,57 @@ class Descriptor(dbus.service.Object):
print('Default WriteValue called, returning error')
raise NotSupportedException()
@dbus.service.signal(DBUS_PROP_IFACE,
signature='sa{sv}as')
def PropertiesChanged(self, interface, changed, invalidated):
pass
# print('\nProperties Changed')
class AlertNotificationService(Service):
TEST_SVC_UUID = '00001811-0000-1000-8000-00805f9b34fb'
def __init__(self, bus, index):
global alert_status_char_obj
Service.__init__(self, bus, index, self.TEST_SVC_UUID, primary=True)
Service.__init__(self, bus, index, SERVICE_UUIDS['ALERT_NOTIF_SVC_UUID'], primary=True)
self.add_characteristic(SupportedNewAlertCategoryCharacteristic(bus, '0001', self))
self.add_characteristic(AlertNotificationControlPointCharacteristic(bus, '0002', self))
alert_status_char_obj = UnreadAlertStatusCharacteristic(bus, '0003', self)
self.add_characteristic(alert_status_char_obj)
self.add_characteristic(UnreadAlertStatusCharacteristic(bus, '0003', self))
def get_char_status(self, uuid, status):
for char in self.characteristics:
if char.uuid == uuid:
if status in char.status:
return True
return False
class SupportedNewAlertCategoryCharacteristic(Characteristic):
SUPPORT_NEW_ALERT_UUID = '00002A47-0000-1000-8000-00805f9b34fb'
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index,
self.SUPPORT_NEW_ALERT_UUID,
CHAR_UUIDS['SUPPORT_NEW_ALERT_UUID'],
['read'],
service)
self.value = [dbus.Byte(2)]
self.status = []
def ReadValue(self, options):
global CHAR_READ
CHAR_READ = True
val_list = []
for val in self.value:
val_list.append(dbus.Byte(val))
print('Read Request received\n', '\tSupportedNewAlertCategoryCharacteristic')
print('\tValue:', '\t', val_list)
self.status.append('read')
return val_list
class AlertNotificationControlPointCharacteristic(Characteristic):
ALERT_NOTIF_UUID = '00002A44-0000-1000-8000-00805f9b34fb'
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index,
self.ALERT_NOTIF_UUID,
['read','write'],
CHAR_UUIDS['ALERT_NOTIF_UUID'],
['read', 'write'],
service)
self.value = [dbus.Byte(0)]
self.status = []
def ReadValue(self, options):
val_list = []
@ -317,48 +334,50 @@ class AlertNotificationControlPointCharacteristic(Characteristic):
val_list.append(dbus.Byte(val))
print('Read Request received\n', '\tAlertNotificationControlPointCharacteristic')
print('\tValue:', '\t', val_list)
self.status.append('read')
return val_list
def WriteValue(self, value, options):
global CHAR_WRITE
CHAR_WRITE = True
print('Write Request received\n', '\tAlertNotificationControlPointCharacteristic')
print('\tCurrent value:', '\t', self.value)
val_list = []
for val in value:
val_list.append(val)
self.value = val_list
self.PropertiesChanged(GATT_CHRC_IFACE, {'Value': self.value}, [])
# Check if new value is written
print('\tNew value:', '\t', self.value)
if not self.value == value:
print('Failed: Write Request\n\tNew value not written\tCurrent value:', self.value)
self.status.append('write')
class UnreadAlertStatusCharacteristic(Characteristic):
UNREAD_ALERT_STATUS_UUID = '00002A45-0000-1000-8000-00805f9b34fb'
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index,
self.UNREAD_ALERT_STATUS_UUID,
CHAR_UUIDS['UNREAD_ALERT_STATUS_UUID'],
['read', 'write', 'notify'],
service)
self.value = [dbus.Byte(0)]
self.cccd_obj = ClientCharacteristicConfigurationDescriptor(bus, '0001', self)
self.add_descriptor(self.cccd_obj)
self.notifying = False
self.status = []
def StartNotify(self):
global CHAR_SUBSCRIBE
CHAR_SUBSCRIBE = True
try:
if self.notifying:
print('\nAlready notifying, nothing to do')
return
print('Notify Started')
self.notifying = True
self.ReadValue()
self.WriteValue([dbus.Byte(1), dbus.Byte(0)])
self.status.append('notify')
if self.notifying:
print('\nAlready notifying, nothing to do')
return
self.notifying = True
print('\nNotify Started')
self.cccd_obj.WriteValue([dbus.Byte(1), dbus.Byte(0)])
self.cccd_obj.ReadValue()
except Exception as e:
print(e)
def StopNotify(self):
if not self.notifying:
@ -367,47 +386,45 @@ class UnreadAlertStatusCharacteristic(Characteristic):
self.notifying = False
print('\nNotify Stopped')
def ReadValue(self, options):
print('Read Request received\n', '\tUnreadAlertStatusCharacteristic')
def ReadValue(self, options=None):
val_list = []
for val in self.value:
val_list.append(dbus.Byte(val))
self.status.append('read')
print('\tValue:', '\t', val_list)
return val_list
def WriteValue(self, value, options):
print('Write Request received\n', '\tUnreadAlertStatusCharacteristic')
def WriteValue(self, value, options=None):
val_list = []
for val in value:
val_list.append(val)
self.value = val_list
self.PropertiesChanged(GATT_CHRC_IFACE, {'Value': self.value}, [])
# Check if new value is written
print('\tNew value:', '\t', self.value)
if not self.value == value:
print('Failed: Write Request\n\tNew value not written\tCurrent value:', self.value)
print('New value:', '\t', self.value)
self.status.append('write')
class ClientCharacteristicConfigurationDescriptor(Descriptor):
CCCD_UUID = '00002902-0000-1000-8000-00805f9b34fb'
def __init__(self, bus, index, characteristic):
self.value = [dbus.Byte(0)]
self.value = [dbus.Byte(1)]
Descriptor.__init__(
self, bus, index,
self.CCCD_UUID,
DESCR_UUIDS['CCCD_UUID'],
['read', 'write'],
characteristic)
def ReadValue(self):
print('\tValue on read:', '\t', self.value)
def ReadValue(self, options=None):
return self.value
def WriteValue(self, value):
def WriteValue(self, value, options=None):
val_list = []
for val in value:
val_list.append(val)
self.value = val_list
self.PropertiesChanged(GATT_DESC_IFACE, {'Value': self.value}, [])
# Check if new value is written
print('New value on write:', '\t', self.value)
if not self.value == value:
print('Failed: Write Request\n\tNew value not written\tCurrent value:', self.value)

View File

@ -44,25 +44,30 @@ class wpa_cli:
self.new_network = None
self.connected = False
self.reset_on_exit = reset_on_exit
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
service = dbus.Interface(bus.get_object('fi.w1.wpa_supplicant1', '/fi/w1/wpa_supplicant1'),
'fi.w1.wpa_supplicant1')
iface_path = service.GetInterface(self.iface_name)
self.iface_obj = bus.get_object('fi.w1.wpa_supplicant1', iface_path)
self.iface_ifc = dbus.Interface(self.iface_obj, 'fi.w1.wpa_supplicant1.Interface')
self.iface_props = dbus.Interface(self.iface_obj, 'org.freedesktop.DBus.Properties')
if self.iface_ifc is None:
raise RuntimeError('supplicant : Failed to fetch interface')
try:
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
self.old_network = self._get_iface_property('CurrentNetwork')
print('Old network is %s' % self.old_network)
service = dbus.Interface(bus.get_object('fi.w1.wpa_supplicant1', '/fi/w1/wpa_supplicant1'),
'fi.w1.wpa_supplicant1')
iface_path = service.GetInterface(self.iface_name)
self.iface_obj = bus.get_object('fi.w1.wpa_supplicant1', iface_path)
self.iface_ifc = dbus.Interface(self.iface_obj, 'fi.w1.wpa_supplicant1.Interface')
self.iface_props = dbus.Interface(self.iface_obj, 'org.freedesktop.DBus.Properties')
if self.iface_ifc is None:
raise RuntimeError('supplicant : Failed to fetch interface')
if self.old_network == '/':
self.old_network = None
else:
self.connected = True
self.old_network = self._get_iface_property('CurrentNetwork')
print('Old network is %s' % self.old_network)
if self.old_network == '/':
self.old_network = None
else:
self.connected = True
except Exception as err:
raise Exception('Failure in wpa_cli init: {}'.format(err))
def _get_iface_property(self, name):
""" Read the property with 'name' from the wi-fi interface object
@ -73,37 +78,41 @@ class wpa_cli:
return self.iface_props.Get('fi.w1.wpa_supplicant1.Interface', name)
def connect(self, ssid, password):
if self.connected is True:
self.iface_ifc.Disconnect()
self.connected = False
try:
if self.connected is True:
self.iface_ifc.Disconnect()
self.connected = False
if self.new_network is not None:
self.iface_ifc.RemoveNetwork(self.new_network)
if self.new_network is not None:
self.iface_ifc.RemoveNetwork(self.new_network)
print('Pre-connect state is %s, IP is %s' % (self._get_iface_property('State'), get_wiface_IPv4(self.iface_name)))
print('Pre-connect state is %s, IP is %s' % (self._get_iface_property('State'), get_wiface_IPv4(self.iface_name)))
self.new_network = self.iface_ifc.AddNetwork({'ssid': ssid, 'psk': password})
self.iface_ifc.SelectNetwork(self.new_network)
time.sleep(10)
self.new_network = self.iface_ifc.AddNetwork({'ssid': ssid, 'psk': password})
self.iface_ifc.SelectNetwork(self.new_network)
time.sleep(10)
ip = None
retry = 10
while retry > 0:
time.sleep(5)
state = str(self._get_iface_property('State'))
print('wpa iface state %s (scanning %s)' % (state, bool(self._get_iface_property('Scanning'))))
if state in ['disconnected', 'inactive']:
self.iface_ifc.Reconnect()
ip = get_wiface_IPv4(self.iface_name)
print('wpa iface %s IP %s' % (self.iface_name, ip))
if ip is not None:
self.connected = True
return ip
retry -= 1
time.sleep(3)
ip = None
retry = 10
while retry > 0:
time.sleep(5)
state = str(self._get_iface_property('State'))
print('wpa iface state %s (scanning %s)' % (state, bool(self._get_iface_property('Scanning'))))
if state in ['disconnected', 'inactive']:
self.iface_ifc.Reconnect()
ip = get_wiface_IPv4(self.iface_name)
print('wpa iface %s IP %s' % (self.iface_name, ip))
if ip is not None:
self.connected = True
return ip
retry -= 1
time.sleep(3)
self.reset()
raise RuntimeError('wpa_cli : Connection failed')
self.reset()
print('wpa_cli : Connection failed')
except Exception as err:
raise Exception('Failure in wpa_cli init: {}'.format(err))
def reset(self):
if self.iface_ifc is not None:

View File

@ -42,13 +42,15 @@ if platform.system() == 'Linux':
# BLE client (Linux Only) using Bluez and DBus
class BLE_Bluez_Client:
def __init__(self):
self.adapter_props = None
def connect(self, devname, iface, chrc_names, fallback_srv_uuid):
self.devname = devname
self.srv_uuid_fallback = fallback_srv_uuid
self.chrc_names = [name.lower() for name in chrc_names]
self.device = None
self.adapter = None
self.adapter_props = None
self.services = None
self.nu_lookup = None
self.characteristics = dict()
@ -58,20 +60,53 @@ class BLE_Bluez_Client:
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object('org.bluez', '/'), 'org.freedesktop.DBus.ObjectManager')
objects = manager.GetManagedObjects()
adapter_path = None
for path, interfaces in iteritems(objects):
adapter = interfaces.get('org.bluez.Adapter1')
if adapter is not None:
if path.endswith(iface):
self.adapter = dbus.Interface(bus.get_object('org.bluez', path), 'org.bluez.Adapter1')
self.adapter_props = dbus.Interface(bus.get_object('org.bluez', path), 'org.freedesktop.DBus.Properties')
adapter_path = path
break
if self.adapter is None:
raise RuntimeError('Bluetooth adapter not found')
# Power on bluetooth adapter
self.adapter_props.Set('org.bluez.Adapter1', 'Powered', dbus.Boolean(1))
self.adapter.StartDiscovery()
print('checking if adapter is powered on')
for cnt in range(10, 0, -1):
time.sleep(5)
powered_on = self.adapter_props.Get('org.bluez.Adapter1', 'Powered')
if powered_on == 1:
# Set adapter props again with powered on value
self.adapter_props = dbus.Interface(bus.get_object('org.bluez', adapter_path), 'org.freedesktop.DBus.Properties')
print('bluetooth adapter powered on')
break
print('number of retries left({})'.format(cnt - 1))
if powered_on == 0:
raise RuntimeError('Failed to starte bluetooth adapter')
# Start discovery if not already discovering
started_discovery = 0
discovery_val = self.adapter_props.Get('org.bluez.Adapter1', 'Discovering')
if discovery_val == 0:
print('starting discovery')
self.adapter.StartDiscovery()
# Set as start discovery is called
started_discovery = 1
for cnt in range(10, 0, -1):
time.sleep(5)
discovery_val = self.adapter_props.Get('org.bluez.Adapter1', 'Discovering')
if discovery_val == 1:
print('start discovery successful')
break
print('number of retries left ({})'.format(cnt - 1))
if discovery_val == 0:
print('start discovery failed')
raise RuntimeError('Failed to start discovery')
retry = 10
while (retry > 0):
@ -80,8 +115,11 @@ class BLE_Bluez_Client:
print('Connecting...')
# Wait for device to be discovered
time.sleep(5)
self._connect_()
print('Connected')
connected = self._connect_()
if connected:
print('Connected')
else:
return False
print('Getting Services...')
# Wait for services to be discovered
time.sleep(5)
@ -92,7 +130,21 @@ class BLE_Bluez_Client:
retry -= 1
print('Retries left', retry)
continue
self.adapter.StopDiscovery()
# Call StopDiscovery() for corresponding StartDiscovery() session
if started_discovery == 1:
print('stopping discovery')
self.adapter.StopDiscovery()
for cnt in range(10, 0, -1):
time.sleep(5)
discovery_val = self.adapter_props.Get('org.bluez.Adapter1', 'Discovering')
if discovery_val == 0:
print('stop discovery successful')
break
print('number of retries left ({})'.format(cnt - 1))
if discovery_val == 1:
print('stop discovery failed')
return False
def _connect_(self):
@ -123,9 +175,26 @@ class BLE_Bluez_Client:
if len(uuids) == 1:
self.srv_uuid_adv = uuids[0]
except dbus.exceptions.DBusException as e:
print(e)
raise RuntimeError(e)
self.device.Connect(dbus_interface='org.bluez.Device1')
# Check device is connected successfully
for cnt in range(10, 0, -1):
time.sleep(5)
device_conn = self.device.Get(
'org.bluez.Device1',
'Connected',
dbus_interface='org.freedesktop.DBus.Properties')
if device_conn == 1:
print('device is connected')
break
print('number of retries left ({})'.format(cnt - 1))
if device_conn == 0:
print('failed to connect device')
return False
return True
except Exception as e:
print(e)
self.device = None
@ -173,8 +242,8 @@ class BLE_Bluez_Client:
continue
try:
readval = desc.ReadValue({}, dbus_interface='org.bluez.GattDescriptor1')
except dbus.exceptions.DBusException:
break
except dbus.exceptions.DBusException as err:
raise RuntimeError('Failed to read value for descriptor while getting services - {}'.format(err))
found_name = ''.join(chr(b) for b in readval).lower()
nu_lookup[found_name] = uuid
break
@ -201,6 +270,8 @@ class BLE_Bluez_Client:
if not service_found:
self.device.Disconnect(dbus_interface='org.bluez.Device1')
# Check if device is disconnected successfully
self._check_device_disconnected()
if self.adapter:
self.adapter.RemoveDevice(self.device)
self.device = None
@ -219,6 +290,8 @@ class BLE_Bluez_Client:
def disconnect(self):
if self.device:
self.device.Disconnect(dbus_interface='org.bluez.Device1')
# Check if device is disconnected successfully
self._check_device_disconnected()
if self.adapter:
self.adapter.RemoveDevice(self.device)
self.device = None
@ -227,6 +300,20 @@ class BLE_Bluez_Client:
if self.adapter_props:
self.adapter_props.Set('org.bluez.Adapter1', 'Powered', dbus.Boolean(0))
def _check_device_disconnected(self):
for cnt in range(10, 0, -1):
time.sleep(5)
device_conn = self.device.Get(
'org.bluez.Device1',
'Connected',
dbus_interface='org.freedesktop.DBus.Properties')
if device_conn == 0:
print('device disconnected')
break
print('number of retries left ({})'.format(cnt - 1))
if device_conn == 1:
print('failed to disconnect device')
def send_data(self, characteristic_uuid, data):
try:
path = self.characteristics[characteristic_uuid]

View File

@ -36,9 +36,9 @@ class Transport_HTTP(Transport):
raise RuntimeError('Unable to resolve hostname :' + hostname)
if ssl_context is None:
self.conn = HTTPConnection(hostname, timeout=45)
self.conn = HTTPConnection(hostname, timeout=60)
else:
self.conn = HTTPSConnection(hostname, context=ssl_context, timeout=45)
self.conn = HTTPSConnection(hostname, context=ssl_context, timeout=60)
try:
print('Connecting to ' + hostname)
self.conn.connect()