From fa0acfd45bfca9afdce1f6479fef03b89d1fa9d4 Mon Sep 17 00:00:00 2001 From: Krzysztof Opasiak Date: Sat, 5 Jul 2014 13:29:55 +0200 Subject: [PATCH] libusbgx: examples: Add gadget-import sample app Signed-off-by: Krzysztof Opasiak [Port from libusbg and update description] Signed-off-by: Krzysztof Opasiak --- examples/Makefile.am | 3 +- examples/gadget-import.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ src/usbg.c | 6 ++-- 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 examples/gadget-import.c diff --git a/examples/Makefile.am b/examples/Makefile.am index 310f5b2..9524425 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,8 +1,9 @@ -bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs gadget-export +bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget-vid-pid-remove gadget-ffs gadget-export gadget-import 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 +gadget_import_SOURCE = gadget-import.c AM_CPPFLAGS=-I../include/ AM_LDFLAGS=-L../src/ -lusbgx diff --git a/examples/gadget-import.c b/examples/gadget-import.c new file mode 100644 index 0000000..cf97d9c --- /dev/null +++ b/examples/gadget-import.c @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2014 Samsung Electronics + * + * Krzysztof Opasiak + * + * 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-import.c + * @example gadget-import.c + * This is an example of how to import a gadget from file. + * Common reason of doing this is to create gadget base on schema + * from other devices or resurect gadget after reboot. + */ + +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + usbg_state *s; + usbg_gadget *g; + int ret = -EINVAL; + int usbg_ret; + FILE *input; + + if (argc != 3) { + fprintf(stderr, "Usage: gadget-import gadget_name file_name\n"); + return ret; + } + + /* Prepare input file */ + input = fopen(argv[2], "r"); + if (!input) { + 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; + } + + usbg_ret = usbg_import_gadget(s, input, argv[1], NULL); + if (usbg_ret != USBG_SUCCESS) { + fprintf(stderr, "Error on import gadget\n"); + fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret), + usbg_strerror(usbg_ret)); + if (usbg_ret == USBG_ERROR_INVALID_FORMAT) + fprintf(stderr, "Line: %d. Error: %s\n", + usbg_get_gadget_import_error_line(s), + usbg_get_gadget_import_error_text(s)); + goto out3; + } + + ret = 0; + +out3: + usbg_cleanup(s); +out2: + fclose(input); +out1: + return ret; +} diff --git a/src/usbg.c b/src/usbg.c index 2cc372d..0cc778b 100644 --- a/src/usbg.c +++ b/src/usbg.c @@ -4213,7 +4213,7 @@ const char *usbg_get_func_import_error_text(usbg_gadget *g) return config_error_text(g->last_failed_import); } -const char *usbg_get_func_import_error_line(usbg_gadget *g) +int usbg_get_func_import_error_line(usbg_gadget *g) { if (!g || !g->last_failed_import) return -1; @@ -4229,7 +4229,7 @@ const char *usbg_get_config_import_error_text(usbg_gadget *g) return config_error_text(g->last_failed_import); } -const char *usbg_get_config_import_error_line(usbg_gadget *g) +int usbg_get_config_import_error_line(usbg_gadget *g) { if (!g || !g->last_failed_import) return -1; @@ -4245,7 +4245,7 @@ const char *usbg_get_gadget_import_error_text(usbg_state *s) return config_error_text(s->last_failed_import); } -const char *usbg_get_gadget_import_error_line(usbg_state *s) +int usbg_get_gadget_import_error_line(usbg_state *s) { if (!s || !s->last_failed_import) return -1; -- 2.7.4