mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-28 17:11:23 +02:00
Use TLV for PHY serialization/unserialization.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
31
src/fs/phy.c
31
src/fs/phy.c
@@ -29,6 +29,7 @@ int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) {
|
|||||||
uint8_t *p = data;
|
uint8_t *p = data;
|
||||||
if (phy->vidpid_present) {
|
if (phy->vidpid_present) {
|
||||||
*p++ = PHY_VIDPID;
|
*p++ = PHY_VIDPID;
|
||||||
|
*p++ = 4;
|
||||||
*p++ = phy->vidpid[1];
|
*p++ = phy->vidpid[1];
|
||||||
*p++ = phy->vidpid[0];
|
*p++ = phy->vidpid[0];
|
||||||
*p++ = phy->vidpid[3];
|
*p++ = phy->vidpid[3];
|
||||||
@@ -36,26 +37,32 @@ int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) {
|
|||||||
}
|
}
|
||||||
if (phy->led_gpio_present) {
|
if (phy->led_gpio_present) {
|
||||||
*p++ = PHY_LED_GPIO;
|
*p++ = PHY_LED_GPIO;
|
||||||
|
*p++ = 1;
|
||||||
*p++ = phy->led_gpio;
|
*p++ = phy->led_gpio;
|
||||||
}
|
}
|
||||||
if (phy->led_brightness_present) {
|
if (phy->led_brightness_present) {
|
||||||
*p++ = PHY_LED_BTNESS;
|
*p++ = PHY_LED_BTNESS;
|
||||||
|
*p++ = 1;
|
||||||
*p++ = phy->led_brightness;
|
*p++ = phy->led_brightness;
|
||||||
}
|
}
|
||||||
*p++ = PHY_OPTS;
|
*p++ = PHY_OPTS;
|
||||||
|
*p++ = 2;
|
||||||
p += put_uint16_t_be(phy->opts, p);
|
p += put_uint16_t_be(phy->opts, p);
|
||||||
if (phy->up_btn_present) {
|
if (phy->up_btn_present) {
|
||||||
*p++ = PHY_UP_BTN;
|
*p++ = PHY_UP_BTN;
|
||||||
|
*p++ = 1;
|
||||||
*p++ = phy->up_btn;
|
*p++ = phy->up_btn;
|
||||||
}
|
}
|
||||||
if (phy->usb_product_present) {
|
if (phy->usb_product_present) {
|
||||||
*p++ = PHY_USB_PRODUCT;
|
*p++ = PHY_USB_PRODUCT;
|
||||||
|
*p++ = strlen(phy->usb_product) + 1;
|
||||||
strcpy((char *)p, phy->usb_product);
|
strcpy((char *)p, phy->usb_product);
|
||||||
p += strlen(phy->usb_product);
|
p += strlen(phy->usb_product);
|
||||||
*p++ = '\0';
|
*p++ = '\0';
|
||||||
}
|
}
|
||||||
if (phy->enabled_curves_present) {
|
if (phy->enabled_curves_present) {
|
||||||
*p++ = PHY_ENABLED_CURVES;
|
*p++ = PHY_ENABLED_CURVES;
|
||||||
|
*p++ = 4;
|
||||||
p += put_uint32_t_be(phy->enabled_curves, p);
|
p += put_uint32_t_be(phy->enabled_curves, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,42 +75,62 @@ int phy_unserialize_data(const uint8_t *data, uint16_t len, phy_data_t *phy) {
|
|||||||
return PICOKEY_ERR_NULL_PARAM;
|
return PICOKEY_ERR_NULL_PARAM;
|
||||||
}
|
}
|
||||||
const uint8_t *p = data;
|
const uint8_t *p = data;
|
||||||
|
uint8_t tag, tlen;
|
||||||
while (p < data + len) {
|
while (p < data + len) {
|
||||||
switch (*p++) {
|
tag = *p++;
|
||||||
|
tlen = *p++;
|
||||||
|
switch (tag) {
|
||||||
case PHY_VIDPID:
|
case PHY_VIDPID:
|
||||||
|
if (tlen == 4) {
|
||||||
memcpy(phy->vidpid, p, 4);
|
memcpy(phy->vidpid, p, 4);
|
||||||
phy->vidpid[1] = *p++;
|
phy->vidpid[1] = *p++;
|
||||||
phy->vidpid[0] = *p++;
|
phy->vidpid[0] = *p++;
|
||||||
phy->vidpid[3] = *p++;
|
phy->vidpid[3] = *p++;
|
||||||
phy->vidpid[2] = *p++;
|
phy->vidpid[2] = *p++;
|
||||||
phy->vidpid_present = true;
|
phy->vidpid_present = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PHY_LED_GPIO:
|
case PHY_LED_GPIO:
|
||||||
|
if (tlen == 1) {
|
||||||
phy->led_gpio = *p++;
|
phy->led_gpio = *p++;
|
||||||
phy->led_gpio_present = true;
|
phy->led_gpio_present = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PHY_LED_BTNESS:
|
case PHY_LED_BTNESS:
|
||||||
|
if (tlen == 1) {
|
||||||
phy->led_brightness = *p++;
|
phy->led_brightness = *p++;
|
||||||
phy->led_brightness_present = true;
|
phy->led_brightness_present = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PHY_OPTS:
|
case PHY_OPTS:
|
||||||
|
if (tlen == 2) {
|
||||||
phy->opts = get_uint16_t_be(p);
|
phy->opts = get_uint16_t_be(p);
|
||||||
p += 2;
|
p += 2;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PHY_UP_BTN:
|
case PHY_UP_BTN:
|
||||||
|
if (tlen == 1) {
|
||||||
phy->up_btn = *p++;
|
phy->up_btn = *p++;
|
||||||
phy->up_btn_present = true;
|
phy->up_btn_present = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PHY_USB_PRODUCT:
|
case PHY_USB_PRODUCT:
|
||||||
|
if (tlen > 0 && tlen <= sizeof(phy->usb_product)) {
|
||||||
memset(phy->usb_product, 0, sizeof(phy->usb_product));
|
memset(phy->usb_product, 0, sizeof(phy->usb_product));
|
||||||
strlcpy(phy->usb_product, (const char *)p, sizeof(phy->usb_product));
|
strlcpy(phy->usb_product, (const char *)p, sizeof(phy->usb_product));
|
||||||
phy->usb_product_present = true;
|
phy->usb_product_present = true;
|
||||||
p += strlen(phy->usb_product) + 1;
|
p += strlen(phy->usb_product) + 1;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PHY_ENABLED_CURVES:
|
case PHY_ENABLED_CURVES:
|
||||||
|
if (tlen == 4) {
|
||||||
phy->enabled_curves = get_uint32_t_be(p);
|
phy->enabled_curves = get_uint32_t_be(p);
|
||||||
p += sizeof(uint32_t);
|
p += 4;
|
||||||
phy->enabled_curves_present = true;
|
phy->enabled_curves_present = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
p += tlen;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ typedef struct phy_data {
|
|||||||
|
|
||||||
} phy_data_t;
|
} phy_data_t;
|
||||||
|
|
||||||
#define PHY_MAX_SIZE sizeof(phy_data_t)
|
#define PHY_MAX_SIZE ((2+4)+(2+4)+(2+32)+(2+2)+(2+1)+(2+1)+(2+1))
|
||||||
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
extern int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len);
|
extern int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len);
|
||||||
|
|||||||
Reference in New Issue
Block a user