summaryrefslogtreecommitdiff
path: root/usb-i2c-sl018
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-01-27 23:00:46 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-01-27 23:00:46 +0000
commit471f8e3dd04990402b2297a99d9e7448f8e6d311 (patch)
tree881ccb15dc74ec1e487d19ab61ef03d5f16688e4 /usb-i2c-sl018
parentadded check card (diff)
clean up @ update keys
git-svn-id: https://svn.spreadspace.org/avr/trunk@79 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-i2c-sl018')
-rw-r--r--usb-i2c-sl018/update-keys.c28
1 files changed, 14 insertions, 14 deletions
diff --git a/usb-i2c-sl018/update-keys.c b/usb-i2c-sl018/update-keys.c
index 9343c3c..116a285 100644
--- a/usb-i2c-sl018/update-keys.c
+++ b/usb-i2c-sl018/update-keys.c
@@ -24,14 +24,14 @@
#include <ctype.h>
#include <stdio.h>
-#define KEY_LEN_MAX 7
-#define MAX_KEYS 128
+#define EEPROM_SIZE 1024
+typedef uint8_t keyslot_t[8];
/* this generates a Fletcher8 checksum */
/* code from: http://stackoverflow.com/questions/13491700/8-bit-fletcher-checksum-of-16-byte-data */
-char generate_csum(char* data)
+uint8_t generate_csum(keyslot_t data)
{
- uint16_t sum1 = 0xf, sum2 = 0xf, len = KEY_LEN_MAX;
+ uint16_t sum1 = 0xf, sum2 = 0xf, len = sizeof(keyslot_t)-1;
do { sum2 += ( sum1 += *data++ ); } while (--len);
return sum2<<4 | sum1;
}
@@ -55,7 +55,7 @@ int main(int argc, char* argv[])
char* line = NULL;
size_t len = 0;
int line_num = 0;
- uint8_t key[KEY_LEN_MAX+1];
+ keyslot_t key;
int key_num = 0;
for(;;) {
@@ -70,7 +70,7 @@ int main(int argc, char* argv[])
break;
}
}
- if(i & 1 || i == 0 || i > KEY_LEN_MAX*2) {
+ if(i & 1 || i == 0 || i > (sizeof(keyslot_t)-1)*2) {
fprintf(stderr, "ignoring invalid key (odd number of digits or empty string or too long) at line %d\n", line_num);
continue;
}
@@ -82,14 +82,14 @@ int main(int argc, char* argv[])
tmp[1] = line[j*2 + 1];
key[j] = (char)strtoul(tmp, NULL, 16);
}
- for(j=i/2; j < KEY_LEN_MAX; ++j) {
+ for(j=i/2; j < sizeof(keyslot_t)-1; ++j) {
key[j] = 0;
}
- key[KEY_LEN_MAX] = generate_csum(key);
- fwrite(key, KEY_LEN_MAX+1, 1, dev);
+ key[sizeof(keyslot_t)-1] = generate_csum(key);
+ fwrite(key, sizeof(keyslot_t), 1, dev);
key_num++;
- if(key_num > MAX_KEYS) {
- fprintf(stderr, "reached maximum number of key slots (%d), will ignore remaining keys\n", MAX_KEYS);
+ if(key_num > EEPROM_SIZE/sizeof(keyslot_t)) {
+ fprintf(stderr, "reached maximum number of key slots (%d), will ignore remaining keys\n", EEPROM_SIZE/sizeof(keyslot_t));
break;
}
}
@@ -97,9 +97,9 @@ int main(int argc, char* argv[])
printf("read %d keys from STDIN\n", key_num);
int i;
- for(i=0; i<=KEY_LEN_MAX; ++i) key[i] = 0xFF;
- for(i=key_num; i < MAX_KEYS; ++i)
- fwrite(key, KEY_LEN_MAX+1, 1, dev);
+ for(i=0; i<=sizeof(keyslot_t)-1; ++i) key[i] = 0xFF;
+ for(i=key_num; i < EEPROM_SIZE/sizeof(keyslot_t); ++i)
+ fwrite(key, sizeof(keyslot_t), 1, dev);
char tmp;
while(fread(&tmp, 1, 1, dev)) {