/* 
 * This software is licensed under terms of the GNU GENERAL PUBLIC LICENSE
 *
 * This software 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.
 *
 * This software  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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Source code is available at http://ppcenter.free.fr
 */


#ifndef SATCOM_COMMON_H
#define SATCOM_COMMON_H

#ifndef MAX
#   define MAX( x, y ) ((x) > (y) ? (x) : (y))
#endif
#ifndef MIN
#   define MIN( x, y ) ((x) < (y) ? (x) : (y))
#endif


#ifdef _BCB
#ifdef  __cplusplus
extern "C" {
#endif
#endif // _BCB


//----------------------------------------------------------------------
// Saturn debugger common stuff.
// 
//----------------------------------------------------------------------
#include "saturn/debugger/scd_common.h"



//----------------------------------------------------------------------
// Debug printf function.
// (it was used in Reed-Solomon and compression stuff for debugging)
// 
//----------------------------------------------------------------------
//#define SC_DEBUG

#ifdef SC_DEBUG
#define sc_dprintf(...) printf(__VA_ARGS__)
#else
#   ifdef _BCB
#       define sc_dprintf
#   else
#       define sc_dprintf(...) {}
#   endif
#endif



//----------------------------------------------------------------------
// Saturn Extended Communication Protocol Version (1 byte).
//  - Used in Parallel Based eXtended Comms.
//  - Used in Serial Communications (planned).
//----------------------------------------------------------------------
#define SC_VERSION 0xC0 /* eXtended Communication version 0 (FOR DEBUG) */





//----------------------------------------------------------------------
// 8/16 bits CRC routines, used for packet validation.
// 
//----------------------------------------------------------------------
unsigned char crc8_start(void);
unsigned char crc8_update(unsigned char crc, unsigned char val);

unsigned short crc16_start(void);
unsigned short crc16_update(unsigned short old_crc, unsigned char data);


//----------------------------------------------------------------------
// RS code routines.
// 
//----------------------------------------------------------------------

/* RS code + confirmation CRC (1 byte). */
#define SC_RSLEN 4

/* Call once at the program startup. */
void rs_start(void);

/* Call before computing a new RS code. (need NPAR+1 bytes in the 'rs` buffer) */
void rse_init(unsigned char* rs);
/* Call for each byte in the message to RS-code. (need NPAR+1 bytes in the 'rs` buffer) */
void rse_update(unsigned char* rs, unsigned short index, unsigned char val);



/* Call when data checksum is incorrect, in order to try to recover a missing byte. */
// Note about return value : 0 when decoding success, 1 when decoding error.
// Note about msg : input/output parameter.
// Note about recov_start and recov_end : specify the range where the erasure may be.
//  Set to {0, msg_len} for a brute force recovery (OK on PC, too slow on Saturn).
//  Set to an appropriate value on Saturn in order to shorten decoding time.
#define SC_RSD_ERASR 1 /* Typical error on PC side */
#define SC_RSD_INSRT 2 /* Typical error on Saturn side */
#define SC_RSD_NOISE 4
int rsd_execute(unsigned char* msg, unsigned short msg_len, unsigned short msg_crc, 
                unsigned char* rs, 
                unsigned short recov_start, unsigned short recov_end, 
                unsigned char recov_pattern);


//----------------------------------------------------------------------
// Compression routines.
// 
//----------------------------------------------------------------------



/**
 * Compressed data's maximum header size.
 * unsigned char version, unsigned long decompressed_length
**/
#define SC_COMP_HEADER_MAXSIZE 5

/**
 * Minmum compression size.
 * (there is no need to compress data smaller than this, since
 *  compression itself may not be efficient, also transmission
 *  overhead due to compression commands may be bigger than
 *  overhead without compression.)
**/
#define SC_COMP_MINSIZE 128
#define SC_BUFF_MAXSIZE (64*1024)

/**
 * Compress data.
 *  ind : input data.
 *  inl : input data length.
 *  outd: output data.
 * At most inl+SC_COMP_HEADER_MAXSIZE bytes are required in the output buffer.
 * Return compressed data size.
**/
unsigned long comp_exec(unsigned char* ind, unsigned long inl, unsigned char* outd);
/**
 * Decompress data.
 * It is supposed there is enough space to decompress the data.
 * Note : output data length is automatically computed from input data.
 *  ind : input data.
 *  inl : input data length.
 *  outd: output data.
 * Return decompressed data size.
**/
unsigned long decomp_exec(unsigned char* ind, unsigned long inl, unsigned char* outd);
/**
 * Get data size when decompressed.
 *  ind : input data.
 *  inl : input data length.
 * Return decompressed data size.
**/
unsigned long decomp_getsize(unsigned char* ind, unsigned long inl);



#ifdef _BCB
#ifdef  __cplusplus
}
#endif
#endif // _BCB






#endif //SATCOM_COMMON_H
