annotate ostc4pack/src/checksum_final_add_fletcher.cpp @ 218:ff59d1d07f9c Improve_IPC_Sync

Splitted 120 seconds UART timeout into chunks of 500ms The UART connection via Bluetooth was realized using a receive call with 120 seconds timeout. By cancellation it seemed for the user as if the connection would have been aborted. In reality the received function keeped executing the wait for RX data till timeout occure. To avaoid this the timeout has been splitted into several calls with 500ms timeout => If the user disconnects by pressing "back" the COMM function is now ended.
author ideenmodellierer
date Sun, 31 Mar 2019 15:44:12 +0200
parents 1e707b34667e
children 01f40cb1057e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
1 #include <iostream>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
2 #include <stdlib.h>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
3 #include <stdio.h>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
4 #include <string.h>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
5 #include <time.h>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
6
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
7 void fletcher16(unsigned char *result1, unsigned char *result2, unsigned char const *data, size_t bytes )
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
8 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
9 unsigned short sum1 = 0xff, sum2 = 0xff;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
10 size_t tlen;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
11
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
12 while (bytes) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
13 tlen = bytes >= 20 ? 20 : bytes;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
14 bytes -= tlen;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
15 do {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
16 sum2 += sum1 += *data++;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
17 } while (--tlen);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
18 sum1 = (sum1 & 0xff) + (sum1 >> 8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
19 sum2 = (sum2 & 0xff) + (sum2 >> 8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
20 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
21 /* Second reduction step to reduce sums to 8 bits */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
22 sum1 = (sum1 & 0xff) + (sum1 >> 8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
23 sum2 = (sum2 & 0xff) + (sum2 >> 8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
24 *result2 = (sum2 & 0xff);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
25 *result1 = (sum1 & 0xff);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
26 // return sum2 << 8 | sum1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
27 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
28
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
29
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
30 // This is a variant of fletcher16 with a 16 bit sum instead of an 8 bit sum,
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
31 // and modulo 2^16 instead of 2^16-1
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
32 void
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
33 hw_ostc3_firmware_checksum (unsigned char *result1, unsigned char *result2, unsigned char *result3, unsigned char *result4, unsigned char const *data, size_t bytes )
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
34 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
35 unsigned short low = 0;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
36 unsigned short high = 0;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
37 for (unsigned int i = 0; i < bytes; i++) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
38 low += data[i];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
39 high += low;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
40 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
41 *result1 = (low & 0xff);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
42 *result2 = (low/256 & 0xff);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
43 *result3 = (unsigned char)(high & 0xff);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
44 *result4 = (unsigned char)((high/256) & 0xff);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
45 // return (((unsigned int)high) << 16) + low;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
46 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
47
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
48
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
49 int main(int argc, char** argv) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
50
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
51
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
52 FILE *fp, * fpout;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
53 size_t lenTotal,lenTemp;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
54 unsigned char buf[2000000];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
55 char *file = argv[1];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
56 char *file2 = argv[2];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
57 char *file3 = argv[3];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
58 unsigned int pruefsumme;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
59 unsigned char buf2[4];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
60
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
61 printf("1: %s\n", file);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
62 printf("2: %s\n", file2);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
63 printf("3: %s\n", file3);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
64 printf("\n");
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
65
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
66
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
67 //write File with length and cheksum
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
68 char filename[500], filenameout[510] ;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
69 sprintf(filename,"%s",file);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
70 int filelength = strlen(filename);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
71 filename[filelength -4] = 0;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
72
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
73 lenTotal = 0;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
74 if (NULL == (fp = fopen(file, "rb")))
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
75 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
76 printf("Unable to open %s for reading\n", file);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
77 return -1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
78 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
79 lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf), fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
80 // lenTemp = fread(buf, sizeof(char), sizeof(buf), fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
81 lenTotal = lenTemp;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
82 printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
83 fclose(fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
84
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
85 if(file2)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
86 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
87 if (NULL == (fp = fopen(file2, "rb")))
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
88 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
89 printf("Unable to open %s for reading\n", file2);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
90 return -1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
91 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
92 lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf)-lenTotal, fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
93 lenTotal += lenTemp;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
94 printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
95 fclose(fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
96 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
97 if(file3)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
98 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
99 if (NULL == (fp = fopen(file3, "rb")))
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
100 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
101 printf("Unable to open %s for reading\n", file3);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
102 return -1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
103 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
104 lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf)-lenTotal, fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
105 lenTotal += lenTemp;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
106 printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
107 fclose(fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
108 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
109
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
110 printf("\n");
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
111 printf("%d bytes read (hex: %#x ) total \n", lenTotal,lenTotal);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
112
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
113 time_t rawtime;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
114 time (&rawtime);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
115 struct tm *timeinfo;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
116 timeinfo = localtime(&rawtime);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
117
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
118 // sprintf(filenameout,"fwupdate_%s.bin",ctime(&rawtime));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
119 sprintf(filenameout,"OSTC4update_%02u%02u%02u.bin",timeinfo->tm_year-100,timeinfo->tm_mon+1,timeinfo->tm_mday);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
120
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
121 fpout = fopen(filenameout, "wb");
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
122 for(int i = 0;i <lenTotal;i++)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
123 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
124 if(fwrite(&buf[i],1,1,fpout) != 1)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
125 printf("error writing\n");
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
126 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
127
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
128 hw_ostc3_firmware_checksum(&buf2[0],&buf2[1],&buf2[2],&buf2[3],buf,lenTotal);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
129 printf("checksum %#x %#x %#x %#x\n", buf2[0],buf2[1], buf2[2], buf2[3]);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
130 if(fwrite(&buf2[0],1,4,fpout) != 4)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
131 printf("error writing checksum\n");
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
132 ;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
133 fclose(fpout);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
134 }