def _from_response(self, data): if len(data) != 16: raise DecodingError('Invalid SEL record length (%d)' % len(data)) self.data = data # pop will change data, therefore copy it buffer = ByteBuffer(data) self.record_id = buffer.pop_unsigned_int(2) self.type = buffer.pop_unsigned_int(1) if (self.type != self.TYPE_SYSTEM_EVENT and self.type not in self.TYPE_OEM_TIMESTAMPED_RANGE and self.type not in self.TYPE_OEM_NON_TIMESTAMPED_RANGE): raise DecodingError('Unknown SEL type (0x%02x)' % self.type) self.timestamp = buffer.pop_unsigned_int(4) self.generator_id = buffer.pop_unsigned_int(2) self.evm_rev = buffer.pop_unsigned_int(1) self.sensor_type = buffer.pop_unsigned_int(1) self.sensor_number = buffer.pop_unsigned_int(1) event_desc = buffer.pop_unsigned_int(1) if event_desc & 0x80: self.event_direction = EVENT_DEASSERTION else: self.event_direction = EVENT_ASSERTION self.event_type = event_desc & 0x3f self.event_data = buffer.pop_string(3)
def _encode(self): if not hasattr(self, '__fields__'): raise NotImplementedError('You have to overwrite this method') data = ByteBuffer() for field in self.__fields__: field.encode(self, data) return data.to_string()
def _encode(self): if not hasattr(self, '__fields__'): return '' data = ByteBuffer() for field in self.__fields__: field.encode(self, data) return data.tostring()
def _common_header(self, data): buffer = ByteBuffer(data[:]) try: self.id = buffer.pop_unsigned_int(2) self.version = buffer.pop_unsigned_int(1) self.type = buffer.pop_unsigned_int(1) self.length = buffer.pop_unsigned_int(1) except: raise DecodingError('Invalid SDR length (%d)' % len(data))
def from_response(self, data): if len(data) < 5: raise DecodingError('Invalid SDR length (%d)' % len(data)) self.data = data buffer = ByteBuffer(data[:]) self.id = buffer.pop_unsigned_int(2) self.version = buffer.pop_unsigned_int(1) self.type = buffer.pop_unsigned_int(1) self.lenght = buffer.pop_unsigned_int(1)
def _from_data(self, data): buffer = ByteBuffer(data[5:]) self.device_slave_address = buffer.pop_unsigned_int(1) >> 1 self.channel_number = buffer.pop_unsigned_int(1) & 0xf self.power_state_notification = buffer.pop_unsigned_int(1) self.global_initialization = 0 self.device_capabilities = buffer.pop_unsigned_int(1) self.reserved = buffer.pop_unsigned_int(3) self._entity(buffer.pop_slice(2)) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.tostring()
def test_bytebuffer_push_string(): buf = ByteBuffer() buf.push_string(b'0123') eq_(buf[0], 0x30) eq_(buf[1], 0x31) eq_(buf[2], 0x32) eq_(buf[3], 0x33) eq_(buf.tostring(), b'0123') buf = ByteBuffer() buf.push_string(b'\x00\xb4') eq_(buf.tostring(), b'\x00\xb4')
def get_sdr_data_helper(reserve_fn, get_fn, record_id, reservation_id=None): """Helper function to retrieve the sdr data using the specified functions. This can be used for SDRs from the Sensor Device or form the SDR repository. """ if reservation_id is None: reservation_id = reserve_fn() (next_id, data) = get_fn(reservation_id, record_id, 0, 5) header = ByteBuffer(data) record_id = header.pop_unsigned_int(2) record_version = header.pop_unsigned_int(1) record_type = header.pop_unsigned_int(1) record_payload_length = header.pop_unsigned_int(1) record_length = record_payload_length + 5 record_data = ByteBuffer(data) offset = len(record_data) max_req_len = 20 retry = 20 # now get the other record data while True: retry -= 1 if retry == 0: raise RetryError() length = max_req_len if (offset + length) > record_length: length = record_length - offset try: (next_id, data) = get_fn(reservation_id, record_id, offset, length) except CompletionCodeError, e: if e.cc == constants.CC_CANT_RET_NUM_REQ_BYTES: # reduce max lenght max_req_len -= 4 if max_req_len <= 0: retry = 0 else: Assert record_data.append_array(data[:]) offset = len(record_data) if len(record_data) >= record_length: break
def _from_data(self, data): buffer = ByteBuffer(data[5:]) # record key bytes self._common_record_key(buffer.pop_slice(3)) # record body bytes self._entity(buffer.pop_slice(2)) self.sensor_type = buffer.pop_unsigned_int(1) self.event_reading_type_code = buffer.pop_unsigned_int(1) self.record_sharing = buffer.pop_unsigned_int(2) self.reserved = buffer.pop_unsigned_int(1) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.tostring()
def sel_entries(self): """Generator which returns all SEL entries.""" req = create_request_by_name('GetSelInfo') rsp = self.send_message(req) check_completion_code(rsp.completion_code) if rsp.entries == 0: return reservation_id = self.get_sel_reservation_id() next_record_id = 0 while True: req = create_request_by_name('GetSelEntry') req.reservation_id = reservation_id req.record_id = next_record_id req.offset = 0 self.max_req_len = 0xff # read entire record record_data = ByteBuffer() while True: req.length = self.max_req_len if (self.max_req_len != 0xff and (req.offset + req.length) > 16): req.length = 16 - req.offset rsp = self.send_message(req) if rsp.completion_code == constants.CC_CANT_RET_NUM_REQ_BYTES: if self.max_req_len == 0xff: self.max_req_len = 16 else: self.max_req_len -= 1 continue else: check_completion_code(rsp.completion_code) record_data.append_array(rsp.record_data) req.offset = len(record_data) if len(record_data) >= 16: break next_record_id = rsp.next_record_id yield SelEntry(record_data) if next_record_id == 0xffff: break
def test_bytebuffer_push_unsigned_int(): buf = ByteBuffer((1, 0)) buf.push_unsigned_int(255, 1) eq_(buf[0], 1) eq_(buf[1], 0) eq_(buf[2], 255) buf.push_unsigned_int(255, 2) eq_(buf[3], 255) eq_(buf[4], 0) buf.push_unsigned_int(256, 2) eq_(buf[5], 0) eq_(buf[6], 1)
def get_sdr_data_helper(reserve_fn, get_fn, record_id, reservation_id=None): """Helper function to retrieve the sdr data using the specified functions. This can be used for SDRs from the Sensor Device or form the SDR repository. """ if reservation_id is None: reservation_id = reserve_fn() (next_id, data) = get_fn(reservation_id, record_id, 0, 5) header = ByteBuffer(data) record_id = header.pop_unsigned_int(2) record_version = header.pop_unsigned_int(1) record_type = header.pop_unsigned_int(1) record_payload_length = header.pop_unsigned_int(1) record_length = record_payload_length + 5 record_data = ByteBuffer(data) offset = len(record_data) max_req_len = 20 retry = 20 # now get the other record data while True: retry -= 1 if retry == 0: raise RetryError() length = max_req_len if (offset + length) > record_length: length = record_length - offset try: (next_id, data) = get_fn(reservation_id, record_id, offset, length) except CompletionCodeError as e: if e.cc == constants.CC_CANT_RET_NUM_REQ_BYTES: # reduce max lenght max_req_len -= 4 if max_req_len <= 0: retry = 0 else: raise CompletionCodeError(e.cc) record_data.extend(data[:]) offset = len(record_data) if len(record_data) >= record_length: break return (next_id, record_data)
def _decode(self, data): if not hasattr(self, '__fields__'): raise NotImplementedError('You have to overwrite this method') data = ByteBuffer(data) cc = None for field in self.__fields__: try: field.decode(self, data) except CompletionCodeError, e: # stop decoding on completion code != 0 cc = e.cc break
def from_data(self, data): buffer = ByteBuffer(data[5:]) self.owner_id = buffer.pop_unsigned_int(1) self.owner_lun = buffer.pop_unsigned_int(1) & 0x3 self.number = buffer.pop_unsigned_int(1) self.entity_id = buffer.pop_unsigned_int(1) self.entity_instance = buffer.pop_unsigned_int(1) self.sensor_type = buffer.pop_unsigned_int(1) self.event_reading_type_code = buffer.pop_unsigned_int(1) self.record_sharing = buffer.pop_unsigned_int(2) self.reserved = buffer.pop_unsigned_int(1) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.to_string()
def from_data(self, data): buffer = ByteBuffer(data[5:]) self.owner_id = buffer.pop_unsigned_int(1) self.owner_lun = buffer.pop_unsigned_int(1) & 0x3 self.number = buffer.pop_unsigned_int(1) self.entity_id = buffer.pop_unsigned_int(1) self.entity_instance = buffer.pop_unsigned_int(1) self.sensor_initialization = buffer.pop_unsigned_int(1) self.capabilities = buffer.pop_unsigned_int(1) self.sensor_type = buffer.pop_unsigned_int(1) self.event_reading_type_code = buffer.pop_unsigned_int(1) self.assertion_mask = buffer.pop_unsigned_int(2) self.deassertion_mask = buffer.pop_unsigned_int(2) self.discrete_reading_mask = buffer.pop_unsigned_int(2) self.units_1 = buffer.pop_unsigned_int(1) self.units_2 = buffer.pop_unsigned_int(1) self.units_3 = buffer.pop_unsigned_int(1) self.record_sharing = buffer.pop_unsigned_int(2) self.positive_going_hysteresis = buffer.pop_unsigned_int(1) self.negative_going_hysteresis = buffer.pop_unsigned_int(1) self.reserved = buffer.pop_unsigned_int(3) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.to_string()
def from_data(self, data): buffer = ByteBuffer(data[5:]) # record key bytes self.owner_id = buffer.pop_unsigned_int(1) self.owner_lun = buffer.pop_unsigned_int(1) & 0x3 self.number = buffer.pop_unsigned_int(1) # record body bytes self.entity_id = buffer.pop_unsigned_int(1) self.entity_instance = buffer.pop_unsigned_int(1) # byte 11 initialization = buffer.pop_unsigned_int(1) self.initialization = [] if initialization & 0x40: self.initialization.append('scanning') if initialization & 0x20: self.initialization.append('events') if initialization & 0x10: self.initialization.append('thresholds') if initialization & 0x08: self.initialization.append('hysteresis') if initialization & 0x04: self.initialization.append('type') if initialization & 0x02: self.initialization.append('default_event_generation') if initialization & 0x01: self.initialization.append('default_scanning') # byte 12 - sensor capabilities capabilities = buffer.pop_unsigned_int(1) self.capabilities = [] # ignore sensor if capabilities & 0x80: self.capabilities.append('ignore_sensor') # sensor auto re-arm support if capabilities & 0x40: self.capabilities.append('auto_rearm') # sensor hysteresis support HYSTERESIS_MASK = 0x30 HYSTERESIS_IS_NOT_SUPPORTED = 0x00 HYSTERESIS_IS_READABLE = 0x10 HYSTERESIS_IS_READ_AND_SETTABLE = 0x20 HYSTERESIS_IS_FIXED = 0x30 if capabilities & HYSTERESIS_MASK == HYSTERESIS_IS_NOT_SUPPORTED: self.capabilities.append('hysteresis_not_supported') elif capabilities & HYSTERESIS_MASK == HYSTERESIS_IS_READABLE: self.capabilities.append('hysteresis_readable') elif capabilities & HYSTERESIS_MASK == HYSTERESIS_IS_READ_AND_SETTABLE: self.capabilities.append('hysteresis_read_and_setable') elif capabilities & HYSTERESIS_MASK == HYSTERESIS_IS_FIXED: self.capabilities.append('hysteresis_fixed') # sensor threshold support THRESHOLD_MASK = 0x30 THRESHOLD_IS_NOT_SUPPORTED = 0x00 THRESHOLD_IS_READABLE = 0x10 THRESHOLD_IS_READ_AND_SETTABLE = 0x20 THRESHOLD_IS_FIXED = 0x30 if capabilities & THRESHOLD_MASK == THRESHOLD_IS_NOT_SUPPORTED: self.capabilities.append('threshold_not_supported') elif capabilities & THRESHOLD_MASK == THRESHOLD_IS_READABLE: self.capabilities.append('threshold_readable') elif capabilities & THRESHOLD_MASK == THRESHOLD_IS_READ_AND_SETTABLE: self.capabilities.append('threshold_read_and_setable') elif capabilities & THRESHOLD_MASK == THRESHOLD_IS_FIXED: self.capabilities.append('threshold_fixed') # sensor event message control support if (capabilities & 0x03) is 0: pass if (capabilities & 0x03) is 1: pass if (capabilities & 0x03) is 2: pass if (capabilities & 0x03) is 3: pass self.sensor_type_code = buffer.pop_unsigned_int(1) self.event_reading_type_code = buffer.pop_unsigned_int(1) self.assertion_mask = buffer.pop_unsigned_int(2) self.deassertion_mask = buffer.pop_unsigned_int(2) self.discrete_reading_mask = buffer.pop_unsigned_int(2) # byte 21, 22, 23 units_1 = buffer.pop_unsigned_int(1) units_2 = buffer.pop_unsigned_int(1) units_3 = buffer.pop_unsigned_int(1) self.analog_data_format = (units_1 >> 6) & 0x3 self.rate_unit = (units_1 >> 3) >> 0x7 self.modifier_unit = (units_1 >> 1) & 0x2 self.percentage = units_1 & 0x1 # byte 24 self.linearization = buffer.pop_unsigned_int(1) & 0x7f # byte 25, 26 m = buffer.pop_unsigned_int(1) m_tol = buffer.pop_unsigned_int(1) self.m = (m & 0xff) | ((m_tol & 0xc0) << 2) self.tolerance = (m_tol & 0x3f) # byte 27, 28, 29 b = buffer.pop_unsigned_int(1) b_acc = buffer.pop_unsigned_int(1) acc_accexp = buffer.pop_unsigned_int(1) self.b = (b & 0xff) | ((b_acc & 0xc0) << 2) self.b = self._convert_complement(self.b, 10) self.accuracy = (b_acc & 0x3f) | ((acc_accexp & 0xf0) << 4) self.accuracy_exp = (acc_accexp & 0x0c) >> 2 # byte 30 rexp_bexp = buffer.pop_unsigned_int(1) self.k2 = (rexp_bexp & 0xf0) >> 4 # convert 2s complement #if self.k2 & 0x8: # 4bit # self.k2 = -0x10 + self.k2 self.k2 = self._convert_complement(self.k2, 4) self.k1 = rexp_bexp & 0x0f # convert 2s complement #if self.k1 & 0x8: # self.k1 = -0x10 + self.k1 self.k1 = self._convert_complement(self.k1, 4) # byte 31 analog_characteristics = buffer.pop_unsigned_int(1) self.analog_characteristic = [] if analog_characteristics & 0x01: self.analog_characteristic.append('nominal_reading') if analog_characteristics & 0x02: self.analog_characteristic.append('normal_max') if analog_characteristics & 0x04: self.analog_characteristic.append('normal_min') self.nominal_reading = buffer.pop_unsigned_int(1) self.normal_maximum = buffer.pop_unsigned_int(1) self.normal_minimum = buffer.pop_unsigned_int(1) self.sensor_maximum_reading = buffer.pop_unsigned_int(1) self.sensor_minimum_reading = buffer.pop_unsigned_int(1) self.threshold = {} self.threshold['unr'] = buffer.pop_unsigned_int(1) self.threshold['ucr'] = buffer.pop_unsigned_int(1) self.threshold['unc'] = buffer.pop_unsigned_int(1) self.threshold['lnr'] = buffer.pop_unsigned_int(1) self.threshold['lcr'] = buffer.pop_unsigned_int(1) self.threshold['lnc'] = buffer.pop_unsigned_int(1) self.hysteresis = {} self.hysteresis['positive_going'] = buffer.pop_unsigned_int(1) self.hysteresis['negative_going'] = buffer.pop_unsigned_int(1) self.reserved = buffer.pop_unsigned_int(2) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.to_string()
def test_bytebuffer_pop_slice_error(): buf = ByteBuffer(b'\x30\x31\x32\x33') buf.pop_slice(5)
def _from_data(self, data): buffer = ByteBuffer(data[5:]) self.device_access_address = buffer.pop_unsigned_int(1) >> 1 self.fru_device_id = buffer.pop_unsigned_int(1) self.logical_physical = buffer.pop_unsigned_int(1) self.channel_number = buffer.pop_unsigned_int(1) self.reserved = buffer.pop_unsigned_int(1) self.device_type = buffer.pop_unsigned_int(1) self.device_type_modifier= buffer.pop_unsigned_int(1) self._entity(buffer.pop_slice(2)) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.tostring()
def test_bytebuffer_pop_unsigned_int_error(): buf = ByteBuffer((0, 0)) buf.pop_unsigned_int(3)
def test_bytebuffer_init_from_tuple(): buf = ByteBuffer((0xf8,)) eq_(buf.array, array('B', [0xf8]))
def test_bytebuffer_init_from_list(): buf = ByteBuffer([0xf8]) eq_(buf.array, array('B', [0xf8]))
def test_bytebuffer_initi_fromstring(): buf = ByteBuffer(b'\xf8') eq_(buf.array, array('B', [0xf8]))
def from_data(self, data): buffer = ByteBuffer(data[5:]) self.device_slave_address = buffer.pop_unsigned_int(1) >> 1 self.channel_number = buffer.pop_unsigned_int(1) & 0xf self.power_state_notification = buffer.pop_unsigned_int(1) self.global_initialization = 0 self.device_capabilities = buffer.pop_unsigned_int(1) self.reserved = buffer.pop_unsigned_int(3) self.entity_id = buffer.pop_unsigned_int(1) self.entity_instance = buffer.pop_unsigned_int(1) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.to_string()
def from_data(self, data): buffer = ByteBuffer(data[5:]) # record key bytes self.device_access_address = buffer.pop_unsigned_int(1) >> 1 self.fru_device_id = buffer.pop_unsigned_int(1) self.logical_physical = buffer.pop_unsigned_int(1) self.channel_number = buffer.pop_unsigned_int(1) # record body bytes self.reserved = buffer.pop_unsigned_int(1) self.device_type = buffer.pop_unsigned_int(1) self.device_type_modifier= buffer.pop_unsigned_int(1) self.entity_id = buffer.pop_unsigned_int(1) self.entity_instance = buffer.pop_unsigned_int(1) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.to_string()
def _from_data(self, data): buffer = ByteBuffer(data[5:]) # record key bytes self._common_record_key(buffer.pop_slice(3)) # record body bytes self._entity(buffer.pop_slice(2)) self.sensor_initialization = buffer.pop_unsigned_int(1) self.capabilities = buffer.pop_unsigned_int(1) self.sensor_type_code = buffer.pop_unsigned_int(1) self.event_reading_type_code = buffer.pop_unsigned_int(1) self.assertion_mask = buffer.pop_unsigned_int(2) self.deassertion_mask = buffer.pop_unsigned_int(2) self.discrete_reading_mask = buffer.pop_unsigned_int(2) self.units_1 = buffer.pop_unsigned_int(1) self.units_2 = buffer.pop_unsigned_int(1) self.units_3 = buffer.pop_unsigned_int(1) self.record_sharing = buffer.pop_unsigned_int(2) self.positive_going_hysteresis = buffer.pop_unsigned_int(1) self.negative_going_hysteresis = buffer.pop_unsigned_int(1) self.reserved = buffer.pop_unsigned_int(3) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.tostring()
def encode(self): data = ByteBuffer() self.field.encode(self, data) return data
def decode(self, data): data = ByteBuffer(data) self.field.decode(self, data)
def test_bytebuffer_pop_unsigned_int(): buf = ByteBuffer((1, 0, 0, 0)) eq_(buf.pop_unsigned_int(1), 1) buf = ByteBuffer((0, 1, 0, 0)) eq_(buf.pop_unsigned_int(2), 0x100) buf = ByteBuffer((0, 0, 1, 0)) eq_(buf.pop_unsigned_int(3), 0x10000) buf = ByteBuffer((0, 0, 0, 1)) eq_(buf.pop_unsigned_int(4), 0x1000000)
def test_bytebuffer_pop_string(): buf = ByteBuffer(b'\x30\x31\x32\x33') eq_(buf.pop_string(2), b'01') eq_(buf.tostring(), b'23')
def from_data(self, data): buffer = ByteBuffer(data[5:]) # record key bytes self.device_access_address = buffer.pop_unsigned_int(1) >> 1 self.fru_device_id = buffer.pop_unsigned_int(1) self.logical_physical = buffer.pop_unsigned_int(1) self.channel_number = buffer.pop_unsigned_int(1) # record body bytes self.reserved = buffer.pop_unsigned_int(1) self.device_type = buffer.pop_unsigned_int(1) self.device_type_modifier = buffer.pop_unsigned_int(1) self.entity_id = buffer.pop_unsigned_int(1) self.entity_instance = buffer.pop_unsigned_int(1) self.oem = buffer.pop_unsigned_int(1) self.device_id_string_type_length = buffer.pop_unsigned_int(1) self.device_id_string = buffer.to_string()
def test_bytebuffer_tostring(): buf = ByteBuffer(b'\x30\x31\x32\x33') eq_(buf.tostring(), b'0123')
def test_bytebuffer_pop_slice(): buf = ByteBuffer(b'\x30\x31\x32\x33') cut = buf.pop_slice(1) eq_(buf.tostring(), b'123') eq_(cut.tostring(), b'0') buf = ByteBuffer(b'\x30\x31\x32\x33') cut = buf.pop_slice(2) eq_(buf.tostring(), b'23') eq_(cut.tostring(), b'01') buf = ByteBuffer(b'\x30\x31\x32\x33') cut = buf.pop_slice(3) eq_(buf.tostring(), b'3') eq_(cut.tostring(), b'012') buf = ByteBuffer(b'\x30\x31\x32\x33') cut = buf.pop_slice(4) eq_(buf.tostring(), b'') eq_(cut.tostring(), b'0123')
def from_response(self, data): if len(data) != 16: raise DecodingError('Invalid SEL record length (%d)' % len(data)) self.data = data # pop will change data, therefore copy it buffer = ByteBuffer(data) self.record_id = buffer.pop_unsigned_int(2) self.type = buffer.pop_unsigned_int(1) if (self.type != self.TYPE_SYSTEM_EVENT and self.type not in self.TYPE_OEM_TIMESTAMPED_RANGE and self.type not in self.TYPE_OEM_NON_TIMESTAMPED_RANGE): raise DecodingError('Unknown SEL type (0x%02x)' % self.type) self.timestamp = buffer.pop_unsigned_int(4) self.generator_id = buffer.pop_unsigned_int(2) self.evm_rev = buffer.pop_unsigned_int(1) self.sensor_type = buffer.pop_unsigned_int(1) self.sensor_number = buffer.pop_unsigned_int(1) event_desc = buffer.pop_unsigned_int(1) if event_desc & 0x80: self.event_direction = EVENT_DEASSERTION else: self.event_direction = EVENT_ASSERTION self.event_type = event_desc & 0x3f self.event_data = buffer.pop_string(3)