Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / scripts / flashing / k32w_firmware_utils.py
1 #!/usr/bin/env python3
2 # Copyright (c) 2020 Project CHIP Authors
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Flash an K32W device.
16
17 This is layered so that a caller can perform individual operations
18 through an `Flasher` instance, or operations according to a command line.
19 For `Flasher`, see the class documentation. For the parse_command()
20 interface or standalone execution:
21
22 usage: k32w_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE]
23                                [--verify_application] [--reset] [--skip_reset]
24                                [--commander FILE] [--serialno SERIAL]
25
26 Flash K32W device
27
28 optional arguments:
29   -h, --help            show this help message and exit
30
31 configuration:
32   --verbose, -v         Report more verbosely
33   --commander FILE      File name of the commander executable
34   --serialno SERIAL, -s SERIAL
35                         Serial number of device to flash
36
37 operations:
38   --erase               Erase device
39   --application FILE    Flash an image
40   --verify_application, --verify-application
41                         Verify the image after flashing
42   --reset               Reset device after flashing
43   --skip_reset, --skip-reset
44                         Do not reset device after flashing
45 """
46
47 import sys
48
49 import firmware_utils
50
51 # Additional options that can be use to configure an `Flasher`
52 # object (as dictionary keys) and/or passed as command line options.
53 K32W_OPTIONS = {
54     # Configuration options define properties used in flashing operations.
55     'configuration': {
56         # Tool configuration options.
57         'commander': {
58             'help': 'File name of the commander executable',
59             'default': 'commander',
60             'argparse': {
61                 'metavar': 'FILE'
62             },
63             'verify': ['{commander}', '--version'],
64             'error':
65                 """\
66                 Unable to execute {commander}.
67
68                 Please ensure that this tool is installed and
69                 available. See the K32W example README for
70                 installation instructions.
71
72                 """,
73         },
74         'serialno': {
75             'help': 'Serial number of device to flash',
76             'default': None,
77             'alias': ['-s'],
78             'argparse': {
79                 'metavar': 'SERIAL'
80             },
81         },
82     },
83 }
84
85
86 class Flasher(firmware_utils.Flasher):
87     """Manage k32w flashing."""
88
89     def __init__(self, **options):
90         super().__init__(platform='K32W', module=__name__, **options)
91         self.define_options(K32W_OPTIONS)
92
93     # Common command line arguments for commander device subcommands.
94     DEVICE_ARGUMENTS = [{'optional': 'serialno'}]
95
96     def erase(self):
97         """Perform `commander device masserase`."""
98         return self.run_tool(
99             'commander', ['device', 'masserase', self.DEVICE_ARGUMENTS],
100             name='Erase device')
101
102     def verify(self, image):
103         """Verify image."""
104         return self.run_tool(
105             'commander',
106             ['verify', self.DEVICE_ARGUMENTS, image],
107             name='Verify',
108             pass_message='Verified',
109             fail_message='Not verified',
110             fail_level=2)
111
112     def flash(self, image):
113         """Flash image."""
114         return self.run_tool(
115             'commander',
116             ['flash', self.DEVICE_ARGUMENTS, image],
117             name='Flash')
118
119     def reset(self):
120         """Reset the device."""
121         return self.run_tool(
122                 'commander',
123                 ['device', 'reset', self.DEVICE_ARGUMENTS],
124                 name='Reset')
125
126     def actions(self):
127         """Perform actions on the device according to self.option."""
128         self.log(3, 'Options:', self.option)
129
130         if self.option.erase:
131             if self.erase().err:
132                 return self
133
134         application = self.optional_file(self.option.application)
135         if application:
136             if self.flash(application).err:
137                 return self
138             if self.option.verify_application:
139                 if self.verify(application).err:
140                     return self
141             if self.option.reset is None:
142                 self.option.reset = True
143
144         if self.option.reset:
145             if self.reset().err:
146                 return self
147
148         return self
149
150
151 if __name__ == '__main__':
152     sys.exit(Flasher().flash_command(sys.argv))