Skip to content

Commit

Permalink
Squashed 'lib/NMEA2000/' changes from 9e14c92..6835188
Browse files Browse the repository at this point in the history
6835188 Merge pull request #61 from sarfata/do-not-redefine-bit
ab2b499 Merge pull request #60 from sarfata/seasmart-converter
6d6fe93 Do not redefine macro BIT(x) if already defined
e657718 Add link to seasmart protocol doc
74765f9 make cmake.sh executable
20b4db0 add travis config
a6ada78 Implemented Seasmart conversions
a3ba1fd Set up a test folder with Catch and cmake based build

git-subtree-dir: lib/NMEA2000
git-subtree-split: 6835188
  • Loading branch information
sarfata committed Jul 24, 2017
1 parent 556a336 commit 480933f
Show file tree
Hide file tree
Showing 15 changed files with 12,154 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk

# CMake
build
tags
45 changes: 45 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
sudo: false
language: cpp
matrix:
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.4']
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.5']
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.6']
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.7']
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.8']
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.9']
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-5']
- compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-6']
- compiler: clang
script: scripts/cmake.sh
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# The MIT License
#
# Copyright (c) 2017 Thomas Sarlandie [email protected]
#
# 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.

cmake_minimum_required(VERSION 3.0)
project(NMEA2000)

add_compile_options(
-Wall
-Werror
-std=c++11
-g
)

enable_testing()

add_subdirectory(src)
add_subdirectory(third-party/catch)
add_subdirectory(test)
8 changes: 8 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ For use with Atmel AVR processors internal CAN controller

- https://www.nmea.org/Assets/20140102%20nmea-2000-126993%20heartbeat%20pgn%20corrigendum.pdf[Heartbeat PGN 126993]

== Running Tests ==

To run the unit tests, you must have `cmake` installed:

cmake .
cmake --build .
ctest .

== License ==

Copyright (c) 2015-2017 Timo Lappalainen, Kave Oy, www.kave.fi
Expand Down
6 changes: 6 additions & 0 deletions scripts/cmake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh -ex

cmake .
cmake --build .
ctest --output-on-failure .

35 changes: 35 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

# The MIT License
#
# Copyright (c) 2017 Thomas Sarlandie [email protected]
#
# 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.


add_library(nmea2000
N2kMsg.cpp
N2kStream.cpp
N2kMessages.cpp
Seasmart.cpp
)

target_include_directories(nmea2000
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
2 changes: 2 additions & 0 deletions src/N2kMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const uint16_t N2kUInt16NA=0xffff;
const int16_t N2kInt16NA=0x7fff;
const uint32_t N2kUInt32NA=0xffffffff;
const int32_t N2kInt32NA=0x7fffffff;
#ifndef BIT
#define BIT(n) (1 << n)
#endif

inline bool N2kIsNA(double v) { return v==N2kDoubleNA; }
inline bool N2kIsNA(uint8_t v) { return v==N2kUInt8NA; }
Expand Down
187 changes: 187 additions & 0 deletions src/Seasmart.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
The MIT License
Copyright (c) 2017 Thomas Sarlandie [email protected]
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.
*/

#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "Seasmart.h"

/* Some private helper functions to generate hex-serialized NMEA messages */
static const char *hex = "0123456789ABCDEF";

static int appendByte(char *s, uint8_t byte) {
s[0] = hex[byte >> 4];
s[1] = hex[byte & 0xf];
return 2;
}

static int append2Bytes(char *s, uint16_t i) {
appendByte(s, i >> 8);
appendByte(s + 2, i & 0xff);
return 4;
}

static int appendWord(char *s, uint32_t i) {
append2Bytes(s, i >> 16);
append2Bytes(s + 4, i & 0xffff);
return 8;
}

static uint8_t nmea_compute_checksum(const char *sentence) {
if (sentence == 0) {
return 0;
}

// Skip the $ at the beginning
int i = 1;

int checksum = 0;
while (sentence[i] != '*') {
checksum ^= sentence[i];
i++;
}
return checksum;
}

size_t N2kToSeasmart(const tN2kMsg &msg, uint32_t timestamp, char *buffer, size_t size) {
size_t pcdin_sentence_length = 6+1+6+1+8+1+2+1+msg.DataLen*2+1+2 + 1;

if (size < pcdin_sentence_length) {
return 0;
}

char *s = buffer;

strcpy(s, "$PCDIN,");
s += 7;
s += appendByte(s, msg.PGN >> 16);
s += append2Bytes(s, msg.PGN & 0xffff);
*s++ = ',';
s += appendWord(s, timestamp);
*s++ = ',';
s += appendByte(s, msg.Source);
*s++ = ',';

for (int i = 0; i < msg.DataLen; i++) {
s += appendByte(s, msg.Data[i]);
}

*s++ = '*';
s += appendByte(s, nmea_compute_checksum(buffer));
*s = 0;

return (size_t)(s - buffer);
}

/*
* Attemts to read n bytes in hexadecimal from input string to value.
*
* Returns true if successful, false otherwise.
*/
static bool readNHexByte(const char *s, unsigned int n, uint32_t &value) {
if (strlen(s) < 2*n) {
return -1;
}
for (unsigned int i = 0; i < 2*n; i++) {
if (!isxdigit(s[i])) {
return -1;
}
}

char sNumber[2*n + 1];
strncpy(sNumber, s, sizeof(sNumber));
sNumber[sizeof(sNumber) - 1] = 0;

value = strtol(sNumber, 0, 16);
return true;
}

bool SeasmartToN2k(const char *buffer, uint32_t &timestamp, tN2kMsg &msg) {
msg.Clear();

const char *s = buffer;
if (strncmp("$PCDIN,", s, 6) != 0) {
return false;
}
s += 7;

uint32_t pgnHigh;
uint32_t pgnLow;
if (!readNHexByte(s, 1, pgnHigh)) {
return false;
}
s += 2;
if (!readNHexByte(s, 2, pgnLow)) {
return false;
}
s += 5;
msg.PGN = (pgnHigh << 16) + pgnLow;

if (!readNHexByte(s, 4, timestamp)) {
return false;
}
s += 9;

uint32_t source;
if (!readNHexByte(s, 1, source)) {
return false;
}
msg.Source = source;
s += 3;

int dataLen = 0;
while (s[dataLen] != 0 && s[dataLen] != '*') {
dataLen++;
}
if (dataLen % 2 != 0) {
return false;
}
dataLen /= 2;

msg.DataLen = dataLen;
if (msg.DataLen > msg.MaxDataLen) {
return false;
}
for (int i = 0; i < dataLen; i++) {
uint32_t byte;
if (!readNHexByte(s, 1, byte)) {
return false;
}
msg.Data[i] = byte;
s += 2;
}

// Skip the terminating '*' which marks beginning of checksum
s += 1;
uint32_t checksum;
if (!readNHexByte(s, 1, checksum)) {
return false;
}

if (checksum != nmea_compute_checksum(buffer)) {
return false;
}

return true;
}
Loading

0 comments on commit 480933f

Please sign in to comment.