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_read_buf_alloc(
const char *path,
const char *name,
251 const char *file,
char **dest,
int len);
253int usbg_write_buf(
const char *path,
const char *name,
254 const char *file,
const char *buf,
int len);
256int usbg_write_int(
const char *path,
const char *name,
const char *file,
257 int value,
const char *str);
259#define usbg_write_dec(p, n, f, v) usbg_write_int(p, n, f, v, "%d\n")
260#define usbg_write_hex(p, n, f, v) usbg_write_int(p, n, f, v, "0x%x\n")
261#define usbg_write_hex16(p, n, f, v) usbg_write_int(p, n, f, v, "0x%04x\n")
262#define usbg_write_hex8(p, n, f, v) usbg_write_int(p, n, f, v, "0x%02x\n")
263#define usbg_write_bool(p, n, f, v) usbg_write_dec(p, n, f, !!v)
265int usbg_write_string(
const char *path,
const char *name,
266 const char *file,
const char *buf);
268int usbg_rm_file(
const char *path,
const char *name);
270int usbg_rm_dir(
const char *path,
const char *name);
272int usbg_rm_all_dirs(
const char *path);
274int usbg_check_dir(
const char *path);
275#define usbg_config_is_int(node) (config_setting_type(node) == CONFIG_TYPE_INT)
276#define usbg_config_is_string(node) \
277 (config_setting_type(node) == CONFIG_TYPE_STRING)
282 const char *type_name,
283 const char *instance,
289#define GENERIC_ALLOC_INST(prefix, _type, _member) \
290 static int prefix##_alloc_inst(struct usbg_function_type *type, \
291 usbg_function_type type_code, \
292 const char *instance, const char *path, \
293 struct usbg_gadget *parent, \
294 struct usbg_function **f) \
299 ff = malloc(sizeof(*ff)); \
301 return USBG_ERROR_NO_MEM; \
303 ret = usbg_init_function(&ff->_member, type, type_code, \
304 type->name, instance, path, parent); \
305 if (ret != USBG_SUCCESS) \
317#define GENERIC_FREE_INST(prefix, _type, _member) \
318 static void prefix##_free_inst(struct usbg_function_type *type, \
319 struct usbg_function *f) \
321 _type *ff = container_of(f, _type, _member); \
323 usbg_cleanup_function(&ff->_member); \
327typedef int (*usbg_attr_get_func)(
const char *,
const char *,
const char *,
void *);
328typedef int (*usbg_attr_set_func)(
const char *,
const char *,
const char *,
void *);
330static inline int usbg_get_dec(
const char *path,
const char *name,
331 const char *attr,
void *val)
333 return usbg_read_dec(path, name, attr, (
int *)val);
336static inline int usbg_set_dec(
const char *path,
const char *name,
337 const char *attr,
void *val)
339 return usbg_write_dec(path, name, attr, *((
int *)val));
342static inline int usbg_get_bool(
const char *path,
const char *name,
343 const char *attr,
void *val)
345 return usbg_read_bool(path, name, attr, (
bool *)val);
348static inline int usbg_set_bool(
const char *path,
const char *name,
349 const char *attr,
void *val)
351 return usbg_write_bool(path, name, attr, *((
bool *)val));
354static inline int usbg_get_string(
const char *path,
const char *name,
355 const char *attr,
void *val)
357 return usbg_read_string_alloc(path, name, attr, (
char **)val);
360static inline int usbg_set_string(
const char *path,
const char *name,
361 const char *attr,
void *val)
363 return usbg_write_string(path, name, attr, *(
char **)val);
366int usbg_get_ether_addr(
const char *path,
const char *name,
const char *attr,
369int usbg_set_ether_addr(
const char *path,
const char *name,
const char *attr,
372int usbg_get_dev(
const char *path,
const char *name,
const char *attr,
375int usbg_write_guid(
const char *path,
const char *name,
376 const char *file,
const char *buf);
384typedef int (*usbg_import_node_func)(config_setting_t *root,
385 const char *node_name,
void *val);
388typedef int (*usbg_export_node_func)(config_setting_t *root,
389 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