tizen 2.3 release
[framework/system/deviced.git] / src / earjack / earjack.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <vconf.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <device-node.h>
31 #include <Ecore.h>
32
33 #include "core/log.h"
34 #include "core/common.h"
35 #include "core/devices.h"
36 #include "core/device-handler.h"
37 #include "core/device-notifier.h"
38 #include "core/edbus-handler.h"
39 #include "display/poll.h"
40
41 #define SIGNAL_EARJACK_STATE    "ChangedEarjack"
42
43 static int earjack_status = 0;
44
45 static void earjack_send_broadcast(int status)
46 {
47         static int old = 0;
48         char *arr[1];
49         char str_status[32];
50
51         if (old == status)
52                 return;
53
54         _I("broadcast earjack status %d", status);
55         old = status;
56         snprintf(str_status, sizeof(str_status), "%d", status);
57         arr[0] = str_status;
58
59         broadcast_edbus_signal(DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
60                         SIGNAL_EARJACK_STATE, "i", arr);
61 }
62
63 static void earjack_chgdet_cb(void *data)
64 {
65         int val;
66         int ret = 0;
67
68         if (data)
69                 val = *(int *)data;
70         else {
71                 ret = device_get_property(DEVICE_TYPE_EXTCON, PROP_EXTCON_EARJACK_ONLINE, &val);
72                 if (ret != 0) {
73                         _E("failed to get status");
74                         return;
75                 }
76         }
77         _I("jack - earjack changed %d", val);
78         vconf_set_int(VCONFKEY_SYSMAN_EARJACK, val);
79         earjack_send_broadcast(val);
80         if (CONNECTED(val)) {
81                 extcon_set_count(EXTCON_EARJACK);
82                 internal_pm_change_state(LCD_NORMAL);
83         }
84 }
85
86 static DBusMessage *dbus_get_status(E_DBus_Object *obj, DBusMessage *msg)
87 {
88         DBusMessageIter iter;
89         DBusMessage *reply;
90         int val;
91
92         val = earjack_status;
93         _D("get hall status %d", val);
94
95         reply = dbus_message_new_method_return(msg);
96         dbus_message_iter_init_append(reply, &iter);
97         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &val);
98         return reply;
99 }
100
101 static const struct edbus_method edbus_methods[] = {
102         { "getstatus",       NULL,   "i", dbus_get_status },
103         /* Add methods here */
104 };
105
106 static void earjack_init(void *data)
107 {
108         int ret, val;
109
110         if (device_get_property(DEVICE_TYPE_EXTCON, PROP_EXTCON_EARJACK_ONLINE, &val) == 0) {
111                 if (CONNECTED(val))
112                         extcon_set_count(EXTCON_EARJACK);
113                 vconf_set_int(VCONFKEY_SYSMAN_EARJACK, val);
114         }
115
116         /* init dbus interface */
117         ret = register_edbus_method(DEVICED_PATH_SYSNOTI, edbus_methods, ARRAY_SIZE(edbus_methods));
118         if (ret < 0)
119                 _E("fail to init edbus method(%d)", ret);
120 }
121
122 static int earjack_get_status(void)
123 {
124         return earjack_status;
125 }
126
127 static int earjack_execute(void *data)
128 {
129         earjack_chgdet_cb(data);
130         return 0;
131 }
132
133 static const struct device_ops earjack_device_ops = {
134         .priority = DEVICE_PRIORITY_NORMAL,
135         .name     = "earjack",
136         .init     = earjack_init,
137         .status   = earjack_get_status,
138         .execute  = earjack_execute,
139 };
140
141 DEVICE_OPS_REGISTER(&earjack_device_ops)