1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef USBG_INTERNAL_H
#define USBG_INTERNAL_H
#include <sys/queue.h>
#include <string.h>
#include <usbg/usbg.h>
#include <malloc.h>
#include <sys/types.h>
#ifdef HAS_LIBCONFIG
#include <libconfig.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef HAS_LIBCONFIG
typedef struct _should_not_be_used config_t;
typedef struct _should_not_be_used config_setting_t;
void config_destroy(config_t *config);
#endif
/**
* @file include/usbg/usbg_internal.h
*/
#ifndef offsetof
#define offsetof(type, member) __builtin_offsetof (type, member)
#endif /* offsetof */
#ifndef container_of
#define container_of(ptr, type, field) ({ \
const typeof(((type *)0)->field) *member = (ptr); \
(type *)( (char *)member - offsetof(type, field) ); \
})
#endif /* container_of */
#define USBG_MAX_PATH_LENGTH PATH_MAX
/* ConfigFS just like SysFS uses page size as max size of file content */
#define USBG_MAX_FILE_SIZE 4096
struct usbg_function_type
{
/* Name of this function type */
char *name;
/* Called to allocate instance of function */
int (*alloc_inst)(struct usbg_function_type *, usbg_function_type,
const char *, const char *, usbg_gadget *,
struct usbg_function **);
/* Called to free memory used by function */
void (*free_inst)(struct usbg_function_type *, struct usbg_function *);
/*
* Called when user would like to remove this function.
* If this callback is provided it will be always called
* before rmdir on function directory. This function
* should check received flags and remove composed function
* attributes (directories) only if USBG_RM_RECURSE flag
* has been passed.
*/
int (*remove)(struct usbg_function *, int);
/* Set the value of all given attributes */
int (*set_attrs)(struct usbg_function *, void *);
/* Get the value of all function attributes */
int (*get_attrs)(struct usbg_function *, void *);
/* Free the additional memory allocated for function attributes */
void (*cleanup_attrs)(struct usbg_function *, void *);
/* Should import all function attributes from libconfig format */
int (*import)(struct usbg_function *, config_setting_t *);
/* Should export all functions attributes to libconfig format */
int (*export)(struct usbg_function *, config_setting_t *);
};
struct usbg_state
{
char *path;
char *configfs_path;
TAILQ_HEAD(ghead, usbg_gadget) gadgets;
TAILQ_HEAD(uhead, usbg_udc) udcs;
config_t *last_failed_import;
};
struct usbg_gadget
{
char *name;
char *path;
TAILQ_ENTRY(usbg_gadget) gnode;
TAILQ_HEAD(chead, usbg_config) configs;
TAILQ_HEAD(fhead, usbg_function) functions;
usbg_state *parent;
config_t *last_failed_import;
usbg_udc *udc;
};
struct usbg_config
{
TAILQ_ENTRY(usbg_config) cnode;
TAILQ_HEAD(bhead, usbg_binding) bindings;
usbg_gadget *parent;
char *name;
char *path;
char *label;
int id;
};
struct usbg_function
{
TAILQ_ENTRY(usbg_function) fnode;
usbg_gadget *parent;
char *name;
char *path;
char *instance;
/* Only for internal library usage */
char *label;
usbg_function_type type;
struct usbg_function_type *ops;
};
struct usbg_binding
{
TAILQ_ENTRY(usbg_binding) bnode;
usbg_config *parent;
usbg_function *target;
char *name;
char *path;
};
struct usbg_udc
{
TAILQ_ENTRY(usbg_udc) unode;
usbg_state *parent;
usbg_gadget *gadget;
char *name;
};
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
#define ARRAY_SIZE_SENTINEL(array, size) \
static void __attribute__ ((unused)) array##_size_sentinel() \
{ \
char array##_smaller_than_expected[ \
(int)(ARRAY_SIZE(array) - size)] \
__attribute__ ((unused)); \
\
char array##_larger_than_expected[ \
(int)(size - ARRAY_SIZE(array))] \
__attribute__ ((unused)); \
}
#define ERROR(msg, ...) do {\
fprintf(stderr, "%s() "msg" \n", \
__func__, ##__VA_ARGS__);\
fflush(stderr);\
} while (0)
#define ERRORNO(msg, ...) do {\
fprintf(stderr, "%s() %s: "msg" \n", \
__func__, strerror(errno), ##__VA_ARGS__);\
fflush(stderr);\
} while (0)
/* Insert in string order */
#define INSERT_TAILQ_STRING_ORDER(HeadPtr, HeadType, NameField, ToInsert, NodeField) \
do { \
if (TAILQ_EMPTY((HeadPtr)) || \
(strcmp((ToInsert)->NameField, TAILQ_FIRST((HeadPtr))->NameField) < 0)) \
TAILQ_INSERT_HEAD((HeadPtr), (ToInsert), NodeField); \
else if (strcmp((ToInsert)->NameField, TAILQ_LAST((HeadPtr), HeadType)->NameField) > 0) \
TAILQ_INSERT_TAIL((HeadPtr), (ToInsert), NodeField); \
else { \
typeof(ToInsert) _cur; \
TAILQ_FOREACH(_cur, (HeadPtr), NodeField) { \
if (strcmp((ToInsert)->NameField, _cur->NameField) > 0) \
continue; \
TAILQ_INSERT_BEFORE(_cur, (ToInsert), NodeField); \
break; \
} \
} \
} while (0)
#define STRINGS_DIR "strings"
#define CONFIGS_DIR "configs"
#define FUNCTIONS_DIR "functions"
#define GADGETS_DIR "usb_gadget"
static inline int file_select(const struct dirent *dent)
{
if ((strcmp(dent->d_name, ".") == 0) || (strcmp(dent->d_name, "..") == 0))
return 0;
else
return 1;
}
int usbg_translate_error(int error);
char *usbg_ether_ntoa_r(const struct ether_addr *addr, char *buf);
int usbg_read_buf(const char *path, const char *name,
const char *file, char *buf);
int usbg_read_buf_limited(const char *path, const char *name,
const char *file, char *buf, int len);
int usbg_read_int(const char *path, const char *name, const char *file,
int base, int *dest);
#define usbg_read_dec(p, n, f, d) usbg_read_int(p, n, f, 10, d)
#define usbg_read_hex(p, n, f, d) usbg_read_int(p, n, f, 16, d)
int usbg_read_bool(const char *path, const char *name,
const char *file, bool *dest);
int usbg_read_string(const char *path, const char *name,
const char *file, char *buf);
int usbg_read_string_limited(const char *path, const char *name,
const char *file, char *buf, int len);
int usbg_read_string_alloc(const char *path, const char *name,
const char *file, char **dest);
int usbg_write_buf(const char *path, const char *name,
const char *file, const char *buf, int len);
int usbg_write_int(const char *path, const char *name, const char *file,
int value, const char *str);
#define usbg_write_dec(p, n, f, v) usbg_write_int(p, n, f, v, "%d\n")
#define usbg_write_hex(p, n, f, v) usbg_write_int(p, n, f, v, "0x%x\n")
#define usbg_write_hex16(p, n, f, v) usbg_write_int(p, n, f, v, "0x%04x\n")
#define usbg_write_hex8(p, n, f, v) usbg_write_int(p, n, f, v, "0x%02x\n")
#define usbg_write_bool(p, n, f, v) usbg_write_dec(p, n, f, !!v)
int usbg_write_string(const char *path, const char *name,
const char *file, const char *buf);
int ubsg_rm_file(const char *path, const char *name);
int usbg_rm_dir(const char *path, const char *name);
int usbg_rm_all_dirs(const char *path);
int usbg_check_dir(const char *path);
#define usbg_config_is_int(node) (config_setting_type(node) == CONFIG_TYPE_INT)
#define usbg_config_is_string(node) \
(config_setting_type(node) == CONFIG_TYPE_STRING)
int usbg_init_function(struct usbg_function *f,
struct usbg_function_type *ops,
usbg_function_type type,
const char *type_name,
const char *instance,
const char *path,
struct usbg_gadget *parent);
void usbg_cleanup_function(struct usbg_function *f);
#define GENERIC_ALLOC_INST(prefix, _type, _member) \
static int prefix##_alloc_inst(struct usbg_function_type *type, \
usbg_function_type type_code, \
const char *instance, const char *path, \
struct usbg_gadget *parent, \
struct usbg_function **f) \
{ \
_type *ff; \
int ret; \
\
ff = malloc(sizeof(*ff)); \
if (!ff) \
return USBG_ERROR_NO_MEM; \
\
ret = usbg_init_function(&ff->_member, type, type_code, \
type->name, instance, path, parent); \
if (ret != USBG_SUCCESS) \
goto free_func; \
\
*f = &ff->_member; \
\
return ret; \
\
free_func: \
free(ff); \
return ret; \
}
#define GENERIC_FREE_INST(prefix, _type, _member) \
static void prefix##_free_inst(struct usbg_function_type *type, \
struct usbg_function *f) \
{ \
_type *ff = container_of(f, _type, _member); \
\
usbg_cleanup_function(&ff->_member); \
free(ff); \
}
typedef int (*usbg_attr_get_func)(const char *, const char *, const char *, void *);
typedef int (*usbg_attr_set_func)(const char *, const char *, const char *, void *);
static inline int usbg_get_dec(const char *path, const char *name,
const char *attr, void *val)
{
return usbg_read_dec(path, name, attr, (int *)val);
}
static inline int usbg_set_dec(const char *path, const char *name,
const char *attr, void *val)
{
return usbg_write_dec(path, name, attr, *((int *)val));
}
static inline int usbg_get_bool(const char *path, const char *name,
const char *attr, void *val)
{
return usbg_read_bool(path, name, attr, (bool *)val);
}
static inline int usbg_set_bool(const char *path, const char *name,
const char *attr, void *val)
{
return usbg_write_bool(path, name, attr, *((bool *)val));
}
static inline int usbg_get_string(const char *path, const char *name,
const char *attr, void *val)
{
return usbg_read_string_alloc(path, name, attr, (char **)val);
}
static inline int usbg_set_string(const char *path, const char *name,
const char *attr, void *val)
{
return usbg_write_string(path, name, attr, *(char **)val);
}
int usbg_get_ether_addr(const char *path, const char *name, const char *attr,
void *val);
int usbg_set_ether_addr(const char *path, const char *name, const char *attr,
void *val);
int usbg_get_dev(const char *path, const char *name, const char *attr,
void *val);
/*
* return:
* 0 - if not found
* usbg_error on error (less than 0)
* above 0 when found suitable value
*/
typedef int (*usbg_import_node_func)(config_setting_t *root,
const char *node_name, void *val);
/* return 0 on success, usbg_error otherwise */
typedef int (*usbg_export_node_func)(config_setting_t *root,
const char *node_name, void *val);
int usbg_get_config_node_int(config_setting_t *root,
const char *node_name, void *val);
int usbg_get_config_node_bool(config_setting_t *root,
const char *node_name, void *val);
int usbg_get_config_node_string(config_setting_t *root,
const char *node_name, void *val);
int usbg_get_config_node_ether_addr(config_setting_t *root,
const char *node_name, void *val);
int usbg_set_config_node_int(config_setting_t *root,
const char *node_name, void *val);
int usbg_set_config_node_int_hex(config_setting_t *root,
const char *node_name, void *val);
int usbg_set_config_node_bool(config_setting_t *root,
const char *node_name, void *val);
int usbg_set_config_node_string(config_setting_t *root,
const char *node_name, void *val);
int usbg_set_config_node_ether_addr(config_setting_t *root,
const char *node_name, void *val);
int usbg_set_config_node_dev(config_setting_t *root,
const char *node_name, void *val);
#ifdef __cplusplus
}
#endif
#endif /* USBG_INTERNAL_H */
|