Skip to content

Commit 97f9926

Browse files
author
Luis Soltero
committed
Ensure that AIS strings are ITU-R M.1371-1 compliant
1 parent c998950 commit 97f9926

File tree

6 files changed

+43
-33
lines changed

6 files changed

+43
-33
lines changed

Documents/src/changes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Changes to the Library {#changes}
22
\tableofcontents
33

4+
## 26.02.2024
5+
6+
- changed (char *) arguments to (const char *) in Set functions for PGN 129809, 129810, 129794
7+
- Added AddAISStr() method to N2kMsg class which filters AIS strings to make complient with ITU-R M.1371-1
8+
- modified Set functions for PGN 129809, 129810, 129794 to use AddAISStr()
9+
410
## 23.02.2024
511

612
- Compatibility change: Parsers ParseN2kPGN129809, ParseN2kPGN129810 and ParseN2kPGN129794 parameter list

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=NMEA2000
2-
version=4.21.1
2+
version=4.21.2
33
author=Timo Lappalainen
44
maintainer=Kave Oy <www.kave.fi>
55
sentence=NMEA 2000 library for building compatible devices for NMEA 2000 bus.

src/N2kMessages.cpp

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
2323
#include "N2kMessages.h"
2424
#include <string.h>
25-
#include <ctype.h>
2625

2726
//*****************************************************************************
2827
// System time
@@ -1609,24 +1608,6 @@ bool AppendN2kPGN129285(tN2kMsg &N2kMsg, uint16_t ID, const char* Name, double L
16091608
}
16101609
}
16111610

1612-
//*****************************************************************************
1613-
// AIS String helper
1614-
// make sure characters fall into range defined in table 14
1615-
// https://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1371-1-200108-S!!PDF-E.pdf
1616-
#define MAXAISSTRBUFLEN 21
1617-
const char *StringToAISString(const char *str, char *AISStr, size_t AISStrBufSize)
1618-
{
1619-
if ( str == nullptr || AISStr == nullptr) return nullptr;
1620-
size_t i;
1621-
for (i = 0; str[i] != '\0' && i < AISStrBufSize; i++ ) {
1622-
char c = toupper((int)str[i]);
1623-
AISStr[i] = (c >= 0x20 && c <= 0x5F) ? c : '?';
1624-
}
1625-
if ( i < AISStrBufSize ) AISStr[i]='\0';
1626-
1627-
return AISStr;
1628-
}
1629-
16301611
//*****************************************************************************
16311612
// AIS static data A
16321613
void SetN2kPGN129794(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, uint32_t UserID,
@@ -1635,14 +1616,13 @@ void SetN2kPGN129794(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, u
16351616
double Draught, const char *Destination, tN2kAISVersion AISversion, tN2kGNSStype GNSStype,
16361617
tN2kAISDTE DTE, tN2kAISTransceiverInformation AISinfo)
16371618
{
1638-
char AISstrBuf[MAXAISSTRBUFLEN];
16391619
N2kMsg.SetPGN(129794L);
16401620
N2kMsg.Priority=6;
16411621
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
16421622
N2kMsg.Add4ByteUInt(UserID);
16431623
N2kMsg.Add4ByteUInt(IMOnumber);
1644-
N2kMsg.AddStr(StringToAISString(Callsign, AISstrBuf, sizeof(AISstrBuf)), 7, false, '@');
1645-
N2kMsg.AddStr(StringToAISString(Name, AISstrBuf, sizeof(AISstrBuf)), 20, false, '@');
1624+
N2kMsg.AddAISStr(Callsign,7);
1625+
N2kMsg.AddAISStr(Name, 20);
16461626
N2kMsg.AddByte(VesselType);
16471627
N2kMsg.Add2ByteDouble(Length, 0.1);
16481628
N2kMsg.Add2ByteDouble(Beam, 0.1);
@@ -1651,7 +1631,7 @@ void SetN2kPGN129794(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, u
16511631
N2kMsg.Add2ByteUInt(ETAdate);
16521632
N2kMsg.Add4ByteUDouble(ETAtime, 0.0001);
16531633
N2kMsg.Add2ByteDouble(Draught, 0.01);
1654-
N2kMsg.AddStr(StringToAISString(Destination, AISstrBuf, sizeof(AISstrBuf)), false, 20, '@');
1634+
N2kMsg.AddAISStr(Destination, 20);
16551635
N2kMsg.AddByte((DTE & 0x01)<<6 | (GNSStype & 0x0f)<<2 | (AISversion & 0x03));
16561636
N2kMsg.AddByte(0xe0 | (AISinfo & 0x1f));
16571637
N2kMsg.AddByte(0xff);
@@ -1692,12 +1672,11 @@ bool ParseN2kPGN129794(const tN2kMsg &N2kMsg, uint8_t &MessageID, tN2kAISRepeat
16921672
// AIS static data class B part A
16931673
void SetN2kPGN129809(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, uint32_t UserID, const char *Name)
16941674
{
1695-
char AISstrBuf[MAXAISSTRBUFLEN];
16961675
N2kMsg.SetPGN(129809L);
16971676
N2kMsg.Priority=6;
16981677
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
16991678
N2kMsg.Add4ByteUInt(UserID);
1700-
N2kMsg.AddStr(StringToAISString(Name,AISstrBuf, sizeof(AISstrBuf)), 20, false, '@');
1679+
N2kMsg.AddAISStr(Name, 20);
17011680
}
17021681

17031682
bool ParseN2kPGN129809(const tN2kMsg &N2kMsg, uint8_t &MessageID, tN2kAISRepeat &Repeat, uint32_t &UserID, char *Name, size_t NameBufSize)
@@ -1720,14 +1699,13 @@ void SetN2kPGN129810(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, u
17201699
uint8_t VesselType, const char *Vendor, const char *Callsign, double Length, double Beam,
17211700
double PosRefStbd, double PosRefBow, uint32_t MothershipID)
17221701
{
1723-
char AISstrBuf[MAXAISSTRBUFLEN];
17241702
N2kMsg.SetPGN(129810L);
17251703
N2kMsg.Priority=6;
17261704
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
17271705
N2kMsg.Add4ByteUInt(UserID);
17281706
N2kMsg.AddByte(VesselType);
1729-
N2kMsg.AddStr(StringToAISString(Vendor, AISstrBuf, sizeof(AISstrBuf)), 7, false, '@');
1730-
N2kMsg.AddStr(StringToAISString(Callsign, AISstrBuf, sizeof(AISstrBuf)), 7, false, '@');
1707+
N2kMsg.AddAISStr(Vendor, 7);
1708+
N2kMsg.AddAISStr(Callsign, 7);
17311709
N2kMsg.Add2ByteUDouble(Length, 0.1);
17321710
N2kMsg.Add2ByteUDouble(Beam, 0.1);
17331711
N2kMsg.Add2ByteUDouble(PosRefStbd, 0.1);

src/N2kMessages.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,7 +4287,7 @@ inline bool ParseN2kPGNSatellitesInView(const tN2kMsg& N2kMsg, uint8_t SVIndex,
42874287
* \param IMOnumber Ship identification number by IMO
42884288
* [1 .. 999999999]; 0 = not available = default
42894289
* Not applicable to SAR aircraft
4290-
* \param Callsign Call Sign - 7x -> 6 bit ASCII characters,
4290+
* \param Callsign Call Sign - 7x -> 6 bit ASCII characters conversion as per ITU-R M.1371-1,
42914291
* \param Name Name of the vessel;
42924292
* Maximum 20 characters 6 bit ASCII;
42934293
* For SAR aircraft, it should be set
@@ -4359,7 +4359,7 @@ inline void SetN2kAISClassAStatic(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRep
43594359
* \param IMOnumber Ship identification number by IMO
43604360
* [1 .. 999999999]; 0 = not available = default
43614361
* Not applicable to SAR aircraft
4362-
* \param Callsign Call Sign - 7x -> 6 bit ASCII characters,
4362+
* \param Callsign Call Sign
43634363
* \param CallsignBufSize size of Callsign buffer
43644364
* \param Name Name of the vessel;
43654365
* Maximum 20 characters 6 bit ASCII;
@@ -4524,7 +4524,7 @@ inline bool ParseN2kAISClassBStaticPartA(const tN2kMsg &N2kMsg, uint8_t &Message
45244524
* Not applicable to SAR aircraft
45254525
* \param Vendor Unique identification of the Unit by a number as
45264526
* defined by the manufacturer
4527-
* \param Callsign Call Sign - 7x -> 6 bit ASCII characters
4527+
* \param Callsign Call Sign - 7x -> 6 bit ASCII characters conversion as per ITU-R M.1371-1
45284528
* \param Length Length/Diameter in meters
45294529
* \param Beam Beam/Diameter in meters
45304530
* \param PosRefStbd Position Reference Point from Starboard
@@ -4578,7 +4578,7 @@ inline void SetN2kAISClassBStaticPartB(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kA
45784578
* \param Vendor Unique identification of the Unit by a number as
45794579
* defined by the manufacturer
45804580
* \param VendorBufSize size of Vendor buffer
4581-
* \param Callsign Call Sign - 7x -> 6 bit ASCII characters
4581+
* \param Callsign Call Sign
45824582
* \param CallsignBufSize size of Callsign buffer
45834583
* \param Length Length/Diameter in meters
45844584
* \param Beam Beam/Diameter in meters

src/N2kMsg.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
#include <math.h>
2727
#include <stdlib.h>
2828
#include <string.h>
29+
#include <ctype.h>
2930
//#include <MemoryFree.h> // For testing used memory
3031

3132
#define Escape 0x10
@@ -213,6 +214,22 @@ void tN2kMsg::AddStr(const char *str, int len, bool UsePgm, unsigned char fillCh
213214
SetBufStr(str,len,DataLen,Data,UsePgm,fillChar);
214215
}
215216

217+
//*****************************************************************************
218+
// Add AIS String
219+
// make sure characters fall into range defined in table 14
220+
// https://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1371-1-200108-S!!PDF-E.pdf
221+
void tN2kMsg::AddAISStr(const char *str, int len) {
222+
char AISStr[21]; // Max AIS strlen defined in ITU-R M.1371-1
223+
size_t i;
224+
for (i = 0; str[i] != '\0' && i < sizeof(AISStr); i++ ) {
225+
char c = toupper((int)str[i]);
226+
AISStr[i] = (c >= 0x20 && c <= 0x5F) ? c : '?';
227+
}
228+
AISStr[i]='\0';
229+
AddStr(AISStr,len,false,'@');
230+
}
231+
232+
216233
//*****************************************************************************
217234
void tN2kMsg::AddVarStr(const char *str, bool UsePgm) {
218235
int len=(str!=0?strlen(str):0);

src/N2kMsg.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,15 @@ class tN2kMsg
950950
*/
951951
void AddStr(const char *str, int len, bool UsePgm=false, unsigned char fillChar=0xff);
952952

953+
/************************************************************************//**
954+
* \brief Add string value to the buffer after filtering characters as defined in ITU-R M.1371-1
955+
* The string will be added to the end (indicated by \ref DataLen) of
956+
* the byte array \ref Data.
957+
* \param str String as pointer to a char array
958+
* \param len Length of the string
959+
*/
960+
void AddAISStr(const char *str, int len);
961+
953962
/************************************************************************//**
954963
* \brief Add string value to the buffer
955964
* This method determines the length of the string by it self using strlen().

0 commit comments

Comments
 (0)