Skip to content

Commit

Permalink
The basic implementation completed. Tests are needed. (issue j123b567#73
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cdwork committed Mar 1, 2016
1 parent 508dece commit 34d681a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 84 deletions.
40 changes: 20 additions & 20 deletions libscpi/inc/scpi/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ extern "C" {
#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


Expand Down Expand Up @@ -259,23 +259,23 @@ extern "C" {
#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 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)
#define SCPIDEFINE_DESCRIPTION_MAX_PARTS 1
#define SCPIDEFINE_strdup(h, s) NULL
#define SCPIDEFINE_free(h, s, r) (void)
#endif

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion libscpi/inc/scpi/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern "C" {
/* 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
}
Expand Down
3 changes: 1 addition & 2 deletions libscpi/inc/scpi/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ extern "C" {

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);
Expand Down
8 changes: 4 additions & 4 deletions libscpi/inc/scpi/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ extern "C" {
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;
Expand Down
38 changes: 4 additions & 34 deletions libscpi/src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ void SCPI_ErrorClear(scpi_t * context) {
* @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);
Expand All @@ -108,20 +108,6 @@ scpi_bool_t SCPI_ErrorPopEx(scpi_t * context, scpi_error_t * error) {

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;
}

/**
* Return number of errors/events in the queue
Expand Down Expand Up @@ -191,7 +177,9 @@ void SCPI_ErrorPushEx(scpi_t * context, int16_t err, char * info) {
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) {
Expand All @@ -207,24 +195,6 @@ void SCPI_ErrorPushEx(scpi_t * context, int16_t err, char * info) {
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;
//}
}

/**
Expand Down
7 changes: 1 addition & 6 deletions libscpi/src/minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,10 @@ scpi_result_t SCPI_SystemVersionQ(scpi_t * context) {
* @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;

}

/**
Expand Down
27 changes: 16 additions & 11 deletions libscpi/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ scpi_bool_t SCPI_Parse(scpi_t * context, char * data, int len) {
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;
}
}
Expand Down Expand Up @@ -511,15 +512,19 @@ size_t SCPI_ResultText(scpi_t * context, const char * data) {


/**
* 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;

Expand All @@ -531,11 +536,11 @@ size_t SCPI_ResultError(scpi_t * context, scpi_error_t * error) {

#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);
Expand Down
19 changes: 13 additions & 6 deletions libscpi/src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,16 @@ char * OUR_strdup(scpi_error_info_heap_t * heap, const char *s) {
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);
Expand Down Expand Up @@ -839,7 +841,6 @@ void OUR_free(scpi_error_info_heap_t * heap, const char * s, scpi_bool_t rollbac

if( !OUR_get_parts( heap, s, &len[0], &data_add, &len[1] ) ) return;


if(data_add) {
len[1]++;
memset(data_add,0,len[1]);
Expand All @@ -849,10 +850,16 @@ void OUR_free(scpi_error_info_heap_t * heap, const char * s, scpi_bool_t rollbac
}
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;
}
}

Expand Down

0 comments on commit 34d681a

Please sign in to comment.