Skip to content

Commit e5e5920

Browse files
committed
hal: C++ API (hal.hh) and pybind11 bindings on the 64-bit HAL API
Reintroduce a C++ interface for HAL, replacing the old hal.hh that was removed ahead of the API break. It is built strictly on the public C API and the query API: no hal_priv.h, no direct shared memory access, no re-implemented library internals. - hal.hh: type-safe, header-only C++ layer in linuxcnc::hal, exported to include/. Typed pin/param handles via traits<T> over rtapi_bool, rtapi_sint, rtapi_uint and rtapi_real (compile-time accessor selection, no 32-bit handles), a runtime-typed pin_t variant and anypin for name-based access, a component class with add_pin for the struct-member idiom, and ULAPI by-name query/set functions on hal_get_p/hal_set_p/hal_get_s/hal_set_s/hal_comp_by_name. Handles re-read the shmem slot on every access so hal_link() slot rewrites stay visible (C pointer-variable semantics). Handles are move-only; HAL objects are unique. Query callback paths are exception-free; range errors are reported after the library releases the HAL mutex. - Streams: linuxcnc::hal::stream, a move-only wrapper around hal_stream_t, created with a depth and a typestring or attached to an existing key. Element types drive the conversion of samples in both directions with the same range checks as the by-name setters. Library failures reported as a negative errno are thrown as std::system_error. - halpybind.cc: pybind11 module (halpp.so) exposing component, Pin, stream and the by-name functions. Built alongside _hal/hal.py, replacing nothing. Type and direction tags are the _hal.Type/_hal.Dir IntEnum classes: arguments accept the members or plain ints, results come back as members. Importing _hal also initializes the HAL library. String set values go through setps_common_cb for halcmd-consistent parsing, std::system_error becomes OSError, and SIGTERM raises KeyboardInterrupt as it does in _hal. - Ports: linuxcnc::hal::port, a move-only handle for HAL_PORT pins with all-or-nothing read/peek/peek_commit/write plus readable/writable/size/clear, created with component::newport() or runtime-typed newpin(). The buffer belongs to the linking signal and is sized with set_signal(); an unlinked port has no buffer and its reads and writes fail quietly. A port pin has no scalar value: get_value() and the runtime-typed get/set raise for it, leaving hal_get_p()/hal_set_p() semantics on port pins to the library. The size is read from the signal (get_value() on a port signal) or with port::size(). halpp exposes the same calls on Pin, with bytes in and out (str is written as UTF-8). - tests/halpp: Python and native C++ smoke suites, plus a stream create/attach pair across two processes the way sampler and streamer are used. The C++ suite compiles against the tree, so the test is skipped for installed packages. Based on the pybind11 branch by rene-dev, rebuilt on the new HAL API.
1 parent 086258b commit e5e5920

13 files changed

Lines changed: 1776 additions & 0 deletions

File tree

‎src/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ build-software: headers $(INFILES)
399399
#
400400
SRCHEADERS := \
401401
hal/hal.h \
402+
hal/hal.hh \
402403
hal/drivers/mesa-hostmot2/hostmot2-serial.h \
403404
emc/linuxcnc.h \
404405
emc/kinematics/kinematics.h \

‎src/hal/Submakefile‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,16 @@ $(HALMODULE): $(call TOOBJS, $(HALMODULESRCS)) $(HALLIB)
2424
$(ECHO) Linking python module $(notdir $@)
2525
$(Q)$(CXX) $(LDFLAGS) -shared -o $@ $^ -lfmt
2626

27+
# pybind11 C++ bindings on hal.hh
28+
HALPPSRCS := hal/halpybind.cc hal/setps_util.c
29+
PYSRCS += $(HALPPSRCS)
30+
31+
HALPP := ../lib/python/halpp.so
32+
$(HALPP): $(call TOOBJS, $(HALPPSRCS)) $(HALLIB)
33+
$(ECHO) Linking python module $(notdir $@)
34+
$(Q)$(CXX) $(LDFLAGS) -shared -o $@ $^
35+
36+
PYTARGETS += $(HALPP)
37+
2738
TARGETS += $(HALLIB) ../lib/liblinuxcnchal.so.0
2839
PYTARGETS += $(HALMODULE)

0 commit comments

Comments
 (0)