def _verify_crc(self, buf, crc_index, crc_init): crc = (buf[crc_index] << 8) | buf[crc_index + 1] calc_crc = crc16(buf[:crc_index], crc_init) if debug.LOG_SERIAL: self._logger.debug('crc: %s calc_crc: %s', hex(crc), hex(calc_crc)) if crc != calc_crc: raise CrcException('bad CRC')
def impl(): write_cmd = _command('SelP' + chr(which)) calc_crc = crc16([which], init=0x1114) self._write(write_cmd) resp = self._read(nbytes=len(write_cmd) + 2) crc = (resp[len(write_cmd)] << 8) | resp[len(write_cmd) + 1] if crc != calc_crc: raise CrcException( 'set preset %s failed: invalid CRC %s != %s' % (which, hex(crc), hex(calc_crc)))
def impl(): write_cmd = _command('WrtP') ii = 1 for preset in presets: preset.is_validated = not preset.is_empty write_cmd.extend(preset.raw_bytes()) # every 510 bytes, compute and add checksum if (ii % 5) == 0: block_num = ii // 5 - 1 start = 4 + block_num * 512 end = start + 510 assert (len(write_cmd) == end) cksum = checksum(write_cmd[start:end], init=0xc8) write_cmd.append(cksum >> 8) write_cmd.append(cksum & 0xff) ii += 1 if self._logger.isEnabledFor(logging.DEBUG): _verify_preset_checksums(write_cmd[4:]) assert (len(write_cmd) == 7684) swap_bytes(write_cmd, start=4) calc_crc = crc16(write_cmd[4:], init=0x4d1) self._logger.debug('erase presets') cmd = _command('ErsP') self._write(cmd) resp = self._read(nbytes=6) _verify_cmd_with_values(cmd, resp, bytes([0x22, 0x1b])) sleep(.05) self._logger.debug('write presets') self._write(write_cmd, timeout=7) sleep(5.25) resp = self._read(nbytes=7686, timeout=7) if len(resp) != 7686: raise VerifyException( 'did not get expected response length: %d != %d' % (len(resp), 7686)) crc = (resp[7684] << 8) | resp[7685] if crc != calc_crc: raise CrcException( 'write presets failed: invalid CRC %s != %s' % (hex(crc), hex(calc_crc))) self._logger.debug('presets write success')
def _handle_messages(self): self._advance_to_next_preamble() buf_len = self._buf.size() while constants.Message.OVERHEAD <= buf_len: assert (self._buf.peek() == constants.Message.PREAMBLE_BYTE) payload_len_start_idx = self._buf.read_index + constants.Message.PAYLOAD_LEN_OFFSET payload_len = struct.unpack( constants.Message.PAYLOAD_LEN_FORMAT, self._buf[payload_len_start_idx:payload_len_start_idx + constants.Message.PAYLOAD_LEN_BYTES])[0] message_size = payload_len + constants.Message.OVERHEAD if debug.LOG_BLUETOOTH: self._logger.debug('message_size: %d', message_size) if message_size > self._buf.capacity(): self._logger.warning('message larger than circ buf size') self._advance_to_next_preamble() buf_len = self._buf.size() continue elif message_size <= buf_len: payload_start_idx = self._buf.read_index + constants.Message.PAYLOAD_OFFSET crc_start_idx = payload_start_idx + payload_len crc = struct.unpack( constants.Message.CRC_FORMAT, self._buf[crc_start_idx:crc_start_idx + constants.Message.CRC_BYTES])[0] calc_crc = crc16(self._buf[self._buf.read_index:crc_start_idx], init=constants.Message.CRC_SEED) if debug.LOG_BLUETOOTH: self._logger.debug('crc: %s calc_crc: %s', hex(crc), hex(calc_crc)) if crc == calc_crc: message_id = self._buf[self._buf.read_index + constants.Message.MESSAGE_ID_OFFSET] payload = self._buf[payload_start_idx:crc_start_idx] self._buf.advance(constants.Message.OVERHEAD + payload_len) self._handle_message(message_id, payload) else: # TODO: add timeout break self._advance_to_next_preamble() buf_len = self._buf.size()
def _write(self, message_id, payload): if debug.LOG_BLUETOOTH: self._logger.debug('_write - notifying: %s', self._notifying) if self._notifying: buf = bytearray(len(payload) + 7) ii = self.add_header(buf, message_id, payload) for bb in payload: buf[ii] = bb ii += 1 crc = crc16(buf[:-constants.Message.CRC_BYTES], init=constants.Message.CRC_SEED) struct.pack_into(constants.Message.CRC_FORMAT, buf, len(buf) - constants.Message.CRC_BYTES, crc) dbus_bytes = [dbus.Byte(bb) for bb in buf] if debug.LOG_BLUETOOTH: print_bytes(self._logger, logging.DEBUG, dbus_bytes, 'w') for ii in range(0, len(dbus_bytes), 40): self.PropertiesChanged(bluez_dbus.GATT_CHRC_IFACE, {'Value': dbus_bytes[ii:ii + 40]}, [])
def impl(): write_cmd = _command('WrtC') write_cmd.extend(options.raw_bytes()[128:192]) assert (len(write_cmd) == 68) swap_bytes(write_cmd, start=4) calc_crc = crc16(write_cmd[4:], init=0xf5) self._logger.debug('erase options') cmd = _command('ErsC') self._write(cmd) resp = self._read(nbytes=6) _verify_cmd_with_values(cmd, resp, bytes([0x0d, 0x04])) self._logger.debug('write options') self._write(write_cmd) resp = self._read(nbytes=70) crc = (resp[68] << 8) | resp[69] if crc != calc_crc: raise CrcException( 'write options failed: invalid CRC %s != %s' % (hex(crc), hex(calc_crc))) self._logger.debug('options write success')