/*
 * Copyright (C) 2013 Marc Schwarzbach
 *               2015 Felix Ruess <felix.ruess@gmail.com>
 *
 * This file is part of paparazzi.
 *
 * paparazzi 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.
 *
 * paparazzi 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 paparazzi; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
/**
 * @file sbs_parser.h
 * Parser for the SBS-1 protocol (ADS-B data).
 *
 *
 * ## Message Types
 *   // Generated when the user changes the selected aircraft in
 *   // BaseStation.
 *   SELECTION_CHANGE: 'SEL',
 *   // Generated when an aircraft being tracked sets or changes its
 *   // callsign.
 *   NEW_ID: 'ID',
 *   // Generated when the SBS picks up a signal for an aircraft that it
 *   // isn't currently tracking,
 *   NEW_AIRCRAFT: 'AIR',
 *   // Generated when an aircraft's status changes according to the
 *   // time-out values in the SBS1 Data Settings menu.
 *   STATUS_CHANGE: 'STA',
 *   // Generated when the user double-clicks (or presses return) on an
 *   // aircraft (i.e. to bring up the aircraft details window).
 *   CLICK: 'CLK',
 *   // Generated by the aircraft. There are eight different MSG
 *   // transmission types, see `TransmissionType`.
 *   TRANSMISSION: 'MSG'
 *
 * ## Transmission Types
 *
 * Transmission messages (MSG) from aircraft may be one of eight types
 * (ES = Extended Squitter, DF = Downlink Format, BDS = B-Definition
 * Subfield).
 *
 * |Type|Description                     |Spec          |
 * |----|--------------------------------|--------------|
 * | 1  | ES identification and category | DF17 BDS 0,8 |
 * | 2  | ES surface position message    | DF17 BDS 0,6 |
 * | 3  | ES airborne position message   | DF17 BDS 0,5 |
 * | 4  | ES airborne velocity message   | DF17 BDS 0,9 |
 * | 5  | Surveillance alt message       | DF4, DF20    |
 * | 6  | Surveillance ID message        | DF5, DF21    |
 * | 7  | Air-to-air message             | DF16         |
 * | 8  | All call reply                 | DF11         |
 *
 */

#ifndef SBS_PARSER_H_
#define SBS_PARSER_H_

#include "math/pprz_geodetic_double.h"

#include <sys/time.h>

#define SBS_INPUT_MAXLEN 255
#define INTRUDER_NAME_MAXLEN 20

struct SbsMsgData {
  int msg_available;
  int pos_available;
  int nb_ovrn;        // number if incomplete messages
  char msg_buf[SBS_INPUT_MAXLEN];  ///< buffer for storing one line
  int msg_len;
};

/// data structure for intruders
struct Intruder {
  int id;
  int flight_id;
  char name[INTRUDER_NAME_MAXLEN];
  struct LlaCoor_d lla;
  int lastalt;
  float gspeed;
  float course;
  float climb;
  struct timeval time4;
  struct timeval time3;
  int used;
};

#define MAX_INTRUDERS 20
extern struct Intruder intruders[MAX_INTRUDERS];

void sbs_parse_msg(struct SbsMsgData *data);
void sbs_parse_char(struct SbsMsgData *data, unsigned char c);

void update_intruders(struct Intruder *intr);


#endif /* SBS_PARSER_H_ */
