/* VectorNav HSI Calibration Library v1.0.0.2
 *
 * Copyright (c) 2018 VectorNav Technologies, LLC
 *
 * This source code is proprietary to and copyrighted by VectorNav Technologies, LLC and is
 * available solely under license from VectorNav. All rights reserved. If you do not have a
 * license to use this source code from VectorNav, any use hereof is strictly prohibited by
 * U.S. law and other applicable law. This source code is restricted for use solely with the
 * products of VectorNav and as otherwise set forth in any agreement with VectorNav. Any
 * disclosure of this source code to the public or to any third party is also prohibited.
 */
/** \file
* {COMMON_HEADER}
*
* \section Description
* This header file contains declarations for using VectorNav HSI file I/O or the VN sensors utilities.
*/
#ifndef VNHSIEXAMPLEUTIL_H_INCLUDED
#define VNHSIEXAMPLEUTIL_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "vectornav.h"
#include "vnint.h"
#include "vnsensor.h"
#include "vnezasyncdata.h"
#include "vncompositedata.h"
#include "vnserialport.h"
#include "vntime.h"
#include "vnupack.h"
#include "vnupackf.h"
#include "vnamath.h"

#include "vnhsi.h"

/** \brief Structure representing the HSI utility configuration
 *
 * The VnHsiUtilConfig structure defines the input, output and configuration file names. 
 * It also define the configuration parameters for the connected VN sensor in the live connection mode. */
typedef struct VnHsiUtilConfig {
	char *measFilename;			/**< The measFilename field. The name of the input file with the Raw data file captured from a VectorNav sensor. */
	char *configFilename;		/**< The configFilename field. The name of the file that defines the configuration parameters. */
	char *outputFilename;		/**< The outputFilename field. The name of the output file that stores the HSI results. */
	char *comPort;				/**< The comPort field. The comPort of the connected VN sensor. */
	unsigned int baudRate;		/**< The baudRate field. The baudRate of the connected VN sensor. */
	bool liveFlag;				/**< The liveFlag field. The live connection flag, the YMR measurement will be read directly from the sensor. */
	bool seqFlag;				/**< The seqFlag field. Computes HSI solution after every measurement added to kept-measurement buffer. */
	bool helpFlag;				/**< The helpFlag field. Prints an HSI example syntax to screen. */
} VnHsiUtilConfig;


/** \brief Load in the configuration parameters from a configuration file into the VnHsiConfig structure.
*
* \param[in] filename The configuration file name.
* \param[in/out] buf Buffer to load the configuration into.
* \return Error code. */
enum VnError vnhsi_util_load_config(const char* filename, VnHsiConfig*);


/** \brief Save the HSI results to an output file.
*
* \param[in] uc The utility configuration structure.
* \param[in] meas  The kept YMR measurement.
* \param[in] numMeas The number of kept measurements.
* \param[in] si_m3 The final soft iron matrix.
* \param[in] hi_v3 The final hard iron vector.
* \param[in] sphereResiduals The sphere residuals.
* \param[in] vertResiduals The vertical (alignment) residuals.
* \param[in] fom  The figures of merit.
* \param[in] fomEvalText The FOM final evaluation tags.
* \return None. */
void vnhsi_util_save_results(VnHsiUtilConfig uc, VnHsiYmr* meas, size_t numMeas, union vn_mat3f si, union vn_vec3f hi, float sphereResiduals[], float vertResiduals[], VnHsiFom fom, unsigned int fomScore[8]);


/** \brief Initialize and connect to the sensor with the configuration parameters.
*
* \param[in] vs The VnSensor structure for working with VectorNav sensors.
* \param[in] comPort The comPort of the connected VN sensor.
* \param[in] baudRate The baudRate of the connected VN sensor.
* \param[in] c The configuration structure.
* \return Error code. */
enum VnError vnhsi_util_sensor_init(struct VnSensor* vs, char* comPort, unsigned int baudRate, VnHsiConfig* config);


/** \brief Finalize, write the final HSI and disconnect from the sensor.
*
* \param[in] vs The VnSensor structure for working with VectorNav sensors.
* \param[in] si_m3 The final soft iron matrix.
* \param[in] hi_v3 The final hard iron vector.
* \return Error code. */
enum VnError vnhsi_util_sensor_finalize(struct VnSensor* vs, union vn_mat3f si, union vn_vec3f hi);


/** \brief Process the received error messages and prints it to the screen.
*
* \param[in] errorMessage The error message text.
* \param[in] errorCode The VN Error code.
* \return Error code. */
enum VnError vnhsi_util_print_error(char* errorMessage, enum VnError errorCode);


/** \brief Print the final FOM to the output file.
*
* \param[in] fp The output file name pointer.
* \param[in] FOM  The figures of merit structure.
* \param[in] Evalind The FOM evaluation index array.
* \return None. */
void vnhsi_util_print_fom(FILE* fp, VnHsiFom fom, unsigned int fomScore[8]);

/** \brief Print the final HSI to the output file.
*
* \param[in] fp The output file name pointer.
* \param[in] si_m3 The final soft iron matrix.
* \param[in] hi_v3 The final hard iron vector.
* \return None. */
void vnhsi_util_print_hsi(FILE* fp, union vn_mat3f si, union vn_vec3f hi);


/** \brief Collect the YMR measurements either from the sensor in the live connection mode or from an input file in the post processing mode.
*
* \param[in] vs The VnSensor structure for working with VectorNav sensors.
* \param[in] c The configuration structure.
* \param[in] uc The utility configuration structure.
* \param[in/out] numKept The number of kept measurements.
* \param[in/out] kept  The kept YMR measurement.
* \return Error code. */
enum VnError vnhsi_util_collect_meas(struct VnSensor* vs, VnHsiConfig* c, VnHsiUtilConfig* uc, VnHsiYmr* kept, unsigned int* numKept);


/** \brief Process the arguments provided by the command line.
*
* \param[in] argc The command line arguments count.
* \param[in] argv A pointer to the command line arguments array.
* \param[in/out] uc The utility configuration structure.
* \return Error code. */
enum VnError vnhsi_util_main_options(int argc, char *argv[], VnHsiUtilConfig* uc);

#ifdef __cplusplus
}
#endif

#endif
