/*
 * serial.h
 *
 *  Created on: Nov 30, 2022
 *      Author: thom maughan
 */

#include <stdint.h>

#ifndef SERIAL_H_
#define SERIAL_H_


#define M2M_RX_PREAMBLE_HACK    'U'
#define M2M_RX_PREAMBLE_TXT     '<'     // was U
#define M2M_RX_PREAMBLE         0xAA

#define M2M_DELIM_MS_TIMEOUT        250         // CF2 / MSC1 timeout is tbd (need to look at ljc code)
#define H2M_DELIM_MS_TIMEOUT        1000

// sensor type, as defined by sys_sensor_mode
#define UNSELECTED_MODE         0
#define ISUS_SENSOR_MODE        1
#define DURA_SENSOR_MODE        2

#define CTRL_A                  0x01
#define CTRL_C                  0x03
#define CTRL_D                  0x04

#define TABS                    (-273.15)

#define ERRBUFSIZE              256

// safegets()
#define RECL                    30
#define MAXINBUFF               RECL+14
#define MAXREAD                 MAXINBUFF-2

// SyncNextSample()
#define SYNC_FROM_CURRENT_TIME  1
#define SYNC_FROM_LAST_SAMPLE   2

// OpenApfLogFile()
#define ISUS_DATA_FILE          1
#define DURA_DATA_FILE          2

// SendRecords() and CalcRecsPending()
#define DATSPOOL_FILE           0
#define ERRORLOG_FILE           1

#define FNULL                   ((FILE *)NULL)

void m2m_statemachine_init(void);
int handle_m2m_comm(void);
int parse_rx0_buf(void);


// handle_m2m_comm returns 0 for no rxByte, 1 for rxByte, and CMD_XXX if command is executed
// start numbering CMD_xxx at 3
#define CMD_TAKESAMPLE          3       //Takesample
#define CMD_STARTSAMPLE         4       // FRAM
#define CMD_SENDLAST            5       // FRAM
#define CMD_GETCAL              6       //
#define CMD_EXPORTCAL           7       //
#define CMD_GETHEADER           8       //
#define CMD_GETMETADATA         9       //
#define CMD_SLEEP               10      //
#define CMD_GETTIME             11      //CFG_TIMESTAMP
#define CMD_SETTIME             12      //
#define CMD_HELP                13      //
#define CMD_GETPERIOD           14      //
#define CMD_SETPERIOD           15      //
#define CMD_GETDATAFIELD        16
#define CMD_SETDATAFIELD        17
#define CMD_GETFILENAME         18      //
#define CMD_SETFILENAME         19      //
#define CMD_GETALIGN            20
#define CMD_SETALIGN            21
#define CMD_GETCFG_SAMPNUM      22      //
#define CMD_SETCFG_SAMPNUM      23      //
#define CMD_GETCFG_TIMESTAMP    24      //
#define CMD_SETCFG_TIMESTAMP    25      //
#define CMD_GETCFG_BIASNEG      26
#define CMD_SETCFG_BIASNEG      27      //CFG_BIAS_BAT_NEG
#define CMD_TEST_BIASNEG        28
#define CMD_GETCFG_BIASPOS      29      //CFG_BIAS_BAT_POS  biaspos
#define CMD_SETCFG_BIASPOS      30
#define CMD_TEST_BIASPOS        31
#define CMD_GETCFG_IB           32      //
#define CMD_SETCFG_IB           33      //
#define CMD_TEST_IB             34
#define CMD_GETCFG_IK           35      //
#define CMD_SETCFG_IK           36      //
#define CMD_TEST_IK             37
#define CMD_GETCFG_SPARE        38      // NOT
#define CMD_SETCFG_SPARE        39      // NOT
#define CMD_GETCFG_VK           40      // CFG_V_COUNTER_ELECT
#define CMD_SETCFG_VK           41      //
#define CMD_TEST_VK             42
#define CMD_GETCFG_VK_STD       43      //
#define CMD_SETCFG_VK_STD       44      //
#define CMD_GETCFG_VTHERM       45      //
#define CMD_SETCFG_VTHERM       46      //
#define CMD_TEST_VTHERM         47      // CFG_V_THERMISTOR
#define CMD_GETCFG_VTHERM_STD   48
#define CMD_SETCFG_VTHERM_STD   49
#define CMD_GETCFG_TEMPC        50      // CFG_CALC_TEMP
#define CMD_SETCFG_TEMPC        51
#define CMD_TEST_TEMPC          52
#define CMD_GETCFG_VRSI         53      // CFG_VRSI
#define CMD_SETCFG_VRSI         54      //
#define CMD_TEST_VRSI           55
#define CMD_GETCFG_VRSI_STD     56      //
#define CMD_SETCFG_VRSI_STD     57      //
#define CMD_GETCFG_PHINT        58      // CFG_CALC_PH_VRSI
#define CMD_SETCFG_PHINT        59
#define CMD_TEST_PHINT          60
#define CMD_GETCFG_VRSE         61      //
#define CMD_SETCFG_VRSE         62      //
#define CMD_TEST_VRSE           63
#define CMD_GETCFG_VRSE_STD     64      //
#define CMD_SETCFG_VRSE_STD     65      //
#define CMD_GETCFG_PHEXT        66
#define CMD_SETCFG_PHEXT        67
#define CMD_TEST_PHEXT          68
#define CMD_GETCFG_VBAT         69      // CFG_VIN_BAT_VOLT
#define CMD_SETCFG_VBAT         70      //
#define CMD_TEST_VBAT           71
#define CMD_GETCFG_BRDTEMP      72      // CFG_BRD_TEMP
#define CMD_SETCFG_BRDTEMP      73
#define CMD_GETCFG_HUMID        74      // CFG_BRD_HUMIDITY
#define CMD_SETCFG_HUMID        75
#define CMD_TEST_ENV            76
#define CMD_HELPCONFIG          77
#define CMD_HELPTEST            78
#define CMD_HELPDEPLOY          79
#define CMD_GETVERSION          80
#define CMD_GETCFG_CONFIG       81
#define CMD_SETCFG_CONFIG       82
#define CMD_MAINMENU            83
#define CMD_GETSTORE            84      // MICROSD Enable
#define CMD_SETSTORE            85
#define CMD_FILESYSTEM          86      // FatFS
#define CMD_TEST_VRSI_STD       87
#define CMD_TEST_VRSE_STD       88
#define CMD_TEST_TIMESTAMP      89
#define CMD_TEST_SAMPNUM        90
#define CMD_TEST_VK_STD         91
#define CMD_TEST_VTHERM_STD     92
#define CMD_SAMP_DATABARF       93

#define CMD_APF_BAKE             100      //
#define CMD_APF_CFG              101      //
#define CMD_APF_CAL              102      //
#define CMD_APF_SET_CTD          103      //
#define CMD_APF_GET_CTD          104      //
#define CMD_APF_GET_ERR          105      // ERR print errors
#define CMD_APF_SET_FIT          106      //
#define CMD_APF_GET_FIT          107      //
#define CMD_APF_INIT             108      //
#define CMD_APF_SET_MODE         109      //
#define CMD_APF_GET_MODE         110      //
#define CMD_APF_OFF              111      // Sleep
#define CMD_APF_GET_PC           112      // PC Print Counters
#define CMD_APF_GET_QQ           113      // Sensor type (ISUS, DURAFET)  would like to deprecate this
#define CMD_APF_SET_RTC          114      //
#define CMD_APF_GET_RTC          115      //
#define CMD_APF_GET_SC           116      // S  SC Sample Counters
#define CMD_APF_XFER_SE          117      // Transfer error log
#define CMD_APF_GET_SE           118      // Calc Recs pending
#define CMD_APF_SLP              119      // Flush RX buf, then suspend (sleep)
#define CMD_APF_SET_SPEC         120      //
#define CMD_APF_GET_SPEC         121      //
#define CMD_APF_GET_TA           122      // TA Time of sample Acq
#define CMD_APF_SET_TINT         123      // TINT Set integration time
#define CMD_APF_GET_TINT         124      // TINT,? Get integration time
#define CMD_APF_PING             125      //d WW
#define CMD_APF_ISUS_T1          126      // Take ISUS sample (like ts)
#define CMD_APF_DURA_T2          127      // Take Dura pH sample
#define CMD_APF_CIH              128      // Isus Header
#define CMD_APF_CDH              129      // Durafet header

//#define CMD_APF_SL                          // Send Last (already have a send last)
//#define CMD_APF_TS                        // Take Sample (already have a take sample)

#define CMD_APF_H2M              130      //  changes timing and command termination, try for measuring the character timing.
#define CMD_APF_M2M              131      //

#define CMD_THOMZ_TEST           132




// APF TODO: all CF2 globals, change to apf_ and then make a struct for persistence


#ifdef APFTODO    //def this as uint32_t in main.c

int32_t    apf_errno    = 0;       //was errno
uint32_t   apf_isus_spec_pwr = 0;
uint32_t   apf_isus_lamp_pwr = 0;
uint32_t   apf_isus_ref_pwr = 0;
uint32_t   apf_dura_atod_pwr = 0;
uint32_t   apf_sys_sensor_mode = 0;   //ISUS_SENSOR_MODE or DURA_SENSOR_MODE
uint32_t   apf_sys_por_ctr = 0;
uint32_t   apf_sys_reset_ctr = 0;
uint32_t   apf_sys_reset_time = 0;
uint32_t   apf_isus_error_time = 0;
uint32_t   apf_dura_error_time = 0;
uint32_t   apf_isus_sample_time = 0;
uint32_t   apf_dura_sample_time = 0;
uint32_t   apf_gotSample = 0;      // flag for indicating sample taken
uint32_t   apf_acquire_time = 0;   // time to acquire sample
uint32_t   apf_dura_error_ctr = 0;
uint32_t   apf_dura_error_time = 0;
uint32_t   apf_acquire_time = 0;


//ucharsapf_sys_slog_delay
uint8_t  apf_sys_deploy_mode
uint8_t  apf_sys_data_mode
uint8_t  apf_sys_time_type
uint8_t  apf_sys_watchdog_enable
uint8_t  apf_sys_message_enable
uint8_t  apf_sys_pump_enable
uint8_t  apf_sys_sensor_type

//ushorts
uint16_t apf_sys_delay_hour
uint16_t apf_sys_sync_hour
uint16_t apf_sys_sync_min
uint16_t apf_sys_df_limit
uint16_t apf_sys_sf_limit
uint16_t apf_sys_ef_limit
uint16_t apf_sys_last_doy
uint16_t apf_sys_serial_num
uint16_t apf_sys_apf_tmout
uint16_t apf_sys_tzoffset
uint16_t apf_sys_last_error
uint16_t apf_sys_error_ctr
uint16_t apf_sys_error_time
uint16_t apf_sys_reset_ctr
uint16_t apf_sys_reset_time
uint16_t apf_sys_por_ctr
uint16_t apf_sys_file_ctr
uint16_t apf_sys_sleep_period
uint16_t apf_sys_pump_period
#endif

#define MAX_SIZE_RX0_BUF    64
struct rxStateMach
{
    unsigned int  state;
    unsigned int  indx;
    unsigned int  cnt;
    unsigned int  cr_cnt;
    unsigned int  cmd;
    unsigned long tick_ms_start;      // for timeout
    unsigned long tick_ms_now;
    unsigned long tick_ms_delta;      // not really needed, but useful for dev
    unsigned long tick_ms_delim;        // time based delimiter

    uint8_t     byte;
    uint8_t     buf[MAX_SIZE_RX0_BUF];
};


#endif /* SERIAL_H_ */
