-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathhal_lib.c
More file actions
4799 lines (4385 loc) · 142 KB
/
Copy pathhal_lib.c
File metadata and controls
4799 lines (4385 loc) · 142 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
/** HAL stands for Hardware Abstraction Layer, and is used by EMC to
transfer realtime data to and from I/O devices and other low-level
modules.
*/
/********************************************************************
* Description: hal_libc.c
* This file, 'hal_lib.c', implements the HAL API, for
* both user space and realtime modules. It uses the
* RTAPI and ULAPI #define symbols to determine which
* version to compile.
*
* Author: John Kasunich
* License: LGPL Version 2
*
* Copyright (c) 2003 All rights reserved.
*
* Last change:
********************************************************************/
/** Copyright (C) 2003 John Kasunich
<jmkasunich AT users DOT sourceforge DOT net>
Other contributors:
Paul Fox
<pgf AT foxharp DOT boston DOT ma DOT us>
Alex Joni
<alex_joni AT users DOT sourceforge DOT net>
*/
/** This library is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU Library General
Public License as published by the Free Software Foundation.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
ANY HARM OR LOSS RESULTING FROM ITS USE. IT IS _EXTREMELY_ UNWISE
TO RELY ON SOFTWARE ALONE FOR SAFETY. Any machinery capable of
harming persons must have provisions for completely removing power
from all motors, etc, before persons enter any danger area. All
machinery must be designed to comply with local and national safety
codes, and the authors of this software can not, and do not, take
any responsibility for such compliance.
This code was written as part of the EMC HAL project. For more
information, go to www.linuxcnc.org.
*/
#define RTAPI_HAL_PRIV /* Use rtapi/hal private functions */
#include <rtapi.h> /* RTAPI realtime OS API */
#include "hal.h" /* HAL public API decls */
#include "hal_priv.h" /* HAL private decls */
#include <rtapi_string.h>
#include <rtapi_atomic.h>
#ifdef RTAPI
#include <rtapi_app.h>
/* module information */
MODULE_AUTHOR("John Kasunich");
MODULE_DESCRIPTION("Hardware Abstraction Layer for EMC");
MODULE_LICENSE("GPL");
#endif /* RTAPI */
#if defined(ULAPI)
#include <sys/types.h> /* pid_t */
#include <unistd.h> /* getpid() */
#include <time.h>
#endif
char *hal_shmem_base = 0;
hal_data_t *hal_data = 0;
static int lib_module_id = -1; /* RTAPI module ID for library module */
static int lib_mem_id = 0; /* RTAPI shmem ID for library module */
/***********************************************************************
* LOCAL FUNCTION DECLARATIONS *
************************************************************************/
/* These functions are used internally by this file. The code is at
the end of the file. */
/** init_hal_data() initializes the entire HAL data structure, only
if the structure has not already been initialized. (The init
is done by the first HAL component to be loaded.
*/
static int init_hal_data(void);
/** The 'shmalloc_xx()' functions allocate blocks of shared memory.
Each function allocates a block that is 'size' bytes long.
If 'size' is 3 or more, the block is aligned on a 4 byte
boundary. If 'size' is 2, it is aligned on a 2 byte boundary,
and if 'size' is 1, it is unaligned.
These functions do not test a mutex - they are called from
within the hal library by code that already has the mutex.
(The public function 'hal_malloc()' is a wrapper that gets the
mutex and then calls 'shmalloc_up()'.)
The only difference between the two functions is the location
of the allocated memory. 'shmalloc_up()' allocates from the
base of shared memory and works upward, while 'shmalloc_dn()'
starts at the top and works down.
This is done to improve realtime performance. 'shmalloc_up()'
is used to allocate data that will be accessed by realtime
code, while 'shmalloc_dn()' is used to allocate the much
larger structures that are accessed only occasionally during
init. This groups all the realtime data together, improving
cache performance.
*/
static void *shmalloc_up(long int size);
static void *shmalloc_dn(long int size);
/** The alloc_xxx_struct() functions allocate a structure of the
appropriate type and return a pointer to it, or 0 if they fail.
They attempt to re-use freed structs first, if none are
available, then they call hal_malloc() to create a new one.
The free_xxx_struct() functions add the structure at 'p' to
the appropriate free list, for potential re-use later.
All of these functions assume that the caller has already
grabbed the hal_data mutex.
*/
hal_comp_t *halpr_alloc_comp_struct(void);
static hal_pin_t *alloc_pin_struct(void);
static hal_sig_t *alloc_sig_struct(void);
static hal_param_t *alloc_param_struct(void);
static hal_oldname_t *halpr_alloc_oldname_struct(void);
#ifdef RTAPI
static hal_funct_t *alloc_funct_struct(void);
#endif /* RTAPI */
static hal_funct_entry_t *alloc_funct_entry_struct(void);
#ifdef RTAPI
static hal_thread_t *alloc_thread_struct(void);
#endif /* RTAPI */
static void free_comp_struct(hal_comp_t * comp);
static void unlink_pin(hal_pin_t * pin);
static void free_pin_struct(hal_pin_t * pin);
static void free_sig_struct(hal_sig_t * sig);
static void free_param_struct(hal_param_t * param);
static void free_oldname_struct(hal_oldname_t * oldname);
#ifdef RTAPI
static void free_funct_struct(hal_funct_t * funct);
#endif /* RTAPI */
static void free_funct_entry_struct(hal_funct_entry_t * funct_entry);
#ifdef RTAPI
static void free_thread_struct(hal_thread_t * thread);
#endif /* RTAPI */
#ifdef RTAPI
/** 'thread_task()' is a function that is invoked as a realtime task.
It implements a thread, by running down the thread's function list
and calling each function in turn.
*/
static void thread_task(void *arg);
#endif /* RTAPI */
/***********************************************************************
* PUBLIC (API) FUNCTION CODE *
************************************************************************/
static int ref_cnt = 0;
int hal_init(const char *name)
{
int comp_id;
#ifdef ULAPI
int retval;
void *mem;
#endif
char rtapi_name[RTAPI_NAME_LEN + 1];
char hal_name[HAL_NAME_LEN + 1];
hal_comp_t *comp;
if (name == 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "HAL: ERROR: no component name\n");
return -EINVAL;
}
if (strlen(name) > HAL_NAME_LEN) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component name '%s' is too long\n", name);
return -EINVAL;
}
#ifdef ULAPI
if(!lib_mem_id) {
rtapi_print_msg(RTAPI_MSG_DBG, "HAL: initializing hal_lib\n");
rtapi_snprintf(rtapi_name, RTAPI_NAME_LEN, "HAL_LIB_%d", (int)getpid());
lib_module_id = rtapi_init(rtapi_name);
if (lib_module_id < 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: could not initialize RTAPI\n");
return -EINVAL;
}
/* get HAL shared memory block from RTAPI */
lib_mem_id = rtapi_shmem_new(HAL_KEY, lib_module_id, HAL_SIZE);
if (lib_mem_id < 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: could not open shared memory\n");
rtapi_exit(lib_module_id);
return -EINVAL;
}
/* get address of shared memory area */
retval = rtapi_shmem_getptr(lib_mem_id, &mem);
if (retval < 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: could not access shared memory\n");
rtapi_exit(lib_module_id);
return -EINVAL;
}
/* set up internal pointers to shared mem and data structure */
hal_shmem_base = (char *) mem;
hal_data = (hal_data_t *) mem;
/* perform a global init if needed */
retval = init_hal_data();
if ( retval ) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: could not init shared memory\n");
rtapi_exit(lib_module_id);
return -EINVAL;
}
}
#endif
rtapi_print_msg(RTAPI_MSG_DBG, "HAL: initializing component '%s'\n",
name);
/* copy name to local vars, truncating if needed */
rtapi_snprintf(rtapi_name, RTAPI_NAME_LEN, "HAL_%s", name);
rtapi_snprintf(hal_name, sizeof(hal_name), "%s", name);
/* do RTAPI init */
comp_id = rtapi_init(rtapi_name);
if (comp_id < 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "HAL: ERROR: rtapi init failed\n");
return -EINVAL;
}
/* get mutex before manipulating the shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* make sure name is unique in the system */
if (halpr_find_comp_by_name(hal_name) != 0) {
/* a component with this name already exists */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: duplicate component name '%s'\n", hal_name);
rtapi_exit(comp_id);
return -EINVAL;
}
/* allocate a new component structure */
comp = halpr_alloc_comp_struct();
if (comp == 0) {
/* couldn't allocate structure */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: insufficient memory for component '%s'\n", hal_name);
rtapi_exit(comp_id);
return -ENOMEM;
}
/* initialize the structure */
comp->comp_id = comp_id;
#ifdef RTAPI
comp->type = COMPONENT_TYPE_REALTIME;
comp->pid = 0;
#else /* ULAPI */
comp->type = COMPONENT_TYPE_USER;
comp->pid = getpid();
#endif
comp->ready = 0;
comp->shmem_base = hal_shmem_base;
comp->insmod_args = 0;
rtapi_snprintf(comp->name, sizeof(comp->name), "%s", hal_name);
/* insert new structure at head of list */
comp->next_ptr = hal_data->comp_list_ptr;
hal_data->comp_list_ptr = SHMOFF(comp);
/* done with list, release mutex */
rtapi_mutex_give(&(hal_data->mutex));
/* done */
rtapi_print_msg(RTAPI_MSG_DBG,
"HAL: component '%s' initialized, ID = %02d\n", hal_name, comp_id);
ref_cnt ++;
return comp_id;
}
int hal_exit(int comp_id)
{
rtapi_intptr_t *prev, next;
hal_comp_t *comp;
char name[HAL_NAME_LEN + 1];
if (hal_data == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: exit called before init\n");
return -EINVAL;
}
rtapi_print_msg(RTAPI_MSG_DBG, "HAL: removing component %02d\n", comp_id);
/* grab mutex before manipulating list */
rtapi_mutex_get(&(hal_data->mutex));
/* search component list for 'comp_id' */
prev = &(hal_data->comp_list_ptr);
next = *prev;
if (next == 0) {
/* list is empty - should never happen, but... */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
while (comp->comp_id != comp_id) {
/* not a match, try the next one */
prev = &(comp->next_ptr);
next = *prev;
if (next == 0) {
/* reached end of list without finding component */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
}
/* found our component, unlink it from the list */
*prev = comp->next_ptr;
/* save component name for later */
rtapi_snprintf(name, sizeof(name), "%s", comp->name);
/* get rid of the component */
free_comp_struct(comp);
/*! \todo Another #if 0 */
#if 0
/*! \todo FIXME - this is the beginning of a two pronged approach to managing
shared memory. Prong 1 - re-init the shared memory allocator whenever
it is known to be safe. Prong 2 - make a better allocator that can
reclaim memory allocated by components when those components are
removed. To be finished later. */
/* was that the last component? */
if (hal_data->comp_list_ptr == 0) {
/* yes, are there any signals or threads defined? */
if ((hal_data->sig_list_ptr == 0) && (hal_data->thread_list_ptr == 0)) {
/* no, invalidate "magic" number so shmem will be re-inited when
a new component is loaded */
hal_data->magic = 0;
}
}
#endif
/* release mutex */
rtapi_mutex_give(&(hal_data->mutex));
--ref_cnt;
#ifdef ULAPI
if(ref_cnt == 0) {
rtapi_print_msg(RTAPI_MSG_DBG, "HAL: releasing RTAPI resources\n");
/* release RTAPI resources */
rtapi_shmem_delete(lib_mem_id, lib_module_id);
rtapi_exit(lib_module_id);
lib_mem_id = 0;
lib_module_id = -1;
hal_shmem_base = NULL;
hal_data = NULL;
}
#endif
rtapi_exit(comp_id);
/* done */
rtapi_print_msg(RTAPI_MSG_DBG,
"HAL: component %02d removed, name = '%s'\n", comp_id, name);
return 0;
}
void *hal_malloc(long int size)
{
void *retval;
if (hal_data == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: hal_malloc called before init\n");
return 0;
}
if (size <= 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "HAL: ERROR: hal_malloc bad size: %ld\n", size);
return 0;
}
/* get the mutex */
rtapi_mutex_get(&(hal_data->mutex));
/* allocate memory */
retval = shmalloc_up(size);
/* release the mutex */
rtapi_mutex_give(&(hal_data->mutex));
/* check return value */
if (retval == 0) {
rtapi_print_msg(RTAPI_MSG_DBG,
"HAL: hal_malloc() can't allocate %ld bytes\n", size);
}
return retval;
}
#ifdef RTAPI
int hal_set_constructor(int comp_id, constructor make) {
int next;
hal_comp_t *comp;
rtapi_mutex_get(&(hal_data->mutex));
/* search component list for 'comp_id' */
next = hal_data->comp_list_ptr;
if (next == 0) {
/* list is empty - should never happen, but... */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
while (comp->comp_id != comp_id) {
/* not a match, try the next one */
next = comp->next_ptr;
if (next == 0) {
/* reached end of list without finding component */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
}
comp->make = make;
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
#endif
int hal_set_unready(int comp_id) {
hal_comp_t *comp;
rtapi_mutex_get(&(hal_data->mutex));
comp = halpr_find_comp_by_id(comp_id);
if (comp) { comp->ready = 0; }
rtapi_mutex_give(&(hal_data->mutex));
if (comp) {return 0;}
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: hal_set_unready(): component %d not found\n", comp_id);
return -EINVAL;
}
int hal_ready(int comp_id) {
int next;
hal_comp_t *comp;
rtapi_mutex_get(&(hal_data->mutex));
/* search component list for 'comp_id' */
next = hal_data->comp_list_ptr;
if (next == 0) {
/* list is empty - should never happen, but... */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
while (comp->comp_id != comp_id) {
/* not a match, try the next one */
next = comp->next_ptr;
if (next == 0) {
/* reached end of list without finding component */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
}
if(comp->ready > 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: Component '%s' already ready\n", comp->name);
rtapi_mutex_give(&(hal_data->mutex));
return -EINVAL;
}
comp->ready = 1;
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
int hal_unready(int comp_id) {
int next;
hal_comp_t *comp;
rtapi_mutex_get(&(hal_data->mutex));
/* search component list for 'comp_id' */
next = hal_data->comp_list_ptr;
if (next == 0) {
/* list is empty - should never happen, but... */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
while (comp->comp_id != comp_id) {
/* not a match, try the next one */
next = comp->next_ptr;
if (next == 0) {
/* reached end of list without finding component */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
comp = SHMPTR(next);
}
if(comp->ready < 1) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: Component '%s' already unready\n", comp->name);
rtapi_mutex_give(&(hal_data->mutex));
return -EINVAL;
}
comp->ready = 0;
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
char *hal_comp_name(int comp_id)
{
hal_comp_t *comp;
char *result = NULL;
rtapi_mutex_get(&(hal_data->mutex));
comp = halpr_find_comp_by_id(comp_id);
if(comp) result = comp->name;
rtapi_mutex_give(&(hal_data->mutex));
return result;
}
hal_realtime_type_t hal_get_realtime_type() {
if (hal_data == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: hal_get_realtime_type called before init\n");
return -EINVAL;
}
return hal_data->realtime_type;
}
/***********************************************************************
* "LOCKING" FUNCTIONS *
************************************************************************/
/** The 'hal_set_lock()' function sets locking based on one of the
locking types defined in hal.h
*/
int hal_set_lock(unsigned char lock_type) {
if (hal_data == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: set_lock called before init\n");
return -EINVAL;
}
hal_data->lock = lock_type;
return 0;
}
/** The 'hal_get_lock()' function returns the current locking level
locking types defined in hal.h
*/
unsigned char hal_get_lock() {
if (hal_data == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: get_lock called before init\n");
return -EINVAL;
}
return hal_data->lock;
}
/***********************************************************************
* "PIN" FUNCTIONS *
************************************************************************/
/* wrapper functs for typed pins - these call the generic funct below */
int hal_pin_bit_new(const char *name, hal_pin_dir_t dir,
hal_bit_t ** data_ptr_addr, int comp_id)
{
return hal_pin_new(name, HAL_BIT, dir, (void **) data_ptr_addr, comp_id);
}
int hal_pin_float_new(const char *name, hal_pin_dir_t dir,
hal_float_t ** data_ptr_addr, int comp_id)
{
return hal_pin_new(name, HAL_FLOAT, dir, (void **) data_ptr_addr,
comp_id);
}
int hal_pin_u32_new(const char *name, hal_pin_dir_t dir,
hal_u32_t ** data_ptr_addr, int comp_id)
{
return hal_pin_new(name, HAL_U32, dir, (void **) data_ptr_addr, comp_id);
}
int hal_pin_s32_new(const char *name, hal_pin_dir_t dir,
hal_s32_t ** data_ptr_addr, int comp_id)
{
return hal_pin_new(name, HAL_S32, dir, (void **) data_ptr_addr, comp_id);
}
int hal_pin_u64_new(const char *name, hal_pin_dir_t dir,
hal_u64_t ** data_ptr_addr, int comp_id)
{
return hal_pin_new(name, HAL_U64, dir, (void **) data_ptr_addr, comp_id);
}
int hal_pin_s64_new(const char *name, hal_pin_dir_t dir,
hal_s64_t ** data_ptr_addr, int comp_id)
{
return hal_pin_new(name, HAL_S64, dir, (void **) data_ptr_addr, comp_id);
}
int hal_pin_port_new(const char *name, hal_pin_dir_t dir,
hal_port_t ** data_ptr_addr, int comp_id)
{
return hal_pin_new(name, HAL_PORT, dir, (void **)data_ptr_addr, comp_id);
}
static int hal_pin_newfv(hal_type_t type, hal_pin_dir_t dir,
void ** data_ptr_addr, int comp_id, const char *fmt, va_list ap)
{
char name[HAL_NAME_LEN + 1];
int sz;
sz = rtapi_vsnprintf(name, sizeof(name), fmt, ap);
if(sz == -1 || sz > HAL_NAME_LEN) {
rtapi_print_msg(RTAPI_MSG_ERR,
"hal_pin_newfv: length %d too long for name starting '%s'\n",
sz, name);
return -ENOMEM;
}
return hal_pin_new(name, type, dir, data_ptr_addr, comp_id);
}
int hal_pin_bit_newf(hal_pin_dir_t dir,
hal_bit_t ** data_ptr_addr, int comp_id, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = hal_pin_newfv(HAL_BIT, dir, (void**)data_ptr_addr, comp_id, fmt, ap);
va_end(ap);
return ret;
}
int hal_pin_float_newf(hal_pin_dir_t dir,
hal_float_t ** data_ptr_addr, int comp_id, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = hal_pin_newfv(HAL_FLOAT, dir, (void**)data_ptr_addr, comp_id, fmt, ap);
va_end(ap);
return ret;
}
int hal_pin_u32_newf(hal_pin_dir_t dir,
hal_u32_t ** data_ptr_addr, int comp_id, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = hal_pin_newfv(HAL_U32, dir, (void**)data_ptr_addr, comp_id, fmt, ap);
va_end(ap);
return ret;
}
int hal_pin_s32_newf(hal_pin_dir_t dir,
hal_s32_t ** data_ptr_addr, int comp_id, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = hal_pin_newfv(HAL_S32, dir, (void**)data_ptr_addr, comp_id, fmt, ap);
va_end(ap);
return ret;
}
int hal_pin_u64_newf(hal_pin_dir_t dir,
hal_u64_t ** data_ptr_addr, int comp_id, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = hal_pin_newfv(HAL_U64, dir, (void**)data_ptr_addr, comp_id, fmt, ap);
va_end(ap);
return ret;
}
int hal_pin_s64_newf(hal_pin_dir_t dir,
hal_s64_t ** data_ptr_addr, int comp_id, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = hal_pin_newfv(HAL_S64, dir, (void**)data_ptr_addr, comp_id, fmt, ap);
va_end(ap);
return ret;
}
int hal_pin_port_newf(hal_pin_dir_t dir,
hal_port_t **data_ptr_addr, int comp_id, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = hal_pin_newfv(HAL_PORT, dir, (void**)data_ptr_addr, comp_id, fmt, ap);
va_end(ap);
return ret;
}
/* this is a generic function that does the majority of the work. */
int hal_pin_new(const char *name, hal_type_t type, hal_pin_dir_t dir,
void **data_ptr_addr, int comp_id)
{
rtapi_intptr_t *prev, next;
int cmp;
hal_pin_t *new, *ptr;
hal_comp_t *comp;
if (hal_data == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin_new called before init\n");
return -EINVAL;
}
if(*data_ptr_addr)
{
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin_new(%s) called with already-initialized memory\n",
name);
}
if (type != HAL_BIT && type != HAL_FLOAT && type != HAL_S32 && type != HAL_U32 && type != HAL_S64 && type != HAL_U64 && type != HAL_PORT) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin type not one of HAL_BIT, HAL_FLOAT, HAL_S32, HAL_U32, HAL_S64, HAL_U64 or HAL_PORT\n");
return -EINVAL;
}
if(dir != HAL_IN && dir != HAL_OUT && dir != HAL_IO) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin direction not one of HAL_IN, HAL_OUT, or HAL_IO\n");
return -EINVAL;
}
if(type == HAL_PORT && dir == HAL_IO) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin direction must be one of HAL_IN or HAL_OUT for hal port\n");
return -EINVAL;
}
if (strlen(name) > HAL_NAME_LEN) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin name '%s' is too long\n", name);
return -EINVAL;
}
if (hal_data->lock & HAL_LOCK_LOAD) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin_new called while HAL locked\n");
return -EPERM;
}
rtapi_print_msg(RTAPI_MSG_DBG, "HAL: creating pin '%s'\n", name);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* validate comp_id */
comp = halpr_find_comp_by_id(comp_id);
if (comp == 0) {
/* bad comp_id */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: component %d not found\n", comp_id);
return -EINVAL;
}
// Already check duplicate before allocating
if(halpr_find_pin_by_name(name)) {
// Duplicate pin name
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR, "HAL: ERROR: duplicate pin '%s'\n", name);
return -EINVAL;
}
if(halpr_find_param_by_name(name)) {
// Overlapping pin/parameter name
// This is a problem because setp does not distinguish and
// cannot set pin or param when the names collide.
rtapi_print_msg(RTAPI_MSG_ERR, "HAL: ERROR: pin '%s' also is the name of a parameter\n", name);
// We continue, as was done before...
}
/* validate passed in pointer - must point to HAL shmem */
if (! SHMCHK(data_ptr_addr)) {
/* bad pointer */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: data_ptr_addr not in shared memory\n");
return -EINVAL;
}
if(comp->ready) {
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin_new called after hal_ready\n");
return -EINVAL;
}
/* allocate a new variable structure */
new = alloc_pin_struct();
if (new == 0) {
/* alloc failed */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: insufficient memory for pin '%s'\n", name);
return -ENOMEM;
}
/* initialize the structure */
new->data_ptr_addr = SHMOFF(data_ptr_addr);
new->owner_ptr = SHMOFF(comp);
new->type = type;
new->dir = dir;
new->signal = 0;
memset(&new->dummysig, 0, sizeof(hal_data_u));
rtapi_snprintf(new->name, sizeof(new->name), "%s", name);
/* make 'data_ptr' point to dummy signal */
*data_ptr_addr = (char *)comp->shmem_base + SHMOFF(&(new->dummysig));
/* search list for 'name' and insert new structure */
prev = &(hal_data->pin_list_ptr);
next = *prev;
while (1) {
if (next == 0) {
/* reached end of list, insert here */
new->next_ptr = next;
*prev = SHMOFF(new);
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
ptr = SHMPTR(next);
cmp = strcmp(ptr->name, new->name);
if (cmp > 0) {
/* found the right place for it, insert here */
new->next_ptr = next;
*prev = SHMOFF(new);
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
if (cmp == 0) {
/* name already in list, can't insert */
free_pin_struct(new);
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: duplicate variable '%s'\n", name);
return -EINVAL;
}
/* didn't find it yet, look at next one */
prev = &(ptr->next_ptr);
next = *prev;
}
}
int hal_pin_alias(const char *pin_name, const char *alias)
{
rtapi_intptr_t *prev, next;
int cmp;
hal_pin_t *pin, *ptr;
hal_oldname_t *oldname;
if (hal_data == 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin_alias called before init\n");
return -EINVAL;
}
if (hal_data->lock & HAL_LOCK_CONFIG) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin_alias called while HAL locked\n");
return -EPERM;
}
if (alias != NULL ) {
if (strlen(alias) > HAL_NAME_LEN) {
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: alias name '%s' is too long\n", alias);
return -EINVAL;
}
}
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
if (alias != NULL ) {
pin = halpr_find_pin_by_name(alias);
if ( pin != NULL ) {
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: duplicate pin/alias name '%s'\n", alias);
return -EINVAL;
}
}
/* once we unlink the pin from the list, we don't want to have to
abort the change and repair things. So we allocate an oldname
struct here, then free it (which puts it on the free list). This
allocation might fail, in which case we abort the command. But
if we actually need the struct later, the next alloc is guaranteed
to succeed since at least one struct is on the free list. */
oldname = halpr_alloc_oldname_struct();
if ( oldname == NULL ) {
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: insufficient memory for pin_alias\n");
return -EINVAL;
}
free_oldname_struct(oldname);
/* find the pin and unlink it from pin list */
prev = &(hal_data->pin_list_ptr);
next = *prev;
while (1) {
if (next == 0) {
/* reached end of list, not found */
rtapi_mutex_give(&(hal_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR,
"HAL: ERROR: pin '%s' not found\n", pin_name);
return -EINVAL;
}
pin = SHMPTR(next);
if ( strcmp(pin->name, pin_name) == 0 ) {
/* found it, unlink from list */
*prev = pin->next_ptr;
break;
}
if (pin->oldname != 0 ) {
oldname = SHMPTR(pin->oldname);
if (strcmp(oldname->name, pin_name) == 0) {
/* found it, unlink from list */
*prev = pin->next_ptr;
break;
}
}
/* didn't find it yet, look at next one */
prev = &(pin->next_ptr);
next = *prev;
}
if ( alias != NULL ) {
/* adding a new alias */
if ( pin->oldname == 0 ) {
/* save old name (only if not already saved) */
oldname = halpr_alloc_oldname_struct();
pin->oldname = SHMOFF(oldname);
rtapi_snprintf(oldname->name, sizeof(oldname->name), "%s", pin->name);
}
/* change pin's name to 'alias' */
rtapi_snprintf(pin->name, sizeof(pin->name), "%s", alias);
} else {
/* removing an alias */
if ( pin->oldname != 0 ) {
/* restore old name (only if pin is aliased) */
oldname = SHMPTR(pin->oldname);
rtapi_snprintf(pin->name, sizeof(pin->name), "%s", oldname->name);
pin->oldname = 0;
free_oldname_struct(oldname);
}
}
/* insert pin back into list in proper place */
prev = &(hal_data->pin_list_ptr);
next = *prev;
while (1) {
if (next == 0) {
/* reached end of list, insert here */
pin->next_ptr = next;
*prev = SHMOFF(pin);
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
ptr = SHMPTR(next);
cmp = strcmp(ptr->name, pin->name);
if (cmp > 0) {
/* found the right place for it, insert here */
pin->next_ptr = next;
*prev = SHMOFF(pin);
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
/* didn't find it yet, look at next one */
prev = &(ptr->next_ptr);
next = *prev;
}
}
/***********************************************************************
* "SIGNAL" FUNCTIONS *
************************************************************************/
int hal_signal_new(const char *name, hal_type_t type)
{
rtapi_intptr_t *prev, next;
int cmp;
hal_sig_t *new, *ptr;
void *data_addr;