From 71c6c18189be9b7bf995d6e71b7ec5eb88161823 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周日, 24 4月 2016 23:18:58 +0800 Subject: [PATCH] Add support for array parameters represened as text (#52) --- libscpi/src/parser.c | 99 ++++++++++++++++++++++++ libscpi/inc/scpi/parser.h | 19 +++- libscpi/test/test_parser.c | 85 +++++++++++++++++++++ 3 files changed, 194 insertions(+), 9 deletions(-) diff --git a/libscpi/inc/scpi/parser.h b/libscpi/inc/scpi/parser.h index 2f00c82..ec60411 100644 --- a/libscpi/inc/scpi/parser.h +++ b/libscpi/inc/scpi/parser.h @@ -35,12 +35,12 @@ */ #ifndef SCPI_PARSER_H -#define SCPI_PARSER_H +#define SCPI_PARSER_H #include <string.h> #include "scpi/types.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif void SCPI_Init(scpi_t * context, @@ -48,7 +48,7 @@ scpi_interface_t * interface, const scpi_unit_def_t * units, const char * idn1, const char * idn2, const char * idn3, const char * idn4, - char * input_buffer, size_t input_buffer_length, + char * input_buffer, size_t input_buffer_length, scpi_error_t * error_queue_data, int16_t error_queue_size); #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE void SCPI_InitHeap(scpi_t * context, char * error_info_heap, size_t error_info_heap_length); @@ -74,7 +74,7 @@ size_t SCPI_ResultFloat(scpi_t * context, float val); size_t SCPI_ResultDouble(scpi_t * context, double val); size_t SCPI_ResultText(scpi_t * context, const char * data); - size_t SCPI_ResultError(scpi_t * context, scpi_error_t * error); + size_t SCPI_ResultError(scpi_t * context, scpi_error_t * error); size_t SCPI_ResultArbitraryBlock(scpi_t * context, const void * data, size_t len); size_t SCPI_ResultArbitraryBlockHeader(scpi_t * context, size_t len); size_t SCPI_ResultArbitraryBlockData(scpi_t * context, const void * data, size_t len); @@ -117,6 +117,13 @@ scpi_bool_t SCPI_ParamBool(scpi_t * context, scpi_bool_t * value, scpi_bool_t mandatory); scpi_bool_t SCPI_ParamChoice(scpi_t * context, const scpi_choice_def_t * options, int32_t * value, scpi_bool_t mandatory); + scpi_bool_t SCPI_ParamArrayInt32(scpi_t * context, int32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory); + scpi_bool_t SCPI_ParamArrayUInt32(scpi_t * context, uint32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory); + scpi_bool_t SCPI_ParamArrayInt64(scpi_t * context, int64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory); + scpi_bool_t SCPI_ParamArrayUInt64(scpi_t * context, uint64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory); + scpi_bool_t SCPI_ParamArrayFloat(scpi_t * context, float *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory); + scpi_bool_t SCPI_ParamArrayDouble(scpi_t * context, double *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory); + scpi_bool_t SCPI_IsCmd(scpi_t * context, const char * cmd); #if USE_COMMAND_TAGS int32_t SCPI_CmdTag(scpi_t * context); @@ -134,9 +141,9 @@ #define SCPI_ParamUnsignedInt(context, value, mandatory) SCPI_ParamUInt32((context), (value), (mandatory)) #endif /* USE_DEPRECATED_FUNCTIONS */ -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#endif /* SCPI_PARSER_H */ +#endif /* SCPI_PARSER_H */ diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c index db6f5df..114fe3a 100644 --- a/libscpi/src/parser.c +++ b/libscpi/src/parser.c @@ -222,7 +222,7 @@ /* place undefined header with error */ /* calculate length of errornouse header and trim \r\n */ size_t r2 = r; - while(r2 > 0 && (data[r2 - 1] == '\r' || data[r2 - 1] == '\n')) r2--; + while (r2 > 0 && (data[r2 - 1] == '\r' || data[r2 - 1] == '\n')) r2--; SCPI_ErrorPushEx(context, SCPI_ERROR_UNDEFINED_HEADER, data, r2); result = FALSE; } @@ -280,6 +280,7 @@ } #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE + /** * Initialize context's * @param context @@ -1545,7 +1546,7 @@ * @param format * @return */ -static size_t parserResultArrayBinary(scpi_t * context, const void * array, size_t count, size_t item_size, scpi_array_format_t format) { +static size_t produceResultArrayBinary(scpi_t * context, const void * array, size_t count, size_t item_size, scpi_array_format_t format) { if (SCPI_GetNativeFormat() == format) { switch (item_size) { @@ -1610,7 +1611,7 @@ result += func(context, array[i]);\ }\ } else {\ - result = parserResultArrayBinary(context, array, count, sizeof(*array), format);\ + result = produceResultArrayBinary(context, array, count, sizeof(*array), format);\ }\ return result;\ } while(0) @@ -1734,3 +1735,95 @@ size_t SCPI_ResultArrayDouble(scpi_t * context, const double * array, size_t count, scpi_array_format_t format) { RESULT_ARRAY(SCPI_ResultDouble); } + +/* + * Template macro to generate all SCPI_ParamArrayXYZ function + */ +#define PARAM_ARRAY_TEMPLATE(func) do{\ + if (format != SCPI_FORMAT_ASCII) return FALSE;\ + for (*o_count = 0; *o_count < i_count; (*o_count)++) {\ + if (!func(context, &data[*o_count], mandatory)) {\ + break;\ + }\ + mandatory = FALSE;\ + }\ + return mandatory ? FALSE : TRUE;\ +}while(0) + +/** + * Read list of values up to i_count + * @param context + * @param data - array to fill + * @param i_count - number of elements of data + * @param o_count - real number of filled elements + * @param mandatory + * @return TRUE on success + */ +scpi_bool_t SCPI_ParamArrayInt32(scpi_t * context, int32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) { + PARAM_ARRAY_TEMPLATE(SCPI_ParamInt32); +} + +/** + * Read list of values up to i_count + * @param context + * @param data - array to fill + * @param i_count - number of elements of data + * @param o_count - real number of filled elements + * @param mandatory + * @return TRUE on success + */ +scpi_bool_t SCPI_ParamArrayUInt32(scpi_t * context, uint32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) { + PARAM_ARRAY_TEMPLATE(SCPI_ParamUInt32); +} + +/** + * Read list of values up to i_count + * @param context + * @param data - array to fill + * @param i_count - number of elements of data + * @param o_count - real number of filled elements + * @param mandatory + * @return TRUE on success + */ +scpi_bool_t SCPI_ParamArrayInt64(scpi_t * context, int64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) { + PARAM_ARRAY_TEMPLATE(SCPI_ParamInt64); +} + +/** + * Read list of values up to i_count + * @param context + * @param data - array to fill + * @param i_count - number of elements of data + * @param o_count - real number of filled elements + * @param mandatory + * @return TRUE on success + */ +scpi_bool_t SCPI_ParamArrayUInt64(scpi_t * context, uint64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) { + PARAM_ARRAY_TEMPLATE(SCPI_ParamUInt64); +} + +/** + * Read list of values up to i_count + * @param context + * @param data - array to fill + * @param i_count - number of elements of data + * @param o_count - real number of filled elements + * @param mandatory + * @return TRUE on success + */ +scpi_bool_t SCPI_ParamArrayFloat(scpi_t * context, float *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) { + PARAM_ARRAY_TEMPLATE(SCPI_ParamFloat); +} + +/** + * Read list of values up to i_count + * @param context + * @param data - array to fill + * @param i_count - number of elements of data + * @param o_count - real number of filled elements + * @param mandatory + * @return TRUE on success + */ +scpi_bool_t SCPI_ParamArrayDouble(scpi_t * context, double *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) { + PARAM_ARRAY_TEMPLATE(SCPI_ParamDouble); +} diff --git a/libscpi/test/test_parser.c b/libscpi/test/test_parser.c index ad5194a..f2ff12f 100644 --- a/libscpi/test/test_parser.c +++ b/libscpi/test/test_parser.c @@ -1325,6 +1325,90 @@ TEST_Result(ArrayDoubleSWAPPED, double_arr, "#216" "\x40\x8c\x16\xd3\x66\x67\xd1\x42" "\x1c\xbc\x6e\xf2\x54\x8b\x11\x43"); } +#define _countof(a) (sizeof(a)/sizeof(*(a))) + +#define TEST_ParamArrayDouble(T, func, data, mandatory, _expected_value, expected_result, expected_error_code) \ +{ \ + T value[10]; \ + scpi_bool_t result; \ + scpi_error_t errCode; \ + T expected_value[] = {NOPAREN _expected_value}; \ + size_t o_count; \ + size_t i_count = _countof(expected_value); \ + \ + SCPI_CoreCls(&scpi_context); \ + scpi_context.input_count = 0; \ + scpi_context.param_list.lex_state.buffer = data; \ + scpi_context.param_list.lex_state.len = strlen(scpi_context.param_list.lex_state.buffer);\ + scpi_context.param_list.lex_state.pos = scpi_context.param_list.lex_state.buffer; \ + result = func(&scpi_context, value, 10, &o_count, SCPI_FORMAT_ASCII, mandatory); \ + \ + SCPI_ErrorPop(&scpi_context, &errCode); \ + CU_ASSERT_EQUAL(result, expected_result); \ + if (expected_result) { \ + CU_ASSERT_EQUAL(i_count, o_count); \ + size_t i; \ + for(i = 0; i < o_count; i++) { \ + CU_ASSERT_DOUBLE_EQUAL(value[i], expected_value[i], 0.000001); \ + } \ + } \ + CU_ASSERT_EQUAL(errCode.error_code, expected_error_code); \ +} + +#define TEST_ParamArrayInt(T, func, data, mandatory, _expected_value, expected_result, expected_error_code) \ +{ \ + T value[10]; \ + scpi_bool_t result; \ + scpi_error_t errCode; \ + T expected_value[] = {NOPAREN _expected_value}; \ + size_t o_count; \ + size_t i_count = _countof(expected_value); \ + \ + SCPI_CoreCls(&scpi_context); \ + scpi_context.input_count = 0; \ + scpi_context.param_list.lex_state.buffer = data; \ + scpi_context.param_list.lex_state.len = strlen(scpi_context.param_list.lex_state.buffer);\ + scpi_context.param_list.lex_state.pos = scpi_context.param_list.lex_state.buffer; \ + result = func(&scpi_context, value, 10, &o_count, SCPI_FORMAT_ASCII, mandatory); \ + \ + SCPI_ErrorPop(&scpi_context, &errCode); \ + CU_ASSERT_EQUAL(result, expected_result); \ + if (expected_result) { \ + CU_ASSERT_EQUAL(i_count, o_count); \ + size_t i; \ + for(i = 0; i < o_count; i++) { \ + CU_ASSERT_EQUAL(value[i], expected_value[i]); \ + } \ + } \ + CU_ASSERT_EQUAL(errCode.error_code, expected_error_code); \ +} + +static void testParamArray(void) { + TEST_ParamArrayDouble(double, SCPI_ParamArrayDouble, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR); + TEST_ParamArrayDouble(double, SCPI_ParamArrayDouble, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER); + TEST_ParamArrayDouble(double, SCPI_ParamArrayDouble, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR); + + TEST_ParamArrayDouble(float, SCPI_ParamArrayFloat, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR); + TEST_ParamArrayDouble(float, SCPI_ParamArrayFloat, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER); + TEST_ParamArrayDouble(float, SCPI_ParamArrayFloat, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR); + + TEST_ParamArrayInt(int32_t, SCPI_ParamArrayInt32, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR); + TEST_ParamArrayInt(int32_t, SCPI_ParamArrayInt32, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER); + TEST_ParamArrayInt(int32_t, SCPI_ParamArrayInt32, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR); + + TEST_ParamArrayInt(uint32_t, SCPI_ParamArrayUInt32, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR); + TEST_ParamArrayInt(uint32_t, SCPI_ParamArrayUInt32, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER); + TEST_ParamArrayInt(uint32_t, SCPI_ParamArrayUInt32, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR); + + TEST_ParamArrayInt(int64_t, SCPI_ParamArrayInt64, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR); + TEST_ParamArrayInt(int64_t, SCPI_ParamArrayInt64, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER); + TEST_ParamArrayInt(int64_t, SCPI_ParamArrayInt64, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR); + + TEST_ParamArrayInt(uint64_t, SCPI_ParamArrayUInt64, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR); + TEST_ParamArrayInt(uint64_t, SCPI_ParamArrayUInt64, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER); + TEST_ParamArrayInt(uint64_t, SCPI_ParamArrayUInt64, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR); +} + static void testNumberToStr(void) { #define TEST_SCPI_NumberToStr(_special, _value, _unit, expected_result) do {\ @@ -1551,6 +1635,7 @@ || (NULL == CU_add_test(pSuite, "SCPI_ResultText", testResultText)) || (NULL == CU_add_test(pSuite, "SCPI_ResultArbitraryBlock", testResultArbitraryBlock)) || (NULL == CU_add_test(pSuite, "SCPI_ResultArray", testResultArray)) + || (NULL == CU_add_test(pSuite, "SCPI_ParamArray", testParamArray)) || (NULL == CU_add_test(pSuite, "SCPI_NumberToStr", testNumberToStr)) || (NULL == CU_add_test(pSuite, "SCPI_ErrorQueue", testErrorQueue)) || (NULL == CU_add_test(pSuite, "Incomplete arbitrary parameter", testIncompleteArbitraryParameter)) -- Gitblit v1.9.1