13#ifndef USBG_INTERNAL_H
14#define USBG_INTERNAL_H
21#ifdef HAS_GADGET_SCHEMES
22#include "usbg_internal_libconfig.h"
24#include "usbg_internal_none.h"
36#define offsetof(type, member) __builtin_offsetof (type, member)
40#define container_of(ptr, type, field) ({ \
41 const typeof(((type *)0)->field) *member = (ptr); \
42 (type *)( (char *)member - offsetof(type, field) ); \
46#define USBG_MAX_PATH_LENGTH PATH_MAX
48#define USBG_MAX_FILE_SIZE 4096
99 config_t *last_failed_import;
111 config_t *last_failed_import;
161#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
163#define ARRAY_SIZE_SENTINEL(array, size) \
164 static void __attribute__ ((unused)) array##_size_sentinel() \
166 char array##_smaller_than_expected[ \
167 (int)(ARRAY_SIZE(array) - size)] \
168 __attribute__ ((unused)); \
170 char array##_larger_than_expected[ \
171 (int)(size - ARRAY_SIZE(array))] \
172 __attribute__ ((unused)); \
175#define ERROR(msg, ...) do {\
176 fprintf(stderr, "%s() "msg" \n", \
177 __func__, ##__VA_ARGS__);\
181#define ERRORNO(msg, ...) do {\
182 fprintf(stderr, "%s() %s: "msg" \n", \
183 __func__, strerror(errno), ##__VA_ARGS__);\
188#define INSERT_TAILQ_STRING_ORDER(HeadPtr, HeadType, NameField, ToInsert, NodeField) \
190 if (TAILQ_EMPTY((HeadPtr)) || \
191 (strcmp((ToInsert)->NameField, TAILQ_FIRST((HeadPtr))->NameField) < 0)) \
192 TAILQ_INSERT_HEAD((HeadPtr), (ToInsert), NodeField); \
193 else if (strcmp((ToInsert)->NameField, TAILQ_LAST((HeadPtr), HeadType)->NameField) > 0) \
194 TAILQ_INSERT_TAIL((HeadPtr), (ToInsert), NodeField); \
196 typeof(ToInsert) _cur; \
197 TAILQ_FOREACH(_cur, (HeadPtr), NodeField) { \
198 if (strcmp((ToInsert)->NameField, _cur->NameField) > 0) \
200 TAILQ_INSERT_BEFORE(_cur, (ToInsert), NodeField); \
206#define STRINGS_DIR "strings"
207#define CONFIGS_DIR "configs"
208#define FUNCTIONS_DIR "functions"
209#define GADGETS_DIR "usb_gadget"
210#define OS_DESC_DIR "os_desc"
212static inline int file_select(
const struct dirent *dent)
214 if ((strcmp(dent->d_name,
".") == 0) || (strcmp(dent->d_name,
"..") == 0))
220int usbg_translate_error(
int error);
222char *usbg_ether_ntoa_r(
const struct ether_addr *addr,
char *buf);
226int usbg_read_buf(
const char *path,
const char *name,
227 const char *file,
char *buf);
229int usbg_read_buf_limited(
const char *path,
const char *name,
230 const char *file,
char *buf,
int len);
232int usbg_read_int(
const char *path,
const char *name,
const char *file,
233 int base,
int *dest);
235#define usbg_read_dec(p, n, f, d) usbg_read_int(p, n, f, 10, d)
236#define usbg_read_hex(p, n, f, d) usbg_read_int(p, n, f, 16, d)
238int usbg_read_bool(
const char *path,
const char *name,
239 const char *file,
bool *dest);
241int usbg_read_string(
const char *path,
const char *name,
242 const char *file,
char *buf);
244int usbg_read_string_limited(
const char *path,
const char *name,
245 const char *file,
char *buf,
int len);
247int usbg_read_string_alloc(
const char *path,
const char *name,
248 const char *file,
char **dest);
250int usbg_write_buf(
const char *path,
const char *name,
251 const char *file,
const char *buf,
int len);
253int usbg_write_int(
const char *path,
const char *name,
const char *file,
254 int value,
const char *str);
256#define usbg_write_dec(p, n, f, v) usbg_write_int(p, n, f, v, "%d\n")
257#define usbg_write_hex(p, n, f, v) usbg_write_int(p, n, f, v, "0x%x\n")
258#define usbg_write_hex16(p, n, f, v) usbg_write_int(p, n, f, v, "0x%04x\n")
259#define usbg_write_hex8(p, n, f, v) usbg_write_int(p, n, f, v, "0x%02x\n")
260#define usbg_write_bool(p, n, f, v) usbg_write_dec(p, n, f, !!v)
262int usbg_write_string(
const char *path,
const char *name,
263 const char *file,
const char *buf);
265int usbg_rm_file(
const char *path,
const char *name);
267int usbg_rm_dir(
const char *path,
const char *name);
269int usbg_rm_all_dirs(
const char *path);
271int usbg_check_dir(
const char *path);
272#define usbg_config_is_int(node) (config_setting_type(node) == CONFIG_TYPE_INT)
273#define usbg_config_is_string(node) \
274 (config_setting_type(node) == CONFIG_TYPE_STRING)
279 const char *type_name,
280 const char *instance,
286#define GENERIC_ALLOC_INST(prefix, _type, _member) \
287 static int prefix##_alloc_inst(struct usbg_function_type *type, \
288 usbg_function_type type_code, \
289 const char *instance, const char *path, \
290 struct usbg_gadget *parent, \
291 struct usbg_function **f) \
296 ff = malloc(sizeof(*ff)); \
298 return USBG_ERROR_NO_MEM; \
300 ret = usbg_init_function(&ff->_member, type, type_code, \
301 type->name, instance, path, parent); \
302 if (ret != USBG_SUCCESS) \
314#define GENERIC_FREE_INST(prefix, _type, _member) \
315 static void prefix##_free_inst(struct usbg_function_type *type, \
316 struct usbg_function *f) \
318 _type *ff = container_of(f, _type, _member); \
320 usbg_cleanup_function(&ff->_member); \
324typedef int (*usbg_attr_get_func)(
const char *,
const char *,
const char *,
void *);
325typedef int (*usbg_attr_set_func)(
const char *,
const char *,
const char *,
void *);
327static inline int usbg_get_dec(
const char *path,
const char *name,
328 const char *attr,
void *val)
330 return usbg_read_dec(path, name, attr, (
int *)val);
333static inline int usbg_set_dec(
const char *path,
const char *name,
334 const char *attr,
void *val)
336 return usbg_write_dec(path, name, attr, *((
int *)val));
339static inline int usbg_get_bool(
const char *path,
const char *name,
340 const char *attr,
void *val)
342 return usbg_read_bool(path, name, attr, (
bool *)val);
345static inline int usbg_set_bool(
const char *path,
const char *name,
346 const char *attr,
void *val)
348 return usbg_write_bool(path, name, attr, *((
bool *)val));
351static inline int usbg_get_string(
const char *path,
const char *name,
352 const char *attr,
void *val)
354 return usbg_read_string_alloc(path, name, attr, (
char **)val);
357static inline int usbg_set_string(
const char *path,
const char *name,
358 const char *attr,
void *val)
360 return usbg_write_string(path, name, attr, *(
char **)val);
363int usbg_get_ether_addr(
const char *path,
const char *name,
const char *attr,
366int usbg_set_ether_addr(
const char *path,
const char *name,
const char *attr,
369int usbg_get_dev(
const char *path,
const char *name,
const char *attr,
378typedef int (*usbg_import_node_func)(config_setting_t *root,
379 const char *node_name,
void *val);
382typedef int (*usbg_export_node_func)(config_setting_t *root,
383 const char *node_name,
void *val);
Definition usbg_internal.h:143
Definition usbg_internal.h:117
Definition usbg_internal.h:51
Definition usbg_internal.h:129
Definition usbg_internal.h:103
Definition usbg_internal.h:93
Definition usbg_internal.h:153