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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
|
/* Copyright (C) 2007 Jeff Epler <jepler@unpythonic.net>
* Copyright (C) 2003 John Kasunich
* <jmkasunich AT users DOT sourceforge DOT net>
*
* Other contributers:
* Martin Kuhnle
* <mkuhnle AT users DOT sourceforge DOT net>
* Alex Joni
* <alex_joni AT users DOT sourceforge DOT net>
* Benn Lipkowitz
* <fenn AT users DOT sourceforge DOT net>
* Stephen Wille Padnos
* <swpadnos AT users DOT sourceforge DOT net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 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.
*/
#include "config.h"
#include "rtapi.h" /* RTAPI realtime OS API */
#include "hal.h" /* HAL public API decls */
#include "../hal_priv.h" /* private HAL decls */
#include "halcmd_commands.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <fnmatch.h>
static int unloadrt_comp(char *mod_name);
static void print_comp_info(char **patterns);
static void print_pin_info(int type, char **patterns);
static void print_pin_aliases(char **patterns);
static void print_param_aliases(char **patterns);
static void print_sig_info(int type, char **patterns);
static void print_script_sig_info(int type, char **patterns);
static void print_param_info(int type, char **patterns);
static void print_funct_info(char **patterns);
static void print_thread_info(char **patterns);
static void print_comp_names(char **patterns);
static void print_pin_names(char **patterns);
static void print_sig_names(char **patterns);
static void print_param_names(char **patterns);
static void print_funct_names(char **patterns);
static void print_thread_names(char **patterns);
static void print_lock_status();
static int count_list(int list_root);
static void print_mem_status();
static const char *data_type(int type);
static const char *data_type2(int type);
static const char *pin_data_dir(int dir);
static const char *param_data_dir(int dir);
static const char *data_arrow1(int dir);
static const char *data_arrow2(int dir);
static char *data_value(int type, void *valptr);
static char *data_value2(int type, void *valptr);
static void save_comps(FILE *dst);
static void save_aliases(FILE *dst);
static void save_signals(FILE *dst, int only_unlinked);
static void save_links(FILE *dst, int arrows);
static void save_nets(FILE *dst, int arrows);
static void save_params(FILE *dst);
static void save_threads(FILE *dst);
static void print_help_commands(void);
static int tmatch(int req_type, int type) {
return req_type == -1 || type == req_type;
}
static int match(char **patterns, char *value) {
int i;
if(!patterns || !patterns[0] || !patterns[0][0]) return 1;
for(i=0; patterns[i] && *patterns[i]; i++) {
char *pattern = patterns[i];
if(strncmp(pattern, value, strlen(pattern)) == 0) return 1;
if (fnmatch(pattern, value, 0) == 0) return 1;
}
return 0;
}
int do_lock_cmd(char *command)
{
int retval=0;
/* if command is blank or "all", want to lock everything */
if ((command == NULL) || (strcmp(command, "all") == 0)) {
retval = hal_set_lock(HAL_LOCK_ALL);
} else if (strcmp(command, "none") == 0) {
retval = hal_set_lock(HAL_LOCK_NONE);
} else if (strcmp(command, "tune") == 0) {
retval = hal_set_lock(HAL_LOCK_LOAD & HAL_LOCK_CONFIG);
} else if (strcmp(command, "all") == 0) {
retval = hal_set_lock(HAL_LOCK_ALL);
}
if (retval == 0) {
/* print success message */
halcmd_info("Locking completed");
} else {
halcmd_error("Locking failed\n");
}
return retval;
}
int do_unlock_cmd(char *command)
{
int retval=0;
/* if command is blank or "all", want to unlock everything */
if ((command == NULL) || (strcmp(command, "all") == 0)) {
retval = hal_set_lock(HAL_LOCK_NONE);
} else if (strcmp(command, "all") == 0) {
retval = hal_set_lock(HAL_LOCK_NONE);
} else if (strcmp(command, "tune") == 0) {
retval = hal_set_lock(HAL_LOCK_LOAD & HAL_LOCK_CONFIG);
}
if (retval == 0) {
/* print success message */
halcmd_info("Unlocking completed");
} else {
halcmd_error("Unlocking failed\n");
}
return retval;
}
int do_linkpp_cmd(char *first_pin_name, char *second_pin_name)
{
int retval;
hal_pin_t *first_pin, *second_pin;
static int dep_msg_printed = 0;
if ( dep_msg_printed == 0 ) {
halcmd_warning("linkpp command is deprecated, use 'net'\n");
dep_msg_printed = 1;
}
rtapi_mutex_get(&(hal_data->mutex));
/* check if the pins are there */
first_pin = halpr_find_pin_by_name(first_pin_name);
second_pin = halpr_find_pin_by_name(second_pin_name);
if (first_pin == 0) {
/* first pin not found*/
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("pin '%s' not found\n", first_pin_name);
return -EINVAL;
} else if (second_pin == 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("pin '%s' not found\n", second_pin_name);
return -EINVAL;
}
/* give the mutex, as the other functions use their own mutex */
rtapi_mutex_give(&(hal_data->mutex));
/* check that both pins have the same type,
don't want to create a sig, which after that won't be usefull */
if (first_pin->type != second_pin->type) {
halcmd_error("pins '%s' and '%s' not of the same type\n",
first_pin_name, second_pin_name);
return -EINVAL;
}
/* now create the signal */
retval = hal_signal_new(first_pin_name, first_pin->type);
if (retval == 0) {
/* if it worked, link the pins to it */
retval = hal_link(first_pin_name, first_pin_name);
if ( retval == 0 ) {
/* if that worked, link the second pin to the new signal */
retval = hal_link(second_pin_name, first_pin_name);
}
}
if (retval < 0) {
halcmd_error("linkpp failed\n");
}
return retval;
}
int do_linkps_cmd(char *pin, char *sig)
{
int retval;
retval = hal_link(pin, sig);
if (retval == 0) {
/* print success message */
halcmd_info("Pin '%s' linked to signal '%s'\n", pin, sig);
} else {
halcmd_error("link failed\n");
}
return retval;
}
int do_linksp_cmd(char *sig, char *pin) {
return do_linkps_cmd(pin, sig);
}
int do_unlinkp_cmd(char *pin)
{
int retval;
retval = hal_unlink(pin);
if (retval == 0) {
/* print success message */
halcmd_info("Pin '%s' unlinked\n", pin);
} else {
halcmd_error("unlink failed\n");
}
return retval;
}
int do_source_cmd(char *hal_filename) {
FILE *f = fopen(hal_filename, "r");
char buf[MAX_CMD_LEN+1];
int fd;
int result = 0;
int lineno_save = halcmd_get_linenumber();
int linenumber = 1;
char *filename_save = strdup(halcmd_get_filename());
if(!f) {
fprintf(stderr, "Could not open hal file '%s': %s\n",
hal_filename, strerror(errno));
free(filename_save);
return -EINVAL;
}
fd = fileno(f);
fcntl(fd, F_SETFD, FD_CLOEXEC);
halcmd_set_filename(hal_filename);
while(1) {
char *readresult = fgets(buf, MAX_CMD_LEN, f);
halcmd_set_linenumber(linenumber++);
if(readresult == 0) {
if(feof(f)) break;
halcmd_error("Error reading file: %s\n", strerror(errno));
result = -EINVAL;
break;
}
result = halcmd_parse_line(buf);
if(result != 0) break;
}
halcmd_set_linenumber(lineno_save);
halcmd_set_filename(filename_save);
free(filename_save);
fclose(f);
return result;
}
int do_start_cmd(void) {
int retval = hal_start_threads();
if (retval == 0) {
/* print success message */
halcmd_info("Realtime threads started\n");
}
return retval;
}
int do_stop_cmd(void) {
int retval = hal_stop_threads();
if (retval == 0) {
/* print success message */
halcmd_info("Realtime threads stopped\n");
}
return retval;
}
int do_echo_cmd(void) {
printf("Echo on\n");
return 0;
}
int do_unecho_cmd(void) {
printf("Echo off\n");
return 0;
}
int do_addf_cmd(char *func, char *thread, char **opt) {
char *position_str = opt ? opt[0] : NULL;
int position = -1;
int retval;
if(position_str && *position_str) position = atoi(position_str);
retval = hal_add_funct_to_thread(func, thread, position);
if(retval == 0) {
halcmd_info("Function '%s' added to thread '%s'\n",
func, thread);
} else {
halcmd_error("addf failed\n");
}
return retval;
}
int do_alias_cmd(char *pinparam, char *name, char *alias) {
int retval;
if ( strcmp (pinparam, "pin" ) == 0 ) {
retval = hal_pin_alias(name, alias);
} else if ( strcmp (pinparam, "param" ) == 0 ) {
retval = hal_param_alias(name, alias);
} else {
retval = -EINVAL;
}
if(retval == 0) {
halcmd_info("%s '%s' aliased to '%s'\n",
pinparam, name, alias);
} else {
halcmd_error("alias failed\n");
}
return retval;
}
int do_unalias_cmd(char *pinparam, char *name) {
int retval;
if (strcmp(pinparam, "pin") == 0) {
retval = hal_pin_alias(name, NULL);
} else if ( strcmp (pinparam, "param" ) == 0 ) {
retval = hal_param_alias(name, NULL);
} else {
return -EINVAL;
};
if(retval == 0) {
halcmd_info("%s '%s' unaliased\n",
pinparam, name);
} else {
halcmd_error("unalias failed\n");
}
return retval;
}
int do_delf_cmd(char *func, char *thread) {
int retval;
retval = hal_del_funct_from_thread(func, thread);
if(retval == 0) {
halcmd_info("Function '%s' removed from thread '%s'\n",
func, thread);
} else {
halcmd_error("delf failed\n");
}
return retval;
}
static int preflight_net_cmd(char *signal, hal_sig_t *sig, char *pins[]) {
int i, type=-1, writers=0, bidirs=0, pincnt=0;
char *writer_name=0, *bidir_name=0;
/* if signal already exists, use its info */
if (sig) {
type = sig->type;
writers = sig->writers;
bidirs = sig->bidirs;
}
if(writers || bidirs)
{
hal_pin_t *pin;
int next;
for(next = hal_data->pin_list_ptr; next; next=pin->next_ptr)
{
pin = SHMPTR(next);
if(SHMPTR(pin->signal) == sig && pin->dir == HAL_OUT)
writer_name = pin->name;
if(SHMPTR(pin->signal) == sig && pin->dir == HAL_IO)
bidir_name = writer_name = pin->name;
}
}
for(i=0; pins[i] && *pins[i]; i++) {
hal_pin_t *pin = 0;
pin = halpr_find_pin_by_name(pins[i]);
if(!pin) {
halcmd_error("Pin '%s' does not exist\n",
pins[i]);
return -ENOENT;
}
if(SHMPTR(pin->signal) == sig) {
/* Already on this signal */
pincnt++;
continue;
} else if(pin->signal != 0) {
hal_sig_t *osig = SHMPTR(pin->signal);
halcmd_error("Pin '%s' was already linked to signal '%s'\n",
pin->name, osig->name);
return -EINVAL;
}
if (type == -1) {
/* no pre-existing type, use this pin's type */
type = pin->type;
}
if(type != pin->type) {
halcmd_error(
"Signal '%s' of type '%s' cannot add pin '%s' of type '%s'\n",
signal, data_type2(type), pin->name, data_type2(pin->type));
return -EINVAL;
}
if(pin->dir == HAL_OUT) {
if(writers || bidirs) {
dir_error:
halcmd_error(
"Signal '%s' can not add %s pin '%s', "
"it already has %s pin '%s'\n",
signal, pin_data_dir(pin->dir), pin->name,
bidir_name ? pin_data_dir(HAL_IO):pin_data_dir(HAL_OUT),
bidir_name ? bidir_name : writer_name);
return -EINVAL;
}
writer_name = pin->name;
writers++;
}
if(pin->dir == HAL_IO) {
if(writers) {
goto dir_error;
}
bidir_name = pin->name;
bidirs++;
}
pincnt++;
}
if(pincnt)
return 0;
halcmd_error("'net' requires at least one pin, none given\n");
return -EINVAL;
}
int do_net_cmd(char *signal, char *pins[]) {
hal_sig_t *sig;
int i, retval;
rtapi_mutex_get(&(hal_data->mutex));
/* see if signal already exists */
sig = halpr_find_sig_by_name(signal);
/* verify that everything matches up (pin types, etc) */
retval = preflight_net_cmd(signal, sig, pins);
if(retval < 0) {
rtapi_mutex_give(&(hal_data->mutex));
return retval;
}
{
hal_pin_t *pin = halpr_find_pin_by_name(signal);
if(pin) {
halcmd_error(
"Signal name '%s' must not be the same as a pin. "
"Did you omit the signal name?\n",
signal);
rtapi_mutex_give(&(hal_data->mutex));
return -ENOENT;
}
}
if(!sig) {
/* Create the signal with the type of the first pin */
hal_pin_t *pin = halpr_find_pin_by_name(pins[0]);
rtapi_mutex_give(&(hal_data->mutex));
if(!pin) {
return -ENOENT;
}
retval = hal_signal_new(signal, pin->type);
} else {
/* signal already exists */
rtapi_mutex_give(&(hal_data->mutex));
}
/* add pins to signal */
for(i=0; retval == 0 && pins[i] && *pins[i]; i++) {
retval = do_linkps_cmd(pins[i], signal);
}
return retval;
}
#if 0 /* newinst deferred to version 2.2 */
int do_newinst_cmd(char *comp_name, char *inst_name) {
hal_comp_t *comp = halpr_find_comp_by_name(comp_name);
if(!comp) {
halcmd_error( "No such component: %s\n", comp_name);
return -ENOENT;
}
if(!comp->make) {
halcmd_error( "%s does not support 'newinst'\n", comp_name);
return -ENOSYS;
}
if ( *inst_name == '\0' ) {
halcmd_error( "Must supply name for new instance\n");
return -EINVAL;
}
#if defined(RTAPI_SIM)
{
char *argv[MAX_TOK];
int m = 0, result;
argv[m++] = EMC2_BIN_DIR "/rtapi_app";
argv[m++] = "newinst";
argv[m++] = comp_name;
argv[m++] = inst_name;
argv[m++] = 0;
result = hal_systemv(argv);
if(result != 0) {
halcmd_error( "newinst failed: %d\n", result);
return -EINVAL;
}
}
#else
{
FILE *f;
f = fopen("/proc/rtapi/hal/newinst", "w");
if(!f) {
halcmd_error( "cannot open proc entry: %s\n",
strerror(errno));
return -EINVAL;
}
rtapi_mutex_get(&(hal_data->mutex));
while(hal_data->pending_constructor) {
struct timespec ts = {0, 100 * 1000 * 1000}; // 100ms
rtapi_mutex_give(&(hal_data->mutex));
nanosleep(&ts, NULL);
rtapi_mutex_get(&(hal_data->mutex));
}
strncpy(hal_data->constructor_prefix, inst_name, HAL_NAME_LEN);
hal_data->constructor_prefix[HAL_NAME_LEN]=0;
hal_data->pending_constructor = comp->make;
rtapi_mutex_give(&(hal_data->mutex));
if(fputc(' ', f) == EOF) {
halcmd_error( "cannot write to proc entry: %s\n",
strerror(errno));
fclose(f);
rtapi_mutex_get(&(hal_data->mutex));
hal_data->pending_constructor = 0;
rtapi_mutex_give(&(hal_data->mutex));
return -EINVAL;
}
if(fclose(f) != 0) {
halcmd_error(
"cannot close proc entry: %s\n",
strerror(errno));
rtapi_mutex_get(&(hal_data->mutex));
hal_data->pending_constructor = 0;
rtapi_mutex_give(&(hal_data->mutex));
return -EINVAL;
}
while(hal_data->pending_constructor) {
struct timespec ts = {0, 100 * 1000 * 1000}; // 100ms
nanosleep(&ts, NULL);
}
}
#endif
rtapi_mutex_get(&hal_data->mutex);
{
hal_comp_t *inst = halpr_alloc_comp_struct();
if (inst == 0) {
/* couldn't allocate structure */
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error(
"insufficient memory for instance '%s'\n", inst_name);
return -ENOMEM;
}
inst->comp_id = comp->comp_id | 0x10000;
inst->mem_id = -1;
inst->type = 2;
inst->pid = 0;
inst->ready = 1;
inst->shmem_base = 0;
rtapi_snprintf(inst->name, sizeof(inst->name), "%s", inst_name);
/* insert new structure at head of list */
inst->next_ptr = hal_data->comp_list_ptr;
hal_data->comp_list_ptr = SHMOFF(inst);
rtapi_mutex_give(&(hal_data->mutex));
}
return 0;
}
#endif /* newinst deferred */
int do_newsig_cmd(char *name, char *type)
{
int retval;
if (strcasecmp(type, "bit") == 0) {
retval = hal_signal_new(name, HAL_BIT);
} else if (strcasecmp(type, "float") == 0) {
retval = hal_signal_new(name, HAL_FLOAT);
} else if (strcasecmp(type, "u32") == 0) {
retval = hal_signal_new(name, HAL_U32);
} else if (strcasecmp(type, "s32") == 0) {
retval = hal_signal_new(name, HAL_S32);
} else {
halcmd_error("Unknown signal type '%s'\n", type);
retval = -EINVAL;
}
if (retval < 0) {
halcmd_error("newsig failed\n");
}
return retval;
}
static int set_common(hal_type_t type, void *d_ptr, char *value) {
// This function assumes that the mutex is held
int retval = 0;
double fval;
long lval;
unsigned long ulval;
char *cp = value;
switch (type) {
case HAL_BIT:
if ((strcmp("1", value) == 0) || (strcasecmp("TRUE", value) == 0)) {
*(hal_bit_t *) (d_ptr) = 1;
} else if ((strcmp("0", value) == 0)
|| (strcasecmp("FALSE", value)) == 0) {
*(hal_bit_t *) (d_ptr) = 0;
} else {
halcmd_error("value '%s' invalid for bit\n", value);
retval = -EINVAL;
}
break;
case HAL_FLOAT:
fval = strtod ( value, &cp );
if ((*cp != '\0') && (!isspace(*cp))) {
/* invalid character(s) in string */
halcmd_error("value '%s' invalid for float\n", value);
retval = -EINVAL;
} else {
*((hal_float_t *) (d_ptr)) = fval;
}
break;
case HAL_S32:
lval = strtol(value, &cp, 0);
if ((*cp != '\0') && (!isspace(*cp))) {
/* invalid chars in string */
halcmd_error("value '%s' invalid for S32\n", value);
retval = -EINVAL;
} else {
*((hal_s32_t *) (d_ptr)) = lval;
}
break;
case HAL_U32:
ulval = strtoul(value, &cp, 0);
if ((*cp != '\0') && (!isspace(*cp))) {
/* invalid chars in string */
halcmd_error("value '%s' invalid for U32\n", value);
retval = -EINVAL;
} else {
*((hal_u32_t *) (d_ptr)) = ulval;
}
break;
default:
/* Shouldn't get here, but just in case... */
halcmd_error("bad type %d\n", type);
retval = -EINVAL;
}
return retval;
}
int do_setp_cmd(char *name, char *value)
{
int retval;
hal_param_t *param;
hal_pin_t *pin;
hal_type_t type;
void *d_ptr;
halcmd_info("setting parameter '%s' to '%s'\n", name, value);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* search param list for name */
param = halpr_find_param_by_name(name);
if (param == 0) {
pin = halpr_find_pin_by_name(name);
if(pin == 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("parameter or pin '%s' not found\n", name);
return -EINVAL;
} else {
/* found it */
type = pin->type;
if(pin->dir == HAL_OUT) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("pin '%s' is not writable\n", name);
return -EINVAL;
}
if(pin->signal != 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("pin '%s' is connected to a signal\n", name);
return -EINVAL;
}
// d_ptr = (void*)SHMPTR(pin->dummysig);
d_ptr = (void*)&pin->dummysig;
}
} else {
/* found it */
type = param->type;
/* is it read only? */
if (param->dir == HAL_RO) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("param '%s' is not writable\n", name);
return -EINVAL;
}
d_ptr = SHMPTR(param->data_ptr);
}
retval = set_common(type, d_ptr, value);
rtapi_mutex_give(&(hal_data->mutex));
if (retval == 0) {
/* print success message */
if(param) {
halcmd_info("Parameter '%s' set to %s\n", name, value);
} else {
halcmd_info("Pin '%s' set to %s\n", name, value);
}
} else {
halcmd_error("setp failed\n");
}
return retval;
}
int do_ptype_cmd(char *name)
{
hal_param_t *param;
hal_pin_t *pin;
hal_type_t type;
rtapi_print_msg(RTAPI_MSG_DBG, "getting parameter '%s'\n", name);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* search param list for name */
param = halpr_find_param_by_name(name);
if (param) {
/* found it */
type = param->type;
halcmd_output("%s\n", data_type2(type));
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
/* not found, search pin list for name */
pin = halpr_find_pin_by_name(name);
if(pin) {
/* found it */
type = pin->type;
halcmd_output("%s\n", data_type2(type));
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("parameter '%s' not found\n", name);
return -EINVAL;
}
int do_getp_cmd(char *name)
{
hal_param_t *param;
hal_pin_t *pin;
hal_sig_t *sig;
hal_type_t type;
void *d_ptr;
rtapi_print_msg(RTAPI_MSG_DBG, "getting parameter '%s'\n", name);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* search param list for name */
param = halpr_find_param_by_name(name);
if (param) {
/* found it */
type = param->type;
d_ptr = SHMPTR(param->data_ptr);
halcmd_output("%s\n", data_value2((int) type, d_ptr));
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
/* not found, search pin list for name */
pin = halpr_find_pin_by_name(name);
if(pin) {
/* found it */
type = pin->type;
if (pin->signal != 0) {
sig = SHMPTR(pin->signal);
d_ptr = SHMPTR(sig->data_ptr);
} else {
sig = 0;
d_ptr = &(pin->dummysig);
}
halcmd_output("%s\n", data_value2((int) type, d_ptr));
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("parameter '%s' not found\n", name);
return -EINVAL;
}
int do_sets_cmd(char *name, char *value)
{
int retval;
hal_sig_t *sig;
hal_type_t type;
void *d_ptr;
rtapi_print_msg(RTAPI_MSG_DBG, "setting signal '%s'\n", name);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* search signal list for name */
sig = halpr_find_sig_by_name(name);
if (sig == 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("signal '%s' not found\n", name);
return -EINVAL;
}
/* found it - does it have a writer? */
if (sig->writers > 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("signal '%s' already has writer(s)\n", name);
return -EINVAL;
}
/* no writer, so we can safely set it */
type = sig->type;
d_ptr = SHMPTR(sig->data_ptr);
retval = set_common(type, d_ptr, value);
rtapi_mutex_give(&(hal_data->mutex));
if (retval == 0) {
/* print success message */
halcmd_info("Signal '%s' set to %s\n", name, value);
} else {
halcmd_error("sets failed\n");
}
return retval;
}
int do_stype_cmd(char *name)
{
hal_sig_t *sig;
hal_type_t type;
rtapi_print_msg(RTAPI_MSG_DBG, "getting signal '%s'\n", name);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* search signal list for name */
sig = halpr_find_sig_by_name(name);
if (sig == 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("signal '%s' not found\n", name);
return -EINVAL;
}
/* found it */
type = sig->type;
halcmd_output("%s\n", data_type2(type));
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
int do_gets_cmd(char *name)
{
hal_sig_t *sig;
hal_type_t type;
void *d_ptr;
rtapi_print_msg(RTAPI_MSG_DBG, "getting signal '%s'\n", name);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* search signal list for name */
sig = halpr_find_sig_by_name(name);
if (sig == 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("signal '%s' not found\n", name);
return -EINVAL;
}
/* found it */
type = sig->type;
d_ptr = SHMPTR(sig->data_ptr);
halcmd_output("%s\n", data_value2((int) type, d_ptr));
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
static int get_type(char ***patterns) {
char *typestr = 0;
if(!(*patterns)) return -1;
if(!(*patterns)[0]) return -1;
if((*patterns)[0][0] != '-' || (*patterns)[0][1] != 't') return -1;
if((*patterns)[0][2]) {
typestr = &(*patterns)[0][2];
*patterns += 1;
} else if((*patterns)[1][0]) {
typestr = (*patterns)[1];
*patterns += 2;
}
if(!typestr) return -1;
if(strcmp(typestr, "float") == 0) return HAL_FLOAT;
if(strcmp(typestr, "bit") == 0) return HAL_BIT;
if(strcmp(typestr, "s32") == 0) return HAL_S32;
if(strcmp(typestr, "u32") == 0) return HAL_U32;
if(strcmp(typestr, "signed") == 0) return HAL_S32;
if(strcmp(typestr, "unsigned") == 0) return HAL_U32;
return -1;
}
int do_show_cmd(char *type, char **patterns)
{
if (rtapi_get_msg_level() == RTAPI_MSG_NONE) {
/* must be -Q, don't print anything */
return 0;
}
if (!type || *type == '\0') {
/* print everything */
print_comp_info(NULL);
print_pin_info(-1, NULL);
print_pin_aliases(NULL);
print_sig_info(-1, NULL);
print_param_info(-1, NULL);
print_param_aliases(NULL);
print_funct_info(NULL);
print_thread_info(NULL);
} else if (strcmp(type, "all") == 0) {
/* print everything, using the pattern */
print_comp_info(patterns);
print_pin_info(-1, patterns);
print_pin_aliases(patterns);
print_sig_info(-1, patterns);
print_param_info(-1, patterns);
print_param_aliases(patterns);
print_funct_info(patterns);
print_thread_info(patterns);
} else if (strcmp(type, "comp") == 0) {
print_comp_info(patterns);
} else if (strcmp(type, "pin") == 0) {
int type = get_type(&patterns);
print_pin_info(type, patterns);
} else if (strcmp(type, "sig") == 0) {
int type = get_type(&patterns);
print_sig_info(type, patterns);
} else if (strcmp(type, "signal") == 0) {
int type = get_type(&patterns);
print_sig_info(type, patterns);
} else if (strcmp(type, "param") == 0) {
int type = get_type(&patterns);
print_param_info(type, patterns);
} else if (strcmp(type, "parameter") == 0) {
int type = get_type(&patterns);
print_param_info(type, patterns);
} else if (strcmp(type, "funct") == 0) {
print_funct_info(patterns);
} else if (strcmp(type, "function") == 0) {
print_funct_info(patterns);
} else if (strcmp(type, "thread") == 0) {
print_thread_info(patterns);
} else if (strcmp(type, "alias") == 0) {
print_pin_aliases(patterns);
print_param_aliases(patterns);
} else {
halcmd_error("Unknown 'show' type '%s'\n", type);
return -1;
}
return 0;
}
int do_list_cmd(char *type, char **patterns)
{
if ( !type) {
halcmd_error("'list' requires type'\n");
return -1;
}
if (rtapi_get_msg_level() == RTAPI_MSG_NONE) {
/* must be -Q, don't print anything */
return 0;
}
if (strcmp(type, "comp") == 0) {
print_comp_names(patterns);
} else if (strcmp(type, "pin") == 0) {
print_pin_names(patterns);
} else if (strcmp(type, "sig") == 0) {
print_sig_names(patterns);
} else if (strcmp(type, "signal") == 0) {
print_sig_names(patterns);
} else if (strcmp(type, "param") == 0) {
print_param_names(patterns);
} else if (strcmp(type, "parameter") == 0) {
print_param_names(patterns);
} else if (strcmp(type, "funct") == 0) {
print_funct_names(patterns);
} else if (strcmp(type, "function") == 0) {
print_funct_names(patterns);
} else if (strcmp(type, "thread") == 0) {
print_thread_names(patterns);
} else {
halcmd_error("Unknown 'list' type '%s'\n", type);
return -1;
}
return 0;
}
int do_status_cmd(char *type)
{
if (rtapi_get_msg_level() == RTAPI_MSG_NONE) {
/* must be -Q, don't print anything */
return 0;
}
if ((type == NULL) || (strcmp(type, "all") == 0)) {
/* print everything */
/* add other status functions here if/when they are defined */
print_lock_status();
print_mem_status();
} else if (strcmp(type, "lock") == 0) {
print_lock_status();
} else if (strcmp(type, "mem") == 0) {
print_mem_status();
} else {
halcmd_error("Unknown 'status' type '%s'\n", type);
return -1;
}
return 0;
}
int do_loadrt_cmd(char *mod_name, char *args[])
{
char arg_string[MAX_CMD_LEN+1];
int m=0, n=0, retval;
hal_comp_t *comp;
char *argv[MAX_TOK+3];
char *cp1;
#if defined(RTAPI_SIM)
argv[m++] = "-Wn";
argv[m++] = mod_name;
argv[m++] = EMC2_BIN_DIR "/rtapi_app";
argv[m++] = "load";
argv[m++] = mod_name;
/* loop thru remaining arguments */
while ( args[n] && args[n][0] != '\0' ) {
argv[m++] = args[n++];
}
argv[m++] = NULL;
retval = do_loadusr_cmd(argv);
#else
static char *rtmod_dir = EMC2_RTLIB_DIR;
struct stat stat_buf;
char mod_path[MAX_CMD_LEN+1];
if (hal_get_lock()&HAL_LOCK_LOAD) {
halcmd_error("HAL is locked, loading of modules is not permitted\n");
return -EPERM;
}
if ( (strlen(rtmod_dir)+strlen(mod_name)+5) > MAX_CMD_LEN ) {
halcmd_error("Module path too long\n");
return -1;
}
/* make full module name '<path>/<name>.o' */
{
int r;
r = snprintf(mod_path, sizeof(mod_path), "%s/%s%s", rtmod_dir, mod_name, MODULE_EXT);
if (r < 0) {
halcmd_error("error making module path for %s/%s%s\n", rtmod_dir, mod_name, MODULE_EXT);
return -1;
} else if (r >= sizeof(mod_path)) {
// truncation!
halcmd_error("module path too long (max %lu) for %s/%s%s\n", (unsigned long)sizeof(mod_path)-1, rtmod_dir, mod_name, MODULE_EXT);
return -1;
}
}
/* is there a file with that name? */
if ( stat(mod_path, &stat_buf) != 0 ) {
/* can't find it */
halcmd_error("Can't find module '%s' in %s\n", mod_name, rtmod_dir);
return -1;
}
argv[0] = EMC2_BIN_DIR "/linuxcnc_module_helper";
argv[1] = "insert";
argv[2] = mod_path;
/* loop thru remaining arguments */
n = 0;
m = 3;
while ( args[n] && args[n][0] != '\0' ) {
argv[m++] = args[n++];
}
/* add a NULL to terminate the argv array */
argv[m] = NULL;
retval = hal_systemv(argv);
#endif
if ( retval != 0 ) {
halcmd_error("insmod failed, returned %d\n"
#if !defined(RTAPI_SIM)
"See the output of 'dmesg' for more information.\n"
#endif
, retval );
return -1;
}
/* make the args that were passed to the module into a single string */
n = 0;
arg_string[0] = '\0';
while ( args[n] && args[n][0] != '\0' ) {
strncat(arg_string, args[n++], MAX_CMD_LEN);
strncat(arg_string, " ", MAX_CMD_LEN);
}
/* allocate HAL shmem for the string */
cp1 = hal_malloc(strlen(arg_string)+1);
if ( cp1 == NULL ) {
halcmd_error("failed to allocate memory for module args\n");
return -1;
}
/* copy string to shmem */
strcpy (cp1, arg_string);
/* get mutex before accessing shared data */
rtapi_mutex_get(&(hal_data->mutex));
/* search component list for the newly loaded component */
comp = halpr_find_comp_by_name(mod_name);
if (comp == 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("module '%s' not loaded\n", mod_name);
return -EINVAL;
}
/* link args to comp struct */
comp->insmod_args = SHMOFF(cp1);
rtapi_mutex_give(&(hal_data->mutex));
/* print success message */
halcmd_info("Realtime module '%s' loaded\n", mod_name);
return 0;
}
int do_delsig_cmd(char *mod_name)
{
int next, retval, retval1, n;
hal_sig_t *sig;
char sigs[MAX_EXPECTED_SIGS][HAL_NAME_LEN+1];
/* check for "all" */
if ( strcmp(mod_name, "all" ) != 0 ) {
retval = hal_signal_delete(mod_name);
if (retval == 0) {
/* print success message */
halcmd_info("Signal '%s' deleted'\n", mod_name);
}
return retval;
} else {
/* build a list of signal(s) to delete */
n = 0;
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->sig_list_ptr;
while (next != 0) {
sig = SHMPTR(next);
/* we want to unload this signal, remember its name */
if ( n < ( MAX_EXPECTED_SIGS - 1 ) ) {
strncpy(sigs[n], sig->name, HAL_NAME_LEN );
sigs[n][HAL_NAME_LEN] = '\0';
n++;
}
next = sig->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
sigs[n][0] = '\0';
if ( sigs[0][0] == '\0' ) {
/* desired signals not found */
halcmd_error("no signals found to be deleted\n");
return -1;
}
/* we now have a list of components, unload them */
n = 0;
retval1 = 0;
while ( sigs[n][0] != '\0' ) {
retval = hal_signal_delete(sigs[n]);
/* check for fatal error */
if ( retval < -1 ) {
return retval;
}
/* check for other error */
if ( retval != 0 ) {
retval1 = retval;
}
if (retval == 0) {
/* print success message */
halcmd_info("Signal '%s' deleted'\n",
sigs[n]);
}
n++;
}
}
return retval1;
}
int do_unloadusr_cmd(char *mod_name)
{
int next, all;
hal_comp_t *comp;
pid_t ourpid = getpid();
/* check for "all" */
if ( strcmp(mod_name, "all" ) == 0 ) {
all = 1;
} else {
all = 0;
}
/* build a list of component(s) to unload */
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->comp_list_ptr;
while (next != 0) {
comp = SHMPTR(next);
if ( comp->type == 0 && comp->pid != ourpid) {
/* found a userspace component besides us */
if ( all || ( strcmp(mod_name, comp->name) == 0 )) {
/* we want to unload this component, send it SIGTERM */
kill(abs(comp->pid), SIGTERM);
}
}
next = comp->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
return 0;
}
int do_unloadrt_cmd(char *mod_name)
{
int next, retval, retval1, n, all;
hal_comp_t *comp;
char comps[64][HAL_NAME_LEN+1];
/* check for "all" */
if ( strcmp(mod_name, "all" ) == 0 ) {
all = 1;
} else {
all = 0;
}
/* build a list of component(s) to unload */
n = 0;
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->comp_list_ptr;
while (next != 0) {
comp = SHMPTR(next);
if ( comp->type == 1 ) {
/* found a realtime component */
if ( all || ( strcmp(mod_name, comp->name) == 0 )) {
/* we want to unload this component, remember its name */
if ( n < 63 ) {
strncpy(comps[n], comp->name, HAL_NAME_LEN );
comps[n][HAL_NAME_LEN] = '\0';
n++;
}
}
}
next = comp->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
/* mark end of list */
comps[n][0] = '\0';
if ( !all && ( comps[0][0] == '\0' )) {
/* desired component not found */
halcmd_error("component '%s' is not loaded\n", mod_name);
return -1;
}
/* we now have a list of components, unload them */
n = 0;
retval1 = 0;
while ( comps[n][0] != '\0' ) {
retval = unloadrt_comp(comps[n++]);
/* check for fatal error */
if ( retval < -1 ) {
return retval;
}
/* check for other error */
if ( retval != 0 ) {
retval1 = retval;
}
}
if (retval1 < 0) {
halcmd_error("unloadrt failed\n");
}
return retval1;
}
static int unloadrt_comp(char *mod_name)
{
int retval;
char *argv[4];
#if defined(RTAPI_SIM)
argv[0] = EMC2_BIN_DIR "/rtapi_app";
argv[1] = "unload";
#else
argv[0] = EMC2_BIN_DIR "/linuxcnc_module_helper";
argv[1] = "remove";
#endif
argv[2] = mod_name;
/* add a NULL to terminate the argv array */
argv[3] = NULL;
retval = hal_systemv(argv);
if ( retval != 0 ) {
halcmd_error("rmmod failed, returned %d\n", retval);
return -1;
}
/* print success message */
halcmd_info("Realtime module '%s' unloaded\n",
mod_name);
return 0;
}
int do_unload_cmd(char *mod_name) {
if(strcmp(mod_name, "all") == 0) {
int res = do_unloadusr_cmd(mod_name);
if(res) return res;
return do_unloadrt_cmd(mod_name);
} else {
hal_comp_t *comp;
int type = -1;
rtapi_mutex_get(&(hal_data->mutex));
comp = halpr_find_comp_by_name(mod_name);
if(comp) type = comp->type;
rtapi_mutex_give(&(hal_data->mutex));
if(type == -1) {
halcmd_error("component '%s' is not loaded\n",
mod_name);
return -1;
}
if(type == 1) return do_unloadrt_cmd(mod_name);
else return do_unloadusr_cmd(mod_name);
}
}
static char *guess_comp_name(char *prog_name)
{
static char name[HAL_NAME_LEN+1];
char *last_slash = strrchr(prog_name, '/');
char *st = last_slash ? last_slash + 1 : prog_name;
char *last_dot = strrchr(st, '.');
char *en = last_dot ? last_dot : prog_name + strlen(prog_name);
size_t len = en-st;
snprintf(name, sizeof(name), "%.*s", (int)len, st);
return name;
}
int do_loadusr_cmd(char *args[])
{
int wait_flag, wait_comp_flag, ignore_flag;
char *prog_name, *new_comp_name=NULL;
char *argv[MAX_TOK+1];
int n, m, retval, status;
pid_t pid;
int argc = 0;
while(args[argc] && *args[argc]) argc++;
args--; argc++;
if (hal_get_lock()&HAL_LOCK_LOAD) {
halcmd_error("HAL is locked, loading of programs is not permitted\n");
return -EPERM;
}
wait_flag = 0;
wait_comp_flag = 0;
ignore_flag = 0;
prog_name = NULL;
/* check for options (-w, -i, and/or -r) */
optind = 0;
while (1) {
int c = getopt(argc, args, "+wWin:");
if(c == -1) break;
switch(c) {
case 'w':
wait_flag = 1; break;
case 'W':
wait_comp_flag = 1; break;
case 'i':
ignore_flag = 1; break;
case 'n':
new_comp_name = optarg; break;
default:
return -EINVAL;
break;
}
}
/* get program and component name */
args += optind;
prog_name = *args++;
if (prog_name == 0) { return -EINVAL; }
if(!new_comp_name) {
new_comp_name = guess_comp_name(prog_name);
}
/* prepare to exec() the program */
argv[0] = prog_name;
/* loop thru remaining arguments */
n = 0;
m = 1;
while ( args[n] && args[n][0] != '\0' ) {
argv[m++] = args[n++];
}
/* add a NULL to terminate the argv array */
argv[m] = NULL;
/* start the child process */
pid = hal_systemv_nowait(argv);
/* make sure we reconnected to the HAL */
if (comp_id < 0) {
fprintf(stderr, "halcmd: hal_init() failed after fork: %d\n",
comp_id );
exit(-1);
}
hal_ready(comp_id);
if ( wait_comp_flag ) {
int ready = 0, count=0, exited=0;
hal_comp_t *comp = NULL;
retval = 0;
while(!ready && !exited) {
/* sleep for 10mS */
struct timespec ts = {0, 10 * 1000 * 1000};
nanosleep(&ts, NULL);
/* check for program ending */
retval = waitpid( pid, &status, WNOHANG );
if ( retval != 0 ) {
exited = 1;
}
/* check for program becoming ready */
rtapi_mutex_get(&(hal_data->mutex));
comp = halpr_find_comp_by_name(new_comp_name);
if(comp && comp->ready) {
ready = 1;
}
rtapi_mutex_give(&(hal_data->mutex));
/* pacify the user */
count++;
if(count == 200) {
fprintf(stderr, "Waiting for component '%s' to become ready.",
new_comp_name);
fflush(stderr);
} else if(count > 200 && count % 10 == 0) {
fprintf(stderr, ".");
fflush(stderr);
}
}
if (count >= 100) {
/* terminate pacifier */
fprintf(stderr, "\n");
}
/* did it work? */
if (ready) {
halcmd_info("Component '%s' ready\n", new_comp_name);
} else {
if ( retval < 0 ) {
halcmd_error("\nwaitpid(%d) failed\n", pid);
} else {
halcmd_error("%s exited without becoming ready\n", prog_name);
}
return -1;
}
}
if ( wait_flag ) {
/* wait for child process to complete */
retval = waitpid ( pid, &status, 0 );
/* check result of waitpid() */
if ( retval < 0 ) {
halcmd_error("waitpid(%d) failed\n", pid);
return -1;
}
if ( WIFEXITED(status) == 0 ) {
halcmd_error("program '%s' did not exit normally\n", prog_name );
return -1;
}
if ( ignore_flag == 0 ) {
retval = WEXITSTATUS(status);
if ( retval != 0 ) {
halcmd_error("program '%s' failed, returned %d\n", prog_name, retval );
return -1;
}
}
/* print success message */
halcmd_info("Program '%s' finished\n", prog_name);
} else {
/* print success message */
halcmd_info("Program '%s' started\n", prog_name);
}
return 0;
}
int do_waitusr_cmd(char *comp_name)
{
hal_comp_t *comp;
int exited;
if (*comp_name == '\0') {
halcmd_error("component name missing\n");
return -EINVAL;
}
rtapi_mutex_get(&(hal_data->mutex));
comp = halpr_find_comp_by_name(comp_name);
if (comp == NULL) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("component '%s' not found\n", comp_name);
return -EINVAL;
}
if (comp->type != 0) {
rtapi_mutex_give(&(hal_data->mutex));
halcmd_error("'%s' is not a userspace component\n", comp_name);
return -EINVAL;
}
rtapi_mutex_give(&(hal_data->mutex));
/* let the user know what is going on */
halcmd_info("Waiting for component '%s'\n", comp_name);
exited = 0;
while(!exited) {
/* sleep for 200mS */
struct timespec ts = {0, 200 * 1000 * 1000};
nanosleep(&ts, NULL);
/* check for component still around */
rtapi_mutex_get(&(hal_data->mutex));
comp = halpr_find_comp_by_name(comp_name);
if(comp == NULL) {
exited = 1;
}
rtapi_mutex_give(&(hal_data->mutex));
}
halcmd_info("Component '%s' finished\n", comp_name);
return 0;
}
static void print_comp_info(char **patterns)
{
int next;
hal_comp_t *comp;
if (scriptmode == 0) {
halcmd_output("Loaded HAL Components:\n");
halcmd_output("ID Type %-*s PID State\n", HAL_NAME_LEN, "Name");
}
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->comp_list_ptr;
while (next != 0) {
comp = SHMPTR(next);
if ( match(patterns, comp->name) ) {
if(comp->type == 2) {
hal_comp_t *comp1 = halpr_find_comp_by_id(comp->comp_id & 0xffff);
halcmd_output(" INST %s %s",
comp1 ? comp1->name : "(unknown)",
comp->name);
} else {
halcmd_output(" %5d %-4s %-*s",
comp->comp_id, (comp->type ? "RT" : "User"),
HAL_NAME_LEN, comp->name);
if(comp->type == 0) {
halcmd_output(" %5d %s", comp->pid, comp->ready > 0 ?
"ready" : "initializing");
} else {
halcmd_output(" %5s %s", "", comp->ready > 0 ?
"ready" : "initializing");
}
}
halcmd_output("\n");
}
next = comp->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_pin_info(int type, char **patterns)
{
int next;
hal_pin_t *pin;
hal_comp_t *comp;
hal_sig_t *sig;
void *dptr;
if (scriptmode == 0) {
halcmd_output("Component Pins:\n");
halcmd_output("Owner Type Dir Value Name\n");
}
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->pin_list_ptr;
while (next != 0) {
pin = SHMPTR(next);
if ( tmatch(type, pin->type) && match(patterns, pin->name) ) {
comp = SHMPTR(pin->owner_ptr);
if (pin->signal != 0) {
sig = SHMPTR(pin->signal);
dptr = SHMPTR(sig->data_ptr);
} else {
sig = 0;
dptr = &(pin->dummysig);
}
if (scriptmode == 0) {
halcmd_output(" %5d %5s %-3s %9s %s",
comp->comp_id,
data_type((int) pin->type),
pin_data_dir((int) pin->dir),
data_value((int) pin->type, dptr),
pin->name);
} else {
halcmd_output("%s %s %s %s %s",
comp->name,
data_type((int) pin->type),
pin_data_dir((int) pin->dir),
data_value2((int) pin->type, dptr),
pin->name);
}
if (sig == 0) {
halcmd_output("\n");
} else {
halcmd_output(" %s %s\n", data_arrow1((int) pin->dir), sig->name);
}
}
next = pin->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_pin_aliases(char **patterns)
{
int next;
hal_oldname_t *oldname;
hal_pin_t *pin;
if (scriptmode == 0) {
halcmd_output("Pin Aliases:\n");
halcmd_output(" %-*s %s\n", HAL_NAME_LEN, "Alias", "Original Name");
}
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->pin_list_ptr;
while (next != 0) {
pin = SHMPTR(next);
if ( pin->oldname != 0 ) {
/* name is an alias */
oldname = SHMPTR(pin->oldname);
if ( match(patterns, pin->name) || match(patterns, oldname->name) ) {
if (scriptmode == 0) {
halcmd_output(" %-*s %s\n", HAL_NAME_LEN, pin->name, oldname->name);
} else {
halcmd_output(" %s %s\n", pin->name, oldname->name);
}
}
}
next = pin->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_sig_info(int type, char **patterns)
{
int next;
hal_sig_t *sig;
void *dptr;
hal_pin_t *pin;
if (scriptmode != 0) {
print_script_sig_info(type, patterns);
return;
}
halcmd_output("Signals:\n");
halcmd_output("Type Value Name (linked to)\n");
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->sig_list_ptr;
while (next != 0) {
sig = SHMPTR(next);
if ( tmatch(type, sig->type) && match(patterns, sig->name) ) {
dptr = SHMPTR(sig->data_ptr);
halcmd_output("%s %s %s\n", data_type((int) sig->type),
data_value((int) sig->type, dptr), sig->name);
/* look for pin(s) linked to this signal */
pin = halpr_find_pin_by_sig(sig, 0);
while (pin != 0) {
halcmd_output(" %s %s\n",
data_arrow2((int) pin->dir), pin->name);
pin = halpr_find_pin_by_sig(sig, pin);
}
}
next = sig->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_script_sig_info(int type, char **patterns)
{
int next;
hal_sig_t *sig;
void *dptr;
hal_pin_t *pin;
if (scriptmode == 0) {
return;
}
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->sig_list_ptr;
while (next != 0) {
sig = SHMPTR(next);
if ( tmatch(type, sig->type) && match(patterns, sig->name) ) {
dptr = SHMPTR(sig->data_ptr);
halcmd_output("%s %s %s", data_type((int) sig->type),
data_value2((int) sig->type, dptr), sig->name);
/* look for pin(s) linked to this signal */
pin = halpr_find_pin_by_sig(sig, 0);
while (pin != 0) {
halcmd_output(" %s %s",
data_arrow2((int) pin->dir), pin->name);
pin = halpr_find_pin_by_sig(sig, pin);
}
halcmd_output("\n");
}
next = sig->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_param_info(int type, char **patterns)
{
int next;
hal_param_t *param;
hal_comp_t *comp;
if (scriptmode == 0) {
halcmd_output("Parameters:\n");
halcmd_output("Owner Type Dir Value Name\n");
}
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->param_list_ptr;
while (next != 0) {
param = SHMPTR(next);
if ( tmatch(type, param->type), match(patterns, param->name) ) {
comp = SHMPTR(param->owner_ptr);
if (scriptmode == 0) {
halcmd_output(" %5d %5s %-3s %9s %s\n",
comp->comp_id, data_type((int) param->type),
param_data_dir((int) param->dir),
data_value((int) param->type, SHMPTR(param->data_ptr)),
param->name);
} else {
halcmd_output("%s %s %s %s %s\n",
comp->name, data_type((int) param->type),
param_data_dir((int) param->dir),
data_value2((int) param->type, SHMPTR(param->data_ptr)),
param->name);
}
}
next = param->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_param_aliases(char **patterns)
{
int next;
hal_oldname_t *oldname;
hal_param_t *param;
if (scriptmode == 0) {
halcmd_output("Parameter Aliases:\n");
halcmd_output(" %-*s %s\n", HAL_NAME_LEN, "Alias", "Original Name");
}
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->param_list_ptr;
while (next != 0) {
param = SHMPTR(next);
if ( param->oldname != 0 ) {
/* name is an alias */
oldname = SHMPTR(param->oldname);
if ( match(patterns, param->name) || match(patterns, oldname->name) ) {
if (scriptmode == 0) {
halcmd_output(" %-*s %s\n", HAL_NAME_LEN, param->name, oldname->name);
} else {
halcmd_output(" %s %s\n", param->name, oldname->name);
}
}
}
next = param->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_funct_info(char **patterns)
{
int next;
hal_funct_t *fptr;
hal_comp_t *comp;
if (scriptmode == 0) {
halcmd_output("Exported Functions:\n");
halcmd_output("Owner CodeAddr Arg FP Users Name\n");
}
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->funct_list_ptr;
while (next != 0) {
fptr = SHMPTR(next);
if ( match(patterns, fptr->name) ) {
comp = SHMPTR(fptr->owner_ptr);
if (scriptmode == 0) {
halcmd_output(" %05d %08lx %08lx %-3s %5d %s\n",
comp->comp_id,
(long)fptr->funct,
(long)fptr->arg, (fptr->uses_fp ? "YES" : "NO"),
fptr->users, fptr->name);
} else {
halcmd_output("%s %08lx %08lx %s %3d %s\n",
comp->name,
(long)fptr->funct,
(long)fptr->arg, (fptr->uses_fp ? "YES" : "NO"),
fptr->users, fptr->name);
}
}
next = fptr->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_thread_info(char **patterns)
{
int next_thread, n;
hal_thread_t *tptr;
hal_list_t *list_root, *list_entry;
hal_funct_entry_t *fentry;
hal_funct_t *funct;
if (scriptmode == 0) {
halcmd_output("Realtime Threads:\n");
halcmd_output(" Period FP Name ( Time, Max-Time )\n");
}
rtapi_mutex_get(&(hal_data->mutex));
next_thread = hal_data->thread_list_ptr;
while (next_thread != 0) {
tptr = SHMPTR(next_thread);
if ( match(patterns, tptr->name) ) {
/* note that the scriptmode format string has no \n */
// TODO FIXME add thread runtime and max runtime to this print
halcmd_output(((scriptmode == 0) ? "%11ld %-3s %20s ( %8ld, %8ld )\n" : "%ld %s %s %ld %ld"),
tptr->period, (tptr->uses_fp ? "YES" : "NO"), tptr->name, (long)tptr->runtime, (long)tptr->maxtime);
list_root = &(tptr->funct_list);
list_entry = list_next(list_root);
n = 1;
while (list_entry != list_root) {
/* print the function info */
fentry = (hal_funct_entry_t *) list_entry;
funct = SHMPTR(fentry->funct_ptr);
/* scriptmode only uses one line per thread, which contains:
thread period, FP flag, name, then all functs separated by spaces */
if (scriptmode == 0) {
halcmd_output(" %2d %s\n", n, funct->name);
} else {
halcmd_output(" %s", funct->name);
}
n++;
list_entry = list_next(list_entry);
}
if (scriptmode != 0) {
halcmd_output("\n");
}
}
next_thread = tptr->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_comp_names(char **patterns)
{
int next;
hal_comp_t *comp;
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->comp_list_ptr;
while (next != 0) {
comp = SHMPTR(next);
if ( match(patterns, comp->name) ) {
halcmd_output("%s ", comp->name);
}
next = comp->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_pin_names(char **patterns)
{
int next;
hal_pin_t *pin;
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->pin_list_ptr;
while (next != 0) {
pin = SHMPTR(next);
if ( match(patterns, pin->name) ) {
halcmd_output("%s ", pin->name);
}
next = pin->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_sig_names(char **patterns)
{
int next;
hal_sig_t *sig;
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->sig_list_ptr;
while (next != 0) {
sig = SHMPTR(next);
if ( match(patterns, sig->name) ) {
halcmd_output("%s ", sig->name);
}
next = sig->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_param_names(char **patterns)
{
int next;
hal_param_t *param;
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->param_list_ptr;
while (next != 0) {
param = SHMPTR(next);
if ( match(patterns, param->name) ) {
halcmd_output("%s ", param->name);
}
next = param->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_funct_names(char **patterns)
{
int next;
hal_funct_t *fptr;
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->funct_list_ptr;
while (next != 0) {
fptr = SHMPTR(next);
if ( match(patterns, fptr->name) ) {
halcmd_output("%s ", fptr->name);
}
next = fptr->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_thread_names(char **patterns)
{
int next_thread;
hal_thread_t *tptr;
rtapi_mutex_get(&(hal_data->mutex));
next_thread = hal_data->thread_list_ptr;
while (next_thread != 0) {
tptr = SHMPTR(next_thread);
if ( match(patterns, tptr->name) ) {
halcmd_output("%s ", tptr->name);
}
next_thread = tptr->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
halcmd_output("\n");
}
static void print_lock_status()
{
int lock;
lock = hal_get_lock();
halcmd_output("HAL locking status:\n");
halcmd_output(" current lock value %d (%02x)\n", lock, lock);
if (lock == HAL_LOCK_NONE)
halcmd_output(" HAL_LOCK_NONE - nothing is locked\n");
if (lock & HAL_LOCK_LOAD)
halcmd_output(" HAL_LOCK_LOAD - loading of new components is locked\n");
if (lock & HAL_LOCK_CONFIG)
halcmd_output(" HAL_LOCK_CONFIG - link and addf is locked\n");
if (lock & HAL_LOCK_PARAMS)
halcmd_output(" HAL_LOCK_PARAMS - setting params is locked\n");
if (lock & HAL_LOCK_RUN)
halcmd_output(" HAL_LOCK_RUN - running/stopping HAL is locked\n");
}
static int count_list(int list_root)
{
int n, next;
rtapi_mutex_get(&(hal_data->mutex));
next = list_root;
n = 0;
while (next != 0) {
n++;
next = *((int *) SHMPTR(next));
}
rtapi_mutex_give(&(hal_data->mutex));
return n;
}
static void print_mem_status()
{
int active, recycled, next;
hal_pin_t *pin;
hal_param_t *param;
halcmd_output("HAL memory status\n");
halcmd_output(" used/total shared memory: %ld/%d\n", (long)(HAL_SIZE - hal_data->shmem_avail), HAL_SIZE);
// count components
active = count_list(hal_data->comp_list_ptr);
recycled = count_list(hal_data->comp_free_ptr);
halcmd_output(" active/recycled components: %d/%d\n", active, recycled);
// count pins
active = count_list(hal_data->pin_list_ptr);
recycled = count_list(hal_data->pin_free_ptr);
halcmd_output(" active/recycled pins: %d/%d\n", active, recycled);
// count parameters
active = count_list(hal_data->param_list_ptr);
recycled = count_list(hal_data->param_free_ptr);
halcmd_output(" active/recycled parameters: %d/%d\n", active, recycled);
// count aliases
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->pin_list_ptr;
active = 0;
while (next != 0) {
pin = SHMPTR(next);
if ( pin->oldname != 0 ) active++;
next = pin->next_ptr;
}
next = hal_data->param_list_ptr;
while (next != 0) {
param = SHMPTR(next);
if ( param->oldname != 0 ) active++;
next = param->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
recycled = count_list(hal_data->oldname_free_ptr);
halcmd_output(" active/recycled aliases: %d/%d\n", active, recycled);
// count signals
active = count_list(hal_data->sig_list_ptr);
recycled = count_list(hal_data->sig_free_ptr);
halcmd_output(" active/recycled signals: %d/%d\n", active, recycled);
// count functions
active = count_list(hal_data->funct_list_ptr);
recycled = count_list(hal_data->funct_free_ptr);
halcmd_output(" active/recycled functions: %d/%d\n", active, recycled);
// count threads
active = count_list(hal_data->thread_list_ptr);
recycled = count_list(hal_data->thread_free_ptr);
halcmd_output(" active/recycled threads: %d/%d\n", active, recycled);
}
/* Switch function for pin/sig/param type for the print_*_list functions */
static const char *data_type(int type)
{
const char *type_str;
switch (type) {
case HAL_BIT:
type_str = "bit ";
break;
case HAL_FLOAT:
type_str = "float";
break;
case HAL_S32:
type_str = "s32 ";
break;
case HAL_U32:
type_str = "u32 ";
break;
default:
/* Shouldn't get here, but just in case... */
type_str = "undef";
}
return type_str;
}
static const char *data_type2(int type)
{
const char *type_str;
switch (type) {
case HAL_BIT:
type_str = "bit";
break;
case HAL_FLOAT:
type_str = "float";
break;
case HAL_S32:
type_str = "s32";
break;
case HAL_U32:
type_str = "u32";
break;
default:
/* Shouldn't get here, but just in case... */
type_str = "undef";
}
return type_str;
}
/* Switch function for pin direction for the print_*_list functions */
static const char *pin_data_dir(int dir)
{
const char *pin_dir;
switch (dir) {
case HAL_IN:
pin_dir = "IN";
break;
case HAL_OUT:
pin_dir = "OUT";
break;
case HAL_IO:
pin_dir = "I/O";
break;
default:
/* Shouldn't get here, but just in case... */
pin_dir = "???";
}
return pin_dir;
}
/* Switch function for param direction for the print_*_list functions */
static const char *param_data_dir(int dir)
{
const char *param_dir;
switch (dir) {
case HAL_RO:
param_dir = "RO";
break;
case HAL_RW:
param_dir = "RW";
break;
default:
/* Shouldn't get here, but just in case... */
param_dir = "??";
}
return param_dir;
}
/* Switch function for arrow direction for the print_*_list functions */
static const char *data_arrow1(int dir)
{
const char *arrow;
switch (dir) {
case HAL_IN:
arrow = "<==";
break;
case HAL_OUT:
arrow = "==>";
break;
case HAL_IO:
arrow = "<=>";
break;
default:
/* Shouldn't get here, but just in case... */
arrow = "???";
}
return arrow;
}
/* Switch function for arrow direction for the print_*_list functions */
static const char *data_arrow2(int dir)
{
const char *arrow;
switch (dir) {
case HAL_IN:
arrow = "==>";
break;
case HAL_OUT:
arrow = "<==";
break;
case HAL_IO:
arrow = "<=>";
break;
default:
/* Shouldn't get here, but just in case... */
arrow = "???";
}
return arrow;
}
/* Switch function to return var value for the print_*_list functions */
/* the value is printed in a 12 character wide field */
static char *data_value(int type, void *valptr)
{
char *value_str;
static char buf[15];
switch (type) {
case HAL_BIT:
if (*((char *) valptr) == 0)
value_str = " FALSE";
else
value_str = " TRUE";
break;
case HAL_FLOAT:
snprintf(buf, 14, "%12.7g", (double)*((hal_float_t *) valptr));
value_str = buf;
break;
case HAL_S32:
snprintf(buf, 14, " %10ld", (long)*((hal_s32_t *) valptr));
value_str = buf;
break;
case HAL_U32:
snprintf(buf, 14, " 0x%08lX", (unsigned long)*((hal_u32_t *) valptr));
value_str = buf;
break;
default:
/* Shouldn't get here, but just in case... */
value_str = " undef ";
}
return value_str;
}
/* Switch function to return var value in string form */
/* the value is printed as a packed string (no whitespace */
static char *data_value2(int type, void *valptr)
{
char *value_str;
static char buf[15];
switch (type) {
case HAL_BIT:
if (*((char *) valptr) == 0)
value_str = "FALSE";
else
value_str = "TRUE";
break;
case HAL_FLOAT:
snprintf(buf, 14, "%.7g", (double)*((hal_float_t *) valptr));
value_str = buf;
break;
case HAL_S32:
snprintf(buf, 14, "%ld", (long)*((hal_s32_t *) valptr));
value_str = buf;
break;
case HAL_U32:
snprintf(buf, 14, "%ld", (unsigned long)*((hal_u32_t *) valptr));
value_str = buf;
break;
default:
/* Shouldn't get here, but just in case... */
value_str = "unknown_type";
}
return value_str;
}
int do_save_cmd(char *type, char *filename)
{
FILE *dst;
if (rtapi_get_msg_level() == RTAPI_MSG_NONE) {
/* must be -Q, don't print anything */
return 0;
}
if (filename == NULL || *filename == '\0' ) {
dst = stdout;
} else {
dst = fopen(filename, "w" );
if ( dst == NULL ) {
halcmd_error("Can't open 'save' destination '%s'\n", filename);
return -1;
}
}
if (type == 0 || *type == '\0') {
type = "all";
}
if (strcmp(type, "all" ) == 0) {
/* save everything */
save_comps(dst);
save_aliases(dst);
save_signals(dst, 1);
save_nets(dst, 3);
save_params(dst);
save_threads(dst);
} else if (strcmp(type, "comp") == 0) {
save_comps(dst);
} else if (strcmp(type, "alias") == 0) {
save_aliases(dst);
} else if (strcmp(type, "sig") == 0) {
save_signals(dst, 0);
} else if (strcmp(type, "signal") == 0) {
save_signals(dst, 0);
} else if (strcmp(type, "sigu") == 0) {
save_signals(dst, 1);
} else if (strcmp(type, "link") == 0) {
save_links(dst, 0);
} else if (strcmp(type, "linka") == 0) {
save_links(dst, 1);
} else if (strcmp(type, "net") == 0) {
save_nets(dst, 0);
} else if (strcmp(type, "neta") == 0) {
save_nets(dst, 1);
} else if (strcmp(type, "netl") == 0) {
save_nets(dst, 2);
} else if (strcmp(type, "netla") == 0 || strcmp(type, "netal") == 0) {
save_nets(dst, 3);
} else if (strcmp(type, "param") == 0) {
save_params(dst);
} else if (strcmp(type, "parameter") == 0) {
save_params(dst);
} else if (strcmp(type, "thread") == 0) {
save_threads(dst);
} else {
halcmd_error("Unknown 'save' type '%s'\n", type);
if (dst != stdout) fclose(dst);
return -1;
}
if (dst != stdout) {
fclose(dst);
}
return 0;
}
static void save_comps(FILE *dst)
{
int next;
hal_comp_t *comp;
fprintf(dst, "# components\n");
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->comp_list_ptr;
while (next != 0) {
comp = SHMPTR(next);
if ( comp->type == 1 ) {
/* only print realtime components */
if ( comp->insmod_args == 0 ) {
fprintf(dst, "#loadrt %s (not loaded by loadrt, no args saved)\n", comp->name);
} else {
fprintf(dst, "loadrt %s %s\n", comp->name,
(char *)SHMPTR(comp->insmod_args));
}
}
next = comp->next_ptr;
}
#if 0 /* newinst deferred to version 2.2 */
next = hal_data->comp_list_ptr;
while (next != 0) {
comp = SHMPTR(next);
if ( comp->type == 2 ) {
hal_comp_t *comp1 = halpr_find_comp_by_id(comp->comp_id & 0xffff);
fprintf(dst, "newinst %s %s\n", comp1->name, comp->name);
}
next = comp->next_ptr;
}
#endif
rtapi_mutex_give(&(hal_data->mutex));
}
static void save_aliases(FILE *dst)
{
int next;
hal_pin_t *pin;
hal_param_t *param;
hal_oldname_t *oldname;
fprintf(dst, "# pin aliases\n");
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->pin_list_ptr;
while (next != 0) {
pin = SHMPTR(next);
if ( pin->oldname != 0 ) {
/* name is an alias */
oldname = SHMPTR(pin->oldname);
fprintf(dst, "alias pin %s %s\n", oldname->name, pin->name);
}
next = pin->next_ptr;
}
fprintf(dst, "# param aliases\n");
next = hal_data->param_list_ptr;
while (next != 0) {
param = SHMPTR(next);
if ( param->oldname != 0 ) {
/* name is an alias */
oldname = SHMPTR(param->oldname);
fprintf(dst, "alias param %s %s\n", oldname->name, param->name);
}
next = param->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
}
static void save_signals(FILE *dst, int only_unlinked)
{
int next;
hal_sig_t *sig;
fprintf(dst, "# signals\n");
rtapi_mutex_get(&(hal_data->mutex));
for( next = hal_data->sig_list_ptr; next; next = sig->next_ptr) {
sig = SHMPTR(next);
if(only_unlinked && (sig->readers || sig->writers)) continue;
fprintf(dst, "newsig %s %s\n", sig->name, data_type((int) sig->type));
}
rtapi_mutex_give(&(hal_data->mutex));
}
static void save_links(FILE *dst, int arrow)
{
int next;
hal_pin_t *pin;
hal_sig_t *sig;
const char *arrow_str;
fprintf(dst, "# links\n");
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->pin_list_ptr;
while (next != 0) {
pin = SHMPTR(next);
if (pin->signal != 0) {
sig = SHMPTR(pin->signal);
if (arrow != 0) {
arrow_str = data_arrow1((int) pin->dir);
} else {
arrow_str = "\0";
}
fprintf(dst, "linkps %s %s %s\n", pin->name, arrow_str, sig->name);
}
next = pin->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
}
static void save_nets(FILE *dst, int arrow)
{
int next;
hal_pin_t *pin;
hal_sig_t *sig;
const char *arrow_str;
fprintf(dst, "# nets\n");
rtapi_mutex_get(&(hal_data->mutex));
for (next = hal_data->sig_list_ptr; next != 0; next = sig->next_ptr) {
sig = SHMPTR(next);
if(arrow == 3) {
int state = 0, first = 1;
/* If there are no pins connected to this signal, do nothing */
pin = halpr_find_pin_by_sig(sig, 0);
if(!pin) continue;
fprintf(dst, "net %s", sig->name);
/* Step 1: Output pin, if any */
for(pin = halpr_find_pin_by_sig(sig, 0); pin;
pin = halpr_find_pin_by_sig(sig, pin)) {
if(pin->dir != HAL_OUT) continue;
fprintf(dst, " %s", pin->name);
state = 1;
}
/* Step 2: I/O pins, if any */
for(pin = halpr_find_pin_by_sig(sig, 0); pin;
pin = halpr_find_pin_by_sig(sig, pin)) {
if(pin->dir != HAL_IO) continue;
fprintf(dst, " ");
if(state) { fprintf(dst, "=> "); state = 0; }
else if(!first) { fprintf(dst, "<=> "); }
fprintf(dst, "%s", pin->name);
first = 0;
}
if(!first) state = 1;
/* Step 3: Input pins, if any */
for(pin = halpr_find_pin_by_sig(sig, 0); pin;
pin = halpr_find_pin_by_sig(sig, pin)) {
if(pin->dir != HAL_IN) continue;
fprintf(dst, " ");
if(state) { fprintf(dst, "=> "); state = 0; }
fprintf(dst, "%s", pin->name);
}
fprintf(dst, "\n");
} else if(arrow == 2) {
/* If there are no pins connected to this signal, do nothing */
pin = halpr_find_pin_by_sig(sig, 0);
if(!pin) continue;
fprintf(dst, "net %s", sig->name);
pin = halpr_find_pin_by_sig(sig, 0);
while (pin != 0) {
fprintf(dst, " %s", pin->name);
pin = halpr_find_pin_by_sig(sig, pin);
}
fprintf(dst, "\n");
} else {
fprintf(dst, "newsig %s %s\n",
sig->name, data_type((int) sig->type));
pin = halpr_find_pin_by_sig(sig, 0);
while (pin != 0) {
if (arrow != 0) {
arrow_str = data_arrow2((int) pin->dir);
} else {
arrow_str = "\0";
}
fprintf(dst, "linksp %s %s %s\n",
sig->name, arrow_str, pin->name);
pin = halpr_find_pin_by_sig(sig, pin);
}
}
}
rtapi_mutex_give(&(hal_data->mutex));
}
static void save_params(FILE *dst)
{
int next;
hal_param_t *param;
fprintf(dst, "# parameter values\n");
rtapi_mutex_get(&(hal_data->mutex));
next = hal_data->param_list_ptr;
while (next != 0) {
param = SHMPTR(next);
if (param->dir != HAL_RO) {
/* param is writable, save its value */
fprintf(dst, "setp %s %s\n", param->name,
data_value((int) param->type, SHMPTR(param->data_ptr)));
}
next = param->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
}
static void save_threads(FILE *dst)
{
int next_thread;
hal_thread_t *tptr;
hal_list_t *list_root, *list_entry;
hal_funct_entry_t *fentry;
hal_funct_t *funct;
fprintf(dst, "# realtime thread/function links\n");
rtapi_mutex_get(&(hal_data->mutex));
next_thread = hal_data->thread_list_ptr;
while (next_thread != 0) {
tptr = SHMPTR(next_thread);
list_root = &(tptr->funct_list);
list_entry = list_next(list_root);
while (list_entry != list_root) {
/* print the function info */
fentry = (hal_funct_entry_t *) list_entry;
funct = SHMPTR(fentry->funct_ptr);
fprintf(dst, "addf %s %s\n", funct->name, tptr->name);
list_entry = list_next(list_entry);
}
next_thread = tptr->next_ptr;
}
rtapi_mutex_give(&(hal_data->mutex));
}
int do_setexact_cmd() {
int retval = 0;
rtapi_mutex_get(&(hal_data->mutex));
if(hal_data->base_period) {
halcmd_error(
"HAL_LIB: Cannot run 'setexact'"
" after a thread has been created\n");
retval = -EINVAL;
} else {
halcmd_warning(
"HAL_LIB: HAL will pretend that the exact"
" base period requested is possible.\n"
"This mode is not suitable for running real hardware.\n");
hal_data->exact_base_period = 1;
}
rtapi_mutex_give(&(hal_data->mutex));
return retval;
}
int do_help_cmd(char *command)
{
if (!command) {
print_help_commands();
} else if (strcmp(command, "help") == 0) {
printf("If you need help to use 'help', then I can't help you.\n");
} else if (strcmp(command, "loadrt") == 0) {
printf("loadrt modname [modarg(s)]\n");
printf(" Loads realtime HAL module 'modname', passing 'modargs'\n");
printf(" to the module.\n");
#if 0 /* newinst deferred to version 2.2 */
} else if (strcmp(command, "newinst") == 0) {
printf("newinst modname instname\n");
printf(" Creates another instance of previously loaded module\n" );
printf(" 'modname', nameing it 'instname'.\n");
#endif
} else if (strcmp(command, "unload") == 0) {
printf("unload compname\n");
printf(" Unloads HAL module 'compname', whether user space or realtime.\n");
printf(" If 'compname' is 'all', unloads all components.\n");
} else if (strcmp(command, "waitusr") == 0) {
printf("waitusr compname\n");
printf(" Waits for user space HAL module 'compname' to exit.\n");
} else if (strcmp(command, "unloadusr") == 0) {
printf("unloadusr compname\n");
printf(" Unloads user space HAL module 'compname'. If 'compname'\n");
printf(" is 'all', unloads all userspace components.\n");
} else if (strcmp(command, "unloadrt") == 0) {
printf("unloadrt modname\n");
printf(" Unloads realtime HAL module 'modname'. If 'modname'\n");
printf(" is 'all', unloads all realtime modules.\n");
} else if (strcmp(command, "loadusr") == 0) {
printf("loadusr [options] progname [progarg(s)]\n");
printf(" Starts user space program 'progname', passing\n");
printf(" 'progargs' to it. Options are:\n");
printf(" -W wait for HAL component to become ready\n");
printf(" -Wn name to wait for the component, which will have the given name.\n");
printf(" -w wait for program to finish\n");
printf(" -i ignore program return value (use with -w)\n");
} else if ((strcmp(command, "linksp") == 0) || (strcmp(command,"linkps") == 0)) {
printf("linkps pinname [arrow] signame\n");
printf("linksp signame [arrow] pinname\n");
printf(" Links pin 'pinname' to signal 'signame'. Both forms do\n");
printf(" the same thing. Use whichever makes sense. The optional\n");
printf(" 'arrow' can be '==>', '<==', or '<=>' and is ignored. It\n");
printf(" can be used in files to show the direction of data flow,\n");
printf(" but don't use arrows on the command line.\n");
} else if (strcmp(command, "linkpp") == 0) {
printf("linkpp firstpin secondpin\n");
printf(" Creates a signal with the name of the first pin,\n"); printf(" then links both pins to the signal. \n");
} else if(strcmp(command, "net") == 0) {
printf("net signame pinname ...\n");
printf("Creates 'signame' with the type of 'pinname' if it does not yet exist\n");
printf("And then links signame to each pinname specified.\n");
}else if (strcmp(command, "unlinkp") == 0) {
printf("unlinkp pinname\n");
printf(" Unlinks pin 'pinname' if it is linked to any signal.\n");
} else if (strcmp(command, "lock") == 0) {
printf("lock [all|tune|none]\n");
printf(" Locks HAL to some degree.\n");
printf(" none - no locking done.\n");
printf(" tune - some tuning is possible (setp & such).\n");
printf(" all - HAL completely locked.\n");
} else if (strcmp(command, "unlock") == 0) {
printf("unlock [all|tune]\n");
printf(" Unlocks HAL to some degree.\n");
printf(" tune - some tuning is possible (setp & such).\n");
printf(" all - HAL completely unlocked.\n");
} else if (strcmp(command, "newsig") == 0) {
printf("newsig signame type\n");
printf(" Creates a new signal called 'signame'. Type\n");
printf(" is 'bit', 'float', 'u32', or 's32'.\n");
} else if (strcmp(command, "delsig") == 0) {
printf("delsig signame\n");
printf(" Deletes signal 'signame'. If 'signame is 'all',\n");
printf(" deletes all signals\n");
} else if (strcmp(command, "setp") == 0) {
printf("setp paramname value\n");
printf("setp pinname value\n");
printf("paramname = value\n");
printf(" Sets parameter 'paramname' to 'value' (if writable).\n");
printf(" Sets pin 'pinname' to 'value' (if an unconnected input).\n");
printf(" 'setp' and '=' work the same, don't use '=' on the\n");
printf(" command line. 'value' may be a constant such as 1.234\n");
printf(" or TRUE, or a reference to an environment variable,\n");
#ifdef NO_INI
printf(" using the syntax '$name'./n");
#else
printf(" using the syntax '$name'. If option -i was given,\n");
printf(" 'value' may also be a reference to an ini file entry\n");
printf(" using the syntax '[section]name'.\n");
#endif
} else if (strcmp(command, "sets") == 0) {
printf("sets signame value\n");
printf(" Sets signal 'signame' to 'value' (if signal has no writers).\n");
} else if (strcmp(command, "getp") == 0) {
printf("getp paramname\n");
printf("getp pinname\n");
printf(" Gets the value of parameter 'paramname' or pin 'pinname'.\n");
} else if (strcmp(command, "ptype") == 0) {
printf("ptype paramname\n");
printf("ptype pinname\n");
printf(" Gets the type of parameter 'paramname' or pin 'pinname'.\n");
} else if (strcmp(command, "gets") == 0) {
printf("gets signame\n");
printf(" Gets the value of signal 'signame'.\n");
} else if (strcmp(command, "stype") == 0) {
printf("stype signame\n");
printf(" Gets the type of signal 'signame'\n");
} else if (strcmp(command, "addf") == 0) {
printf("addf functname threadname [position]\n");
printf(" Adds function 'functname' to thread 'threadname'. If\n");
printf(" 'position' is specified, adds the function to that spot\n");
printf(" in the thread, otherwise adds it to the end. Negative\n");
printf(" 'position' means position with respect to the end of the\n");
printf(" thread. For example '1' is start of thread, '-1' is the\n");
printf(" end of the thread, '-3' is third from the end.\n");
} else if (strcmp(command, "delf") == 0) {
printf("delf functname threadname\n");
printf(" Removes function 'functname' from thread 'threadname'.\n");
} else if (strcmp(command, "show") == 0) {
printf("show [type] [pattern]\n");
printf(" Prints info about HAL items of the specified type.\n");
printf(" 'type' is 'comp', 'pin', 'sig', 'param', 'funct',\n");
printf(" 'thread', or 'all'. If 'type' is omitted, it assumes\n");
printf(" 'all' with no pattern. If 'pattern' is specified\n");
printf(" it prints only those items whose names match the\n");
printf(" pattern, which may be a 'shell glob'.\n");
} else if (strcmp(command, "list") == 0) {
printf("list type [pattern]\n");
printf(" Prints the names of HAL items of the specified type.\n");
printf(" 'type' is 'comp', 'pin', 'sig', 'param', 'funct', or\n");
printf(" 'thread'. If 'pattern' is specified it prints only\n");
printf(" those names that match the pattern, which may be a\n");
printf(" 'shell glob'.\n");
printf(" For 'sig', 'pin' and 'param', the first pattern may be\n");
printf(" -tdatatype where datatype is the data type (e.g., 'float')\n");
printf(" in this case, the listed pins, signals, or parameters\n");
printf(" are restricted to the given data type\n");
printf(" Names are printed on a single line, space separated.\n");
} else if (strcmp(command, "status") == 0) {
printf("status [type]\n");
printf(" Prints status info about HAL.\n");
printf(" 'type' is 'lock', 'mem', or 'all'. \n");
printf(" If 'type' is omitted, it assumes\n");
printf(" 'all'.\n");
} else if (strcmp(command, "save") == 0) {
printf("save [type] [filename]\n");
printf(" Prints HAL state to 'filename' (or stdout), as a series\n");
printf(" of HAL commands. State can later be restored by using\n");
printf(" \"halcmd -f filename\".\n");
printf(" Type can be 'comp', 'sig', 'link[a]', 'net[a]', 'netl', 'param',\n");
printf(" or 'thread'. ('linka' and 'neta' show arrows for pin\n");
printf(" direction.) If 'type' is omitted or 'all', does the\n");
printf(" equivalent of 'comp', 'netl', 'param', and 'thread'.\n");
} else if (strcmp(command, "start") == 0) {
printf("start\n");
printf(" Starts all realtime threads.\n");
} else if (strcmp(command, "stop") == 0) {
printf("stop\n");
printf(" Stops all realtime threads.\n");
} else if (strcmp(command, "quit") == 0) {
printf("quit\n");
printf(" Stop processing input and terminate halcmd (when\n");
printf(" reading from a file or stdin).\n");
} else if (strcmp(command, "exit") == 0) {
printf("exit\n");
printf(" Stop processing input and terminate halcmd (when\n");
printf(" reading from a file or stdin).\n");
} else if (strcmp(command, "alias") == 0) {
printf("alias type name alias\n");
printf(" Assigns \"alias\" as a second name for the pin or parameter\n");
printf(" \"name\". For most operations, an alias provides a second\n");
printf(" name that can be used to refer to a pin or parameter, both the\n");
printf(" original name and the alias will work.\n");
printf(" \"type\" must be pin or param\n");
printf(" \"name\" must be an existing name or alias of the specified type.\n");
} else if (strcmp(command, "unalias") == 0) {
printf("unalias type name\n");
printf(" Removes any alias from the pin or parameter \"name\".\n");
printf(" \"type\" must be pin or param\n");
printf(" \"name\" must be an existing name or alias of the specified type.\n");
} else if (strcmp(command, "echo") == 0) {
printf("echo\n");
printf("echo the commands from stdin to stderr\n");
printf("Useful for debugging scripted commands from a running program\n");
} else if (strcmp(command, "unecho") == 0) {
printf("unecho\n");
printf("Turn off echo of commands from stdin to stdout\n");
} else {
printf("No help for unknown command '%s'\n", command);
}
return 0;
}
static void print_help_commands(void)
{
printf("Use 'help <command>' for more details about each command\n");
printf("Available commands:\n");
printf(" loadrt Load realtime module(s)\n");
printf(" loadusr Start user space program\n");
printf(" waitusr Waits for userspace component to exit\n");
printf(" unload Unload realtime module or terminate userspace component\n");
printf(" lock, unlock Lock/unlock HAL behaviour\n");
printf(" linkps Link pin to signal\n");
printf(" linksp Link signal to pin\n");
printf(" net Link a number of pins to a signal\n");
printf(" unlinkp Unlink pin\n");
printf(" newsig, delsig Create/delete a signal\n");
printf(" getp, gets Get the value of a pin, parameter or signal\n");
printf(" ptype, stype Get the type of a pin, parameter or signal\n");
printf(" setp, sets Set the value of a pin, parameter or signal\n");
printf(" addf, delf Add/remove function to/from a thread\n");
printf(" show Display info about HAL objects\n");
printf(" list Display names of HAL objects\n");
printf(" source Execute commands from another .hal file\n");
printf(" status Display status information\n");
printf(" save Print config as commands\n");
printf(" start, stop Start/stop realtime threads\n");
printf(" alias, unalias Add or remove pin or parameter name aliases\n");
printf(" echo, unecho Echo commands from stdin to stderr\n");
printf(" quit, exit Exit from halcmd\n");
}
|