/****************************************************************************/
/* Copyright (c) 2014 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Interface to the Cathx M12 camera.                            */
/* Filename : CathxM12.h                                                    */
/* Author   : Henthorn                                                      */
/* Project  : Iceberg AUV                                                   */
/* Version  : 1.0                                                           */
/* Created  : 05/13/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _CATHXM12_H
#define _CATHXM12_H

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <curl.h>
#include "ourTypes.h";
#include "PeriodicTask.h"
#include "NavigationIF.h"

#define STATE_SIZE 20
#define IMGSV_SIZE 10
#define BUF_SIZE 1024

class CathxM12Msg;
class CathxM12Log;

// Simple libcurl interface for use with the M12 camera from Cathx.
//
class CathxM12 : public PeriodicTask
{
   friend CathxM12Log;

public:
   // Create an interface to the server. The connection is only opened
   // when posting and getting.
   //
   CathxM12(const char* name, const char* server_ip,
            unsigned long port, int period = 1000);
   ~CathxM12();

   enum CAPTURE_STATE {
      UNDEFINED = -1,
      IDLE = 0,
      ACQUIRE = 1,
      OTHER
   };

protected:

   // Called periodically to get messages from server
   //
   void m12_callback(void);
   int initialize_sockets();

   /* Interface to the Cathx web server */
   CURL *_curl;
   static char   _response[];
   static size_t _response_size;
   static size_t read_curl_response(void *buffer, size_t size,
                                    size_t nmemb, void *userp);
   int curl_setup();
   int curl_cleanup();
   int curl_execute();

   int capture(char* True_or_False);     // "True" = no, "False" = off
   int profile(int profile_number);      // Set the active profile
   int status();                         // Requests camera status message
   void send_nav_data();                 // Sends vehicle position to camera
   int cmd_and_response(const char* cmd);// Sends the cmd, waits for response
   int parse_status(const char* msg);    // Parse status message from the Cathx

   // Here are the interested values
   //
   char _state[STATE_SIZE+1];
   char _img_saving[IMGSV_SIZE+1];
   int _total, _session;
   long _available_space;
   int _profile;
   int _capture_state, _saving;
   int _unresponsive;

   /* data and such */

   CathxM12Msg    *_msgQ;
   CathxM12Log    *_log;

   NavigationIF   *_navif;

   char _buf[BUF_SIZE+1];
   char _errorbuf[CURL_ERROR_SIZE];

   Boolean _recording;

   char* _name;
   char* _server_ip;
   unsigned long _port;
   int _period;

   /* UDP comms */
   int _sockfd;
   struct sockaddr_in _server_addr;

   /* TCP comms */
   int _tcpfd;
   struct sockaddr_in _tcp_addr;

   Boolean _errmsg;
};

#endif
