2 * Copyright (C) 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.
20 #include <json-c/json.h>
22 #include <afb/afb-binding.h>
23 #include <afb/afb-service-itf.h>
25 #include "ofono_manager.h"
26 #include "ofono_voicecallmanager.h"
27 #include "ofono_voicecall.h"
29 static const struct afb_binding_interface *interface;
31 static OrgOfonoVoiceCallManager *vcm;
32 static OrgOfonoVoiceCall *incoming_call, *voice_call;
34 static void dial(struct afb_req request)
36 struct json_object *query, *val;
39 query = afb_req_json(request);
40 json_object_object_get_ex(query, "value", &val);
41 if (json_object_is_type(val, json_type_string)) {
42 number = json_object_get_string(val);
44 ERROR(interface, "dial: cannot dial with active call");
45 afb_req_fail(request, "active call", NULL);
47 DEBUG(interface, "dial: %s...\n", number);
48 if (ofono_voicecallmanager_dial(vcm, (gchar *)number, "")) {
49 afb_req_success(request, NULL, NULL);
51 ERROR(interface, "dial: failed to dial number\n");
52 afb_req_fail(request, "failed dial", NULL);
56 ERROR(interface, "dial: no phone number parameter\n");
57 afb_req_fail(request, "no number", NULL);
61 static void hangup(struct afb_req request)
64 DEBUG(interface, "Hangup voice call\n");
65 ofono_voicecall_hangup(voice_call);
66 afb_req_success(request, NULL, NULL);
67 } else if (incoming_call) {
68 DEBUG(interface, "Reject incoming call\n");
69 ofono_voicecall_hangup(incoming_call);
70 afb_req_success(request, NULL, NULL);
72 ERROR(interface, "Hangup: no active call");
73 afb_req_fail(request, "failed hangup", NULL);
77 static void answer(struct afb_req request)
80 DEBUG(interface, "Answer voice call\n");
81 voice_call = incoming_call;
82 ofono_voicecall_answer(voice_call);
84 ERROR(interface, "Answer: no incoming call");
88 static void call_state_changed_cb(OrgOfonoVoiceCall *vc, gchar *state)
90 struct json_object *call_state;
91 call_state = json_object_new_object();
92 json_object_object_add(call_state, "state", json_object_new_string(state));
93 afb_daemon_broadcast_event(interface->daemon, "callStateChanged", call_state);
96 static void incoming_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op, gchar *clip)
98 struct json_object *call_info;
100 call_info = json_object_new_object();
101 json_object_object_add(call_info, "clip", json_object_new_string(clip));
102 afb_daemon_broadcast_event(interface->daemon, "incomingCall", call_info);
103 incoming_call = ofono_voicecall_new(interface, op, call_state_changed_cb);
106 static void dialing_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op, gchar *colp)
108 struct json_object *call_info;
110 call_info = json_object_new_object();
111 json_object_object_add(call_info, "colp", json_object_new_string(colp));
112 afb_daemon_broadcast_event(interface->daemon, "dialingCall", call_info);
113 voice_call = ofono_voicecall_new(interface, op, call_state_changed_cb);
116 static void terminated_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op)
119 ofono_voicecall_free(incoming_call);
120 incoming_call = NULL;
121 } else if (voice_call) {
122 ofono_voicecall_free(voice_call);
126 afb_daemon_broadcast_event(interface->daemon, "terminatedCall", NULL);
129 static void *main_loop_thread(void *unused)
131 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
132 g_main_loop_run(loop);
136 static int ofono_init(void)
141 /* Start the main loop thread */
142 pthread_create(&tid, NULL, main_loop_thread, NULL);
144 ret = ofono_manager_init(interface);
146 const gchar *modem_path = ofono_manager_get_default_modem_path();
148 DEBUG(interface, "modem_path: %s\n", modem_path);
149 vcm = ofono_voicecallmanager_init(interface, modem_path,
154 ERROR(interface, "[telephony] failed to initialize voice call manager\n");
158 ERROR(interface, "[telephony] default modem not set\n");
162 ERROR(interface, "[telephony] failed to initialize ofono manager: " \
163 "HFP device not connected or Bluetooth disabled\n");
169 static const struct afb_verb_desc_v1 verbs[]= {
172 .session = AFB_SESSION_NONE,
178 .session = AFB_SESSION_NONE,
180 .info = "Hangup phone"
184 .session = AFB_SESSION_NONE,
186 .info = "Answer phone"
191 static const struct afb_binding binding_desc = {
192 .type = AFB_BINDING_VERSION_1,
194 .info = "telephony service",
195 .prefix = "telephony",
200 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
204 return &binding_desc;
207 int afbBindingV1ServiceInit(struct afb_service service)
209 DEBUG(interface, "Initializing telephony service\n");