From 5c5f76d60e63a535caa8b54bfc5f12e21467052a Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Thu, 30 Jun 2016 14:18:05 +1200 Subject: [PATCH] target: Remove last accesses to private structure from outside. Only include target_internal.h from inside target files. --- src/adiv5_swdp.c | 9 ++------- src/command.c | 26 ++++++++++++-------------- src/cortexa.c | 7 +------ src/cortexm.c | 4 +--- src/efm32.c | 4 +--- src/include/command.h | 8 -------- src/include/target.h | 10 +++------- src/jtag_scan.c | 6 ------ src/kinetis.c | 1 + src/lmi.c | 1 + src/lpc11xx.c | 1 + src/lpc15xx.c | 1 + src/lpc43xx.c | 2 +- src/lpc_common.c | 1 + src/nrf51.c | 3 +-- src/sam3x.c | 3 +-- src/samd.c | 4 +--- src/stm32f1.c | 3 +-- src/stm32f4.c | 3 +-- src/stm32l0.c | 3 +-- src/stm32l4.c | 3 +-- src/target.c | 21 +++++++++++++++++++++ src/target_internal.h | 14 ++++++++++++++ 23 files changed, 68 insertions(+), 70 deletions(-) diff --git a/src/adiv5_swdp.c b/src/adiv5_swdp.c index 7cba941..f396c7d 100644 --- a/src/adiv5_swdp.c +++ b/src/adiv5_swdp.c @@ -26,9 +26,8 @@ #include "exception.h" #include "adiv5.h" #include "swdptap.h" -#include "command.h" -#include "morse.h" -#include "gdb_packet.h" +#include "target.h" +#include "target_internal.h" #define SWDP_ACK_OK 0x01 #define SWDP_ACK_WAIT 0x02 @@ -58,7 +57,6 @@ int adiv5_swdp_scan(void) ack = swdptap_seq_in(3); if((ack != SWDP_ACK_OK) || swdptap_seq_in_parity(&dp->idcode, 32)) { DEBUG("\n"); - morse("NO TARGETS.", 1); free(dp); return -1; } @@ -71,9 +69,6 @@ int adiv5_swdp_scan(void) adiv5_swdp_error(dp); adiv5_dp_init(dp); - if(!target_list) morse("NO TARGETS.", 1); - else morse(NULL, 0); - return target_list?1:0; } diff --git a/src/command.c b/src/command.c index 8b1a11a..be53dad 100644 --- a/src/command.c +++ b/src/command.c @@ -34,6 +34,15 @@ # include "traceswo.h" #endif +typedef bool (*cmd_handler)(target *t, int argc, const char **argv); + +struct command_s { + const char *cmd; + cmd_handler handler; + + const char *help; +}; + static bool cmd_version(void); static bool cmd_help(target *t); @@ -81,7 +90,6 @@ bool debug_bmp; int command_process(target *t, char *cmd) { - struct target_command_s *tc; const struct command_s *c; int argc = 0; const char **argv; @@ -101,19 +109,14 @@ int command_process(target *t, char *cmd) /* Accept a partial match as GDB does. * So 'mon ver' will match 'monitor version' */ - if(!strncmp(argv[0], c->cmd, strlen(argv[0]))) + if ((argc == 0) || !strncmp(argv[0], c->cmd, strlen(argv[0]))) return !c->handler(t, argc, argv); } if (!t) return -1; - for (tc = t->commands; tc; tc = tc->next) - for(c = tc->cmds; c->cmd; c++) - if(!strncmp(argv[0], c->cmd, strlen(argv[0]))) - return !c->handler(t, argc, argv); - - return -1; + return target_command(t, argc, argv); } bool cmd_version(void) @@ -128,7 +131,6 @@ bool cmd_version(void) bool cmd_help(target *t) { - struct target_command_s *tc; const struct command_s *c; gdb_out("General commands:\n"); @@ -138,11 +140,7 @@ bool cmd_help(target *t) if (!t) return -1; - for (tc = t->commands; tc; tc = tc->next) { - gdb_outf("%s specific commands:\n", tc->specific_name); - for(c = tc->cmds; c->cmd; c++) - gdb_outf("\t%s -- %s\n", c->cmd, c->help); - } + target_command_help(t); return true; } diff --git a/src/cortexa.c b/src/cortexa.c index 4baf4e0..949fc5c 100644 --- a/src/cortexa.c +++ b/src/cortexa.c @@ -29,14 +29,10 @@ */ #include "general.h" #include "exception.h" -#include "jtagtap.h" -#include "jtag_scan.h" #include "adiv5.h" #include "target.h" -#include "command.h" +#include "target_internal.h" #include "gdb_packet.h" -#include "cortexm.h" -#include "morse.h" #include @@ -606,7 +602,6 @@ static int cortexa_halt_wait(target *t) case EXCEPTION_ERROR: /* Oh crap, there's no recovery from this... */ target_list_free(); - morse("TARGET LOST.", 1); return SIGLOST; case EXCEPTION_TIMEOUT: /* Timeout isn't a problem, target could be in WFI */ diff --git a/src/cortexm.c b/src/cortexm.c index 1e538e6..46fa6ed 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -29,10 +29,9 @@ #include "exception.h" #include "adiv5.h" #include "target.h" -#include "command.h" +#include "target_internal.h" #include "gdb_packet.h" #include "cortexm.h" -#include "morse.h" #include @@ -507,7 +506,6 @@ static int cortexm_halt_wait(target *t) case EXCEPTION_ERROR: /* Oh crap, there's no recovery from this... */ target_list_free(); - morse("TARGET LOST.", 1); return SIGLOST; case EXCEPTION_TIMEOUT: /* Timeout isn't a problem, target could be in WFI */ diff --git a/src/efm32.c b/src/efm32.c index 52b2ea0..e71d1a4 100644 --- a/src/efm32.c +++ b/src/efm32.c @@ -37,10 +37,8 @@ */ #include "general.h" -#include "jtagtap.h" -#include "adiv5.h" #include "target.h" -#include "command.h" +#include "target_internal.h" #include "gdb_packet.h" #include "cortexm.h" diff --git a/src/include/command.h b/src/include/command.h index 3910bbb..539af5c 100644 --- a/src/include/command.h +++ b/src/include/command.h @@ -24,14 +24,6 @@ #include "target.h" int command_process(target *t, char *cmd); -typedef bool (*cmd_handler)(target *t, int argc, const char **argv); - -struct command_s { - const char *cmd; - cmd_handler handler; - - const char *help; -}; #endif diff --git a/src/include/target.h b/src/include/target.h index fe47e21..182f1c6 100644 --- a/src/include/target.h +++ b/src/include/target.h @@ -87,13 +87,9 @@ const char *target_tdesc(target *t); const char *target_mem_map(target *t); const char *target_driver_name(target *t); -struct target_command_s { - const char *specific_name; - const struct command_s *cmds; - struct target_command_s *next; -}; - -#include "target_internal.h" +/* Command interpreter */ +void target_command_help(target *t); +int target_command(target *t, int argc, const char *argv[]); #endif diff --git a/src/jtag_scan.c b/src/jtag_scan.c index db39c5c..4546be5 100644 --- a/src/jtag_scan.c +++ b/src/jtag_scan.c @@ -25,9 +25,7 @@ #include "general.h" #include "jtagtap.h" -#include "morse.h" #include "jtag_scan.h" -#include "gdb_packet.h" #include "target.h" #include "adiv5.h" @@ -187,7 +185,6 @@ int jtag_scan(const uint8_t *irlens) jtagtap_next(1, 1); jtagtap_return_idle(); if(!jtag_dev_count) { - morse("NO TARGETS.", 1); return 0; } @@ -224,9 +221,6 @@ int jtag_scan(const uint8_t *irlens) break; } - if(!target_list) morse("NO TARGETS.", 1); - else morse(NULL, 0); - return jtag_dev_count; } diff --git a/src/kinetis.c b/src/kinetis.c index 914df7e..d8cb9f2 100644 --- a/src/kinetis.c +++ b/src/kinetis.c @@ -29,6 +29,7 @@ #include "general.h" #include "target.h" +#include "target_internal.h" #define SIM_SDID 0x40048024 diff --git a/src/lmi.c b/src/lmi.c index e2d1809..5989990 100644 --- a/src/lmi.c +++ b/src/lmi.c @@ -26,6 +26,7 @@ #include "general.h" #include "target.h" +#include "target_internal.h" #include "cortexm.h" #define SRAM_BASE 0x20000000 diff --git a/src/lpc11xx.c b/src/lpc11xx.c index fcf9f0e..e929d6a 100644 --- a/src/lpc11xx.c +++ b/src/lpc11xx.c @@ -20,6 +20,7 @@ #include "general.h" #include "target.h" +#include "target_internal.h" #include "cortexm.h" #include "lpc_common.h" diff --git a/src/lpc15xx.c b/src/lpc15xx.c index 3f23f9f..4e362e2 100644 --- a/src/lpc15xx.c +++ b/src/lpc15xx.c @@ -21,6 +21,7 @@ #include "general.h" #include "target.h" +#include "target_internal.h" #include "cortexm.h" #include "lpc_common.h" diff --git a/src/lpc43xx.c b/src/lpc43xx.c index b6c499a..699580c 100644 --- a/src/lpc43xx.c +++ b/src/lpc43xx.c @@ -19,8 +19,8 @@ */ #include "general.h" -#include "command.h" #include "target.h" +#include "target_internal.h" #include "gdb_packet.h" #include "cortexm.h" #include "lpc_common.h" diff --git a/src/lpc_common.c b/src/lpc_common.c index ad62e15..88ab4a8 100644 --- a/src/lpc_common.c +++ b/src/lpc_common.c @@ -18,6 +18,7 @@ */ #include "general.h" #include "target.h" +#include "target_internal.h" #include "cortexm.h" #include "lpc_common.h" diff --git a/src/nrf51.c b/src/nrf51.c index 88e63f5..df609f2 100644 --- a/src/nrf51.c +++ b/src/nrf51.c @@ -22,9 +22,8 @@ */ #include "general.h" -#include "adiv5.h" #include "target.h" -#include "command.h" +#include "target_internal.h" #include "gdb_packet.h" #include "cortexm.h" diff --git a/src/sam3x.c b/src/sam3x.c index 703bacc..4cd1227 100644 --- a/src/sam3x.c +++ b/src/sam3x.c @@ -25,9 +25,8 @@ */ #include "general.h" -#include "adiv5.h" #include "target.h" -#include "command.h" +#include "target_internal.h" #include "gdb_packet.h" static int sam4_flash_erase(struct target_flash *f, uint32_t addr, size_t len); diff --git a/src/samd.c b/src/samd.c index a39cc37..294018f 100644 --- a/src/samd.c +++ b/src/samd.c @@ -33,10 +33,8 @@ */ #include "general.h" -#include "jtagtap.h" -#include "adiv5.h" #include "target.h" -#include "command.h" +#include "target_internal.h" #include "gdb_packet.h" #include "cortexm.h" diff --git a/src/stm32f1.c b/src/stm32f1.c index 17d6126..13996b3 100644 --- a/src/stm32f1.c +++ b/src/stm32f1.c @@ -30,10 +30,9 @@ */ #include "general.h" -#include "adiv5.h" #include "target.h" +#include "target_internal.h" #include "cortexm.h" -#include "command.h" #include "gdb_packet.h" static bool stm32f1_cmd_erase_mass(target *t); diff --git a/src/stm32f4.c b/src/stm32f4.c index f94f8db..33844ae 100644 --- a/src/stm32f4.c +++ b/src/stm32f4.c @@ -31,10 +31,9 @@ */ #include "general.h" -#include "adiv5.h" #include "target.h" +#include "target_internal.h" #include "cortexm.h" -#include "command.h" #include "gdb_packet.h" static bool stm32f4_cmd_erase_mass(target *t); diff --git a/src/stm32l0.c b/src/stm32l0.c index b5ee548..699d23e 100644 --- a/src/stm32l0.c +++ b/src/stm32l0.c @@ -74,9 +74,8 @@ */ #include "general.h" -#include "adiv5.h" #include "target.h" -#include "command.h" +#include "target_internal.h" #include "gdb_packet.h" #include "cortexm.h" diff --git a/src/stm32l4.c b/src/stm32l4.c index d4e22a7..24d1eb8 100644 --- a/src/stm32l4.c +++ b/src/stm32l4.c @@ -31,10 +31,9 @@ */ #include "general.h" -#include "adiv5.h" #include "target.h" +#include "target_internal.h" #include "cortexm.h" -#include "command.h" #include "gdb_packet.h" static bool stm32l4_cmd_erase_mass(target *t); diff --git a/src/target.c b/src/target.c index 415166e..5e98009 100644 --- a/src/target.c +++ b/src/target.c @@ -20,6 +20,7 @@ #include "general.h" #include "target.h" +#include "target_internal.h" target *target_list = NULL; @@ -399,3 +400,23 @@ void target_mem_write8(target *t, uint32_t addr, uint8_t value) target_mem_write(t, addr, &value, sizeof(value)); } +#include "gdb_packet.h" + +void target_command_help(target *t) +{ + for (struct target_command_s *tc = t->commands; tc; tc = tc->next) { + gdb_outf("%s specific commands:\n", tc->specific_name); + for(const struct command_s *c = tc->cmds; c->cmd; c++) + gdb_outf("\t%s -- %s\n", c->cmd, c->help); + } +} + +int target_command(target *t, int argc, const char *argv[]) +{ + for (struct target_command_s *tc = t->commands; tc; tc = tc->next) + for(const struct command_s *c = tc->cmds; c->cmd; c++) + if(!strncmp(argv[0], c->cmd, strlen(argv[0]))) + return !c->handler(t, argc, argv); + return -1; +} + diff --git a/src/target_internal.h b/src/target_internal.h index 98f6239..190ea72 100644 --- a/src/target_internal.h +++ b/src/target_internal.h @@ -54,6 +54,20 @@ struct target_flash { void *buf; }; +typedef bool (*cmd_handler)(target *t, int argc, const char **argv); + +struct command_s { + const char *cmd; + cmd_handler handler; + const char *help; +}; + +struct target_command_s { + const char *specific_name; + const struct command_s *cmds; + struct target_command_s *next; +}; + struct target_s { bool attached; /* Notify controlling debugger if target is lost */