Initialize Tizen 2.3
[framework/system/deviced.git] / src / haptic / external.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 <stdbool.h>
22 #include <dlfcn.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26
27 #include "core/log.h"
28 #include "haptic.h"
29
30 #define HAPTIC_MODULE_PATH                      "/usr/lib/libhaptic-module.so"
31
32 /* Haptic Plugin Interface */
33 static void *dlopen_handle;
34 static const struct haptic_plugin_ops *plugin_intf;
35
36 static bool is_valid(void)
37 {
38         struct stat buf;
39         const struct haptic_plugin_ops *(*get_haptic_plugin_interface) () = NULL;
40
41         if (stat(HAPTIC_MODULE_PATH, &buf)) {
42                 _E("file(%s) is not presents", HAPTIC_MODULE_PATH);
43                 goto error;
44         }
45
46         dlopen_handle = dlopen(HAPTIC_MODULE_PATH, RTLD_NOW);
47         if (!dlopen_handle) {
48                 _E("dlopen failed: %s", dlerror());
49                 goto error;
50         }
51
52         get_haptic_plugin_interface = dlsym(dlopen_handle, "get_haptic_plugin_interface");
53         if (!get_haptic_plugin_interface) {
54                 _E("dlsym failed : %s", dlerror());
55                 goto error;
56         }
57
58         plugin_intf = get_haptic_plugin_interface();
59         if (!plugin_intf) {
60                 _E("get_haptic_plugin_interface() failed");
61                 goto error;
62         }
63
64         _I("Support external haptic device");
65         return true;
66
67 error:
68         if (dlopen_handle) {
69                 dlclose(dlopen_handle);
70                 dlopen_handle = NULL;
71         }
72
73         _I("Do not support external haptic device");
74         return false;
75 }
76
77 static const struct haptic_plugin_ops *load(void)
78 {
79         return plugin_intf;
80 }
81
82 static void release(void)
83 {
84         if (dlopen_handle) {
85                 dlclose(dlopen_handle);
86                 dlopen_handle = NULL;
87         }
88
89         plugin_intf = NULL;
90 }
91
92 static const struct haptic_ops ext_ops = {
93         .is_valid = is_valid,
94         .load     = load,
95         .release  = release,
96 };
97
98 HAPTIC_OPS_REGISTER(&ext_ops)