def create_rf_command_datagram(dest_callsign, dest_device_id, command, payload): """ This function creates a command packet datagram for commanding REMOTE (RF) Faraday devices. This datagram encapsulates the actual command packets to the passed to the remote device as a payload. The important difference between the RF and non RF command packet functions are remote callsign and ID arguments that tell Faraday what devices to target for commanding. .. note:: Remote commanding simply calls a specific local command that transmits a "local" command packet as a payload to the target Faraday device. Also, This command module has a predefined set of command number definitions for use/reference. :param dest_callsign: The callsign of the target Faraday unit :param dest_device_id: The device ID number of the target Faraday unit :param command: The command "number" that identifies the command packet to be parsed or direct action to be taken :param payload: The payload for the specified command packet type. This is usually just commands full "packet" structure :return: A complete command datagram packet as a string of raw bytes """ #Cheack if callsign is too long if len(dest_callsign) < DEST_CALLSIGN_MAX_LEN: #Define packet structures pkt_cmd_datagram_struct = struct.Struct( '2B25s' ) #Command packect to be run on remote unit as local command pkt_cmd_datagram_error_detection_struct = struct.Struct( '>1H' ) #Command packect to be run on remote unit as local command Error Detection pkt_rf_cmd_struct = struct.Struct( '9s2B29s' ) #RF Command packet that encapsulates local command to be run on remote unit pkt_rf_error_detection_struct = struct.Struct( '>1H') #16bit Error Detection (Checksum) #Create local command for remote unit pkt_cmd_datagram = pkt_cmd_datagram_struct.pack( command, len(payload), payload) pkt_cmd_datagram_error_detection = pkt_cmd_datagram_error_detection_struct.pack( checksum.compute_checksum_16(pkt_cmd_datagram, len(pkt_cmd_datagram))) pkt_cmd_datagram_final = pkt_cmd_datagram + pkt_cmd_datagram_error_detection print repr(pkt_cmd_datagram_final) #Create RF Command for local device without Error Detection appended. NOTE Callsign must be in uppercase! pkt_rf_cmd = pkt_rf_cmd_struct.pack( str(dest_callsign).upper(), len(dest_callsign), dest_device_id, pkt_cmd_datagram_final) #Create final local command packet with Error Detection appeneded pkt_rf_error_detection = pkt_rf_error_detection_struct.pack( checksum.compute_checksum_16(pkt_rf_cmd, len(pkt_rf_cmd))) packet = pkt_rf_cmd + pkt_rf_error_detection return packet else: print "Error: Callsign too long!"
def create_command_datagram(command, payload): """ This function creates a command packet datagram that encapsulates the actual command packets to the parsed. This allows identification of packet "types" and modularity within the command system. :param command: The command "number" that identifies the command packet to be parsed or direct action to be taken :param payload: The payload for the specified command packet type. This is usually just commands full "packet" structure :return: A complete command datagram packet as a string of raw bytes .. note:: This command module has a predefined set of command number definitions for use/reference. """ #Define packets pkt_struct1 = struct.Struct('2B') pkt_struct2 = struct.Struct('119s') pkt_struct3 = struct.Struct('>1H') #Determine payload length payload_len = len(payload) #Create sub packets pkt_a = pkt_struct1.pack(command, payload_len) pkt_b = pkt_struct2.pack(payload) checksum_computed = checksum.compute_checksum_16(pkt_a + pkt_b, len(pkt_a + pkt_b)) pkt_c = pkt_struct3.pack(checksum_computed) #Concactenate sub packets into a single packet packet_packed = pkt_a + pkt_b + pkt_c #Only return packet if all variables are correctly sized if len(payload) <= FIXED_PAYLOAD_LEN_MAX: #print "Lengths", len(packet_packed) packet_final = packet_packed return packet_final else: return False print "ERROR - Create Command: Payload Too Long!", len(payload)