|
CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
|
#include "mod_vcard/vcard_store.h"#include "cdc_core/Hash.h"#include "cdc_log.h"#include "nvs.h"#include "nvs_flash.h"#include "esp_attr.h"#include <cctype>#include <cstdio>#include <cstring>Go to the source code of this file.
Classes | |
| struct | cdc::mod_vcard::vcard_meta_t |
Namespaces | |
| namespace | cdc |
| namespace | cdc::mod_vcard |
Functions | |
| static uint32_t | fnv1a_hash (const char *data, size_t len) |
| Computes FNV-1a hash for vCard duplicate tracking. | |
| static void | vcard_key_for_slot (char *out, size_t out_len, uint16_t slot) |
| Formats NVS key name for a card slot. | |
| static void | vcard_trim_cr (char *line) |
| Trims trailing CR/LF characters from one line. | |
| static bool | vcard_line_has_content (const char *line, size_t len) |
| Checks whether a vCard line contains meaningful field content. | |
| size_t | vcard_filter_empty_fields (char *vcard, size_t len) |
| Removes empty optional fields from vCard text in-place. | |
| static bool | vcard_extract_line (const char *vcard, const char *prefix, char *out, size_t out_len) |
| Extracts value from first vCard line matching prefix. | |
| static void | vcard_parse_names (const char *vcard, char *last, size_t last_len, char *display, size_t display_len) |
| Derives sortable last-name and display-name fields from vCard. | |
| static void | set_err (char *err, size_t err_len, const char *msg) |
| Writes error text into bounded output buffer. | |
| static bool | vcard_validate (const char *vcard, size_t len, char *err, size_t err_len) |
| Validates basic vCard format constraints. | |
| static void | vcard_load_own (void) |
| Lazily loads local own-vCard from NVS cache. | |
| bool | vcard_store_set_own (const char *vcard, size_t len, char *err, size_t err_len) |
| Stores local own-vCard after validation and field filtering. | |
| size_t | vcard_store_get_own (char *out, size_t max_len) |
| Retrieves local own-vCard text. | |
| bool | vcard_store_has_own (void) |
| Returns whether local own-vCard exists. | |
| bool | vcard_store_get_display_own (char *out, size_t max_len) |
| Retrieves display name derived from local own-vCard. | |
| bool | vcard_store_clear_own (void) |
| Deletes local own-vCard from storage. | |
| void | vcard_store_init (void) |
| Initializes metadata cache for stored peer vCards. | |
| uint16_t | vcard_store_count (void) |
| Returns number of stored peer vCards. | |
| static bool | vcard_is_duplicate (nvs_handle_t nvs, const char *vcard, size_t len, uint32_t hash) |
| Checks whether candidate vCard is already stored. | |
| bool | vcard_store_contains (const char *vcard, size_t len) |
| Reports whether an exact-text vCard is already stored. | |
| bool | vcard_store_add (const char *vcard, size_t len, char *err, size_t err_len) |
| Adds peer vCard to first free slot after validation and duplicate check. | |
| bool | vcard_store_update (uint16_t slot, const char *vcard, size_t len, char *err, size_t err_len) |
| Overwrites the vCard stored at slot in place after validation. | |
| bool | vcard_store_delete (uint16_t slot) |
| Deletes peer vCard at slot index. | |
| size_t | vcard_store_get (uint16_t slot, char *out, size_t max_len) |
| Retrieves raw vCard text from slot. | |
| bool | vcard_store_get_display (uint16_t slot, char *out, size_t max_len) |
| Retrieves cached display label for slot. | |
| static void | copy_field (char *dst, size_t dst_size, const char *src, size_t src_len) |
| Copies a bounded value into a fixed-size destination buffer. | |
| static size_t | trim_line_len (const char *line, size_t len) |
| Trims trailing CR and LF and whitespace characters from a length-bounded view. | |
| static const char * | line_value (const char *line, size_t line_len, size_t *out_len) |
| Returns pointer to the value portion of a vCard line and its length. For a line like TEL;TYPE=HOME:1234, this returns the part after the first colon. | |
| static bool | match_property (const char *line, size_t line_len, const char *prop, const char *type_value) |
| Tests whether one vCard property prefix matches a line, optionally accepting a TYPE=... parameter and a value sub-prefix (for IMPP). Matching is case-insensitive for the property name and parameter value. | |
| static void | parse_line_into_struct (const char *line, size_t line_len, vcard_data_t *out) |
| Parses a single vCard line into the structured data, when recognized. | |
| bool | vcard_parse_to_struct (const char *raw, vcard_data_t *out) |
| Parses raw vCard 4.0 text into a structured representation. | |
| static bool | append_field (char *buf, size_t buf_size, size_t *pos, const char *prefix, const char *value) |
| Appends prefix:value\n to the output buffer if value is non-empty. | |
| size_t | vcard_generate_from_struct (const vcard_data_t *data, char *out_buf, size_t buf_len) |
| Generates a vCard 4.0 text representation from the structured data. | |
| uint16_t | vcard_store_get_sorted (uint16_t *out_slots, uint16_t max_slots) |
| Returns slot indices of stored cards sorted by last name. | |
Variables | |
| static const char * | TAG = "VCARD" |
| static constexpr const char * | VCARD_NAMESPACE = "mod_vcard" |
| static constexpr const char * | VCARD_KEY_OWN = "own" |
| static char | g_own_vcard [768+1] |
| static bool | g_own_loaded = false |
| static bool | g_own_present = false |
| static vcard_meta_t | g_cards [100] |
| static bool | g_cards_loaded = false |
| static uint16_t | g_card_count = 0 |
|
static |
Appends prefix:value\n to the output buffer if value is non-empty.
Definition at line 963 of file vcard_store.cpp.
Referenced by vcard_generate_from_struct().
|
static |
Copies a bounded value into a fixed-size destination buffer.
Definition at line 731 of file vcard_store.cpp.
Referenced by parse_line_into_struct().
|
static |
Computes FNV-1a hash for vCard duplicate tracking.
| data | Input buffer. |
| len | Input length. |
Definition at line 47 of file vcard_store.cpp.
References cdc::core::hash::fnv1a_32().
Referenced by vcard_store_add(), vcard_store_contains(), vcard_store_init(), and vcard_store_update().
|
static |
Returns pointer to the value portion of a vCard line and its length. For a line like TEL;TYPE=HOME:1234, this returns the part after the first colon.
| line | Pointer to start of line. |
| line_len | Length of the line (without terminator). |
| out_len | Output: length of the value portion. |
Definition at line 757 of file vcard_store.cpp.
References trim_line_len().
Referenced by parse_line_into_struct().
|
static |
Tests whether one vCard property prefix matches a line, optionally accepting a TYPE=... parameter and a value sub-prefix (for IMPP). Matching is case-insensitive for the property name and parameter value.
| line | Pointer to line start. |
| line_len | Total line length. |
| prop | Property name (e.g. "TEL", "EMAIL"). |
| type_value | Optional TYPE= match (e.g. "HOME"); pass nullptr to skip. |
Definition at line 777 of file vcard_store.cpp.
Referenced by parse_line_into_struct().
|
static |
Parses a single vCard line into the structured data, when recognized.
Definition at line 818 of file vcard_store.cpp.
References copy_field(), vcard_data_t::email, vcard_data_t::family_name, vcard_data_t::formatted_name, vcard_data_t::given_name, vcard_data_t::impp_matrix, vcard_data_t::impp_signal, vcard_data_t::impp_telegram, vcard_data_t::impp_threema, line_value(), match_property(), vcard_data_t::note, vcard_data_t::organization, vcard_data_t::social_profile, vcard_data_t::tel_cell, vcard_data_t::tel_home, vcard_data_t::tel_work, vcard_data_t::title, trim_line_len(), and vcard_data_t::url.
Referenced by vcard_parse_to_struct().
|
static |
Writes error text into bounded output buffer.
| err | Output error buffer. |
| err_len | Output buffer size. |
| Message transfer (badge-to-badge) | Error message text. |
Definition at line 271 of file vcard_store.cpp.
Referenced by vcard_store_add(), vcard_store_set_own(), vcard_store_update(), and vcard_validate().
|
static |
Trims trailing CR and LF and whitespace characters from a length-bounded view.
Definition at line 741 of file vcard_store.cpp.
Referenced by line_value(), and parse_line_into_struct().
|
static |
Extracts value from first vCard line matching prefix.
| vcard | vCard text. |
| prefix | Line prefix (for example FN:). |
| out | Output value buffer. |
| out_len | Output buffer size. |
Definition at line 170 of file vcard_store.cpp.
References vcard_trim_cr().
Referenced by vcard_parse_names().
| size_t vcard_filter_empty_fields | ( | char * | vcard, |
| size_t | len ) |
Removes empty optional fields from vCard text in-place.
| vcard | Mutable vCard buffer. |
| len | Input length. |
Definition at line 126 of file vcard_store.cpp.
References vcard_line_has_content(), and VCARD_MAX_LEN.
Referenced by vcard_store_add(), vcard_store_set_own(), and vcard_store_update().
| size_t vcard_generate_from_struct | ( | const vcard_data_t * | data, |
| char * | out_buf, | ||
| size_t | buf_len ) |
Generates a vCard 4.0 text representation from the structured data.
Generates vCard 4.0 text from a structured vcard_data_t. Empty fields are omitted. FN falls back to "given family" when empty.
Definition at line 981 of file vcard_store.cpp.
References append_field(), vcard_data_t::email, vcard_data_t::family_name, vcard_data_t::formatted_name, vcard_data_t::given_name, vcard_data_t::impp_matrix, vcard_data_t::impp_signal, vcard_data_t::impp_telegram, vcard_data_t::impp_threema, vcard_data_t::note, vcard_data_t::organization, vcard_data_t::social_profile, vcard_data_t::tel_cell, vcard_data_t::tel_home, vcard_data_t::tel_work, vcard_data_t::title, and vcard_data_t::url.
Referenced by cdc::mod_vcard::wizardFinish().
|
static |
Checks whether candidate vCard is already stored.
| NVS (plugin-namespaced) | Open NVS handle. |
| vcard | Candidate vCard text. |
| len | Candidate length. |
| hash | Candidate hash (currently unused optimization hint). |
Definition at line 498 of file vcard_store.cpp.
References vcard_key_for_slot(), VCARD_MAX_CARDS, and VCARD_MAX_LEN.
Referenced by vcard_store_add(), and vcard_store_contains().
|
static |
Formats NVS key name for a card slot.
| out | Output key buffer. |
| out_len | Output buffer size. |
| slot | Slot index. |
Definition at line 57 of file vcard_store.cpp.
Referenced by vcard_is_duplicate(), vcard_store_add(), vcard_store_delete(), vcard_store_get(), vcard_store_init(), and vcard_store_update().
|
static |
Checks whether a vCard line contains meaningful field content.
| line | Line pointer. |
| len | Line length. |
Definition at line 79 of file vcard_store.cpp.
Referenced by vcard_filter_empty_fields().
|
static |
Lazily loads local own-vCard from NVS cache.
Definition at line 316 of file vcard_store.cpp.
References g_own_loaded, g_own_present, g_own_vcard, VCARD_KEY_OWN, and VCARD_NAMESPACE.
Referenced by vcard_store_get_display_own(), vcard_store_get_own(), and vcard_store_has_own().
|
static |
Derives sortable last-name and display-name fields from vCard.
| vcard | vCard text. |
| last | Output last-name buffer. |
| last_len | Last-name buffer size. |
| display | Output display-name buffer. |
| display_len | Display-name buffer size. |
Definition at line 203 of file vcard_store.cpp.
References vcard_extract_line().
Referenced by vcard_store_add(), vcard_store_get_display_own(), vcard_store_init(), and vcard_store_update().
| bool vcard_parse_to_struct | ( | const char * | raw, |
| vcard_data_t * | out ) |
Parses raw vCard 4.0 text into a structured representation.
Parses vCard 4.0 raw text into a structured vcard_data_t.
Definition at line 935 of file vcard_store.cpp.
References parse_line_into_struct().
Referenced by cdc::mod_vcard::VcardWizard::edit(), cdc::mod_vcard::VcardWizard::editReceived(), cdc::mod_vcard::showVcardDetails(), and cdc::mod_vcard::showVcardQr().
| bool vcard_store_add | ( | const char * | vcard, |
| size_t | len, | ||
| char * | err, | ||
| size_t | err_len ) |
Adds peer vCard to first free slot after validation and duplicate check.
| vcard | vCard text. |
| len | Input length. |
| err | Output error buffer. |
| err_len | Error buffer size. |
Definition at line 539 of file vcard_store.cpp.
References fnv1a_hash(), g_card_count, g_cards, set_err(), vcard_filter_empty_fields(), vcard_is_duplicate(), vcard_key_for_slot(), VCARD_MAX_CARDS, VCARD_MAX_LEN, VCARD_NAMESPACE, vcard_parse_names(), vcard_store_init(), and vcard_validate().
Referenced by cdc::mod_vcard::deliverVcard(), cdc::mod_vcard::importReceivedVcard(), and cdc::mod_vcard::wizardFinish().
| bool vcard_store_clear_own | ( | void | ) |
Deletes local own-vCard from storage.
Definition at line 429 of file vcard_store.cpp.
References g_own_loaded, g_own_present, g_own_vcard, VCARD_KEY_OWN, and VCARD_NAMESPACE.
Referenced by cdc::mod_vcard::cmdVcardDelete().
| bool vcard_store_contains | ( | const char * | vcard, |
| size_t | len ) |
Reports whether an exact-text vCard is already stored.
| vcard | Candidate vCard text. |
| len | Candidate length. |
vcard byte for byte. Definition at line 518 of file vcard_store.cpp.
References fnv1a_hash(), vcard_is_duplicate(), VCARD_NAMESPACE, and vcard_store_init().
Referenced by cdc::mod_vcard::importReceivedVcard().
| uint16_t vcard_store_count | ( | void | ) |
Returns number of stored peer vCards.
Definition at line 485 of file vcard_store.cpp.
References g_card_count, and vcard_store_init().
| bool vcard_store_delete | ( | uint16_t | slot | ) |
Deletes peer vCard at slot index.
| slot | Slot index. |
Definition at line 654 of file vcard_store.cpp.
References g_card_count, g_cards, vcard_key_for_slot(), VCARD_MAX_CARDS, VCARD_NAMESPACE, and vcard_store_init().
Referenced by cdc::mod_vcard::cmdVcardDelete(), and cdc::mod_vcard::onReceivedDeleteConfirm().
| size_t vcard_store_get | ( | uint16_t | slot, |
| char * | out, | ||
| size_t | max_len ) |
Retrieves raw vCard text from slot.
| slot | Slot index. |
| out | Output buffer. |
| max_len | Output buffer size. |
Definition at line 683 of file vcard_store.cpp.
References g_cards, vcard_key_for_slot(), VCARD_MAX_CARDS, VCARD_NAMESPACE, and vcard_store_init().
Referenced by cdc::mod_vcard::cmdVcardGet(), cdc::mod_vcard::ctxReceivedForward(), cdc::mod_vcard::ctxReceivedQr(), cdc::mod_vcard::VcardWizard::editReceived(), cdc::mod_vcard::VcardModule::exportBackup(), and cdc::mod_vcard::onReceivedSelect().
| bool vcard_store_get_display | ( | uint16_t | slot, |
| char * | out, | ||
| size_t | max_len ) |
Retrieves cached display label for slot.
| slot | Slot index. |
| out | Output label buffer. |
| max_len | Output buffer size. |
Definition at line 717 of file vcard_store.cpp.
References g_cards, VCARD_MAX_CARDS, and vcard_store_init().
Referenced by cdc::mod_vcard::cmdVcardList(), cdc::mod_vcard::cmdVcardSet(), and cdc::mod_vcard::rebuildReceivedList().
| bool vcard_store_get_display_own | ( | char * | out, |
| size_t | max_len ) |
Retrieves display name derived from local own-vCard.
| out | Output buffer. |
| max_len | Output buffer size. |
Definition at line 413 of file vcard_store.cpp.
References g_own_present, g_own_vcard, vcard_load_own(), and vcard_parse_names().
| size_t vcard_store_get_own | ( | char * | out, |
| size_t | max_len ) |
Retrieves local own-vCard text.
| out | Output buffer. |
| max_len | Output buffer size. |
Definition at line 387 of file vcard_store.cpp.
References g_own_present, g_own_vcard, and vcard_load_own().
Referenced by cdc::mod_vcard::cmdVcardGet(), cdc::mod_vcard::VcardWizard::edit(), cdc::mod_vcard::VcardModule::exportBackup(), cdc::mod_vcard::onMainMenuSelect(), and cdc::mod_vcard::onMyVcardLockscreenSelect().
| uint16_t vcard_store_get_sorted | ( | uint16_t * | out_slots, |
| uint16_t | max_slots ) |
Returns slot indices of stored cards sorted by last name.
| out_slots | Output slot-index array. |
| max_slots | Maximum writable entries. |
Definition at line 1048 of file vcard_store.cpp.
References g_cards, VCARD_MAX_CARDS, and vcard_store_init().
Referenced by cdc::mod_vcard::cmdVcardList(), cdc::mod_vcard::VcardModule::exportBackup(), and cdc::mod_vcard::rebuildReceivedList().
| bool vcard_store_has_own | ( | void | ) |
Returns whether local own-vCard exists.
Definition at line 402 of file vcard_store.cpp.
References g_own_present, and vcard_load_own().
Referenced by cdc::mod_vcard::onMainMenuSelect().
| void vcard_store_init | ( | void | ) |
Initializes metadata cache for stored peer vCards.
Definition at line 448 of file vcard_store.cpp.
References fnv1a_hash(), g_card_count, g_cards, g_cards_loaded, vcard_key_for_slot(), VCARD_MAX_CARDS, VCARD_MAX_LEN, VCARD_NAMESPACE, and vcard_parse_names().
Referenced by vcard_store_add(), vcard_store_contains(), vcard_store_count(), vcard_store_delete(), vcard_store_get(), vcard_store_get_display(), vcard_store_get_sorted(), and vcard_store_update().
| bool vcard_store_set_own | ( | const char * | vcard, |
| size_t | len, | ||
| char * | err, | ||
| size_t | err_len ) |
Stores local own-vCard after validation and field filtering.
| vcard | vCard text. |
| len | Input length. |
| err | Output error buffer. |
| err_len | Error buffer size. |
Definition at line 343 of file vcard_store.cpp.
References g_own_loaded, g_own_present, g_own_vcard, LOG_I, set_err(), TAG, vcard_filter_empty_fields(), VCARD_KEY_OWN, VCARD_MAX_LEN, VCARD_NAMESPACE, and vcard_validate().
Referenced by cdc::mod_vcard::VcardModule::importBackup(), cdc::mod_vcard::vcardLineInterceptor(), and cdc::mod_vcard::wizardFinish().
| bool vcard_store_update | ( | uint16_t | slot, |
| const char * | vcard, | ||
| size_t | len, | ||
| char * | err, | ||
| size_t | err_len ) |
Overwrites the vCard stored at slot in place after validation.
Overwrites the vCard stored at slot in place after validation.
| slot | Slot index of an existing stored card. |
| vcard | New vCard text. |
| len | New vCard length. |
| err | Output error buffer. |
| err_len | Error buffer size. |
Definition at line 610 of file vcard_store.cpp.
References fnv1a_hash(), g_cards, set_err(), vcard_filter_empty_fields(), vcard_key_for_slot(), VCARD_MAX_CARDS, VCARD_MAX_LEN, VCARD_NAMESPACE, vcard_parse_names(), vcard_store_init(), and vcard_validate().
Referenced by cdc::mod_vcard::vcardLineInterceptor(), and cdc::mod_vcard::wizardFinish().
|
static |
Trims trailing CR/LF characters from one line.
| line | Mutable line buffer. |
Definition at line 65 of file vcard_store.cpp.
Referenced by vcard_extract_line().
|
static |
Validates basic vCard format constraints.
| vcard | vCard text buffer. |
| len | Input length. |
| err | Output error buffer. |
| err_len | Error buffer size. |
Definition at line 285 of file vcard_store.cpp.
References set_err(), and VCARD_MAX_LEN.
Referenced by vcard_store_add(), vcard_store_set_own(), and vcard_store_update().
|
static |
Definition at line 39 of file vcard_store.cpp.
Referenced by vcard_store_add(), vcard_store_count(), vcard_store_delete(), and vcard_store_init().
|
static |
Definition at line 37 of file vcard_store.cpp.
Referenced by vcard_store_add(), vcard_store_delete(), vcard_store_get(), vcard_store_get_display(), vcard_store_get_sorted(), vcard_store_init(), and vcard_store_update().
|
static |
Definition at line 38 of file vcard_store.cpp.
Referenced by vcard_store_init().
|
static |
Definition at line 19 of file vcard_store.cpp.
Referenced by vcard_load_own(), vcard_store_clear_own(), and vcard_store_set_own().
|
static |
Definition at line 20 of file vcard_store.cpp.
Referenced by vcard_load_own(), vcard_store_clear_own(), vcard_store_get_display_own(), vcard_store_get_own(), vcard_store_has_own(), and vcard_store_set_own().
|
static |
Definition at line 18 of file vcard_store.cpp.
Referenced by vcard_load_own(), vcard_store_clear_own(), vcard_store_get_display_own(), vcard_store_get_own(), and vcard_store_set_own().
|
static |
Definition at line 13 of file vcard_store.cpp.
|
staticconstexpr |
Definition at line 16 of file vcard_store.cpp.
Referenced by vcard_load_own(), vcard_store_clear_own(), and vcard_store_set_own().
|
staticconstexpr |
Definition at line 15 of file vcard_store.cpp.
Referenced by vcard_load_own(), vcard_store_add(), vcard_store_clear_own(), vcard_store_contains(), vcard_store_delete(), vcard_store_get(), vcard_store_init(), vcard_store_set_own(), and vcard_store_update().