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"
35 static const char appname[] = "afm-system-daemon";
36 static const char *rootdir = NULL;
41 "usage: %s [-q] [-v] [-r rootdir]\n"
43 " -r rootdir set root directory of applications\n"
44 " -d run as a daemon\n"
52 static struct option options[] = {
53 { "root", required_argument, NULL, 'r' },
54 { "daemon", no_argument, NULL, 'd' },
55 { "quiet", no_argument, NULL, 'q' },
56 { "verbose", no_argument, NULL, 'v' },
57 { "help", no_argument, NULL, 'h' },
61 static struct jbus *jbus;
63 const char error_nothing[] = "[]";
64 const char error_bad_request[] = "\"bad request\"";
65 const char error_not_found[] = "\"not found\"";
66 const char error_cant_start[] = "\"can't start\"";
68 static void on_install(struct jreq *jreq, struct json_object *req)
74 struct json_object *resp;
76 /* scan the request */
77 switch (json_object_get_type(req)) {
78 case json_type_string:
79 wgtfile = json_object_get_string(req);
83 case json_type_object:
84 wgtfile = j_get_string(req, "wgt", NULL);
85 if (wgtfile != NULL) {
86 root = j_get_string(req, "root", rootdir);
87 force = j_get_boolean(req, "force", 0);
91 jbus_reply_error_s(jreq, error_bad_request);
95 /* install the widget */
96 ifo = install_widget(wgtfile, root, force);
98 jbus_reply_error_s(jreq, "\"installation failed\"");
102 /* build the response */
103 resp = json_object_new_object();
104 if(!resp || !j_add_string(resp, "added", wgt_info_desc(ifo)->idaver)) {
105 json_object_put(resp);
107 jbus_reply_error_s(jreq, "\"out of memory but installed!\"");
112 /* reply and propagate event */
113 jbus_reply_j(jreq, resp);
114 jbus_send_signal_j(jbus, "changed", resp);
115 json_object_put(resp);
118 static void on_uninstall(struct jreq *jreq, struct json_object *req)
120 jbus_reply_error_s(jreq, "\"not yet implemented\"");
123 static int daemonize()
133 int main(int ac, char **av)
139 /* interpretation of arguments */
140 while ((i = getopt_long(ac, av, "hdqvr:", options, NULL)) >= 0) {
159 ERROR("duplicate definition of rootdir");
164 ERROR("missing argument value");
167 ERROR("unrecognized option");
172 /* check the rootdir */
174 rootdir = FWK_APP_DIR;
176 rootdir = realpath(rootdir, NULL);
177 if (rootdir == NULL) {
178 ERROR("out of memory");
182 if (chdir(rootdir)) {
183 ERROR("can't enter %s", rootdir);
188 if (daemon && daemonize()) {
189 ERROR("daemonization failed");
194 jbus = create_jbus(0, AFM_SYSTEM_DBUS_PATH);
196 ERROR("create_jbus failed");
199 if(jbus_add_service_j(jbus, "install", on_install)
200 || jbus_add_service_j(jbus, "uninstall", on_uninstall)) {
201 ERROR("adding services failed");
206 if (jbus_start_serving(jbus)) {
207 ERROR("can't start server");
210 while (!jbus_read_write_dispatch(jbus, -1));