-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
15 changed files
with
12,154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,7 @@ $RECYCLE.BIN/ | |
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
# CMake | ||
build | ||
tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh -ex | ||
|
||
cmake . | ||
cmake --build . | ||
ctest --output-on-failure . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ×tamp, 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; | ||
} |
Oops, something went wrong.