def __init__(self, serial_port=None, snr=None, family='NRF51'): if serial_port is None and snr is None: raise NordicSemiException('Invalid Flasher initialization') nrfjprog = Flasher.which(Flasher.NRFJPROG) if nrfjprog == None: nrfjprog = Flasher.which("{}.exe".format(Flasher.NRFJPROG)) if nrfjprog == None: raise NordicSemiException('nrfjprog not installed') serial_ports = BLEDriver.enum_serial_ports() try: if serial_port is None: serial_port = [ d.port for d in serial_ports if d.serial_number == snr ][0] elif snr is None: snr = [ d.serial_number for d in serial_ports if d.port == serial_port ][0] except IndexError: raise NordicSemiException('board not found') self.serial_port = serial_port self.snr = snr.lstrip("0") self.family = family
def on_gattc_evt_hvx(self, ble_driver, conn_handle, status, error_handle, attr_handle, hvx_type, data): if status != BLEGattStatusCode.success: logger.error( "Error. Handle value notification failed. Status {}.".format( status)) return if hvx_type == BLEGattHVXType.notification: uuid = self.db_conns[conn_handle].get_char_uuid(attr_handle) if uuid == None: raise NordicSemiException('UUID not found') for obs in self.observers: obs.on_notification(ble_adapter=self, conn_handle=conn_handle, uuid=uuid, data=data) elif hvx_type == BLEGattHVXType.indication: uuid = self.db_conns[conn_handle].get_char_uuid(attr_handle) if uuid == None: raise NordicSemiException('UUID not found') for obs in self.observers: obs.on_indication(ble_adapter=self, conn_handle=conn_handle, uuid=uuid, data=data) self.driver.ble_gattc_hv_confirm(conn_handle, attr_handle)
def write_cmd(self, conn_handle, uuid, data): handle = self.db_conns[conn_handle].get_char_value_handle(uuid) if handle == None: raise NordicSemiException('Characteristic value handler not found') write_params = BLEGattcWriteParams(BLEGattWriteOperation.write_cmd, BLEGattExecWriteFlag.unused, handle, data, 0) self.driver.ble_gattc_write(conn_handle, write_params) self.evt_sync[conn_handle].wait(evt=BLEEvtID.evt_tx_complete)
def uuid128_to_c(self): if not isinstance(self.value, list): raise NordicSemiException('Not vendor specific UUID {}'.format( self.value)) lsb_list = self.value[::-1] self.__uuid128_array = util.list_to_uint8_array(lsb_list) uuid = driver.ble_uuid128_t() uuid.uuid128 = self.__uuid128_array.cast() return uuid
def write_prep(self, conn_handle, uuid, data, offset): handle = self.db_conns[conn_handle].get_char_value_handle(uuid) if handle == None: raise NordicSemiException('Characteristic value handler not found') write_params = BLEGattcWriteParams( BLEGattWriteOperation.prepare_write_req, BLEGattExecWriteFlag.prepared_write, handle, data, offset) self.driver.ble_gattc_write(conn_handle, write_params) result = self.evt_sync[conn_handle].wait( evt=BLEEvtID.gattc_evt_write_rsp) return result['status']
def read_req(self, conn_handle, uuid): handle = self.db_conns[conn_handle].get_char_value_handle(uuid) if handle == None: raise NordicSemiException('Characteristic value handler not found') self.driver.ble_gattc_read(conn_handle, handle, 0) result = self.evt_sync[conn_handle].wait(evt = BLEEvtID.gattc_evt_read_rsp) gatt_res = result['status'] if gatt_res == BLEGattStatusCode.success: return (gatt_res, result['data']) else: return (gatt_res, None)
def write_cmd(self, conn_handle, uuid, data): handle = self.db_conns[conn_handle].get_char_value_handle(uuid) if handle == None: raise NordicSemiException('Characteristic value handler not found') write_params = BLEGattcWriteParams(BLEGattWriteOperation.write_cmd, BLEGattExecWriteFlag.unused, handle, data, 0) # Send packet and skip waiting for TX-complete event. Try maximum 3 times. for _ in range(3): try: self.driver.ble_gattc_write(conn_handle, write_params) return except NordicSemiException as e: # Retry if BLE_ERROR_NO_TX_PACKETS error code. if "Error code: 12292" in e.message: self.evt_sync[conn_handle].wait( evt=BLEEvtID.evt_tx_complete, timeout=1) else: raise e raise NordicSemiException( 'Unable to successfully call ble_gattc_write')
def enable_notification(self, conn_handle, uuid): cccd_list = [1, 0] handle = self.db_conns[conn_handle].get_cccd_handle(uuid) if handle == None: raise NordicSemiException('CCCD not found') write_params = BLEGattcWriteParams(BLEGattWriteOperation.write_req, BLEGattExecWriteFlag.unused, handle, cccd_list, 0) self.driver.ble_gattc_write(conn_handle, write_params) result = self.evt_sync[conn_handle].wait( evt=BLEEvtID.gattc_evt_write_rsp) return result['status']
def enum_serial_ports(cls): MAX_SERIAL_PORTS = 64 c_descs = [ driver.sd_rpc_serial_port_desc_t() for i in range(MAX_SERIAL_PORTS) ] c_desc_arr = util.list_to_serial_port_desc_array(c_descs) arr_len = driver.new_uint32() driver.uint32_assign(arr_len, MAX_SERIAL_PORTS) err_code = driver.sd_rpc_serial_port_enum(c_desc_arr, arr_len) if err_code != driver.NRF_SUCCESS: raise NordicSemiException('Failed to {}. Error code: {}'.format( func.__name__, err_code)) dlen = driver.uint32_value(arr_len) descs = util.serial_port_desc_array_to_list(c_desc_arr, dlen) return map(SerialPortDescriptor.from_c, descs)
def to_c(self): data_list = list() for k in self.records: data_list.append(len(self.records[k]) + 1) # add type length data_list.append(k.value) if isinstance(self.records[k], str): data_list.extend([ord(c) for c in self.records[k]]) elif isinstance(self.records[k], list): data_list.extend(self.records[k]) else: raise NordicSemiException( 'Unsupported value type: 0x{:02X}'.format( type(self.records[k]))) data_len = len(data_list) if data_len == 0: return (data_len, None) else: self.__data_array = util.list_to_uint8_array(data_list) return (data_len, self.__data_array.cast())
def wrapper(wrapped, instance, args, kwargs): err_code = wrapped(*args, **kwargs) if err_code != expected: raise NordicSemiException('Failed to {}. Error code: {}'.format( wrapped.__name__, err_code))