tango-client-sys 0.1.6

C bindings for a client to the Tango control system.
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/******************************************************************************
 *
 * File:        c_tango_dbase.c
 * Project:     C client interface to Tango
 * Description: Interface functions to access Tango properties
 * Original:    November 2007
 * Author:      jensmeyer
 *
 * Adapted for tango-rs by Georg Brandl, 2015.
 *
 ******************************************************************************/

#include "c_tango.h"

ErrorStack *tango_translate_exception(Tango::DevFailed& tango_exception);
static void convert_property_reading(Tango::DbDatum& tango_prop, DbDatum *prop);
static void convert_property_writing(DbDatum *prop, Tango::DbDatum& tango_prop);

ErrorStack *tango_create_database_proxy(void **db_proxy) {
    try {
        Tango::Database *dbase = new Tango::Database();
        *db_proxy = (void *)dbase;
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_delete_database_proxy(void *db_proxy) {
    Tango::Database *dbase = (Tango::Database *)db_proxy;

    try {
        delete dbase;
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_get_device_exported(void *db_proxy, char *name_filter, DbDatum *dev_list) {
    Tango::Database *dbase = (Tango::Database *)db_proxy;
    Tango::DbDatum tango_dev_list;

    try {
        std::string filter = name_filter;
        tango_dev_list = dbase->get_device_exported(filter);

        // the result is a string array, set the data type for the conversion
        dev_list->data_type = DEVVAR_STRINGARRAY;
        convert_property_reading(tango_dev_list, dev_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_get_device_exported_for_class(void *db_proxy, char *class_name, DbDatum *dev_list) {
    Tango::Database *dbase = (Tango::Database *)db_proxy;
    Tango::DbDatum tango_dev_list;

    try {
        std::string name = class_name;
        tango_dev_list = dbase->get_device_exported_for_class(name);

        // the result is a string array, set the data type for the conversion
        dev_list->data_type = DEVVAR_STRINGARRAY;
        convert_property_reading(tango_dev_list, dev_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_get_object_list(void *db_proxy, char *name_filter, DbDatum *obj_list) {
    Tango::Database *dbase = (Tango::Database *)db_proxy;
    Tango::DbDatum tango_obj_list;

    try {
        std::string filter = name_filter;
        tango_obj_list = dbase->get_object_list(filter);

        // the result is a string array, set the data type for the conversion
        obj_list->data_type = DEVVAR_STRINGARRAY;
        convert_property_reading(tango_obj_list, obj_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_get_object_property_list(void *db_proxy, char *obj_name, char *name_filter, DbDatum *prop_list) {
    Tango::Database *dbase = (Tango::Database *) db_proxy;
    Tango::DbDatum tango_prop_list;

    try {
        std::string name = obj_name;
        std::string filter = name_filter;
        tango_prop_list = dbase->get_object_property_list(name, filter);

        // the result is a string array, set the data type for the conversion
        prop_list->data_type = DEVVAR_STRINGARRAY;
        convert_property_reading(tango_prop_list, prop_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_get_property(void *db_proxy, char *obj_name, DbData *prop_list) {
    Tango::Database *dbase = (Tango::Database *)db_proxy;
    Tango::DbData tango_prop_list;

    try {
        std::string name = obj_name;

        // copy the property names into the Tango object
        for (uint32_t i = 0; i < prop_list->length; i++) {
            tango_prop_list.push_back(Tango::DbDatum(prop_list->sequence[i].property_name));
        }

        // read the properties
        dbase->get_property(name, tango_prop_list);

        for (uint32_t i = 0; i < prop_list->length; i++) {
            // copy the property data into the C structure
            convert_property_reading(tango_prop_list[i], &prop_list->sequence[i]);
        }
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_put_property(void *db_proxy, char *obj_name, DbData *prop_list) {
    Tango::Database *dbase = (Tango::Database *)db_proxy;
    Tango::DbData tango_prop_list;

    try {
        std::string name = obj_name;

        // copy the property names and data into the Tango object
        tango_prop_list.resize(prop_list->length);
        for (uint32_t i = 0; i < prop_list->length; i++) {
            convert_property_writing(&prop_list->sequence[i], tango_prop_list[i]);
        }

        // write the properties
        dbase->put_property(name, tango_prop_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_delete_property(void *db_proxy, char *obj_name, DbData *prop_list) {
    Tango::Database *dbase = (Tango::Database *)db_proxy;
    Tango::DbData tango_prop_list;

    try {
        std::string name = obj_name;

        // copy the property names into the Tango object
        for (uint32_t i = 0; i < prop_list->length; i++) {
            tango_prop_list.push_back(Tango::DbDatum(prop_list->sequence[i].property_name));
        }

        // read the properties
        dbase->delete_property(name, tango_prop_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_get_device_property(void *proxy, DbData *prop_list) {
    Tango::DeviceProxy *dev = (Tango::DeviceProxy *)proxy;
    Tango::DbData tango_prop_list;

    try {
        // copy the property names into the Tango object
        for (uint32_t i = 0; i < prop_list->length; i++) {
            tango_prop_list.push_back(Tango::DbDatum(prop_list->sequence[i].property_name));
        }

        // read the properties
        dev->get_property(tango_prop_list);

        for (uint32_t i = 0; i < prop_list->length; i++) {
            // copy the property data into the C structure
            convert_property_reading(tango_prop_list[i], &prop_list->sequence[i]);
        }
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_put_device_property(void *proxy, DbData *prop_list) {
    Tango::DeviceProxy *dev = (Tango::DeviceProxy *)proxy;
    Tango::DbData tango_prop_list;

    try {
        // copy the property names into the Tango object
        tango_prop_list.resize(prop_list->length);

        for (uint32_t i = 0; i < prop_list->length; i++) {
            convert_property_writing(&prop_list->sequence[i], tango_prop_list[i]);
        }

        // write the properties
        dev->put_property(tango_prop_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


ErrorStack *tango_delete_device_property(void *proxy, DbData *prop_list) {
    Tango::DeviceProxy *dev = (Tango::DeviceProxy *)proxy;
    Tango::DbData tango_prop_list;

    try {
        // copy the property names into the Tango object
        for (uint32_t i = 0; i < prop_list->length; i++) {
            tango_prop_list.push_back(Tango::DbDatum(prop_list->sequence[i].property_name));
        }

        // read the properties
        dev->delete_property(tango_prop_list);
    }
    catch (Tango::DevFailed &tango_exception) {
        return tango_translate_exception(tango_exception);
    }
    return 0;
}


void tango_free_DbDatum(DbDatum *db_datum) {
    free(db_datum->property_name);  // from strdup
    
    switch (db_datum->data_type) {
    case DEV_STRING:
        free(db_datum->prop_data.string_val);
        break;

    case DEVVAR_SHORTARRAY:
        delete[] db_datum->prop_data.short_arr.sequence;
        break;

    case DEVVAR_USHORTARRAY:
        delete[] db_datum->prop_data.ushort_arr.sequence;
        break;

    case DEVVAR_LONGARRAY:
        delete[] db_datum->prop_data.long_arr.sequence;
        break;

    case DEVVAR_ULONGARRAY:
        delete[] db_datum->prop_data.ulong_arr.sequence;
        break;

    case DEVVAR_LONG64ARRAY:
        delete[] db_datum->prop_data.long64_arr.sequence;
        break;

    case DEVVAR_ULONG64ARRAY:
        delete[] db_datum->prop_data.ulong64_arr.sequence;
        break;

    case DEVVAR_FLOATARRAY:
        delete[] db_datum->prop_data.float_arr.sequence;
        break;

    case DEVVAR_DOUBLEARRAY:
        delete[] db_datum->prop_data.double_arr.sequence;
        break;

    case DEVVAR_STRINGARRAY:
        for (uint32_t i = 0; i < db_datum->prop_data.string_arr.length; i++) {
            free(db_datum->prop_data.string_arr.sequence[i]);  // from strdup
        }

        delete[] db_datum->prop_data.string_arr.sequence;
        break;

    default:
        break;
    }
}


void tango_free_DbData(DbData *db_data) {
    for (uint32_t i = 0; i < db_data->length; i++) {
        tango_free_DbDatum(&db_data->sequence[i]);
    }
}


static void convert_property_reading(Tango::DbDatum &tango_prop, DbDatum *prop) {
    // allocate property name
    prop->property_name = strdup(tango_prop.name.c_str());

    // copy the property data into the C structure
    if (!tango_prop.is_empty()) {
        // set the flags
        prop->is_empty = false;
        prop->wrong_data_type = false;

        // convert the data
        switch (prop->data_type) {
        case DEV_BOOLEAN:
            if (!(tango_prop >> prop->prop_data.bool_val))
                prop->wrong_data_type = true;
            break;

        case DEV_UCHAR:
            if (!(tango_prop >> prop->prop_data.char_val))
                prop->wrong_data_type = true;
            break;

        case DEV_SHORT:
            if (!(tango_prop >> prop->prop_data.short_val))
                prop->wrong_data_type = true;
            break;

        case DEV_USHORT:
            if (!(tango_prop >> prop->prop_data.ushort_val))
                prop->wrong_data_type = true;
            break;

        case DEV_LONG: {
            Tango::DevLong long_val;
            if (!(tango_prop >> long_val))
                prop->wrong_data_type = true;
            else
                prop->prop_data.long_val = long_val;
            break;
        }

        case DEV_ULONG: {
            Tango::DevULong ulong_val;
            if (!(tango_prop >> ulong_val))
                prop->wrong_data_type = true;
            else
                prop->prop_data.ulong_val = ulong_val;
            break;
        }

        case DEV_LONG64: {
            Tango::DevLong64 long64_val;
            if (!(tango_prop >> long64_val))
                prop->wrong_data_type = true;
            else
                prop->prop_data.long64_val = long64_val;
            break;
        }

        case DEV_ULONG64: {
            Tango::DevULong64 ulong64_val;
            if (!(tango_prop >> ulong64_val))
                prop->wrong_data_type = true;
            else
                prop->prop_data.ulong64_val = ulong64_val;
            break;
        }

        case DEV_FLOAT:
            if (!(tango_prop >> prop->prop_data.float_val))
                prop->wrong_data_type = true;
            break;

        case DEV_DOUBLE:
            if (!(tango_prop >> prop->prop_data.double_val))
                prop->wrong_data_type = true;
            break;

        case DEV_STRING: {
            std::string string_val;
            if (tango_prop >> string_val) {
                prop->prop_data.string_val = strdup(string_val.c_str());
            } else {
                prop->wrong_data_type = true;
            }
            break;
        }

#define EXTRACT_ARRAY(member, type)                                     \
            {                                                           \
                std::vector<type> vect;                                 \
                uint32_t nb_data;                                       \
                if (tango_prop >> vect) {                               \
                    nb_data = vect.size();                              \
                    prop->prop_data.member.length = nb_data;            \
                    prop->prop_data.member.sequence = new type[nb_data]; \
                    memcpy(prop->prop_data.member.sequence,             \
                           vect.data(), sizeof(type) * nb_data);        \
                } else {                                                \
                    prop->wrong_data_type = true;                       \
                }                                                       \
                break;                                                  \
            }

        case DEVVAR_SHORTARRAY:   EXTRACT_ARRAY(short_arr, int16_t);
        case DEVVAR_USHORTARRAY:  EXTRACT_ARRAY(ushort_arr, uint16_t);
        case DEVVAR_LONGARRAY:    EXTRACT_ARRAY(long_arr, Tango::DevLong);
        case DEVVAR_ULONGARRAY:   EXTRACT_ARRAY(ulong_arr, Tango::DevULong);
        case DEVVAR_LONG64ARRAY:  EXTRACT_ARRAY(long64_arr, Tango::DevLong64);
        case DEVVAR_ULONG64ARRAY: EXTRACT_ARRAY(ulong64_arr, Tango::DevULong64);
        case DEVVAR_FLOATARRAY:   EXTRACT_ARRAY(float_arr, float);
        case DEVVAR_DOUBLEARRAY:  EXTRACT_ARRAY(double_arr, double);

        case DEVVAR_STRINGARRAY: {
            std::vector<std::string> string_vect;
            uint32_t nb_data;

            if (tango_prop >> string_vect) {
                nb_data = string_vect.size();

                prop->prop_data.string_arr.sequence = new char *[nb_data];
                prop->prop_data.string_arr.length = nb_data;

                for (uint32_t i = 0; i < nb_data; i++) {
                    prop->prop_data.string_arr.sequence[i] = strdup(string_vect[i].c_str());
                }
            } else {
                prop->wrong_data_type = true;
            }
            break;
        }

        default:
            Tango::Except::throw_exception(
                "Data type error",
                "The requested data type is not implemented for property reading!",
                "c_tango_dbase.c::convert_property_reading()");
            break;
        }
    } else {
        // no property value found, set the is_empty flag
        prop->is_empty = true;
        prop->wrong_data_type = false;
    }
}


static void convert_property_writing(DbDatum *prop, Tango::DbDatum& tango_prop) {
    tango_prop.name = prop->property_name;

    switch (prop->data_type) {
    case DEV_BOOLEAN:
        tango_prop << prop->prop_data.bool_val;
        break;

    case DEV_UCHAR:
        tango_prop << prop->prop_data.char_val;
        break;

    case DEV_SHORT:
        tango_prop << prop->prop_data.short_val;
        break;

    case DEV_USHORT:
        tango_prop << prop->prop_data.ushort_val;
        break;

    case DEV_LONG:
        tango_prop << (Tango::DevLong)prop->prop_data.long_val;
        break;

    case DEV_ULONG:
        tango_prop << (Tango::DevULong)prop->prop_data.ulong_val;
        break;

    case DEV_LONG64:
        tango_prop << (Tango::DevLong64)prop->prop_data.long64_val;
        break;

    case DEV_ULONG64:
        tango_prop << (Tango::DevULong64)prop->prop_data.ulong64_val;
        break;

    case DEV_FLOAT:
        tango_prop << prop->prop_data.float_val;
        break;

    case DEV_DOUBLE:
        tango_prop << prop->prop_data.double_val;
        break;

    case DEV_STRING:
    case CONST_DEV_STRING:
        tango_prop << prop->prop_data.string_val;
        break;

#define INSERT_ARRAY(member, type) \
        {                                                               \
            std::vector<type> arr(prop->prop_data.member.length);       \
            memcpy(arr.data(), prop->prop_data.member.sequence,         \
                   sizeof(type) * prop->prop_data.member.length);       \
            tango_prop << arr;                                          \
            break;                                                      \
        }

    case DEVVAR_SHORTARRAY:   INSERT_ARRAY(short_arr, int16_t);
    case DEVVAR_USHORTARRAY:  INSERT_ARRAY(ushort_arr, uint16_t);
    case DEVVAR_LONGARRAY:    INSERT_ARRAY(long_arr, Tango::DevLong);
    case DEVVAR_ULONGARRAY:   INSERT_ARRAY(ulong_arr, Tango::DevULong);
    case DEVVAR_LONG64ARRAY:  INSERT_ARRAY(long64_arr, Tango::DevLong64);
    case DEVVAR_ULONG64ARRAY: INSERT_ARRAY(ulong64_arr, Tango::DevULong64);
    case DEVVAR_FLOATARRAY:   INSERT_ARRAY(float_arr, float);
    case DEVVAR_DOUBLEARRAY:  INSERT_ARRAY(double_arr, double);

    case DEVVAR_STRINGARRAY: {
        std::vector<std::string> string_arr(prop->prop_data.string_arr.length);

        for (uint32_t i = 0; i < prop->prop_data.string_arr.length; i++) {
            string_arr[i] = prop->prop_data.string_arr.sequence[i];
        }

        tango_prop << string_arr;
        break;
    }

    default:
        Tango::Except::throw_exception(
            "Data type error",
            "The requested data type is not implemented for property writing!",
            "c_tango_dbase.c::convert_property_writing()");
        break;
    }
}