From c7e4fd778b5d06cdf25d6174c378898e6283cf43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 11 Aug 2019 19:36:45 +0200 Subject: [PATCH] CLI discover Subcommand --- cli/CMakeLists.txt | 10 ++++-- cli/include/chiaki-cli.h | 33 ++++++++++++++++++ cli/src/discover.c | 74 ++++++++++++++++++++++++++++++++++++++++ cli/src/main.c | 34 ++++++++++++++++-- 4 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 cli/include/chiaki-cli.h create mode 100644 cli/src/discover.c diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 333de6d..b44d0f5 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -1,5 +1,11 @@ set(SOURCE - src/main.c) + include/chiaki-cli.h + src/discover.c) -add_executable(chiaki_cli ${SOURCE}) +add_library(chiaki-cli-lib STATIC ${SOURCE}) +target_include_directories(chiaki-cli-lib PUBLIC "include") +target_link_libraries(chiaki-cli-lib chiaki-lib) + +add_executable(chiaki-cli src/main.c) +target_link_libraries(chiaki-cli chiaki-cli-lib) diff --git a/cli/include/chiaki-cli.h b/cli/include/chiaki-cli.h new file mode 100644 index 0000000..0f34104 --- /dev/null +++ b/cli/include/chiaki-cli.h @@ -0,0 +1,33 @@ +/* + * This file is part of Chiaki. + * + * Chiaki 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 3 of the License, or + * (at your option) any later version. + * + * Chiaki 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. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_CHIAKI_CLI_H +#define CHIAKI_CHIAKI_CLI_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +CHIAKI_EXPORT int chiaki_cli_cmd_discover(int argc, char *argv[]); + +#ifdef __cplusplus +} +#endif + +#endif //CHIAKI_CHIAKI_CLI_H diff --git a/cli/src/discover.c b/cli/src/discover.c new file mode 100644 index 0000000..76aaec8 --- /dev/null +++ b/cli/src/discover.c @@ -0,0 +1,74 @@ +/* + * This file is part of Chiaki. + * + * Chiaki 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 3 of the License, or + * (at your option) any later version. + * + * Chiaki 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. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include + +#include + +#include + +static char doc[] = "Send a PS4 discovery request."; + +static struct argp_option options[] = { + { "ip", 'i', "IP", 0, "IP to send discovery request to", 0 }, + { 0 } +}; + +typedef struct arguments +{ + char *ip; +} Arguments; + +static int parse_opt(int key, char *arg, struct argp_state *state) +{ + Arguments *arguments = state->input; + + switch(key) + { + case 'i': + arguments->ip = arg; + break; + case ARGP_KEY_ARG: + argp_usage(state); + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +static struct argp argp = { options, parse_opt, 0, doc, 0, 0, 0 }; + +CHIAKI_EXPORT int chiaki_cli_cmd_discover(int argc, char *argv[]) +{ + Arguments arguments = { 0 }; + error_t r = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &arguments); + if(r != 0) + return 1; + + if(!arguments.ip) + { + fprintf(stderr, "ip for discovery is not set.\n"); + return 1; + } + + // TODO + printf("run discovery\n"); + + return 0; +} \ No newline at end of file diff --git a/cli/src/main.c b/cli/src/main.c index 0e75462..8e1f708 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -15,25 +15,53 @@ * along with Chiaki. If not, see . */ +#include + #include +#include #include +#include static const char doc[] = "CLI for Chiaki (PS4 Remote Play Client)" "\v" "Supported commands are:\n" - " discover Discover Consoled.\n"; + " discover Discover Consoles.\n"; static struct argp_option options[] = { { 0 } }; +static int call_subcmd(struct argp_state *state, const char *name, int (*subcmd)(int argc, char *argv[])) +{ + if(state->next < 1 || state->argc < state->next) + return 1; + + int argc = state->argc - state->next + 1; + char **argv = &state->argv[state->next - 1]; + + size_t l = strlen(state->name) + strlen(name) + 2; + argv[0] = malloc(l); + if(!argv[0]) + return 1; + snprintf(argv[0], l, "%s %s", state->name, name); + + int r = subcmd(argc, argv); + + free(argv[0]); + return r; +} + static int parse_opt(int key, char *arg, struct argp_state *state) { switch(key) { case ARGP_KEY_ARG: + if(strcmp(arg, "discover") == 0) + exit(call_subcmd(state, "discover", chiaki_cli_cmd_discover)); + // fallthrough + case ARGP_KEY_END: argp_usage(state); break; default: @@ -42,11 +70,11 @@ static int parse_opt(int key, char *arg, struct argp_state *state) return 0; } -static struct argp argp = { options, parse_opt, "[ [CMD-OPTION...]]", doc, 0, 0, 0 }; +static struct argp argp = { options, parse_opt, " [CMD-ARGS...]", doc, 0, 0, 0 }; int main(int argc, char *argv[]) { - argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, 0); + argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, NULL); return 0; }