From patchwork Mon Jan 21 15:33:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 300 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8E15660C78 for ; Mon, 21 Jan 2019 16:33:41 +0100 (CET) X-Halon-ID: e7ae6dc2-1d91-11e9-9adf-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (unknown [89.233.230.99]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id e7ae6dc2-1d91-11e9-9adf-005056917a89; Mon, 21 Jan 2019 16:33:37 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 21 Jan 2019 16:33:31 +0100 Message-Id: <20190121153331.19430-1-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2] cam: add utility to control cameras X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Jan 2019 15:33:41 -0000 Provide a utility to interact with cameras. This initial state is limited and only supports listing cameras in the system and selecting a camera to interact with. There is not much a interacting possible yet with a camera so the tool simply exercise the API to get hold of a camera. Signed-off-by: Niklas Söderlund --- * Changes since v1 - Rewrite how options are handled to create a better infrastructure to add more options in the future. - Add description for all options. - Rebased on latest master. - Update printouts to be more descriptive. --- src/cam/cam.cpp | 144 ++++++++++++++++++++++++++++++++++++++++++++ src/cam/meson.build | 7 +++ src/meson.build | 1 + 3 files changed, 152 insertions(+) create mode 100644 src/cam/cam.cpp create mode 100644 src/cam/meson.build diff --git a/src/cam/cam.cpp b/src/cam/cam.cpp new file mode 100644 index 0000000000000000..0f795be781069292 --- /dev/null +++ b/src/cam/cam.cpp @@ -0,0 +1,144 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * main.cpp - cam-ctl a tool to interact with the library + */ + +#include +#include +#include +#include +#include + +#include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +using namespace std; +using namespace libcamera; + +enum Option { + OptCamera = 'c', + OptHelp = 'h', + OptList = 'l', + OptLast = 0, +}; + +struct OptionInfo { + Option id; + const char *name; + const char *arguments; + const char *description; +}; + +static struct OptionInfo option_info[] = { + { OptCamera, "camera", "", "Specify which camera to operate on" }, + { OptHelp, "help", nullptr, "Display this help message" }, + { OptList, "list", nullptr, "List all cameras" }, + { OptLast, nullptr, nullptr, nullptr }, +}; + +std::map options; + +void usage() +{ + struct OptionInfo *info; + + cout << "Options:" << endl; + for (info = option_info; info->id != OptLast; info++) { + string arg(info->name); + + if (info->arguments) + arg += string(" ") + info->arguments; + + cout << " -" << static_cast(info->id) << " --" << + setw(20) << left << arg << " - " << + info->description << endl; + } +} + +int parseOptions(int argc, char **argv) +{ + char short_options[ARRAY_SIZE(option_info) * 2 + 1]; + struct option long_options[ARRAY_SIZE(option_info)]; + struct OptionInfo *info; + unsigned ids = 0, idl = 0; + + memset(short_options, 0, sizeof(short_options)); + memset(long_options, 0, sizeof(long_options)); + + for (info = option_info; info->id != OptLast; info++) { + short_options[ids++] = info->id; + if (info->arguments) + short_options[ids++] = ':'; + + long_options[idl].name = info->name; + long_options[idl].has_arg = + info->arguments ? required_argument : no_argument; + long_options[idl].flag = 0; + long_options[idl].val = info->id; + idl++; + } + + while (true) { + int c = getopt_long(argc, argv, short_options, long_options, nullptr); + + if (c == -1) + break; + + if (!isalpha(c)) + return EXIT_FAILURE; + + options[static_cast