/*==================================================================================================

FILE INFORMATION HEADER

USED IN PROJECT:			S1003065-01 PPS-TX/S1003067-01 PPS-RX software application
ITEM TYPE:					Source Code
ITEM NAME:					xlru_common_func.cpp
ITEM DESCRIPTION:			This file contains the implementations for convertions functions between char* to int/float/uint/etc
TARGET:						ATSAM4LC8B / ATSAM4LS8B
DOC REFERENCES:				SWDS1003065-01/SWDS1003065-01
PROGRAMMER NAME(S):			A.Chessa, L.Vallongo
COPYRIGHT:					LEONARDO-FINMECCANICA S.p.A
REVISION HISTORY:
- 01.00: first release

====================================================================================================
© Copyright LEONARDO S.p.A.. All rights reserved
Any right of industrial and intellectual property on this document,
and of technical Know-how herein contained, belongs to
LEONARDO S.p.A. and/or third parties.
According to the law, it is forbidden to disclose, reproduce or however
use this document and any data herein contained for any use without
previous written authorization by LEONARDO S.p.A.
==================================================================================================*/
#include "xlru_common_func.h"
#include "string.h"
#include <stdlib.h>

#define DEBUG

uint16_t xlru::check_sum_calculus(const char *pstring, short bytestoshift, short bytetocheck)
{
	int i;
	uint16_t res;
    unsigned char* p=(unsigned char*)pstring; //to be sure to use unsigned arithmetic
	i = bytestoshift;
	unsigned char tmp = p[i]; //*(pstring+i);
	for (i=bytestoshift+1; i < (bytestoshift+bytetocheck); i++)
		tmp += p[i]; //*(pstring+i);
	//tmp ^= *(pstring+i);

	res = tmp;

	return (res);
}

void xlru::conv_decimale_to_hex(char s[], uint32_t num_temp,  uint8_t nrbytes)
{
	int cont=0;
	int rest;

	while(num_temp>0){
		rest = num_temp & 0x0F; //%16;
		num_temp=num_temp>>4;
		s[cont]="0123456789ABCDEF"[rest];
		++cont;
#if 0
		switch(rest){
			case 10:
			s[cont]='A';
			cont++;
			break;
			case 11:
			s[cont]='B';
			cont++;
			break;
			case 12:
			s[cont]='C';
			cont++;
			break;
			case 13:
			s[cont]='D';
			cont++;
			break;
			case 14:
			s[cont]='E';
			cont++;
			break;
			case 15:
			s[cont]= 'F';
			cont++;
			break;
			default:
			s[cont]='0'+rest;
			cont++;
		}
		num_temp/=16;
#endif
	}
	while(cont<nrbytes)
	{
		s[cont]='0';
		cont++;
	}
}

/* reverse:  reverse string s in place */
void xlru::reverse(char s[], int nrchar)
{
	int i;
	char c;

	for (i= 0; i<nrchar/2; i++)
	{
		c = s[i];
		s[i] = s[nrchar-i-1];
		s[nrchar-i-1] = c;
	}
}

int xlru::check_complements_2(uint8_t rawValue)
{
	if (((rawValue & 0x80)==0) || (rawValue==0x80))
		return rawValue;
    return (uint8_t)(~(rawValue-1))*(-1);
}

int xlru::check_complements_2_from_buffer(char* buff, int index)
{
	int valueInt = 0;

    /*bool ok=*/xlru::hex2s8(&valueInt , &buff[index]);

    return valueInt;
}


void xlru::calc_checksum(char output[2], char* pstring)
{
	int len_mess = strlen(pstring);

	uint16_t checksum_tx = check_sum_calculus(pstring, 0, len_mess);

	conv_decimale_to_hex(&output[0], checksum_tx, 2);
	reverse(&output[0],2);
}


