transport: rewrite command 'transport select' as COMMAND_HANDLER
[openocd.git] / src / transport / transport.c
index c05db3f005f5ac1e503aeccd6c72ccc0bd1de65e..81d3d583bcd12103101da265f716366d55a4a8b1 100644 (file)
@@ -252,64 +252,62 @@ COMMAND_HANDLER(handle_transport_list)
  * set supported by the debug adapter being used.  Return value
  * is scriptable (allowing "if swd then..." etc).
  */
-static int jim_transport_select(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
+COMMAND_HANDLER(handle_transport_select)
 {
-       int res;
-       switch (argc) {
-               case 1: /* autoselect if necessary, then return/display current config */
-                       if (!session) {
-                               if (!allowed_transports) {
-                                       LOG_ERROR("Debug adapter does not support any transports? Check config file order.");
-                                       return JIM_ERR;
-                               }
-                               LOG_INFO("auto-selecting first available session transport \"%s\". "
-                                        "To override use 'transport select <transport>'.", allowed_transports[0]);
-                               res = transport_select(global_cmd_ctx, allowed_transports[0]);
-                               if (res != JIM_OK)
-                                       return res;
-                       }
-                       Jim_SetResultString(interp, session->name, -1);
-                       return JIM_OK;
-               case 2: /* assign */
-                       if (session) {
-                               if (!strcmp(session->name, argv[1]->bytes)) {
-                                       LOG_WARNING("Transport \"%s\" was already selected", session->name);
-                                       Jim_SetResultString(interp, session->name, -1);
-                                       return JIM_OK;
-                               } else {
-                                       LOG_ERROR("Can't change session's transport after the initial selection was made");
-                                       return JIM_ERR;
-                               }
-                       }
+       if (CMD_ARGC > 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
-                       /* Is this transport supported by our debug adapter?
-                        * Example, "JTAG-only" means SWD is not supported.
-                        *
-                        * NOTE:  requires adapter to have been set up, with
-                        * transports declared via C.
-                        */
+       if (CMD_ARGC == 0) {
+               /* autoselect if necessary, then return/display current config */
+               if (!session) {
                        if (!allowed_transports) {
-                               LOG_ERROR("Debug adapter doesn't support any transports?");
-                               return JIM_ERR;
+                               command_print(CMD, "Debug adapter does not support any transports? Check config file order.");
+                               return ERROR_FAIL;
                        }
+                       LOG_INFO("auto-selecting first available session transport \"%s\". "
+                               "To override use 'transport select <transport>'.", allowed_transports[0]);
+                       int retval = transport_select(CMD_CTX, allowed_transports[0]);
+                       if (retval != ERROR_OK)
+                               return retval;
+               }
+               command_print(CMD, "%s", session->name);
+               return ERROR_OK;
+       }
 
-                       for (unsigned i = 0; allowed_transports[i]; i++) {
+       /* assign transport */
+       if (session) {
+               if (!strcmp(session->name, CMD_ARGV[0])) {
+                       LOG_WARNING("Transport \"%s\" was already selected", session->name);
+                       command_print(CMD, "%s", session->name);
+                       return ERROR_OK;
+               }
+               command_print(CMD, "Can't change session's transport after the initial selection was made");
+               return ERROR_FAIL;
+       }
 
-                               if (strcmp(allowed_transports[i], argv[1]->bytes) == 0) {
-                                       if (transport_select(global_cmd_ctx, argv[1]->bytes) == ERROR_OK) {
-                                               Jim_SetResultString(interp, session->name, -1);
-                                               return JIM_OK;
-                                       }
-                                       return JIM_ERR;
-                               }
-                       }
+       /* Is this transport supported by our debug adapter?
+        * Example, "JTAG-only" means SWD is not supported.
+        *
+        * NOTE:  requires adapter to have been set up, with
+        * transports declared via C.
+        */
+       if (!allowed_transports) {
+               command_print(CMD, "Debug adapter doesn't support any transports?");
+               return ERROR_FAIL;
+       }
 
-                       LOG_ERROR("Debug adapter doesn't support '%s' transport", argv[1]->bytes);
-                       return JIM_ERR;
-               default:
-                       Jim_WrongNumArgs(interp, 1, argv, "[too many parameters]");
-                       return JIM_ERR;
+       for (unsigned int i = 0; allowed_transports[i]; i++) {
+               if (!strcmp(allowed_transports[i], CMD_ARGV[0])) {
+                       int retval = transport_select(CMD_CTX, CMD_ARGV[0]);
+                       if (retval != ERROR_OK)
+                               return retval;
+                       command_print(CMD, "%s", session->name);
+                       return ERROR_OK;
+               }
        }
+
+       command_print(CMD, "Debug adapter doesn't support '%s' transport", CMD_ARGV[0]);
+       return ERROR_FAIL;
 }
 
 static const struct command_registration transport_commands[] = {
@@ -333,7 +331,7 @@ static const struct command_registration transport_commands[] = {
        },
        {
                .name = "select",
-               .jim_handler = jim_transport_select,
+               .handler = handle_transport_select,
                .mode = COMMAND_ANY,
                .help = "Select this session's transport",
                .usage = "[transport_name]",

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)