def __init__(self): self.constructFrame = Struct( "parser", OptionalGreedyRange( Struct( "packets", UBInt8("header"), UBInt16("plen"), UBInt8("dir"), ULInt64("nodeid"), UBInt16("funcid"), Array( lambda ctx: (ctx.plen - 1 - 8 - 2) / 8, Struct( "datas", ULInt16("type"), ULInt16("unit"), LFloat32("value"), )), UBInt8("sum"), # UBInt8("simulation") ), ), OptionalGreedyRange(UBInt8("leftovers"), ), )
SLInt64('rx_bytes'), SLInt64('tx_bytes'), SLInt64('rx_packets'), SLInt64('rx_duration'), SLInt64('tx_packets'), SLInt64('tx_retries'), SLInt64('tx_failed'), SLInt64('beacon_loss'), SLInt64('beacon_rx'), SLInt64('rx_drop_misc'), SLInt32('signal'), SLInt32('signal_avg'), SLInt32('beacon_signal_avg'), SLInt64('time_offset'), SLInt64('connected_time'), LFloat32('tx_bitrate'), ) msg_sta_statistics = Struct('msg_sta_statistics', Embed(msg_default), # default fields Embed(field_intf_name), Embed(field_station), SLInt32('num_stats'), Array(lambda ctx: ctx.num_stats, stats_field), Embed(field_time_stamp), # Probe() ) def get_sta_statistics(server, id=0, intf_name=None, sta_ip=None, sta_port=0): """
def read_ftr(filename, delete_keys=None): offset_size = 1859 ftr_record_struct = Struct( "record", LFloat32("Datetime"), LFloat32("PosX"), LFloat32("PosY"), LFloat32("Altitude"), LFloat32("Qx"), LFloat32("Qy"), LFloat32("Qz"), LFloat32("Qw"), LFloat32("DistUnused"), ) ftr_struct = Struct( "ftr_header", String("filetype", 4), #Bytes("unknown00", 136), #String("FirstName", 17), Bytes("unknown00", 135), String("FirstName", 17), String("FamilyName", 17), String("Country", 17), String("RN", 8), String("CN", 4), Bytes("unknown02", 5), String("Landscape", 17), Bytes("unknown03", offset_size - 4 - 135 - 17 - 17 - 17 - 17 - 17), ULInt32("length"), # uint32 (4 bytes) @ 1859 Array(lambda ctx: ctx.length, ftr_record_struct), ) with open(filename, "rb") as fd: dat = ftr_struct.parse_stream(fd) df_ftr = pd.DataFrame(dat['record']) df_ftr['Time'] = df_ftr['Datetime'] * 3600.0 df_ftr['Time'] = df_ftr['Time'] - df_ftr['Time'].irow(0) df_ftr['Datetime'] = pd.to_datetime(df_ftr['Time'], unit='s') df_ftr['Deltatime'] = df_ftr['Datetime'] - df_ftr['Datetime'].shift(1) df_ftr['Deltatime'] = df_ftr['Deltatime'] / np.timedelta64( 1, 's') # Deltatime as seconds #df_ftr['Vz'] = ((df_ftr['Altitude'] - df_ftr['Altitude'].shift(1)).fillna(0) / df_ftr['Deltatime']).fillna(0) #df_ftr = df_ftr.set_index('Datetime', verify_integrity=True) df_ftr = df_ftr.set_index('Time', verify_integrity=True) # Time (s) dat['record'] = df_ftr if delete_keys is not None: for key in delete_keys: if key in dat.keys(): del dat[key] for key in ['FirstName', 'FamilyName', 'Country', 'Landscape', 'RN', 'CN']: length = ord(dat[key][0]) s = dat[key][1:length + 1] dat[key] = s.replace('\x00', '') assert len( dat[key]) == length, "Length error with %s len=%d should be %d" % ( s, len(s), length) return (dat)
from pox.ethanol.ssl_message.msg_common import is_error_msg, tri_boolean, len_of_string msg_ping = Struct( 'msg_ping', Embed(msg_default), # default fields SLInt32('data_size'), If(lambda ctx: ctx["data_size"] > 0, CString("data")), # Probe(), ) """ ping message data structure """ msg_pong = Struct( 'msg_pong', Embed(msg_default), # default fields LFloat32('rtt'), # float com 32 bits, LFloat32 (C little endian 32 bits) SLInt8('verify_data'), # class boolean # Probe(), ) """ pong message data structure """ BYTE_INICIAL = 48 def generate_ping_data(p_size=64): data = '' for i in range(p_size): data += chr((BYTE_INICIAL + i) % 128) # 7-bit ASCII data += chr(0) return data
Field("data", lambda ctx: ctx.size - 7)) struct_byte = Struct("byte", UBInt8("byte")) struct_batt_connected = Struct("batt_connected", UBInt8("slots_number"), Array(lambda ctx: ctx.slots_number, UBInt8("slots"))) struct_batt_get_status = Struct("batt_get_status", UBInt8("batt_number"), Array(lambda ctx: ctx.batt_number, Struct("batt_data", UBInt8("slot_number"), UBInt16("tension"), SBInt16("corriente"), LFloat32("temp"), UBInt16("remaining"), UBInt16("full_charge"), UBInt16("ciclos")))) struct_batt_params = Struct("batt_params", UBInt8("batt_number"), Array(lambda ctx: ctx.batt_number, Struct("batt_data", UBInt8("slot_number"), UBInt16("design_capacity"), Array(11, UBInt8("manufacturer")), UBInt16("serial_number"), Array(7, UBInt8("model")), Array(4, UBInt8("chem")), UBInt16("date_manuf"), UBInt16("nominal_tension"))))
>>> sng = SONG.parse(raw_input) >>> build_output = SONG.build(sng) >>> build_output == raw_input True """ from construct import Struct, If, Array, PrefixedArray, Padding, \ SLInt8, ULInt16, SLInt16, ULInt32, SLInt32, LFloat32, LFloat64, String def array(struct): """Standard prefixed arrays.""" return PrefixedArray(struct, ULInt32('count')) BEAT = Struct('ebeats', LFloat32('time'), ULInt16('measure'), ULInt16('beat'), ULInt32('phraseIteration'), ULInt32('mask')) PHRASE = Struct('phrases', SLInt8('solo'), SLInt8('disparity'), SLInt8('ignore'), Padding(1), ULInt32('maxDifficulty'), ULInt32('phraseIterationLinks'), String('name', 32, padchar='\x00')) CHORD_TEMPLATE = Struct('chordTemplates', ULInt32('mask'), SLInt8('fret0'), SLInt8('fret1'), SLInt8('fret2'), SLInt8('fret3'), SLInt8('fret4'), SLInt8('fret5'), SLInt8('finger0'), SLInt8('finger1'), SLInt8('finger2'), SLInt8('finger3'), SLInt8('finger4'), SLInt8('finger5'), Array(6, SLInt32('notes')), String('chordName', 32, padchar='\x00'))
from pox.ethanol.ethanol.ap import add_ap from pox.ethanol.ethanol.station import add_station from pox.ethanol.events import Events events_hello = Events() """to handle a receiving hello message, just add your function to events_hello your function must use 'def my_funct(**kwargs)' signature for compatibility @change: we send to parameters: msg, fromaddr """ msg_hello = Struct('msg_hello', Embed(msg_default), # default fields SLInt32('device_type'), # 0 = controller, 1 = ap, 2 = station SLInt32('tcp_port'), LFloat32('rtt') # float com 32 bits ) def send_msg_hello(server, m_id=0): """ @param server: tuple (ip, port_num) @param m_id: message id @return: msg - received message """ ssl_sock, sckt = connect_ssl_socket(server) # print "send_msg_hello id:", m_id # 1) create message msg_struct = Container(m_type=MSG_TYPE.MSG_HELLO_TYPE,
from construct import ULInt32, LFloat32, ULInt8 from construct import Embed from construct import Array, Struct, Container # from construct.debug import Probe from pox.ethanol.ssl_message.msg_core import msg_default from pox.ethanol.ssl_message.msg_core import field_intf_name from pox.ethanol.ssl_message.msg_core import field_station from pox.ethanol.ssl_message.msg_core import field_mac_addr from pox.ethanol.ssl_message.msg_common import MSG_TYPE, VERSION from pox.ethanol.ssl_message.msg_common import send_and_receive_msg, len_of_string iw_bitrates = Struct( 'iw_bitrates', LFloat32("bitrate"), ULInt8('is_short'), # this is a boolean coded as a byte ) iw_bands = Struct( 'iw_bands', Embed(field_intf_name), ULInt32('band'), ULInt32('num_bitrates'), # Probe(), Array(lambda ctx: ctx.num_bitrates, iw_bitrates), ) msg_tx_bitrates = Struct( 'msg_tx_bitrates', Embed(msg_default), # default fields
def __iter__(self): table_definition = self.tables.get_definition( self.current_table_number) for page_ref in self.pages.list(): if self.pages[page_ref].hierarchy_level == 0: for record in TpsRecordsList(self, self.pages[page_ref], encoding=self.encoding, check=self.check): if record.type == 'DATA' and record.data.table_number == self.current_table_number: check_value('table_record_size', len(record.data.data), table_definition.record_size) # TODO convert name to string fields = {"b':RecNo'": record.data.record_number} for field in table_definition.record_table_definition_field: field_data = record.data.data[field. offset:field.offset + field.size] value = '' if field.type == 'BYTE': value = ULInt8('byte').parse(field_data) elif field.type == 'SHORT': value = SLInt16('short').parse(field_data) elif field.type == 'USHORT': value = ULInt16('ushort').parse(field_data) elif field.type == 'DATE': value = self.to_date(field_data) elif field.type == 'TIME': value = self.to_time(field_data) elif field.type == 'LONG': #TODO if field.name.decode(encoding='cp437').split( ':')[1].lower() in self.date_fieldname: if SLInt32('long').parse(field_data) == 0: value = None else: value = date.fromordinal( 657433 + SLInt32('long').parse(field_data)) elif field.name.decode(encoding='cp437').split( ':')[1].lower() in self.time_fieldname: s, ms = divmod( SLInt32('long').parse(field_data), 100) value = str('{}.{:03d}'.format( time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)) else: value = SLInt32('long').parse(field_data) elif field.type == 'ULONG': value = ULInt32('ulong').parse(field_data) elif field.type == 'FLOAT': value = LFloat32('float').parse(field_data) elif field.type == 'DOUBLE': value = LFloat64('double').parse(field_data) elif field.type == 'DECIMAL': # TODO BCD if field_data[0] & 0xF0 == 0xF0: sign = -1 field_data = bytearray(field_data) field_data[0] &= 0x0F else: sign = 1 value = sign * int(hexlify( field_data)) / 10**field.decimal_count elif field.type == 'STRING': value = text_type( field_data, encoding=self.encoding).strip() elif field.type == 'CSTRING': value = text_type( field_data, encoding=self.encoding).strip() elif field.type == 'PSTRING': value = text_type( field_data[1:field_data[0] + 1], encoding=self.encoding).strip() else: # GROUP=0x16 # raise ValueError #TODO pass fields[text_type(field.name)] = value # print(fields) yield fields
def register_metric(mac, device): """ use this function to register the device object process_association will call the object's methods to deal with each one of the association steps mac is the device's mac address """ # print "inside register_functions" registered_functions[mac] = device msg_metric_received = Struct( 'msg_metric', Embed(msg_default), # default fields Embed(field_mac_addr), # mac of the device ULInt64('metric'), # index of the metric LFloat32('value'), # metric EWMA ) """ all received metric message types are the same """ def process_metric(received_msg, fromaddr): """ calls the device evMetric""" msg = msg_metric_received.parse(received_msg) mac_device = msg['mac_addr'] if mac_device in registered_functions: device = registered_functions[mac_device] value = msg['value'] metric = msg['metric'] device.evMetric(metric, value) return None
# from construct.debug import Probe from pox.ethanol.ssl_message.msg_core import msg_default from pox.ethanol.ssl_message.msg_core import field_mac_addr, field_ssid, field_intf_name, field_station from pox.ethanol.ssl_message.msg_common import MSG_TYPE, VERSION from pox.ethanol.ssl_message.msg_common import send_and_receive_msg, tri_boolean, len_of_string ap_in_range = Struct( 'ap_in_range', Embed(field_intf_name), Embed(field_mac_addr), Embed(field_ssid), SLInt32('status'), SLInt64('frequency'), SLInt32('channel'), LFloat32('signal'), # float in C is coded as little endian 32 bit number SLInt32('powerconstraint'), SLInt32('tx_power'), SLInt32('link_margin'), SLInt32('age'), SLInt8('is_dBm'), # this is a boolean coded as a 8 bit integer ) msg_ap_in_range = Struct( 'msg_ap_in_range', Embed(msg_default), # default fields Embed(field_intf_name), Embed(field_station), ULInt32('num_aps'), Array(lambda ctx: ctx.num_aps, ap_in_range), # Probe(),
struct_base = Struct("base", UBInt8("version"), SizeAdapter(Field("size", 3))) struct_common = Struct("common", Embed(struct_base), UBInt8("msg_type"), UBInt8("device"), UBInt8("command"), Field("data", lambda ctx: ctx.size - 7)) struct_byte = Struct("byte", UBInt8("byte")) struct_batt_connected = Struct( "batt_connected", UBInt8("slots_number"), Array(lambda ctx: ctx.slots_number, UBInt8("slots"))) struct_batt_get_status = Struct( "batt_get_status", UBInt8("batt_number"), Array( lambda ctx: ctx.batt_number, Struct("batt_data", UBInt8("slot_number"), UBInt16("tension"), SBInt16("corriente"), LFloat32("temp"), UBInt16("remaining"), UBInt16("full_charge"), UBInt16("ciclos")))) struct_batt_params = Struct( "batt_params", UBInt8("batt_number"), Array( lambda ctx: ctx.batt_number, Struct("batt_data", UBInt8("slot_number"), UBInt16("design_capacity"), Array(11, UBInt8("manufacturer")), UBInt16("serial_number"), Array(7, UBInt8("model")), Array(4, UBInt8("chem")), UBInt16("date_manuf"), UBInt16("nominal_tension")))) struct_power_check = Struct("power_check", LFloat32("v_24"), LFloat32("v_12"), LFloat32("v_5"), LFloat32("v_3")) struct_batt_level = Struct("batt_level", UBInt8("slot_number"), UBInt8("level_empty"), UBInt8("level_critical"), UBInt8("level_min"), UBInt8("full_charge"), UBInt16("full_charge_current"))