device-node : remove unreachable code
[platform/core/system/libdevice-node.git] / src / device-node.c
1 /*
2  * device-node
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19 #include <string.h>
20 #include <glib.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include "device-internal.h"
24
25 #define container_of(ptr, type, member) ({            \
26                  const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
27                  (type *)( (char *)__mptr - offsetof(type,member) );})
28
29 static GList *dev_head;
30 static void *dlopen_handle;
31 const OEM_sys_devman_plugin_interface *oem_intf;
32
33 void add_device(const enum device_type *devtype)
34 {
35         dev_head = g_list_append(dev_head, (enum device_type *)devtype);
36 }
37
38 void remove_device(const enum device_type *devtype)
39 {
40         dev_head = g_list_remove(dev_head, (enum device_type *)devtype);
41 }
42
43 static enum device_type *find_device(enum device_type devtype)
44 {
45         GList *elem;
46         enum device_type *type;
47
48         for (elem = dev_head; elem; elem = elem->next) {
49                 type = elem->data;
50                 if (*type == devtype)
51                         return type;
52         }
53
54         return NULL;
55 }
56
57 API int device_get_property(enum device_type devtype, int property, int *value)
58 {
59         struct device *dev;
60         enum device_type *type;
61         int r;
62
63         type = find_device(devtype);
64         if (type == NULL) {
65                 _E("devtype cannot find");
66                 errno = EPERM;
67                 return -1;
68         }
69
70         dev = container_of(type, struct device, type);
71
72         if (dev->get_prop == NULL) {
73                 _E("devtype doesn't have getter function");
74                 errno = EPERM;
75                 return -1;
76         }
77
78         r = dev->get_prop(property, value);
79         if (r == -ENODEV) {
80                 _E("Not support driver");
81                 errno = ENODEV;
82                 return -1;
83         } else if (r == -1) {
84                 _E("get_prop of %s(%d) return failes", dev->name, property);
85                 errno = EPERM;
86                 return -1;
87         }
88
89         errno = 0;
90         return 0;
91 }
92
93 API int device_set_property(enum device_type devtype, int property, int value)
94 {
95         enum device_type *type;
96         struct device *dev;
97         int r;
98
99         type = find_device(devtype);
100         if (type == NULL) {
101                 _E("devtype cannot find");
102                 errno = EPERM;
103                 return -1;
104         }
105
106         dev = container_of(type, struct device, type);
107
108         if (dev->set_prop == NULL) {
109                 _E("devtype doesn't have setter function");
110                 errno = EPERM;
111                 return -1;
112         }
113
114         r = dev->set_prop(property, value);
115         if (r == -ENODEV) {
116                 _E("Not support driver");
117                 errno = ENODEV;
118                 return -1;
119         } else if (r == -1) {
120                 _E("set_prop of %s(%d) return failes", dev->name, property);
121                 errno = EPERM;
122                 return -1;
123         }
124
125         errno = 0;
126         return 0;
127 }
128
129 static void __CONSTRUCTOR__ module_init(void)
130 {
131         const OEM_sys_devman_plugin_interface *(*OEM_sys_get_devman_plugin_interface) ();
132         char *error;
133
134         dlopen_handle = dlopen(DEVMAN_PLUGIN_PATH, RTLD_NOW);
135         if (!dlopen_handle) {
136                 _E("dlopen() failed");
137                 goto ERROR;
138         }
139
140         OEM_sys_get_devman_plugin_interface = dlsym(dlopen_handle, "OEM_sys_get_devman_plugin_interface");
141         if ((error = dlerror()) != NULL) {
142                 _E("dlsym() failed: %s", error);
143                 goto ERROR;
144         }
145
146         oem_intf = OEM_sys_get_devman_plugin_interface();
147         if (!oem_intf) {
148                 _E("get_devman_plugin_interface() failed");
149                 goto ERROR;
150         }
151
152         return;
153
154 ERROR:
155         if (dlopen_handle)
156                 dlclose(dlopen_handle);
157 }
158
159 static void __DESTRUCTOR__ module_fini(void)
160 {
161         if (dlopen_handle)
162                 dlclose(dlopen_handle);
163 }