AMU Library 3.0
C/C++ library for communicating with AMU (Aerospace Measurement Unit) devices
Loading...
Searching...
No Matches
error.c
Go to the documentation of this file.
1/*-
2 * Copyright (c) 2012-2013 Jan Breuer,
3 *
4 * All Rights Reserved
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
36
37#include <stdint.h>
38
39#include "parser.h"
40#include "ieee488.h"
41#include "error.h"
42#include "fifo_private.h"
43#include "constants.h"
44
45#if USE_DEVICE_DEPENDENT_ERROR_INFORMATION
46#define SCPI_ERROR_SETVAL(e, c, i) do { (e)->error_code = (c); (e)->device_dependent_info = (i); } while(0)
47#else
48#define SCPI_ERROR_SETVAL(e, c, i) do { (e)->error_code = (c); (void)(i);} while(0)
49#endif
50
55void SCPI_ErrorInit(scpi_t * context, scpi_error_t * data, int16_t size) {
56 fifo_init(&context->error_queue, data, size);
57}
58
63static void SCPI_ErrorEmitEmpty(scpi_t * context) {
64 if ((SCPI_ErrorCount(context) == 0) && (SCPI_RegGet(context, SCPI_REG_STB) & STB_QMA)) {
66
67 if (context->interface && context->interface->error) {
68 context->interface->error(context, 0);
69 }
70 }
71}
72
78static void SCPI_ErrorEmit(scpi_t * context, int16_t err) {
80
81 if (context->interface && context->interface->error) {
82 context->interface->error(context, err);
83 }
84}
85
90void SCPI_ErrorClear(scpi_t * context) {
91#if USE_DEVICE_DEPENDENT_ERROR_INFORMATION
92 scpi_error_t error;
93 while (fifo_remove(&context->error_queue, &error)) {
94 SCPIDEFINE_free(&context->error_info_heap, error.device_dependent_info, false);
95 }
96#endif
97 fifo_clear(&context->error_queue);
98
99 SCPI_ErrorEmitEmpty(context);
100}
101
108scpi_bool_t SCPI_ErrorPop(scpi_t * context, scpi_error_t * error) {
109 if (!error || !context) return FALSE;
110 SCPI_ERROR_SETVAL(error, 0, NULL);
111 fifo_remove(&context->error_queue, error);
112
113 SCPI_ErrorEmitEmpty(context);
114
115 return TRUE;
116}
117
123int32_t SCPI_ErrorCount(scpi_t * context) {
124 int16_t result = 0;
125
126 fifo_count(&context->error_queue, &result);
127
128 return result;
129}
130
131static scpi_bool_t SCPI_ErrorAddInternal(scpi_t * context, int16_t err, char * info, size_t info_len) {
132 scpi_error_t error_value;
133 char * info_ptr = info ? SCPIDEFINE_strndup(&context->error_info_heap, info, info_len) : NULL;
134 SCPI_ERROR_SETVAL(&error_value, err, info_ptr);
135 if (!fifo_add(&context->error_queue, &error_value)) {
136 SCPIDEFINE_free(&context->error_info_heap, error_value.device_dependent_info, true);
137 fifo_remove_last(&context->error_queue, &error_value);
138 SCPIDEFINE_free(&context->error_info_heap, error_value.device_dependent_info, true);
139 SCPI_ERROR_SETVAL(&error_value, SCPI_ERROR_QUEUE_OVERFLOW, NULL);
140 fifo_add(&context->error_queue, &error_value);
141 return FALSE;
142 }
143 return TRUE;
144}
145
146struct error_reg {
147 int16_t from;
148 int16_t to;
150};
151
152#define ERROR_DEFS_N 9
153
154static const struct error_reg errs[ERROR_DEFS_N] = {
155 {-100, -199, ESR_CER}, /* Command error (e.g. syntax error) ch 21.8.9 */
156 {-200, -299, ESR_EER}, /* Execution Error (e.g. range error) ch 21.8.10 */
157 {-300, -399, ESR_DER}, /* Device specific error -300, -399 ch 21.8.11 */
158 { 1, 32767, ESR_DER}, /* Device designer provided specific error 1, 32767 ch 21.8.11 */
159 {-400, -499, ESR_QER}, /* Query error -400, -499 ch 21.8.12 */
160 {-500, -599, ESR_PON}, /* Power on event -500, -599 ch 21.8.13 */
161 {-600, -699, ESR_URQ}, /* User Request Event -600, -699 ch 21.8.14 */
162 {-700, -799, ESR_REQ}, /* Request Control Event -700, -799 ch 21.8.15 */
163 {-800, -899, ESR_OPC}, /* Operation Complete Event -800, -899 ch 21.8.16 */
164};
165
173void SCPI_ErrorPushEx(scpi_t * context, int16_t err, char * info, size_t info_len) {
174 int i;
175 /* automatic calculation of length */
176 if (info && info_len == 0) {
178 }
179 scpi_bool_t queue_overflow = !SCPI_ErrorAddInternal(context, err, info, info_len);
180
181 for (i = 0; i < ERROR_DEFS_N; i++) {
182 if ((err <= errs[i].from) && (err >= errs[i].to)) {
184 }
185 }
186
187 SCPI_ErrorEmit(context, err);
188 if (queue_overflow) {
190 }
191
192 if (context) {
193 context->cmd_error = TRUE;
194 }
195}
196
202void SCPI_ErrorPush(scpi_t * context, int16_t err) {
203 SCPI_ErrorPushEx(context, err, NULL, 0);
204 return;
205}
206
212const char * SCPI_ErrorTranslate(int16_t err) {
213 switch (err) {
214#define X(def, val, str) case def: return str;
215#if USE_FULL_ERROR_LIST
216#define XE X
217#else
218#define XE(def, val, str)
219#endif
221
222#if USE_USER_ERROR_LIST
223 LIST_OF_USER_ERRORS
224#endif
225#undef X
226#undef XE
227 default: return "Unknown error";
228 }
229}
230
231
#define SCPIDEFINE_strnlen(s, l)
Definition config.h:174
#define SCPIDEFINE_strndup(h, s, l)
Definition config.h:226
#define SCPIDEFINE_free(h, s, r)
Definition config.h:227
#define SCPI_STD_ERROR_DESC_MAX_STRING_LENGTH
Definition constants.h:54
void SCPI_ErrorPush(scpi_t *context, int16_t err)
Definition error.c:202
#define SCPI_ERROR_SETVAL(e, c, i)
Definition error.c:48
int32_t SCPI_ErrorCount(scpi_t *context)
Definition error.c:123
static void SCPI_ErrorEmitEmpty(scpi_t *context)
Definition error.c:63
void SCPI_ErrorPushEx(scpi_t *context, int16_t err, char *info, size_t info_len)
Definition error.c:173
const char * SCPI_ErrorTranslate(int16_t err)
Definition error.c:212
void SCPI_ErrorInit(scpi_t *context, scpi_error_t *data, int16_t size)
Definition error.c:55
scpi_bool_t SCPI_ErrorPop(scpi_t *context, scpi_error_t *error)
Definition error.c:108
static void SCPI_ErrorEmit(scpi_t *context, int16_t err)
Definition error.c:78
void SCPI_ErrorClear(scpi_t *context)
Definition error.c:90
static scpi_bool_t SCPI_ErrorAddInternal(scpi_t *context, int16_t err, char *info, size_t info_len)
Definition error.c:131
static const struct error_reg errs[9]
Definition error.c:154
#define ERROR_DEFS_N
Definition error.c:152
@ SCPI_ERROR_QUEUE_OVERFLOW
Definition error.h:193
#define LIST_OF_ERRORS
Definition error.h:62
scpi_bool_t fifo_remove_last(scpi_fifo_t *fifo, scpi_error_t *value)
Definition fifo.c:94
scpi_bool_t fifo_remove(scpi_fifo_t *fifo, scpi_error_t *value)
Definition fifo.c:72
scpi_bool_t fifo_add(scpi_fifo_t *fifo, const scpi_error_t *value)
Definition fifo.c:51
scpi_bool_t fifo_count(scpi_fifo_t *fifo, int16_t *value)
Definition fifo.c:116
void fifo_init(scpi_fifo_t *fifo, scpi_error_t *data, int16_t size)
Definition fifo.c:8
void fifo_clear(scpi_fifo_t *fifo)
Definition fifo.c:20
void SCPI_RegSetBits(scpi_t *context, scpi_reg_name_t name, scpi_reg_val_t bits)
Definition ieee488.c:171
scpi_reg_val_t SCPI_RegGet(scpi_t *context, scpi_reg_name_t name)
Definition ieee488.c:73
void SCPI_RegClearBits(scpi_t *context, scpi_reg_name_t name, scpi_reg_val_t bits)
Definition ieee488.c:180
#define ESR_QER
Definition ieee488.h:73
#define ESR_REQ
Definition ieee488.h:72
#define ESR_DER
Definition ieee488.h:74
#define STB_QMA
Definition ieee488.h:63
#define ESR_EER
Definition ieee488.h:75
#define ESR_CER
Definition ieee488.h:76
#define ESR_URQ
Definition ieee488.h:77
#define ESR_PON
Definition ieee488.h:78
#define ESR_OPC
Definition ieee488.h:71
scpi_reg_val_t esrBit
Definition error.c:149
int16_t to
Definition error.c:148
int16_t from
Definition error.c:147
bool scpi_bool_t
Definition types.h:67
@ SCPI_REG_STB
Definition types.h:72
@ SCPI_REG_ESR
Definition types.h:74
uint16_t scpi_reg_val_t
Definition types.h:106
#define TRUE
Definition types.h:63
#define FALSE
Definition types.h:60