Skip to content

Commit 5a53772

Browse files
committed
Fix N2K AIS string encoding: STRING_LAU for AtoN name (129041)/safety text (129802), AddAISStr for fixed name/callsign fields
1 parent 8833c64 commit 5a53772

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

Source/IO/N2KStream.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ namespace IO
210210
int ais_version = 0, IMO = 0, shiptype = 0, to_bow = 0, to_stern = 0, to_starboard = 0, to_port = 0, month = 0, day = 0, hour = 0;
211211
int minute = 0, epfd = 0, dte = 0, nDays = 0;
212212
char callsign[7], shipname[20], destination[20];
213-
std::memset(callsign, ' ', sizeof(callsign));
214-
std::memset(shipname, ' ', sizeof(shipname));
215-
std::memset(destination, ' ', sizeof(destination));
213+
std::memset(callsign, 0, sizeof(callsign));
214+
std::memset(shipname, 0, sizeof(shipname));
215+
std::memset(destination, 0, sizeof(destination));
216216
float draught = 0;
217217

218218
for (const JSON::Member &p : data[0].getMembers())
@@ -303,8 +303,8 @@ namespace IO
303303
N2kMsg.AddByte((ais.repeat() & 0x03) << 6 | (ais.type() & 0x3f));
304304
N2kMsg.Add4ByteUInt(ais.mmsi());
305305
N2kMsg.Add4ByteUInt(IMO);
306-
N2kMsg.AddStr(callsign, 7);
307-
N2kMsg.AddStr(shipname, 20);
306+
N2kMsg.AddAISStr(callsign, 7);
307+
N2kMsg.AddAISStr(shipname, 20);
308308
N2kMsg.AddByte(shiptype);
309309
N2kMsg.Add2ByteDouble(to_bow + to_stern, 0.1);
310310
N2kMsg.Add2ByteDouble(to_port + to_starboard, 0.1);
@@ -313,7 +313,7 @@ namespace IO
313313
N2kMsg.Add2ByteUInt(nDays);
314314
N2kMsg.Add4ByteUInt((hour * 3600 + minute * 60) * 10000);
315315
N2kMsg.Add2ByteDouble(draught, 0.01);
316-
N2kMsg.AddStr(destination, 20);
316+
N2kMsg.AddAISStr(destination, 20);
317317
N2kMsg.AddByte((dte & 0x01) << 6 | (epfd & 0x0f) << 2 | (ais_version & 0x03));
318318
N2kMsg.AddByte(0xe0 | (ais.getChannel() == 'A' ? 0 : 1));
319319
N2kMsg.AddByte(0xff);
@@ -414,7 +414,7 @@ namespace IO
414414
N2kMsg.AddByte((ais.repeat() & 0x03) << 6 | ais.type());
415415
N2kMsg.Add4ByteUInt(ais.mmsi());
416416
N2kMsg.AddByte(0xe0 | (ais.getChannel() == 'A' ? 0 : 1));
417-
N2kMsg.AddStr(text.c_str(), text.size());
417+
N2kMsg.AddVarStr(text.c_str(), 163, tN2kMsg::vss_ForceASCII); // safety text is STRING_LAU (max 163)
418418

419419
N2K::N2KInterface.sendMsg(N2kMsg);
420420
}
@@ -505,7 +505,7 @@ namespace IO
505505
double lat = LAT_UNDEFINED, lon = LON_UNDEFINED, cog = COG_UNDEFINED, speed = SPEED_UNDEFINED;
506506
int heading = HEADING_UNDEFINED, shiptype = 0, to_bow = 0, to_stern = 0, second = 0, to_port = 0, to_starboard = 0, epfd = 0, accuracy = 0, raim = 0, dte = 0, assigned = 0;
507507
char shipname[20];
508-
std::memset(shipname, ' ', sizeof(shipname));
508+
std::memset(shipname, 0, sizeof(shipname));
509509

510510
for (const JSON::Member &p : data[0].getMembers())
511511
{
@@ -588,7 +588,7 @@ namespace IO
588588
N2kMsg.Add2ByteDouble(to_port + to_starboard, 0.1);
589589
N2kMsg.Add2ByteDouble(to_starboard, 0.1);
590590
N2kMsg.Add2ByteDouble(to_bow, 0.1);
591-
N2kMsg.AddStr(shipname, 20);
591+
N2kMsg.AddAISStr(shipname, 20);
592592
N2kMsg.AddByte(dte | assigned << 1);
593593
N2kMsg.AddByte(0x00);
594594
N2kMsg.AddByte(0xff);
@@ -603,19 +603,15 @@ namespace IO
603603
int off_position = 0, regional = 0, raim = 0, virtual_aid = 0, assigned = 0;
604604
double lat = LAT_UNDEFINED, lon = LON_UNDEFINED;
605605

606-
char name[20];
607-
std::memset(name, ' ', sizeof(name));
606+
std::string name;
608607

609608
for (const JSON::Member &p : data[0].getMembers())
610609
{
611610
switch (p.Key())
612611
{
613612
case AIS::KEY_NAME:
614-
{
615-
const std::string &s = p.Get().getString();
616-
std::memcpy(name, s.c_str(), std::min(sizeof(name), s.size()));
617-
}
618-
break;
613+
name = p.Get().getString();
614+
break;
619615
case AIS::KEY_AID_TYPE:
620616
aid_type = p.Get().getInt();
621617
break;
@@ -678,7 +674,7 @@ namespace IO
678674
N2kMsg.AddByte(epfd << 1);
679675
N2kMsg.AddByte(regional);
680676
N2kMsg.AddByte((ais.getChannel() == 'A' ? 1 : 0) | 0xe0);
681-
N2kMsg.AddVarStr(name);
677+
N2kMsg.AddVarStr(name.c_str(), 34, tN2kMsg::vss_ForceASCII); // AtoN name is STRING_LAU (max 34)
682678

683679
N2K::N2KInterface.sendMsg(N2kMsg);
684680
}
@@ -688,9 +684,9 @@ namespace IO
688684

689685
int shiptype = 0, to_bow = 0, to_stern = 0, to_starboard = 0, to_port = 0, mothership_mmsi = 0, partno = 0;
690686
char shipname[20], callsign[7], vendorid[7];
691-
std::memset(shipname, ' ', sizeof(shipname));
692-
std::memset(callsign, ' ', sizeof(callsign));
693-
std::memset(vendorid, ' ', sizeof(vendorid));
687+
std::memset(shipname, 0, sizeof(shipname));
688+
std::memset(callsign, 0, sizeof(callsign));
689+
std::memset(vendorid, 0, sizeof(vendorid));
694690

695691
for (const JSON::Member &p : data[0].getMembers())
696692
{
@@ -746,7 +742,7 @@ namespace IO
746742
N2kMsg.Priority = 6;
747743
N2kMsg.AddByte(ais.repeat() << 6 | ais.type());
748744
N2kMsg.Add4ByteUInt(ais.mmsi());
749-
N2kMsg.AddStr(shipname, 20);
745+
N2kMsg.AddAISStr(shipname, 20);
750746
}
751747
else
752748
{
@@ -755,8 +751,8 @@ namespace IO
755751
N2kMsg.AddByte(ais.repeat() << 6 | ais.type());
756752
N2kMsg.Add4ByteUInt(ais.mmsi());
757753
N2kMsg.AddByte(shiptype);
758-
N2kMsg.AddStr(vendorid, 7);
759-
N2kMsg.AddStr(callsign, 7);
754+
N2kMsg.AddAISStr(vendorid, 7);
755+
N2kMsg.AddAISStr(callsign, 7);
760756
N2kMsg.Add2ByteUDouble(to_bow + to_stern, 0.1);
761757
N2kMsg.Add2ByteUDouble(to_port + to_starboard, 0.1);
762758
N2kMsg.Add2ByteUDouble(to_starboard, 0.1);

0 commit comments

Comments
 (0)