summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcbdev <cb@cbcdn.com>2023-08-26 15:15:07 +0200
committercbdev <cb@cbcdn.com>2023-08-26 15:15:07 +0200
commit2d2c7d80ca6a90d0b0eef469fa4bf68c0d71c722 (patch)
tree9d305eb46e0b9107b152a6df1b87c88f077083f7
parent12cf1472f159b5aaf26d871d88f684f1f46cf856 (diff)
downloadlibtwn-2d2c7d80ca6a90d0b0eef469fa4bf68c0d71c722.tar.gz
libtwn-2d2c7d80ca6a90d0b0eef469fa4bf68c0d71c722.tar.bz2
libtwn-2d2c7d80ca6a90d0b0eef469fa4bf68c0d71c722.zip
Implement basic writing
-rw-r--r--libtwn3.c18
-rw-r--r--libtwn3.h6
-rw-r--r--twn3_test.c26
3 files changed, 37 insertions, 13 deletions
diff --git a/libtwn3.c b/libtwn3.c
index 03e56c0..6359995 100644
--- a/libtwn3.c
+++ b/libtwn3.c
@@ -51,7 +51,7 @@ static inline size_t decode_ascii_inplace(uint8_t* data, size_t bytes){
break;
}
if(!isxdigit(data[u * 2]) || !isxdigit(data[u * 2 + 1])){
- return -1;
+ return 0;
}
data[u] = (hex_nibble(data[u * 2]) << 4) | hex_nibble(data[u * 2 + 1]);
}
@@ -268,7 +268,7 @@ int twn3_sync_antenna(int fd, uint8_t flags, uint8_t enable){
return (bytes > 2 && data[0] == 'P') ? 0 : -1;
}
-int twn3_sync_select_first(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length){
+int twn3_sync_select_first(int fd, uint8_t flags, uint8_t* type, uint8_t* uid, size_t* length){
char data[30];
size_t u = 0;
ssize_t bytes = twn3_sync_command(fd, flags, (uint8_t*) "s", 1, (uint8_t*) data, sizeof(data));
@@ -279,23 +279,23 @@ int twn3_sync_select_first(int fd, uint8_t flags, uint8_t* type, uint8_t* serial
u = decode_ascii_inplace((uint8_t*) data, bytes - 2);
- //TBD
- if(u % 2){
+ //currently, all uids are either 4, 7, or 10 bytes
+ if(u == 5 || u == 8 || u == 11){
//type tag prepended
*type = data[0];
- memcpy(serial, data + 1, MIN(*length, u - 1));
+ memcpy(uid, data + 1, MIN(*length, u - 1));
*length = u - 1;
}
else{
- memcpy(serial, data, MIN(*length, u));
+ memcpy(uid, data, MIN(*length, u));
*length = u;
}
}
return (bytes > 0) ? 0 : -1;
}
-int twn3_sync_list(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
-int twn3_sync_select(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
+int twn3_sync_list(int fd, uint8_t flags, uint8_t* type, uint8_t* uid, size_t* length);
+int twn3_sync_select(int fd, uint8_t flags, uint8_t* type, uint8_t* uid, size_t* length);
int twn3_sync_auth(int fd, uint8_t flags, uint8_t key_a, uint8_t sector, uint8_t key[6]){
char data[20];
@@ -361,7 +361,7 @@ int twn3_sync_write(int fd, uint8_t flags, uint8_t block, uint8_t value[16]){
value[8], value[9], value[10], value[11],
value[12], value[13], value[14], value[15]);
- bytes = twn3_sync_command(fd, flags, (uint8_t*) data, 19, (uint8_t*) data, sizeof(data));
+ bytes = twn3_sync_command(fd, flags, (uint8_t*) data, 35, (uint8_t*) data, sizeof(data));
if(bytes > 0){
if(data[1] != '\r'){
return 0;
diff --git a/libtwn3.h b/libtwn3.h
index 91b3406..d998c24 100644
--- a/libtwn3.h
+++ b/libtwn3.h
@@ -36,11 +36,11 @@ int twn3_sync_storekey(int fd, uint8_t flags, uint8_t keyid, uint8_t key[6]);
int twn3_sync_antenna(int fd, uint8_t flags, uint8_t enable);
/* Mifare commands */
-int twn3_sync_select_first(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
+int twn3_sync_select_first(int fd, uint8_t flags, uint8_t* type, uint8_t* uid, size_t* length);
//TBD clarify interface
-int twn3_sync_list(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
-int twn3_sync_select(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
+int twn3_sync_list(int fd, uint8_t flags, uint8_t* type, uint8_t* uid, size_t* length);
+int twn3_sync_select(int fd, uint8_t flags, uint8_t* type, uint8_t* uid, size_t* length);
int twn3_sync_auth(int fd, uint8_t flags, uint8_t key_a, uint8_t block, uint8_t key[6]);
int twn3_sync_auth_stored(int fd, uint8_t flags, uint8_t key_a, uint8_t block, uint8_t key_id);
diff --git a/twn3_test.c b/twn3_test.c
index 734201e..692cd3a 100644
--- a/twn3_test.c
+++ b/twn3_test.c
@@ -11,6 +11,7 @@
int main(int argc, char** argv){
char data[1024];
unsigned u;
+ uint8_t test_block = 4, test_sector = 1;
int device = twn3_open("/dev/ttyACM3");
@@ -44,7 +45,7 @@ int main(int argc, char** argv){
//store a key
//uint8_t key[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
//twn3_sync_storekey(device, 0, 12, key);
-
+
//wait for a single tag
int status;
uint8_t type = tag_none;
@@ -60,6 +61,29 @@ int main(int argc, char** argv){
serial_length = sizeof(serial);
}
+ printf("Authenticating for sector %d\n", test_sector);
+ uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+ twn3_sync_auth(device, 0, 1, test_sector, key);
+ printf("\n");
+
+ printf("Reading block %d\n", test_block);
+ twn3_sync_read(device, 0, test_block, (uint8_t*) data, 16);
+ printf("Block %d data: ", test_block);
+ for(u = 0; u < 16; u++){
+ printf("%02x ", data[u]);
+ }
+ printf("\n\n");
+
+ printf("Incrementing all bytes in block %d\n", test_block);
+ for(u = 0; u < 16; u++){
+ data[u]++;
+ }
+
+ printf("Writing block %d\n", test_block);
+ twn3_sync_write(device, 0, test_block, (uint8_t*) data);
+ printf("\n");
+
+
close(device);
return EXIT_SUCCESS;
}