[libcamera-devel,v2] cam: add utility to control cameras

Message ID 20190121153331.19430-1-niklas.soderlund@ragnatech.se
State Accepted
Commit fba3d0460750fb4123d66f55315dbd9b34cc3d90
Headers show
Series
  • [libcamera-devel,v2] cam: add utility to control cameras
Related show

Commit Message

Niklas Söderlund Jan. 21, 2019, 3:33 p.m. UTC
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 <niklas.soderlund@ragnatech.se>
---
* 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

Patch

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 <getopt.h>
+#include <iomanip>
+#include <iostream>
+#include <map>
+#include <string.h>
+
+#include <libcamera/libcamera.h>
+
+#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", "<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<Option, std::string> 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<char>(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<Option>(c)] = optarg ? string(optarg) : "";
+	}
+
+	return 0;
+}
+
+bool optSet(Option opt)
+{
+	return options.count(opt) != 0;
+}
+
+int main(int argc, char **argv)
+{
+	int ret;
+
+	ret = parseOptions(argc, argv);
+	if (ret == EXIT_FAILURE)
+		return ret;
+
+	if (argc == 1 || optSet(OptHelp)) {
+		usage();
+		return 0;
+	}
+
+	CameraManager *cm = CameraManager::instance();
+
+	ret = cm->start();
+	if (ret) {
+		cout << "Failed to start camera manager: " << strerror(-ret) << endl;
+		return EXIT_FAILURE;
+	}
+
+	if (optSet(OptList)) {
+		cout << "Available cameras:" << endl;
+		for (const std::shared_ptr<Camera> &camera : cm->cameras())
+			cout << "- " << camera->name() << endl;
+	}
+
+	if (optSet(OptCamera)) {
+		std::shared_ptr<Camera> cam = cm->get(options[OptCamera]);
+
+		if (cam) {
+			cout << "Using camera " << cam->name() << endl;
+		} else {
+			cout << "Camera " << options[OptCamera] << " not found" << endl;
+		}
+	}
+
+	cm->stop();
+
+	return 0;
+}
diff --git a/src/cam/meson.build b/src/cam/meson.build
new file mode 100644
index 0000000000000000..809a40e0349236f9
--- /dev/null
+++ b/src/cam/meson.build
@@ -0,0 +1,7 @@ 
+cam_sources = files([
+    'cam.cpp',
+])
+
+cam  = executable('cam', cam_sources,
+                  link_with : libcamera,
+                  include_directories : libcamera_includes)
diff --git a/src/meson.build b/src/meson.build
index 4ce9668caa7b8997..a7f2e75de158cae8 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1 +1,2 @@ 
 subdir('libcamera')
+subdir('cam')