// Copyright (C) 2007, 2009, 2010, 2012, 2015Yaxin Liu, Patrick Beeson, Austin Robot Technology, Jack O'Quin
// All rights reserved.
//
// Software License Agreement (BSD License 2.0)
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above
//    copyright notice, this list of conditions and the following
//    disclaimer in the documentation and/or other materials provided
//    with the distribution.
//  * Neither the name of {copyright_holder} nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

/** @file
 *
 *  Velodyne 3D LIDAR data input classes
 *
 *    These classes provide raw Velodyne LIDAR input packets from
 *    either a live socket interface or a previously-saved PCAP dump
 *    file.
 *
 *  Classes:
 *
 *     velodyne::Input -- base class for accessing the data
 *                      independently of its source
 *
 *     velodyne::InputSocket -- derived class reads live data from the
 *                      device via a UDP socket
 *
 *     velodyne::InputPCAP -- derived class provides a similar interface
 *                      from a PCAP dump file
 */

#ifndef VELODYNE_DRIVER_INPUT_H
#define VELODYNE_DRIVER_INPUT_H

#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string>
#include <vector>

#include <glog/logging.h>
// #include <Eigen/Core>

const int HDL_NUM_ROT_ANGLES = 36001;
const int HDL_LASER_PER_FIRING = 32;
const int VLP_LASER_PER_FIRING = 16;
const int HDL_MAX_NUM_LASERS = 64;
const int HDL_FIRING_PER_PKT = 12;
const int PACKET_SIZE = 1206;

// #pragma pack(push, 1)

// struct HDLLaserReturn
// {
//   unsigned short distance;
//   unsigned char intensity;
// };

// struct HDLFiringData
// {
//   unsigned short blockIdentifier;
//   unsigned short rotationalPosition;
//   HDLLaserReturn laserReturns[HDL_LASER_PER_FIRING];
// };

// enum HDLBlock
// {
//   BLOCK_0_TO_31 = 0xeeff,
//   BLOCK_32_TO_63 = 0xddff
// };

// struct HDLDataPacket
// {
//   HDLFiringData firingData[HDL_FIRING_PER_PKT];
//   unsigned int gpsTimestamp;
//   unsigned char blank1;
//   unsigned char blank2;
// };

// #pragma pack(pop)

namespace velodyne_driver
{

static uint16_t DATA_PORT_NUMBER = 2368;      // default data port

/** @brief Live Velodyne input from socket. */
class InputSocket
{
public:
  /** @brief constructor
   *
   *  @param ip desired ip for lidar, input empty string to listen any data from assigned udp port.
   *  @param port UDP port number
   */
  InputSocket(){}
  ~InputSocket(){}

  bool init(std::string ip, uint16_t port = DATA_PORT_NUMBER);

  void uninit();

  // return 0 success, -1 error
  int getPacket(unsigned char* pkt);

private:
  uint16_t m_port;
  std::string m_devip_str;

  int m_sockfd;
  in_addr m_devip;
};

}  // namespace velodyne_driver

#endif  // VELODYNE_DRIVER_INPUT_H
