/*****************************************************************************
 * Copyright (c) 2002-2020 MBARI
 * Monterey Bay Aquarium Research Institute, all rights reserved.
 *****************************************************************************
 * @file    Idt83pParser.h
 * @authors r. henthorn
 * @date    07/21/2020
 * @brief   Delta T packet parser utility functions and data definitions
 *
 * Project: LRAUV Obstacle Avoidance
 * Summary: See @brief
 *****************************************************************************/
#ifndef IDT83PPARSER_H
#define IDT83PPARSER_H

/*****************************************************************************
 * Copyright Information:
 * Copyright 2002-2020 MBARI
 * Monterey Bay Aquarium Research Institute, all rights reserved.
 *
 * Terms of Use:
 * 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 3 of the License, or (at your option)
 * any later version. You can access the GPLv3 license at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * 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 (http://www.gnu.org/licenses/gpl-3.0.html).
 *
 * MBARI provides the documentation and software code "as is", with no
 * warranty, express or implied, as to the software, title, non-infringement
 * of third party rights, merchantability, or fitness for any particular
 * purpose, the accuracy of the code, or the performance or results which you
 * may obtain from its use. You assume the entire risk associated with use of
 * the code, and you agree to be responsible for the entire cost of repair or
 * servicing of the program with which you are using the code.
 *
 * In no event shall MBARI be liable for any damages,whether general, special,
 * incidental or consequential damages, arising out of your use of the
 * software, including, but not limited to,the loss or corruption of your data
 * or damages of any kind resulting from use of the software, any prohibited
 * use, or your inability to use the software. You agree to defend, indemnify
 * and hold harmless MBARI and its officers, directors, and employees against
 * any claim,loss,liability or expense,including attorneys' fees,resulting from
 * loss of or damage to property or the injury to or death of any person
 * arising out of the use of the software.
 *
 * The MBARI software is provided without obligation on the part of the
 * Monterey Bay Aquarium Research Institute to assist in its use, correction,
 * modification, or enhancement.
 *
 * MBARI assumes no responsibility or liability for any third party and/or
 * commercial software required for the database or applications. Licensee
 * agrees to obtain and maintain valid licenses for any additional third party
 * software required.
 *****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

/******************************
 * Headers
 ******************************/

#include <stdlib.h>
#include <stdint.h>

/******************************
 * Macros
 ******************************/

#define IDT_MAX_BEAMS     240
#define IDT_HDR_SIZE      256   // size of 83P header
#define IDT_B_BYTES         2   // bytes per beam range
#define IDT_I_BYTES         2   // bytes per beam intensity

// byte locations
#define IDT_LOC_TYPE        0   // location of 3-byte packet type (e.g., '83P')
#define IDT_LOC_INTENSE   117   // location of 1-byte intensity flag
#define IDT_LOC_NBEAMS     70   // location of 2-byte number of beams
#define IDT_LOC_RESOLUTION 85   // location of 2-byte range resolution value
#define IDT_LOC_INTERVAL   91   // location of 2-byte ping interval
#define IDT_LOC_PINGNUM    95   // location of 2-byte ping number
#define IDT_LOC_DATE        8   // location of 12-byte interrogation date ("07-JUL-2020")
#define IDT_LOC_TIME       20   // location of 9-byte interrogation time ("15:41:52")
#define IDT_LOC_MSEC      112   // location of 5-byte interrogation msec (".999")
#define IDT_LOC_INTENSITY 117   // location of flag indicating intensity values
#define IDT_LOC_ALTITUDE  133   // location of 4-byte float altitude
#define IDT_NUM_MONTHS     12

// valid codes
#define IDT_83P         1   // valid value when valid 83P without intensities
#define IDT_83P_INTENSE 2   // valid value when valid 83P with intensities
#define IDT_NOT_83P     0   // valid value when packet is not an 83P packet
#define IDT_SIZE_ERR   -1   // valid value when packetSize is invalid
#define IDT_OTHER_ERR  -2   // valid value when some other error occurred

// Packet data is parsed into this structure for use in applications
typedef struct sIdt83pData
{
    int8_t   valid;                  // > 0 means valid ping
    double   timestamp;              // local system time received epoch seconds
    double   pingtime;               // time of sonar ping epoch seconds
    uint32_t pingnumber;             // sonar ping number
    float    altitude;               // altitude calculated by sonar
    uint16_t interval;               // interval between pings milliseconds
    uint16_t nbeams;                 // number of beams in the packet
    float    range[IDT_MAX_BEAMS];   // beam ranges meters
    uint16_t intense[IDT_MAX_BEAMS]; // beam intensities
}
Idt83pData;

// IDT 83P Packet parser
// Parameters:
//   p83pData     - pointer to users Idt83pData object
//   pIdtPacket   - pointer to packet read from 83P source (e.g., beamformer)
//   packetSize   - number of bytes read from 83P source (<=256+4*IDT_MAX_BEAMS)
// Returns:
//   Value > 0 when packet is parsed successfully.
//   Value <=0 otherwise. See valid codes above
int8_t parseIdt83p(Idt83pData* p83pData,
                   const char *pIdtPacket, int16_t packetSize);


#ifdef __cplusplus
}
#endif

#endif
