def add_service(self, uuid, start, end): if not self.find_service(uuid): logger.debug('Adding service UUID: %s (%d:%d)', uuid2str(uuid), start, end) # Check if the service has a specific class implemented if len(uuid) == 2: uuid16 = (uuid[1] << 8) | uuid[0] if uuid16 == Device.BATTERY_SERVICE_UUID: s = BatteryService(self.bglib, self.connection_handle, self.cmd_q, uuid, start, end) elif uuid16 == Device.DEVICE_INFORMATION_UUID: s = DeviceInformationService(self.bglib, self.connection_handle, self.cmd_q, uuid, start, end) elif uuid16 == Device.GENERIC_ACCESS_SERVICE_UUID: s = GenericAccessService(self.bglib, self.connection_handle, self.cmd_q, uuid, start, end) elif uuid16 == Device.GENERIC_ATTRIBUTE_SERVICE_UUID: s = GenericAttributeService(self.bglib, self.connection_handle, self.cmd_q, uuid, start, end) else: s = Service(self.bglib, self.connection_handle, self.cmd_q, uuid, start, end) else: # Custom services can be registered prior to discovery so that we instantiate them correctly uuid16 = uuid[12:14] s = None for custom_service in self.custom_services: if uuid16 == custom_service[0]: s = custom_service[1](self.bglib, self.connection_handle, self.cmd_q, uuid, start, end) break # If all else fails, use the default service class if not s: s = Service(self.bglib, self.connection_handle, self.cmd_q, uuid, start, end) self.services.append(s)
def read(self): logger.debug('Reading handle %d (%s)', self.handle, uuid2str(self.short_uuid)) self.cmd_q.put(self.bglib.ble_cmd_attclient_read_by_handle(self.connection_handle, self.handle)) try: data = self.rx_q.get(True, 3) except Empty as e: raise e return data
def add_characteristic(self, uuid, handle): logger.debug('Adding Characteristic UUID: %s Handle: %d', uuid2str(uuid), handle) self.characteristics.append(Characteristic(self.bglib, self.connection_handle, self.cmd_q, uuid, handle))
def __str__(self): return '%s -- %s' % (self.name, uuid2str(self.short_uuid))
def disconnect_handler(self): for c in self.characteristics[:]: logger.debug('Removing Characteristic UUID: %s Handle: %d', uuid2str(c.uuid), c.handle) self.characteristics.remove(c)
def attclient_attribute_value_handler(self, args): if args['type'] == 0x01: if self.notification_callback: # This is a notification event logger.debug('Calling notification callback for handle %d (%s)', self.handle, uuid2str(self.short_uuid)) self.notification_callback(self.short_uuid, args['value']) else: logger.warn('No notification callback for handle %d (%s)', self.handle, uuid2str(self.short_uuid)) elif args['type'] == 0x00: # This is read data logger.debug('Placing data onto RX Queue for handle %d (%s)', self.handle, uuid2str(self.short_uuid)) self.rx_q.put(args['value'])
def write(self, data): logger.debug('Writing handle %d (%s)', self.handle, uuid2str(self.short_uuid)) self.cmd_q.put(self.bglib.ble_cmd_attclient_attribute_write(self.connection_handle, self.handle, data))
def remove_service(self, uuid): logger.debug('Removing service %s', uuid2str(uuid)) s = self.find_service(uuid) if s: s.disconnect_handler() self.services.remove(s)