| | |
| | | |
| | | #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; |
| | |
| | | //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, |
| | |
| | | .control_io = NULL, |
| | | .evtQueue = 0, |
| | | }; |
| | | |
| | | |
| | | size_t SCPI_Write(scpi_t * context, const char * data, size_t len) { |
| | | if (context->user_context != NULL) { |
| | |
| | | (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]; |
| | |
| | | } |
| | | |
| | | |
| | | #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); |
| | | } |
| | |
| | | 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; |
| | |
| | | * |
| | | */ |
| | | 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; |
| | |
| | | user_data.control_io_listen = createServer(CONTROL_PORT); |
| | | |
| | | while(1) { |
| | | rc = waitServer(&user_data); |
| | | waitServer(&user_data, &evt); |
| | | |
| | | if (rc < 0) { // failed |
| | | iprintf("select failed"); |
| | | break; |
| | | } |
| | | |
| | | if (rc == 0) { // timeout |
| | | if (evt.cmd == SCPI_MSG_TIMEOUT) { // 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); |
New file |
| | |
| | | #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_ */ |
| | |
| | | |
| | | #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); |
| | |
| | | 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 */ |
| | | |
| | |
| | | /*- |
| | | * Copyright (c) 2012-2013 Jan Breuer, |
| | | * Copyright (c) 2013 Jan Breuer |
| | | * Richard.hmm |
| | | * Copyright (c) 2012 Jan Breuer |
| | | * |
| | | * All Rights Reserved |
| | | * |
| | |
| | | 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; |
| | | |
| | |
| | | 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; |
| | |
| | | mask &= ~STB_SRQ; |
| | | if (val & mask) { |
| | | val |= STB_SRQ; |
| | | /* avoid sending SRQ if nothing has changed */ |
| | | if (old_val != val) { |
| | | srq = TRUE; |
| | | } |
| | | } else { |
| | | val &= ~STB_SRQ; |
| | | } |
| | |
| | | #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); |
| | |
| | | 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 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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) { |
| | |
| | | |
| | | 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; |
| | |
| | | /*- |
| | | * Copyright (c) 2012-2013 Jan Breuer, |
| | | * Copyright (c) 2013 Jan Breuer |
| | | * Richard.hmm |
| | | * Copyright (c) 2012 Jan Breuer |
| | | * |
| | | * All Rights Reserved |
| | | * |
| | |
| | | #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. |
| | |
| | | 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 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | 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 */ |
| | |
| | | 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; |
| | |
| | | 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); |