Getting started

Getting started may be the most challenging part of every new library. This guide is describing how to start with the library quickly and effectively

Download library

Library is primarly hosted on Github.

You can get it by:

  • Downloading latest release from releases area on Github

  • Cloning main branch for latest stable version

  • Cloning develop branch for latest development

Download from releases

All releases are available on Github releases area.

Clone from Github

First-time clone

This is used when you do not have yet local copy on your machine.

  • Make sure git is installed.

  • Open console and navigate to path in the system to clone repository to. Use command cd your_path

  • Clone repository with one of available options below

    • Run git clone --recurse-submodules https://github.com/MaJerle/lwpkt command to clone entire repository, including submodules

    • Run git clone --recurse-submodules --branch develop https://github.com/MaJerle/lwpkt to clone development branch, including submodules

    • Run git clone --recurse-submodules --branch main https://github.com/MaJerle/lwpkt to clone latest stable branch, including submodules

  • Navigate to examples directory and run favourite example

Update cloned to latest version

  • Open console and navigate to path in the system where your repository is located. Use command cd your_path

  • Run git pull origin main command to get latest changes on main branch

  • Run git pull origin develop command to get latest changes on develop branch

  • Run git submodule update --init --remote to update submodules to latest version

Note

This is preferred option to use when you want to evaluate library and run prepared examples. Repository consists of multiple submodules which can be automatically downloaded when cloning and pulling changes from root repository.

Add library to project

At this point it is assumed that you have successfully download library, either cloned it or from releases page. Next step is to add the library to the project, by means of source files to compiler inputs and header files in search path.

CMake is the main supported build system. Package comes with the CMakeLists.txt and library.cmake files, both located in the lwpkt directory:

  • CMakeLists.txt: Is a wrapper and only includes library.cmake file. It is used if target application uses add_subdirectory and then uses target_link_libraries to include the library in the project

  • library.cmake: It is a fully configured set of variables. User must use include(path/to/library.cmake) to include the library and must manually add files/includes to the final target

Tip

Open library.cmake file and manually analyze all the possible variables you can set for full functionality.

If you do not use the CMake, you can do the following:

  • Copy lwpkt folder to your project, it contains library files

  • Add lwpkt/src/include folder to include path of your toolchain. This is where C/C++ compiler can find the files during compilation process. Usually using -I flag

  • Add source files from lwpkt/src/ folder to toolchain build. These files are built by C/C++ compiler. CMake configuration comes with the library, allows users to include library in the project as subdirectory and library.

  • Copy lwpkt/src/include/lwpkt/lwpkt_opts_template.h to project folder and rename it to lwpkt_opts.h

  • Build the project

Configuration file

Configuration file is used to overwrite default settings defined for the essential use case. Library comes with template config file, which can be modified according to the application needs. and it should be copied (or simply renamed in-place) and named lwpkt_opts.h

Note

Default configuration template file location: lwpkt/src/include/lwpkt/lwpkt_opts_template.h. File must be renamed to lwpkt_opts.h first and then copied to the project directory where compiler include paths have access to it by using #include "lwpkt_opts.h".

Tip

If you are using CMake build system, define the variable LWPKT_OPTS_DIR before adding library’s directory to the CMake project. Variable must set the output directory path. CMake will copy the template file there, and name it as required.

Configuration options list is available available in the Configuration section. If any option is about to be modified, it should be done in configuration file

Template configuration file
 1/**
 2 * \file            lwpkt_opts_template.h
 3 * \brief           LwPKT configuration file
 4 */
 5
 6/*
 7 * Copyright (c) 2024 Tilen MAJERLE
 8 *
 9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 *
29 * This file is part of LwPKT - Lightweight packet protocol library.
30 *
31 * Author:          Tilen MAJERLE <tilen@majerle.eu>
32 * Version:         v1.3.0
33 */
34#ifndef LWPKT_OPTS_HDR_H
35#define LWPKT_OPTS_HDR_H
36
37/* Rename this file to "lwpkt_opts.h" for your application */
38
39/*
40 * Open "include/lwpkt/lwpkt_opt.h" and
41 * copy & replace here settings you want to change values
42 */
43
44#endif /* LWPKT_OPTS_HDR_H */

Note

If you prefer to avoid using configuration file, application must define a global symbol LWPKT_IGNORE_USER_OPTS, visible across entire application. This can be achieved with -D compiler option.

Minimal example code

To verify proper library setup, minimal example has been prepared. Run it in your main application file to verify its proper execution

Absolute minimum example
  1#include <stdio.h>
  2#include "lwpkt/lwpkt.h"
  3
  4/* LwPKT data */
  5static lwpkt_t pkt;
  6static lwrb_t pkt_tx_rb, pkt_rx_rb;
  7static uint8_t pkt_tx_rb_data[64], pkt_rx_rb_data[64];
  8
  9/* Data to read and write */
 10static const char* data = "Hello World\r\n";
 11
 12/**
 13 * \brief           LwPKT example code
 14 */
 15void
 16example_lwpkt(void) {
 17    lwpktr_t res;
 18    uint8_t b;
 19
 20    printf("---\r\nLwPKT default example..\r\n\r\n");
 21
 22    /* 
 23     * Initialize both ring buffers, for TX and RX operations
 24     *
 25     * Initialize LwPKT and link buffers together
 26     */
 27    lwrb_init(&pkt_tx_rb, pkt_tx_rb_data, sizeof(pkt_tx_rb_data));
 28    lwrb_init(&pkt_rx_rb, pkt_rx_rb_data, sizeof(pkt_rx_rb_data));
 29    lwpkt_init(&pkt, &pkt_tx_rb, &pkt_rx_rb);
 30
 31#if LWPKT_CFG_USE_ADDR
 32    /* Set device address (if feature enabled) */
 33    lwpkt_set_addr(&pkt, 0x12);
 34#endif /* LWPKT_CFG_USE_ADDR */
 35
 36    /* 
 37     * Write packet to the TX ringbuffer,
 38     * act as device wants to send some data
 39     */
 40    res = lwpkt_write(&pkt,
 41#if LWPKT_CFG_USE_ADDR
 42                      0x11, /* End address to whom to send */
 43#endif                      /* LWPKT_CFG_USE_ADDR */
 44#if LWPKT_CFG_USE_FLAGS
 45                      0x12345678,
 46#endif /* LWPKT_CFG_USE_FLAGS */
 47#if LWPKT_CFG_USE_CMD
 48                      0x85,                /* Command type */
 49#endif                                     /* LWPKT_CFG_USE_CMD */
 50                      data, strlen(data)); /* Length of data and actual data */
 51
 52    /*
 53     * LwPKT wrote data to pkt_tx_rb ringbuffer
 54     * Now actually transmit data over your interface
 55     * (USART for example, ...)
 56     */
 57
 58    /*
 59     * For the purpose of this example, application will
 60     * fake data transmission by doing reading from TX buffer
 61     * and writing it to RX buffer
 62     */
 63    while (lwrb_read(&pkt_tx_rb, &b, 1) == 1) {
 64        lwrb_write(&pkt_rx_rb, &b, 1);
 65    }
 66
 67    /*
 68     * Here we have our data in RX buffer
 69     * means we received data over network interface
 70     */
 71
 72    /* Now read and process packet */
 73    res = lwpkt_read(&pkt);
 74
 75    if (res == lwpktVALID) {
 76        size_t len;
 77
 78        /* Packet is valid */
 79        printf("Packet is valid!\r\n");
 80
 81        /* Print debug messages for packet */
 82#if LWPKT_CFG_USE_ADDR
 83        printf("Packet from: 0x%08X\r\n", (unsigned)lwpkt_get_from_addr(&pkt));
 84        printf("Packet to: 0x%08X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
 85#endif /* LWPKT_CFG_USE_ADDR */
 86#if LWPKT_CFG_USE_FLAGS
 87        printf("Packet flags: 0x%08X\r\n", (unsigned)lwpkt_get_flags(&pkt));
 88#endif /* LWPKT_CFG_USE_FLAGS */
 89#if LWPKT_CFG_USE_CMD
 90        printf("Packet cmd: 0x%02X\r\n", (unsigned)lwpkt_get_cmd(&pkt));
 91#endif /* LWPKT_CFG_USE_CMD */
 92        printf("Packet data length: 0x%08X\r\n", (unsigned)lwpkt_get_data_len(&pkt));
 93        if ((len = lwpkt_get_data_len(&pkt)) > 0) {
 94            uint8_t* d = lwpkt_get_data(&pkt);
 95            printf("Packet data: ");
 96            for (size_t i = 0; i < len; ++i) {
 97                printf("0x%02X ", (unsigned)d[i]);
 98            }
 99            printf("\r\n");
100        }
101
102        /* Check who should be dedicated receiver */
103#if LWPKT_CFG_USE_ADDR
104        if (lwpkt_is_for_me(&pkt)) {
105            printf("Packet is for me\r\n");
106        } else if (lwpkt_is_broadcast(&pkt)) {
107            printf("Packet is broadcast to all devices\r\n");
108        } else {
109            printf("Packet is for device ID: 0x%08X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
110        }
111#endif /* LWPKT_CFG_USE_ADDR */
112    } else if (res == lwpktINPROG) {
113        printf("Packet is still in progress, did not receive yet all bytes..\r\n");
114    } else {
115        printf("Packet is not valid!\r\n");
116    }
117}