From 9cb3af610a1d7cc2d8c1433f54077938d0268a8f Mon Sep 17 00:00:00 2001
From: zwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>
Date: Sun, 31 May 2009 09:38:43 +0000
Subject: [PATCH] Add wrappers for target->type->examined: - replace all checks
 of target->type->examined with target_was_examined(). - replace all setting
 of target->type->examined with target_set_examined(). - replace clearing of
 target->type->examined with target_reset_examined(). - add documentation in
 target_s to warn not to access field directly.

git-svn-id: svn://svn.berlios.de/openocd/trunk@1962 b42882b7-edfa-0310-969c-e2dbd0fdcd60
---
 src/target/arm11.c         |  2 +-
 src/target/arm7_9_common.c |  2 +-
 src/target/arm7tdmi.c      |  4 +--
 src/target/arm9tdmi.c      |  4 +--
 src/target/cortex_a8.c     |  2 +-
 src/target/cortex_m3.c     |  6 ++--
 src/target/mips32.c        |  4 +--
 src/target/mips_m4k.c      |  2 +-
 src/target/target.c        | 56 ++++++++++++++++++++++++--------------
 src/target/target.h        | 13 +++++++++
 10 files changed, 62 insertions(+), 33 deletions(-)

diff --git a/src/target/arm11.c b/src/target/arm11.c
index 45deb60c5..0dc6bf476 100644
--- a/src/target/arm11.c
+++ b/src/target/arm11.c
@@ -1631,7 +1631,7 @@ int arm11_examine(struct target_s *target)
 
 	arm11_check_init(arm11, NULL);
 
-	target->type->examined = 1;
+	target_set_examined(target);
 
 	return ERROR_OK;
 }
diff --git a/src/target/arm7_9_common.c b/src/target/arm7_9_common.c
index 0d202d1ae..f6f277102 100644
--- a/src/target/arm7_9_common.c
+++ b/src/target/arm7_9_common.c
@@ -818,7 +818,7 @@ int arm7_9_handle_target_request(void *priv)
 {
 	int retval = ERROR_OK;
 	target_t *target = priv;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 		return ERROR_OK;
 	armv4_5_common_t *armv4_5 = target->arch_info;
 	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
diff --git a/src/target/arm7tdmi.c b/src/target/arm7tdmi.c
index 93281ce58..93d053cf8 100644
--- a/src/target/arm7tdmi.c
+++ b/src/target/arm7tdmi.c
@@ -718,7 +718,7 @@ int arm7tdmi_examine(struct target_s *target)
 	int retval;
 	armv4_5_common_t *armv4_5 = target->arch_info;
 	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		/* get pointers to arch-specific information */
 		reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
@@ -735,7 +735,7 @@ int arm7tdmi_examine(struct target_s *target)
 			(*cache_p)->next = etm_build_reg_cache(target, jtag_info, arm7_9->etm_ctx);
 			arm7_9->etm_ctx->reg_cache = (*cache_p)->next;
 		}
-		target->type->examined = 1;
+		target_set_examined(target);
 	}
 	if ((retval=embeddedice_setup(target))!=ERROR_OK)
 		return retval;
diff --git a/src/target/arm9tdmi.c b/src/target/arm9tdmi.c
index 4bcffd07b..93b2d66c2 100644
--- a/src/target/arm9tdmi.c
+++ b/src/target/arm9tdmi.c
@@ -810,7 +810,7 @@ int arm9tdmi_examine(struct target_s *target)
 	int retval;
 	armv4_5_common_t *armv4_5 = target->arch_info;
 	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
 		reg_cache_t *t;
@@ -827,7 +827,7 @@ int arm9tdmi_examine(struct target_s *target)
 			(*cache_p)->next = etm_build_reg_cache(target, jtag_info, arm7_9->etm_ctx);
 			arm7_9->etm_ctx->reg_cache = (*cache_p)->next;
 		}
-		target->type->examined = 1;
+		target_set_examined(target);
 	}
 	if ((retval=embeddedice_setup(target))!=ERROR_OK)
 		return retval;
diff --git a/src/target/cortex_a8.c b/src/target/cortex_a8.c
index 1e36f336b..e02545fb2 100644
--- a/src/target/cortex_a8.c
+++ b/src/target/cortex_a8.c
@@ -168,7 +168,7 @@ int cortex_a8_write_memory(struct target_s *target, u32 address, u32 size, u32 c
 int cortex_a8_handle_target_request(void *priv)
 {
 	target_t *target = priv;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 		return ERROR_OK;
 	armv7m_common_t *armv7m = target->arch_info;
 	swjdp_common_t *swjdp = &armv7m->swjdp_info;
diff --git a/src/target/cortex_m3.c b/src/target/cortex_m3.c
index ab217f98a..8a5983be0 100644
--- a/src/target/cortex_m3.c
+++ b/src/target/cortex_m3.c
@@ -1429,9 +1429,9 @@ int cortex_m3_examine(struct target_s *target)
 	if ((retval = ahbap_debugport_init(swjdp)) != ERROR_OK)
 		return retval;
 	
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
-		target->type->examined = 1;
+		target_set_examined(target);
 		
 		/* Read from Device Identification Registers */
 		if ((retval = target_read_u32(target, CPUID, &cpuid)) != ERROR_OK)
@@ -1526,7 +1526,7 @@ int cortex_m3_target_request_data(target_t *target, u32 size, u8 *buffer)
 int cortex_m3_handle_target_request(void *priv)
 {
 	target_t *target = priv;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 		return ERROR_OK;
 	armv7m_common_t *armv7m = target->arch_info;
 	swjdp_common_t *swjdp = &armv7m->swjdp_info;
diff --git a/src/target/mips32.c b/src/target/mips32.c
index 084e62768..9eb7b0c9a 100644
--- a/src/target/mips32.c
+++ b/src/target/mips32.c
@@ -346,9 +346,9 @@ int mips32_examine(struct target_s *target)
 {
 	mips32_common_t *mips32 = target->arch_info;
 	
-	if (!target->type->examined)
+	if (target_was_examined(target))
 	{
-		target->type->examined = 1;
+		target_set_examined(target);
 	
 		/* we will configure later */
 		mips32->bp_scanned = 0;
diff --git a/src/target/mips_m4k.c b/src/target/mips_m4k.c
index e9fcf43d1..539cc56bd 100644
--- a/src/target/mips_m4k.c
+++ b/src/target/mips_m4k.c
@@ -854,7 +854,7 @@ int mips_m4k_examine(struct target_s *target)
 	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
 	u32 idcode = 0;
 
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		mips_ejtag_get_idcode(ejtag_info, &idcode, NULL);
 		ejtag_info->idcode = idcode;
diff --git a/src/target/target.c b/src/target/target.c
index 71eb9d2b1..2209612cf 100644
--- a/src/target/target.c
+++ b/src/target/target.c
@@ -384,7 +384,7 @@ target_t* get_current_target(command_context_t *cmd_ctx)
 int target_poll(struct target_s *target)
 {
 	/* We can't poll until after examine */
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		/* Fail silently lest we pollute the log */
 		return ERROR_FAIL;
@@ -395,7 +395,7 @@ int target_poll(struct target_s *target)
 int target_halt(struct target_s *target)
 {
 	/* We can't poll until after examine */
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -408,7 +408,7 @@ int target_resume(struct target_s *target, int current, u32 address, int handle_
 	int retval;
 
 	/* We can't poll until after examine */
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -463,7 +463,7 @@ static int default_mmu(struct target_s *target, int *enabled)
 
 static int default_examine(struct target_s *target)
 {
-	target->type->examined = 1;
+	target_set_examined(target);
 	return ERROR_OK;
 }
 
@@ -487,7 +487,7 @@ int target_examine(void)
 
 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
 {
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -497,7 +497,7 @@ static int target_write_memory_imp(struct target_s *target, u32 address, u32 siz
 
 static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
 {
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -507,7 +507,7 @@ static int target_read_memory_imp(struct target_s *target, u32 address, u32 size
 
 static int target_soft_reset_halt_imp(struct target_s *target)
 {
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -517,7 +517,7 @@ static int target_soft_reset_halt_imp(struct target_s *target)
 
 static int target_run_algorithm_imp(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_param, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info)
 {
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -548,6 +548,22 @@ int target_run_algorithm(struct target_s *target,
 			entry_point, exit_point, timeout_ms, arch_info);
 }
 
+/// @returns @c true if the target has been examined.
+bool target_was_examined(struct target_s *target)
+{
+	return target->type->examined;
+}
+/// Sets the @c examined flag for the given target.
+void target_set_examined(struct target_s *target)
+{
+	target->type->examined = true;
+}
+// Reset the @c examined flag for the given target.
+void target_reset_examined(struct target_s *target)
+{
+	target->type->examined = false;
+}
+
 
 int target_init(struct command_context_s *cmd_ctx)
 {
@@ -556,7 +572,7 @@ int target_init(struct command_context_s *cmd_ctx)
 
 	while (target)
 	{
-		target->type->examined = 0;
+		target_reset_examined(target);
 		if (target->type->examine == NULL)
 		{
 			target->type->examine = default_examine;
@@ -1003,7 +1019,7 @@ int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buff
 	int retval;
 	LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
 
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1082,7 +1098,7 @@ int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffe
 	int retval;
 	LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
 
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1149,7 +1165,7 @@ int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32*
 	int retval;
 	u32 i;
 	u32 checksum = 0;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1191,7 +1207,7 @@ int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32*
 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
 {
 	int retval;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1208,7 +1224,7 @@ int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u3
 int target_read_u32(struct target_s *target, u32 address, u32 *value)
 {
 	u8 value_buf[4];
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1233,7 +1249,7 @@ int target_read_u32(struct target_s *target, u32 address, u32 *value)
 int target_read_u16(struct target_s *target, u32 address, u16 *value)
 {
 	u8 value_buf[2];
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1258,7 +1274,7 @@ int target_read_u16(struct target_s *target, u32 address, u16 *value)
 int target_read_u8(struct target_s *target, u32 address, u8 *value)
 {
 	int retval = target_read_memory(target, address, 1, 1, value);
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1281,7 +1297,7 @@ int target_write_u32(struct target_s *target, u32 address, u32 value)
 {
 	int retval;
 	u8 value_buf[4];
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1302,7 +1318,7 @@ int target_write_u16(struct target_s *target, u32 address, u16 value)
 {
 	int retval;
 	u8 value_buf[2];
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -1322,7 +1338,7 @@ int target_write_u16(struct target_s *target, u32 address, u16 value)
 int target_write_u8(struct target_s *target, u32 address, u8 value)
 {
 	int retval;
-	if (!target->type->examined)
+	if (!target_was_examined(target))
 	{
 		LOG_ERROR("Target not examined yet");
 		return ERROR_FAIL;
@@ -3682,7 +3698,7 @@ static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
 			Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
 			return JIM_ERR;
 		}
-		if( !(target->type->examined) ){
+		if( !(target_was_examined(target)) ){
 			e = ERROR_TARGET_NOT_EXAMINED;
 		} else {
 			e = target->type->poll( target );
diff --git a/src/target/target.h b/src/target/target.h
index c319a25b2..662c95b94 100644
--- a/src/target/target.h
+++ b/src/target/target.h
@@ -109,6 +109,12 @@ typedef struct target_type_s
 {
 	char *name;
 
+	/**
+	 * Indicates whether this target has been examined.
+	 *
+	 * Do @b not access this field directly, use target_was_examined()
+	 * target_set_examined(), and target_reset_examined().
+	 */
 	int examined;
 
 	/* poll current target status */
@@ -383,6 +389,13 @@ extern target_t* get_current_target(struct command_context_s *cmd_ctx);
 extern int get_num_by_target(target_t *query_target);
 extern target_t *get_target(const char *id);
 
+/// @returns @c true if the target has been examined.
+extern bool target_was_examined(struct target_s *target);
+/// Sets the @c examined flag for the given target.
+extern void target_set_examined(struct target_s *target);
+/// Reset the @c examined flag for the given target.
+extern void target_reset_examined(struct target_s *target);
+
 /**
  * Run an algorithm on the @a target given.
  *
-- 
GitLab