Skip to content

Commit 236208a

Browse files
committed
transport: use a bitmask for the transport
Move the transport's names in a local array in the transport framework. Replace the string struct transport::name, that identifies the transport, with a bitmask where each bit corresponds to one of the available transports. Change-Id: I6bdf7264d5979c355299f63fcf80bf54dcd95cee Signed-off-by: Antonio Borneo <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/8674 Tested-by: jenkins Reviewed-by: zapb <[email protected]>
1 parent 9a5de74 commit 236208a

File tree

7 files changed

+102
-31
lines changed

7 files changed

+102
-31
lines changed

src/jtag/core.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ static int jtag_select(struct command_context *ctx)
18231823
}
18241824

18251825
static struct transport jtag_transport = {
1826-
.name = "jtag",
1826+
.id = TRANSPORT_JTAG,
18271827
.select = jtag_select,
18281828
.init = jtag_init,
18291829
};
@@ -1868,7 +1868,7 @@ int adapter_resets(int trst, int srst)
18681868
transport_is_swim()) {
18691869
if (trst == TRST_ASSERT) {
18701870
LOG_ERROR("transport %s has no trst signal",
1871-
get_current_transport()->name);
1871+
get_current_transport_name());
18721872
return ERROR_FAIL;
18731873
}
18741874

@@ -1884,7 +1884,7 @@ int adapter_resets(int trst, int srst)
18841884
return ERROR_OK;
18851885

18861886
LOG_ERROR("reset is not supported on transport %s",
1887-
get_current_transport()->name);
1887+
get_current_transport_name());
18881888

18891889
return ERROR_FAIL;
18901890
}
@@ -1903,7 +1903,7 @@ int adapter_assert_reset(void)
19031903
return adapter_system_reset(1);
19041904
else if (get_current_transport())
19051905
LOG_ERROR("reset is not supported on %s",
1906-
get_current_transport()->name);
1906+
get_current_transport_name());
19071907
else
19081908
LOG_ERROR("transport is not selected");
19091909
return ERROR_FAIL;
@@ -1920,7 +1920,7 @@ int adapter_deassert_reset(void)
19201920
return adapter_system_reset(0);
19211921
else if (get_current_transport())
19221922
LOG_ERROR("reset is not supported on %s",
1923-
get_current_transport()->name);
1923+
get_current_transport_name());
19241924
else
19251925
LOG_ERROR("transport is not selected");
19261926
return ERROR_FAIL;

src/jtag/hla/hla_transport.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,20 @@ static int hl_transport_init(struct command_context *cmd_ctx)
177177
return ERROR_FAIL;
178178
}
179179

180-
LOG_DEBUG("current transport %s", transport->name);
180+
LOG_DEBUG("current transport %s", get_current_transport_name());
181181

182182
/* get selected transport as enum */
183-
tr = HL_TRANSPORT_UNKNOWN;
184-
185-
if (strcmp(transport->name, "hla_swd") == 0)
183+
switch (transport->id) {
184+
case TRANSPORT_HLA_SWD:
186185
tr = HL_TRANSPORT_SWD;
187-
else if (strcmp(transport->name, "hla_jtag") == 0)
186+
break;
187+
case TRANSPORT_HLA_JTAG:
188188
tr = HL_TRANSPORT_JTAG;
189+
break;
190+
default:
191+
tr = HL_TRANSPORT_UNKNOWN;
192+
break;
193+
}
189194

190195
int retval = hl_interface_open(tr);
191196

@@ -213,14 +218,14 @@ static int hl_swd_transport_select(struct command_context *cmd_ctx)
213218
}
214219

215220
static struct transport hl_swd_transport = {
216-
.name = "hla_swd",
221+
.id = TRANSPORT_HLA_SWD,
217222
.select = hl_swd_transport_select,
218223
.init = hl_transport_init,
219224
.override_target = hl_interface_override_target,
220225
};
221226

222227
static struct transport hl_jtag_transport = {
223-
.name = "hla_jtag",
228+
.id = TRANSPORT_HLA_JTAG,
224229
.select = hl_jtag_transport_select,
225230
.init = hl_transport_init,
226231
.override_target = hl_interface_override_target,

src/jtag/swim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int swim_transport_init(struct command_context *cmd_ctx)
136136
}
137137

138138
static struct transport swim_transport = {
139-
.name = "swim",
139+
.id = TRANSPORT_SWIM,
140140
.select = swim_transport_select,
141141
.init = swim_transport_init,
142142
};

src/target/adi_v5_dapdirect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ static int dapdirect_init(struct command_context *ctx)
207207
}
208208

209209
static struct transport dapdirect_jtag_transport = {
210-
.name = "dapdirect_jtag",
210+
.id = TRANSPORT_DAPDIRECT_JTAG,
211211
.select = dapdirect_jtag_select,
212212
.init = dapdirect_init,
213213
};
214214

215215
static struct transport dapdirect_swd_transport = {
216-
.name = "dapdirect_swd",
216+
.id = TRANSPORT_DAPDIRECT_SWD,
217217
.select = dapdirect_swd_select,
218218
.init = dapdirect_init,
219219
};

src/target/adi_v5_swd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ static int swd_init(struct command_context *ctx)
756756
}
757757

758758
static struct transport swd_transport = {
759-
.name = "swd",
759+
.id = TRANSPORT_SWD,
760760
.select = swd_select,
761761
.init = swd_init,
762762
};

src/transport/transport.c

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* messaging and error handling.
3131
*/
3232

33+
#include <helper/align.h>
34+
#include <helper/bits.h>
3335
#include <helper/log.h>
3436
#include <helper/replacements.h>
3537
#include <transport/transport.h>
@@ -43,6 +45,20 @@ extern struct command_context *global_cmd_ctx;
4345
*/
4446

4547
/** List of transports known to OpenOCD. */
48+
static const struct {
49+
unsigned int id;
50+
const char *name;
51+
} transport_names[] = {
52+
{ TRANSPORT_JTAG, "jtag", },
53+
{ TRANSPORT_SWD, "swd", },
54+
{ TRANSPORT_HLA_JTAG, "hla_jtag", },
55+
{ TRANSPORT_HLA_SWD, "hla_swd", },
56+
{ TRANSPORT_DAPDIRECT_JTAG, "dapdirect_jtag", },
57+
{ TRANSPORT_DAPDIRECT_SWD, "dapdirect_swd", },
58+
{ TRANSPORT_SWIM, "swim", },
59+
};
60+
61+
/** List of transports registered in OpenOCD. */
4662
static struct transport *transport_list;
4763

4864
/**
@@ -60,12 +76,26 @@ static bool transport_single_is_autoselected;
6076
/** * The transport being used for the current OpenOCD session. */
6177
static struct transport *session;
6278

79+
static const char *transport_name(unsigned int id)
80+
{
81+
for (unsigned int i = 0; i < ARRAY_SIZE(transport_names); i++)
82+
if (id == transport_names[i].id)
83+
return transport_names[i].name;
84+
85+
return NULL;
86+
}
87+
88+
static bool is_transport_id_valid(unsigned int id)
89+
{
90+
return (id != 0) && ((id & ~TRANSPORT_VALID_MASK) == 0) && IS_PWR_OF_2(id);
91+
}
92+
6393
static int transport_select(struct command_context *ctx, const char *name)
6494
{
6595
/* name may only identify a known transport;
6696
* caller guarantees session's transport isn't yet set.*/
6797
for (struct transport *t = transport_list; t; t = t->next) {
68-
if (strcmp(t->name, name) == 0) {
98+
if (!strcmp(transport_name(t->id), name)) {
6999
int retval = t->select(ctx);
70100
/* select() registers commands specific to this
71101
* transport, and may also reset the link, e.g.
@@ -74,7 +104,7 @@ static int transport_select(struct command_context *ctx, const char *name)
74104
if (retval == ERROR_OK)
75105
session = t;
76106
else
77-
LOG_ERROR("Error selecting '%s' as transport", t->name);
107+
LOG_ERROR("Error selecting '%s' as transport", name);
78108
return retval;
79109
}
80110
}
@@ -136,20 +166,28 @@ int transport_register(struct transport *new_transport)
136166
{
137167
struct transport *t;
138168

169+
if (!is_transport_id_valid(new_transport->id)) {
170+
LOG_ERROR("invalid transport ID 0x%x", new_transport->id);
171+
return ERROR_FAIL;
172+
}
173+
139174
for (t = transport_list; t; t = t->next) {
140-
if (strcmp(t->name, new_transport->name) == 0) {
141-
LOG_ERROR("transport name already used");
175+
if (t->id == new_transport->id) {
176+
LOG_ERROR("transport '%s' already registered",
177+
transport_name(t->id));
142178
return ERROR_FAIL;
143179
}
144180
}
145181

146182
if (!new_transport->select || !new_transport->init)
147-
LOG_ERROR("invalid transport %s", new_transport->name);
183+
LOG_ERROR("invalid transport %s",
184+
transport_name(new_transport->id));
148185

149186
/* splice this into the list */
150187
new_transport->next = transport_list;
151188
transport_list = new_transport;
152-
LOG_DEBUG("register '%s'", new_transport->name);
189+
LOG_DEBUG("register '%s' (ID %d)",
190+
transport_name(new_transport->id), new_transport->id);
153191

154192
return ERROR_OK;
155193
}
@@ -166,6 +204,14 @@ struct transport *get_current_transport(void)
166204
return session;
167205
}
168206

207+
const char *get_current_transport_name(void)
208+
{
209+
if (!session || !is_transport_id_valid(session->id))
210+
NULL;
211+
212+
return transport_name(session->id);
213+
}
214+
169215
/*-----------------------------------------------------------------------*/
170216

171217
/*
@@ -191,7 +237,7 @@ COMMAND_HANDLER(handle_transport_init)
191237
if (transport_single_is_autoselected)
192238
LOG_WARNING("DEPRECATED: auto-selecting transport \"%s\". "
193239
"Use 'transport select %s' to suppress this message.",
194-
session->name, session->name);
240+
transport_name(session->id), transport_name(session->id));
195241

196242
return session->init(CMD_CTX);
197243
}
@@ -204,7 +250,7 @@ COMMAND_HANDLER(handle_transport_list)
204250
command_print(CMD, "The following transports are available:");
205251

206252
for (struct transport *t = transport_list; t; t = t->next)
207-
command_print(CMD, "\t%s", t->name);
253+
command_print(CMD, "\t%s", transport_name(t->id));
208254

209255
return ERROR_OK;
210256
}
@@ -234,19 +280,19 @@ COMMAND_HANDLER(handle_transport_select)
234280
if (retval != ERROR_OK)
235281
return retval;
236282
}
237-
command_print(CMD, "%s", session->name);
283+
command_print(CMD, "%s", transport_name(session->id));
238284
return ERROR_OK;
239285
}
240286

241287
/* assign transport */
242288
if (session) {
243-
if (!strcmp(session->name, CMD_ARGV[0])) {
289+
if (!strcmp(transport_name(session->id), CMD_ARGV[0])) {
244290
if (transport_single_is_autoselected) {
245291
/* Nothing to do, but also nothing to complain */
246292
transport_single_is_autoselected = false;
247293
return ERROR_OK;
248294
}
249-
LOG_WARNING("Transport \"%s\" was already selected", session->name);
295+
LOG_WARNING("Transport \"%s\" was already selected", CMD_ARGV[0]);
250296
return ERROR_OK;
251297
}
252298
command_print(CMD, "Can't change session's transport after the initial selection was made");

src/transport/transport.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,27 @@
1212
#include "config.h"
1313
#endif
1414

15+
#include "helper/bits.h"
1516
#include "helper/command.h"
1617

18+
#define TRANSPORT_JTAG BIT(0)
19+
#define TRANSPORT_SWD BIT(1)
20+
#define TRANSPORT_HLA_JTAG BIT(2)
21+
#define TRANSPORT_HLA_SWD BIT(3)
22+
#define TRANSPORT_DAPDIRECT_JTAG BIT(4)
23+
#define TRANSPORT_DAPDIRECT_SWD BIT(5)
24+
#define TRANSPORT_SWIM BIT(6)
25+
26+
/* mask for valid ID */
27+
#define TRANSPORT_VALID_MASK \
28+
(TRANSPORT_JTAG | \
29+
TRANSPORT_SWD | \
30+
TRANSPORT_HLA_JTAG | \
31+
TRANSPORT_HLA_SWD | \
32+
TRANSPORT_DAPDIRECT_JTAG | \
33+
TRANSPORT_DAPDIRECT_SWD | \
34+
TRANSPORT_SWIM)
35+
1736
/**
1837
* Wrapper for transport lifecycle operations.
1938
*
@@ -34,11 +53,10 @@
3453
*/
3554
struct transport {
3655
/**
37-
* Each transport has a unique name, used to select it
38-
* from among the alternatives. Examples might include
39-
* "jtag", * "swd", "AVR_ISP" and more.
56+
* Each transport has a unique ID, used to select it
57+
* from among the alternatives.
4058
*/
41-
const char *name;
59+
unsigned int id;
4260

4361
/**
4462
* When a transport is selected, this method registers
@@ -75,6 +93,8 @@ int transport_register(struct transport *new_transport);
7593

7694
struct transport *get_current_transport(void);
7795

96+
const char *get_current_transport_name(void);
97+
7898
int transport_register_commands(struct command_context *ctx);
7999

80100
int allow_transports(struct command_context *ctx, const char * const *vector);

0 commit comments

Comments
 (0)