tizen 2.3 release
[framework/system/deviced.git] / src / gpio / sim.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 <limits.h>
23 #include <signal.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "core/common.h"
29 #include "core/devices.h"
30 #include "core/log.h"
31 #include "gpio.h"
32
33 #define GPIO_CHECK_PATH         "/sys/class/gpio/sim"
34 #define GPIO_CHECK_STATUS       "/sys/class/gpio/sim/status"
35
36 static int sim_init(void)
37 {
38         static int type = GPIO_DEVICE_UNKNOWN;
39         int fd;
40
41         if (type != GPIO_DEVICE_UNKNOWN)
42                 goto out;
43
44         fd = open(GPIO_CHECK_PATH, O_RDONLY);
45         if (fd == -1) {
46                 _E("%s open error: %s", GPIO_CHECK_PATH, strerror(errno));
47                 type = GPIO_DEVICE_NOT_EXIST;
48                 goto out;
49         }
50         close(fd);
51         type = GPIO_DEVICE_EXIST;
52 out:
53         return type;
54 }
55
56 static int sim_status(void)
57 {
58         static int type = GPIO_DEVICE_UNKNOWN;
59         int val = GPIO_DEVICE_UNKNOWN;
60         int ret;
61         int fd;
62         char buf[2];
63
64         type = sim_init();
65         if (type != GPIO_DEVICE_EXIST)
66                 goto out;
67
68         fd = open(GPIO_CHECK_STATUS, O_RDONLY);
69         if (fd == -1) {
70                 _E("%s open error: %s", GPIO_CHECK_STATUS, strerror(errno));
71                 goto out;
72         }
73
74         ret = read(fd, buf, 1);
75         close(fd);
76         if (ret != 1) {
77                 _E("fail to get status %d", ret);
78                 goto out;
79         }
80         buf[1] = '\0';
81         val = atoi(buf);
82         _I("device is (%d)", val);
83 out:
84         return val;
85 }
86
87 static const struct gpio_device sim_gpio = {
88         .type   = GPIO_DEVICE_SIM,
89         .name   = "sim",
90         .init   = sim_init,
91         .status = sim_status,
92 };
93
94 static void __CONSTRUCTOR__ module_init(void)
95 {
96         register_gpio_device(&sim_gpio);
97 }