LIRC libraries
LinuxInfraredRemoteControl
Driver API manual excerpt

Driver API manual excerpt. More...

Driver API manual excerpt.


Documentation of the LIRC driver API version 2.


This document describes the API used by the LIRC userspace drivers in version 2.

It is targeted at active C programmers, and not written with the intention to be understandable by non-programmers.

The guide aims at being fully compatible with the upcoming release LIRC 0.9.2. (CLARIFY)



Introduction

There are three kinds of drivers in LIRC:

  1. Kernel drivers, also called kernel modules.
  2. User space, statically linked drivers.
  3. User space, dynamically linked drivers.

Kernel drivers will not be covered in the present article. User space drivers are nothing else but normal compiled C functions, running as the invoking user (which may or may not be root). Therefore, the decomposition between "program" and driver is a priori not always necessary, but instead serves modularization and structuring.

In the beginning, due to its focus on very simple hardware, LIRC was very centered around kernal modules like directly connected LEDs or IR sensors. Modern hardware, due to embedded micro processors etc., have less requirements on precise timing, and user space drivers are normally the preferred solution. One such driver consists of a C file, containing a number of function, which are linked into the executing program (e. g., Lircd). Traditionally (at least for LIRC) linking takes place during the build process (statically linking). The API for these drivers we will call "Driver API version 1".

Dynamically loaded drivers was introduced LIRC just recently, replacing the statically linked drivers. It turned out to be necessary to slighty augment the driver API version 1. The new version will be referred to as "Driver API version 2".

In this guide, the word plugin refers to the so-file on disk. Each such file contains one or more driver(s) visible in the user interface.

Two flavors of drivers

An IR signal consists of a sequence of on-times (pulses) and off-times (gaps). (We disregard modulation for the time being.) A full-fledged IR driver, on reading, can deliver the timing of these period ("durations"). On sending, it can be fed with a list of durations, and it sends these on- and off-periods as (modulated) IR signals. Such a piece of hardware is suitable for receiving and/or transmitting "arbitrary" IR sequences.

Much IR hardware was not designed as general-purpose IR hardware, but to allow e.g. a laptop computer or a TV card to be be controlled by the supplied remote. This hardware in general decodes the IR signal in its own hardware, and delivers, for recognized signals, an integer code denoting the code received (like "play"). For signals not following the protocol, no output is generated. This property is indicated in the drivers as the feature LIRC_CAN_REC_LIRCCODE.

As usable and flexible IR hardware for LIRC, these devices are second choice. The "learns" (configuration files) from such a device is of very limited value, and not portable to other IR devices, since timing information is missing. In the configuration file, this can be determined by the lines one 0 0 and one 0 0, which is clearly not usable without the context, namely the hardware used for capturing.

This guide is intended for programmers writing and maintain "real" drivers.

At the time of writing, there are about 44 plugins and 54 drivers. Of the drivers, 13 are "general receivers"; the others are LIRCCODE drivers. (The actual numbers vary depending on configuration i. e. libraries installed on the build system.) Since hardware related to LIRCCODE drivers (typically a TV-card) normally doesn't support sending there is no LIRCCODE driver which can send data.

The tool lirc-lsplugins can be used to generate overviews of the drivers and their capabilities.

The C interface

A (user space) driver is a C file where all the functions are declared static, i.e. not (directly) visible from outside of the file. The only thing visible from outside is a particular data structure, containing some data elements, and some pointers to functions, that in these way effectively are made public API. In this way a certain encapsulation is achieved. The data structure will be described next.

The struct driver

The struct driver is the data structure representing the driver for LIRC. It is defined in driver.h (in directory lib).

struct driver {
        char *device;
        int fd;
        __u32 features;
        __u32 send_mode;
        __u32 rec_mode;
        __u32 code_length;
        int (*init_func) (void);
        int (*deinit_func) (void);
        int (*send_func) (struct ir_remote* remote, struct ir_ncode* code);
        char* (*rec_func) (struct ir_remote* remotes);
        int (*decode_func) (struct ir_remote* remote, struct decode_ctx_t* ctx);
        int (*drvctl_func) (unsigned int cmd, void* arg);
         lirc_t(*readdata) (lirc_t timeout);
        char *name;
        unsigned int resolution;
        /* The following fields are API version 2 extensions */
        const int api_version;
        const char* driver_version;
        const char* info;
        int (*const close_func)(void);
        int (*const open_func)(const char* device);
};
    

These fields will next be described. Note that a driver sometimes "misuses" a field; e.g. the UDP driver expects a port number (as string) in the device field.

Also note that some function pointer may be NULL, indicating that the driver does not implement the said functionality (e.g. sending of IR signals).

The structures ir_remote, ir_ncode and, ir_code are declared in the file ir_remote_types.h. The data type lirc_t (what a meaningful name!) is an integer value used in many places for storing timing data. It is defined by #define int in lirc.h.

device

Typically the name of the device as text string in Linux. This corresponds to the –device argument to lircd Not all drivers respect the device field as input; some have hardcoded device name(s), some use autodetecting code.

fd

A file descriptor associated with the driver.

features
Code for the features of the present device, see Appendix. It consist of the bitwise or of a number of possible features.
send_mode
Use LIRC_MODE_PULSE.
rec_mode
Use LIRC_MODE_MODE2.
code_length
Only of relevance for LIRC_MODE_LIRCCODE drivers, for which it is the size in bits of the LIRCCODE.
init_func
Function pointer, see below.
deinit_func
Function pointer, see below.
send_func
Function pointer, see below. NULL if the driver cannot transmit.
rec_func
Function pointer, see below. NULL if the driver cannot receive.
decode_func
Function pointer, for "real" drivers (not using LIRC_MODE_LIRCCODE), just use receive_decode, a function defined in receive.c.
drvctl_func
Function pointer, see below. May be NULL if there are no ioctls.
readdata
Function pointer, see below. Is NULL if the driver reads uses LIRC_MODE_LIRCCODE.
name
Name of the driver in a human readable form, as listed e.g. by lircd –driver help or irrecord –driver help. Although not enforced, it is recommended to use names following the syntax for C identifiers.
resolution
The resolution in microseconds of the recorded durations when reading signals. Used as default value for the aeps parameter in remotes.
api_version
Use 2.
driver_version
Free form string used to identify the version of the driver, if desired.
info
Free form string, overall driver information.
open_func
Function pointer, see below.
close_func
Function pointer, see below.

Driver lifecycle and lircd

The driver is loaded, used and unloaded by the executing program according to the following:

  1. The program Lircd code determines what driver to use (normally the –driver runtime option) and invokes hw_choose_driver() (in drv_admin.c) which loads the driver and exposes the driver interface in the global variable drv (which is a struct driver).
  2. Lircd invokes the open_func() which establishes the device to use, usually based on the runtime option –device.
  3. When a client connects, either for sending or receiving, the function init_func() is invoked, initializing the driver. In some drivers, the capabilities of the drivers, i.e. the field features, are available only after initializing. The driver also exports the fd field, a file descriptor (a positive int) for the underlying file.
  4. From this point, calls to read_func() and send_func() are possible.
  5. When the last client is terminated, executing program calls deinit_func(). Note that the Lircd program may need the device again e.g., if another sending request is received, so a hard closing of a device may not be optimal here.
  6. As a last operation, executing program should call the close_func(). This relinquishes all resources allocated by the driver.
  7. Calls to drvctl_func() are possible at any time. They are the responsibility of the programmer. In particular, ioctl operations may fail under some circumstances.
  8. The associated functions are not guaranteed to be executed in the order described. In general, the driver should not break if e.g., the init_func() is called on an already open device, or deinit_func() called on a device not open.

Driver structure

Typical structure of a driver file will be shown next.

/* Include system includes files as needed */
# 211 "/usr/local/var/lirc-rebuild/lirc/lib/driver_api.dox" 2 /* The overall include, mandatory, often sufficient.  */
# 212 "/usr/local/var/lirc-rebuild/lirc/lib/driver_api.dox" 2      /* For drivers using serial hardware. */
/* Global (but "static") variable definition. */
/* static function definitions. */
static int generic_init() {
    /* Open device/hardware */
    send_buffer_init();
    return 1;
}
static int generic_deinit(void) {
    /* close device */
    return 1;
}
static int generic_send(struct ir_remote* remote, struct ir_ncode* code) {
    result = send_buffer_put(remote, code);
    if (!result)
        return 0;
    /* Payload signal is now available in global variable send_buf. */
    /* Process sendbuf (see transmit.h). */
    return success;
}
static char *rec_func(struct ir_remote* remotes) {
    if (!rec_buffer_clear) {
        /* handle errors */
        return NULL;
    }
    return decode_all(remotes);
}
static char *decode_func(struct ir_remote* remote, struct decode_ctx_t* ctx) {
    return receive_decode(remote, ctx);
}
static int generic_drvctl(unsigned int cmd, void *arg) {
    /* Do something device specific, e.g.
       return ioctl(drv.fd, cmd, arg); */
    return cmd == 42 ? 1 : 0;
}
static lirc_t *readdata(lirc_t timeout) {
    /* compute and return the next duration in microseconds */
}
struct driver my_driver = {
   /* see above. */
}
struct driver* hardwares[] = { &my_hardware, NULL };
    

Syntax and semantics of the functions

Note that, even if implementing the functions in C++, there is no need to declare them as external "C".

init_func

int myinit_func(void)

Function called for initializing the driver and the hardware. It should return nonzero in the case of success. This function simple opens the device. In addition, the functions rec_buffer_init(void) and/or send_buffer_init(void) needs to be called.

deinit_func

int mydeinit_func(void)

Function called for suspending the driver. Zero return value indicates failure, all other return values success.

send_func

int mysend_func(struct ir_remote *remote, struct ir_ncode *code)

Function called for sending an IR code, residing in the second argument. For this, the function send_buffer_put(struct ir_remote *remote, struct ir_ncode *code) is called, with the same arguments. This makes the global variable send_bug containing the durations in micro seconds for the IR signal. The field data is a pointer to wptr number of numbers, having the data type lirc_t. Returns non-zero if operation successful.

Called from ir_remote.c.

rec_func

char* myrec_func(struct ir_remote* remotes)

The canonical implementation for non-LIRCCODE drivers is

        if (!rec_buffer_clear) {
            /* handle errors */
            return NULL;
        }
        return decode_all(remotes);
  

This is a non-blocking function which returns NULL if there is no data available currently, but might very well succeed later. The device might get closed as part of error handling, so calling code should be prepared to reopen device if required.

decode_func

char mydecode_func(struct ir_remote * remote, struct decode_ctx_t ctx)

For non-LIRC_MODE_LIRCCODE, just call receive_decode, or write receive_decode directly in the struct driver.

drvctl_func

mydrvctl_func(unsigned int cmd, void *arg)

Depending on the particular driver and hardware, additional functionality can be implemented here, with semantics as determined by the driver. There are some cmd definitions in driver.h which could be used by any driver. Driver-specific cmd constants should be > DRVCTL_MAX.

drvctl() returns 0 if OK, else a positive error code some of which defined in driver.h

driver.h defines the DRVCTL_SET_OPTION cmd. This is a generic way to set a named key to value which should cover many (most?) needs for the drvctl() function.

readdata

lirc_t myreaddata(lirc_t timeout)

This function returns one integer of read information from the device. For this, it may wait for a time determined by the argument, in micro seconds. A wait time of 0 (or < 0) blocks indefinitely. The return value has the semantic of a duration (pulse or gap) in microseconds. For this reason, 0 is an "impossible" value, and would indicate an error. In LIRC, only the lower 24 bits are used for the length of the duration (making the largest duration that LIRC can represent 2^24 - 1 = 16777215 microseconds). See the example code.

The function is called from the daemon Lircd as well as from irrecord, and mode2.

close_func

int close_func(void)

Hard close of the device. zero return value indicates success, other values indicates an error. Some standard error codes are defined in driver.h.

open_func

int open_func(void)

Open the device. This is the basic, possibly expensive steps taken to make the device usable. Returns 0 on success, else an error code, some of which defined in driver.h.

When running using the effective-user option, this function is called running as root - other functions are called running as the effective-user optionn.

Comments

Generating console output

It is possible to generate console output in any way; writing on stdout or stderr, using e.g. stdio or C++ streams. However, to conform with the working of the rest of LIRC, it is recommended to use only the various void log_(const char *format_str, ...) and void log_perror_ (const char *format, ...) which are declared in the header lirc_log.h. The normal set of log_error(), log_warn(), log_notice() ... and log_perror_err(), log_perror_warn(), log_perror_debug() etc. are available

Timing issues

Sending

The daemon Lircd takes care of the timing between the IR sequences, calling the send_func as it sees fit. Also, the final gap is the responsibility of Lircd. For example, if a signal is to be sent repeatedly every x milliseconds, Lircd will take care of the timing, at least as long as send_func does not consume too much time.

Receiving

Except for the timeout argument to readdata, there does not appear to be any timing issues the driver author needs to address.

Compiling

Compiling in-tree

To compile in-tree and assuming autogen.sh and ./configure has been run just copy the plugin source code to the plugins directory and run

    $ cd plugins;
    $ ./make-pluginlist.sh > pluginlist.am
    $ make

Compiling out-of-tree

Plugins can also easily be built out-of tree. Only some include files from LIRC are needed. Just a trivial compilation is needed. A simple generic Makefile is provided in the Appendix, another example is available in the sources as Makefile. The autotools are not needed, in particular not libtool.

The anatomy of a complete driver package.

A complete driver package consists of three files: the driver code, the configuration support and the documentation.

lirc
 |
 |-----doc
 |      |
 |      |
 |     plugindocs ------
 |                     |
 |                  foo.html
 |-----configs
 |        |
 |     foo.conf
 |
 |----plugins
          |
       foo.so

The driver code has been described all over in this document. There is also plenty of examples in the plugins/ directory. The compiled .so file could just be dropped into a directory in the -U/–plugins search path which basically could be anywhere.

The configuration support is a single file in the configs/ directory. The file configs/README describes the format. This is used by tools like lirc-setup, but also to create the list of all drivers. New files are automatically picked up.

The documentation is a single html file in the doc/plugindocs directory. The plugindocs directory contains a makefile which could be used to update the main html documents when the plugindocs/ contents have changed. See example in drivers/default/Makefile

The lirc-driver pkgconfig file defines three variables useful when installing files: plugindir, configdir and plugindocs. These are the configured locations for each file. E. g., to get the plugindir location

        $ pkg-config --variable=plugindir lirc-driver
        /usr/lib64/lirc/plugins

Using C++

Plugins can also be written in C++. The used include files are required to be "C++-safe" i. e., the functions to be called from C++ have to be declared extern "C", to make sure that the generated code follows C's calling conventions. Since the plugin code is called only indirectly through the hardware struct, no extern "C" declarations are required in the plugin code.

Adding additional libraries

Extra libraries, as well as include files, can be simply added to the Makefile (or Makefile.am for the case of in-tree builds). Note that for libraries located outside of the "standard" directories, it may be required to use an -L and an -rpath argument to the linker arguments. See the Makefile in Appendix 2.

Appendix. List of features (lirc.h).

There are four "modes of operation" of LIRC: LIRC_MODE_RAW, LIRC_MODE_PULSE, LIRC_MODE_MODE2, LIRC_MODE_LIRCCODE. In the current code, no semantic difference between the first three can be inferred. For the meaning of the last, see above ("Two flavors of drivers").

There are a number of "features" that a driver can have or not have. These are documented in the lirc(4)manpage which has been upstreamed from the LIRC project. The last LIRC version is available in the sources.

Appendix 2. Generic Makefile

LIRC_SRC=/home/bengt/lirc/master
PLUGINDIR := /home/bengt/lirc/root/lib/lirc/plugins
MACHINE := -m64
INCLUDE := -I/lib -I 
OPTIMIZE := -O2
DEBUG := -g
SHARED :=-shared -fPIC
WARNINGS=-Wall
CC := gcc
CPP := g++
%.so: %.c
                -o $@ $< 
%.so: %.cpp
               -o $@ $< 
default:
         "There is no default target in this makefile."
         "Type \"make plugin.so" to compile the plugin named plugin,"
        @echo "and "make install" to install it"
install:
        cp *.so 
clean:
        rm -f *.so