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) <noreply@anthropic.com>
This commit is contained in:
parent
85caaefa9c
commit
f369657971
6 changed files with 276 additions and 0 deletions
17
include/util/log.h
Normal file
17
include/util/log.h
Normal file
|
|
@ -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 */
|
||||
40
include/util/plist_helpers.h
Normal file
40
include/util/plist_helpers.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#ifndef PLIST_HELPERS_H
|
||||
#define PLIST_HELPERS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <plist/plist.h>
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
37
include/util/usb_helpers.h
Normal file
37
include/util/usb_helpers.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef USB_HELPERS_H
|
||||
#define USB_HELPERS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <libusb-1.0/libusb.h>
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue