1#!/usr/bin/env python3 2# Copyright(c) 2022 Intel Corporation. All rights reserved. 3# SPDX-License-Identifier: Apache-2.0 4import os 5import sys 6import logging 7import time 8import argparse 9import socket 10import struct 11import hashlib 12from urllib.parse import urlparse 13 14RET = 0 15HOST = None 16PORT = 0 17PORT_LOG = 9999 18PORT_REQ = PORT_LOG + 1 19BUF_SIZE = 4096 20 21# Define the command and its 22# possible max size 23CMD_LOG_START = "start_log" 24CMD_DOWNLOAD = "download" 25MAX_CMD_SZ = 16 26 27# Define the header format and size for 28# transmiting the firmware 29PACKET_HEADER_FORMAT_FW = 'I 42s 32s' 30 31logging.basicConfig() 32log = logging.getLogger("cavs-client") 33log.setLevel(logging.INFO) 34 35class cavstool_client(): 36 def __init__(self, host, port, args): 37 self.host = host 38 self.port = port 39 self.args = args 40 self.sock = None 41 self.cmd = None 42 43 def send_cmd(self, cmd): 44 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: 45 self.sock = sock 46 self.cmd = cmd 47 self.sock.connect((self.host, self.port)) 48 self.sock.sendall(cmd.encode("utf-8")) 49 log.info(f"Sent: {cmd}") 50 ack = str(self.sock.recv(MAX_CMD_SZ), "utf-8") 51 log.info(f"Receive: {ack}") 52 53 if ack == CMD_LOG_START: 54 self.monitor_log() 55 elif ack == CMD_DOWNLOAD: 56 self.run() 57 else: 58 log.error(f"Receive incorrect msg:{ack} expect:{cmd}") 59 60 def uploading(self, filename): 61 # Send the FW to server 62 fname = os.path.basename(filename) 63 fsize = os.path.getsize(filename) 64 65 md5_tx = hashlib.md5(open(filename,'rb').read()).hexdigest() 66 67 # Pack the header and the expecting packed size is 78 bytes. 68 # The header by convention includes: 69 # size(4), filename(42), MD5(32) 70 values = (fsize, fname.encode('utf-8'), md5_tx.encode('utf-8')) 71 log.info(f'filename:{fname}, size:{fsize}, md5:{md5_tx}') 72 73 s = struct.Struct(PACKET_HEADER_FORMAT_FW) 74 header_data = s.pack(*values) 75 header_size = s.size 76 log.info(f'header size: {header_size}') 77 78 with open(filename,'rb') as f: 79 log.info(f'Sending...') 80 81 total = self.sock.send(header_data) 82 total += self.sock.sendfile(f) 83 84 log.info(f"Done Sending ({total}).") 85 86 rck = self.sock.recv(MAX_CMD_SZ).decode("utf-8") 87 log.info(f"RCK ({rck}).") 88 if not rck == "success": 89 global RET 90 RET = -1 91 log.error(f"Firmware uploading failed") 92 93 def run(self): 94 filename = str(self.args.fw_file) 95 self.uploading(filename) 96 97 def monitor_log(self): 98 log.info(f"Start to monitor log output...") 99 while True: 100 # Receive data from the server and print out 101 receive_log = str(self.sock.recv(BUF_SIZE), "utf-8").replace('\x00','') 102 if receive_log: 103 sys.stdout.write(f"{receive_log}") 104 sys.stdout.flush() 105 time.sleep(0.1) 106 107 def __del__(self): 108 self.sock.close() 109 110 111def main(): 112 if args.log_only: 113 log.info("Monitor process") 114 115 try: 116 client = cavstool_client(HOST, PORT, args) 117 client.send_cmd(CMD_LOG_START) 118 except KeyboardInterrupt: 119 pass 120 121 else: 122 log.info("Uploading process") 123 client = cavstool_client(HOST, PORT, args) 124 client.send_cmd(CMD_DOWNLOAD) 125 126ap = argparse.ArgumentParser(description="DSP loader/logger client tool", allow_abbrev=False) 127ap.add_argument("-q", "--quiet", action="store_true", 128 help="No loader output, just DSP logging") 129ap.add_argument("-l", "--log-only", action="store_true", 130 help="Don't load firmware, just show log output") 131ap.add_argument("-s", "--server-addr", default="localhost", 132 help="Specify the adsp server address") 133ap.add_argument("-p", "--log-port", type=int, 134 help="Specify the PORT that connected to log server") 135ap.add_argument("-r", "--req-port", type=int, 136 help="Specify the PORT that connected to request server") 137ap.add_argument("fw_file", nargs="?", help="Firmware file") 138args = ap.parse_args() 139 140if args.quiet: 141 log.setLevel(logging.WARN) 142 143if args.log_port: 144 PORT_LOG = args.log_port 145 146if args.req_port: 147 PORT_REQ = args.req_port 148 149if args.server_addr: 150 url = urlparse("//" + args.server_addr) 151 152 if url.hostname: 153 HOST = url.hostname 154 155 if url.port: 156 PORT = int(url.port) 157 else: 158 if args.log_only: 159 PORT = PORT_LOG 160 else: 161 PORT = PORT_REQ 162 163log.info(f"REMOTE HOST: {HOST} PORT: {PORT}") 164 165if __name__ == "__main__": 166 main() 167 168 sys.exit(RET) 169