Kanzi Connect service template

Use the Kanzi Connect service template as a basis for creating custom service plugins for embedded targets.

The template includes a Windows solution which you can use to compile the included test service, but this topic does not cover the compilation on Windows.

Requirements

  • To generate code on Windows you must have the Kanzi Connect for Windows installed.

  • To compile the service on Linux, you must have the Kanzi Connect for Linux extracted on a Linux host and the environment variables set. You can set these variables with the helper script <KanziConnectInstallation>/SDK/setup_env.sh.

The structure of the Kanzi Connect service template

The Kanzi Connect service template includes the example service you can create by completing the Create a custom service tutorial. See Tutorial: Create a custom Kanzi Connect service.

The only difference is the directory structure, which allows for more convenient cross-compilation using SCons:

  • configs directory contains the SCons build files and the Visual Studio project.

  • interfaces directory contain the XML service descriptions and the generate_interfaces.py that you can use to generate C++ code from an XML description.

  • sources directory contains the generated and modified source code.

You can find the Kanzi Connect service template in the <KanziConnectInstallation>/templates/servicetemplate directory.

Using the Kanzi Connect service template

To use the Kanzi Connect service template:

  1. Copy the Kanzi Connect service template directory <KanziConnectInstallation>/templates/servicetemplate to a directory where you want to create a Kanzi Connect service.

    For example, copy the template directory to the <KanziConnectInstallation>/SDK directory.

  2. Generate the C++ code from custom interface XML.

    1. Place the XML file that describes the services interface to the interfaces/definitions directory. This procedure assumes that the file that contains the description is named myService_interface.xml.

    2. Execute the <KanziConnectInstallation>/SDK/interfaces/generate.py

      $KANZI_CONNECT_SDK/interfaces/generate.py interfaces/definitions/myService_interface.xml sources/services/myservice_service/interface user --pluginoutput sources/plugins/myservice_service
      

      This command generates the sources for a non-core service from the service description in interfaces/definitions/myService_interface.xml and stores the generated code of the service to these directories:

      • sources/services/myservice_service

      • sources/plugins/myservice_service

  3. Compile the service for Linux.

    1. In the configs/platforms/common/config.py change the line

      defineServerLibraryTarget("service", "test", "service_test", sdk_root, code_root, platform_name_with_arch)
      

      to

      defineServerLibraryTarget("service","myservice", "service_myservice", sdk_root, code_root, platform_name_with_arch)
      
    2. To build the service for the linux_x11_glx target, in the configs/platforms/linux_x11_glx run

      scons project_name=service_myservice Release --dynamic
      

      When you want to create static libraries, omit --dynamic.

      When successfully compiled, you can find the service binary in the directory $KANZI_CONNECT_SDK/servicetemplate/lib/service_myservice/linux_x11_glx/GL_Release and the binary name contains the name of your service. For example, libservice_myservice.so.

      To build the service library for other platforms, replace the platform name linux_x11_glx with the platform name for which you want to build the service. For example, to build a service for QNX 7 aarch64, use qnx700_screen_aarch64 and run the service on the target hardware.

    3. In the <KanziConnectInstallation>/configs/connect_server_config.xml, add your service to the services section

      <services>
      
          ...
      
          <service library="service_myservice" name="myservice" optional="true" />
      </services
      
    4. Add the directories where the Kanzi Connect Server and libraries are stored, to the Linux library search path, and run the Kanzi Connect Server:

      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONNECT_SHARED_LIBS:$KANZI_CONNECT_SDK/servicetemplate/lib/service_myservice/linux_x11_glx/GL_Release
      $KANZI_CONNECT_SDK/lib/linux_x11_glx/GL_Release/connect_server
      

      When the Kanzi Connect Server starts successfully, it prints the information to the console log:

      2021-08-05T11:01:09+0300 140121170081600 INF [connectdomain] Registered metaclass Connect.Service.MyService from module: service_myservice
      2021-08-05T11:01:09+0300 140121170081600 INF [connectdomain] Registered module with name service_myservice
      

      You must run the first command only once. It enables the Kanzi Connect Server to find your library. Alternatively, copy the service library to the directory where the connect_server application is.