Add sample application[phone]
[staging/soundmanager.git] / sample / phone / telephony-binding / gdbus / ofono_voicecall.c
1 /*
2  * Copyright (C) 2017 Konsulko Group
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #define _GNU_SOURCE
18
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <afb/afb-binding.h>
23
24 #include "ofono_voicecall_interface.h"
25
26 const struct afb_binding_interface *interface;
27
28 static void property_changed(OrgOfonoVoiceCall *voice_call,
29                              gchar *property,
30                              GVariant *value)
31 {
32         GVariant *vvalue;
33         const gchar *state;
34
35         if (!strcmp(property, "State")) {
36                 vvalue = g_variant_get_variant(value);
37                 state = g_variant_get_string(vvalue, NULL);
38                 g_signal_emit_by_name(voice_call, "call-state-changed", state);
39         }
40 }
41
42 OrgOfonoVoiceCall *ofono_voicecall_new(const struct afb_binding_interface *iface,
43                                        gchar *op,
44                                        void (*call_state_changed)(OrgOfonoVoiceCall *,gchar *))
45 {
46         OrgOfonoVoiceCall *voice_call;
47
48         interface = iface;
49         voice_call = org_ofono_voice_call_proxy_new_for_bus_sync(
50                         G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
51                         "org.ofono", op, NULL, NULL);
52
53         if (voice_call) {
54                 g_signal_new("call-state-changed",
55                              G_TYPE_OBJECT,
56                              G_SIGNAL_RUN_LAST,
57                              0,
58                              NULL,
59                              NULL,
60                              g_cclosure_marshal_generic,
61                              G_TYPE_NONE,
62                              1,
63                              G_TYPE_STRING);
64
65                 if (g_signal_connect(G_OBJECT(voice_call), "call-state-changed", G_CALLBACK(call_state_changed), NULL) <= 0) {
66                         ERROR(interface, "Failed to connect to signal call-state-changed\n");
67                                                 }
68
69                 if (g_signal_connect(voice_call, "property-changed", G_CALLBACK(property_changed), NULL) <= 0) {
70                         ERROR(interface, "Failed to connect to signal call-added\n");
71                 }
72         }
73
74         return voice_call;
75 }
76
77 void ofono_voicecall_free(OrgOfonoVoiceCall *voice_call)
78 {
79         g_object_unref(G_OBJECT(voice_call));
80 }
81
82 void ofono_voicecall_hangup(OrgOfonoVoiceCall *voice_call)
83 {
84         GError *error = NULL;
85
86         org_ofono_voice_call_call_hangup_sync(voice_call, NULL, &error);
87 }
88
89 void ofono_voicecall_answer(OrgOfonoVoiceCall *voice_call)
90 {
91         GError *error = NULL;
92
93         org_ofono_voice_call_call_answer_sync(voice_call, NULL, &error);
94 }