From f3696579716afc2895f06a8691b42e9f3e87fc45 Mon Sep 17 00:00:00 2001 From: tr4m0ryp Date: Sun, 5 Apr 2026 01:55:57 +0200 Subject: [PATCH] Task 002: Utility modules (log, plist_helpers, usb_helpers) Implemented three foundational utility modules: - log: leveled logging with ANSI colors to stderr - plist_helpers: libplist convenience wrappers for dict access and XML serialization (renamed plist_dict_get_bool_val/plist_dict_get_uint_val to avoid collision with libplist 2.x native symbols plist_dict_get_bool/plist_dict_get_uint) - usb_helpers: libusb control transfer wrappers for DFU operations Files created: - include/util/log.h, src/util/log.c - include/util/plist_helpers.h, src/util/plist_helpers.c - include/util/usb_helpers.h, src/util/usb_helpers.c Co-Authored-By: Claude Opus 4.6 (1M context) --- include/util/log.h | 17 ++++++++ include/util/plist_helpers.h | 40 ++++++++++++++++++ include/util/usb_helpers.h | 37 +++++++++++++++++ src/util/log.c | 58 ++++++++++++++++++++++++++ src/util/plist_helpers.c | 79 ++++++++++++++++++++++++++++++++++++ src/util/usb_helpers.c | 45 ++++++++++++++++++++ 6 files changed, 276 insertions(+) create mode 100644 include/util/log.h create mode 100644 include/util/plist_helpers.h create mode 100644 include/util/usb_helpers.h create mode 100644 src/util/log.c create mode 100644 src/util/plist_helpers.c create mode 100644 src/util/usb_helpers.c diff --git a/include/util/log.h b/include/util/log.h new file mode 100644 index 0000000..f51451a --- /dev/null +++ b/include/util/log.h @@ -0,0 +1,17 @@ +#ifndef LOG_H +#define LOG_H + +typedef enum { + LOG_DEBUG = 0, + LOG_INFO, + LOG_WARN, + LOG_ERROR +} log_level_t; + +void log_set_level(log_level_t level); +void log_debug(const char *fmt, ...); +void log_info(const char *fmt, ...); +void log_warn(const char *fmt, ...); +void log_error(const char *fmt, ...); + +#endif /* LOG_H */ diff --git a/include/util/plist_helpers.h b/include/util/plist_helpers.h new file mode 100644 index 0000000..35e0dc2 --- /dev/null +++ b/include/util/plist_helpers.h @@ -0,0 +1,40 @@ +#ifndef PLIST_HELPERS_H +#define PLIST_HELPERS_H + +#include +#include + +/* + * Retrieve a string value from a plist dict by key. + * Returns a malloc'd copy the caller must free, or NULL on error. + */ +char *plist_dict_get_string(plist_t dict, const char *key); + +/* + * Retrieve a boolean value from a plist dict by key. + * Stores result in *out (1=true, 0=false). + * Returns 0 on success, -1 on error. + */ +int plist_dict_get_bool_val(plist_t dict, const char *key, int *out); + +/* + * Retrieve an unsigned 64-bit integer value from a plist dict by key. + * Stores result in *out. + * Returns 0 on success, -1 on error. + */ +int plist_dict_get_uint_val(plist_t dict, const char *key, uint64_t *out); + +/* + * Serialize a plist to an XML string. + * *len is set to the byte length (excluding NUL). + * Returns a malloc'd string the caller must free, or NULL on error. + */ +char *plist_to_xml_string(plist_t plist, uint32_t *len); + +/* + * Parse an XML plist from a buffer. + * Returns a new plist_t the caller must free with plist_free(), or NULL on error. + */ +plist_t plist_from_xml_string(const char *xml, uint32_t len); + +#endif /* PLIST_HELPERS_H */ diff --git a/include/util/usb_helpers.h b/include/util/usb_helpers.h new file mode 100644 index 0000000..30b6710 --- /dev/null +++ b/include/util/usb_helpers.h @@ -0,0 +1,37 @@ +#ifndef USB_HELPERS_H +#define USB_HELPERS_H + +#include +#include + +/* + * Perform a USB control transfer with a data stage. + * Returns number of bytes transferred on success, negative libusb error code + * on failure. + */ +int usb_ctrl_transfer(libusb_device_handle *dev, + uint8_t bmRequestType, + uint8_t bRequest, + uint16_t wValue, + uint16_t wIndex, + unsigned char *data, + uint16_t wLength, + unsigned int timeout); + +/* + * Perform a USB control transfer with no data stage (wLength=0). + * Returns 0 on success, negative libusb error code on failure. + */ +int usb_ctrl_transfer_no_data(libusb_device_handle *dev, + uint8_t bmRequestType, + uint8_t bRequest, + uint16_t wValue, + uint16_t wIndex, + unsigned int timeout); + +/* + * Print a human-readable description of a libusb error code to stderr. + */ +void usb_print_error(int libusb_error); + +#endif /* USB_HELPERS_H */ diff --git a/src/util/log.c b/src/util/log.c new file mode 100644 index 0000000..67d45b8 --- /dev/null +++ b/src/util/log.c @@ -0,0 +1,58 @@ +#include +#include +#include "util/log.h" + +#define ANSI_RESET "\033[0m" +#define ANSI_WHITE "\033[37m" +#define ANSI_GREEN "\033[32m" +#define ANSI_YELLOW "\033[33m" +#define ANSI_RED "\033[31m" + +static log_level_t g_level = LOG_INFO; + +void log_set_level(log_level_t level) +{ + g_level = level; +} + +static void log_vprint(log_level_t level, const char *color, + const char *tag, const char *fmt, va_list ap) +{ + if (level < g_level) + return; + fprintf(stderr, "%s%s ", color, tag); + vfprintf(stderr, fmt, ap); + fprintf(stderr, "%s\n", ANSI_RESET); +} + +void log_debug(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + log_vprint(LOG_DEBUG, ANSI_WHITE, "[DEBUG]", fmt, ap); + va_end(ap); +} + +void log_info(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + log_vprint(LOG_INFO, ANSI_GREEN, "[INFO]", fmt, ap); + va_end(ap); +} + +void log_warn(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + log_vprint(LOG_WARN, ANSI_YELLOW, "[WARN]", fmt, ap); + va_end(ap); +} + +void log_error(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + log_vprint(LOG_ERROR, ANSI_RED, "[ERROR]", fmt, ap); + va_end(ap); +} diff --git a/src/util/plist_helpers.c b/src/util/plist_helpers.c new file mode 100644 index 0000000..0e7fb45 --- /dev/null +++ b/src/util/plist_helpers.c @@ -0,0 +1,79 @@ +#include +#include +#include "util/plist_helpers.h" + +char *plist_dict_get_string(plist_t dict, const char *key) +{ + plist_t node; + char *val = NULL; + + if (!dict || !key) + return NULL; + + node = plist_dict_get_item(dict, key); + if (!node || plist_get_node_type(node) != PLIST_STRING) + return NULL; + + plist_get_string_val(node, &val); + return val; /* libplist malloc's this; caller frees */ +} + +int plist_dict_get_bool_val(plist_t dict, const char *key, int *out) +{ + plist_t node; + uint8_t b = 0; + + if (!dict || !key || !out) + return -1; + + node = plist_dict_get_item(dict, key); + if (!node || plist_get_node_type(node) != PLIST_BOOLEAN) + return -1; + + plist_get_bool_val(node, &b); + *out = (int)b; + return 0; +} + +int plist_dict_get_uint_val(plist_t dict, const char *key, uint64_t *out) +{ + plist_t node; + + if (!dict || !key || !out) + return -1; + + node = plist_dict_get_item(dict, key); + if (!node || plist_get_node_type(node) != PLIST_UINT) + return -1; + + plist_get_uint_val(node, out); + return 0; +} + +char *plist_to_xml_string(plist_t plist, uint32_t *len) +{ + char *xml = NULL; + uint32_t xml_len = 0; + + if (!plist) + return NULL; + + plist_to_xml(plist, &xml, &xml_len); + if (!xml) + return NULL; + + if (len) + *len = xml_len; + return xml; +} + +plist_t plist_from_xml_string(const char *xml, uint32_t len) +{ + plist_t result = NULL; + + if (!xml || len == 0) + return NULL; + + plist_from_xml(xml, len, &result); + return result; +} diff --git a/src/util/usb_helpers.c b/src/util/usb_helpers.c new file mode 100644 index 0000000..8f0554c --- /dev/null +++ b/src/util/usb_helpers.c @@ -0,0 +1,45 @@ +#include +#include "util/usb_helpers.h" + +int usb_ctrl_transfer(libusb_device_handle *dev, + uint8_t bmRequestType, + uint8_t bRequest, + uint16_t wValue, + uint16_t wIndex, + unsigned char *data, + uint16_t wLength, + unsigned int timeout) +{ + int ret; + + if (!dev) + return LIBUSB_ERROR_INVALID_PARAM; + + ret = libusb_control_transfer(dev, bmRequestType, bRequest, + wValue, wIndex, data, wLength, timeout); + return ret; +} + +int usb_ctrl_transfer_no_data(libusb_device_handle *dev, + uint8_t bmRequestType, + uint8_t bRequest, + uint16_t wValue, + uint16_t wIndex, + unsigned int timeout) +{ + int ret; + + if (!dev) + return LIBUSB_ERROR_INVALID_PARAM; + + ret = libusb_control_transfer(dev, bmRequestType, bRequest, + wValue, wIndex, NULL, 0, timeout); + /* libusb returns 0 for transfers with no data stage on success */ + return (ret < 0) ? ret : 0; +} + +void usb_print_error(int libusb_error) +{ + fprintf(stderr, "[usb] error %d: %s\n", + libusb_error, libusb_strerror((enum libusb_error)libusb_error)); +}