Permalink
Browse files

error handling now in place. starting on classb

git-svn-id: https://cowfish.unh.edu/projects/schwehr/trunk/src/libais@13631 a19cddd1-5311-0410-bb07-9ca93daf0f0b
  • Loading branch information...
1 parent cc8f563 commit 554518396477430b21f6a9e1bbcc91f78c856f9e schwehr committed May 5, 2010
Showing with 201 additions and 75 deletions.
  1. +18 −2 Makefile
  2. +12 −1 ais.cpp
  3. +35 −8 ais.h
  4. +15 −7 ais123.cpp
  5. +9 −3 ais4_11.cpp
  6. +4 −7 ais5.cpp
  7. +16 −16 ais7_13.cpp
  8. +52 −28 ais_py.cpp
  9. +40 −3 test_libais.cpp
View
@@ -3,15 +3,16 @@ VERSION:=${shell cat VERSION}
#CXXFLAGS += -O3 -funroll-loops -fexpensive-optimizations -DNDEBUG
#CXXFLAGS += -ffast-math
-CXXFLAGS += -g3 -O0
+CXXFLAGS := -g3 -O0
CXXFLAGS += -D_GLIBCXX_DEBUG
-CXXFLAGS := -Wall
+CXXFLAGS += -Wall
CXXFLAGS += -Wimplicit
CXXFLAGS += -pedantic
CXXFLAGS += -W
CXXFLAGS += -Wredundant-decls
CXXFLAGS += -Werror
+#CFLAGS := ${CXXFLAGS}
SRCS:= ais.cpp ais123.cpp ais4_11.cpp ais5.cpp ais7_13.cpp
OBJS:=${SRCS:.cpp=.o}
@@ -61,4 +62,19 @@ libais.a: ${OBJS}
clean:
-rm -rf *.o build *.a
+#.cpp.o:
+# ${CXX} -c $< ${CXXFLAGS}
+
test_libais: ${OBJS} test_libais.cpp
+ @echo SRCS: ${SRCS}
+ @echo OBJS: ${OBJS}
+ ${CXX} -o $@ ${OBJS} test_libais.cpp ${CXXFLAGS}
+
+# Hard coded depends
+ais.o: ais.h
+ais123.o: ais.h
+ais4_11.o: ais.h
+ais5.o: ais.h
+ais7_13.o: ais.h
+ais_py.o: ais.h
+test_libais.o: ais.h
View
13 ais.cpp
@@ -14,7 +14,7 @@ std::bitset<6> nmea_ord[128];
bool nmea_ord_initialized = false;
void build_nmea_lookup() {
- std::cout << "building lut..." << std::endl;
+ //std::cout << "building lut..." << std::endl;
for (int c=0; c < 128; c++) {
int val = c - 48;
if (val>=40) val-= 8;
@@ -29,5 +29,16 @@ void build_nmea_lookup() {
nmea_ord_initialized = true;
}
+// for decoding str bits inside of a binary message
const std::string bits_to_char_tbl="@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^- !\"#$%&`()*+,-./0123456789:;<=>?";
+const char * const AIS_STATUS_STRINGS[AIS_STATUS_NUM_CODES] = {
+ "AIS_ERR_NONE",
+ "AIS_ERR_BAD_BIT_COUNT",
+ "AIS_ERR_WRONG_MSG_TYPE",
+ "AIS_ERR_BAD_NMEA_CHR",
+ "AIS_ERR_BAD_PTR",
+ "AIS_ERR_UNKNOWN_MSG_TYPE",
+ "AIS_ERR_MSG_NOT_IMPLEMENTED",
+ "AIS_ERR_EXPECTED_STRING"
+};
View
43 ais.h
@@ -15,7 +15,30 @@ void build_nmea_lookup();
//void aivdm_to_bits(bitset<168> &bits, const char *nmea_payload);
-class Ais1_2_3 {
+enum AIS_STATUS {
+ AIS_OK,
+ AIS_ERR_BAD_BIT_COUNT,
+ AIS_ERR_WRONG_MSG_TYPE,
+ AIS_ERR_BAD_NMEA_CHR,
+ AIS_ERR_BAD_PTR,
+ AIS_ERR_UNKNOWN_MSG_TYPE,
+ AIS_ERR_MSG_NOT_IMPLEMENTED, // Meaning I haven't got to it yet
+ AIS_ERR_EXPECTED_STRING,
+ AIS_STATUS_NUM_CODES
+};
+
+extern const char *const AIS_STATUS_STRINGS[AIS_STATUS_NUM_CODES];
+
+class AisMsg {
+public:
+ bool had_error() {return status != AIS_OK;}
+ AIS_STATUS get_error() {return status;}
+protected:
+ AIS_STATUS status; // AIS_OK or error code
+ void init() {status = AIS_OK;}
+};
+
+class Ais1_2_3 : public AisMsg {
public:
int message_id;
int repeat_indicator;
@@ -68,7 +91,7 @@ class Ais1_2_3 {
std::ostream& operator<< (std::ostream& o, Ais1_2_3 const& a);
// 4 bsreport and 11 utc date response
-class Ais4_11 {
+class Ais4_11 : public AisMsg {
public:
int message_id;
int repeat_indicator;
@@ -111,7 +134,7 @@ class Ais4_11 {
std::ostream& operator<< (std::ostream& o, Ais4_11 const& msg);
-class Ais5 {
+class Ais5 : public AisMsg {
public:
int message_id;
int repeat_indicator;
@@ -143,7 +166,7 @@ std::ostream& operator<< (std::ostream& o, Ais5 const& msg);
// FIX: figure out how to handle Ais6
// msg 6 and 12 ack
-class Ais7_13 {
+class Ais7_13 : public AisMsg {
public:
int message_id;
int repeat_indicator;
@@ -170,17 +193,21 @@ std::ostream& operator<< (std::ostream& o, Ais7_13 const& msg);
extern std::bitset<6> nmea_ord[128];
template<size_t T>
-void aivdm_to_bits(std::bitset<T> &bits, const char *nmea_payload) {
+AIS_STATUS aivdm_to_bits(std::bitset<T> &bits, const char *nmea_payload) {
assert (nmea_payload);
assert (strlen(nmea_payload) <= T/6 );
- CHECKPOINT;
for (size_t char_idx=0; nmea_payload[char_idx] != '\0' && char_idx < T/6; char_idx++) {
- //std::cout << "FIX: " << char_idx << std::endl;
- const std::bitset<6> bs_for_char = nmea_ord[ int(nmea_payload[char_idx]) ];
+ int c = int(nmea_payload[char_idx]);
+ if (c<48 or c>119 or (c>=88 and c<=95) ) {
+ //std::cout << "bad character: '" << nmea_payload[char_idx] << "' " << c << std::endl;
+ return AIS_ERR_BAD_NMEA_CHR;
+ }
+ const std::bitset<6> bs_for_char = nmea_ord[ c ];
for (size_t offset=0; offset < 6 ; offset++) {
bits[char_idx*6 + offset] = bs_for_char[offset];
}
}
+ return AIS_OK;
}
template<size_t T>
View
@@ -1,5 +1,4 @@
// Since Apr 2010
-// g++ ais_pos.cxx -o ais_pos -g -Wall -O3 -Wimplicit -W -Wredundant-decls -pedantic -funroll-loops -fexpensive-optimizations
#include "ais.h"
@@ -8,25 +7,33 @@
#include <string>
#include <cassert>
#include <cmath>
-//using namespace std;
Ais1_2_3::Ais1_2_3(const char *nmea_payload) {
- //build_nmea_lookup();
+ assert(nmea_payload);
+
+ init();
+
+ if (strlen(nmea_payload) != 168/6) {
+ status = AIS_ERR_BAD_BIT_COUNT;
+ return;
+ }
std::bitset<168> bs; // 1 slot
- aivdm_to_bits(bs, nmea_payload);
- std::cout << bs;
+ status = aivdm_to_bits(bs, nmea_payload);
+ if (had_error()) return;
message_id = ubits(bs, 0, 6);
- assert (message_id >= 1 and message_id <= 3);
+ if (message_id < 1 or message_id > 3) {
+ status = AIS_ERR_WRONG_MSG_TYPE;
+ return;
+ }
repeat_indicator = ubits(bs,6,2);
mmsi = ubits(bs,8,30);
nav_status = ubits(bs,38,4);
const int rot_raw = sbits(bs,42,8);
- assert(rot_raw == -128);
rot_over_range = abs(rot_raw) > 126 ? true : false ;
rot = 4.733 * sqrt(fabs(rot_raw));
if (rot_raw < 0) rot = -rot;
@@ -85,6 +92,7 @@ Ais1_2_3::Ais1_2_3(const char *nmea_payload) {
assert (false);
}
} else {
+ std::cout << "expecting 3: " << message_id << std::endl;
assert (3 == message_id);
slot_increment = ubits(bs, 151, 13);
slot_increment_valid = true;
View
@@ -9,13 +9,19 @@
//#include <cmath>
Ais4_11::Ais4_11(const char *nmea_payload) {
+ init();
std::bitset<168> bs;
- aivdm_to_bits(bs, nmea_payload);
- std::cout << bs << "\n";
+
+ status = aivdm_to_bits(bs, nmea_payload);
+ if (had_error()) return;
message_id = ubits(bs, 0, 6);
- assert (message_id == 4 or message_id == 11);
+ if (message_id != 4 and message_id != 11) {
+ status = AIS_ERR_WRONG_MSG_TYPE;
+ return;
+ }
+
repeat_indicator = ubits(bs,6,2);
mmsi = ubits(bs,8,30);
View
@@ -8,18 +8,15 @@
#include <string>
#include <cassert>
#include <cmath>
-//using namespace std;
-
-#define UNUSED __attribute((__unused__))
-
-
Ais5::Ais5(const char *nmea_payload) {
+ init();
+
std::bitset<426> bs; // 424 + 2 spare bits
- aivdm_to_bits(bs, nmea_payload);
+ status = aivdm_to_bits(bs, nmea_payload);
+ if (had_error()) return;
- std::cout << bs << std::endl;
message_id = ubits(bs, 0, 6);
message_id = ubits(bs, 0, 6);
View
@@ -5,37 +5,37 @@
//#include <string>
#include <cassert>
//#include <vector>
-using namespace std;
+//using namespace std;
Ais7_13::Ais7_13(const char *nmea_payload) {
+ init();
+
assert (nmea_payload);
const size_t num_bits = strlen(nmea_payload) * 6;
- cout << "num_bits: " << num_bits << endl;
+ //cout << "num_bits: " << num_bits << endl;
- assert ( (40+32*1)==num_bits or (40+32*2)==num_bits or (40+32*3)==num_bits or (40+32*4) == num_bits);
+ if (! ((40+32*1)==num_bits or (40+32*2)==num_bits or (40+32*3)==num_bits or (40+32*4)==num_bits) ){
+ status = AIS_ERR_BAD_BIT_COUNT;
+ return;
+ }
std::bitset<168> bs;
- CHECKPOINT;
-
- aivdm_to_bits(bs, nmea_payload);
+ status = aivdm_to_bits(bs, nmea_payload);
+ if (had_error()) return;
message_id = ubits(bs, 0, 6);
- cout << "message_id: " << message_id << endl;
- assert (message_id == 7 or message_id == 13);
+ if (message_id != 7 and message_id != 13) {
+ status = AIS_ERR_WRONG_MSG_TYPE;
+ return;
+ }
repeat_indicator = ubits(bs,6,2);
mmsi = ubits(bs,8,30);
-
spare = ubits(bs,38,2);
const size_t num_acks = (num_bits - 40) / 32;
for (size_t i=0; i < num_acks; i++) {
- const size_t start = 40 + i*32;
- cout << i << ": " << start << endl;
- const int id = ubits(bs,40+i*32,30);
- cout << "id: " << id << endl;
- const int seq = ubits(bs,40+i*32+30,2);
- dest_mmsi.push_back(id);
- seq_num.push_back(seq);
+ dest_mmsi.push_back(ubits(bs,40+i*32,30));
+ seq_num.push_back(ubits(bs,40+i*32+30,2));
}
}
Oops, something went wrong.

0 comments on commit 5545183

Please sign in to comment.