From 34d681a00a7fc3c8d82a5ba3082dd5524f2a8389 Mon Sep 17 00:00:00 2001
From: Chernov Dmitriy <cd_work@mail.ru>
Date: 周二, 01 3月 2016 21:26:22 +0800
Subject: [PATCH] The basic implementation completed. Tests are needed. (issue #73)

---
 libscpi/inc/scpi/error.h     |    3 
 libscpi/inc/scpi/types.h     |    8 +-
 libscpi/src/minimal.c        |    7 --
 libscpi/src/parser.c         |   27 +++++---
 libscpi/inc/scpi/config.h    |   40 ++++++------
 libscpi/src/error.c          |   38 +-----------
 libscpi/inc/scpi/constants.h |    2 
 libscpi/src/utils.c          |   19 ++++--
 8 files changed, 60 insertions(+), 84 deletions(-)

diff --git a/libscpi/inc/scpi/config.h b/libscpi/inc/scpi/config.h
index ea19d8e..85cd07f 100644
--- a/libscpi/inc/scpi/config.h
+++ b/libscpi/inc/scpi/config.h
@@ -90,10 +90,10 @@
 #endif
 
 #ifndef USE_DEVICE_DEPENDENT_ERROR_INFORMATION
-#define USE_DEVICE_DEPENDENT_ERROR_INFORMATION 1
-	#ifndef USE_MEMORY_ALLOCATION_FREE
-	#define USE_MEMORY_ALLOCATION_FREE 1
-	#endif
+#define USE_DEVICE_DEPENDENT_ERROR_INFORMATION 0
+#ifndef USE_MEMORY_ALLOCATION_FREE
+#define USE_MEMORY_ALLOCATION_FREE 1
+#endif
 #endif
 
 
@@ -259,23 +259,23 @@
 #endif
 
 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION
-	#if USE_MEMORY_ALLOCATION_FREE
-		#include <stdlib.h>
-		#include <string.h>
-		#include <malloc.h>
-		#define SCPIDEFINE_DESCRIPTION_MAX_PARTS	2
-		#define SCPIDEFINE_strdup(h, s)				strdup((s))
-		#define SCPIDEFINE_free(h, s, r)			free((s))
-	#else
-		#define SCPIDEFINE_DESCRIPTION_MAX_PARTS	3
-		#define SCPIDEFINE_strdup(h, s)				OUR_strdup((h), (s))
-		#define SCPIDEFINE_free(h, s, r)			OUR_free((h), (s), (r))
-		#define SCPIDEFINE_get_parts(h, s, l1, s2, l2)	OUR_get_parts((h), (s), (l1), (s2), (l2))
-	#endif
+
+#if USE_MEMORY_ALLOCATION_FREE
+#include <stdlib.h>
+#include <string.h>
+#define SCPIDEFINE_DESCRIPTION_MAX_PARTS		2
+#define SCPIDEFINE_strdup(h, s)					strdup((s))
+#define SCPIDEFINE_free(h, s, r)				free((s))
 #else
-	#define SCPIDEFINE_DESCRIPTION_MAX_PARTS	1
-	#define SCPIDEFINE_strdup(h, s)				NULL
-	#define SCPIDEFINE_free(h, s, r)			(void)
+#define SCPIDEFINE_DESCRIPTION_MAX_PARTS		3
+#define SCPIDEFINE_strdup(h, s)					OUR_strdup((h), (s))
+#define SCPIDEFINE_free(h, s, r)				OUR_free((h), (s), (r))
+#define SCPIDEFINE_get_parts(h, s, l1, s2, l2)	OUR_get_parts((h), (s), (l1), (s2), (l2))
+#endif
+#else
+#define SCPIDEFINE_DESCRIPTION_MAX_PARTS		1
+#define SCPIDEFINE_strdup(h, s)					NULL
+#define SCPIDEFINE_free(h, s, r)				(void)
 #endif
 
 #ifdef	__cplusplus
diff --git a/libscpi/inc/scpi/constants.h b/libscpi/inc/scpi/constants.h
index e2ad2d0..ecbfb55 100644
--- a/libscpi/inc/scpi/constants.h
+++ b/libscpi/inc/scpi/constants.h
@@ -51,7 +51,7 @@
 /* 21.8 :ERRor Subsystem
  * The maximum string length of <Error/event_description> plus <Device-dependent_info> is 255 characters.
  */
-#define SCPI_STD_ERROR_DESC_CHARS_LIMIT 255
+#define SCPI_STD_ERROR_DESC_MAX_STRING_LENGTH 255
 
 #ifdef	__cplusplus
 }
diff --git a/libscpi/inc/scpi/error.h b/libscpi/inc/scpi/error.h
index 36c8986..bc446d6 100644
--- a/libscpi/inc/scpi/error.h
+++ b/libscpi/inc/scpi/error.h
@@ -46,8 +46,7 @@
 
     void SCPI_ErrorInit(scpi_t * context, scpi_error_t * data, int16_t size);
     void SCPI_ErrorClear(scpi_t * context);
-	scpi_bool_t SCPI_ErrorPopEx(scpi_t * context, scpi_error_t * error);
-    int16_t SCPI_ErrorPop(scpi_t * context);
+	scpi_bool_t SCPI_ErrorPop(scpi_t * context, scpi_error_t * error);
 	void SCPI_ErrorPushEx(scpi_t * context, int16_t err, char * info);
     void SCPI_ErrorPush(scpi_t * context, int16_t err);
     int32_t SCPI_ErrorCount(scpi_t * context);
diff --git a/libscpi/inc/scpi/types.h b/libscpi/inc/scpi/types.h
index 085ca00..0468624 100644
--- a/libscpi/inc/scpi/types.h
+++ b/libscpi/inc/scpi/types.h
@@ -202,10 +202,10 @@
     typedef scpi_result_t(*scpi_command_callback_t)(scpi_t *);
 
 	struct _scpi_error_info_heap_t {
-        int16_t wr;
-        //int16_t rd;
-        int16_t count;
-        int16_t size;
+        size_t wr;
+        //size_t rd;
+		size_t count;
+        size_t size;
         char * data;
 	};
 	typedef struct _scpi_error_info_heap_t scpi_error_info_heap_t;
diff --git a/libscpi/src/error.c b/libscpi/src/error.c
index 7b17f4c..d1307a4 100644
--- a/libscpi/src/error.c
+++ b/libscpi/src/error.c
@@ -98,8 +98,8 @@
  * @param error
  * @return 
  */
-scpi_bool_t SCPI_ErrorPopEx(scpi_t * context, scpi_error_t * error) {
-	if(!error) return FALSE;
+scpi_bool_t SCPI_ErrorPop(scpi_t * context, scpi_error_t * error) {
+	if(!error || !context) return FALSE;
 	error->error_code = 0;
 	error->device_dependent_info = NULL;
 	fifo_remove(&context->error_queue, error);
@@ -107,20 +107,6 @@
 	SCPI_ErrorEmitEmpty(context);
 
 	return TRUE;
-}
-/**
- * Pop error from queue
- * @param context - scpi context
- * @return error number
- */
-int16_t SCPI_ErrorPop(scpi_t * context) {
-    int16_t result = 0;
-
-    fifo_remove(&context->error_queue, &result);
-
-    SCPI_ErrorEmitEmpty(context);
-
-    return result;
 }
 
 /**
@@ -191,7 +177,9 @@
 	SCPI_ErrorEmit(context, err);
 	if (queue_overflow) {
 		SCPI_ErrorEmit(context, SCPI_ERROR_QUEUE_OVERFLOW);
+#if USE_DEVICE_DEPENDENT_ERROR_INFORMATION		
 		SCPIDEFINE_free(&context->error_info_heap, info_ptr, true);
+#endif
 	}
 
 	if (context) {
@@ -207,24 +195,6 @@
 void SCPI_ErrorPush(scpi_t * context, int16_t err) {
 	SCPI_ErrorPushEx(context, err, NULL);
 	return;
-    //int i;
-//
-    //scpi_bool_t queue_overflow = !SCPI_ErrorAddInternal(context, err, NULL);
-//
-    //for (i = 0; i < ERROR_DEFS_N; i++) {
-        //if ((err <= errs[i].from) && (err >= errs[i].to)) {
-            //SCPI_RegSetBits(context, SCPI_REG_ESR, errs[i].bit);
-        //}
-    //}
-//
-    //SCPI_ErrorEmit(context, err);
-    //if (queue_overflow) {
-        //SCPI_ErrorEmit(context, SCPI_ERROR_QUEUE_OVERFLOW);
-    //}
-//
-    //if (context) {
-        //context->cmd_error = TRUE;
-    //}
 }
 
 /**
diff --git a/libscpi/src/minimal.c b/libscpi/src/minimal.c
index f2af219..8abd39d 100644
--- a/libscpi/src/minimal.c
+++ b/libscpi/src/minimal.c
@@ -77,15 +77,10 @@
  * @return 
  */
 scpi_result_t SCPI_SystemErrorNextQ(scpi_t * context) {
-	SCPI_ResultInt32(context,context->error_info_heap.count);
-	
 	scpi_error_t error;	
-	SCPI_ErrorPopEx(context, &error);
+	SCPI_ErrorPop(context, &error);
 	SCPI_ResultError(context, &error);
-	
-	SCPI_ResultInt32(context,context->error_info_heap.count);
 	return SCPI_RES_OK;
-
 }
 
 /**
diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index fd85e47..00cba78 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -219,10 +219,11 @@
                 result &= processCommand(context);
                 cmd_prev = state->programHeader;
             } else {
-                //SCPI_ErrorPush(context, SCPI_ERROR_UNDEFINED_HEADER);
 				/* test */
-				data[r-1]=0;
+				/* place undefined header with error */
+				data[r?(r-1):r]=0;
 				SCPI_ErrorPushEx(context, SCPI_ERROR_UNDEFINED_HEADER, data);
+                //SCPI_ErrorPush(context, SCPI_ERROR_UNDEFINED_HEADER);				
                 result = FALSE;
             }
         }
@@ -511,15 +512,19 @@
 
 
 /**
- * Write string withn " to the result
+ * SCPI-99:21.8 Device-dependent error information.
+ * Write error information with the following syntax:
+ * <Error/event_number>,"<Error/event_description>[;<Device-dependent_info>]"
+ * The maximum string length of <Error/event_description> plus <Device-dependent_info>
+ * is SCPI_STD_ERROR_DESC_MAX_STRING_LENGTH (255) characters.
+ *
  * @param context
- * @param data
+ * @param error
  * @return
  */
-static size_t outputlimit_0=0;
 size_t SCPI_ResultError(scpi_t * context, scpi_error_t * error) {
  	size_t result = 0;
-	size_t outputlimit = outputlimit_0++; //SCPI_STD_ERROR_DESC_CHARS_LIMIT;
+	size_t outputlimit = SCPI_STD_ERROR_DESC_MAX_STRING_LENGTH;
 	size_t step = 0;
 	const char * quote;
 
@@ -531,11 +536,11 @@
 	
 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION
 	data[1] = error->device_dependent_info;
-	#if USE_MEMORY_ALLOCATION_FREE
-		len[1] = error->device_dependent_info ? strlen(data[1]) : 0;
-	#else
-		SCPIDEFINE_get_parts(&context->error_info_heap, data[1], &len[1], &data[2], &len[2]);
-	#endif
+#if USE_MEMORY_ALLOCATION_FREE
+	len[1] = error->device_dependent_info ? strlen(data[1]) : 0;
+#else
+	SCPIDEFINE_get_parts(&context->error_info_heap, data[1], &len[1], &data[2], &len[2]);
+#endif
 #endif
 
 	result += SCPI_ResultInt32(context, error->error_code);
diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index de8fb64..b2b9e03 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -763,14 +763,16 @@
 	if(!s || !heap) {
 		return NULL;
 	}
+
 	if(heap->data[heap->wr]!='\0'){
 		return NULL;
 	}
 	
-	size_t len=strlen(s) + 1; // additional '\0' at end
-	if(len > heap->count){
+	size_t len=strlen(s);
+	if( ( len == 0 ) || ( len > heap->count ) ){
 		return NULL;
 	}
+	len++;	// additional '\0' at end
 	char * ptrs = s;
 	char * head = &heap->data[heap->wr];
 	size_t rem = heap->size - (&heap->data[heap->wr]-heap->data);
@@ -839,7 +841,6 @@
 		
 	if( !OUR_get_parts( heap, s, &len[0], &data_add, &len[1] ) ) return;
 	
-	
 	if(data_add) {
 		len[1]++;
 		memset(data_add,0,len[1]);
@@ -849,10 +850,16 @@
 	}
 	memset(s,0,len[0]);
 	heap->count += len[0];
+	if( heap->count == heap->size){
+		heap->wr = 0;
+		return;
+	}
 	if(rollback){
-		heap->wr-=len[0];
-		heap->wr-=len[1];
-		if(heap->wr < 0)heap->wr += heap->size;
+		size_t rb = len[0] + len[1];
+		if( rb > heap->wr){
+			heap->wr += heap->size; 
+		}
+		heap->wr -= rb;
 	}
 }
 

--
Gitblit v1.9.1