summaryrefslogtreecommitdiffstats
path: root/src/af-usrd.c
blob: f7e039fe6b28637a51cb3867119c761870869360 (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
147
148
149
150
151
152
153
154
155
156
/*
 Copyright 2015 IoT.bzh

 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 <json.h>

#include "verbose.h"
#include "utils-jbus.h"
#include "appfwk.h"

static struct jbus *jbus;
static struct appfwk *appfwk;

const char error_nothing[] = "[]";
const char error_not_found[] = "{\"status\":\"error: not found\"}";

static const char *getappid(struct json_object *obj)
{
	return json_object_get_string(obj);
}

static const char *getrunid(struct json_object *obj)
{
	return json_object_get_string(obj);
}

static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
{
	if (obj)
		jbus_reply(jreq, resp);
	else
		jbus_replyj(jreq, errstr);
}

static void on_runnables(struct jreq *jreq, struct json_object *obj)
{
	struct json_object *resp = appfwk_application_list(appfwk);
	jbus_reply(jreq, resp);
	json_object_put(obj);
}

static void on_detail(struct jreq *jreq, struct json_object *obj)
{
	const char *appid = getappid(obj);
	struct json_object *resp = appfwk_get_application_public(appfwk, appid);
	reply(jreq, resp, error_not_found);
	json_object_put(obj);
}

static void on_start(struct jreq *jreq, struct json_object *obj)
{
	const char *appid = getappid(obj);
	const char *runid = appfwk_start(appfwk, appid);
	jbus_replyj(jreq, runid ? runid : error_not_found);
	json_object_put(obj);
}

static void on_stop(struct jreq *jreq, struct json_object *obj)
{
	const char *runid = getrunid(obj);
	int status = appfwk_stop(appfwk, runid);
	jbus_replyj(jreq, status ? error_not_found : "true");
	json_object_put(obj);
}

static void on_suspend(struct jreq *jreq, struct json_object *obj)
{
	const char *runid = getrunid(obj);
	int status = appfwk_suspend(appfwk, runid);
	jbus_replyj(jreq, status ? error_not_found : "true");
	json_object_put(obj);
}

static void on_resume(struct jreq *jreq, struct json_object *obj)
{
	const char *runid = getrunid(obj);
	int status = appfwk_resume(appfwk, runid);
	jbus_replyj(jreq, status ? error_not_found : "true");
	json_object_put(obj);
}

static void on_runners(struct jreq *jreq, struct json_object *obj)
{
	struct json_object *resp = appfwk_running_list(appfwk);
	jbus_reply(jreq, resp);
	json_object_put(obj);
}

static void on_state(struct jreq *jreq, struct json_object *obj)
{
	const char *runid = getrunid(obj);
	int status = appfwk_state(appfwk, runid);
	jbus_replyj(jreq, status ? error_not_found : "true");
	json_object_put(obj);
}

int main(int ac, char **av)
{
	LOGAUTH("af-usrd");

	/* init framework */
	appfwk = appfwk_create();
	if (!appfwk) {
		ERROR("appfwk_create failed");
		return 1;
	}
	if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
		ERROR("can't add root %s", FWK_APP_DIR);
		return 1;
	}
	if (appfwk_update_applications(appfwk)) {
		ERROR("appfwk_update_applications failed");
		return 1;
	}

	/* init service	*/
	jbus = create_jbus(1, "/org/automotive/linux/framework");
	if (!jbus) {
		ERROR("create_jbus failed");
		return 1;
	}
	if(jbus_add_service(jbus, "runnables", on_runnables)
	|| jbus_add_service(jbus, "detail", on_detail)
	|| jbus_add_service(jbus, "start", on_run)
	|| jbus_add_service(jbus, "stop", on_stop)
	|| jbus_add_service(jbus, "suspend", on_suspend)
	|| jbus_add_service(jbus, "resume", on_resume)
	|| jbus_add_service(jbus, "runners", on_runners)
	|| jbus_add_service(jbus, "state", on_state)) {
		ERROR("adding services failed");
		return 1;
	}

	/* start and run */
	if (jbus_start_serving(jbus)) {
		ERROR("cant start server");
		return 1;
	}
	while (!jbus_read_write_dispatch(jbus, -1));
	return 0;
}