Skip to content

Commit b6f0ee2

Browse files
author
Cruz Monrreal
authored
Merge pull request #7844 from theotherjimmy/fw-update-integration
Implement device management subcommand
2 parents 06a98e7 + ebdad75 commit b6f0ee2

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ pyelftools>=0.24
1515
jsonschema>=2.6
1616
future>=0.16.0
1717
six>=1.11.0
18+
git+https://github.com/armmbed/[email protected]
19+
mbed-cloud-sdk==2.0.0

tools/device_management.py

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#! /usr/bin/env python2
2+
"""
3+
mbed SDK
4+
Copyright (c) 2011-2013 ARM Limited
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
19+
device-management, dev-mgmt, and dm sub command
20+
"""
21+
from __future__ import print_function, absolute_import
22+
import logging
23+
import sys
24+
import argparse
25+
from os.path import join, abspath, dirname, basename
26+
from os import getenv
27+
28+
from manifesttool import create, parse, verify, cert, init, update
29+
from manifesttool.argparser import MainArgumentParser
30+
from mbed_cloud import AccountManagementAPI, CertificatesAPI
31+
import colorama
32+
colorama.init()
33+
34+
35+
LOG = logging.getLogger(__name__)
36+
LOG_FORMAT = '[%(levelname)s] %(asctime)s - %(name)s - %(message)s'
37+
38+
# Be sure that the tools directory is in the search path
39+
ROOT = abspath(join(dirname(__file__), ".."))
40+
sys.path.insert(0, ROOT)
41+
42+
from tools.config import Config
43+
from tools.options import extract_mcus
44+
45+
46+
class MbedExtendedArgs(MainArgumentParser):
47+
def _addCreateArgs(self, parser, exclusions=[]):
48+
if 'payload' not in exclusions:
49+
parser.add_argument(
50+
'-p', '--payload',
51+
help='Supply a local copy of the payload file.'
52+
'This option overrides any payload file supplied in a '
53+
'`-i` argument.',
54+
metavar='FILE',
55+
type=argparse.FileType('rb')
56+
)
57+
parser.add_argument('-m', '--mcu')
58+
parser.add_argument('-t', '--toolchain')
59+
parser.add_argument('--source', nargs='+', dest='source_dir')
60+
parser.add_argument('--build')
61+
exclusions.append('payload')
62+
super(MbedExtendedArgs, self)._addCreateArgs(parser, exclusions)
63+
64+
65+
def wrap_payload(func):
66+
def inner(options):
67+
if not options.payload and options.mcu and options.build:
68+
mcus = extract_mcus(MbedExtendedArgs(), options)
69+
sources = options.source_dir or ['.']
70+
config = Config(mcus[0], sources)
71+
app_name = config.name or basename(abspath(sources[0]))
72+
output_ext = getattr(config.target, "OUTPUT_EXT", "bin")
73+
payload_name = join(options.build, "{}_application.{}".format(
74+
app_name, output_ext
75+
))
76+
options.payload = open(payload_name, "rb")
77+
return func(options)
78+
return inner
79+
80+
81+
def wrap_init(func):
82+
def inner(options):
83+
accounts = AccountManagementAPI()
84+
certs = CertificatesAPI()
85+
api_key = accounts.list_api_keys(filter={
86+
'key': getenv("MBED_CLOUD_SDK_API_KEY")
87+
}).next()
88+
user = accounts.get_user(api_key.owner_id)
89+
certificates_owned = list(certs.list_certificates())
90+
dev_cert_info = None
91+
for certif in certificates_owned:
92+
if certif.type == "developer" and (certif.owner_id == user.id or
93+
certif.owner_id == api_key.id):
94+
dev_cert_info = certs.get_certificate(certif.id)
95+
LOG.info("Found developer certificate onwed by %s named %s",
96+
user.full_name, dev_cert_info.name)
97+
break
98+
else:
99+
LOG.warning(
100+
"Could not find developer certificate for this account."
101+
" Generting a new developer certificate."
102+
)
103+
dev_cert_info = CertificatesAPI().add_developer_certificate(
104+
"mbed-cli-auto {}".format(user.full_name),
105+
description="cetificate auto-generated by Mbed CLI"
106+
)
107+
LOG.info("Writing developer certificate %s into c file "
108+
"mbed_cloud_dev_credentials.c", dev_cert_info.name)
109+
with open("mbed_cloud_dev_credentials.c", "w") as fout:
110+
fout.write(dev_cert_info.header_file)
111+
return func(options)
112+
return inner
113+
114+
115+
def main():
116+
options = MbedExtendedArgs().parse_args().options
117+
118+
log_level = {
119+
'debug': logging.DEBUG,
120+
'info': logging.INFO,
121+
'warning': logging.WARNING,
122+
'exception': logging.CRITICAL,
123+
}[options.log_level]
124+
logging.basicConfig(
125+
level=log_level,
126+
format=LOG_FORMAT,
127+
datefmt='%Y-%m-%d %H:%M:%S',
128+
)
129+
logging.addLevelName(
130+
logging.INFO,
131+
"\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO)
132+
)
133+
logging.addLevelName(
134+
logging.WARNING,
135+
"\033[1;93m%s\033[1;0m" % logging.getLevelName(logging.WARNING)
136+
)
137+
logging.addLevelName(
138+
logging.CRITICAL,
139+
"\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL)
140+
)
141+
LOG.debug('CLIDriver created. Arguments parsed and logging setup.')
142+
143+
rc = {
144+
"create": wrap_payload(create.main),
145+
"parse": parse.main,
146+
"verify": verify.main,
147+
"cert": cert.main,
148+
"init": wrap_init(init.main),
149+
"update": wrap_payload(update.main),
150+
}[options.action](options) or 0
151+
152+
sys.exit(rc)
153+
154+
if __name__ == "__main__":
155+
main()

0 commit comments

Comments
 (0)