view ostc4pack/src/checksum_final_add_fletcher.cpp @ 224:ceecabfddb57 div-fixes-3

Bugfix, deco: fix 2 (small) problems with calculated ceiling This fixes 1 trivial, and 1 not really trivial bug in the calculation of the ceiling. When simulating a bounce dive to 80m, things become clear (tried this on a CCR dive, fixed setpoint 1.2bar, about 15 minutes of bottom time). Closely watch the behavior of the ceiling data. At some point during the ascent, the ceiling begins to decrease in 10cm steps. Then suddenly (while still ascending), the ceiling increases again with 1m, does not change for some time, and then suddenly steps 1.1m less deep. While not very relevant to real deco diving, it is simply wrong. The reason for this is subtle. The algorithm used to find the ceiling is a sort of linear search, stepping down a meter, overshoot the depth, and search back in 10cm steps. It seems some numerical instability. Fixing this, was a bit more computational intensive search by stepping up down in equal steps of 10cm. But, I'm pretty sure that things can be speeded up here, as a ceiling does not change fast, so it should be not that difficult to limit the search space, or use a binary search algorithm instead. The trivial second problem fixed, is that the ceiling ends at the surface and not at 1m depth. This small issue became visible after changing the step down size above. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Sun, 31 Mar 2019 19:35:51 +0200
parents 1e707b34667e
children 01f40cb1057e
line wrap: on
line source

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

void fletcher16(unsigned char *result1, unsigned char *result2, unsigned char const *data, size_t bytes )
{
        unsigned short sum1 = 0xff, sum2 = 0xff;
        size_t tlen;
 
        while (bytes) {
                tlen = bytes >= 20 ? 20 : bytes;
                bytes -= tlen;
                do {
                        sum2 += sum1 += *data++;
                } while (--tlen);
                sum1 = (sum1 & 0xff) + (sum1 >> 8);
                sum2 = (sum2 & 0xff) + (sum2 >> 8);
        }
        /* Second reduction step to reduce sums to 8 bits */
        sum1 = (sum1 & 0xff) + (sum1 >> 8);
        sum2 = (sum2 & 0xff) + (sum2 >> 8);
        *result2 = (sum2 & 0xff);
        *result1 = (sum1 & 0xff);
//        return sum2 << 8 | sum1;
}


// This is a variant of fletcher16 with a 16 bit sum instead of an 8 bit sum,
// and modulo 2^16 instead of 2^16-1
void
hw_ostc3_firmware_checksum (unsigned char *result1, unsigned char *result2, unsigned char *result3, unsigned char *result4, unsigned char const *data, size_t bytes )
{
	unsigned short low = 0;
	unsigned short high = 0;
	for (unsigned int i = 0; i < bytes; i++) {
		low  += data[i];
		high += low;
	}
        *result1 = (low & 0xff);
        *result2 = (low/256 & 0xff);
        *result3 = (unsigned char)(high & 0xff);
        *result4 = (unsigned char)((high/256) & 0xff);
//	return (((unsigned int)high) << 16) + low;
}


int main(int argc, char** argv) {
	
	
	FILE *fp, * fpout;
	size_t lenTotal,lenTemp;
	unsigned char buf[2000000];
	char *file = argv[1];
	char *file2 =  argv[2];
	char *file3 =  argv[3];
	unsigned int pruefsumme;
	unsigned char buf2[4];

   	printf("1: %s\n",  file);
   	printf("2: %s\n",  file2);
   	printf("3: %s\n",  file3);
   	printf("\n");
     
	
		//write File with length and cheksum
	char filename[500], filenameout[510] ;
	sprintf(filename,"%s",file);
	int filelength = strlen(filename);
	filename[filelength -4] = 0;
	
	lenTotal = 0;
	if (NULL == (fp = fopen(file, "rb")))
	{
	    printf("Unable to open %s for reading\n", file);
	    return -1;
	}
	lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf), fp);
//	lenTemp = fread(buf, sizeof(char), sizeof(buf), fp);
	lenTotal = lenTemp;
	printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp);
	fclose(fp);

	if(file2)
	{
		if (NULL == (fp = fopen(file2, "rb")))
		{
		    printf("Unable to open %s for reading\n", file2);
		    return -1;
		}
		lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf)-lenTotal, fp);
		lenTotal += lenTemp;
		printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp);
		fclose(fp);
	}
	if(file3)
	{
		if (NULL == (fp = fopen(file3, "rb")))
		{
		    printf("Unable to open %s for reading\n", file3);
		    return -1;
		}
		lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf)-lenTotal, fp);
		lenTotal += lenTemp;
		printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp);
		fclose(fp);
	}

   	printf("\n");
	printf("%d bytes read (hex: %#x ) total \n", lenTotal,lenTotal);

	time_t rawtime;
	time (&rawtime);
	struct tm *timeinfo;
	timeinfo = localtime(&rawtime);
	
//	sprintf(filenameout,"fwupdate_%s.bin",ctime(&rawtime));
	sprintf(filenameout,"OSTC4update_%02u%02u%02u.bin",timeinfo->tm_year-100,timeinfo->tm_mon+1,timeinfo->tm_mday);

    fpout = fopen(filenameout, "wb");     
    for(int i = 0;i <lenTotal;i++)
    {
    	if(fwrite(&buf[i],1,1,fpout) != 1)
     	printf("error writing\n");
	}
	
	hw_ostc3_firmware_checksum(&buf2[0],&buf2[1],&buf2[2],&buf2[3],buf,lenTotal);
	printf("checksum  %#x %#x %#x %#x\n", buf2[0],buf2[1], buf2[2], buf2[3]);
   	if(fwrite(&buf2[0],1,4,fpout) != 4)
    	printf("error writing checksum\n");
    ;
	fclose(fpout);
}