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