def __init__(self, porta=None): self.sincro = { # list of devices 'scan': queue.Queue(), # bootloader response 'blr': queue.Queue() } self.mac = None self.blc = None CY567x.CY567x.__init__(self, porta=porta) try: if not self.is_ok(): raise utili.Problema('not OK') if not self.init_ble_stack(): raise utili.Problema('err init') if not self.set_device_io_capabilities('KEYBOARD DISPLAY'): raise utili.Problema('err capa') if not self.set_local_device_security('2'): raise utili.Problema('err security') mio = self.my_address() if mio is None: raise utili.Problema('err bdaddr') self.mio = mio print('io sono ' + utili.str_da_mac(mio)) except utili.Problema as err: print(err)
def __init__(self, porta=None, logga=False): # prima di sincro! bl.CY_BL_SERVICE.__init__(self) self.sincro = { # cypress bootloader service 'blr': queue.Queue(), # list of devices 'scan': queue.Queue(), # user list of devices 'user': None, # signaled by gap_auth_req_cb 'authReq': threading.Event(), # signaled by gap_passkey_entry_request_cb 'pairReq': threading.Event(), # command response 'rsp': queue.Queue() } self.priv = None self.crc = crcmod.Crc(0x11021, 0xC681, False, 0x0000) self.srvdata = None CY567x.CY567x.__init__(self, porta=porta) self.mio = None self.disc = None if logga: self.logger = utili.LOGGA('ghost') else: self.logger = utili.LOGGA() try: if not self.is_ok(): raise utili.Problema('not OK') if not self.init_ble_stack(): raise utili.Problema('err init') if not self.set_device_io_capabilities('KEYBOARD DISPLAY'): raise utili.Problema('err capa') if not self.set_local_device_security('3'): raise utili.Problema('err security') mio = self.my_address() if mio is None: raise utili.Problema('err bdaddr') self.mio = mio self.logger.debug('io sono ' + utili.str_da_mac(mio)) except utili.Problema as err: self.logger.critical(str(err))
def _exec_command(self): try: cmd = self.command['todo'].get(True, self.command['poll']) if cmd.are_you(self.QUIT): return False if cmd.are_you(self.ABORT_CURRENT_COMMAND): self.command['curr'] = None raise utili.Problema('abort') if self.command['curr'] is None: self.command['curr'] = cmd msg = self.proto['tx'].compose(cmd.get()) self._print('IRP_MJ_WRITE Data: ' + utili.esa_da_ba(msg, ' ')) self.uart.write(msg) else: # busy self.command['todo'].put_nowait(cmd) except (utili.Problema, queue.Empty) as err: if isinstance(err, utili.Problema): self._print(str(err)) return True
def cmd_void_rsp(self, char, cmd, dim=None, to=3): msg = self._create_command(cmd) try: rsp = self._send_cmd_and_rx_rsp(char, msg, to, cmd) if rsp is None: raise utili.Problema('? error ?') if dim is not None: if dim != len(rsp['prm']): raise utili.Problema('? prm ?') return rsp['prm'] except (queue.Empty, utili.Problema) as err: if isinstance(err, utili.Problema): print(err) return None
def reiniz(self): """ reinit cy5677 dongle :return: bool """ try: self.disconnect() if not self.init_ble_stack(): raise utili.Problema('err init') if not self.set_device_io_capabilities('KEYBOARD DISPLAY'): raise utili.Problema('err capa') if not self.set_local_device_security('3'): raise utili.Problema('err security') return True except utili.Problema as err: print(err) return False
def connect_to(self, bda, mode, secret, to=20): """ execute connection with authentication and authorization :param bda: mac address (bytearray) :param mode: 'CONF' o 'NORM' :param secret: bytearray :param to: timeout :return: bool """ self.logger.info('connect_to') pk = self._compute_passkey(bda, secret) self.logger.debug('passkey={:06d}'.format(pk)) self.sincro['authReq'].clear() self.sincro['pairReq'].clear() try: # connection if not self.connect(bda, public=False): raise utili.Problema("err connect") # authentication if not self.sincro['authReq'].wait(to): raise utili.Problema("err autReq") mtu = self.exchange_gatt_mtu_size() if mtu == 0: raise utili.Problema('err mtu') print('mtu {}'.format(mtu)) if not self.initiate_pairing_request(): raise utili.Problema('err pair req') if not self.sincro['pairReq'].wait(to): raise utili.Problema("err pairReq") if not self.pairing_passkey(pk): raise utili.Problema('err passkey') # la cy5677 non funziona benissimo time.sleep(2) # authorization crt_ = CYBLE_SERVICE_AUTHOR_CHAR_HANDLE if mode == 'CONF': crt_ = CYBLE_CONFIG_AUTHOR_CHAR_HANDLE if not self._authorize(crt_): raise utili.Problema('err autor') return True except utili.Problema as err: self.logger.error(str(err)) return False
def _send_cmd_and_rx_rsp(self, char, msg, to, cmd): try: if not self.write_characteristic_value(char, msg): raise utili.Problema('? write_characteristic_value ?') _crt, ntf = self.sincro['rsp'].get(True, to) if _crt != char: raise utili.Problema('? crt ?') rsp = self._extract_response(ntf) if rsp is None: raise utili.Problema('? rsp ?') if rsp['cmd'] != cmd: raise utili.Problema('? cmd ?') return rsp except (queue.Empty, utili.Problema) as err: if isinstance(err, utili.Problema): print(err) return None
def _esci(self, _): raise utili.Problema('fine')
return self.write_characteristic_value(self.blc, msg, to=to) if __name__ == '__main__': #mac = "00:A0:50:C4:A4:2D" mac = "00:A0:50:D4:19:AB" nomef = 'BLE_External_Memory_Bootloadable01.cyacd' # Cypress Bootloader Service: 2.1 Bootloader Service Definition bl_service = '00060000-F8CE-11E4-ABF4-0002A5D5C51B' dispo = EXAMPLE(porta='com22') try: if not dispo.is_ok(): raise utili.Problema('no dongle') cyacd = cyacd.CYACD() cyacd.load(nomef) if not dispo.find(mac): raise utili.Problema('not found') if not dispo.connect(mac): raise utili.Problema('not connected') # alcune caratteristiche sono cifrate if not dispo.initiate_pairing_request(): raise utili.Problema('no pairing') # Log di CySmart
# MAC = arghi.mac MTU = arghi.mtu # DIM = arghi.dim # NUM = arghi.num # criterio = lambda cnt, lim: True # if NUM: # criterio = lambda cnt, lim: cnt < lim # test ghost = GHOST() if ghost.is_ok(): try: elem = ghost.find(FAKE_PRD) if elem is None: raise utili.Problema('no disp') MAC = elem['bda'] MODE = elem['fase'] if not ghost.connect_to(MAC, MODE, FAKE_SECRET): raise utili.Problema('err connect') print('connesso') if MODE == 'CONF': tempi = ghost.read_times() if tempi is None: raise utili.Problema('err read_times') print('Tnp={} Tp={} Tall={}'.format(tempi[0], tempi[1], tempi[2]))