// Copyright 2015 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // TODO(schwehr): Add logging back in or remove it. #include "decode_body.h" #include #include "ais.h" using libais::AisMsg; using std::unique_ptr; namespace libais { // TODO(schwehr): Switch to make_unique when C++14 is available on Travis-CI. template std::unique_ptr MakeUnique(Args &&... args) { return std::unique_ptr(new T(std::forward(args)...)); } template std::unique_ptr MakeNull() { return std::unique_ptr(nullptr); } unique_ptr CreateAisMsg6(const string &body, const int fill_bits) { libais::Ais6 msg(body.c_str(), fill_bits); switch (msg.dac) { // International Maritime Organization (IMO). case libais::AIS_DAC_1_INTERNATIONAL: switch (msg.fi) { case 0: return MakeUnique(body.c_str(), fill_bits); case 1: return MakeUnique(body.c_str(), fill_bits); case 2: return MakeUnique(body.c_str(), fill_bits); case 3: return MakeUnique(body.c_str(), fill_bits); case 4: return MakeUnique(body.c_str(), fill_bits); case 12: return MakeUnique(body.c_str(), fill_bits); case 14: return MakeUnique(body.c_str(), fill_bits); case 18: return MakeUnique(body.c_str(), fill_bits); case 20: return MakeUnique(body.c_str(), fill_bits); case 25: return MakeUnique(body.c_str(), fill_bits); // TODO(schwehr): 28. // TODO(schwehr): 30. case 32: return MakeUnique(body.c_str(), fill_bits); case 40: return MakeUnique(body.c_str(), fill_bits); } // FI not handled. break; } return nullptr; } unique_ptr CreateAisMsg8(const string &body, const int fill_bits) { libais::Ais8 msg(body.c_str(), fill_bits); switch (msg.dac) { // International Maritime Organization (IMO). case libais::AIS_DAC_1_INTERNATIONAL: switch (msg.fi) { case 0: return MakeUnique(body.c_str(), fill_bits); case 11: return MakeUnique(body.c_str(), fill_bits); case 13: return MakeUnique(body.c_str(), fill_bits); case 15: return MakeUnique(body.c_str(), fill_bits); case 16: return MakeUnique(body.c_str(), fill_bits); case 17: return MakeUnique(body.c_str(), fill_bits); case 19: return MakeUnique(body.c_str(), fill_bits); case 21: return MakeUnique(body.c_str(), fill_bits); case 22: return MakeUnique(body.c_str(), fill_bits); case 24: return MakeUnique(body.c_str(), fill_bits); case 26: return MakeUnique(body.c_str(), fill_bits); case 27: return MakeUnique(body.c_str(), fill_bits); case 29: return MakeUnique(body.c_str(), fill_bits); case 31: return MakeUnique(body.c_str(), fill_bits); } // FI not handled. break; // European River Information System (RIS). case libais::AIS_DAC_200_RIS: switch (msg.fi) { case 10: // Inland ship static and voyage related data return MakeUnique(body.c_str(), fill_bits); case 21: // ETA at lock/bridge/terminal return MakeUnique(body.c_str(), fill_bits); case 22: // RTA at lock/bridge/terminal return MakeUnique(body.c_str(), fill_bits); case 23: // EMMA warning return MakeUnique(body.c_str(), fill_bits); case 24: // Water levels return MakeUnique(body.c_str(), fill_bits); case 40: // Signal status return MakeUnique(body.c_str(), fill_bits); case 55: // Number of persons on board return MakeUnique(body.c_str(), fill_bits); } // FI not handled. break; // TODO(schwehr): 366 US Coast Guard. case 367: // US Coast Guard. switch (msg.fi) { case 22: return MakeUnique(body.c_str(), fill_bits); } // FI not handled. break; } return nullptr; } unique_ptr CreateAisMsg(const string &body, const int fill_bits) { if (body.empty()) { return nullptr; } if (fill_bits < 0 || fill_bits > 5) { return nullptr; } switch (body[0]) { case '1': // FALLTHROUGH case '2': // FALLTHROUGH case '3': // 1-3: Class A position report. return MakeUnique(body.c_str(), fill_bits); case '4': // FALLTHROUGH - 4 - Basestation report case ';': // 11 - UTC date response //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case '5': // 5 - Ship and Cargo return MakeUnique(body.c_str(), fill_bits); //return MakeNull(); case '6': // 6 - Addressed binary message //return CreateAisMsg6(body, fill_bits); return MakeNull(); case '7': // FALLTHROUGH - 7 - ACK for addressed binary message case '=': // 13 - ASRM Ack (safety message) //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case '8': // 8 - Binary broadcast message (BBM) //return CreateAisMsg8(body, fill_bits); return MakeNull(); case '9': // 9 - SAR Position //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case ':': // 10 - UTC Query //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); // ';' 11 - See 4 case '<': // 12 - Addressed Safety Related Messages (ASRM) //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); // '=' 13 - See 7 case '>': // 14 - Safety Related Broadcast Message (SRBM) //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case '?': // 15 - Interrogation //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case '@': // 16 - Assigned mode command //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'A': // 17 - GNSS broadcast //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'B': // 18 - Position, Class B return MakeUnique(body.c_str(), fill_bits); case 'C': // 19 - Position and ship, Class B //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'D': // 20 - Data link management //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'E': // 21 - Aids to navigation report return MakeUnique(body.c_str(), fill_bits); //return MakeNull(); case 'F': // 22 - Channel Management //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'G': // 23 - Group Assignment Command //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'H': // 24 - Static data report return MakeUnique(body.c_str(), fill_bits); case 'I': // 25 - Single slot binary message //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'J': // 26 - Multi slot binary message with comm state //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); case 'K': // 27 - Long-range AIS broadcast message //return MakeUnique(body.c_str(), fill_bits); return MakeNull(); default: return nullptr; } return nullptr; } } // namespace libais