split jim_newtap_cmd into pieces
[openocd.git] / src / jtag / tcl.c
index 929c784e2280765fa8569ac946809bfc6b29323d..7ec7fa40980fb711b9e408e18508ac9700d64b57 100644 (file)
@@ -293,94 +293,105 @@ static Jim_Nvp nvp_config_opts[] = {
        { .name = NULL,          .value = -1 }
 };
 
-static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
+static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
 {
+       if (goi->argc == 0)
+       {
+               Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
+               return JIM_ERR;
+       }
+
        Jim_Nvp *n;
-       Jim_Obj *o;
-       int e;
+       int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
+       if (e != JIM_OK)
+       {
+               Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
+               return e;
+       }
+
+       if (goi->isconfigure) {
+               if (goi->argc != 1) {
+                       Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
+                       return JIM_ERR;
+               }
+       } else {
+               if (goi->argc != 0) {
+                       Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
+                       return JIM_ERR;
+               }
+       }
 
+       struct jtag_tap_event_action *jteap  = tap->event_action;
+       /* replace existing event body */
+       bool found = false;
+       while (jteap)
+       {
+               if (jteap->event == (enum jtag_event)n->value)
+               {
+                       found = true;
+                       break;
+               }
+               jteap = jteap->next;
+       }
+
+       Jim_SetEmptyResult(goi->interp);
+
+       if (goi->isconfigure)
+       {
+               if (!found)
+                       jteap = calloc(1, sizeof(*jteap));
+               else if (NULL != jteap->body)
+                       Jim_DecrRefCount(interp, jteap->body);
+
+               jteap->event = n->value;
+
+               Jim_Obj *o;
+               Jim_GetOpt_Obj(goi, &o);
+               jteap->body = Jim_DuplicateObj(goi->interp, o);
+               Jim_IncrRefCount(jteap->body);
+
+               if (!found)
+               {
+                       /* add to head of event list */
+                       jteap->next = tap->event_action;
+                       tap->event_action = jteap;
+               }
+       }
+       else if (found)
+       {
+               Jim_SetResult(goi->interp,
+                       Jim_DuplicateObj(goi->interp, jteap->body));
+       }
+       return JIM_OK;
+}
+
+static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
+{
        /* parse config or cget options */
-       while (goi->argc > 0) {
+       while (goi->argc > 0)
+       {
                Jim_SetEmptyResult (goi->interp);
 
-               e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
-               if (e != JIM_OK) {
+               Jim_Nvp *n;
+               int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
+               if (e != JIM_OK)
+               {
                        Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
                        return e;
                }
 
-               switch (n->value) {
-                       case JCFG_EVENT:
-                               if (goi->argc == 0) {
-                                       Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
-                                       return JIM_ERR;
-                               }
-
-                               e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
-                               if (e != JIM_OK) {
-                                       Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
-                                       return e;
-                               }
-
-                               if (goi->isconfigure) {
-                                       if (goi->argc != 1) {
-                                               Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
-                                               return JIM_ERR;
-                                       }
-                               } else {
-                                       if (goi->argc != 0) {
-                                               Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
-                                               return JIM_ERR;
-                                       }
-                               }
-
-                               {
-                                       struct jtag_tap_event_action *jteap;
-
-                                       jteap = tap->event_action;
-                                       /* replace existing? */
-                                       while (jteap) {
-                                               if (jteap->event == (enum jtag_event)n->value) {
-                                                       break;
-                                               }
-                                               jteap = jteap->next;
-                                       }
-
-                                       if (goi->isconfigure) {
-                                               bool replace = true;
-                                               if (jteap == NULL) {
-                                                       /* create new */
-                                                       jteap = calloc(1, sizeof (*jteap));
-                                                       replace = false;
-                                               }
-                                               jteap->event = n->value;
-                                               Jim_GetOpt_Obj(goi, &o);
-                                               if (jteap->body) {
-                                                       Jim_DecrRefCount(interp, jteap->body);
-                                               }
-                                               jteap->body = Jim_DuplicateObj(goi->interp, o);
-                                               Jim_IncrRefCount(jteap->body);
-
-                                               if (!replace)
-                                               {
-                                                       /* add to head of event list */
-                                                       jteap->next = tap->event_action;
-                                                       tap->event_action = jteap;
-                                               }
-                                               Jim_SetEmptyResult(goi->interp);
-                                       } else {
-                                               /* get */
-                                               if (jteap == NULL) {
-                                                       Jim_SetEmptyResult(goi->interp);
-                                               } else {
-                                                       Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
-                                               }
-                                       }
-                               }
-                               /* loop for more */
-                               break;
+               switch (n->value)
+               {
+               case JCFG_EVENT:
+                       e = jtag_tap_configure_event(goi, tap);
+                       if (e != JIM_OK)
+                               return e;
+                       break;
+               default:
+                       Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
+                       return JIM_ERR;
                }
-       } /* while (goi->argc) */
+       }
 
        return JIM_OK;
 }
@@ -395,26 +406,106 @@ static int is_bad_irval(int ir_length, jim_wide w)
        return (w & v) != 0;
 }
 
+static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
+               struct jtag_tap *pTap)
+{
+       jim_wide w;
+       int e = Jim_GetOpt_Wide(goi, &w);
+       if (e != JIM_OK) {
+               Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
+               return e;
+       }
+
+       unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
+       uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
+       if (new_expected_ids == NULL)
+       {
+               Jim_SetResult_sprintf(goi->interp, "no memory");
+               return JIM_ERR;
+       }
+
+       memcpy(new_expected_ids, pTap->expected_ids, expected_len);
+
+       new_expected_ids[pTap->expected_ids_cnt] = w;
+
+       free(pTap->expected_ids);
+       pTap->expected_ids = new_expected_ids;
+       pTap->expected_ids_cnt++;
+
+       return JIM_OK;
+}
+
+#define NTAP_OPT_IRLEN     0
+#define NTAP_OPT_IRMASK    1
+#define NTAP_OPT_IRCAPTURE 2
+#define NTAP_OPT_ENABLED   3
+#define NTAP_OPT_DISABLED  4
+#define NTAP_OPT_EXPECTED_ID 5
+
+static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
+               struct jtag_tap *pTap)
+{
+       jim_wide w;
+       int e = Jim_GetOpt_Wide(goi, &w);
+       if (e != JIM_OK)
+       {
+               Jim_SetResult_sprintf(goi->interp,
+                               "option: %s bad parameter", n->name);
+               free((void *)pTap->dotted_name);
+               return e;
+       }
+       switch (n->value) {
+       case NTAP_OPT_IRLEN:
+               if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
+               {
+                       LOG_WARNING("%s: huge IR length %d",
+                                       pTap->dotted_name, (int) w);
+               }
+               pTap->ir_length = w;
+               break;
+       case NTAP_OPT_IRMASK:
+               if (is_bad_irval(pTap->ir_length, w))
+               {
+                       LOG_ERROR("%s: IR mask %x too big",
+                                       pTap->dotted_name,
+                                       (int) w);
+                       return JIM_ERR;
+               }
+               if ((w & 3) != 3)
+                       LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
+               pTap->ir_capture_mask = w;
+               break;
+       case NTAP_OPT_IRCAPTURE:
+               if (is_bad_irval(pTap->ir_length, w))
+               {
+                       LOG_ERROR("%s: IR capture %x too big",
+                                       pTap->dotted_name, (int) w);
+                       return JIM_ERR;
+               }
+               if ((w & 3) != 1)
+                       LOG_WARNING("%s: nonstandard IR value",
+                                       pTap->dotted_name);
+               pTap->ir_capture_value = w;
+               break;
+       default:
+               return JIM_ERR;
+       }
+       return JIM_OK;
+}
+
 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
 {
        struct jtag_tap *pTap;
-       jim_wide w;
        int x;
        int e;
        Jim_Nvp *n;
        char *cp;
        const Jim_Nvp opts[] = {
-#define NTAP_OPT_IRLEN     0
                { .name = "-irlen"                      ,       .value = NTAP_OPT_IRLEN },
-#define NTAP_OPT_IRMASK    1
                { .name = "-irmask"                     ,       .value = NTAP_OPT_IRMASK },
-#define NTAP_OPT_IRCAPTURE 2
                { .name = "-ircapture"          ,       .value = NTAP_OPT_IRCAPTURE },
-#define NTAP_OPT_ENABLED   3
                { .name = "-enable"                     ,       .value = NTAP_OPT_ENABLED },
-#define NTAP_OPT_DISABLED  4
                { .name = "-disable"            ,       .value = NTAP_OPT_DISABLED },
-#define NTAP_OPT_EXPECTED_ID 5
                { .name = "-expected-id"        ,       .value = NTAP_OPT_EXPECTED_ID },
                { .name = NULL                          ,       .value = -1 },
        };
@@ -472,81 +563,25 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi)
                        pTap->disabled_after_reset = true;
                        break;
                case NTAP_OPT_EXPECTED_ID:
-               {
-                       uint32_t *new_expected_ids;
-
-                       e = Jim_GetOpt_Wide(goi, &w);
-                       if (e != JIM_OK) {
-                               Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
+                       e = jim_newtap_expected_id(n, goi, pTap);
+                       if (JIM_OK != e)
+                       {
                                free((void *)pTap->dotted_name);
                                free(pTap);
                                return e;
                        }
-
-                       new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
-                       if (new_expected_ids == NULL) {
-                               Jim_SetResult_sprintf(goi->interp, "no memory");
-                               free((void *)pTap->dotted_name);
-                               free(pTap);
-                               return JIM_ERR;
-                       }
-
-                       memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
-
-                       new_expected_ids[pTap->expected_ids_cnt] = w;
-
-                       free(pTap->expected_ids);
-                       pTap->expected_ids = new_expected_ids;
-                       pTap->expected_ids_cnt++;
                        break;
-               }
                case NTAP_OPT_IRLEN:
                case NTAP_OPT_IRMASK:
                case NTAP_OPT_IRCAPTURE:
-                       e = Jim_GetOpt_Wide(goi, &w);
-                       if (e != JIM_OK) {
-                               Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
+                       e = jim_newtap_ir_param(n, goi, pTap);
+                       if (JIM_OK != e)
+                       {
                                free((void *)pTap->dotted_name);
                                free(pTap);
                                return e;
                        }
-                       switch (n->value) {
-                       case NTAP_OPT_IRLEN:
-                               if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
-                                       LOG_WARNING("%s: huge IR length %d",
-                                                       pTap->dotted_name,
-                                                       (int) w);
-                               pTap->ir_length = w;
-                               break;
-                       case NTAP_OPT_IRMASK:
-                               if (is_bad_irval(pTap->ir_length, w)) {
-                                       LOG_ERROR("%s: IR mask %x too big",
-                                                       pTap->dotted_name,
-                                                       (int) w);
-                                       free((void *)pTap->dotted_name);
-                                       free(pTap);
-                                       return ERROR_FAIL;
-                               }
-                               if ((w & 3) != 3)
-                                       LOG_WARNING("%s: nonstandard IR mask",
-                                                       pTap->dotted_name);
-                               pTap->ir_capture_mask = w;
-                               break;
-                       case NTAP_OPT_IRCAPTURE:
-                               if (is_bad_irval(pTap->ir_length, w)) {
-                                       LOG_ERROR("%s: IR capture %x too big",
-                                                       pTap->dotted_name,
-                                                       (int) w);
-                                       free((void *)pTap->dotted_name);
-                                       free(pTap);
-                                       return ERROR_FAIL;
-                               }
-                               if ((w & 3) != 1)
-                                       LOG_WARNING("%s: nonstandard IR value",
-                                                       pTap->dotted_name);
-                               pTap->ir_capture_value = w;
-                               break;
-                       }
+                       break;
                } /* switch (n->value) */
        } /* while (goi->argc) */
 
@@ -571,29 +606,36 @@ static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
 {
        struct jtag_tap_event_action * jteap;
 
-       for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
-               if (jteap->event == e) {
-                       LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
-                                       tap->dotted_name,
-                                       e,
-                                       Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
-                                       Jim_GetString(jteap->body, NULL));
-                       if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
-                               Jim_PrintErrorMessage(interp);
-                       } else switch (e) {
-                       case JTAG_TAP_EVENT_ENABLE:
-                       case JTAG_TAP_EVENT_DISABLE:
-                               /* NOTE:  we currently assume the handlers
-                                * can't fail.  Right here is where we should
-                                * really be verifying the scan chains ...
-                                */
-                               tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
-                               LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
-                                       tap->enabled ? "enabled" : "disabled");
-                               break;
-                       default:
-                               break;
-                       }
+       for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
+       {
+               if (jteap->event != e)
+                       continue;
+
+               Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
+               LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
+                               tap->dotted_name, e, nvp->name,
+                               Jim_GetString(jteap->body, NULL));
+
+               if (Jim_EvalObj(interp, jteap->body) != JIM_OK)
+               {
+                       Jim_PrintErrorMessage(interp);
+                       continue;
+               }
+
+               switch (e)
+               {
+               case JTAG_TAP_EVENT_ENABLE:
+               case JTAG_TAP_EVENT_DISABLE:
+                       /* NOTE:  we currently assume the handlers
+                        * can't fail.  Right here is where we should
+                        * really be verifying the scan chains ...
+                        */
+                       tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
+                       LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
+                               tap->enabled ? "enabled" : "disabled");
+                       break;
+               default:
+                       break;
                }
        }
 }

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)