examples/test-LwIP-netconn/scpi_server.c | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
examples/test-LwIP-netconn/scpi_server.h | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
libscpi/inc/scpi/ieee488.h | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
libscpi/inc/scpi/types.h | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
libscpi/src/ieee488.c | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
libscpi/src/parser.c | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
libscpi/src/utils.c | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
libscpi/src/utils.h | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
examples/test-LwIP-netconn/scpi_server.c
@@ -54,6 +54,15 @@ #define SCPI_THREAD_PRIO (tskIDLE_PRIORITY + 2) #define SCPI_MSG_TIMEOUT 0 #define SCPI_MSG_TEST 1 #define SCPI_MSG_IO_LISTEN 2 #define SCPI_MSG_CONTROL_IO_LISTEN 3 #define SCPI_MSG_IO 4 #define SCPI_MSG_CONTROL_IO 5 #define SCPI_MSG_SET_ESE_REQ 6 #define SCPI_MSG_SET_ERROR 7 typedef struct { struct netconn *io_listen; struct netconn *control_io_listen; @@ -64,14 +73,22 @@ //fd_set fds; } user_data_t; struct _queue_event_t { uint8_t cmd; uint8_t param1; int16_t param2; } __attribute__ ((__packed__)); typedef struct _queue_event_t queue_event_t; user_data_t user_data = { .io_listen = NULL, .io = NULL, .io = NULL, .control_io_listen = NULL, .control_io = NULL, .evtQueue = 0, }; }; size_t SCPI_Write(scpi_t * context, const char * data, size_t len) { if (context->user_context != NULL) { @@ -83,7 +100,7 @@ return 0; } scpi_result_t SCPI_Flush(scpi_t * context) { scpi_result_t SCPI_Flush(scpi_t * context) { if (context->user_context != NULL) { user_data_t * u = (user_data_t *)(context->user_context); if (u->io) { @@ -98,9 +115,16 @@ (void) context; // BEEP iprintf("**ERROR: %ld, \"%s\"\r\n", (int32_t) err, SCPI_ErrorTranslate(err)); if (err != 0) { /* New error */ /* Beep */ /* Error LED ON */ } else { /* No more errors in the queue */ /* Error LED OFF */ } return 0; } scpi_result_t SCPI_Control(scpi_t * context, scpi_ctrl_name_t ctrl, scpi_reg_val_t val) { char b[16]; @@ -117,7 +141,7 @@ snprintf(b, sizeof(b), "SRQ%d\r\n", val); return netconn_write(u->control_io, b, strlen(b), NETCONN_NOCOPY) == ERR_OK ? SCPI_RES_OK : SCPI_RES_ERR; } } } return SCPI_RES_OK; } @@ -139,27 +163,50 @@ } #define SCPI_MSG_TEST 1 #define SCPI_MSG_IO_LISTEN 2 #define SCPI_MSG_CONTROL_IO_LISTEN 3 #define SCPI_MSG_IO 4 #define SCPI_MSG_CONTROL_IO 5 static void setEseReq(void) { SCPI_RegSetBits(&scpi_context, SCPI_REG_ESR, ESR_REQ); } static void setError(int16_t err) { SCPI_ErrorPush(&scpi_context, err); } void SCPI_RequestControl(void) { queue_event_t msg; msg.cmd = SCPI_MSG_SET_ESE_REQ; /* Avoid sending evtQueue message if ESR_REQ is already set if((SCPI_RegGet(&scpi_context, SCPI_REG_ESR) & ESR_REQ) == 0) { xQueueSend(user_data.evtQueue, &msg, 1000); } */ xQueueSend(user_data.evtQueue, &msg, 1000); } void SCPI_AddError(int16_t err) { queue_event_t msg; msg.cmd = SCPI_MSG_SET_ERROR; msg.param2 = err; xQueueSend(user_data.evtQueue, &msg, 1000); } void scpi_netconn_callback(struct netconn * conn, enum netconn_evt evt, u16_t len) { uint32_t msg; queue_event_t msg; (void) len; if (evt == NETCONN_EVT_RCVPLUS) { msg = SCPI_MSG_TEST; msg.cmd = SCPI_MSG_TEST; if (conn == user_data.io) { msg = SCPI_MSG_IO; msg.cmd = SCPI_MSG_IO; } else if (conn == user_data.io_listen) { msg = SCPI_MSG_IO_LISTEN; msg.cmd = SCPI_MSG_IO_LISTEN; } else if (conn == user_data.control_io) { msg = SCPI_MSG_CONTROL_IO; msg.cmd = SCPI_MSG_CONTROL_IO; } else if (conn == user_data.control_io_listen) { msg = SCPI_MSG_CONTROL_IO_LISTEN; msg.cmd = SCPI_MSG_CONTROL_IO_LISTEN; } xQueueSend(user_data.evtQueue, &msg, 1000); } @@ -168,40 +215,34 @@ static struct netconn * createServer(int port) { struct netconn * conn; err_t err; conn = netconn_new_with_callback(NETCONN_TCP, scpi_netconn_callback); if (conn == NULL) { return NULL; } err = netconn_bind(conn, NULL, port); if (err != ERR_OK) { netconn_delete(conn); return NULL; } netconn_listen(conn); return conn; } static int waitServer(user_data_t * user_data) { uint32_t rc; static void waitServer(user_data_t * user_data, queue_event_t * evt) { /* 5s timeout */ if (xQueueReceive(user_data->evtQueue, &rc, 5000 * portTICK_RATE_MS) != pdPASS) { rc = 0; if (xQueueReceive(user_data->evtQueue, evt, 5000 * portTICK_RATE_MS) != pdPASS) { evt->cmd = SCPI_MSG_TIMEOUT; } return rc; } static int processIoListen(user_data_t * user_data) { struct netconn *newconn; if (netconn_accept(user_data->io_listen, &newconn) == ERR_OK) { if (user_data->io) { // Close unwanted connection @@ -212,14 +253,14 @@ iprintf("***Connection established %s\r\n", inet_ntoa(newconn->pcb.ip->remote_ip)); user_data->io = newconn; } } } return 0; } static int processSrqIoListen(user_data_t * user_data) { struct netconn *newconn; if (netconn_accept(user_data->control_io_listen, &newconn) == ERR_OK) { if (user_data->control_io) { netconn_close(newconn); @@ -229,8 +270,8 @@ iprintf("***Control Connection established %s\r\n", inet_ntoa(newconn->pcb.ip->remote_ip)); user_data->control_io = newconn; } } } return 0; } @@ -247,13 +288,13 @@ netconn_close(user_data->control_io); netconn_delete(user_data->control_io); user_data->control_io = NULL; iprintf("***Control Connection closed\r\n"); iprintf("***Control Connection closed\r\n"); } static int processIo(user_data_t * user_data) { struct netbuf *inbuf; char* buf; u16_t buflen; u16_t buflen; if (netconn_recv(user_data->io, &inbuf) != ERR_OK) { goto fail1; @@ -261,7 +302,7 @@ if (netconn_err(user_data->io) != ERR_OK) { goto fail2; } netbuf_data(inbuf, (void**) &buf, &buflen); if (buflen > 0) { @@ -269,23 +310,23 @@ } else { //goto fail2; } netbuf_delete(inbuf); return 0; fail2: netbuf_delete(inbuf); fail1: closeIo(user_data); return 0; } static int processSrqIo(user_data_t * user_data) { struct netbuf *inbuf; char* buf; u16_t buflen; u16_t buflen; if (netconn_recv(user_data->control_io, &inbuf) != ERR_OK) { goto fail1; @@ -293,7 +334,7 @@ if (netconn_err(user_data->control_io) != ERR_OK) { goto fail2; } netbuf_data(inbuf, (void**) &buf, &buflen); if (buflen > 0) { @@ -301,16 +342,16 @@ } else { //goto fail2; } netbuf_delete(inbuf); return 0; fail2: netbuf_delete(inbuf); fail1: closeSrqIo(user_data); return 0; } @@ -318,50 +359,53 @@ * */ static void scpi_server_thread(void *arg) { int rc; queue_event_t evt; (void)arg; user_data.evtQueue = xQueueCreate(10, sizeof(uint32_t)); user_data.evtQueue = xQueueCreate(10, sizeof(queue_event_t)); // user_context will be pointer to socket scpi_context.user_context = &user_data; SCPI_Init(&scpi_context); user_data.io_listen = createServer(DEVICE_PORT); user_data.control_io_listen = createServer(CONTROL_PORT); while(1) { rc = waitServer(&user_data); if (rc < 0) { // failed iprintf("select failed"); break; waitServer(&user_data, &evt); if (evt.cmd == SCPI_MSG_TIMEOUT) { // timeout SCPI_Input(&scpi_context, NULL, 0); } if (rc == 0) { // timeout SCPI_Input(&scpi_context, NULL, 0); } if ((user_data.io_listen != NULL) && (rc == SCPI_MSG_IO_LISTEN)) { if ((user_data.io_listen != NULL) && (evt.cmd == SCPI_MSG_IO_LISTEN)) { processIoListen(&user_data); } if ((user_data.control_io_listen != NULL) && (rc == SCPI_MSG_CONTROL_IO_LISTEN)) { if ((user_data.control_io_listen != NULL) && (evt.cmd == SCPI_MSG_CONTROL_IO_LISTEN)) { processSrqIoListen(&user_data); } if ((user_data.io != NULL) && (rc == SCPI_MSG_IO)) { if ((user_data.io != NULL) && (evt.cmd == SCPI_MSG_IO)) { processIo(&user_data); } if ((user_data.control_io != NULL) && (rc == SCPI_MSG_CONTROL_IO)) { if ((user_data.control_io != NULL) && (evt.cmd == SCPI_MSG_CONTROL_IO)) { processSrqIo(&user_data); } if (evt.cmd == SCPI_MSG_SET_ESE_REQ) { setEseReq(); } if (evt.cmd == SCPI_MSG_SET_ERROR) { setError(evt.param2); } } vTaskDelete(NULL); } examples/test-LwIP-netconn/scpi_server.h
New file @@ -0,0 +1,13 @@ #ifndef _SCPI_SERVER_H_ #define _SCPI_SERVER_H_ #include <stdint.h> void scpi_server_init(void); void SCPI_AddError(int16_t err); void SCPI_RequestControl(void); #endif /* _SCPI_SERVER_H_ */ libscpi/inc/scpi/ieee488.h
@@ -39,6 +39,10 @@ #include "scpi/types.h" #ifdef __cplusplus extern "C" { #endif scpi_result_t SCPI_CoreCls(scpi_t * context); scpi_result_t SCPI_CoreEse(scpi_t * context); scpi_result_t SCPI_CoreEseQ(scpi_t * context); @@ -79,6 +83,9 @@ void SCPI_RegSetBits(scpi_t * context, scpi_reg_name_t name, scpi_reg_val_t bits); void SCPI_RegClearBits(scpi_t * context, scpi_reg_name_t name, scpi_reg_val_t bits); #ifdef __cplusplus } #endif #endif /* SCPI_IEEE488_H */ libscpi/inc/scpi/types.h
@@ -1,5 +1,7 @@ /*- * Copyright (c) 2012-2013 Jan Breuer, * Copyright (c) 2013 Jan Breuer * Richard.hmm * Copyright (c) 2012 Jan Breuer * * All Rights Reserved * @@ -135,7 +137,8 @@ SCPI_UNIT_OHM, SCPI_UNIT_HERTZ, SCPI_UNIT_CELSIUS, SCPI_UNIT_SECONDS SCPI_UNIT_SECONDS, SCPI_UNIT_DISTANCE }; typedef enum _scpi_unit_t scpi_unit_t; libscpi/src/ieee488.c
@@ -100,11 +100,14 @@ void SCPI_RegSet(scpi_t * context, scpi_reg_name_t name, scpi_reg_val_t val) { bool_t srq = FALSE; scpi_reg_val_t mask; scpi_reg_val_t old_val; if ((name >= SCPI_REG_COUNT) || (context->registers == NULL)) { return; } /* store old register value */ old_val = context->registers[name]; /* set register value */ context->registers[name] = val; @@ -116,7 +119,10 @@ mask &= ~STB_SRQ; if (val & mask) { val |= STB_SRQ; srq = TRUE; /* avoid sending SRQ if nothing has changed */ if (old_val != val) { srq = TRUE; } } else { val &= ~STB_SRQ; } libscpi/src/parser.c
@@ -44,14 +44,11 @@ #include "scpi/error.h" static size_t patternSeparatorPos(const char * pattern, size_t len); static size_t cmdSeparatorPos(const char * cmd, size_t len); static size_t cmdTerminatorPos(const char * cmd, size_t len); static size_t cmdlineSeparatorPos(const char * cmd, size_t len); static const char * cmdlineSeparator(const char * cmd, size_t len); static const char * cmdlineTerminator(const char * cmd, size_t len); static const char * cmdlineNext(const char * cmd, size_t len); static bool_t cmdMatch(const char * pattern, const char * cmd, size_t len); static void paramSkipBytes(scpi_t * context, size_t num); static void paramSkipWhitespace(scpi_t * context); @@ -74,40 +71,6 @@ return result; } */ /** * Find pattern separator position * @param pattern * @param len - max search length * @return position of separator or len */ size_t patternSeparatorPos(const char * pattern, size_t len) { char * separator = strnpbrk(pattern, len, "?:[]"); if (separator == NULL) { return len; } else { return separator - pattern; } } /** * Find command separator position * @param cmd - input command * @param len - max search length * @return position of separator or len */ size_t cmdSeparatorPos(const char * cmd, size_t len) { char * separator = strnpbrk(cmd, len, ":?"); size_t result; if (separator == NULL) { result = len; } else { result = separator - cmd; } return result; } /** * Find command termination character @@ -175,67 +138,6 @@ } /** * Compare pattern and command * @param pattern * @param cmd - command * @param len - max search length * @return TRUE if pattern matches, FALSE otherwise */ bool_t cmdMatch(const char * pattern, const char * cmd, size_t len) { int result = FALSE; const char * pattern_ptr = pattern; int pattern_len = SCPI_strnlen(pattern, len); const char * pattern_end = pattern + pattern_len; const char * cmd_ptr = cmd; size_t cmd_len = SCPI_strnlen(cmd, len); const char * cmd_end = cmd + cmd_len; while (1) { int pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_end - pattern_ptr); int cmd_sep_pos = cmdSeparatorPos(cmd_ptr, cmd_end - cmd_ptr); if (matchPattern(pattern_ptr, pattern_sep_pos, cmd_ptr, cmd_sep_pos)) { pattern_ptr = pattern_ptr + pattern_sep_pos; cmd_ptr = cmd_ptr + cmd_sep_pos; result = TRUE; /* command is complete */ if ((pattern_ptr == pattern_end) && (cmd_ptr >= cmd_end)) { break; } /* pattern complete, but command not */ if ((pattern_ptr == pattern_end) && (cmd_ptr < cmd_end)) { result = FALSE; break; } /* command complete, but pattern not */ if (cmd_ptr >= cmd_end) { result = FALSE; break; } /* both command and patter contains command separator at this position */ if ((pattern_ptr[0] == cmd_ptr[0]) && ((pattern_ptr[0] == ':') || (pattern_ptr[0] == '?'))) { pattern_ptr = pattern_ptr + 1; cmd_ptr = cmd_ptr + 1; } else { result = FALSE; break; } } else { result = FALSE; break; } } return result; } /** * Write data to SCPI output * @param context * @param data @@ -273,9 +175,9 @@ } /** * Zapis nove radky na SCPI vystup * Conditionaly write "New Line" * @param context * @return pocet zapsanych znaku * @return number of characters written */ static size_t writeNewLine(scpi_t * context) { if (context->output_count > 0) { @@ -330,7 +232,7 @@ for (i = 0; context->cmdlist[i].pattern != NULL; i++) { cmd = &context->cmdlist[i]; if (cmdMatch(cmd->pattern, cmdline_ptr, cmd_len)) { if (matchCommand(cmd->pattern, cmdline_ptr, cmd_len)) { context->paramlist.cmd = cmd; context->paramlist.parameters = cmdline_ptr + cmd_len; context->paramlist.length = cmdline_len - cmd_len; libscpi/src/utils.c
@@ -1,5 +1,7 @@ /*- * Copyright (c) 2012-2013 Jan Breuer, * Copyright (c) 2013 Jan Breuer * Richard.hmm * Copyright (c) 2012 Jan Breuer * * All Rights Reserved * @@ -42,6 +44,8 @@ #include "utils.h" static size_t patternSeparatorShortPos(const char * pattern, size_t len); static size_t patternSeparatorPos(const char * pattern, size_t len); static size_t cmdSeparatorPos(const char * cmd, size_t len); /** * Find the first occurrence in str of a character in set. @@ -372,6 +376,14 @@ return len; } /** * is colon or not * @param cmd - command * @return */ static bool_t iscolon(char ch) { return (':' == ch) ? TRUE : FALSE; } /** * Pattern is composed from upper case an lower case letters. This function @@ -391,6 +403,41 @@ } /** * Find pattern separator position * @param pattern * @param len - max search length * @return position of separator or len */ size_t patternSeparatorPos(const char * pattern, size_t len) { char * separator = strnpbrk(pattern, len, "?:[]"); if (separator == NULL) { return len; } else { return separator - pattern; } } /** * Find command separator position * @param cmd - input command * @param len - max search length * @return position of separator or len */ size_t cmdSeparatorPos(const char * cmd, size_t len) { char * separator = strnpbrk(cmd, len, ":?"); size_t result; if (separator == NULL) { result = len; } else { result = separator - cmd; } return result; } /** * Match pattern and str. Pattern is in format UPPERCASElowercase * @param pattern * @param pattern_len @@ -404,6 +451,73 @@ compareStr(pattern, pattern_sep_pos_short, str, str_len); } /** * Compare pattern and command * @param pattern * @param cmd - command * @param len - max search length * @return TRUE if pattern matches, FALSE otherwise */ bool_t matchCommand(const char * pattern, const char * cmd, size_t len) { int result = FALSE; const char * pattern_ptr = pattern; int pattern_len = strlen(pattern); const char * pattern_end = pattern + pattern_len; const char * cmd_ptr = cmd; size_t cmd_len = SCPI_strnlen(cmd, len); const char * cmd_end = cmd + cmd_len; /* TODO: now it is possible to send command ":*IDN?" which is incorrect */ if (iscolon(cmd_ptr[0])) { cmd_len --; cmd_ptr ++; } while (1) { int pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_end - pattern_ptr); int cmd_sep_pos = cmdSeparatorPos(cmd_ptr, cmd_end - cmd_ptr); if (matchPattern(pattern_ptr, pattern_sep_pos, cmd_ptr, cmd_sep_pos)) { pattern_ptr = pattern_ptr + pattern_sep_pos; cmd_ptr = cmd_ptr + cmd_sep_pos; result = TRUE; /* command is complete */ if ((pattern_ptr == pattern_end) && (cmd_ptr >= cmd_end)) { break; } /* pattern complete, but command not */ if ((pattern_ptr == pattern_end) && (cmd_ptr < cmd_end)) { result = FALSE; break; } /* command complete, but pattern not */ if (cmd_ptr >= cmd_end) { result = FALSE; break; } /* both command and patter contains command separator at this position */ if ((pattern_ptr[0] == cmd_ptr[0]) && ((pattern_ptr[0] == ':') || (pattern_ptr[0] == '?'))) { pattern_ptr = pattern_ptr + 1; cmd_ptr = cmd_ptr + 1; } else { result = FALSE; break; } } else { result = FALSE; break; } } return result; } #if !HAVE_STRNLEN /* use FreeBSD strnlen */ libscpi/src/utils.h
@@ -45,7 +45,11 @@ extern "C" { #endif #if defined(__GNUC__) && (__GNUC__ >= 4) #define LOCAL __attribute__((visibility ("hidden"))) #else #define LOCAL #endif char * strnpbrk(const char *str, size_t size, const char *set) LOCAL; bool_t compareStr(const char * str1, size_t len1, const char * str2, size_t len2) LOCAL; @@ -56,7 +60,9 @@ bool_t locateText(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL; bool_t locateStr(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL; size_t skipWhitespace(const char * cmd, size_t len) LOCAL; size_t skipColon(const char * cmd, size_t len) LOCAL; bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len) LOCAL; bool_t matchCommand(const char * pattern, const char * cmd, size_t len) LOCAL; #if !HAVE_STRNLEN size_t BSD_strnlen(const char *s, size_t maxlen);