Skip to content

Commit

Permalink
message 1-5 are able to decode in python
Browse files Browse the repository at this point in the history
git-svn-id: https://cowfish.unh.edu/projects/schwehr/trunk/src/libais@13603 a19cddd1-5311-0410-bb07-9ca93daf0f0b
  • Loading branch information
schwehr committed May 3, 2010
1 parent 9ca2bcc commit 1531d7c
Show file tree
Hide file tree
Showing 10 changed files with 766 additions and 135 deletions.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#CXXFLAGS += -O3 -funroll-loops -fexpensive-optimizations -DNDEBUG
#CXXFLAGS += -ffast-math

CXXFLAGS += -g3 -O0
CXXFLAGS += -D_GLIBCXX_DEBUG
CXXFLAGS := -Wall
CXXFLAGS += -Wimplicit
CXXFLAGS += -pedantic
CXXFLAGS += -W
CXXFLAGS += -Wredundant-decls
CXXFLAGS += -Werror


SRCS:= ais.cpp ais123.cpp ais4_11.cpp ais5.cpp
OBJS:=${SRCS:.cpp=.o}

# Remove the NDEBUG that python tries to use
default:
CFLAGS="-m32 -O0 -g -D_GLIBCXX_DEBUG -UNDEBUG" /sw/bin/python setup.py build

libais.a: ${OBJS}
ls ${OBJS}
ar rv $@ $?
ranlib $@

clean:
-rm *.o
-rm -rf build
-rm *.a
6 changes: 6 additions & 0 deletions TODO.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

* List of things that need to be done for this library.

- Python error handling
- code all the messages that are around
- build testing infrastructure (nose?)
33 changes: 33 additions & 0 deletions ais.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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"

#include <iostream>
#include <bitset>
#include <string>
#include <cassert>
#include <cmath>
//using namespace std;

std::bitset<6> nmea_ord[128];
bool nmea_ord_initialized = false;

void build_nmea_lookup() {
std::cout << "building lut..." << std::endl;
for (int c=0; c < 128; c++) {
int val = c - 48;
if (val>=40) val-= 8;
if (val < 0) continue;
std::bitset<6> bits(val);
bool tmp;
tmp = bits[5]; bits[5] = bits[0]; bits[0] = tmp;
tmp = bits[4]; bits[4] = bits[1]; bits[1] = tmp;
tmp = bits[3]; bits[3] = bits[2]; bits[2] = tmp;
nmea_ord[c] = bits;
}
nmea_ord_initialized = true;
}

const std::string bits_to_char_tbl="@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^- !\"#$%&`()*+,-./0123456789:;<=>?";

147 changes: 143 additions & 4 deletions ais.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// -*- c++ -*-
// Since Apr 2010
// g++ ais_pos.cxx -o ais_pos -g -Wall -O3 -Wimplicit -W -Wredundant-decls -pedantic -funroll-loops -fexpensive-optimizations

#include <bitset>
using namespace std;
#include <string>
#include <cassert>
//using namespace std;

extern bool nmea_ord_initialized; // If this is false, you need to call build_nmea_lookup.

void build_nmea_lookup();

void aivdm_to_bits(bitset<168> &bits, const char *nmea_payload);
//void aivdm_to_bits(bitset<168> &bits, const char *nmea_payload);

class AisPos123 {
class Ais1_2_3 {
public:
int message_id;
int repeat_indicator;
Expand Down Expand Up @@ -55,6 +60,140 @@ class AisPos123 {
bool keep_flag;
bool keep_flag_valid;

AisPos123(const char *nmea_payload);
Ais1_2_3(const char *nmea_payload);
void print();
};
std::ostream& operator<< (std::ostream& o, Ais1_2_3 const& a);

// 4 bsreport and 11 utc date response
class Ais4_11 {
public:
int message_id;
int repeat_indicator;
int mmsi;
int year;
int month;
int day;
int hour;
int minute;
int second;
int position_accuracy;
float x, y; // longitude, latitude
int fix_type;
int spare;
bool raim;

// COMM state SOTDMA msgs 1 and 2
int sync_state;
int slot_timeout;

// Based on slot_timeout which ones are valid
int received_stations;
bool received_stations_valid;

int slot_number;
bool slot_number_valid;

bool utc_valid; // Only means that the values are set. Can still have
int utc_hour;
int utc_min;
int utc_spare;

int slot_offset;
bool slot_offset_valid;

// **NO** ITDMA
Ais4_11(const char *nmea_payload);
void print();
};
std::ostream& operator<< (std::ostream& o, Ais4_11 const& a);


class Ais5 {
public:
int message_id;
int repeat_indicator;
int mmsi;
int ais_version;
int imo_num;
std::string callsign;
std::string name;
int type_and_cargo;
int dim_a;
int dim_b;
int dim_c;
int dim_d;
int fix_type;
int eta_month;
int eta_day;
int eta_hour;
int eta_minute;
float draught; // present static draft. m
std::string destination;
int dte;
int spare;

Ais5(const char *nmea_payload);
void print();
};

extern std::bitset<6> nmea_ord[128];

template<size_t T>
void aivdm_to_bits(std::bitset<T> &bits, const char *nmea_payload) {
assert (nmea_payload);
assert (strlen(nmea_payload) <= T/6 );
for (size_t char_idx=0; nmea_payload != '\0' && char_idx < T/6; char_idx++) {
const std::bitset<6> bs_for_char = nmea_ord[ int(nmea_payload[char_idx]) ];
for (size_t offset=0; offset < 6 ; offset++) {
bits[char_idx*6 + offset] = bs_for_char[offset];
}
}
}

template<size_t T>
int ubits(const std::bitset<T> &bits, const size_t start, const size_t len)
{
assert (len <= 32);
assert (start+len <= T);
std::bitset<32> bs_tmp;
for (size_t i = 0; i < len; i++)
bs_tmp[i] = bits[start+len-i-1];
return bs_tmp.to_ulong();
}

typedef union {
long long_val;
unsigned long ulong_val;
} long_union;

template<size_t T>
int sbits(std::bitset<T> bs, const size_t start, const size_t len) {
assert (len <= 32);
assert (start+len <= T); // FIX: should it just be < ?
std::bitset<32> bs32;
if (len < 32 && 1 == bs[start] ) bs32.flip(); // pad 1's to the left if negative

for (size_t i = 0; i < len; i++)
bs32[i] = bs[start+len-i-1];

long_union val;
val.ulong_val = bs32.to_ulong();
return val.long_val;
}

extern const std::string bits_to_char_tbl;

template<size_t T>
const std::string ais_str(const std::bitset<T> &bits, const size_t start, const size_t len) {
assert (start+len < T);

const size_t num_char = len / 6;
std::string result(num_char, '@');
//cout << "str: " << T << " " << start << " " << len << " " << num_char << " " << result << endl;
for (size_t char_idx=0; char_idx < num_char; char_idx++) {
const int char_num = ubits(bits, start+char_idx*6, 6);
result[char_idx] = bits_to_char_tbl[char_num];
}
return result;
}
Loading

0 comments on commit 1531d7c

Please sign in to comment.