altos: Add ao_boot_chain to telemega v0.3
[fw/altos] / src / util / ublox-cksum
1 #!/usr/bin/env nickle
2
3 typedef struct {
4         int     a, b;
5 } ck_t;
6
7 /* Fletcher algorithm */
8 ck_t checksum(int[] msg)
9 {
10         ck_t    ck = { .a = 0, .b = 0 };
11         for (int i = 4; i < dim(msg); i++) {
12                 ck.a += msg[i];
13                 ck.b += ck.a;
14                 ck.a &= 0xff;
15                 ck.b &= 0xff;
16         }
17         return ck;
18 }
19
20 void main()
21 {
22         string[...]     input;
23         int[...]        msg;
24
25         setdim(input, 0);
26         while (!File::end(stdin)) {
27                 input[dim(input)] = gets();
28         }
29
30         setdim(msg, 0);
31         for (int i = 0; i < dim(input); i++) {
32                 string[*] words = String::wordsplit(input[i], " ,\t");
33                 for (int j = 0; j < dim(words); j++) {
34                         if (words[j] == "/" + "*")
35                                 break;
36                         if (String::length(words[j]) > 0 &&
37                             Ctype::isdigit(words[j][0])) {
38                                 msg[dim(msg)] = string_to_integer(words[j]);
39                         }
40                  }
41         }
42         printf("\t0xb5, 0x62, \t\t/* length: %d bytes */\n", dim(msg));
43         for (int i = 0; i < dim(input); i++)
44                 printf("%s\n", input[i]);
45         ck_t ck = checksum(msg);
46         printf ("\t0x%02x, 0x%02x,\n",
47                 ck.a, ck.b);
48 }
49
50 main();