示例#1
0
 def handle_event_response(self):
     """
     This reads the enciphered response from an accessory after registering for events.
     :return: the event data as string (not as json object)
     """
     try:
         return self._read_response(1)
     except OSError as e:
         raise exceptions.AccessoryDisconnectedError(str(e))
示例#2
0
 def _handle_request(self, data):
     with self.lock:
         data = data.replace("\n", "\r\n")
         assert len(data) < 1024
         len_bytes = len(data).to_bytes(2, byteorder='little')
         cnt_bytes = self.c2a_counter.to_bytes(8, byteorder='little')
         self.c2a_counter += 1
         ciper_and_mac = chacha20_aead_encrypt(len_bytes, self.c2a_key, cnt_bytes, bytes([0, 0, 0, 0]),
                                               data.encode())
         try:
             self.sock.send(len_bytes + ciper_and_mac[0] + ciper_and_mac[1])
             return self._read_response(self.timeout)
         except OSError as e:
             raise exceptions.AccessoryDisconnectedError(str(e))
示例#3
0
    def _handle_request(self, data):
        logging.debug('handle request: %s', data)
        with self.lock:
            while len(data) > 0:
                # split the data to max 1024 bytes (see page 71)
                len_data = min(len(data), 1024)
                tmp_data = data[:len_data]
                data = data[len_data:]
                len_bytes = len_data.to_bytes(2, byteorder='little')
                cnt_bytes = self.c2a_counter.to_bytes(8, byteorder='little')
                self.c2a_counter += 1
                ciper_and_mac = chacha20_aead_encrypt(len_bytes, self.c2a_key,
                                                      cnt_bytes,
                                                      bytes([0, 0, 0,
                                                             0]), tmp_data)

                try:
                    self.sock.send(len_bytes + ciper_and_mac[0] +
                                   ciper_and_mac[1])
                except OSError as e:
                    raise exceptions.AccessoryDisconnectedError(str(e))

            return self._read_response(self.timeout)