tizen 2.3 release
[framework/system/deviced.git] / src / hall / hall-handler.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 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-notifier.h"
37 #include "core/edbus-handler.h"
38 #include "hall-handler.h"
39
40 #define SIGNAL_HALL_STATE       "ChangeState"
41 #define HALL_PMQOS_TIME         1000 //ms
42 enum hall_ic_init_type{
43         HALL_IC_NOT_READY = 0,
44         HALL_IC_INITIALIZED = 1,
45 };
46
47 static int hall_ic_status = HALL_IC_OPENED;
48 static int hall_ic_initialized = HALL_IC_NOT_READY;
49
50 static int hall_ic_get_status(void)
51 {
52         int ret;
53         int val = HALL_IC_OPENED;
54
55         if (hall_ic_initialized == HALL_IC_INITIALIZED)
56                 return hall_ic_status;
57
58         ret = device_get_property(DEVICE_TYPE_HALL, PROP_HALL_STATUS, &val);
59         if (ret == 0 && (val == HALL_IC_OPENED || val == HALL_IC_CLOSED))
60                 hall_ic_status = val;
61
62         return hall_ic_status;
63 }
64
65 static void hall_ic_chgdet_cb(void)
66 {
67         int val = -1;
68         int fd, r, ret;
69         char buf[2];
70         char *arr[1];
71         char str_status[32];
72
73         fd = open(HALL_IC_STATUS, O_RDONLY);
74         if (fd == -1) {
75                 _E("%s open error: %s", HALL_IC_STATUS, strerror(errno));
76                 return;
77         }
78         r = read(fd, buf, 1);
79         close(fd);
80         if (r != 1) {
81                 _E("fail to get hall status %d", r);
82                 return;
83         }
84
85         buf[1] = '\0';
86
87         hall_ic_initialized = HALL_IC_INITIALIZED;
88
89         val = atoi(buf);
90         if (val != HALL_IC_OPENED && val != HALL_IC_CLOSED)
91                 val = HALL_IC_OPENED;
92         hall_ic_status = val;
93
94         _I("cover is opened(%d)", hall_ic_status);
95
96         device_notify(DEVICE_NOTIFIER_HALLIC_OPEN, (void *)hall_ic_status);
97
98         snprintf(str_status, sizeof(str_status), "%d", hall_ic_status);
99         arr[0] = str_status;
100
101         broadcast_edbus_signal(DEVICED_PATH_HALL, DEVICED_INTERFACE_HALL,
102                         SIGNAL_HALL_STATE, "i", arr);
103
104         /* Set touch screen flip mode when cover is closed */
105         ret = device_set_property(DEVICE_TYPE_TOUCH,
106             PROP_TOUCH_SCREEN_FLIP_MODE, (hall_ic_status ? 0 : 1));
107         if (ret < 0)
108                 _E("Failed to set touch screen flip mode!");
109
110         /* Set touch key flip mode when cover is closed */
111         ret = device_set_property(DEVICE_TYPE_TOUCH,
112             PROP_TOUCH_KEY_FLIP_MODE, (hall_ic_status ? 0 : 1));
113         if (ret < 0)
114                 _E("Failed to set touch key flip mode!");
115
116 }
117
118 static void hall_edbus_signal_handler(void *data, DBusMessage *msg)
119 {
120         _D("hall_edbus_signal_handler occurs!!!");
121 }
122
123 static DBusMessage *edbus_getstatus_cb(E_DBus_Object *obj, DBusMessage *msg)
124 {
125         DBusMessageIter iter;
126         DBusMessage *reply;
127         int val;
128
129         val = hall_ic_get_status();
130         _D("get hall status %d", val);
131         reply = dbus_message_new_method_return(msg);
132         dbus_message_iter_init_append(reply, &iter);
133         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &val);
134         return reply;
135 }
136
137 static const struct edbus_method edbus_methods[] = {
138         { "getstatus",       NULL,   "i", edbus_getstatus_cb },
139         /* Add methods here */
140 };
141
142 static void hall_ic_init(void *data)
143 {
144         int ret, val;
145
146         /* init dbus interface */
147         ret = register_edbus_method(DEVICED_PATH_HALL, edbus_methods, ARRAY_SIZE(edbus_methods));
148         if (ret < 0)
149                 _E("fail to init edbus method(%d)", ret);
150
151         register_edbus_signal_handler(DEVICED_PATH_HALL, DEVICED_INTERFACE_HALL,
152                         SIGNAL_HALL_STATE,
153                         hall_edbus_signal_handler);
154         val = hall_ic_get_status();
155         _D("get hall status %d", val);
156 }
157
158 static int hall_ic_execute(void *data)
159 {
160         device_notify(DEVICE_NOTIFIER_PMQOS_HALL, (void*)HALL_PMQOS_TIME);
161         hall_ic_chgdet_cb();
162         return 0;
163 }
164
165 static const struct device_ops hall_device_ops = {
166         .priority = DEVICE_PRIORITY_NORMAL,
167         .name     = HALL_IC_NAME,
168         .init     = hall_ic_init,
169         .status   = hall_ic_get_status,
170         .execute  = hall_ic_execute,
171 };
172
173 DEVICE_OPS_REGISTER(&hall_device_ops)