/* NMEA0183AISMsg.cpp Copyright (c) 2019 Ronnie Zeiller, www.zeiller.eu Based on the works of Timo Lappalainen NMEA2000 and NMEA0183 Library 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 "NMEA0183AISMsg.h" #include #include #include #include #include #include #include const char AsciiChar[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_ !\"#$%&\'()*+,-./0123456789:;<=>?"; const char *tNMEA0183AISMsg::EmptyAISField = "000000"; //***************************************************************************** tNMEA0183AISMsg::tNMEA0183AISMsg() { ClearAIS(); } //***************************************************************************** void tNMEA0183AISMsg::ClearAIS() { PayloadBin[0]=0; Payload[0]=0; iAddPldBin=0; iAddPld=0; } //***************************************************************************** // Add 6bit with no data. bool tNMEA0183AISMsg::AddEmptyFieldToPayloadBin(uint8_t iBits) { if ( (iAddPldBin + iBits * 6) >= AIS_BIN_MAX_LEN ) return false; // Is there room for any data for (uint8_t i=0;i= AIS_BIN_MAX_LEN ) return false; // Is there room for any data AISBitSet bset(ival); PayloadBin[iAddPldBin]=0; uint16_t iAdd=iAddPldBin; for(int i = countBits-1; i >= 0 ; i--) { PayloadBin[iAdd] = bset[i]?'1':'0'; iAdd++; } iAddPldBin += countBits; PayloadBin[iAddPldBin]=0; return true; } // **************************************************************************** bool tNMEA0183AISMsg::AddBoolToPayloadBin(bool &bval, uint8_t size) { int8_t iTemp; (bval == true)? iTemp = 1 : iTemp = 0; if ( ! AddIntToPayloadBin(iTemp, size) ) return false; return true; } // ***************************************************************************** // converts sval into binary 6-bit AScii encoded string and appends it to PayloadBin // filled up with "@" == "000000" to given bit-size bool tNMEA0183AISMsg::AddEncodedCharToPayloadBin(char *sval, size_t countBits) { if ( (iAddPldBin + countBits ) >= AIS_BIN_MAX_LEN ) return false; // Is there room for any data PayloadBin[iAddPldBin]=0; std::bitset<6> bs; char * ptr; size_t len = strlen(sval); // e.g.: should be 7 for Callsign if ( len * 6 > countBits ) len = countBits / 6; for (int i = 0; i= 0){ AddIntToPayloadBin(index, 6); } } else { AddIntToPayloadBin(0, 6); } } PayloadBin[iAddPldBin+1]=0; // fill up with "@", also covers empty sval if ( len * 6 < countBits ) { for (int i=0;i<(countBits/6-len);i++) { AddIntToPayloadBin(0, 6); } } PayloadBin[iAddPldBin]=0; return true; } // ***************************************************************************** bool tNMEA0183AISMsg::ConvertBinaryAISPayloadBinToAscii(const char *payloadbin) { uint16_t len; len = strlen( payloadbin ) / 6; // 28 uint32_t offset; char s[7]; uint8_t dec; int i; for ( i=0; i