-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathhalmodule.cc
More file actions
2431 lines (2154 loc) · 78.7 KB
/
Copy pathhalmodule.cc
File metadata and controls
2431 lines (2154 loc) · 78.7 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This is a component of AXIS, a front-end for emc
// Copyright 2004, 2005, 2006 Jeff Epler <jepler@unpythonic.net> and
// Chris Radek <chris@timeguy.com>
// Copyright 2026 B.Stultiens
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <Python.h>
#include <structmember.h>
#include <string>
#include <map>
#include <vector>
#include <rtapi.h>
#include <rtapi_mutex.h>
#include "hal.h"
#include "hal_priv.h"
#include "config.h"
#define EXCEPTION_IF_NOT_LIVE(retval) do { \
if(self->hal_id <= 0) { \
PyErr_SetString(PyExc_RuntimeError, "Invalid operation on closed HAL component"); \
return retval; \
} \
} while(0)
#define TEST_HAL_SHMEM_BASE(fname) do { \
if(!hal_shmem_base) { \
PyErr_Format(PyExc_RuntimeError, "%s: Cannot call before creating component", fname); \
return NULL; \
} \
} while(0)
// Use stdint interfaces when we have them
#if PY_VERSION_HEX >= 0x030e00f0 // 3.14
#define PyLong_FromRtapiS32 PyLong_FromInt32
#define PyLong_FromRtapiS64 PyLong_FromInt64
#define PyLong_FromRtapiU32 PyLong_FromUInt32
#define PyLong_FromRtapiU64 PyLong_FromUInt64
#else
#define PyLong_FromRtapiS32 PyLong_FromLong
#define PyLong_FromRtapiS64 PyLong_FromLongLong
#define PyLong_FromRtapiU32 PyLong_FromUnsignedLong
#define PyLong_FromRtapiU64 PyLong_FromUnsignedLongLong
#endif
//
// Thread-safe locale switcher for LC_NUMERIC to "C" that can be employed
// within a scoped block and automatically reverts to the previous locale when
// the class instance is destructed.
//
struct scoped_lc_numeric_c {
scoped_lc_numeric_c() {
// Make sure we always use the C locale for conversion (thread local)
// cppcheck-suppress useInitializationList
oldlc = uselocale(static_cast<locale_t>(0));
newlc = newlocale(LC_NUMERIC_MASK, "C", static_cast<locale_t>(0));
if(static_cast<locale_t>(0) == newlc) {
// FIXME: This is not nice, to print directly to stderr...
fprintf(stderr, "halmodule: internal error: Cannot set locale to \"C\" for numeric conversions");
return;
}
uselocale(newlc);
}
~scoped_lc_numeric_c() {
if(static_cast<locale_t>(0) != newlc) {
uselocale(oldlc);
freelocale(newlc);
}
}
private:
locale_t oldlc;
locale_t newlc;
};
//
// Acquire the HAL mutex for operations.
// *****
// NOTE: the HAL mutex is _not_ recursive safe!
// *****
// The mutex is automatically released when the instance is destructed.
//
struct scoped_hal_mutex {
scoped_hal_mutex() {
rtapi_mutex_get(&(hal_data->mutex));
}
~scoped_hal_mutex() {
rtapi_mutex_give(&(hal_data->mutex));
}
};
PyObject *to_python(bool b) {
return PyBool_FromLong(b);
}
PyObject *to_python(rtapi_u32 u) {
return PyLong_FromRtapiU32(u);
}
PyObject *to_python(rtapi_s32 i) {
return PyLong_FromRtapiS32(i);
}
PyObject *to_python(rtapi_u64 u) {
return PyLong_FromRtapiU64(u);
}
PyObject *to_python(rtapi_s64 i) {
return PyLong_FromRtapiS64(i);
}
PyObject *to_python(double d) {
return PyFloat_FromDouble(d);
}
bool from_python(PyObject *o, bool *b)
{
// If it is a bool (True, False), we're fine
if(PyBool_Check(o)) {
*b = PyObject_IsTrue(o);
return true;
}
// Maybe a textual description of a bool
if(PyUnicode_Check(o)) {
// Note that in python: bool("False") == True
// That means we cannot use the standard conversion if it is expected
// to work more natural and aligned with the ini-file format.
const char *cptr = PyUnicode_AsUTF8AndSize(o, NULL);
if (!cptr) {
PyErr_Format(PyExc_RuntimeError, "Invalid UTF-8 detected");
return false;
}
static const struct {
const char *name;
bool value;
} boolnames[] = {
// Both "1" and "0" could be caught with PyNumber_Long, but we're
// already here and it is easier this way. These are in the order
// in which you are most likely to see the value written.
{ "1", true },
{ "0", false },
{ "true", true },
{ "false", false },
{ "on", true },
{ "off", false },
{ "yes", true },
{ "no", false },
{ NULL, false } // Termination
};
for(int i = 0; boolnames[i].name; i++) {
if (!strcasecmp(cptr, boolnames[i].name)) {
*b = boolnames[i].value;
return true;
}
}
}
if(PyFloat_Check(o)) {
// Floating point is false *only* when it is 0.0
double v = PyFloat_AsDouble(o);
*b = 0.0 == v;
return true;
}
// Try the usual int(obj) conversion
PyObject *tmp = 0;
long long l;
tmp = PyLong_Check(o) ? o : PyNumber_Long(o);
if(!tmp) goto fail;
l = PyLong_AsLongLong(tmp);
if(-1 == l && PyErr_Occurred())
goto fail;
*b = l != 0;
if(tmp && tmp != o) Py_XDECREF(tmp);
return true;
fail:
if(tmp && tmp != o) Py_XDECREF(tmp);
return false;
}
bool from_python(PyObject *o, double *d) {
if(PyFloat_Check(o)) {
*d = PyFloat_AsDouble(o);
return true;
} else if(PyLong_Check(o)) {
*d = PyLong_AsDouble(o);
return !PyErr_Occurred();
}
// Ensure that float conversions are using decimal '.'
scoped_lc_numeric_c force_lc_numeric_c;
PyObject *tmp = PyNumber_Float(o);
if(!tmp) {
PyErr_Format(PyExc_TypeError, "Number expected, not %s", Py_TYPE(o)->tp_name);
return false;
}
*d = PyFloat_AsDouble(tmp);
Py_XDECREF(tmp);
return true;
}
bool from_python(PyObject *o, rtapi_u32 *u) {
PyObject *tmp = 0;
long long l;
tmp = PyLong_Check(o) ? o : PyNumber_Long(o);
if(!tmp) goto fail;
l = PyLong_AsLongLong(tmp);
if(-1 == l && PyErr_Occurred()) goto fail;
if(l < 0 || l > RTAPI_UINT32_MAX) {
PyErr_Format(PyExc_OverflowError, "Value %lld out of range", l);
goto fail;
}
*u = l;
if(tmp && tmp != o) Py_XDECREF(tmp);
return true;
fail:
if(tmp && tmp != o) Py_XDECREF(tmp);
return false;
}
bool from_python(PyObject *o, rtapi_s32 *i) {
PyObject *tmp = 0;
long long l;
tmp = PyLong_Check(o) ? o : PyNumber_Long(o);
if(!tmp) goto fail;
l = PyLong_AsLongLong(tmp);
if(-1 == l && PyErr_Occurred()) goto fail;
if(l < RTAPI_INT32_MIN || l > RTAPI_INT32_MAX) {
PyErr_Format(PyExc_OverflowError, "Value %lld out of range", l);
goto fail;
}
*i = l;
if(tmp && tmp != o) Py_XDECREF(tmp);
return true;
fail:
if(tmp && tmp != o) Py_XDECREF(tmp);
return false;
}
bool from_python(PyObject *o, rtapi_u64 *u) {
PyObject *tmp = 0;
unsigned long long l;
tmp = PyLong_Check(o) ? o : PyNumber_Long(o);
if(!tmp) goto fail;
l = PyLong_AsUnsignedLongLong(tmp);
if((unsigned long long)-1 == l && PyErr_Occurred())
goto fail;
*u = l;
if(tmp && tmp != o) Py_XDECREF(tmp);
return true;
fail:
if(tmp && tmp != o) Py_XDECREF(tmp);
return false;
}
bool from_python(PyObject *o, rtapi_s64 *i) {
PyObject *tmp = 0;
long long l;
tmp = PyLong_Check(o) ? o : PyNumber_Long(o);
if(!tmp) goto fail;
l = PyLong_AsLongLong(tmp);
if(-1 == l && PyErr_Occurred())
goto fail;
*i = l;
if(tmp && tmp != o) Py_XDECREF(tmp);
return true;
fail:
if(tmp && tmp != o) Py_XDECREF(tmp);
return false;
}
union paramunion {
hal_bit_t b;
hal_u32_t u32;
hal_s32_t s32;
hal_u64_t u64;
hal_s64_t s64;
hal_float_t f;
};
union pinunion {
void *v;
hal_bit_t *b;
hal_u32_t *u32;
hal_s32_t *s32;
hal_u64_t *u64;
hal_s64_t *s64;
hal_float_t *f;
hal_port_t *p;
};
union halunion {
union pinunion pin;
union paramunion param;
};
union haldirunion {
hal_pin_dir_t pindir;
hal_param_dir_t paramdir;
};
struct halitem {
bool is_pin;
hal_type_t type;
union haldirunion dir;
union halunion *u;
};
struct pyhalitem {
PyObject_HEAD
halitem pin;
char * name;
};
static PyObject * pyhal_pin_new(halitem * pin, const char *name);
typedef std::map<std::string, struct halitem> itemmap;
typedef struct halobject {
PyObject_HEAD
int hal_id;
char *name;
char *prefix;
itemmap *items;
} halobject;
PyObject *pyhal_error_type = NULL;
static PyObject *pyrtapi_error(int code) {
PyErr_SetString(pyhal_error_type, strerror(-code));
return NULL;
}
static PyObject *pyhal_error(int code) {
PyErr_SetString(pyhal_error_type, strerror(-code));
return NULL;
}
static int pyhal_init(PyObject *_self, PyObject *args, PyObject *kw) {
(void)kw;
const char *name;
const char *prefix = 0;
halobject *self = reinterpret_cast<halobject *>(_self);
if(!PyArg_ParseTuple(args, "s|s:hal.component", &name, &prefix)) return -1;
self->items = new itemmap();
self->hal_id = hal_init(name);
if(self->hal_id <= 0) {
pyhal_error(self->hal_id);
return -1;
}
self->name = strdup(name);
self->prefix = strdup(prefix ? prefix : name);
if(!self->name) {
PyErr_SetString(PyExc_MemoryError, "strdup(name) failed");
return -1;
}
if(!self->prefix) {
PyErr_SetString(PyExc_MemoryError, "strdup(prefix) failed");
return -1;
}
return 0;
}
static void pyhal_exit_impl(halobject *self) {
if(self->hal_id > 0)
hal_exit(self->hal_id);
self->hal_id = 0;
free(self->name);
self->name = 0;
free(self->prefix);
self->prefix = 0;
delete self->items;
self->items = 0;
}
static void pyhal_delete(PyObject *_self) {
halobject *self = reinterpret_cast<halobject *>(_self);
pyhal_exit_impl(self);
Py_TYPE(self)->tp_free(self);
}
static int pyhal_write_common(halitem *pin, PyObject *value) {
if(!pin) return -1;
if(pin->is_pin) {
switch(pin->type) {
case HAL_BIT: {
bool tmp;
if(!from_python(value, &tmp)) return -1;
*pin->u->pin.b = tmp;
break;
}
case HAL_FLOAT: {
double tmp;
if(!from_python(value, &tmp)) return -1;
*pin->u->pin.f = tmp;
break;
}
case HAL_U32: {
rtapi_u32 tmp;
if(!from_python(value, &tmp)) return -1;
*pin->u->pin.u32 = tmp;
break;
}
case HAL_S32: {
rtapi_s32 tmp;
if(!from_python(value, &tmp)) return -1;
*pin->u->pin.s32 = tmp;
break;
}
case HAL_U64: {
rtapi_u64 tmp;
if(!from_python(value, &tmp)) return -1;
*pin->u->pin.u64 = tmp;
break;
}
case HAL_S64: {
rtapi_s64 tmp;
if(!from_python(value, &tmp)) return -1;
*pin->u->pin.s64 = tmp;
break;
}
default:
PyErr_Format(pyhal_error_type, "Invalid pin type %d", pin->type);
}
} else {
switch(pin->type) {
case HAL_BIT: {
bool tmp;
if(!from_python(value, &tmp)) return -1;
pin->u->param.b = tmp;
break;
}
case HAL_FLOAT: {
double tmp;
if(!from_python(value, &tmp)) return -1;
pin->u->param.f = tmp;
break;
}
case HAL_U32: {
rtapi_u32 tmp;
if(!from_python(value, &tmp)) return -1;
pin->u->param.u32 = tmp;
break;
}
case HAL_S32:
rtapi_s32 tmp;
if(!from_python(value, &tmp)) return -1;
pin->u->param.s32 = tmp;
break;
case HAL_U64: {
rtapi_u64 tmp;
if(!from_python(value, &tmp)) return -1;
pin->u->param.u64 = tmp;
break;
}
case HAL_S64: {
rtapi_s64 tmp;
if(!from_python(value, &tmp)) return -1;
pin->u->param.s64 = tmp;
break;
}
default:
PyErr_Format(pyhal_error_type, "Invalid pin type %d", pin->type);
}
}
return 0;
}
static PyObject *pyhal_read_common(halitem *item) {
if(!item) return NULL;
if(item->is_pin) {
switch(item->type) {
case HAL_BIT: return to_python(*(item->u->pin.b));
case HAL_U32: return to_python(*(item->u->pin.u32));
case HAL_S32: return to_python(*(item->u->pin.s32));
case HAL_U64: return to_python(*(item->u->pin.u64));
case HAL_S64: return to_python(*(item->u->pin.s64));
case HAL_FLOAT: return to_python(*(item->u->pin.f));
case HAL_PORT: return to_python(hal_port_buffer_size(item->u->pin.p));
default:
break;
}
} else {
switch(item->type) {
case HAL_BIT: return to_python(item->u->param.b);
case HAL_U32: return to_python(item->u->param.u32);
case HAL_S32: return to_python(item->u->param.s32);
case HAL_U64: return to_python(item->u->param.u64);
case HAL_S64: return to_python(item->u->param.s64);
case HAL_FLOAT: return to_python(item->u->param.f);
case HAL_PORT: return to_python((unsigned)0); // HAL_PORT cannot be a parameter
default:
break;
}
}
PyErr_Format(pyhal_error_type, "Invalid item type %d", item->type);
return NULL;
}
static halitem *find_item(halobject *self, const char *name) {
if(!name) return NULL;
itemmap::iterator i = self->items->find(name);
if(i == self->items->end()) {
PyErr_Format(PyExc_AttributeError, "Pin or param '%s' does not exist", name);
return NULL;
}
return &(i->second);
}
static PyObject * pyhal_create_param(halobject *self, const char *name, hal_type_t type, hal_param_dir_t dir) {
char param_name[HAL_NAME_LEN+1];
int res;
halitem param;
param.is_pin = 0;
/* FIXME consider type < HAL_TYPE_UNSPECIFIED || HAL_TYPE_MAX <= type */
if(type < HAL_BIT || type > HAL_U64) {
PyErr_Format(pyhal_error_type, "Invalid param type %d", type);
return NULL;
}
param.type = type;
param.dir.paramdir = dir;
param.u = (halunion*)hal_malloc(sizeof(halunion));
if(!param.u) {
PyErr_SetString(PyExc_MemoryError, "hal_malloc failed");
return NULL;
}
res = snprintf(param_name, sizeof(param_name), "%s.%s", self->prefix, name);
if(res > HAL_NAME_LEN || res < 0) { return pyhal_error(-EINVAL); }
res = hal_param_new(param_name, type, dir, (void*)param.u, self->hal_id);
if(res) return pyhal_error(res);
(*self->items)[name] = param;
return pyhal_pin_new(¶m, name);
}
static PyObject * pyhal_create_pin(halobject *self, const char *name, hal_type_t type, hal_pin_dir_t dir) {
char pin_name[HAL_NAME_LEN+1];
int res;
halitem pin;
pin.is_pin = 1;
if(type < HAL_BIT || type > HAL_U64) {
PyErr_Format(pyhal_error_type, "Invalid pin type %d", type);
return NULL;
}
pin.type = type;
pin.dir.pindir = dir;
pin.u = (halunion*)hal_malloc(sizeof(halunion));
if(!pin.u) {
PyErr_SetString(PyExc_MemoryError, "hal_malloc failed");
return NULL;
}
res = snprintf(pin_name, sizeof(pin_name), "%s.%s", self->prefix, name);
if(res > HAL_NAME_LEN || res < 0) {
PyErr_Format(pyhal_error_type,
"Invalid pin name length \"%s.%s\": max = %d characters",
self->prefix, name, HAL_NAME_LEN);
return NULL;
}
res = hal_pin_new(pin_name, type, dir, (void**)pin.u, self->hal_id);
if(res) return pyhal_error(res);
(*self->items)[name] = pin;
return pyhal_pin_new(&pin, name);
}
static PyObject *pyhal_new_param(PyObject *_self, PyObject *o) {
const char *name;
int type, dir;
halobject *self = reinterpret_cast<halobject *>(_self);
if(!PyArg_ParseTuple(o, "sii", &name, &type, &dir))
return NULL;
EXCEPTION_IF_NOT_LIVE(NULL);
if (find_item(self, name)) {
PyErr_Format(PyExc_ValueError, "Duplicate parameter name '%s'", name);
return NULL;
} else { PyErr_Clear(); }
return pyhal_create_param(self, name, (hal_type_t)type, (hal_param_dir_t)dir);
}
static PyObject *pyhal_new_pin(PyObject *_self, PyObject *o) {
const char *name;
int type, dir;
halobject *self = reinterpret_cast<halobject *>(_self);
if(!PyArg_ParseTuple(o, "sii", &name, &type, &dir))
return NULL;
EXCEPTION_IF_NOT_LIVE(NULL);
if (find_item(self, name)) {
PyErr_Format(PyExc_ValueError, "Duplicate pin name '%s'", name);
return NULL;
} else { PyErr_Clear(); }
return pyhal_create_pin(self, name, (hal_type_t)type, (hal_pin_dir_t)dir);
}
enum what_type_e {
WHAT_ANY,
WHAT_PIN,
WHAT_PARAM,
};
static PyObject *get_pin_or_param(halobject *self, PyObject *args, what_type_e what)
{
const char *name;
if(!PyArg_ParseTuple(args, "s", &name))
return NULL;
EXCEPTION_IF_NOT_LIVE(NULL);
halitem *pin = find_item(self, name);
if(!pin)
return NULL;
if(what == WHAT_ANY || (pin->is_pin && what == WHAT_PIN) || (!pin->is_pin && what == WHAT_PARAM))
return pyhal_pin_new(pin, name);
PyErr_Format(PyExc_AttributeError, "%s '%s' does not exist", what == WHAT_PIN ? "Pin" : "Param", name);
return NULL;
}
static PyObject *pyhal_get_pin(PyObject *self, PyObject *args)
{
return get_pin_or_param(reinterpret_cast<halobject *>(self), args, WHAT_PIN);
}
static PyObject *pyhal_get_param(PyObject *self, PyObject *args)
{
return get_pin_or_param(reinterpret_cast<halobject *>(self), args, WHAT_PARAM);
}
static PyObject *pyhal_get_item(PyObject *self, PyObject *args)
{
return get_pin_or_param(reinterpret_cast<halobject *>(self), args, WHAT_ANY);
}
static PyObject *pyhal_get_pins(PyObject *_self, PyObject * /*o*/) {
halobject *self = reinterpret_cast<halobject *>(_self);
EXCEPTION_IF_NOT_LIVE(NULL);
PyObject *d = PyDict_New();
for(itemmap::iterator i = self->items->begin(); i != self->items->end(); ++i) {
halitem * pin = &(i->second);
PyDict_SetItem(d, PyUnicode_FromString(i->first.c_str()), pyhal_read_common(pin));
}
return d;
}
static PyObject *pyhal_ready(PyObject *_self, PyObject * /*o*/) {
// hal_ready did not exist in EMC 2.0.x, make it a no-op
halobject *self = reinterpret_cast<halobject *>(_self);
EXCEPTION_IF_NOT_LIVE(NULL);
int res = hal_ready(self->hal_id);
if(res) return pyhal_error(res);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pyhal_unready(PyObject *_self, PyObject * /*o*/) {
// hal_ready did not exist in EMC 2.0.x, make it a no-op
halobject *self = reinterpret_cast<halobject *>(_self);
EXCEPTION_IF_NOT_LIVE(NULL);
int res = hal_unready(self->hal_id);
if(res) return pyhal_error(res);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pyhal_exit(PyObject *_self, PyObject * /*o*/) {
halobject *self = reinterpret_cast<halobject *>(_self);
pyhal_exit_impl(self);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pyhal_repr(PyObject *_self) {
halobject *self = reinterpret_cast<halobject *>(_self);
return PyUnicode_FromFormat("<hal component %s(%d) with %d pins and params>",
self->name, self->hal_id, (int)self->items->size());
}
static PyObject *pyhal_getattro(PyObject *_self, PyObject *attro) {
PyObject *result;
halobject *self = reinterpret_cast<halobject *>(_self);
EXCEPTION_IF_NOT_LIVE(NULL);
result = PyObject_GenericGetAttr(reinterpret_cast<PyObject*>(self), attro);
if(result) return result;
PyErr_Clear();
return pyhal_read_common(find_item(self, PyUnicode_AsUTF8(attro)));
}
static int pyhal_setattro(PyObject *_self, PyObject *attro, PyObject *v) {
halobject *self = reinterpret_cast<halobject *>(_self);
// FIXME: The documentation states that when v==NULL it means to delete the
// attribute and it must be supported.
EXCEPTION_IF_NOT_LIVE(-1);
return pyhal_write_common(find_item(self, PyUnicode_AsUTF8(attro)), v);
}
static Py_ssize_t pyhal_len(PyObject *_self) {
halobject* self = reinterpret_cast<halobject*>(_self);
EXCEPTION_IF_NOT_LIVE(-1);
return self->items->size();
}
static PyObject *pyhal_get_prefix(PyObject *_self, PyObject *args) {
halobject* self = reinterpret_cast<halobject*>(_self);
if(!PyArg_ParseTuple(args, "")) return NULL;
EXCEPTION_IF_NOT_LIVE(NULL);
if(!self->prefix) {
Py_INCREF(Py_None);
return Py_None;
}
return PyUnicode_FromString(self->prefix);
}
static PyObject *pyhal_set_prefix(PyObject *_self, PyObject *args) {
const char *newprefix;
halobject* self = reinterpret_cast<halobject*>(_self);
if(!PyArg_ParseTuple(args, "s", &newprefix)) return NULL;
EXCEPTION_IF_NOT_LIVE(NULL);
if(self->prefix)
free(self->prefix);
self->prefix = strdup(newprefix);
if(!self->prefix) {
PyErr_SetString(PyExc_MemoryError, "strdup(prefix) failed");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef hal_methods[] = {
{"setprefix", pyhal_set_prefix, METH_VARARGS,
"Set the prefix for newly created pins and parameters"},
{"getprefix", pyhal_get_prefix, METH_VARARGS,
"Get the prefix for newly created pins and parameters"},
{"newparam", pyhal_new_param, METH_VARARGS,
"Create a new parameter"},
{"newpin", pyhal_new_pin, METH_VARARGS,
"Create a new pin"},
{"getitem", pyhal_get_item, METH_VARARGS,
"Get existing pin or param object"},
{"getpin", pyhal_get_pin, METH_VARARGS,
"Get existing pin object"},
{"getparam", pyhal_get_param, METH_VARARGS,
"Get existing param object"},
{"getpins", pyhal_get_pins, METH_VARARGS,
"Get all pins and values of component"},
{"exit", pyhal_exit, METH_NOARGS,
"Call hal_exit"},
{"ready", pyhal_ready, METH_NOARGS,
"Call hal_ready"},
{"unready", pyhal_unready, METH_NOARGS,
"Call hal_unready"},
{},
};
static PyMappingMethods halobject_map = {
pyhal_len,
pyhal_getattro,
pyhal_setattro
};
static
PyTypeObject halobject_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"hal.component", /*tp_name*/
sizeof(halobject), /*tp_basicsize*/
0, /*tp_itemsize*/
pyhal_delete, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
pyhal_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
&halobject_map, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
pyhal_getattro, /*tp_getattro*/
pyhal_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
"HAL Component", /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
hal_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
pyhal_init, /*tp_init*/
0, /*tp_alloc*/
PyType_GenericNew, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
0, /*tp_bases*/
0, /*tp_mro*/
0, /*tp_cache*/
0, /*tp_subclasses*/
0, /*tp_weaklink*/
0, /*tp_del*/
0, /*tp_version_tag*/
0, /*tp_finalize*/
#if PY_VERSION_HEX >= 0x030800f0 // 3.8
0, /*tp_vectorcall*/
#if PY_VERSION_HEX >= 0x030c00f0 // 3.12
0, /*tp_watched*/
#if PY_VERSION_HEX >= 0x030d00f0 // 3.13
0, /*tp_versions_used*/
#endif
#endif
#endif
};
static const char * pin_type2name(hal_type_t type) {
switch (type) {
case HAL_BIT: return "BIT";
case HAL_S32: return "S32";
case HAL_U32: return "U32";
case HAL_S64: return "S64";
case HAL_U64: return "U64";
case HAL_FLOAT: return "FLOAT";
case HAL_PORT: return "PORT";
default: return "unknown";
}
}
static const char * pin_dir2name(hal_pin_dir_t type) {
switch (type) {
case HAL_IN: return "IN";
case HAL_IO: return "IO";
case HAL_OUT: return "OUT";
default: return "unknown";
}
}
static const char * param_dir2name(hal_param_dir_t type) {
switch (type) {
case HAL_RO: return "RO";
case HAL_RW: return "RW";
default: return "unknown";
}
}
static PyObject *pyhalpin_repr(PyObject *_self) {
pyhalitem *pyself = reinterpret_cast<pyhalitem *>(_self);
halitem *self = &pyself->pin;
const char * name = "(null)";
if (pyself->name) name = pyself->name;
if (!self->is_pin)
return PyUnicode_FromFormat("<hal param \"%s\" %s-%s>", name,
pin_type2name(self->type), param_dir2name(self->dir.paramdir));
return PyUnicode_FromFormat("<hal pin \"%s\" %s-%s>", name,
pin_type2name(self->type), pin_dir2name(self->dir.pindir));
}
static int pyhalpin_init(PyObject * /*_self*/, PyObject *, PyObject *) {
PyErr_Format(PyExc_RuntimeError, "Cannot be constructed directly");
return -1;
}
static void pyhalpin_delete(PyObject *_self) {
pyhalitem *self = reinterpret_cast<pyhalitem *>(_self);
if(self->name) free(self->name);
PyObject_Del(self);
}
static PyObject * pyhal_pin_set(PyObject * _self, PyObject * value) {
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
if (pyhal_write_common(&self->pin, value) == -1)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject * pyhal_pin_get(PyObject * _self, PyObject *) {
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
return pyhal_read_common(&self->pin);
}
static PyObject * pyhal_pin_get_type(PyObject * _self, PyObject *) {
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
return PyLong_FromLong(self->pin.type);
}
static PyObject * pyhal_pin_get_dir(PyObject * _self, PyObject *) {
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
if (self->pin.is_pin)
return PyLong_FromLong(self->pin.dir.pindir);
else
return PyLong_FromLong(self->pin.dir.paramdir);
}
static PyObject * pyhal_pin_is_pin(PyObject * _self, PyObject *) {
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
return PyBool_FromLong(self->pin.is_pin);
}
static PyObject * pyhal_pin_get_name(PyObject * _self, PyObject *) {
pyhalitem * self = reinterpret_cast<pyhalitem *>(_self);
if (!self->name) {
Py_INCREF(Py_None);
return Py_None;
}
return PyUnicode_FromString(self->name);
}
// - - - -
// Port methods
static bool check_port(const pyhalitem *item, const char *pfx)
{
if(!item->pin.is_pin) {
PyErr_Format(PyExc_RuntimeError, "%s: %s: Not a pin", pfx, item->name);
return false;
}
if(item->pin.type != HAL_PORT) {
PyErr_Format(PyExc_RuntimeError, "%s: %s: Pin type not HAL_PORT but '%d'", pfx, (int)item->pin.type);
return false;
}
return true;
}
static PyObject *pyhal_port_write(PyObject *self, PyObject *o)
{
pyhalitem *item = reinterpret_cast<pyhalitem *>(self);
if(!check_port(item, "write"))
return NULL;
if(item->pin.dir.pindir != HAL_OUT) {
PyErr_Format(PyExc_RuntimeError, "write: %s: Pin not output", item->name);
return NULL;