summaryrefslogtreecommitdiffstats
path: root/objects/host_watchdog_obj.c
blob: 6396658ec2aca01c8786ebf52ca670e221e241a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "interfaces/watchdog.h"
#include "openbmc.h"


/* ---------------------------------------------------------------------------------------------------- */

static const gchar* dbus_object_path = "/org/openbmc/watchdog/HostWatchdog";
static const gchar* dbus_name        = "org.openbmc.watchdog.HostWatchdog";

static GDBusObjectManagerServer *manager = NULL;


static gboolean
poll_watchdog(gpointer user_data)
{
	Watchdog *watchdog = object_get_watchdog((Object*)user_data);

 	guint count = watchdog_get_watchdog(watchdog);
	g_print("Polling watchdog: %d\n",count);
	
	if (count == 0)
	{
		watchdog_emit_watchdog_error(watchdog);
	}

	//reset watchdog
	watchdog_set_watchdog(watchdog,0);	
	return TRUE;
}

static gboolean
on_start        (Watchdog  *wd,
                GDBusMethodInvocation  *invocation,
                gpointer                user_data)
{
	guint poll_interval = watchdog_get_poll_interval(wd);
  	g_timeout_add(poll_interval, poll_watchdog, user_data);
	watchdog_complete_start(wd,invocation);
	return TRUE;
}

static gboolean
on_poke         (Watchdog  *wd,
                GDBusMethodInvocation  *invocation,
                gpointer                user_data)
{
	watchdog_set_watchdog(wd,1);
	watchdog_complete_poke(wd,invocation);
	return TRUE;
}



static void 
on_bus_acquired (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer         user_data)
{
  	g_print ("Acquired a message bus connection: %s\n",name);

  	cmdline *cmd = user_data;
	if (cmd->argc < 2)
	{
		g_print("No objects created.  Put object name(s) on command line\n");
		return;
	}	
  	manager = g_dbus_object_manager_server_new (dbus_object_path);
  	int i=0;
  	for (i=1;i<cmd->argc;i++)
  	{
		gchar *s;
 		s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
		ObjectSkeleton *object = object_skeleton_new (s);
		g_free (s);

		Watchdog *wd = watchdog_skeleton_new ();
  		object_skeleton_set_watchdog (object, wd);
  		g_object_unref (wd);
		
  		// set properties
  		watchdog_set_watchdog(wd,1);
		
		//define method callbacks here
 		g_signal_connect (wd,
                    "handle-start",
                    G_CALLBACK (on_start),
                    object); /* user_data */
 
 		g_signal_connect (wd,
                    "handle-poke",
                    G_CALLBACK (on_poke),
                    object); /* user_data */


  		/* Export the object (@manager takes its own reference to @object) */
  		g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
  		g_object_unref (object);
	}

  /* Export all objects */
  g_dbus_object_manager_server_set_connection (manager, connection);
}

static void
on_name_acquired (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{
  g_print ("Acquired the name %s\n", name);
}

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
  g_print ("Lost the name %s\n", name);
}


gint
main (gint argc, gchar *argv[])
{
  GMainLoop *loop;
  cmdline cmd;
  cmd.argc = argc;
  cmd.argv = argv;
  guint id;
  loop = g_main_loop_new (NULL, FALSE);

  id = g_bus_own_name (G_BUS_TYPE_SESSION,
                       dbus_name,
                       G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
                       on_bus_acquired,
                       on_name_acquired,
                       on_name_lost,
                       &cmd,
                       NULL);

  g_main_loop_run (loop);
  
  g_bus_unown_name (id);
  g_main_loop_unref (loop);
  return 0;
}
OpenPOWER on IntegriCloud