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.
27 #include "utils-jbus.h"
31 static const char appname[] = "afm-system-daemon";
36 "usage: %s [-q] [-v] [-r rootdir]... [-a appdir]...\n"
38 " -a appdir adds an application directory\n"
39 " -r rootdir adds a root directory of applications\n"
40 " -d run as a daemon\n"
48 static struct option options[] = {
49 { "root", required_argument, NULL, 'r' },
50 { "application", required_argument, NULL, 'a' },
51 { "daemon", no_argument, NULL, 'd' },
52 { "quiet", no_argument, NULL, 'q' },
53 { "verbose", no_argument, NULL, 'v' },
54 { "help", no_argument, NULL, 'h' },
58 static struct jbus *jbus;
59 static struct afm_db *afdb;
61 const char error_nothing[] = "[]";
62 const char error_bad_request[] = "\"bad request\"";
63 const char error_not_found[] = "\"not found\"";
64 const char error_cant_start[] = "\"can't start\"";
66 static const char *getappid(struct json_object *obj)
68 return json_type_string == json_object_get_type(obj) ? json_object_get_string(obj) : NULL;
71 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
74 jbus_reply_j(jreq, resp);
76 jbus_reply_error_s(jreq, errstr);
79 static void on_runnables(struct jreq *jreq, struct json_object *obj)
81 struct json_object *resp = afm_db_application_list(afdb);
82 jbus_reply_j(jreq, resp);
83 json_object_put(resp);
86 static void on_detail(struct jreq *jreq, struct json_object *obj)
88 const char *appid = getappid(obj);
89 struct json_object *resp = afm_db_get_application_public(afdb, appid);
90 reply(jreq, resp, error_not_found);
91 json_object_put(resp);
94 extern void install_widget(const char *wgtfile, const char *root, int force);
95 static void on_install(struct jreq *jreq, struct json_object *obj)
97 jbus_reply_error_s(jreq, "\"not yet implemented\"");
100 static void on_uninstall(struct jreq *jreq, struct json_object *obj)
102 jbus_reply_error_s(jreq, "\"not yet implemented\"");
105 static int daemonize()
115 int main(int ac, char **av)
121 /* first interpretation of arguments */
122 while ((i = getopt_long(ac, av, "hdqvr:a:", options, NULL)) >= 0) {
142 ERROR("missing argument value");
145 ERROR("unrecognized option");
151 afdb = afm_db_create();
153 ERROR("afm_create failed");
156 if (afm_db_add_root(afdb, FWK_APP_DIR)) {
157 ERROR("can't add root %s", FWK_APP_DIR);
161 /* second interpretation of arguments */
163 while ((i = getopt_long(ac, av, "hdqvr:a:", options, NULL)) >= 0) {
166 if (afm_db_add_root(afdb, optarg)) {
167 ERROR("can't add root %s", optarg);
172 if (afm_db_add_application(afdb, optarg)) {
173 ERROR("can't add application %s", optarg);
180 /* update the database */
181 if (afm_db_update_applications(afdb)) {
182 ERROR("afm_update_applications failed");
186 if (daemon && daemonize()) {
187 ERROR("daemonization failed");
192 jbus = create_jbus(0, AFM_SYSTEM_DBUS_PATH);
194 ERROR("create_jbus failed");
197 if(jbus_add_service_j(jbus, "runnables", on_runnables)
198 || jbus_add_service_j(jbus, "detail", on_detail)
199 || 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("cant start server");
210 while (!jbus_read_write_dispatch(jbus, -1));