1""" 2Utility script to migrate Zephyr-based projects to new MCUmgr Kconfig options 3 4Usage:: 5 6 python $ZEPHYR_BASE/scripts/utils/migrate_mcumgr_kconfigs.py -r root_path 7 8The utility will process c, cpp, h, hpp, rst, conf, CMakeLists.txt, 9yml, yaml and Kconfig files. 10 11 12Copyright (c) 2022 Nordic Semiconductor ASA 13SPDX-License-Identifier: Apache-2.0 14""" 15 16import argparse 17from pathlib import Path 18import re 19import sys 20 21 22ZEPHYR_BASE = Path(__file__).parents[2] 23 24FILE_PATTERNS = ( 25 r".+\.c", r".+\.cpp", r".+\.hpp", r".+\.h", r".+\.rst", r".+\.conf", 26 r".+\.yml", r".+\.yaml", r"CMakeLists.txt", r"Kconfig(\..+)?" 27) 28 29REPLACEMENTS = { 30 "MCUMGR_SMP_WORKQUEUE_STACK_SIZE" : "MCUMGR_TRANSPORT_WORKQUEUE_STACK_SIZE", 31 "MCUMGR_SMP_WORKQUEUE_THREAD_PRIO" : "MCUMGR_TRANSPORT_WORKQUEUE_THREAD_PRIO", 32 "MGMT_MAX_MAIN_MAP_ENTRIES" : "MCUMGR_SMP_CBOR_MAX_MAIN_MAP_ENTRIES", 33 "MGMT_MIN_DECODING_LEVELS" : "MCUMGR_SMP_CBOR_MIN_DECODING_LEVELS", 34 "MGMT_MIN_DECODING_LEVEL_1" : "MCUMGR_SMP_CBOR_MIN_DECODING_LEVEL_1", 35 "MGMT_MIN_DECODING_LEVEL_2" : "MCUMGR_SMP_CBOR_MIN_DECODING_LEVEL_2", 36 "MGMT_MIN_DECODING_LEVEL_3" : "MCUMGR_SMP_CBOR_MIN_DECODING_LEVEL_3", 37 "MGMT_MIN_DECODING_LEVEL_4" : "MCUMGR_SMP_CBOR_MIN_DECODING_LEVEL_4", 38 "MGMT_MIN_DECODING_LEVEL_5" : "MCUMGR_SMP_CBOR_MIN_DECODING_LEVEL_5", 39 "MGMT_MAX_DECODING_LEVELS" : "MCUMGR_SMP_CBOR_MAX_DECODING_LEVELS", 40 "MCUMGR_CMD_FS_MGMT" : "MCUMGR_GRP_FS", 41 "FS_MGMT_MAX_FILE_SIZE_64KB" : "MCUMGR_GRP_FS_MAX_FILE_SIZE_64KB", 42 "FS_MGMT_MAX_FILE_SIZE_4GB" : "MCUMGR_GRP_FS_MAX_FILE_SIZE_4GB", 43 "FS_MGMT_MAX_OFFSET_LEN" : "MCUMGR_GRP_FS_MAX_OFFSET_LEN", 44 "FS_MGMT_DL_CHUNK_SIZE_LIMIT" : "MCUMGR_GRP_FS_DL_CHUNK_SIZE_LIMIT", 45 "FS_MGMT_DL_CHUNK_SIZE" : "MCUMGR_GRP_FS_DL_CHUNK_SIZE", 46 "FS_MGMT_FILE_STATUS" : "MCUMGR_GRP_FS_FILE_STATUS", 47 "FS_MGMT_CHECKSUM_HASH" : "MCUMGR_GRP_FS_CHECKSUM_HASH", 48 "FS_MGMT_CHECKSUM_HASH_CHUNK_SIZE" : "MCUMGR_GRP_FS_CHECKSUM_HASH_CHUNK_SIZE", 49 "FS_MGMT_CHECKSUM_IEEE_CRC32" : "MCUMGR_GRP_FS_CHECKSUM_IEEE_CRC32", 50 "FS_MGMT_HASH_SHA256" : "MCUMGR_GRP_FS_HASH_SHA256", 51 "FS_MGMT_FILE_ACCESS_HOOK" : "MCUMGR_GRP_FS_FILE_ACCESS_HOOK", 52 "FS_MGMT_PATH_SIZE" : "MCUMGR_GRP_FS_PATH_LEN", 53 "MCUMGR_CMD_IMG_MGMT" : "MCUMGR_GRP_IMG", 54 "IMG_MGMT_USE_HEAP_FOR_FLASH_IMG_CONTEXT" : "MCUMGR_GRP_IMG_USE_HEAP_FOR_FLASH_IMG_CONTEXT", 55 "IMG_MGMT_UPDATABLE_IMAGE_NUMBER" : "MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER", 56 "IMG_MGMT_VERBOSE_ERR" : "MCUMGR_GRP_IMG_VERBOSE_ERR", 57 "IMG_MGMT_DIRECT_IMAGE_UPLOAD" : "MCUMGR_GRP_IMG_DIRECT_UPLOAD", 58 "IMG_MGMT_REJECT_DIRECT_XIP_MISMATCHED_SLOT" : "MCUMGR_GRP_IMG_REJECT_DIRECT_XIP_MISMATCHED_SLOT", 59 "IMG_MGMT_FRUGAL_LIST" : "MCUMGR_GRP_IMG_FRUGAL_LIST", 60 "MCUMGR_CMD_OS_MGMT" : "MCUMGR_GRP_OS", 61 "MCUMGR_GRP_OS_OS_RESET_HOOK" : "MCUMGR_GRP_OS_RESET_HOOK", 62 "OS_MGMT_RESET_MS" : "MCUMGR_GRP_OS_RESET_MS", 63 "OS_MGMT_TASKSTAT" : "MCUMGR_GRP_OS_TASKSTAT", 64 "OS_MGMT_TASKSTAT_ONLY_SUPPORTED_STATS" : "MCUMGR_GRP_OS_TASKSTAT_ONLY_SUPPORTED_STATS", 65 "OS_MGMT_TASKSTAT_MAX_NUM_THREADS" : "MCUMGR_GRP_OS_TASKSTAT_MAX_NUM_THREADS", 66 "OS_MGMT_TASKSTAT_THREAD_NAME_LEN" : "MCUMGR_GRP_OS_TASKSTAT_THREAD_NAME_LEN", 67 "OS_MGMT_TASKSTAT_SIGNED_PRIORITY" : "MCUMGR_GRP_OS_TASKSTAT_SIGNED_PRIORITY", 68 "OS_MGMT_TASKSTAT_STACK_INFO" : "MCUMGR_GRP_OS_TASKSTAT_STACK_INFO", 69 "OS_MGMT_ECHO" : "MCUMGR_GRP_OS_ECHO", 70 "OS_MGMT_MCUMGR_PARAMS" : "MCUMGR_GRP_OS_MCUMGR_PARAMS", 71 "MCUMGR_CMD_SHELL_MGMT" : "MCUMGR_GRP_SHELL", 72 "MCUMGR_CMD_SHELL_MGMT_LEGACY_RC_RETURN_CODE" : "MCUMGR_GRP_SHELL_LEGACY_RC_RETURN_CODE", 73 "MCUMGR_CMD_STAT_MGMT" : "MCUMGR_GRP_STAT", 74 "STAT_MGMT_MAX_NAME_LEN" : "MCUMGR_GRP_STAT_MAX_NAME_LEN", 75 "MCUMGR_GRP_ZEPHYR_BASIC" : "MCUMGR_GRP_ZBASIC", 76 "MCUMGR_GRP_BASIC_CMD_STORAGE_ERASE" : "MCUMGR_GRP_ZBASIC_STORAGE_ERASE", 77 "MGMT_VERBOSE_ERR_RESPONSE" : "MCUMGR_SMP_VERBOSE_ERR_RESPONSE", 78 "MCUMGR_SMP_REASSEMBLY_BT" : "MCUMGR_TRANSPORT_BT_REASSEMBLY", 79 "MCUMGR_SMP_REASSEMBLY" : "MCUMGR_TRANSPORT_REASSEMBLY", 80 "MCUMGR_SMP_REASSEMBLY_UNIT_TESTS" : "MCUMGR_TRANSPORT_REASSEMBLY_UNIT_TESTS", 81 "MCUMGR_BUF_COUNT" : "MCUMGR_TRANSPORT_NETBUF_COUNT", 82 "MCUMGR_BUF_SIZE" : "MCUMGR_TRANSPORT_NETBUF_SIZE", 83 "MCUMGR_BUF_USER_DATA_SIZE" : "MCUMGR_TRANSPORT_NETBUF_USER_DATA_SIZE", 84 "MCUMGR_SMP_BT" : "MCUMGR_TRANSPORT_BT", 85 "MCUMGR_SMP_BT_AUTHEN" : "MCUMGR_TRANSPORT_BT_AUTHEN", 86 "MCUMGR_SMP_BT_CONN_PARAM_CONTROL" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL", 87 "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_MIN_INT" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_MIN_INT", 88 "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_MAX_INT" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_MAX_INT", 89 "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_LATENCY" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_LATENCY", 90 "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_TIMEOUT" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_TIMEOUT", 91 "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_RESTORE_TIME" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_RESTORE_TIME", 92 "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_RETRY_TIME" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_RETRY_TIME", 93 "MCUMGR_SMP_DUMMY" : "MCUMGR_TRANSPORT_DUMMY", 94 "MCUMGR_SMP_DUMMY_RX_BUF_SIZE" : "MCUMGR_TRANSPORT_DUMMY_RX_BUF_SIZE", 95 "MCUMGR_SMP_SHELL" : "MCUMGR_TRANSPORT_SHELL", 96 "MCUMGR_SMP_SHELL_MTU" : "MCUMGR_TRANSPORT_SHELL_MTU", 97 "MCUMGR_SMP_SHELL_RX_BUF_COUNT" : "MCUMGR_TRANSPORT_SHELL_RX_BUF_COUNT", 98 "MCUMGR_SMP_UART" : "MCUMGR_TRANSPORT_UART", 99 "MCUMGR_SMP_UART_ASYNC" : "MCUMGR_TRANSPORT_UART_ASYNC", 100 "MCUMGR_SMP_UART_ASYNC_BUFS" : "MCUMGR_TRANSPORT_UART_ASYNC_BUFS", 101 "MCUMGR_SMP_UART_ASYNC_BUF_SIZE" : "MCUMGR_TRANSPORT_UART_ASYNC_BUF_SIZE", 102 "MCUMGR_SMP_UART_MTU" : "MCUMGR_TRANSPORT_UART_MTU", 103 "MCUMGR_SMP_UDP" : "MCUMGR_TRANSPORT_UDP", 104 "MCUMGR_SMP_UDP_IPV4" : "MCUMGR_TRANSPORT_UDP_IPV4", 105 "MCUMGR_SMP_UDP_IPV6" : "MCUMGR_TRANSPORT_UDP_IPV6", 106 "MCUMGR_SMP_UDP_PORT" : "MCUMGR_TRANSPORT_UDP_PORT", 107 "MCUMGR_SMP_UDP_STACK_SIZE" : "MCUMGR_TRANSPORT_UDP_STACK_SIZE", 108 "MCUMGR_SMP_UDP_THREAD_PRIO" : "MCUMGR_TRANSPORT_UDP_THREAD_PRIO", 109 "MCUMGR_SMP_UDP_MTU" : "MCUMGR_TRANSPORT_UDP_MTU", 110} 111 112def process_file(path): 113 modified = False 114 output = [] 115 116 try: 117 with open(path) as f: 118 lines = f.readlines() 119 120 for line in lines: 121 longest = "" 122 length = 0 123 for m in REPLACEMENTS: 124 if re.match(".*" + m + ".*", line) and len(m) > length: 125 length = len(m) 126 longest = m 127 128 if length != 0: 129 modified = True 130 line = line.replace(longest, REPLACEMENTS[longest]) 131 132 output.append(line) 133 134 if modified is False: 135 return 136 137 with open(path, "w") as f: 138 f.writelines(output) 139 140 except UnicodeDecodeError: 141 print(f"Unable to read lines from {path}", file=sys.stderr) 142 except Exception as e: 143 print(f"Failed with exception {e}", e) 144 145def process_tree(project): 146 for p in project.glob("**/*"): 147 for fp in FILE_PATTERNS: 148 cfp = re.compile(".+/" + fp + "$") 149 if re.match(cfp, str(p)) is not None: 150 process_file(p) 151 152if __name__ == "__main__": 153 parser = argparse.ArgumentParser(allow_abbrev=False) 154 parser.add_argument( 155 "-r", "--root", type=Path, required=True, help="Zephyr-based project path" 156 ) 157 args = parser.parse_args() 158 159 process_tree(args.root) 160