HomePort
|
In this tutorial, we will briefly cover the required elements of the main program, an adapter, and an application. See the other examples for more in depth guidance on how to implement this required functions.
The full files can be found in the example/template folder, and can be compiled with make template
.
The includes part of the main program (template_main.c) will need to include hpd_daemon_api.h for all hpd daemon related functions, and include the header files of all the modules (adapters and applications) that it starts:
The main program should allocate memory for hpd, add each module (with a unique id), start hpd, and finally clean up to avoid memory leaks. You will most likely only have to change the add modules part of this template to provide a main program for your project.
A module consists of two files; a header file and a source file. The header file of a module should only need to contain a single struct, which is the module definition of said module:
The source file should include hpd_adapter_api.h, which contains all the required functions to create an adapter.
To instantiate this struct, the source file needs to implement five functions:
This can be expected to be called in the sequence:
The the struct defining our module become:
on_create() generally have two tasks. First, it should allocate the memory we need for our module, and store this within the given data pointer. Secondly, it should add the supported command-line arguments to hpd. See the other examples for more in-depth guidance on the contents of these functions.
on_destroy() should clean up the memory allocated by on_create() (and in other places of our module), to avoid any memory leaks.
on_start() is where you would most likely open a connection to an underlying network, and add the required objects (adapters, devices, services, and parameters) to hpd. Note as many of these probably depends on asynchronous responses from the network, it is perfectly fine to add/remove objects during the entire runtime (that is between calls to on_start() and on_stop()).
on_stop() should stop everything that was started during runtime.
Finally, on_parse_opt() will be called for each command-line argument given to your module. Note, this should return HPD_E_ARGUMENT if it does not understand the given argument:
The application template is almost identical with just one exception; the source file includes hpd_application_api.h instead of hpd_adapter_api.h. First the header file (template_application.h):
Then the source file (template_application.c):