def send(self, cmd_type, addr, data_tags = None, cb = None): """Send the msg to the mote """ oap_msg = OAPMessage.build_oap( self.seq_num, self.session_id, cmd_type, addr, tags=data_tags, sync=True ) # TODO: adjust send_data to match connector # send_data expects msg as list of integers oap_payload = [ord(b) for b in oap_msg] #print ' '.join(['TX: ']+["%.2x"%c for c in oap_payload]) self.send_data( self.mac, 0, OAPMessage.OAP_PORT, OAPMessage.OAP_PORT, 0, oap_payload ) # append the callback for the response to the message queue if cb: self.message_queue.append((self.seq_num, cmd_type, cb))
def dispatch_pkt(self, notif_type, data_notif): """Parse and dispatch an OAP packet to the correct handler based on its type""" if data_notif.dstPort != OAPMessage.OAP_PORT: return payload = array('B', data_notif.data) # first two bytes are transport header trans = OAPMessage.extract_oap_header(payload) # third byte is OAP command type cmd_type = payload[2] #print trans #print int(cmd_type) if trans['response']: oap_resp = OAPMessage.parse_oap_response(payload, 2) self._response_callbacks(data_notif.macAddress, oap_resp, trans) self.last_response = oap_resp elif cmd_type == OAPMessage.CmdType.NOTIF: oap_notif = OAPNotif.parse_oap_notif(payload, 3) self._notif_callbacks(data_notif.macAddress, oap_notif) self.last_notif = oap_notif
def send(self, cmd_type, addr, data_tags=None, cb=None): """Send the msg to the mote """ oap_msg = OAPMessage.build_oap(self.seq_num, self.session_id, cmd_type, addr, tags=data_tags, sync=True) # TODO: adjust send_data to match connector # send_data expects msg as list of integers oap_payload = [ord(b) for b in oap_msg] #print ' '.join(['TX: ']+["%.2x"%c for c in oap_payload]) self.send_data(self.mac, 0, OAPMessage.OAP_PORT, OAPMessage.OAP_PORT, 0, oap_payload) # append the callback for the response to the message queue if cb: self.message_queue.append((self.seq_num, cmd_type, cb))
def parse_oap_notif(data, index=0): ''' Parse the OAP notification data and return a OAPNotif object. data: array of bytes from the OAP notification payload. OAP transport header and OAP command type should be stripped ''' # TODO: validate length result = None # notif type notif_type = data[index] index += 1 # payload if notif_type == NOTIFTYPE_SAMPLE: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag != TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index + 1) index += 12 # rate rate = struct.unpack_from('!l', data, index)[0] index += 4 # numSamples num_samples = int(data[index]) index += 1 # sampleSize sample_size = int(data[index]) index += 1 #===== create and populate result structure if channel == TEMP_ADDRESS: result = OAPTempSample() result.packet_timestamp = (secs, usecs) result.rate = rate result.num_samples = num_samples result.sample_size = sample_size for i in range(num_samples): temp = struct.unpack('!h', data[index:index + 2])[0] index += 2 result.samples.append(temp) else: result = OAPSample() result.packet_timestamp = (secs, usecs) result.rate = rate result.num_samples = num_samples result.sample_size = sample_size result.samples = [] bit_index = 0 for i in range(num_samples): sample_data = OAPMessage.read_bits(data[index:], bit_index, sample_size) result.samples.append(sample_data) elif notif_type == NOTIFTYPE_STATS: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag != TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index + 1) index += 12 # rate rate = struct.unpack_from('!l', data, index)[0] index += 4 # numSamples num_samples = int(data[index]) index += 1 # sampleSize sample_size = int(data[index]) index += 1 #===== create and populate result structure # TODO populate result = OAPAnalogStats() result.packet_timestamp = (secs, usecs) elif notif_type == NOTIFTYPE_DIG: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag != TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index + 1) index += 12 # new value new_val = int(data[index]) index += 1 #===== create and populate result structure result = OAPDigitalIn() result.channel = channel result.packet_timestamp = (secs, usecs) result.new_val = new_val elif notif_type == NOTIFTYPE_LOCATEME: #===== parse raise NotImplementedError("parsing locateMe OAP notification") #===== create and populate result structure raise NotImplementedError("filling locateMe OAP result structure") elif notif_type == 4: # pkgen #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag != 0xFF: raise ValueError('invalid notification data: expected address tag') index += 2 + l # pid pid = struct.unpack_from('!l', data, index)[0] index += 4 # startPid startPid = struct.unpack_from('!l', data, index)[0] index += 4 # numPackets numPackets = struct.unpack_from('!l', data, index)[0] index += 4 # payload payload = data[index:] #===== create and populate result structure result = OAPpkGenPacket() result.pid = pid result.startPid = startPid result.numPackets = numPackets result.payload = payload # set common notification attributes if result: result.raw_data = data result.channel = channel result.received_timestamp = datetime.datetime.now() return result
def parse_oap_notif(data, index = 0): ''' Parse the OAP notification data and return a OAPNotif object. data: array of bytes from the OAP notification payload. OAP transport header and OAP command type should be stripped ''' # TODO: validate length result = None # notif type notif_type = data[index] index += 1 # payload if notif_type==NOTIFTYPE_SAMPLE: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag!=TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index + 1) index += 12 # rate rate = struct.unpack_from('!l', data, index)[0] index += 4 # numSamples num_samples = int(data[index]) index += 1 # sampleSize sample_size = int(data[index]) index += 1 #===== create and populate result structure if channel == TEMP_ADDRESS: result = OAPTempSample() result.packet_timestamp = (secs, usecs) result.rate = rate result.num_samples = num_samples result.sample_size = sample_size for i in range(num_samples): temp = struct.unpack('!h', data[index:index+2])[0] index += 2 result.samples.append(temp) else: result = OAPSample() result.packet_timestamp = (secs, usecs) result.rate = rate result.num_samples = num_samples result.sample_size = sample_size result.samples = [] bit_index = 0 for i in range(num_samples): sample_data = OAPMessage.read_bits(data[index:], bit_index, sample_size) result.samples.append(sample_data) elif notif_type==NOTIFTYPE_STATS: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag!=TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index + 1) index += 12 # rate rate = struct.unpack_from('!l', data, index)[0] index += 4 # numSamples num_samples = int(data[index]) index += 1 # sampleSize sample_size = int(data[index]) index += 1 #===== create and populate result structure # TODO populate result = OAPAnalogStats() result.packet_timestamp = (secs, usecs) elif notif_type==NOTIFTYPE_DIG: #===== parse raise NotImplementedError("parsing digital OAP notification") #===== create and populate result structure raise NotImplementedError("filling digital OAP result structure") elif notif_type==NOTIFTYPE_LOCATEME: #===== parse raise NotImplementedError("parsing locateMe OAP notification") #===== create and populate result structure raise NotImplementedError("filling locateMe OAP result structure") elif notif_type == 4: # pkgen #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag != 0xFF: raise ValueError('invalid notification data: expected address tag') index += 2 + l # pid pid = struct.unpack_from('!l', data, index)[0] index += 4 # startPid startPid = struct.unpack_from('!l', data, index)[0] index += 4 # numPackets numPackets = struct.unpack_from('!l', data, index)[0] index += 4 # payload payload = data[index:] #===== create and populate result structure result = OAPpkGenPacket() result.pid = pid result.startPid = startPid result.numPackets = numPackets result.payload = payload # set common notification attributes if result: result.raw_data = data result.channel = channel result.received_timestamp = datetime.datetime.now() return result
def parse_oap_notif(data, index = 0): ''' Parse the OAP notification data and return a OAPNotif object. data: array of bytes from the OAP notification payload. OAP transport header and OAP command type should be stripped ''' # TODO: validate length result = None # notif type notif_type = data[index] index += 1 # payload if notif_type==NOTIFTYPE_SAMPLE: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag!=TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index) index += 12 # rate rate = struct.unpack_from('!l', data, index)[0] index += 4 # numSamples num_samples = int(data[index]) index += 1 # sampleSize sample_size = int(data[index]) index += 1 #===== create result structure if len(channel)==2 and channel[0]==DIGITAL_IN_ADDRESS: result = OAPDigitalInSample() elif channel == TEMP_ADDRESS: result = OAPTempSample() elif len(channel)==2 and channel[0]==ANALOG_ADDRESS: result = OAPAnalogSample() else: raise SystemError("unknown OAP sample with channel={0}").format(channel) #===== populate result structure if channel[0]==ANALOG_ADDRESS or channel[0]==DIGITAL_IN_ADDRESS: result.input = channel[1] result.packet_timestamp = (secs, usecs) result.rate = rate result.num_samples = num_samples result.sample_size = sample_size for i in range(num_samples): if sample_size==8: temp = struct.unpack('!B', data[index:index+1])[0] index += 1 elif sample_size==16: temp = struct.unpack('!h', data[index:index+2])[0] index += 2 else: raise SystemError("unexpected sample_size of {0}".format(sample_size)) result.samples.append(temp) elif notif_type==NOTIFTYPE_STATS: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag!=TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index) index += 12 # rate rate = struct.unpack_from('!l', data, index)[0] index += 4 # numSamples num_samples = int(data[index]) index += 1 # sampleSize sample_size = int(data[index]) index += 1 #===== create and populate result structure # TODO populate result = OAPAnalogStats() result.packet_timestamp = (secs, usecs) elif notif_type==NOTIFTYPE_DIG: #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag!=TAG_ADDRESS: raise ValueError('invalid notification data: expected address tag') index += 2 + l # timestamp (secs, usecs) = struct.unpack_from('!ql', data, index) index += 12 # new value new_val = int(data[index]) index += 1 #===== create and populate result structure result = OAPDigitalIn() result.channel = channel result.packet_timestamp = (secs, usecs) result.new_val = new_val elif notif_type==NOTIFTYPE_LOCATEME: #===== parse raise NotImplementedError("parsing locateMe OAP notification") #===== create and populate result structure raise NotImplementedError("filling locateMe OAP result structure") elif notif_type == 4: # pkgen #===== parse # channel (TLV) (tag, l, channel) = OAPMessage.parse_tlv(data[index:]) if tag != 0xFF: raise ValueError('invalid notification data: expected address tag') index += 2 + l # pid pid = struct.unpack_from('!l', data, index)[0] index += 4 # startPid startPid = struct.unpack_from('!l', data, index)[0] index += 4 # numPackets numPackets = struct.unpack_from('!l', data, index)[0] index += 4 # payload payload = data[index:] #===== create and populate result structure result = OAPpkGenPacket() result.pid = pid result.startPid = startPid result.numPackets = numPackets result.payload = payload # set common notification attributes if result: result.raw_data = data result.channel = channel result.received_timestamp = datetime.datetime.now() return result