From 834faf3b0a91d6af089258d9917fbc86a92699a1 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周日, 18 1月 2015 16:39:11 +0800 Subject: [PATCH] Resolve #18 *TST? should return 0 as OK --- examples/test-LwIP-netconn/scpi_server.c | 86 +++++++++++++++++++++++++++---------------- 1 files changed, 54 insertions(+), 32 deletions(-) diff --git a/examples/test-LwIP-netconn/scpi_server.c b/examples/test-LwIP-netconn/scpi_server.c index 653224b..afbfd7a 100644 --- a/examples/test-LwIP-netconn/scpi_server.c +++ b/examples/test-LwIP-netconn/scpi_server.c @@ -53,13 +53,16 @@ #define CONTROL_PORT 5026 #define SCPI_THREAD_PRIO (tskIDLE_PRIORITY + 2) +#define SCPI_THREAD_STACKSIZE (512) +#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; @@ -70,6 +73,15 @@ //FILE * fio; //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, @@ -134,10 +146,13 @@ return SCPI_RES_OK; } +/** + * Return 0 as OK and other number as error + */ scpi_result_t SCPI_Test(scpi_t * context) { (void) context; iprintf("**Test\r\n"); - return SCPI_RES_OK; + return 0; } scpi_result_t SCPI_Reset(scpi_t * context) { @@ -156,9 +171,14 @@ SCPI_RegSetBits(&scpi_context, SCPI_REG_ESR, ESR_REQ); } -void SCPI_RequestControl(void) { - uint32_t msg = SCPI_MSG_SET_ESE_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); @@ -168,21 +188,29 @@ 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); } @@ -209,16 +237,11 @@ 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) { @@ -340,11 +363,11 @@ * */ 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; @@ -355,35 +378,34 @@ 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 (rc == SCPI_MSG_SET_ESE_REQ) { + if (evt.cmd == SCPI_MSG_SET_ESE_REQ) { setEseReq(); + } + + if (evt.cmd == SCPI_MSG_SET_ERROR) { + setError(evt.param2); } } @@ -392,5 +414,5 @@ } void scpi_server_init(void) { - sys_thread_new("SCPI", scpi_server_thread, NULL, 2 * DEFAULT_THREAD_STACKSIZE, SCPI_THREAD_PRIO); + sys_thread_new("SCPI", scpi_server_thread, NULL, SCPI_THREAD_STACKSIZE, SCPI_THREAD_PRIO); } \ No newline at end of file -- Gitblit v1.9.1