Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / scripts / flashing / qpg6100_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 QPG6100 device.
16
17 Currently only support CMSIS mBed Drag and Drop for public use.
18
19 usage: qpg6100_firmware_utils.py [-h] [--application FILE] [--drive DRIVE]
20
21 optional arguments:
22   -h, --help            show this help message and exit
23
24 configuration:
25   --verbose             Report more verbosely
26   --drive               Connected mBed USB mount to copy to
27
28 operations:
29   --application FILE    Flash an image through drag and drop
30 """
31
32 import sys
33 import os
34 import shutil
35
36 import firmware_utils
37
38 # Additional options that can be use to configure an `Flasher`
39 # object (as dictionary keys) and/or passed as command line options.
40 QPG6100_OPTIONS = {
41     # Configuration options define properties used in flashing operations.
42     'configuration': {
43         # Tool configuration options.
44         'drive': {
45             'help': 'Location of the mBed mount',
46             'default': '/mnt/e',
47             'alias': ['-d'],
48             'argparse': {
49                 'metavar': 'FILE'
50             },
51         },
52     },
53 }
54
55
56 class Flasher(firmware_utils.Flasher):
57     """Manage flashing."""
58
59     def __init__(self, **options):
60         super().__init__(platform='QPG6100', module=__name__, **options)
61         self.define_options(QPG6100_OPTIONS)
62
63     def erase(self):
64         """Not supported"""
65         self.log(0, "Erase not supported")
66         return self
67
68     def verify(self, image):
69         """Not supported"""
70         self.log(0, "Verify not supported")
71         return self
72
73     def flash(self, image):
74         """Flash image."""
75         self.log(1, "Copying to {} drive {}".format(image, self.option.drive or "None"))
76         if not self.option.drive:
77             self.log(0, "--drive or -d required for copy action")
78             self.err = 1
79             return self
80
81         # Check for drive mount
82         if not os.path.exists(self.option.drive):
83             self.log(0, "Drive '{}' does not exist. Is the USB device mounted correctly ?".format(self.option.drive))
84             self.err = 2
85             return self
86
87         # Check for valid mBed device
88         mbed_marker = os.path.join(self.option.drive, 'MBED.HTM')
89         if not os.path.exists(mbed_marker):
90             self.log(0, "Drive '{}' not a path to an MBED device".format(self.option.drive))
91             self.err = 3
92             return self
93
94         shutil.copyfile(image, os.path.join(self.option.drive, os.path.basename(image)))
95         return self
96
97     def reset(self):
98         """Not supported"""
99         self.log(0, "Reset is triggered automatically after completion of mBed copy.")
100         return self
101
102     def actions(self):
103         """Perform actions on the device according to self.option."""
104         self.log(3, 'Options:', self.option)
105
106         if self.option.erase:
107             if self.erase().err:
108                 return self
109
110         application = self.optional_file(self.option.application)
111         if application:
112             if self.flash(application).err:
113                 return self
114             if self.option.verify_application:
115                 if self.verify(application).err:
116                     return self
117             if self.option.reset is None:
118                 self.option.reset = True
119
120         if self.option.reset:
121             if self.reset().err:
122                 return self
123
124         return self
125
126
127 if __name__ == '__main__':
128     sys.exit(Flasher().flash_command(sys.argv))