example.cpp
The example.cpp file is the primary source file for the custom driver example. It contains a simple, startup function called from main.cpp that provides the driver an opportunity to set up an interface device or port if required, as well as initialize anything necessary before the driver starts up. It also contains the sample thread code related to any background processing that a typical custom driver might need to do. Finally, it contains the code for the callback functions originally registered with IDL and any driver-specific code associated with these callback events.
As seen in the following comments, example.cpp includes a stub function called IdiStart() that can be used to open a port or interface device and initialize any protocol-specific structures required to run the custom driver.
int IdiStart() { /* Your custom IDL driver can start up any other driver-specific */ /* actions here. For example, you could open a connection to a */ /* serial port or a USB interface. You should return a 0 here */ /* if your code started up your driver properly, or else return 1. */ printf("The Example IDL Driver is connected and ready...\n"); return 0; }
This source file also includes a simple example of a custom driver function launched on a separate thread called ExampleRxFunction(). Since the IdlInit() function in main.cpp that initiates the driver instance never returns, any custom driver that needs to execute any background processing tasks independent of the callback functions will need to create separate thread(s) for this code. For example, a driver might need a background task that handles incoming, asynchronous packets from devices that are not tied to actions associated with the callback functions. While the ExampleRxFunction() in this sample driver code does not do anything other than continuously suspend via usleep() and restart execution of the thread, its purpose is to illustrate how a custom driver could create separate execution threads if required.
void *ExampleRxFunction(void* argA) { printf("The Example IDL Driver thread started...\r\n"); while(1) { /* Custom code to receive & process incoming packets */ usleep(50000); // 50 ms } return NULL; }
The remainder of example.cpp contains the callback functions previously registered with the IDL library in main.cpp. These callback functions provide entry points for the custom driver to insert code to execute based on various events such as creating a new device using the protocol, provisioning a device, requests to read and write data points and so on. A complete list of available callback functions can be found in the IAP Driver Library (IDL). This example driver contains simple implementations of many of the common callback functions. These example callback functions output basic information to the console when called to assist in understanding the interaction between IDL and a custom driver and the basic parameters passed with these functions.