def wait(self, commands, default_timeout = 2, not_occurences = True): # not_occurences - defines which result will be sent back:answer packet or command occurence counter if type(commands) is not list: #support for multiple commands to wait commands = [commands] # if cmd_byte == 0: cmd_byte = self._Protocol.CMD_BYTE timeout = default_timeout #Setting parameters that were passed with the function call if (self.SET_PARAMS != None): if (self.SET_PARAMS["ack"] != None): not_occurences = self.SET_PARAMS["ack"] self._EventReader._ack_needed = 0 elif not_occurences == False: # we want to count packet occurences only, # so disable acknowledge mechanism self.SET_PARAMS["ack"] = False if(self.SET_PARAMS["timeout"] != None): timeout = self.SET_PARAMS["timeout"] if (timeout <= 0): return False log.write("Waiting for command " + ', '.join([hexdump(x) for x in commands]) +". Timeout = " + str(timeout) + " seconds") start_time = time.time() index = 0 packet_occurences = 0 while(time.time() - start_time < timeout): while (index < self.pool_size()): #Checking message - optional, depend on protocol current_message = self.get_message(index) #Checking if correct command found if (ord(current_message[cmd_byte - 1]) in commands): log.write("Correct packet found: " + hexdump(current_message)) #_describe_command(current_message, self._Protocol.NAME, None) #Removing current packet from pool packet = self.pop_message(index) packet_occurences += 1 if (not_occurences): return packet #else: index += 1 #Waiting for next message time.sleep(0.1) if (not_occurences): log.write("Failed to receive command [" + ', '.join([hexdump(x) for x in commands]) + "]") return False else: log.write("Packet [" + ', '.join([hexdump(x) for x in commands]) + "] was received: " + str(packet_occurences) + " times") return packet_occurences
def get_network_data(self, integrity=False): supervision_period = 0x2D0 enrollment_sate = 1 data = prutils.num_to_chars(self._long_id, 4, 0) + self._dev_type_code + \ chr(self._short_id) + chr(enrollment_sate) # + prutils.num_to_chars(supervision_period, 2, 0) # data = prutils.str_l(data) log.write("network data = %s" % prutils.hexdump(data)) if integrity: return prutils.crc_PGH_20(data) return data
def dispatch_message(self, dpr_list): # 24 00 00 00 00 00 02 04 B5 02 0D 01 if not self._auto_response: return None # log.write('got [%d] dpr messages: %s' % (len(dpr_list), dpr_list), "console") for dpr_msg in dpr_list: # if len(dpr_msg) < 3 : return None seq = ord(dpr_msg[2]) # filter out wrong messages if seq !=0 and seq == self.prev_seq: log.write('Got repetition of command') # continue seq = self.prev_seq mcode = ord(dpr_msg[6]) tldv_len = ord(dpr_msg[7]) tldv = dpr_msg[7 : 8 + tldv_len] tldv_type = ord(tldv[1]) log.write("camera [%d:%d] got mcode[%d] type=%02X, tldv = %s, seq = %d" % \ (self._short_id, self._long_id, mcode, tldv_type, prutils.hexdump(tldv), seq)) # FIXME: snum !!!! if mcode == 2: # GET if tldv_type == 0xB5: # file info requested_film_id = ord(tldv[3]) requested_row = ord(tldv[4]) log.write("Requested file info:film id=0x%02X, row=0x%02X" % (requested_film_id, requested_row)) self.report_file_info(self.film_type, self.film_id, self.file_size, self.row, self.crc16, self.index, self.zone, 0, seq) dpr_list.remove(dpr_msg) elif mcode == 9: # file request - ONLY IF File info request was before requested_film_id = ord(tldv[1]) requested_row = ord(tldv[2]) log.write("Requested for FILE, film id=%d, row=%d" % (requested_film_id, requested_row)) requested_chunk_size = ord(tldv[3]) if self.film_id != requested_film_id: log.write("Wrong requested film id:%d" % requested_film_id) self.send_file_body(self.image_data, requested_row) self.row += 1 if self.row == self.files_amount: self.set_autoresponse_activity(False) self.row = 0 dpr_list.remove(dpr_msg) elif mcode == 7: # confirm if tldv_len == 1 and ord(tldv[1]) == 0x00: # OK # Confirmationion must be check on HPR sequence level log.write("Confirmation") self.message_confirmed = True dpr_list.remove(dpr_msg) # if message not processed run default dispatcher if(len(dpr_list)): Device.dispatch_message(self, dpr_list)
def run(self): while not (self._stop_needed): try: priority, index, packet, delay_after = heapq.heappop(self._out_pool) except IndexError: time.sleep(0.01) else: #with self._lock: log.write("in queue: %d, pr = %d" % (len(self._out_pool), priority), "debug") log.write ("{0} *[TX]*: {1}".format(self._port.port, hexdump(packet)), "debug") self._port.write(packet) if delay_after: time.sleep(delay_after) print 'Message Writer - End of RUN cycle'
def dispatch_message(self, dpr_list): if not self._auto_response: return None # log.write('Device::dispatch_message') # dpr_list : ['\xA4\x00\x8E\x00\x00\x00\x04\x03\xF1\x01\x02','\x04\x00\xFF\x00\x00\x00\x04\x05\x3E\x03\x00\x84\x03'] response_amount = 0 for dpr_msg in dpr_list: seq = ord(dpr_msg[2]) mcode = ord(dpr_msg[6]) tldv_len = ord(dpr_msg[7]) tldv = dpr_msg[7 : 8 + tldv_len] log.write("device [%d:%d] got mcode[%d] type=%02X, tldv = %s, seq = %d" % \ (self._short_id, self._long_id, mcode, ord(tldv[1]), prutils.hexdump(tldv), seq), "debug") if mcode == 0x04: # response_amount += 1 if tldv[1] == '\xF1': # log.write("set device state") self._state = prutils.chars_to_int(tldv[3:].strip()) self.send_response(0x06, seq) #, response_amount) # log.write("device [%d:%d]:state = %d, seq = %d" % (self._short_id, self._long_id, self._state, seq)) elif tldv[1] == '\x3E': # log.write("set notification period") response_amount -= 1 notification_period = ord(tldv[3]) #log.write('** hello from [%d:%d] **' % (self._short_id, self._long_id)) self.send_hello(seq, notification_period) #self._Procedures.hello(self._long_id, self._short_id, 0, notification_period) elif tldv[1] == '\xFC': # log.write("set update config") self.send_response(0x06, seq) #, response_amount) # response comes first !!! #response_amount -= 1 log.write('** update from [%d:%d] **' % (self._short_id, self._long_id)) self._Procedures.update(self._long_id, self._short_id) else: # other set commands self.send_response(0x06, seq) # , response_amount) # if response_amount > 0: elif mcode == 5: if tldv[1] == '\x40': # hello self.send_hello() # confirm to report self.send_response(0x07, seq)
def write(self, packet): #TODO: Check if it works and create thread if needed #self._Serial.send_packet(packet) self._Serial.write(packet) log.write ("{0} [TX]: {1}".format(self._port_name, hexdump(packet)), "debug")
def _convert_result(self, value): if (value): # Packet received return hexdump(value) # Returning 'None' or 'False' values return value
def get_application_data(self, fn_sniffer_wait, integrity=False): # use request to get configuration according to device type config_cache = {} cur_seq = 0xff snum = 0xff data = '' for cfg in self._config_types: device_config_template = self._get_config_type(cfg) if device_config_template is None: log.write("Don't know how to get the 0x%04X device configuration" % cfg) return None dpr_type = device_config_template[1] # log.write("ASK FOR dpr_type = %x" % dpr_type) if dpr_type in config_cache.keys(): packet = config_cache[dpr_type] else: packet = self._Procedures.request_configuration_data(self._short_id, self._long_id, dpr_type) # filter out retransmittions while snum == cur_seq: packet = fn_sniffer_wait(0x10) if packet == False: break packet = packet[5:] # dpr only dprp = DPR_Packet(packet) snum = dprp.snum.value if snum == 0xff: break # special case cur_seq = snum if packet : config_cache[dpr_type] = packet self.send_response(0x07, dprp.snum.value) if packet: dprp = DPR_Packet(packet) rcv_dpr_type = ord(packet[8]) # if dpr_type != rcv_dpr_type: # log.write("wrong TLDV:%d != %d" % (dpr_type, rcv_dpr_type)) value_len_off = dprp.data_len.offset + 2 value_len = ord(packet[value_len_off]) # add config to cash # log.write("len = %d " % value_len) add = prutils.num_to_chars(cfg, 2, 0) # add += prutils.num_to_chars(cfg, 2, 0) # BF [07 05 01 00 01 00 08 20] if len(device_config_template) > 2 : # extract the tldv parameter offset = device_config_template[2][0] size = device_config_template[2][1] tldv_data = dprp.data[offset + 2: offset + size + 2] # log.write("template:%s" % device_config_template) # log.write("dpr data = %s" % prutils.hexdump(dprp.data)) # log.write("0x%X:%d extraction: %s" % (offset, size, prutils.hexdump(tldv_data))) add += chr(len(tldv_data)) + tldv_data # log.write("TLDV:%s => %s" % (prutils.hexdump(packet[8:11]), prutils.hexdump(add))) else: add += chr(value_len) add += packet[value_len_off + 1: value_len_off + value_len + 1] data += add # data = chr(len(data)) + data time.sleep(0.3) else: return None log.write("application data = %s" % prutils.hexdump(data)) if integrity == True: if len(self._config_types) != 0: data = prutils.crc_PGH_20(data) else: data = 0x0000 return data