{"id":300,"url":"https://patchwork.libcamera.org/api/patches/300/?format=json","web_url":"https://patchwork.libcamera.org/patch/300/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190121153331.19430-1-niklas.soderlund@ragnatech.se>","date":"2019-01-21T15:33:31","name":"[libcamera-devel,v2] cam: add utility to control cameras","commit_ref":"fba3d0460750fb4123d66f55315dbd9b34cc3d90","pull_url":null,"state":"accepted","archived":false,"hash":"f8cf0a679482a3bd200994e62319b3df4aa09d80","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/300/mbox/","series":[{"id":104,"url":"https://patchwork.libcamera.org/api/series/104/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=104","date":"2019-01-21T15:33:31","name":"[libcamera-devel,v2] cam: add utility to control cameras","version":2,"mbox":"https://patchwork.libcamera.org/series/104/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/300/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/300/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net\n\t[195.74.38.229])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8E15660C78\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 21 Jan 2019 16:33:41 +0100 (CET)","from bismarck.berto.se (unknown [89.233.230.99])\n\tby bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA\n\tid e7ae6dc2-1d91-11e9-9adf-005056917a89;\n\tMon, 21 Jan 2019 16:33:37 +0100 (CET)"],"X-Halon-ID":"e7ae6dc2-1d91-11e9-9adf-005056917a89","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","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","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","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":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 21 Jan 2019 15:33:41 -0000"},"content":"Provide a utility to interact with cameras. This initial state is\nlimited and only supports listing cameras in the system and selecting a\ncamera to interact with.\n\nThere is not much a interacting possible yet with a camera so the tool\nsimply exercise the API to get hold of a camera.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n* Changes since v1\n- Rewrite how options are handled to create a better infrastructure to \n  add more options in the future.\n- Add description for all options.\n- Rebased on latest master.\n- Update printouts to be more descriptive.\n---\n src/cam/cam.cpp     | 144 ++++++++++++++++++++++++++++++++++++++++++++\n src/cam/meson.build |   7 +++\n src/meson.build     |   1 +\n 3 files changed, 152 insertions(+)\n create mode 100644 src/cam/cam.cpp\n create mode 100644 src/cam/meson.build","diff":"diff --git a/src/cam/cam.cpp b/src/cam/cam.cpp\nnew file mode 100644\nindex 0000000000000000..0f795be781069292\n--- /dev/null\n+++ b/src/cam/cam.cpp\n@@ -0,0 +1,144 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * main.cpp - cam-ctl a tool to interact with the library\n+ */\n+\n+#include <getopt.h>\n+#include <iomanip>\n+#include <iostream>\n+#include <map>\n+#include <string.h>\n+\n+#include <libcamera/libcamera.h>\n+\n+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))\n+\n+using namespace std;\n+using namespace libcamera;\n+\n+enum Option {\n+\tOptCamera = 'c',\n+\tOptHelp = 'h',\n+\tOptList = 'l',\n+\tOptLast = 0,\n+};\n+\n+struct OptionInfo {\n+\tOption id;\n+\tconst char *name;\n+\tconst char *arguments;\n+\tconst char *description;\n+};\n+\n+static struct OptionInfo option_info[] = {\n+\t{ OptCamera, \"camera\", \"<camera>\", \"Specify which camera to operate on\" },\n+\t{ OptHelp, \"help\", nullptr, \"Display this help message\" },\n+\t{ OptList, \"list\", nullptr, \"List all cameras\" },\n+\t{ OptLast, nullptr, nullptr, nullptr },\n+};\n+\n+std::map<Option, std::string> options;\n+\n+void usage()\n+{\n+\tstruct OptionInfo *info;\n+\n+\tcout << \"Options:\" << endl;\n+\tfor (info = option_info; info->id != OptLast; info++) {\n+\t\tstring arg(info->name);\n+\n+\t\tif (info->arguments)\n+\t\t\targ += string(\" \") + info->arguments;\n+\n+\t\tcout << \"  -\" << static_cast<char>(info->id) << \" --\" <<\n+\t\t\tsetw(20) << left << arg << \" - \" <<\n+\t\t\tinfo->description << endl;\n+\t}\n+}\n+\n+int parseOptions(int argc, char **argv)\n+{\n+\tchar short_options[ARRAY_SIZE(option_info) * 2 + 1];\n+\tstruct option long_options[ARRAY_SIZE(option_info)];\n+\tstruct OptionInfo *info;\n+\tunsigned ids = 0, idl = 0;\n+\n+\tmemset(short_options, 0, sizeof(short_options));\n+\tmemset(long_options, 0, sizeof(long_options));\n+\n+\tfor (info = option_info; info->id != OptLast; info++) {\n+\t\tshort_options[ids++] = info->id;\n+\t\tif (info->arguments)\n+\t\t\tshort_options[ids++] = ':';\n+\n+\t\tlong_options[idl].name = info->name;\n+\t\tlong_options[idl].has_arg =\n+\t\t\tinfo->arguments ? required_argument : no_argument;\n+\t\tlong_options[idl].flag = 0;\n+\t\tlong_options[idl].val = info->id;\n+\t\tidl++;\n+\t}\n+\n+\twhile (true) {\n+\t\tint c = getopt_long(argc, argv, short_options, long_options, nullptr);\n+\n+\t\tif (c == -1)\n+\t\t\tbreak;\n+\n+\t\tif (!isalpha(c))\n+\t\t\treturn EXIT_FAILURE;\n+\n+\t\toptions[static_cast<Option>(c)] = optarg ? string(optarg) : \"\";\n+\t}\n+\n+\treturn 0;\n+}\n+\n+bool optSet(Option opt)\n+{\n+\treturn options.count(opt) != 0;\n+}\n+\n+int main(int argc, char **argv)\n+{\n+\tint ret;\n+\n+\tret = parseOptions(argc, argv);\n+\tif (ret == EXIT_FAILURE)\n+\t\treturn ret;\n+\n+\tif (argc == 1 || optSet(OptHelp)) {\n+\t\tusage();\n+\t\treturn 0;\n+\t}\n+\n+\tCameraManager *cm = CameraManager::instance();\n+\n+\tret = cm->start();\n+\tif (ret) {\n+\t\tcout << \"Failed to start camera manager: \" << strerror(-ret) << endl;\n+\t\treturn EXIT_FAILURE;\n+\t}\n+\n+\tif (optSet(OptList)) {\n+\t\tcout << \"Available cameras:\" << endl;\n+\t\tfor (const std::shared_ptr<Camera> &camera : cm->cameras())\n+\t\t\tcout << \"- \" << camera->name() << endl;\n+\t}\n+\n+\tif (optSet(OptCamera)) {\n+\t\tstd::shared_ptr<Camera> cam = cm->get(options[OptCamera]);\n+\n+\t\tif (cam) {\n+\t\t\tcout << \"Using camera \" << cam->name() << endl;\n+\t\t} else {\n+\t\t\tcout << \"Camera \" << options[OptCamera] << \" not found\" << endl;\n+\t\t}\n+\t}\n+\n+\tcm->stop();\n+\n+\treturn 0;\n+}\ndiff --git a/src/cam/meson.build b/src/cam/meson.build\nnew file mode 100644\nindex 0000000000000000..809a40e0349236f9\n--- /dev/null\n+++ b/src/cam/meson.build\n@@ -0,0 +1,7 @@\n+cam_sources = files([\n+    'cam.cpp',\n+])\n+\n+cam  = executable('cam', cam_sources,\n+                  link_with : libcamera,\n+                  include_directories : libcamera_includes)\ndiff --git a/src/meson.build b/src/meson.build\nindex 4ce9668caa7b8997..a7f2e75de158cae8 100644\n--- a/src/meson.build\n+++ b/src/meson.build\n@@ -1 +1,2 @@\n subdir('libcamera')\n+subdir('cam')\n","prefixes":["libcamera-devel","v2"]}