|
| 1 | +#! /usr/bin/env -S python3 -B |
| 2 | + |
| 3 | +import io |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import time |
| 7 | +from subprocess import PIPE |
| 8 | + |
| 9 | +import click |
| 10 | +from chiptest.accessories import AppsRegister |
| 11 | +from chiptest.runner import Runner |
| 12 | +from chiptest.test_definition import App, ExecutionCapture |
| 13 | +from yaml.paths_finder import PathsFinder |
| 14 | + |
| 15 | +TEST_NODE_ID = '0x12344321' |
| 16 | +TEST_VID = '0xFFF1' |
| 17 | +TEST_PID = '0x8001' |
| 18 | + |
| 19 | + |
| 20 | +class DarwinToolRunner: |
| 21 | + def __init__(self, runner, command): |
| 22 | + self.process = None |
| 23 | + self.outpipe = None |
| 24 | + self.runner = runner |
| 25 | + self.lastLogIndex = 0 |
| 26 | + self.command = command |
| 27 | + self.stdin = None |
| 28 | + |
| 29 | + def start(self): |
| 30 | + self.process, self.outpipe, errpipe = self.runner.RunSubprocess(self.command, |
| 31 | + name='DARWIN-TOOL', |
| 32 | + wait=False, |
| 33 | + stdin=PIPE) |
| 34 | + self.stdin = io.TextIOWrapper(self.process.stdin, line_buffering=True) |
| 35 | + |
| 36 | + def stop(self): |
| 37 | + if self.process: |
| 38 | + self.process.kill() |
| 39 | + |
| 40 | + def waitForMessage(self, message): |
| 41 | + logging.debug('Waiting for %s' % message) |
| 42 | + |
| 43 | + start_time = time.monotonic() |
| 44 | + ready, self.lastLogIndex = self.outpipe.CapturedLogContains( |
| 45 | + message, self.lastLogIndex) |
| 46 | + while not ready: |
| 47 | + if self.process.poll() is not None: |
| 48 | + died_str = ('Process died while waiting for %s, returncode %d' % |
| 49 | + (message, self.process.returncode)) |
| 50 | + logging.error(died_str) |
| 51 | + raise Exception(died_str) |
| 52 | + if time.monotonic() - start_time > 10: |
| 53 | + raise Exception('Timeout while waiting for %s' % message) |
| 54 | + time.sleep(0.1) |
| 55 | + ready, self.lastLogIndex = self.outpipe.CapturedLogContains( |
| 56 | + message, self.lastLogIndex) |
| 57 | + |
| 58 | + logging.debug('Success waiting for: %s' % message) |
| 59 | + |
| 60 | + |
| 61 | +class InteractiveDarwinTool(DarwinToolRunner): |
| 62 | + def __init__(self, runner, binary_path): |
| 63 | + self.prompt = "WAITING FOR COMMANDS NOW" |
| 64 | + super().__init__(runner, [binary_path, "interactive", "start", "--additional-prompt", self.prompt]) |
| 65 | + |
| 66 | + def waitForPrompt(self): |
| 67 | + self.waitForMessage(self.prompt) |
| 68 | + |
| 69 | + def sendCommand(self, command): |
| 70 | + logging.debug('Sending command %s' % command) |
| 71 | + print(command, file=self.stdin) |
| 72 | + self.waitForPrompt() |
| 73 | + |
| 74 | + |
| 75 | +@click.group(chain=True) |
| 76 | +@click.pass_context |
| 77 | +def main(context): |
| 78 | + pass |
| 79 | + |
| 80 | + |
| 81 | +@main.command( |
| 82 | + 'run', help='Execute the test') |
| 83 | +@click.option( |
| 84 | + '--darwin-framework-tool', |
| 85 | + help="what darwin-framework-tool to use") |
| 86 | +@click.option( |
| 87 | + '--ota-requestor-app', |
| 88 | + help='what ota requestor app to use') |
| 89 | +@click.option( |
| 90 | + '--ota-data-file', |
| 91 | + required=True, |
| 92 | + help='The file to use to store our OTA data. This file does not need to exist.') |
| 93 | +@click.option( |
| 94 | + '--ota-image-file', |
| 95 | + required=True, |
| 96 | + help='The file to use to store the OTA image we plan to send. This file does not need to exist.') |
| 97 | +@click.option( |
| 98 | + '--ota-destination-file', |
| 99 | + required=True, |
| 100 | + help='The destination file to use for the requestor\'s download. This file does not need to exist.') |
| 101 | +@click.option( |
| 102 | + '--ota-candidate-file', |
| 103 | + required=True, |
| 104 | + help='The file to use for our OTA candidate JSON. This file does not need to exist.') |
| 105 | +@click.pass_context |
| 106 | +def cmd_run(context, darwin_framework_tool, ota_requestor_app, ota_data_file, ota_image_file, ota_destination_file, ota_candidate_file): |
| 107 | + paths_finder = PathsFinder() |
| 108 | + |
| 109 | + if darwin_framework_tool is None: |
| 110 | + darwin_framework_tool = paths_finder.get('darwin-framework-tool') |
| 111 | + if ota_requestor_app is None: |
| 112 | + ota_requestor_app = paths_finder.get('chip-ota-requestor-app') |
| 113 | + |
| 114 | + runner = Runner() |
| 115 | + runner.capture_delegate = ExecutionCapture() |
| 116 | + |
| 117 | + apps_register = AppsRegister() |
| 118 | + apps_register.init() |
| 119 | + |
| 120 | + darwin_tool = None |
| 121 | + |
| 122 | + try: |
| 123 | + apps_register.createOtaImage(ota_image_file, ota_data_file, "This is some test OTA data", vid=TEST_VID, pid=TEST_PID) |
| 124 | + json_data = { |
| 125 | + "deviceSoftwareVersionModel": [{ |
| 126 | + "vendorId": int(TEST_VID, 16), |
| 127 | + "productId": int(TEST_PID, 16), |
| 128 | + "softwareVersion": 2, |
| 129 | + "softwareVersionString": "2.0", |
| 130 | + "cDVersionNumber": 18, |
| 131 | + "softwareVersionValid": True, |
| 132 | + "minApplicableSoftwareVersion": 0, |
| 133 | + "maxApplicableSoftwareVersion": 100, |
| 134 | + "otaURL": ota_image_file |
| 135 | + }] |
| 136 | + } |
| 137 | + with open(ota_candidate_file, "w") as f: |
| 138 | + json.dump(json_data, f) |
| 139 | + |
| 140 | + requestor_app = App(runner, [ota_requestor_app, '--otaDownloadPath', ota_destination_file]) |
| 141 | + apps_register.add('default', requestor_app) |
| 142 | + |
| 143 | + requestor_app.start() |
| 144 | + |
| 145 | + pairing_cmd = [darwin_framework_tool, 'pairing', 'code', TEST_NODE_ID, requestor_app.setupCode] |
| 146 | + runner.RunSubprocess(pairing_cmd, name='PAIR', dependencies=[apps_register]) |
| 147 | + |
| 148 | + # pairing get-commissioner-node-id does not seem to work right in interactive mode for some reason |
| 149 | + darwin_tool = DarwinToolRunner(runner, [darwin_framework_tool, 'pairing', 'get-commissioner-node-id']) |
| 150 | + darwin_tool.start() |
| 151 | + darwin_tool.waitForMessage(": Commissioner Node Id") |
| 152 | + nodeIdLine = darwin_tool.outpipe.FindLastMatchingLine('.*: Commissioner Node Id (0x[0-9A-F]+)') |
| 153 | + if not nodeIdLine: |
| 154 | + raise Exception("Unable to find commissioner node id") |
| 155 | + commissionerNodeId = nodeIdLine.group(1) |
| 156 | + darwin_tool.stop() |
| 157 | + |
| 158 | + darwin_tool = InteractiveDarwinTool(runner, darwin_framework_tool) |
| 159 | + darwin_tool.start() |
| 160 | + |
| 161 | + darwin_tool.waitForPrompt() |
| 162 | + |
| 163 | + darwin_tool.sendCommand("otasoftwareupdateapp candidate-file-path %s" % ota_candidate_file) |
| 164 | + darwin_tool.sendCommand("otasoftwareupdateapp set-reply-params --status 0") |
| 165 | + darwin_tool.sendCommand("otasoftwareupdaterequestor announce-otaprovider %s 0 0 0 %s 0" % |
| 166 | + (commissionerNodeId, TEST_NODE_ID)) |
| 167 | + |
| 168 | + # Now wait for the OTA download to finish. |
| 169 | + requestor_app.waitForMessage("OTA image downloaded to %s" % ota_destination_file) |
| 170 | + |
| 171 | + # Make sure the right thing was downloaded. |
| 172 | + apps_register.compareFiles(ota_data_file, ota_destination_file) |
| 173 | + |
| 174 | + except Exception: |
| 175 | + logging.error("!!!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!!!!!") |
| 176 | + runner.capture_delegate.LogContents() |
| 177 | + raise |
| 178 | + finally: |
| 179 | + if darwin_tool is not None: |
| 180 | + darwin_tool.stop() |
| 181 | + apps_register.killAll() |
| 182 | + apps_register.factoryResetAll() |
| 183 | + apps_register.removeAll() |
| 184 | + apps_register.uninit() |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == '__main__': |
| 188 | + main(auto_envvar_prefix='CHIP') |
0 commit comments