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 with:

  • Downloading latest release from releases area on Github

  • Cloning master 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 3 options

    • 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 master 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 resources repository is. Use command cd your_path

  • Run git pull origin master --recurse-submodules command to pull latest changes and to fetch latest changes from submodules on master branch

  • Run git pull origin develop --recurse-submodules command to pull latest changes and to fetch latest changes from submodules on develop branch

  • Run git submodule foreach git pull origin master to update & merge all submodules

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

  • 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

  • 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 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".

List of configuration options are 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) 2020 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.2.0
33 */
34#ifndef LWPKT_HDR_OPTS_H
35#define LWPKT_HDR_OPTS_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_HDR_OPTS_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_CMD
 45        0x85,                       /* Command type */
 46#endif /* LWPKT_CFG_USE_CMD */
 47        data, strlen(data));        /* Length of data and actual data */
 48
 49    /*
 50     * LwPKT wrote data to pkt_tx_rb ringbuffer
 51     * Now actually transmit data over your interface
 52     * (USART for example, ...)
 53     */
 54
 55    /*
 56     * For the purpose of this example, application will
 57     * fake data transmission by doing reading from TX buffer
 58     * and writing it to RX buffer
 59     */
 60    while (lwrb_read(&pkt_tx_rb, &b, 1) == 1) {
 61        lwrb_write(&pkt_rx_rb, &b, 1);
 62    }
 63
 64    /*
 65     * Here we have our data in RX buffer
 66     * means we received data over network interface
 67     */
 68
 69    /* Now read and process packet */
 70    res = lwpkt_read(&pkt);
 71
 72    if (res == lwpktVALID) {
 73        size_t len;
 74
 75        /* Packet is valid */
 76        printf("Packet is valid!\r\n");
 77
 78        /* Print debug messages for packet */
 79#if LWPKT_CFG_USE_ADDR
 80        printf("Packet from: 0x%08X\r\n", (unsigned)lwpkt_get_from_addr(&pkt));
 81        printf("Packet to: 0x%08X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
 82#endif /* LWPKT_CFG_USE_ADDR */
 83#if LWPKT_CFG_USE_CMD
 84        printf("Packet cmd: 0x%02X\r\n", (unsigned)lwpkt_get_cmd(&pkt));
 85#endif /* LWPKT_CFG_USE_CMD */
 86        printf("Packet data length: 0x%08X\r\n", (unsigned)lwpkt_get_data_len(&pkt));
 87        if ((len = lwpkt_get_data_len(&pkt)) > 0) {
 88            uint8_t* d = lwpkt_get_data(&pkt);
 89            printf("Packet data: ");
 90            for (size_t i = 0; i < len; ++i) {
 91                printf("0x%02X ", (unsigned)d[i]);
 92            }
 93            printf("\r\n");
 94        }
 95
 96        /* Check who should be dedicated receiver */
 97#if LWPKT_CFG_USE_ADDR
 98        if (lwpkt_is_for_me(&pkt)) {
 99            printf("Packet is for me\r\n");
100        } else if (lwpkt_is_broadcast(&pkt)) {
101            printf("Packet is broadcast to all devices\r\n");
102        } else {
103            printf("Packet is for device ID: 0x%08X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
104        }
105#endif /* LWPKT_CFG_USE_ADDR */
106    } else if (res == lwpktINPROG) {
107        printf("Packet is still in progress, did not receive yet all bytes..\r\n");
108    } else {
109        printf("Packet is not valid!\r\n");
110    }
111}