File: ms.h

package info (click to toggle)
libusbgx 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 992 kB
  • sloc: ansic: 13,134; xml: 176; makefile: 73; sh: 64; cpp: 14
file content (422 lines) | stat: -rw-r--r-- 13,285 bytes parent folder | download | duplicates (2)
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
421
422
/*
 * 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_FUNCTION_MS__
#define USBG_FUNCTION_MS__

#include <usbg/usbg.h>

#include <malloc.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct usbg_f_ms usbg_f_ms;

struct usbg_f_ms_lun_attrs {
	int id;
	bool cdrom;
	bool ro;
	bool nofua;
	bool removable;
	const char *file;
	const char *inquiry_string;
};

struct usbg_f_ms_attrs {
	bool stall;
	int nluns;
	struct usbg_f_ms_lun_attrs **luns;
};

enum usbg_f_ms_lun_attr {
	USBG_F_MS_LUN_ATTR_MIN = 0,
	USBG_F_MS_LUN_CDROM = USBG_F_MS_LUN_ATTR_MIN,
	USBG_F_MS_LUN_RO,
	USBG_F_MS_LUN_NOFUA,
	USBG_F_MS_LUN_REMOVABLE,
	USBG_F_MS_LUN_FILE,
	USBG_F_MS_LUN_INQUIRY_STRING,
	USBG_F_MS_LUN_ATTR_MAX
};

union usbg_f_ms_lun_attr_val {
	bool cdrom;
	bool ro;
	bool nofua;
	bool removable;
	const char *file;
	const char *inquiry_string;
};

#define USBG_F_MS_LUN_BOOL_TO_ATTR_VAL(WHAT)		\
	USBG_TO_UNION(usbg_f_ms_lun_attr_val, cdrom, WHAT)

#define USBG_F_MS_LUN_CCHAR_PTR_TO_ATTR_VAL(WHAT)		\
	USBG_TO_UNION(usbg_f_ms_lun_attr_val, file, WHAT)



/**
 * @brief Cast from generic function to mass storage function
 * @param[in] f function to be converted to ms funciton.
 *         Should be one of types:
 *         ecm, subset, ncm, eem, rndis
 * @return Converted ms function or NULL if function hasn't suitable type
 */
usbg_f_ms *usbg_to_ms_function(usbg_function *f);

/**
 * @brief Cast form ms function to generic one
 * @param[in] mf function to be converted to generic one
 * @return Generic usbg function
 */
usbg_function *usbg_from_ms_function(usbg_f_ms *mf);

/**
 * @brief Create new LUN in choosen funcion
 * @note LUN ids should be contiguous (so id for new LUN should be
 *         current value nluns)
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun to be created
 * @param[in] lattrs LUN attributes to be set (may be NULL to use defaults)
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_create_lun(usbg_f_ms *mf, int lun_id,
			 struct usbg_f_ms_lun_attrs *lattrs);

/**
 * @brief Remove LUN from choosen funcion
 * @note LUN ids should be contiguous (so you should remove LUN with id equal
 *        to nluns - 1)
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun to be created
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_rm_lun(usbg_f_ms *mf, int lun_id);

/**
 * @brief Get attributes of choosen LUN
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] lattrs Structure to be filled with data
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_get_lun_attrs(usbg_f_ms *mf, int lun_id,
			struct usbg_f_ms_lun_attrs *lattrs);

/**
 * @brief Set attributes of choosen LUN
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] lattrs Lun attributes to be set
 * @return 0 on success usbg_error if error occurred.
 * @note The id value in lattrs is ignored by this function
 */
int usbg_f_ms_set_lun_attrs(usbg_f_ms *mf, int lun_id,
			    const struct usbg_f_ms_lun_attrs *lattrs);

/**
 * @brief Cleanup LUN attributes structure after usage
 * @param[in] attrs to be cleaned up
 */
static inline void usbg_f_ms_cleanup_lun_attrs(struct usbg_f_ms_lun_attrs *lattrs)
{
	if (lattrs) {
		free((char *)lattrs->file);
		lattrs->file = NULL;
		free((char *)lattrs->inquiry_string);
		lattrs->inquiry_string = NULL;
	}
}

/**
 * @brief Get the value of single LUN attribute
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] lattr Code of attribute which value should be taken
 * @param[out] val Current value of this attribute
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_get_lun_attr_val(usbg_f_ms *mf, int lun_id,
			       enum usbg_f_ms_lun_attr lattr,
			       union usbg_f_ms_lun_attr_val *val);

/**
 * @brief Set the value of single LUN attribute
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] lattr Code of attribute which value should be set
 * @param[in] val Value of attribute which should be set
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_set_lun_attr_val(usbg_f_ms *mf, int lun_id,
			       enum usbg_f_ms_lun_attr lattr,
			       const union usbg_f_ms_lun_attr_val val);

/**
 * @brief Get the value which determines if lun is visible as a cdrom
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] cdrom Current value of stall attribute
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_get_lun_cdrom(usbg_f_ms *mf, int lun_id,
					  bool *cdrom)
{
	return usbg_f_ms_get_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_CDROM,
					  (union usbg_f_ms_lun_attr_val *)cdrom);
}

/**
 * @brief Set the value which determines if lun is visible as a cdrom
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] cdrom Value of stall attribute which should be set
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_set_lun_cdrom(usbg_f_ms *mf, int lun_id,
					  bool cdrom)
{
	return usbg_f_ms_set_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_CDROM,
					  USBG_F_MS_LUN_BOOL_TO_ATTR_VAL(cdrom));
}

/**
 * @brief Get the value which determines if lun is read only
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] ro Indicates if LUN is read only
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_get_lun_ro(usbg_f_ms *mf, int lun_id, bool *ro)
{
	return usbg_f_ms_get_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_RO,
					  (union usbg_f_ms_lun_attr_val *)ro);
}

/**
 * @brief Set the value which determines if lun read only
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] ro True if LUN should be read only
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_set_lun_ro(usbg_f_ms *mf, int lun_id, bool ro)
{
	return usbg_f_ms_set_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_RO,
					  USBG_F_MS_LUN_BOOL_TO_ATTR_VAL(ro));
}

/**
 * @brief Get the value which determines if lun should ignore
 *        the FUA (Force Unit Access) flag in SCSI commands
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] nofua Indicates if FUA bit is ignored
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_get_lun_nofua(usbg_f_ms *mf, int lun_id,
					  bool *nofua)
{
	return usbg_f_ms_get_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_NOFUA,
					  (union usbg_f_ms_lun_attr_val *)nofua);
}

/**
 * @brief Set the value which determines if lun should ignore
 *        the FUA (Force Unit Access) flag in SCSI commands
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] nofua True if LUN should ignore FUA bit
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_set_lun_nofua(usbg_f_ms *mf, int lun_id,
				bool nofua)
{
	return usbg_f_ms_set_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_NOFUA,
					  USBG_F_MS_LUN_BOOL_TO_ATTR_VAL(nofua));
}

/**
 * @brief Get the value which determines if lun is removable
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] removable Indicates if LUN is removable
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_get_lun_removable(usbg_f_ms *mf, int lun_id,
				bool *removable)
{
	return usbg_f_ms_get_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_REMOVABLE,
					  (union usbg_f_ms_lun_attr_val *)removable);
}

/**
 * @brief Set the value which determines if lun removable
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] removable True if LUN should be removable
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_set_lun_removable(usbg_f_ms *mf, int lun_id,
				bool removable)
{
	return usbg_f_ms_set_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_REMOVABLE,
					  USBG_F_MS_LUN_BOOL_TO_ATTR_VAL(removable));
}

/**
 * @brief Get the name of file which is used as backend storage by this LUN
 *         into newly allocated storage
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] file Newly allocated string name of file used as backend
 *         storage
 * @return 0 on success usbg_error if error occurred.
 * @note returned file should be free() by caller
 */
static inline int usbg_f_ms_get_lun_file(usbg_f_ms *mf, int lun_id,
			       char **file)
{
	return usbg_f_ms_get_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_FILE,
					  (union usbg_f_ms_lun_attr_val *)file);
}

/**
 * @brief Get the name of file which is used as backend storage by this LUN
 *         into user buffer
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] buf Place where file should be stored
 * @param[in] len Size of buffer
 * @return Number of bytes placed into buf (excluding '\0') or the number of
 *         characters which would have been written to the buffer if enough
 *         space had been available. (just like snprintf() family). This may
 *         also return negative error code from usbg_error.
 */
int usbg_f_ms_get_lun_file_s(usbg_f_ms *mf, int lun_id,
			     char *buf, int len);

/**
 * @brief Set the name of file which is used as backend storage by this LUN
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] file Name of file which should be used as backend storage
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_set_lun_file(usbg_f_ms *mf, int lun_id,
			   const char *file)
{
	return usbg_f_ms_set_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_FILE,
					  USBG_F_MS_LUN_CCHAR_PTR_TO_ATTR_VAL(file));
}

/**
 * @brief Get the inquiry string of LUN which is used as name in SCSI inquiry
 *         by this LUN into newly allocated storage
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[out] inquiry_string Newly allocated string name of LUN used in SCSI
 *         inquiry
 * @return 0 on success usbg_error if error occurred.
 * @note returned inquiry_string should be free() by caller
 */
static inline int usbg_f_ms_get_lun_inquiry_string(usbg_f_ms *mf, int lun_id,
			       char **inquiry_string)
{
	return usbg_f_ms_get_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_INQUIRY_STRING,
					  (union usbg_f_ms_lun_attr_val *)inquiry_string);
}

/**
 * @brief Set the inquiry string which is used as name in SCSI inquiry by this LUN
 * @param[in] mf Pointer to ms function
 * @param[in] lun_id ID of lun
 * @param[in] inquiry_string Name of LUN which used in SCSI inquiry
 * @return 0 on success usbg_error if error occurred.
 */
static inline int usbg_f_ms_set_lun_inquiry_string(usbg_f_ms *mf, int lun_id,
			   const char *inquiry_string)
{
	return usbg_f_ms_set_lun_attr_val(mf, lun_id, USBG_F_MS_LUN_INQUIRY_STRING,
					  USBG_F_MS_LUN_CCHAR_PTR_TO_ATTR_VAL(inquiry_string));
}

/**
 * @brief Get attributes of given ms function
 * @param[in] mf Pointer to ms function
 * @param[out] attrs Structure to be filled with data
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_get_attrs(usbg_f_ms *mf, struct usbg_f_ms_attrs *attrs);

/**
 * @brief Set attributes of given ms function
 * @param[in] mf Pointer to ms function
 * @param[in] attrs to be set
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_set_attrs(usbg_f_ms *mf, const struct usbg_f_ms_attrs *attrs);

/**
 * @brief Cleanup attributes structure after usage
 * @param[in] attrs to be cleaned up
 */
static inline void usbg_f_ms_cleanup_attrs(struct usbg_f_ms_attrs *attrs)
{
	int i;

	if (!attrs)
		return;

	for (i = 0; i < attrs->nluns; ++i) {
		if (!attrs->luns[i])
			continue;

		usbg_f_ms_cleanup_lun_attrs(attrs->luns[i]);
		free(attrs->luns[i]);
	}
	free(attrs->luns);
	attrs->luns = NULL;
}

/**
 * @brief Get the value of device side MAC address
 * @param[in] mf Pointer to ms function
 * @param[out] stall Current value of stall attribute
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_get_stall(usbg_f_ms *mf, bool *stall);

/**
 * @brief Set the value of device side MAC address
 * @param[in] mf Pointer to ms function
 * @param[in] stall Value of stall attribute which should be set
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_set_stall(usbg_f_ms *mf, bool stall);

/**
 * @brief Get the value of device side MAC address
 * @param[in] mf Pointer to ms function
 * @param[out] nluns Current number of LUNs
 * @return 0 on success usbg_error if error occurred.
 */
int usbg_f_ms_get_nluns(usbg_f_ms *mf, int *nluns);

#ifdef __cplusplus
}
#endif

#endif /* USBG_FUNCTION_MS__ */