mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
componeent structure, preparation for component branch
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"ena-binary-export.c"
|
||||
INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES
|
||||
ena
|
||||
nanopb
|
||||
EMBED_FILES
|
||||
"test/export.bin"
|
||||
)
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-exposure.h"
|
||||
|
||||
#include "pb_decode.h"
|
||||
#include "TemporaryExposureKeyExport.pb.h"
|
||||
|
||||
static const char kFileHeader[] = "EK Export v1 ";
|
||||
static size_t kFileHeaderSize = sizeof(kFileHeader) - 1;
|
||||
|
||||
bool ena_binary_export_decode_key_data(pb_istream_t *stream, const pb_field_t *field, void **arg)
|
||||
{
|
||||
uint8_t *key_data = (uint8_t *)*arg;
|
||||
|
||||
if (!pb_read(stream, key_data, stream->bytes_left))
|
||||
{
|
||||
ESP_LOGW(ENA_EXPOSURE_LOG, "Decoding failed: %s\n", PB_GET_ERROR(stream));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ena_binary_export_decode_key(pb_istream_t *stream, const pb_field_t *field, void **arg)
|
||||
{
|
||||
uint8_t key_data[ENA_KEY_LENGTH] = {0};
|
||||
TemporaryExposureKey tek = TemporaryExposureKey_init_zero;
|
||||
|
||||
tek.key_data = (pb_callback_t){
|
||||
.funcs.decode = ena_binary_export_decode_key_data,
|
||||
.arg = &key_data,
|
||||
};
|
||||
|
||||
if (!pb_decode(stream, TemporaryExposureKey_fields, &tek))
|
||||
{
|
||||
ESP_LOGW(ENA_EXPOSURE_LOG, "Decoding failed: %s\n", PB_GET_ERROR(stream));
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGD(ENA_EXPOSURE_LOG,
|
||||
"check reported tek: rolling_start_interval_number %d, rolling_period %d, days_since_last_exposure %d, report_type %d, transmission_risk_values %d",
|
||||
tek.rolling_start_interval_number, tek.rolling_period, tek.days_since_onset_of_symptoms, tek.report_type, tek.transmission_risk_level);
|
||||
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_EXPOSURE_LOG, &key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
|
||||
ena_temporary_exposure_key_t temporary_exposure_key = {
|
||||
.transmission_risk_level = tek.transmission_risk_level,
|
||||
.rolling_start_interval_number = tek.rolling_start_interval_number,
|
||||
.rolling_period = tek.rolling_period,
|
||||
.report_type = tek.report_type,
|
||||
.days_since_onset_of_symptoms = tek.days_since_onset_of_symptoms,
|
||||
};
|
||||
|
||||
memcpy(temporary_exposure_key.key_data, key_data, ENA_KEY_LENGTH);
|
||||
|
||||
ena_exposure_check_temporary_exposure_key(temporary_exposure_key);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
esp_err_t ena_binary_export_check_export(uint8_t *buf, size_t size)
|
||||
{
|
||||
// validate header
|
||||
if (memcmp(kFileHeader, buf, kFileHeaderSize) != 0)
|
||||
{
|
||||
ESP_LOGW(ENA_EXPOSURE_LOG, "Wrong or missing header!");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
TemporaryExposureKeyExport tek_export = TemporaryExposureKeyExport_init_zero;
|
||||
|
||||
tek_export.keys = (pb_callback_t){
|
||||
.funcs.decode = ena_binary_export_decode_key,
|
||||
};
|
||||
|
||||
pb_istream_t stream = pb_istream_from_buffer(&buf[kFileHeaderSize], (size - kFileHeaderSize));
|
||||
|
||||
if (!pb_decode(&stream, TemporaryExposureKeyExport_fields, &tek_export))
|
||||
{
|
||||
ESP_LOGW(ENA_EXPOSURE_LOG, "Decoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief decode Exposure Key export and run exposure check
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_BINARY_H_
|
||||
#define _ena_BINARY_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
/**
|
||||
* @brief reads a Temporary Exposue Key Export binary and check for exposures
|
||||
*
|
||||
* @param[in] buf the buffer containing the binary data
|
||||
* @param[in] size the size of the buffer
|
||||
*
|
||||
* @return
|
||||
* esp_err_t status of reading binary
|
||||
*/
|
||||
esp_err_t ena_binary_export_check_export(uint8_t *buf, size_t size);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user