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