Use TLV for PHY serialization/unserialization.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
parent
7191cda6d3
commit
ef9b66f990
2 changed files with 50 additions and 23 deletions
71
src/fs/phy.c
71
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;
|
||||
if (phy->vidpid_present) {
|
||||
*p++ = PHY_VIDPID;
|
||||
*p++ = 4;
|
||||
*p++ = phy->vidpid[1];
|
||||
*p++ = phy->vidpid[0];
|
||||
*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) {
|
||||
*p++ = PHY_LED_GPIO;
|
||||
*p++ = 1;
|
||||
*p++ = phy->led_gpio;
|
||||
}
|
||||
if (phy->led_brightness_present) {
|
||||
*p++ = PHY_LED_BTNESS;
|
||||
*p++ = 1;
|
||||
*p++ = phy->led_brightness;
|
||||
}
|
||||
*p++ = PHY_OPTS;
|
||||
*p++ = 2;
|
||||
p += put_uint16_t_be(phy->opts, p);
|
||||
if (phy->up_btn_present) {
|
||||
*p++ = PHY_UP_BTN;
|
||||
*p++ = 1;
|
||||
*p++ = phy->up_btn;
|
||||
}
|
||||
if (phy->usb_product_present) {
|
||||
*p++ = PHY_USB_PRODUCT;
|
||||
*p++ = strlen(phy->usb_product) + 1;
|
||||
strcpy((char *)p, phy->usb_product);
|
||||
p += strlen(phy->usb_product);
|
||||
*p++ = '\0';
|
||||
}
|
||||
if (phy->enabled_curves_present) {
|
||||
*p++ = PHY_ENABLED_CURVES;
|
||||
*p++ = 4;
|
||||
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;
|
||||
}
|
||||
const uint8_t *p = data;
|
||||
uint8_t tag, tlen;
|
||||
while (p < data + len) {
|
||||
switch (*p++) {
|
||||
tag = *p++;
|
||||
tlen = *p++;
|
||||
switch (tag) {
|
||||
case PHY_VIDPID:
|
||||
memcpy(phy->vidpid, p, 4);
|
||||
phy->vidpid[1] = *p++;
|
||||
phy->vidpid[0] = *p++;
|
||||
phy->vidpid[3] = *p++;
|
||||
phy->vidpid[2] = *p++;
|
||||
phy->vidpid_present = true;
|
||||
if (tlen == 4) {
|
||||
memcpy(phy->vidpid, p, 4);
|
||||
phy->vidpid[1] = *p++;
|
||||
phy->vidpid[0] = *p++;
|
||||
phy->vidpid[3] = *p++;
|
||||
phy->vidpid[2] = *p++;
|
||||
phy->vidpid_present = true;
|
||||
}
|
||||
break;
|
||||
case PHY_LED_GPIO:
|
||||
phy->led_gpio = *p++;
|
||||
phy->led_gpio_present = true;
|
||||
if (tlen == 1) {
|
||||
phy->led_gpio = *p++;
|
||||
phy->led_gpio_present = true;
|
||||
}
|
||||
break;
|
||||
case PHY_LED_BTNESS:
|
||||
phy->led_brightness = *p++;
|
||||
phy->led_brightness_present = true;
|
||||
if (tlen == 1) {
|
||||
phy->led_brightness = *p++;
|
||||
phy->led_brightness_present = true;
|
||||
}
|
||||
break;
|
||||
case PHY_OPTS:
|
||||
phy->opts = get_uint16_t_be(p);
|
||||
p += 2;
|
||||
if (tlen == 2) {
|
||||
phy->opts = get_uint16_t_be(p);
|
||||
p += 2;
|
||||
}
|
||||
break;
|
||||
case PHY_UP_BTN:
|
||||
phy->up_btn = *p++;
|
||||
phy->up_btn_present = true;
|
||||
if (tlen == 1) {
|
||||
phy->up_btn = *p++;
|
||||
phy->up_btn_present = true;
|
||||
}
|
||||
break;
|
||||
case PHY_USB_PRODUCT:
|
||||
memset(phy->usb_product, 0, sizeof(phy->usb_product));
|
||||
strlcpy(phy->usb_product, (const char *)p, sizeof(phy->usb_product));
|
||||
phy->usb_product_present = true;
|
||||
p += strlen(phy->usb_product) + 1;
|
||||
if (tlen > 0 && tlen <= sizeof(phy->usb_product)) {
|
||||
memset(phy->usb_product, 0, sizeof(phy->usb_product));
|
||||
strlcpy(phy->usb_product, (const char *)p, sizeof(phy->usb_product));
|
||||
phy->usb_product_present = true;
|
||||
p += strlen(phy->usb_product) + 1;
|
||||
}
|
||||
break;
|
||||
case PHY_ENABLED_CURVES:
|
||||
phy->enabled_curves = get_uint32_t_be(p);
|
||||
p += sizeof(uint32_t);
|
||||
phy->enabled_curves_present = true;
|
||||
if (tlen == 4) {
|
||||
phy->enabled_curves = get_uint32_t_be(p);
|
||||
p += 4;
|
||||
phy->enabled_curves_present = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
p += tlen;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ typedef struct phy_data {
|
|||
|
||||
} 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
|
||||
extern int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue