mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2024-12-05 00:55:43 +01:00
Make the packing and unpacking work
This commit is contained in:
parent
a50b6746c0
commit
8f532ac7e4
@ -9,7 +9,7 @@
|
|||||||
#include "extract_keys.h"
|
#include "extract_keys.h"
|
||||||
|
|
||||||
|
|
||||||
#define KEY_SIZE 8
|
#define KEY_SIZE 16
|
||||||
#ifndef PROTOBUF_BLOCK_SIZE
|
#ifndef PROTOBUF_BLOCK_SIZE
|
||||||
#define PROTOBUF_BLOCK_SIZE 0
|
#define PROTOBUF_BLOCK_SIZE 0
|
||||||
#endif
|
#endif
|
||||||
@ -31,28 +31,30 @@ void process_key(TemporaryExposureKey* key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t generate_keys(uint8_t** buffer_pointer, size_t max_size, int num_keys) {
|
size_t generate_keys(uint8_t** buffer_pointer, size_t max_size, int num_keys) {
|
||||||
TemporaryExposureKeyExport export = {};
|
TemporaryExposureKeyExport export = TEMPORARY_EXPOSURE_KEY_EXPORT__INIT;
|
||||||
temporary_exposure_key_export__init(&export);
|
// temporary_exposure_key_export__init(&export);
|
||||||
TemporaryExposureKey** key_ptrs = (TemporaryExposureKey**) k_malloc(sizeof(TemporaryExposureKey*) * num_keys);
|
TemporaryExposureKey** key_ptrs = (TemporaryExposureKey**) k_malloc(sizeof(TemporaryExposureKey*) * num_keys);
|
||||||
if (key_ptrs == NULL) {
|
if (key_ptrs == NULL) {
|
||||||
printk("Could not allocate memory for pointers\n");
|
printk("Could not allocate memory for pointers\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
export.batch_num = 1;
|
||||||
|
export.batch_size = 1;
|
||||||
// TemporaryExposureKey* key_ptrs[num_keys];
|
// TemporaryExposureKey* key_ptrs[num_keys];
|
||||||
uint8_t key_data[KEY_SIZE];
|
uint8_t key_data[KEY_SIZE];
|
||||||
for (int i = 0; i < KEY_SIZE; i++) {
|
for (int i = 0; i < KEY_SIZE; i++) {
|
||||||
key_data[i] = 0xFF;
|
key_data[i] = 0xFF;
|
||||||
}
|
}
|
||||||
TemporaryExposureKey key;
|
TemporaryExposureKey key = TEMPORARY_EXPOSURE_KEY__INIT;
|
||||||
temporary_exposure_key__init(&key);
|
// temporary_exposure_key__init(&key);
|
||||||
key.key_data.data = key_data;
|
key.key_data.data = key_data;
|
||||||
key.key_data.len = KEY_SIZE;
|
key.key_data.len = KEY_SIZE;
|
||||||
key.has_key_data = true;
|
key.has_key_data = true;
|
||||||
for (int i = 0; i < num_keys; i++) {
|
for (int i = 0; i < num_keys; i++) {
|
||||||
key_ptrs[i] = &key;
|
key_ptrs[i] = &key;
|
||||||
}
|
}
|
||||||
export.keys = key_ptrs;
|
|
||||||
export.n_keys = num_keys;
|
export.n_keys = num_keys;
|
||||||
|
export.keys = key_ptrs;
|
||||||
size_t required_buffer = temporary_exposure_key_export__get_packed_size(&export);
|
size_t required_buffer = temporary_exposure_key_export__get_packed_size(&export);
|
||||||
uint8_t* buffer = (uint8_t*) k_malloc(required_buffer);
|
uint8_t* buffer = (uint8_t*) k_malloc(required_buffer);
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
@ -62,7 +64,7 @@ size_t generate_keys(uint8_t** buffer_pointer, size_t max_size, int num_keys) {
|
|||||||
*buffer_pointer = buffer;
|
*buffer_pointer = buffer;
|
||||||
if (buffer != NULL) {
|
if (buffer != NULL) {
|
||||||
size_t buf_size = temporary_exposure_key_export__pack(&export, buffer);
|
size_t buf_size = temporary_exposure_key_export__pack(&export, buffer);
|
||||||
free(key_ptrs);
|
k_free(key_ptrs);
|
||||||
return buf_size;
|
return buf_size;
|
||||||
} else {
|
} else {
|
||||||
printk("Buffer too small to serialize %d keys. %d bytes necessary\n", num_keys, required_buffer);
|
printk("Buffer too small to serialize %d keys. %d bytes necessary\n", num_keys, required_buffer);
|
||||||
@ -78,7 +80,9 @@ int unpack_keys(uint8_t* buf, size_t buf_size) {
|
|||||||
printk("error unpacking incoming message\n");
|
printk("error unpacking incoming message\n");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
printk("Buffer %s\n", buf);
|
||||||
|
printk("Batch num: %d, batch size: %d\n", export->batch_num, export->batch_size);
|
||||||
|
printk("Number of keys: %d\n", export->n_keys);
|
||||||
// Iterate over new keys
|
// Iterate over new keys
|
||||||
for (int i = 0; i < export->n_keys; i++) {
|
for (int i = 0; i < export->n_keys; i++) {
|
||||||
TemporaryExposureKey* key = export->keys[i];
|
TemporaryExposureKey* key = export->keys[i];
|
||||||
|
Loading…
Reference in New Issue
Block a user