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_PERM_RW_AUTHEN",
86    "MCUMGR_TRANSPORT_BT_AUTHEN" : "MCUMGR_TRANSPORT_BT_PERM_RW_AUTHEN",
87    "MCUMGR_SMP_BT_CONN_PARAM_CONTROL" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL",
88    "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_MIN_INT" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_MIN_INT",
89    "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_MAX_INT" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_MAX_INT",
90    "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_LATENCY" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_LATENCY",
91    "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_TIMEOUT" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_TIMEOUT",
92    "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_RESTORE_TIME" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_RESTORE_TIME",
93    "MCUMGR_SMP_BT_CONN_PARAM_CONTROL_RETRY_TIME" : "MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_RETRY_TIME",
94    "MCUMGR_SMP_DUMMY" : "MCUMGR_TRANSPORT_DUMMY",
95    "MCUMGR_SMP_DUMMY_RX_BUF_SIZE" : "MCUMGR_TRANSPORT_DUMMY_RX_BUF_SIZE",
96    "MCUMGR_SMP_SHELL" : "MCUMGR_TRANSPORT_SHELL",
97    "MCUMGR_SMP_SHELL_MTU" : "MCUMGR_TRANSPORT_SHELL_MTU",
98    "MCUMGR_SMP_SHELL_RX_BUF_COUNT" : "MCUMGR_TRANSPORT_SHELL_RX_BUF_COUNT",
99    "MCUMGR_SMP_UART" : "MCUMGR_TRANSPORT_UART",
100    "MCUMGR_SMP_UART_ASYNC" : "MCUMGR_TRANSPORT_UART_ASYNC",
101    "MCUMGR_SMP_UART_ASYNC_BUFS" : "MCUMGR_TRANSPORT_UART_ASYNC_BUFS",
102    "MCUMGR_SMP_UART_ASYNC_BUF_SIZE" : "MCUMGR_TRANSPORT_UART_ASYNC_BUF_SIZE",
103    "MCUMGR_SMP_UART_MTU" : "MCUMGR_TRANSPORT_UART_MTU",
104    "MCUMGR_SMP_UDP" : "MCUMGR_TRANSPORT_UDP",
105    "MCUMGR_SMP_UDP_IPV4" : "MCUMGR_TRANSPORT_UDP_IPV4",
106    "MCUMGR_SMP_UDP_IPV6" : "MCUMGR_TRANSPORT_UDP_IPV6",
107    "MCUMGR_SMP_UDP_PORT" : "MCUMGR_TRANSPORT_UDP_PORT",
108    "MCUMGR_SMP_UDP_STACK_SIZE" : "MCUMGR_TRANSPORT_UDP_STACK_SIZE",
109    "MCUMGR_SMP_UDP_THREAD_PRIO" : "MCUMGR_TRANSPORT_UDP_THREAD_PRIO",
110    "MCUMGR_SMP_UDP_MTU" : "MCUMGR_TRANSPORT_UDP_MTU",
111}
112
113def process_file(path):
114    modified = False
115    output = []
116
117    try:
118        with open(path) as f:
119            lines = f.readlines()
120
121            for line in lines:
122                longest = ""
123                length = 0
124                for m in REPLACEMENTS:
125                    if re.match(".*" + m + ".*", line) and len(m) > length:
126                        length = len(m)
127                        longest = m
128
129                if length != 0:
130                    modified = True
131                    line = line.replace(longest, REPLACEMENTS[longest])
132
133                output.append(line)
134
135        if modified is False:
136            return
137
138        with open(path, "w") as f:
139            f.writelines(output)
140
141    except UnicodeDecodeError:
142        print(f"Unable to read lines from {path}", file=sys.stderr)
143    except Exception as e:
144        print(f"Failed with exception {e}", e)
145
146def process_tree(project):
147    for p in project.glob("**/*"):
148        for fp in FILE_PATTERNS:
149            cfp = re.compile(".+/" + fp + "$")
150            if re.match(cfp, str(p)) is not None:
151                process_file(p)
152
153if __name__ == "__main__":
154    parser = argparse.ArgumentParser(allow_abbrev=False)
155    parser.add_argument(
156        "-r", "--root", type=Path, required=True, help="Zephyr-based project path"
157    )
158    args = parser.parse_args()
159
160    process_tree(args.root)
161