Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
nschurando committed Oct 29, 2023
0 parents commit 4e8fa36
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Standard: Cpp11
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
KeepEmptyLinesAtTheStartOfBlocks: true
AllowShortIfStatementsOnASingleLine: Always
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patreon: sitronlabs
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Sitron Labs

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.
3 changes: 3 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tic KEYWORD1
setup KEYWORD2
process KEYWORD2
15 changes: 15 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Sitron_Labs_TIC_Arduino_Library",
"version": "0.1.0",
"description": "Arduino library for parsing consumer-side data provided by electricity meters in France.",
"keywords": "sitron labs,sitron,labs,arduino,library,uart,enedis,linky,tic,tele information client,parser",
"repository": {
"type": "git",
"url": "https:\/\/github.com\/sitronlabs\/SitronLabs_Enedis_TIC_Arduino_Library.git"
},
"frameworks": "arduino",
"platforms": "*",
"build": {
"includeDir": "src"
}
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Sitron Labs TIC Arduino Library
version=0.1.0
author=Sitron Labs
maintainer=Sitron Labs <[email protected]>
sentence=Arduino library for parsing consumer-side data provided by electricity meters in France.
paragraph=
category=Data Processing
url=https://github.com/sitronlabs/SitronLabs_Enedis_TIC_Arduino_Library
architectures=*
184 changes: 184 additions & 0 deletions src/tic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/* Self header */
#include "tic.h"

/**
* @brief
* @param[in] uart
* @return
*/
int tic::setup(Stream &uart) {

/* Save uart */
m_stream = &uart;

/* Reset state machine */
m_sm = STATE_0;

/* Return success */
return 0;
}

/**
* @brief
* @param[out] message
* @return
*/
int tic::process(struct tic_message &message) {

/* Ensure setup has been performed */
if (m_stream == NULL) {
return -EINVAL;
}

/* Process incoming bytes */
while (m_stream->available() > 0) {

/* Read byte */
int rx = m_stream->read();
if (rx < 0 || rx > UINT8_MAX) {
m_sm = STATE_0;
return -EIO;
}

/* Perform parity check */
if (__builtin_parity(rx) != 0) {
m_sm = STATE_0;
return -EIO;
}

/* Remove parity bit */
rx = rx & 0x7F;

/* Parsing state machine */
switch (m_sm) {

case STATE_0: { // Wait for frame start

if (rx == 0x02) {
m_sm = STATE_1;
break;
}
break;
}

case STATE_1: { // Wait for either message start or frame stop

if (rx == 0x0A) { // Message start
m_buffer_index = 0;
m_sm = STATE_2;
} else if (rx == 0x03) { // Frame stop
m_sm = STATE_0;
} else {
m_sm = STATE_0;
return -EIO;
}
break;
}

case STATE_2: { // Parse message tag

if (rx == 0x09 || rx == 0x20) {
m_buffer[m_buffer_index] = rx;
m_buffer_index++;
m_splitter_char = rx;
m_sm = STATE_3;
} else {
if (m_buffer_index >= 8) {
m_sm = STATE_0;
return -EIO;
} else {
m_buffer[m_buffer_index] = rx;
m_buffer_index++;
}
}
break;
}

case STATE_3: { // Wait for message stop

/* If we receive the end of message character */
if (rx == 0x0D) {

/* Verify checksum:
* - for historic messages, where the splitter char is 0x20, the checksum doesn't include the last splitter char,
* - for standard messages, where the splitter char is 0x09, the checkum includes the last splitter char.
* Go figure */
if (m_splitter_char == 0x20) {
uint8_t checksum_received = m_buffer[m_buffer_index - 1];
uint8_t checksum_computed = 0x00;
for (size_t i = 0; i < m_buffer_index - 2; i++) checksum_computed += m_buffer[i];
checksum_computed = (checksum_computed & 0x3F) + 0x20;
if (checksum_computed != checksum_received) {
m_sm = STATE_0;
return -EIO;
}
} else if (m_splitter_char == 0x09) {
uint8_t checksum_received = m_buffer[m_buffer_index - 1];
uint8_t checksum_computed = 0x00;
for (size_t i = 0; i < m_buffer_index - 1; i++) checksum_computed += m_buffer[i];
checksum_computed = (checksum_computed & 0x3F) + 0x20;
if (checksum_computed != checksum_received) {
m_sm = STATE_0;
return -EIO;
}
} else {
m_sm = STATE_0;
return -EIO;
}

/* Split message */
uint8_t splitter_posistions[3] = {0};
uint8_t splitter_count = 0;
for (uint8_t i = 0; i < m_buffer_index - 1; i++) {
if (m_buffer[i] == m_splitter_char) {
if (splitter_count < 3) {
splitter_posistions[splitter_count] = i;
}
splitter_count++;
}
}

/* Fill message struct and move on */
if (splitter_count == 2) {
strncpy(message.name, (char *)(&m_buffer[0]), splitter_posistions[0]);
strncpy(message.data, (char *)(&m_buffer[splitter_posistions[0] + 1]), splitter_posistions[1] - (splitter_posistions[0] + 1));
message.name[min(8, splitter_posistions[0])] = '\0';
message.data[min(12, splitter_posistions[1] - (splitter_posistions[0] + 1))] = '\0';
m_sm = STATE_1;
return 1;
} else if (splitter_count == 3) {
strncpy(message.name, (char *)(&m_buffer[0]), splitter_posistions[0]);
strncpy(message.data, (char *)(&m_buffer[splitter_posistions[1] + 1]), splitter_posistions[2] - (splitter_posistions[1] + 1));
message.name[min(8, splitter_posistions[0])] = '\0';
message.data[min(12, splitter_posistions[2] - (splitter_posistions[1] + 1))] = '\0';
m_sm = STATE_1;
return 1;
} else {
m_sm = STATE_0;
return -EIO;
}
break;
}

/* Otherwise, keep appending data to current message */
else {
if (m_buffer_index >= 37) {
m_sm = STATE_0;
} else {
m_buffer[m_buffer_index] = rx;
m_buffer_index++;
}
}
break;
}

default: {
m_sm = STATE_0;
break;
}
}
}

/* Return no message */
return 0;
}
44 changes: 44 additions & 0 deletions src/tic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef TIC_H
#define TIC_H

/* Arduino libraries */
#include <Arduino.h>

/* C/C++ libraries */
#include <errno.h>
#include <stdint.h>

/**
*
*/
struct tic_message {
char name[8 + 1]; //!< Label of the dataset (null-terminated)
char data[12 + 1]; //!< Content of the dataset (null-terminated)
};

/**
*
*/
class tic {
public:
/* Setup */
int setup(Stream &uart);

/* Process incoming data */
int process(struct tic_message &message);

protected:
Stream *m_stream = NULL;
enum {
STATE_0,
STATE_1,
STATE_2,
STATE_3,
STATE_ERROR,
} m_sm;
size_t m_buffer_index;
uint8_t m_buffer[8 + 1 + 13 + 1 + 12 + 1 + 1];
uint8_t m_splitter_char;
};

#endif

0 comments on commit 4e8fa36

Please sign in to comment.