LinuxCNC Documentation

SYNOPSIS

#include <hal.h>

typedef int (*hal_query_cb)(hal_query_t *query, void *arg);

int hal_list_p_s(hal_query_t *query, hal_query_cb callback, void *arg);

ARGUMENTS

query

The query structure containing the data to search for and returns all data found.

callback

The callback function to invoke for each signal found.

arg

A user defined pointer to a user defined data structure. The arg pointer is passed verbatim to the callback function.

DESCRIPTION

The hal_list_p_s() function will iterate all pins connected to a specific signal and call the callback for each. The query structure should be set to zero before the call, except the query.name field. The query.name must be set to the signal to search for.

The callback is called with all fields set appropriately, including value and data reference. The query.qtype is set to HAL_QTYPE_PIN for each pin found. You must demultiplex the query.pp.value and query.pp.ref according to the query.pp.type field in the callback function. However, the pin type is always the same as the signal type or they could not be connected. If you know the signal type, then you could use that for the pins too.

The iteration continues until no more connected pins are found or the callback returns non-zero. You can return a positive value from the callback to signal termination of iteration without triggering an error. The return value from the callback is returned from the hal_list_p_s() function as is.

RETURN VALUE

Returns zero (0) on success. The return value of the callback is returned if it was non-zero. A negative errno code is returned if any problem is detected:

-EFAULT

The shared memory was not mapped.

-EINVAL

An invalid argument was passed.

-ENOENT

The signal is not found.

-EBADF

An invalid type was detected.

EXAMPLES

static int print_sigpins_cb(hal_query_t *query, void *arg)
{
    (void)arg; // Not used in this example

    printf("%s: %s: ", query->pp.signal, query->name);
    if(HAL_OUT == query->pp.dir)
        printf("out: ");
    else if(HAL_IN == query->pp.dir)
        printf("in : ");
    else
        printf("io : ");
    switch(query->pp.type) {
    case HAL_BOOL: printf("%s\n",  query->pp.value.b ? "true" : "false"); break;
    case HAL_REAL: printf("%f\n",  query->pp.value.r); break;
    case HAL_SINT: printf("%ld\n", query->pp.value.s); break;
    case HAL_PORT:
    case HAL_UINT: printf("%lu\n", query->pp.value.u); break;
    default: printf("unsupported value type %d\n", (int)query->pp.type); break;
    }
    return 0; // Keep on going
}

// Print only pins that match a pattern
int print_sigpins(const char *signame)
{
    hal_query_t query = {};
    query.name = signame;

    int rv = hal_list_p_s(&query, print_sigpins_cb, NULL);

    if(0 != rv)
        printf("Iteration failure: %s", hal_strerror(rv));
    return rv;
}

SEE ALSO

hal_query_t(3), hal_list_p(3), hal_list_s(3), hal_get_p(3), hal_get_s(3)