Jan Breuer
2014-09-24 6bc097eba30c30c45bd7c89d97991428b53660dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*-
 * Copyright (c) 2012-2013 Jan Breuer,
 *
 * All Rights Reserved
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
/**
 * @file   scpi_units.c
 * @date   Thu Nov 15 10:58:45 UTC 2012
 * 
 * @brief  SCPI units
 * 
 * 
 */
 
#include <string.h>
#include "scpi/parser.h"
#include "scpi/units.h"
#include "utils_private.h"
#include "scpi/error.h"
#include "lexer_private.h"
 
 
/*
 * multipliers IEEE 488.2-1992 tab 7-2
 * 1E18         EX
 * 1E15         PE
 * 1E12         T
 * 1E9          G
 * 1E6          MA (use M for OHM and HZ)
 * 1E3          K
 * 1E-3         M (disaalowed for OHM and HZ)
 * 1E-6         U
 * 1E-9         N
 * 1E-12        P
 * 1E-15        F
 * 1E-18        A
 */
 
/*
 * units definition IEEE 488.2-1992 tab 7-1
 */
const scpi_unit_def_t scpi_units_def[] = {
    /* voltage */
    {/* name */ "UV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-6},
    {/* name */ "MV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-3},
    {/* name */ "V", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1},
    {/* name */ "KV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e3},
 
    /* current */
    {/* name */ "UA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-6},
    {/* name */ "MA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-3},
    {/* name */ "A", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1},
    {/* name */ "KA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e3},
 
    /* resistance */
    {/* name */ "OHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1},
    {/* name */ "KOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e3},
    {/* name */ "MOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e6},
 
    /* frequency */
    {/* name */ "HZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1},
    {/* name */ "KHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e3},
    {/* name */ "MHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e6},
    {/* name */ "GHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e9},
 
    /* temperature */
    {/* name */ "CEL", /* unit */ SCPI_UNIT_CELSIUS, /* mult */ 1},
 
    /* time */
    {/* name */ "PS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-12},
    {/* name */ "NS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-9},
    {/* name */ "US", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-6},
    {/* name */ "MS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-3},
    {/* name */ "S", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1},
    {/* name */ "MIN", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 60},
    {/* name */ "HR", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 3600},
 
    SCPI_UNITS_LIST_END,
};
 
/*
 * Special number values definition
 */
const scpi_special_number_def_t scpi_special_numbers_def[] = {
    {/* name */ "MINimum", /* type */ SCPI_NUM_MIN},
    {/* name */ "MAXimum", /* type */ SCPI_NUM_MAX},
    {/* name */ "DEFault", /* type */ SCPI_NUM_DEF},
    {/* name */ "UP", /* type */ SCPI_NUM_UP},
    {/* name */ "DOWN", /* type */ SCPI_NUM_DOWN},
    {/* name */ "NAN", /* type */ SCPI_NUM_NAN},
    {/* name */ "INFinity", /* type */ SCPI_NUM_INF},
    {/* name */ "NINF", /* type */ SCPI_NUM_NINF},
    SCPI_SPECIAL_NUMBERS_LIST_END,
};
 
/**
 * Match string constant to one of special number values
 * @param specs specifications of special numbers (patterns)
 * @param str string to be recognised
 * @param len length of string
 * @param value resultin value
 * @return TRUE if str matches one of specs patterns
 */
static scpi_bool_t translateSpecialNumber(const scpi_special_number_def_t * specs, const char * str, size_t len, scpi_number_parameter_t * value) {
    int i;
 
    value->value = 0.0;
    value->unit = SCPI_UNIT_NONE;
    value->type = SCPI_NUM_NUMBER;
 
    if (specs == NULL) {
        return FALSE;
    }
 
    for (i = 0; specs[i].name != NULL; i++) {
        if (matchPattern(specs[i].name, strlen(specs[i].name), str, len)) {
            value->type = specs[i].type;
            return TRUE;
        }
    }
 
    return FALSE;
}
 
/**
 * Convert special number type to its string representation
 * @param specs specifications of special numbers (patterns)
 * @param type type of special number
 * @return String representing special number or NULL
 */
static const char * translateSpecialNumberInverse(const scpi_special_number_def_t * specs, scpi_special_number_t type) {
    int i;
 
    if (specs == NULL) {
        return NULL;
    }
 
    for (i = 0; specs[i].name != NULL; i++) {
        if (specs[i].type == type) {
            return specs[i].name;
        }
    }
 
    return NULL;
}
 
/**
 * Convert string describing unit to its representation
 * @param units units patterns
 * @param unit text representation of unknown unit
 * @param len length of text representation
 * @return pointer of related unit definition or NULL
 */
static const scpi_unit_def_t * translateUnit(const scpi_unit_def_t * units, const char * unit, size_t len) {
    int i;
 
    if (units == NULL) {
        return NULL;
    }
 
    for (i = 0; units[i].name != NULL; i++) {
        if (compareStr(unit, len, units[i].name, strlen(units[i].name))) {
            return &units[i];
        }
    }
 
    return NULL;
}
 
/**
 * Convert unit definition to string
 * @param units units definitions (patterns)
 * @param unit type of unit
 * @return string representation of unit
 */
static const char * translateUnitInverse(const scpi_unit_def_t * units, const scpi_unit_t unit) {
    int i;
 
    if (units == NULL) {
        return NULL;
    }
 
    for (i = 0; units[i].name != NULL; i++) {
        if ((units[i].unit == unit) && (units[i].mult == 1)) {
            return units[i].name;
        }
    }
 
    return NULL;
}
 
/**
 * Transform number to base units
 * @param context
 * @param unit text representation of unit
 * @param len length of text representation
 * @param value preparsed numeric value
 * @return TRUE if value parameter was converted to base units
 */
static scpi_bool_t transformNumber(scpi_t * context, const char * unit, size_t len, scpi_number_parameter_t * value) {
    size_t s;
    const scpi_unit_def_t * unitDef;
    s = skipWhitespace(unit, len);
 
    if (s == len) {
        value->unit = SCPI_UNIT_NONE;
        return TRUE;
    }
 
    unitDef = translateUnit(context->units, unit + s, len - s);
 
    if (unitDef == NULL) {
        SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX);
        return FALSE;
    }
 
    value->value *= unitDef->mult;
    value->unit = unitDef->unit;
 
    return TRUE;
}
 
/**
 * Parse parameter as number, number with unit or special value (min, max, default, ...)
 * @param context
 * @param value return value
 * @param mandatory if the parameter is mandatory
 * @return 
 */
scpi_bool_t SCPI_ParamTranslateNumberVal(scpi_t * context, scpi_parameter_t * parameter) {
    token_t token;
    lex_state_t state;
 
    state.buffer = parameter->data.ptr;
    state.pos = state.buffer;
    state.len = parameter->data.len;
 
    switch(parameter->type) {
        case TokDecimalNumericProgramDataWithSuffix:
            lexDecimalNumericProgramData(&state, &token);
            lexWhiteSpace(&state, &token);
            lexSuffixProgramData(&state, &token);
 
            return transformNumber(context, token.ptr, token.len, &parameter->number);
        case TokProgramMnemonic:
            lexWhiteSpace(&state, &token);
            lexCharacterProgramData(&state, &token);
 
            /* convert string to special number type */
            return translateSpecialNumber(context->special_numbers, token.ptr, token.len, &parameter->number);
        default:
            return FALSE;
    }
}
 
/**
 * Convert scpi_number_t to string
 * @param context
 * @param value number value
 * @param str target string
 * @param len max length of string
 * @return number of chars written to string
 */
size_t SCPI_NumberToStr(scpi_t * context, scpi_number_parameter_t * value, char * str, size_t len) {
    const char * type;
    const char * unit;
    size_t result;
 
    if (!value || !str) {
        return 0;
    }
 
    type = translateSpecialNumberInverse(context->special_numbers, value->type);
 
    if (type) {
        strncpy(str, type, len);
        return min(strlen(type), len);
    }
 
    result = doubleToStr(value->value, str, len);
 
    unit = translateUnitInverse(context->units, value->unit);
 
    if (unit) {
        strncat(str, " ", len);
        strncat(str, unit, len);
        result += strlen(unit) + 1;
    }
 
    return result;
}