Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Nov 15, 2020
2 parents 9d35a1b + 6339b4f commit 6afec74
Show file tree
Hide file tree
Showing 16 changed files with 1,459 additions and 246 deletions.
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Packet protocol manager

## Features

* Written in ANSI C99, compatible with ``size_t`` for size data types
* Implements simple protocol for (one target) RS-485 communication with multiple slaves
* Variable number of length bytes available to support packet longer than `255` bytes
* Supports CRC-8 integrity check
* Uses ringbuffers for TX and RX data, gives application full autonomy for data transmit or receive
* Supports manual packet state reset if timeout detected by application
* Operating system ready, thread-safe API
* User friendly MIT license

## Contribute

Fresh contributions are always welcome. Simple instructions to proceed::

1. Fork Github repository
2. Respect [C style & coding rules](https://github.com/MaJerle/c-code-style) used by the library
3. Create a pull request to develop branch with new features or bug fixes

Alternatively you may:

1. Report a bug
2. Ask for a feature request
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.452
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "packet_protocol", "packet_protocol.vcxproj", "{65B0BF4D-8CA6-48F2-8B0B-6BFD456AA57B}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lwpkt_dev", "lwpkt_dev.vcxproj", "{65B0BF4D-8CA6-48F2-8B0B-6BFD456AA57B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{65B0BF4D-8CA6-48F2-8B0B-6BFD456AA57B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>ringbuff_dev</RootNamespace>
<RootNamespace>lwpkt_dev</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
Expand Down Expand Up @@ -72,7 +72,7 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>..\..\libs\ringbuff\ringbuff\src\include;..\..\packet\src\include\;$(IncludePath)</IncludePath>
<IncludePath>..\..\libs\lwrb\src\include;..\..\lwpkt\src\include\;.;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
Expand Down Expand Up @@ -143,8 +143,8 @@
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\libs\ringbuff\ringbuff\src\ringbuff\ringbuff.c" />
<ClCompile Include="..\..\packet\src\packet\packet.c" />
<ClCompile Include="..\..\libs\lwrb\src\lwrb\lwrb.c" />
<ClCompile Include="..\..\lwpkt\src\lwpkt\lwpkt.c" />
<ClCompile Include="main.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<ClCompile Include="main.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\packet\src\packet\packet.c">
<ClCompile Include="..\..\libs\lwrb\src\lwrb\lwrb.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\libs\ringbuff\ringbuff\src\ringbuff\ringbuff.c">
<ClCompile Include="..\..\lwpkt\src\lwpkt\lwpkt.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
Expand Down
44 changes: 44 additions & 0 deletions dev/VisualStudio/lwpkt_opts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* \file lwpkt_opts_template.h
* \brief LwPKT configuration file
*/

/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwPKT - Lightweight packet protocol library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
*/
#ifndef LWPKT_HDR_OPTS_H
#define LWPKT_HDR_OPTS_H

/* Rename this file to "lwpkt_opts.h" for your application */

/*
* Open "include/lwpkt/lwpkt_opt.h" and
* copy & replace here settings you want to change values
*/

#endif /* LWPKT_HDR_OPTS_H */
79 changes: 78 additions & 1 deletion dev/VisualStudio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,87 @@

#include <stdio.h>
#include <string.h>
#include "packet/packet.h"
#include "lwpkt/lwpkt.h"

lwpkt_t pkt;
lwrb_t pkt_tx_rb, pkt_rx_rb;
uint8_t pkt_tx_rb_data[64], pkt_rx_rb_data[64];

const char* data = "Hello World\r\n";

int
main() {
lwpktr_t res;
uint8_t b;

/* Initialize buffers for packet protocol, for TX and RX */
lwrb_init(&pkt_tx_rb, pkt_tx_rb_data, sizeof(pkt_tx_rb_data));
lwrb_init(&pkt_rx_rb, pkt_rx_rb_data, sizeof(pkt_rx_rb_data));

/* Initialize packet and its buffers with default address */
lwpkt_init(&pkt, &pkt_tx_rb, &pkt_rx_rb);
#if LWPKT_CFG_USE_ADDR
lwpkt_set_addr(&pkt, 0x12);
#endif /* LWPKT_CFG_USE_ADDR */

/* Write packet data to TX ringbuff... */
res = lwpkt_write(&pkt,
#if LWPKT_CFG_USE_ADDR
0x11,
#endif /* LWPKT_CFG_USE_ADDR */
#if LWPKT_CFG_USE_CMD
0x85,
#endif /* LWPKT_CFG_USE_CMD */
data, strlen(data));

/* Transmit TX buffer data to communication line... */
/* Write received data from communication line to RX buffer */

/* Copy buffers */
while (lwrb_read(&pkt_tx_rb, &b, 1) == 1) {
lwrb_write(&pkt_rx_rb, &b, 1);
}

/* Read data from RX ringbuffer */
res = lwpkt_read(&pkt);

if (res == lwpktVALID) {
size_t len;

/* Packet is valid */
printf("Packet is valid!\r\n");

/* Print debug messages for packet */
#if LWPKT_CFG_USE_ADDR
printf("Packet from: 0x%02X\r\n", (unsigned)lwpkt_get_from_addr(&pkt));
printf("Packet to: 0x%02X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
#endif /* LWPKT_CFG_USE_ADDR */
#if LWPKT_CFG_USE_CMD
printf("Packet cmd: 0x%02X\r\n", (unsigned)lwpkt_get_cmd(&pkt));
#endif /* LWPKT_CFG_USE_CMD */
printf("Packet data length: 0x%02X\r\n", (unsigned)lwpkt_get_data_len(&pkt));
if ((len = lwpkt_get_data_len(&pkt)) > 0) {
uint8_t* d = lwpkt_get_data(&pkt);
printf("Packet data: ");
for (size_t i = 0; i < len; ++i) {
printf("0x%02X ", (unsigned)d[i]);
}
printf("\r\n");
}

/* Check who should be dedicated receiver */
#if LWPKT_CFG_USE_ADDR
if (lwpkt_is_for_me(&pkt)) {
printf("Packet is for me\r\n");
} else if (lwpkt_is_broadcast(&pkt)) {
printf("Packet is broadcast to all devices\r\n");
} else {
printf("Packet is for device ID: 0x%02X\r\n", (unsigned)lwpkt_get_to_addr(&pkt));
}
#endif /* LWPKT_CFG_USE_ADDR */
} else {
printf("Packet is not valid!\r\n");
}

return 0;
}
135 changes: 135 additions & 0 deletions libs/lwrb/src/include/lwrb/lwrb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* \file lwrb.h
* \brief LwRB - Lightweight ring buffer
*/

/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwRB - Lightweight ring buffer library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.3.1
*/
#ifndef LWRB_HDR_H
#define LWRB_HDR_H

#include <string.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
* \defgroup LWRB Lightweight ring buffer manager
* \brief Lightweight ring buffer manager
* \{
*/

/**
* \brief Enable buffer structure pointer parameter as volatile
* To use this feature, uncomment keyword below
*/
#define LWRB_VOLATILE /* volatile */

/**
* \brief Adds 2 magic words to make sure if memory is corrupted
* application can detect wrong data pointer and maximum size
*/
#define LWRB_USE_MAGIC 1

/**
* \brief Event type for buffer operations
*/
typedef enum {
LWRB_EVT_READ, /*!< Read event */
LWRB_EVT_WRITE, /*!< Write event */
LWRB_EVT_RESET, /*!< Reset event */
} lwrb_evt_type_t;

/**
* \brief Buffer structure forward declaration
*/
struct lwrb;

/**
* \brief Event callback function type
* \param[in] buff: Buffer handle for event
* \param[in] evt: Event type
* \param[in] bp: Number of bytes written or read (when used), depends on event type
*/
typedef void (*lwrb_evt_fn)(LWRB_VOLATILE struct lwrb* buff, lwrb_evt_type_t evt, size_t bp);

/**
* \brief Buffer structure
*/
typedef struct lwrb {
#if LWRB_USE_MAGIC
uint32_t magic1; /*!< Magic 1 word */
#endif /* LWRB_USE_MAGIC */
uint8_t* buff; /*!< Pointer to buffer data.
Buffer is considered initialized when `buff != NULL` and `size > 0` */
size_t size; /*!< Size of buffer data. Size of actual buffer is `1` byte less than value holds */
size_t r; /*!< Next read pointer. Buffer is considered empty when `r == w` and full when `w == r - 1` */
size_t w; /*!< Next write pointer. Buffer is considered empty when `r == w` and full when `w == r - 1` */
lwrb_evt_fn evt_fn; /*!< Pointer to event callback function */
#if LWRB_USE_MAGIC
uint32_t magic2; /*!< Magic 2 word */
#endif /* LWRB_USE_MAGIC */
} lwrb_t;

uint8_t lwrb_init(LWRB_VOLATILE lwrb_t* buff, void* buffdata, size_t size);
uint8_t lwrb_is_ready(LWRB_VOLATILE lwrb_t* buff);
void lwrb_free(LWRB_VOLATILE lwrb_t* buff);
void lwrb_reset(LWRB_VOLATILE lwrb_t* buff);
void lwrb_set_evt_fn(LWRB_VOLATILE lwrb_t* buff, lwrb_evt_fn fn);

/* Read/Write functions */
size_t lwrb_write(LWRB_VOLATILE lwrb_t* buff, const void* data, size_t btw);
size_t lwrb_read(LWRB_VOLATILE lwrb_t* buff, void* data, size_t btr);
size_t lwrb_peek(LWRB_VOLATILE lwrb_t* buff, size_t skip_count, void* data, size_t btp);

/* Buffer size information */
size_t lwrb_get_free(LWRB_VOLATILE lwrb_t* buff);
size_t lwrb_get_full(LWRB_VOLATILE lwrb_t* buff);

/* Read data block management */
void* lwrb_get_linear_block_read_address(LWRB_VOLATILE lwrb_t* buff);
size_t lwrb_get_linear_block_read_length(LWRB_VOLATILE lwrb_t* buff);
size_t lwrb_skip(LWRB_VOLATILE lwrb_t* buff, size_t len);

/* Write data block management */
void* lwrb_get_linear_block_write_address(LWRB_VOLATILE lwrb_t* buff);
size_t lwrb_get_linear_block_write_length(LWRB_VOLATILE lwrb_t* buff);
size_t lwrb_advance(LWRB_VOLATILE lwrb_t* buff, size_t len);

/**
* \}
*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* LWRB_HDR_H */

0 comments on commit 6afec74

Please sign in to comment.