4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
28 #include "utils-jbus.h"
29 #include "utils-json.h"
33 #include "wgtpkg-install.h"
34 #include "wgtpkg-uninstall.h"
36 static const char appname[] = "afm-system-daemon";
37 static const char *rootdir = NULL;
42 "usage: %s [-q] [-v] [-r rootdir]\n"
44 " -r rootdir set root directory of applications\n"
45 " -d run as a daemon\n"
53 static struct option options[] = {
54 { "root", required_argument, NULL, 'r' },
55 { "daemon", no_argument, NULL, 'd' },
56 { "quiet", no_argument, NULL, 'q' },
57 { "verbose", no_argument, NULL, 'v' },
58 { "help", no_argument, NULL, 'h' },
62 static struct jbus *jbus;
64 const char error_nothing[] = "[]";
65 const char error_bad_request[] = "\"bad request\"";
66 const char error_not_found[] = "\"not found\"";
67 const char error_cant_start[] = "\"can't start\"";
69 static void on_install(struct jreq *jreq, struct json_object *req)
75 struct json_object *resp;
77 /* scan the request */
78 switch (json_object_get_type(req)) {
79 case json_type_string:
80 wgtfile = json_object_get_string(req);
84 case json_type_object:
85 wgtfile = j_get_string(req, "wgt", NULL);
86 if (wgtfile != NULL) {
87 root = j_get_string(req, "root", rootdir);
88 force = j_get_boolean(req, "force", 0);
92 jbus_reply_error_s(jreq, error_bad_request);
96 /* install the widget */
97 ifo = install_widget(wgtfile, root, force);
99 jbus_reply_error_s(jreq, "\"installation failed\"");
101 /* build the response */
102 resp = json_object_new_object();
103 if(!resp || !j_add_string(resp, "added", wgt_info_desc(ifo)->idaver))
104 jbus_reply_error_s(jreq, "\"out of memory but installed!\"");
106 jbus_reply_j(jreq, resp);
110 json_object_put(resp);
113 /* still sends the signal */
114 jbus_send_signal_s(jbus, "changed", "true");
117 static void on_uninstall(struct jreq *jreq, struct json_object *req)
123 /* scan the request */
124 switch (json_object_get_type(req)) {
125 case json_type_string:
126 idaver = json_object_get_string(req);
129 case json_type_object:
130 idaver = j_get_string(req, "id", NULL);
131 if (idaver != NULL) {
132 root = j_get_string(req, "root", rootdir);
136 jbus_reply_error_s(jreq, error_bad_request);
140 /* install the widget */
141 rc = uninstall_widget(idaver, root);
143 jbus_reply_error_s(jreq, "\"uninstallation had error\"");
145 jbus_reply_s(jreq, "true");
147 /* still sends the signal */
148 jbus_send_signal_s(jbus, "changed", "true");
151 static int daemonize()
161 int main(int ac, char **av)
167 /* interpretation of arguments */
168 while ((i = getopt_long(ac, av, "hdqvr:", options, NULL)) >= 0) {
187 ERROR("duplicate definition of rootdir");
192 ERROR("missing argument value");
195 ERROR("unrecognized option");
200 /* check the rootdir */
202 rootdir = FWK_APP_DIR;
204 rootdir = realpath(rootdir, NULL);
205 if (rootdir == NULL) {
206 ERROR("out of memory");
210 if (chdir(rootdir)) {
211 ERROR("can't enter %s", rootdir);
216 if (daemon && daemonize()) {
217 ERROR("daemonization failed");
222 jbus = create_jbus(0, AFM_SYSTEM_DBUS_PATH);
224 ERROR("create_jbus failed");
227 if(jbus_add_service_j(jbus, "install", on_install)
228 || jbus_add_service_j(jbus, "uninstall", on_uninstall)) {
229 ERROR("adding services failed");
234 if (jbus_start_serving(jbus)) {
235 ERROR("can't start server");
238 while (!jbus_read_write_dispatch(jbus, -1));