2015-12-02 05:44:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2015-12-08 23:28:33 +00:00
|
|
|
#include "ais_app.hpp"
|
2015-12-02 05:44:13 +00:00
|
|
|
|
2015-12-08 23:49:20 +00:00
|
|
|
#include "string_format.hpp"
|
|
|
|
|
2015-12-02 05:44:13 +00:00
|
|
|
#include "portapack.hpp"
|
|
|
|
using namespace portapack;
|
|
|
|
|
2015-12-04 04:34:02 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2015-12-02 17:31:14 +00:00
|
|
|
namespace ais {
|
2015-12-08 23:49:20 +00:00
|
|
|
namespace format {
|
2015-12-02 17:31:14 +00:00
|
|
|
|
2015-12-08 23:49:20 +00:00
|
|
|
static std::string latlon_normalized(const int32_t normalized) {
|
2015-12-02 17:31:14 +00:00
|
|
|
const int32_t t = (normalized * 5) / 3;
|
|
|
|
const int32_t degrees = t / (100 * 10000);
|
|
|
|
const int32_t fraction = std::abs(t) % (100 * 10000);
|
2015-12-02 21:38:17 +00:00
|
|
|
return to_string_dec_int(degrees) + "." + to_string_dec_int(fraction, 6, '0');
|
2015-12-02 17:31:14 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 23:49:20 +00:00
|
|
|
static std::string mmsi(
|
|
|
|
const baseband::ais::MMSI& mmsi
|
2015-12-04 06:09:06 +00:00
|
|
|
) {
|
|
|
|
return to_string_dec_uint(mmsi, 9, '0');
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:49:20 +00:00
|
|
|
static std::string datetime(
|
|
|
|
const baseband::ais::DateTime& datetime
|
2015-12-02 17:31:14 +00:00
|
|
|
) {
|
2015-12-02 21:38:17 +00:00
|
|
|
return to_string_dec_uint(datetime.year, 4, '0') + "/" +
|
|
|
|
to_string_dec_uint(datetime.month, 2, '0') + "/" +
|
|
|
|
to_string_dec_uint(datetime.day, 2, '0') + " " +
|
|
|
|
to_string_dec_uint(datetime.hour, 2, '0') + ":" +
|
|
|
|
to_string_dec_uint(datetime.minute, 2, '0') + ":" +
|
|
|
|
to_string_dec_uint(datetime.second, 2, '0');
|
2015-12-02 17:31:14 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 23:49:20 +00:00
|
|
|
static std::string navigational_status(const unsigned int value) {
|
2015-12-02 17:31:14 +00:00
|
|
|
switch(value) {
|
|
|
|
case 0: return "under way w/engine";
|
|
|
|
case 1: return "at anchor";
|
|
|
|
case 2: return "not under command";
|
|
|
|
case 3: return "restricted maneuv";
|
|
|
|
case 4: return "constrained draught";
|
|
|
|
case 5: return "moored";
|
|
|
|
case 6: return "aground";
|
|
|
|
case 7: return "fishing";
|
|
|
|
case 8: return "sailing";
|
|
|
|
case 9: case 10: case 13: return "reserved";
|
|
|
|
case 11: return "towing astern";
|
|
|
|
case 12: return "towing ahead/along";
|
|
|
|
case 14: return "SART/MOB/EPIRB";
|
|
|
|
case 15: return "undefined";
|
|
|
|
default: return "unexpected";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:49:20 +00:00
|
|
|
} /* namespace format */
|
2015-12-02 17:31:14 +00:00
|
|
|
} /* namespace ais */
|
|
|
|
|
2015-12-02 05:44:13 +00:00
|
|
|
AISModel::AISModel() {
|
|
|
|
receiver_model.set_baseband_configuration({
|
|
|
|
.mode = 3,
|
|
|
|
.sampling_rate = 2457600,
|
|
|
|
.decimation_factor = 4,
|
|
|
|
});
|
|
|
|
receiver_model.set_baseband_bandwidth(1750000);
|
2015-12-02 06:39:27 +00:00
|
|
|
|
|
|
|
log_file.open_for_append("ais.txt");
|
2015-12-02 05:44:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 20:16:39 +00:00
|
|
|
bool AISModel::on_packet(const baseband::ais::Packet& packet) {
|
|
|
|
// TODO: Unstuff here, not in baseband!
|
|
|
|
|
|
|
|
if( !packet.is_valid() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-02 06:39:27 +00:00
|
|
|
|
|
|
|
if( log_file.is_ready() ) {
|
2015-12-02 20:16:39 +00:00
|
|
|
std::string entry;
|
|
|
|
entry.reserve((packet.length() + 3) / 4);
|
|
|
|
|
|
|
|
for(size_t i=0; i<packet.length(); i+=4) {
|
|
|
|
const auto nibble = packet.read(i, 4);
|
|
|
|
entry += (nibble >= 10) ? ('W' + nibble) : ('0' + nibble);
|
|
|
|
}
|
|
|
|
|
2015-12-03 22:04:20 +00:00
|
|
|
log_file.write_entry(packet.received_at(), entry);
|
2015-12-02 06:39:27 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 20:16:39 +00:00
|
|
|
return true;
|
2015-12-02 05:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
void AISView::on_show() {
|
2015-12-04 04:34:02 +00:00
|
|
|
View::on_show();
|
2015-12-02 05:44:13 +00:00
|
|
|
|
|
|
|
auto& message_map = context().message_map();
|
|
|
|
message_map.register_handler(Message::ID::AISPacket,
|
|
|
|
[this](Message* const p) {
|
|
|
|
const auto message = static_cast<const AISPacketMessage*>(p);
|
2015-12-03 22:04:20 +00:00
|
|
|
rtc::RTC datetime;
|
|
|
|
rtcGetTime(&RTCD1, &datetime);
|
2015-12-08 23:15:51 +00:00
|
|
|
const baseband::ais::Packet packet { datetime, message->packet };
|
2015-12-02 20:16:39 +00:00
|
|
|
if( this->model.on_packet(packet) ) {
|
2015-12-05 22:22:35 +00:00
|
|
|
this->on_packet(packet);
|
2015-12-02 20:16:39 +00:00
|
|
|
}
|
2015-12-02 05:44:13 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AISView::on_hide() {
|
|
|
|
auto& message_map = context().message_map();
|
|
|
|
message_map.unregister_handler(Message::ID::AISPacket);
|
|
|
|
|
2015-12-04 04:34:02 +00:00
|
|
|
View::on_hide();
|
2015-12-02 05:44:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-06 03:52:12 +00:00
|
|
|
void AISView::truncate_entries() {
|
|
|
|
while(recent.size() > 64) {
|
|
|
|
recent.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:22:35 +00:00
|
|
|
void AISView::on_packet(const baseband::ais::Packet& packet) {
|
2015-12-04 04:34:02 +00:00
|
|
|
const auto source_id = packet.source_id();
|
|
|
|
auto matching_recent = std::find_if(recent.begin(), recent.end(),
|
|
|
|
[source_id](const AISView::RecentEntry& entry) { return entry.mmsi == source_id; }
|
|
|
|
);
|
|
|
|
if( matching_recent != recent.end() ) {
|
|
|
|
// Found within. Move to front of list, increment counter.
|
|
|
|
recent.push_front(*matching_recent);
|
|
|
|
recent.erase(matching_recent);
|
|
|
|
} else {
|
|
|
|
recent.emplace_front(source_id);
|
2015-12-06 03:52:12 +00:00
|
|
|
truncate_entries();
|
2015-12-04 04:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto& entry = recent.front();
|
|
|
|
entry.received_count++;
|
2015-12-02 20:16:39 +00:00
|
|
|
|
|
|
|
switch(packet.message_id()) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2015-12-04 04:34:02 +00:00
|
|
|
entry.navigational_status = packet.read(38, 4);
|
|
|
|
entry.last_position.timestamp = packet.received_at();
|
|
|
|
entry.last_position.latitude = packet.latitude(89);
|
|
|
|
entry.last_position.longitude = packet.longitude(61);
|
2015-12-02 20:16:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2015-12-04 04:34:02 +00:00
|
|
|
// packet.datetime(38)
|
|
|
|
entry.last_position.timestamp = packet.received_at();
|
|
|
|
entry.last_position.latitude = packet.latitude(107);
|
|
|
|
entry.last_position.longitude = packet.longitude(79);
|
2015-12-02 20:16:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2015-12-04 04:34:02 +00:00
|
|
|
entry.call_sign = packet.text(70, 7);
|
|
|
|
entry.name = packet.text(112, 20);
|
|
|
|
entry.destination = packet.text(302, 20);
|
2015-12-02 20:16:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 21:
|
2015-12-04 04:34:02 +00:00
|
|
|
entry.name = packet.text(43, 20);
|
|
|
|
entry.last_position.timestamp = packet.received_at();
|
|
|
|
entry.last_position.latitude = packet.latitude(192);
|
|
|
|
entry.last_position.longitude = packet.longitude(164);
|
2015-12-02 20:16:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-12-02 05:44:13 +00:00
|
|
|
}
|
2015-12-02 20:16:39 +00:00
|
|
|
|
2015-12-04 04:34:02 +00:00
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:23:53 +00:00
|
|
|
void AISView::on_focus() {
|
|
|
|
has_focus = true;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AISView::on_blur() {
|
|
|
|
has_focus = false;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:24:41 +00:00
|
|
|
bool AISView::on_encoder(const EncoderEvent event) {
|
|
|
|
advance(event);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-06 04:25:05 +00:00
|
|
|
void AISView::draw_entry(
|
2015-12-06 04:15:30 +00:00
|
|
|
const RecentEntry& entry,
|
|
|
|
const Rect& target_rect,
|
|
|
|
Painter& painter,
|
|
|
|
const Style& s
|
|
|
|
) {
|
2015-12-08 23:49:20 +00:00
|
|
|
std::string line = ais::format::mmsi(entry.mmsi) + " ";
|
2015-12-06 04:15:30 +00:00
|
|
|
if( !entry.name.empty() ) {
|
|
|
|
line += entry.name;
|
|
|
|
} else {
|
|
|
|
line += entry.call_sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
line.resize(target_rect.width() / 8, ' ');
|
2015-12-06 04:25:05 +00:00
|
|
|
painter.draw_string(target_rect.pos, s, line);
|
2015-12-06 04:15:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 04:34:02 +00:00
|
|
|
void AISView::paint(Painter& painter) {
|
|
|
|
const auto r = screen_rect();
|
|
|
|
const auto& s = style();
|
|
|
|
|
2015-12-06 04:15:30 +00:00
|
|
|
Rect target_rect { r.pos, { r.width(), s.font.line_height() }};
|
2015-12-08 22:16:36 +00:00
|
|
|
const size_t visible_item_count = r.height() / s.font.line_height();
|
2015-12-06 17:50:53 +00:00
|
|
|
|
2015-12-08 22:16:36 +00:00
|
|
|
auto selected = selected_entry();
|
|
|
|
if( selected == std::end(recent) ) {
|
|
|
|
selected = std::begin(recent);
|
|
|
|
}
|
2015-12-05 22:24:41 +00:00
|
|
|
|
2015-12-08 22:16:36 +00:00
|
|
|
RecentEntries::iterator start = selected;
|
|
|
|
RecentEntries::iterator end = selected;
|
|
|
|
size_t i = 0;
|
2015-12-04 04:34:02 +00:00
|
|
|
|
2015-12-08 22:16:36 +00:00
|
|
|
// Move start iterator toward first entry.
|
|
|
|
while( (start != std::begin(recent)) && (i < visible_item_count / 2) ) {
|
|
|
|
std::advance(start, -1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move end iterator toward last entry.
|
|
|
|
while( (end != std::end(recent)) && (i < visible_item_count) ) {
|
|
|
|
std::advance(end, 1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto p = start; p != end; p++) {
|
|
|
|
const auto& entry = *p;
|
|
|
|
const auto is_selected_key = (selected_key == entry.mmsi);
|
|
|
|
const auto& draw_style = (has_focus && is_selected_key) ? s.invert() : s;
|
|
|
|
draw_entry(entry, target_rect, painter, draw_style);
|
|
|
|
target_rect.pos.y += target_rect.height();
|
2015-12-04 04:34:02 +00:00
|
|
|
}
|
2015-12-02 05:44:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 22:24:41 +00:00
|
|
|
AISView::RecentEntries::iterator AISView::selected_entry() {
|
|
|
|
const auto key = selected_key;
|
|
|
|
return std::find_if(std::begin(recent), std::end(recent), [key](const RecentEntry& e) { return e.mmsi == key; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void AISView::advance(const int32_t amount) {
|
|
|
|
auto selected = selected_entry();
|
|
|
|
if( selected == std::end(recent) ) {
|
|
|
|
if( recent.empty() ) {
|
|
|
|
selected_key = invalid_key;
|
|
|
|
} else {
|
|
|
|
selected_key = recent.front().mmsi;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if( amount < 0 ) {
|
|
|
|
if( selected != std::begin(recent) ) {
|
|
|
|
std::advance(selected, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( amount > 0 ) {
|
|
|
|
std::advance(selected, 1);
|
|
|
|
if( selected == std::end(recent) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selected_key = selected->mmsi;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:44:13 +00:00
|
|
|
} /* namespace ui */
|