Ejemplo n.º 1
0
    def decompose(self, cosa):
        """
        extract event and parameters from a message
        :param cosa: bytearray (message)
        :return: dictionary with 'evn', 'prm' (or empty)
        """
        msg = {}
        if len(cosa) >= 4:
            tot, evn = struct.unpack('<2H', cosa[:4])
            prm = cosa[4:]
            tot -= 2

            if tot != len(prm):
                self._print(
                    self.name +
                    ' ERR DIM {:04X}[{} != {}]: '.format(evn, tot, len(prm)) +
                    utili.esa_da_ba(prm, ' '))
            else:
                msg['evn'] = evn
                msg['prm'] = prm

                self._print(self.name + ' {:04X}[{}]: '.format(evn, tot) +
                            utili.esa_da_ba(prm, ' '))
        else:
            self._print(self.name + ' ????: ' + utili.esa_da_ba(cosa, ' '))
        return msg
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def find(self, cp, to=10):
        """
        find the ghost with a specific serial number
        :param cp: serial number (i.e. 'XXXAT000000')
        :param to: timeout
        :return: dict (cfr scan_report) or None
        """

        sdata = service_data_from(cp)
        self.logger.info('find <' + cp + '> => ' + utili.esa_da_ba(sdata, ' '))

        self.srvdata = sdata

        # empty scan queue
        self._reset('SCAN')

        # find it
        if self.scan_start():
            try:
                ud = self.sincro['scan'].get(True, to)
                self.scan_stop()
                return ud
            except queue.Empty:
                self.scan_stop()

        return None
Ejemplo n.º 4
0
 def _stato_1(self, rx):
     if rx == self.second:
         self.stato = 2
     else:
         if len(self.partial):
             self._print('_stato_1 elimino {}'.format(
                 utili.esa_da_ba(self.partial, '-')))
         self.reinit()
     return False
Ejemplo n.º 5
0
    def msg_to_string(self, cosa):
        risul = self.name + ' '
        if len(cosa) >= 4:
            msg, tot = struct.unpack('<2H', cosa[:4])
            prm = cosa[4:]

            risul += quale_comando(msg) + ' '

            if tot != len(prm):
                risul += 'ERR DIM [{} != {}]: '.format(
                    tot, len(prm)) + '\n\t' + utili.esa_da_ba(prm, ' ')
            else:
                risul += '[{}]: '.format(tot) + '\n\t' + \
                         utili.esa_da_ba(prm, ' ')
        else:
            risul += '????: ' + '\n\t' + utili.esa_da_ba(cosa, ' ')

        return risul
Ejemplo n.º 6
0
 def gattc_handle_value_ntf_cb(self, crt, ntf):
     """
     callback invoked when the peripheral sends a notification
     :param crt: characteristic's handle
     :param ntf: notification (bytearray)
     :return: n.a.
     """
     self._print('gattc_handle_value_ntf_cb: handle:{:04X} '.format(crt) +
                 utili.esa_da_ba(ntf, ' '))
Ejemplo n.º 7
0
 def gattc_handle_value_ind_cb(self, crt, result, ind):
     """
     callback invoked when the peripheral sends an indication
     :param crt: characteristic's handle
     :param result: of CyBle_GattcConfirmation
     :param ind: bytearray
     :return: n.a.
     """
     self._print('gattc_handle_value_ind_cb: handle:{:04X} confirm={}'.
                 format(crt, result) + utili.esa_da_ba(ind, ' '))
Ejemplo n.º 8
0
 def _evt_report_stack_misc_status(self, prm):
     """
     EVT_REPORT_STACK_MISC_STATUS [5]:
         29 00 event
         01 00 prm size
         01    prm
     """
     event, dim = struct.unpack('<2H', prm[:4])
     prm = prm[4:]
     self._print('EVT_REPORT_STACK_MISC_STATUS: CYBLE_EVT_={:04X}[{}] '.
                 format(event, dim) + utili.esa_da_ba(prm, ' '))
Ejemplo n.º 9
0
    def _create_command(self, cmd, prm=None):
        fcrc = self.crc.new()

        xxx = bytearray([cmd])
        if prm is not None:
            xxx += prm

        fcrc.update(xxx)
        xxx += fcrc.digest()

        print('comando: ' + utili.esa_da_ba(xxx, ' '))

        return xxx
Ejemplo n.º 10
0
    def run(self):
        self._print('nasco')
        while True:
            # any command?
            if not self._exec_command():
                # quit
                break

            # any data?
            while self.uart.in_waiting:
                tmp = self.uart.read(self.uart.in_waiting)
                if len(tmp) == 0:
                    break

                self._print('IRP_MJ_READ Data: ' + utili.esa_da_ba(tmp, ' '))
                self.proto['rx'].examine(tmp)

            # any message?
            while True:
                msg = self.proto['rx'].get_msg()
                if msg is None:
                    break

                dec = self.proto['rx'].decompose(msg)
                if any(dec):
                    try:
                        self.events[dec['evn']](dec['prm'])
                    except KeyError:
                        self._print('PLEASE MANAGE ' +
                                    self.proto['rx'].msg_to_string(msg))
        self._print('muoio')

        # switch dongle to initial configuration
        cmd = _COMMAND(self.Cmd_Tool_Disconnected_Api)
        msg = self.proto['tx'].compose(cmd.get())

        self._print('IRP_MJ_WRITE Data: ' + utili.esa_da_ba(msg, ' '))
        self.uart.write(msg)
Ejemplo n.º 11
0
 def empty_partial():
     # a new packet starts
     if len(self.partial):
         self._print('scarto ' + utili.esa_da_ba(self.partial, '-'))
         self.reinit(True)
Ejemplo n.º 12
0
            self.disc(reason)


DESCRIZIONE = \
    '''
    prova della demo
    '''

if __name__ == '__main__':

    import argparse

    FAKE_PRD = 'TD3py239168'

    sd = service_data_from(FAKE_PRD)
    print(utili.esa_da_ba(sd, ' '))
    nsp = nsp_from(sd)
    print(nsp)
    print(FAKE_PRD == nsp)

    FAKE_SECRET = bytes([
        0x1D, 0x2B, 0xE0, 0x8B, 0xF0, 0x37, 0x1C, 0x60, 0x8B, 0xC3, 0xC7, 0x5A,
        0x66, 0x6D, 0x89, 0x66, 0xA2, 0x43, 0x4D, 0x5F, 0x60, 0xA6, 0xCA, 0x91,
        0xDF, 0x3B, 0x10, 0x22, 0x84, 0xBD, 0x72, 0x1F, 0x06, 0xA2, 0x30, 0xD2,
        0xD4, 0x5C, 0xAB, 0x57, 0x98, 0x8A, 0x92, 0xC2, 0x02, 0x86, 0x13, 0xAB,
        0x23, 0xC7, 0x1A, 0x98, 0xBE, 0x0B, 0x8D, 0x25, 0x12, 0xB3, 0x59, 0xBF,
        0x95, 0xAF, 0x5D, 0xF5
    ])

    # argomenti
    argom = argparse.ArgumentParser(