2 * Copyright 2017 Konsulko Group
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <json-c/json.h>
23 #include <afb/afb-binding.h>
25 #include "mediaplayer-manager.h"
27 const struct afb_binding_interface *afbitf;
29 static struct afb_event media_added_event;
30 static struct afb_event media_removed_event;
33 * @brief Subscribe for an event
35 * @param struct afb_req : an afb request structure
38 static void subscribe(struct afb_req request)
40 const char *value = afb_req_value(request, "value");
42 if(!strcasecmp(value, "media_added")) {
43 afb_req_subscribe(request, media_added_event);
44 } else if(!strcasecmp(value, "media_removed")) {
45 afb_req_subscribe(request, media_removed_event);
47 afb_req_fail(request, "failed", "Invalid event");
51 afb_req_success(request, NULL, NULL);
55 * @brief Unsubscribe for an event
57 * @param struct afb_req : an afb request structure
60 static void unsubscribe(struct afb_req request)
62 const char *value = afb_req_value(request, "value");
64 if(!strcasecmp(value, "media_added")) {
65 afb_req_unsubscribe(request, media_added_event);
66 } else if(!strcasecmp(value, "media_removed")) {
67 afb_req_unsubscribe(request, media_removed_event);
69 afb_req_fail(request, "failed", "Invalid event");
73 afb_req_success(request, NULL, NULL);
76 static json_object *new_json_object_from_device(GList *list)
78 json_object *jarray = json_object_new_array();
79 json_object *jresp = json_object_new_object();
80 json_object *jstring = NULL;
83 for (l = list; l; l = l->next)
85 jstring = json_object_new_string(l->data);
86 json_object_array_add(jarray, jstring);
92 json_object_object_add(jresp, "Media", jarray);
94 // TODO: Add media path
95 jstring = json_object_new_string("");
96 json_object_object_add(jresp, "Path", jstring);
101 static void media_results_get (struct afb_req request)
104 json_object *jresp = NULL;
107 list = media_lightmediascanner_scan();
109 afb_req_fail(request, "failed", "media scan error");
114 jresp = new_json_object_from_device(list);
119 afb_req_fail(request, "failed", "media parsing error");
123 afb_req_success(request, jresp, "Media Results Displayed");
126 static void media_broadcast_device_added (GList *list)
128 json_object *jresp = new_json_object_from_device(list);
131 afb_event_push(media_added_event, jresp);
135 static void media_broadcast_device_removed (const char *obj_path)
137 json_object *jresp = json_object_new_object();
138 json_object *jstring = json_object_new_string(obj_path);
140 json_object_object_add(jresp, "Path", jstring);
142 afb_event_push(media_removed_event, jresp);
145 static const struct afb_verb_desc_v1 binding_verbs[] = {
146 { "media_result", AFB_SESSION_CHECK, media_results_get, "Media scan result" },
147 { "subscribe", AFB_SESSION_CHECK, subscribe, "Subscribe for an event" },
148 { "unsubscribe", AFB_SESSION_CHECK, unsubscribe, "Unsubscribe for an event" },
152 static const struct afb_binding binding_description = {
153 .type = AFB_BINDING_VERSION_1,
155 .prefix = "media-manager",
156 .info = "mediaplayer API",
157 .verbs = binding_verbs,
161 const struct afb_binding
162 *afbBindingV1Register(const struct afb_binding_interface *itf)
166 Binding_RegisterCallback_t API_Callback;
167 API_Callback.binding_device_added = media_broadcast_device_added;
168 API_Callback.binding_device_removed = media_broadcast_device_removed;
169 BindingAPIRegister(&API_Callback);
171 MediaPlayerManagerInit();
173 return &binding_description;
176 int afbBindingV1ServiceInit(struct afb_service service)
178 media_added_event = afb_daemon_make_event(afbitf->daemon, "media_added");
179 media_removed_event = afb_daemon_make_event(afbitf->daemon, "media_removed");