Remove libmapper dependency

In the latest openbmc repo, there is no need to rely on libmapper.
This patch is to remove the dependence on libmapper, and then
libmapper will be deleted in the future and mapper calls will be used
directly.

Signed-off-by: George Liu <liuxiwei@ieisystem.com>
Change-Id: Ibe9c345e760e5a4215a2fe3c3c2ace3c54f8e179
diff --git a/op-pwrctl/pgood_wait/Makefile b/op-pwrctl/pgood_wait/Makefile
index c54382e..67678cc 100644
--- a/op-pwrctl/pgood_wait/Makefile
+++ b/op-pwrctl/pgood_wait/Makefile
@@ -1,5 +1,4 @@
 BINS=pgood_wait
 BIN_SUFFIX=""
-LDLIBS=-lmapper
 include ../../sdbus.mk
 include ../../rules.mk
diff --git a/op-pwrctl/pgood_wait/pgood_wait.c b/op-pwrctl/pgood_wait/pgood_wait.c
index 949776d..e7ddb8c 100644
--- a/op-pwrctl/pgood_wait/pgood_wait.c
+++ b/op-pwrctl/pgood_wait/pgood_wait.c
@@ -18,7 +18,6 @@
 #include <errno.h>
 #include <systemd/sd-bus.h>
 #include <systemd/sd-event.h>
-#include <mapper.h>
 
 static void quit(int r, void *loop)
 {
@@ -32,7 +31,8 @@
 	char *property = NULL;
 
 	r = sd_bus_message_skip(m, "s");
-	if (r < 0) {
+	if (r < 0)
+	{
 		fprintf(stderr, "Error skipping message fields: %s\n",
 				strerror(-r));
 		quit(r, loop);
@@ -40,26 +40,29 @@
 	}
 
 	r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
-	if (r < 0) {
+	if (r < 0)
+	{
 		fprintf(stderr, "Error entering container: %s\n",
 				strerror(-r));
 		quit(r, loop);
 		return r;
 	}
 
-	while((r = sd_bus_message_enter_container(
-					m,
-					SD_BUS_TYPE_DICT_ENTRY,
-					"sv")) > 0) {
+	while ((r = sd_bus_message_enter_container(
+				m,
+				SD_BUS_TYPE_DICT_ENTRY,
+				"sv")) > 0)
+	{
 		r = sd_bus_message_read(m, "s", &property);
-		if (r < 0) {
+		if (r < 0)
+		{
 			fprintf(stderr, "Error reading message: %s\n",
 					strerror(-r));
 			quit(r, loop);
 			return r;
 		}
 
-		if(strcmp(property, "pgood"))
+		if (strcmp(property, "pgood"))
 			continue;
 
 		quit(0, loop);
@@ -69,6 +72,80 @@
 	return 0;
 }
 
+static int get_object(sd_bus *conn, const char *obj,
+					  sd_bus_message **reply)
+{
+	sd_bus_message *request = NULL;
+	int r, retry = 0;
+
+	r = sd_bus_message_new_method_call(
+		conn, &request, "xyz.openbmc_project.ObjectMapper",
+		"/xyz/openbmc_project/object_mapper",
+		"xyz.openbmc_project.ObjectMapper", "GetObject");
+	if (r < 0)
+		goto exit;
+
+	r = sd_bus_message_append(request, "s", obj);
+	if (r < 0)
+		goto exit;
+	r = sd_bus_message_append(request, "as", 0, NULL);
+	if (r < 0)
+		goto exit;
+
+	while (true)
+	{
+		r = sd_bus_call(conn, request, 0, NULL, reply);
+		if (r == -EBUSY || r == -ENOBUFS)
+		{
+			if (retry >= mapper_busy_retries)
+				break;
+
+			usleep(mapper_busy_delay_interval_usec * (1 << retry));
+			++retry;
+			continue;
+		}
+		break;
+	}
+
+	if (r < 0)
+		goto exit;
+
+exit:
+	sd_bus_message_unref(request);
+
+	return r;
+}
+
+static int get_service(sd_bus *conn, const char *obj, char **service)
+{
+	sd_bus_message *reply = NULL;
+	const char *tmp;
+	int r;
+
+	r = get_object(conn, obj, &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_message_unref(reply);
+
+	return r;
+}
+
 int main(int argc, char *argv[])
 {
 	static const char *matchfmt =
@@ -90,51 +167,58 @@
 	int r, dest = -1, state;
 	char match[LEN];
 
-	if(argc < 3) {
+	if (argc < 3)
+	{
 		fprintf(stderr, usage, argv[0]);
 		exit(EXIT_FAILURE);
 	}
 
-	if(!strcmp(argv[2], "on"))
+	if (!strcmp(argv[2], "on"))
 		dest = 1;
-	if(!strcmp(argv[2], "off"))
+	if (!strcmp(argv[2], "off"))
 		dest = 0;
 
-	if(dest != 0 && dest != 1) {
+	if (dest != 0 && dest != 1)
+	{
 		fprintf(stderr, usage, argv[0]);
 		exit(EXIT_FAILURE);
 	}
 
 	r = sd_bus_default_system(&conn);
-	if(r < 0) {
+	if (r < 0)
+	{
 		fprintf(stderr, "Error connecting to system bus: %s\n",
 				strerror(-r));
 		goto finish;
 	}
 
-	r = mapper_get_service(conn, argv[1], &service);
-	if (r < 0) {
+	r = get_service(conn, argv[1], &service);
+	if (r < 0)
+	{
 		fprintf(stderr, "Error obtaining host service: %s\n",
 				strerror(-r));
 		goto finish;
 	}
 
 	r = sd_event_default(&loop);
-	if (r < 0) {
+	if (r < 0)
+	{
 		fprintf(stderr, "Error obtaining event loop: %s\n",
 				strerror(-r));
 		goto finish;
 	}
 
 	r = sd_bus_attach_event(conn, loop, SD_EVENT_PRIORITY_NORMAL);
-	if (r < 0) {
+	if (r < 0)
+	{
 		fprintf(stderr, "Failed to attach system "
-				"bus to event loop: %s\n",
+						"bus to event loop: %s\n",
 				strerror(-r));
 		goto finish;
 	}
 
-	if(strlen(matchfmt) + strnlen(argv[1], LEN) > LEN) {
+	if (strlen(matchfmt) + strnlen(argv[1], LEN) > LEN)
+	{
 		r = -E2BIG;
 		fprintf(stderr, "Error adding match rule: %s\n",
 				strerror(-r));
@@ -144,35 +228,38 @@
 	sprintf(match, matchfmt, argv[1], service);
 
 	r = sd_bus_add_match(conn,
-			&slot,
-			match,
-			callback,
-			loop);
-	if(r < 0) {
+						 &slot,
+						 match,
+						 callback,
+						 loop);
+	if (r < 0)
+	{
 		fprintf(stderr, "Error adding match rule: %s\n",
 				strerror(-r));
 		goto finish;
 	}
 
 	r = sd_bus_get_property_trivial(conn,
-			service,
-			argv[1],
-			"org.openbmc.control.Power",
-			"pgood",
-			&error,
-			'i',
-			&state);
-	if(r < 0) {
+									service,
+									argv[1],
+									"org.openbmc.control.Power",
+									"pgood",
+									&error,
+									'i',
+									&state);
+	if (r < 0)
+	{
 		fprintf(stderr, "Error getting property: %s\n",
 				strerror(-r));
 		goto finish;
 	}
 
-	if(dest == state)
+	if (dest == state)
 		goto finish;
 
 	r = sd_event_loop(loop);
-	if(r < 0) {
+	if (r < 0)
+	{
 		fprintf(stderr, "Error starting event loop: %s\n",
 				strerror(-r));
 		goto finish;