diff options
author | Krzysztof Opasiak <k.opasiak@samsung.com> | 2014-07-05 12:48:18 +0200 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2014-08-25 10:42:09 +0200 |
commit | 0c450f97276aa73834d6aa4c72976798d13a0469 (patch) | |
tree | 68a223a3564fae6c0bf81bd654df94631d1d80e5 /examples | |
parent | d8fad2dabdcf091b4183a3103ab4f7dfe40ecf9d (diff) | |
download | libusbg-0c450f97276aa73834d6aa4c72976798d13a0469.tar.gz libusbg-0c450f97276aa73834d6aa4c72976798d13a0469.tar.bz2 libusbg-0c450f97276aa73834d6aa4c72976798d13a0469.zip |
libusbg: examples: Add sample application to export gadget
Add sample C code which shows how to use new functionality
of libusbg - gadget export. This program allows to export
chosen gadget from configfs to a file.
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 3 | ||||
-rw-r--r-- | examples/gadget-export.c | 81 |
2 files changed, 83 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index abafe3c..aeb0273 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,7 +1,8 @@ -bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs +bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs gadget-export gadget_acm_ecm_SOURCES = gadget-acm-ecm.c show_gadgets_SOURCES = show-gadgets.c gadget_vid_pid_remove_SOURCES = gadget-vid-pid-remove.c gadget_ffs_SOURCES = gadget-ffs.c +gadget_export_SOURCE = gadget-export.c AM_CPPFLAGS=-I../include/ AM_LDFLAGS=-L../src/ -lusbg diff --git a/examples/gadget-export.c b/examples/gadget-export.c new file mode 100644 index 0000000..9d51e9e --- /dev/null +++ b/examples/gadget-export.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2014 Samsung Electronics + * + * Krzysztof Opasiak <k.opasiak@samsung.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + */ + +/** + * @file gadget-export.c + * @example gadget-export.c + * This is an example of how to export a gadget to file. + * Common reason of doing this is to share schema of gadget + * between different devices or preserve gadget between reboots. + */ + +#include <errno.h> +#include <string.h> +#include <stdio.h> +#include <usbg/usbg.h> + +int main(int argc, char **argv) +{ + usbg_state *s; + usbg_gadget *g; + int ret = -EINVAL; + int usbg_ret; + FILE *output; + + if (argc != 3) { + fprintf(stderr, "Usage: gadget-export gadget_name file_name\n"); + return ret; + } + + /* Prepare output file */ + output = fopen(argv[2], "w"); + if (!output) { + fprintf(stderr, "Error on fopen. Error: %s\n", strerror(errno)); + goto out1; + } + + /* Do gadget exporting */ + usbg_ret = usbg_init("/sys/kernel/config", &s); + if (usbg_ret != USBG_SUCCESS) { + fprintf(stderr, "Error on USB gadget init\n"); + fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret), + usbg_strerror(usbg_ret)); + goto out2; + } + + g = usbg_get_gadget(s, argv[1]); + if (!g) { + fprintf(stderr, "Error on get gadget\n"); + goto out3; + } + + usbg_ret = usbg_export_gadget(g, output); + if (usbg_ret != USBG_SUCCESS) { + fprintf(stderr, "Error on export gadget\n"); + fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret), + usbg_strerror(usbg_ret)); + goto out3; + } + + ret = 0; + +out3: + usbg_cleanup(s); +out2: + fclose(output); +out1: + return ret; +} |