CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_vcard.cpp
Go to the documentation of this file.
1
10
14#include "cdc_log.h"
15
16#include <cstring>
17
18namespace pm = cdc::plugin_manager;
19
20extern "C" void* plg_get_active_plugin(void);
21
22namespace {
23
24bool vcard_allowed() {
25 auto* p = static_cast<pm::Plugin*>(plg_get_active_plugin());
26 return p && p->manifest().capabilities.vcard;
27}
28
29// Resolve a 0-based sorted position to a store slot; returns -1 on a bad index.
30int resolve_sorted_slot(uint16_t index) {
31 uint16_t slots[VCARD_MAX_CARDS];
32 uint16_t n = vcard_store_get_sorted(slots, VCARD_MAX_CARDS);
33 if (index >= n) return -1;
34 return slots[index];
35}
36
37} // namespace
38
39extern "C" {
40
41int host_vcard_get_own(char* out, size_t out_size) {
42 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
43 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
44 size_t n = vcard_store_get_own(out, out_size);
45 return n > 0 ? static_cast<int>(n) : HOST_ERR_NOT_FOUND;
46}
47
49 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
50 return vcard_store_count();
51}
52
53int host_vcard_received_get(uint16_t index, char* out, size_t out_size) {
54 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
55 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
56 int slot = resolve_sorted_slot(index);
57 if (slot < 0) return HOST_ERR_NOT_FOUND;
58 size_t n = vcard_store_get(static_cast<uint16_t>(slot), out, out_size);
59 return n > 0 ? static_cast<int>(n) : HOST_ERR_NOT_FOUND;
60}
61
62int host_vcard_received_display(uint16_t index, char* out, size_t out_size) {
63 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
64 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
65 int slot = resolve_sorted_slot(index);
66 if (slot < 0) return HOST_ERR_NOT_FOUND;
67 if (!vcard_store_get_display(static_cast<uint16_t>(slot), out, out_size)) {
68 return HOST_ERR_NOT_FOUND;
69 }
70 return static_cast<int>(std::strlen(out));
71}
72
73int host_vcard_set_own(const char* vcard, size_t len) {
74 if (!vcard || len == 0 || len >= HOST_VCARD_MAX_LEN) return HOST_ERR_INVALID_ARG;
75 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
76 char err[64] = {};
77 if (!vcard_store_set_own(vcard, len, err, sizeof(err))) {
78 LOG_W("PLG_VCARD", "set_own rejected: %s", err);
80 }
81 return HOST_OK;
82}
83
84int host_vcard_received_add(const char* vcard, size_t len) {
85 if (!vcard || len == 0 || len >= HOST_VCARD_MAX_LEN) return HOST_ERR_INVALID_ARG;
86 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
88 if (vcard_store_contains(vcard, len)) return HOST_ERR_BUSY;
89 char err[64] = {};
90 if (!vcard_store_add(vcard, len, err, sizeof(err))) {
91 LOG_W("PLG_VCARD", "add rejected: %s", err);
93 }
94 return HOST_OK;
95}
96
97int host_vcard_received_update(uint16_t index, const char* vcard, size_t len) {
98 if (!vcard || len == 0 || len >= HOST_VCARD_MAX_LEN) return HOST_ERR_INVALID_ARG;
99 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
100 int slot = resolve_sorted_slot(index);
101 if (slot < 0) return HOST_ERR_NOT_FOUND;
102 char err[64] = {};
103 if (!vcard_store_update(static_cast<uint16_t>(slot), vcard, len, err, sizeof(err))) {
104 LOG_W("PLG_VCARD", "update rejected: %s", err);
106 }
107 return HOST_OK;
108}
109
110int host_vcard_received_delete(uint16_t index) {
111 if (!vcard_allowed()) return HOST_ERR_NO_CAPABILITY;
112 int slot = resolve_sorted_slot(index);
113 if (slot < 0) return HOST_ERR_NOT_FOUND;
114 return vcard_store_delete(static_cast<uint16_t>(slot)) ? HOST_OK : HOST_ERR_GENERIC;
115}
116
117} // extern "C"
Owned WAMR module instance + per-plugin state.
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
Definition cdc_log.h:146
int host_vcard_received_count(void)
Number of received vCards in the store.
int host_vcard_received_add(const char *vcard, size_t len)
Store a new received vCard.
int host_vcard_received_display(uint16_t index, char *out, size_t out_size)
Copy the display name of the received vCard at sorted position index into out (for list rows).
int host_vcard_set_own(const char *vcard, size_t len)
Set / replace the badge's own vCard.
#define HOST_VCARD_MAX_LEN
Maximum length of a single vCard text including the NUL.
Definition host_api.h:2010
int host_vcard_received_update(uint16_t index, const char *vcard, size_t len)
Overwrite the received vCard at sorted position index.
int host_vcard_get_own(char *out, size_t out_size)
Copy the badge's own vCard text into out.
int host_vcard_received_delete(uint16_t index)
Delete the received vCard at sorted position index.
int host_vcard_received_get(uint16_t index, char *out, size_t out_size)
Copy the received vCard at sorted position index into out.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_ERR_NO_CAPABILITY
Definition host_api.h:40
#define HOST_OK
Definition host_api.h:37
#define HOST_ERR_INVALID_ARG
Definition host_api.h:39
#define HOST_ERR_NO_MEMORY
Definition host_api.h:43
#define HOST_ERR_NOT_FOUND
Definition host_api.h:41
#define HOST_ERR_GENERIC
Definition host_api.h:38
#define HOST_ERR_BUSY
Definition host_api.h:44
void * plg_get_active_plugin(void)
void * plg_get_active_plugin(void)
size_t vcard_store_get_own(char *out, size_t max_len)
Retrieves local own-vCard text.
#define VCARD_MAX_CARDS
Definition vcard_store.h:7
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.
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.
size_t vcard_store_get(uint16_t slot, char *out, size_t max_len)
Retrieves raw vCard text from slot.
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.
uint16_t vcard_store_get_sorted(uint16_t *out_slots, uint16_t max_slots)
Returns slot indices of stored cards sorted by last name.
uint16_t vcard_store_count(void)
Returns number of stored peer vCards.
bool vcard_store_contains(const char *vcard, size_t len)
Reports whether an exact-text vCard is already stored.
bool vcard_store_get_display(uint16_t slot, char *out, size_t max_len)
Retrieves cached display label for slot.
bool vcard_store_delete(uint16_t slot)
Deletes peer vCard at slot index.