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 versionCloning
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 submodulesRun
git clone --recurse-submodules --branch develop https://github.com/MaJerle/lwpkt
to clone development branch, including submodulesRun
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 onmain
branchRun
git pull origin develop
command to get latest changes ondevelop
branchRun
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 with git clone
command or with manual download from the library 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:
library.cmake
: It is a fully configured set of variables and with library definition. User can include this file to the project file withinclude(path/to/library.cmake)
and then manually use the variables provided by the file, such as list of source files, include paths or necessary compiler definitions. It is up to the user to properly use the this file on its own.CMakeLists.txt
: It is a wrapper-only file and includeslibrary.cmake
file. It is used for when user wants to include the library to the main project by simply calling CMakeadd_subdirectory
command, followed bytarget_link_libraries
to link external library to the final project.
Tip
Open library.cmake
and analyze the provided information. Among variables, you can also find list of all possible exposed libraries for the user.
If you do not use the CMake, you can do the following:
Copy
lwpkt
folder to your project, it contains library filesAdd
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
flagAdd source files from
lwpkt/src/
folder to toolchain build. These files are built by C/C++ compilerCopy
lwpkt/src/include/lwpkt/lwpkt_opts_template.h
to project folder and rename it tolwpkt_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_FILE
before adding library’s directory to the CMake project.
Variable must contain the path to the user options file. If not provided and to avoid build error, one will be generated in the build directory.
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
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
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 */
15int
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 printf("Tx RB content len: %u, content: ", (unsigned)lwrb_get_full(&pkt_tx_rb));
64 while (lwrb_read(&pkt_tx_rb, &b, 1) == 1) {
65 printf("0x%02X, ", (unsigned)b);
66 lwrb_write(&pkt_rx_rb, &b, 1);
67 }
68 printf("\r\n");
69
70 /*
71 * Here we have our data in RX buffer
72 * means we received data over network interface
73 */
74
75 /* Now read and process packet */
76 res = lwpkt_read(&pkt);
77
78 if (res == lwpktVALID) {
79 size_t len;
80
81 /* Packet is valid */
82 printf("Packet is valid!\r\n");
83
84 /* Print debug messages for packet */
85#if LWPKT_CFG_USE_ADDR
86 printf("Packet from: 0x%08X\r\n", (unsigned)lwpkt_get_from_addr(&pkt));
87 printf("Packet to: 0x%08X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
88#endif /* LWPKT_CFG_USE_ADDR */
89#if LWPKT_CFG_USE_FLAGS
90 printf("Packet flags: 0x%08X\r\n", (unsigned)lwpkt_get_flags(&pkt));
91#endif /* LWPKT_CFG_USE_FLAGS */
92#if LWPKT_CFG_USE_CMD
93 printf("Packet cmd: 0x%02X\r\n", (unsigned)lwpkt_get_cmd(&pkt));
94#endif /* LWPKT_CFG_USE_CMD */
95 printf("Packet data length: 0x%08X\r\n", (unsigned)lwpkt_get_data_len(&pkt));
96 if ((len = lwpkt_get_data_len(&pkt)) > 0) {
97 uint8_t* d = lwpkt_get_data(&pkt);
98 printf("Packet data: ");
99 for (size_t i = 0; i < len; ++i) {
100 printf("0x%02X ", (unsigned)d[i]);
101 }
102 printf("\r\n");
103 }
104
105 /* Check who should be dedicated receiver */
106#if LWPKT_CFG_USE_ADDR
107 if (lwpkt_is_for_me(&pkt)) {
108 printf("Packet is for me\r\n");
109 } else if (lwpkt_is_broadcast(&pkt)) {
110 printf("Packet is broadcast to all devices\r\n");
111 } else {
112 printf("Packet is for device ID: 0x%08X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
113 }
114#endif /* LWPKT_CFG_USE_ADDR */
115 } else if (res == lwpktINPROG) {
116 printf("Packet is still in progress, did not receive yet all bytes..\r\n");
117 } else {
118 printf("Packet is not valid!\r\n");
119 }
120
121 return 0;
122}