Introducing libmapper
libmapper is a convenience library for sdbus applications
that interact with the phosphor object manager.
Change-Id: I5a638e0a313d575591055214cb3ac5701b730ae5
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/libmapper/.gitignore b/libmapper/.gitignore
new file mode 100644
index 0000000..216823b
--- /dev/null
+++ b/libmapper/.gitignore
@@ -0,0 +1,3 @@
+*.so
+*.so.*
+*.o
diff --git a/libmapper/Makefile b/libmapper/Makefile
new file mode 100644
index 0000000..806390c
--- /dev/null
+++ b/libmapper/Makefile
@@ -0,0 +1,33 @@
+libdir=/usr/lib
+includedir=/usr/include
+
+PACKAGE_DEPS=libsystemd
+SONAME=libmapper.so
+VERSION=1
+LIBMAPPER=$(SONAME).$(VERSION)
+INCLUDES=mapper.h
+
+LDLIBS+=$(shell pkg-config --libs $(PACKAGE_DEPS))
+ALL_CFLAGS+=$(shell pkg-config --cflags $(PACKAGE_DEPS)) -fPIC -Werror $(CFLAGS)
+
+all: $(SONAME)
+
+%.o: %.c
+ $(CC) -c $(ALL_CFLAGS) -o $@ $<
+
+$(SONAME): $(LIBMAPPER)
+ ln -sf $^ $@
+
+$(LIBMAPPER): lib%.so.$(VERSION): %.o
+ $(CC) -shared $(CFLAGS) $(LDFLAGS) -Wl,-soname,$(SONAME) \
+ -o $@ $^ $(LDLIBS)
+
+install: $(SONAME) $(LIBMAPPER)
+ @mkdir -p $(DESTDIR)$(includedir)
+ install $(INCLUDES) $(DESTDIR)$(includedir)
+ @mkdir -p $(DESTDIR)$(libdir)
+ install $(LIBMAPPER) $(DESTDIR)$(libdir)
+ ln -sf $(LIBMAPPER) $(DESTDIR)$(libdir)/$(SONAME)
+
+clean:
+ rm -f *.o $(LIBMAPPER) $(SONAME)
diff --git a/libmapper/mapper.c b/libmapper/mapper.c
new file mode 100644
index 0000000..62dd2e6
--- /dev/null
+++ b/libmapper/mapper.c
@@ -0,0 +1,65 @@
+/**
+ * Copyright © 2016 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <string.h>
+#include <systemd/sd-bus.h>
+#include "mapper.h"
+
+int mapper_get_service(sd_bus *conn, const char *obj, char **service)
+{
+ sd_bus_error error = SD_BUS_ERROR_NULL;
+ sd_bus_message *request = NULL, *reply = NULL;
+ const char *tmp;
+ int r;
+
+ r = sd_bus_message_new_method_call(
+ conn,
+ &request,
+ "org.openbmc.ObjectMapper",
+ "/org/openbmc/ObjectMapper",
+ "org.openbmc.ObjectMapper",
+ "GetObject");
+ if (r < 0)
+ goto exit;
+
+ r = sd_bus_message_append(request, "s", obj);
+ if (r < 0)
+ goto exit;
+
+ r = sd_bus_call(conn, request, 0, &error, &reply);
+ if (r < 0)
+ goto exit;
+
+ r = sd_bus_message_enter_container(reply, 0, NULL);
+ if (r < 0)
+ goto exit;
+
+ r = sd_bus_message_enter_container(reply, 0, NULL);
+ if (r < 0)
+ goto exit;
+
+ r = sd_bus_message_read(reply, "s", &tmp);
+ if (r < 0)
+ goto exit;
+
+ *service = strdup(tmp);
+
+exit:
+ sd_bus_error_free(&error);
+ sd_bus_message_unref(request);
+ sd_bus_message_unref(reply);
+
+ return r;
+}
diff --git a/libmapper/mapper.h b/libmapper/mapper.h
new file mode 100644
index 0000000..58f1844
--- /dev/null
+++ b/libmapper/mapper.h
@@ -0,0 +1,9 @@
+#include <systemd/sd-bus.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+int mapper_get_service(sd_bus *conn, const char *obj, char **service);
+#ifdef __cplusplus
+}
+#endif