Kanzi Connect service template

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

The code generation can provide the build files for Windows, Android, and most embedded platforms. You can use the build files to generate and compile the included service definitions, but this topic does not cover the compilation on Visual Studio.

Requirements

  • To generate code you must have Kanzi Connect and its dependencies installed.

  • To compile the service on Linux, you must have Kanzi Connect for Linux extracted on a Linux host and the environment variables set.

    You can use the helper script <KanziConnectInstallation>/SDK/setup_env.sh to set the environment variables.

    In most cases to compile the service it is enough to set:

    • KANZI_HOME to point to the Kanzi workspace that contains the Engine/lib folder for the given platform.

    • KANZI_CONNECT_SDK to point to the Kanzi Connect installation SDK folder.

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 and android_gradle build files.

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

  • sources directory contains the generated and modified source code and CMake build files.

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

Using the Kanzi Connect service template

To use the Kanzi Connect service template:

  1. To generate to directory structure required by the code generation, either:

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

      For example, copy and rename the template directory to <KanziWorkspace>/services.

      Place the XML files that describe the service interfaces to the <KanziWorkspace>/services/interfaces/definitions directory.

    • Export the Kanzi Connect simulator project with the services that you want to build. See Exporting a Simulator project.

      For example, create the <KanziWorkspace>/services directory and extract the contents of the simulator project into the directory.

  2. To generate the C++ code from the custom interface XML, execute the <KanziWorkspace>/services/interfaces/generate_interfaces.py script:

    cd interfaces
    py -3 generate_interfaces.py
    

    This way you generate the sources and build files for all service descriptions in the interfaces/definitions/ directory and store the generated code of the services in the sources/services/<service_identifier>_service directories.

  3. Compile the service for Windows.

    For example, to build the service for the Win64GL_vs2017_Release_DLL target, in the <KanziWorkspace>/services run

    py -3 %KANZI_CONNECT_SDK%\build_app.py --config GL_vs2017_Release_DLL --gfx_api GL --dynamic . win64
    
  4. Compile the service for Linux.

    1. To build the service for the linux_x11_glx target, in the <KanziWorkspace>/services run

      python3 $KANZI_CONNECT_SDK/build_app.py --config Release --gfx_api GL --dynamic . linux_x11_glx
      

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

      When successfully compiled, you can find the service binary in the directory <KanziWorkspace>services/lib/service_myservice/linux_x11_glx/GL_Release and the binary name contains the name of your service. For example, libservice_test.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.

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

      <services>
      
          ...
      
          <service library="service_test" name="test" optional="true" />
      </services
      
    3. 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:$KANZI_CONNECT_SDK/lib/linux_x11_glx/GL_Release:$KANZI_HOME/services/lib/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.Test from module: service_test
      2021-08-05T11:01:09+0300 140121170081600 INF [connectdomain] Registered module with name service_test
      

      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.