feedback: Add feedback_stop_type_internal()
[platform/core/system/libsvi.git] / src / devices.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 <libsyscommon/list.h>
22
23 #include "feedback-ids.h"
24 #include "devices.h"
25 #include "log.h"
26
27 static GList *dev_head;
28
29 void add_device(const struct device_ops *dev)
30 {
31         SYS_G_LIST_APPEND(dev_head, (struct device_ops*)dev);
32 }
33
34 //LCOV_EXCL_START System Error
35 void remove_device(const struct device_ops *dev)
36 {
37         SYS_G_LIST_REMOVE(dev_head, (struct device_ops*)dev);
38 }
39 //LCOV_EXCL_STOP
40
41 const struct device_ops *find_device(int type)
42 {
43         GList *elem;
44         const struct device_ops *dev;
45
46         SYS_G_LIST_FOREACH(dev_head, elem, dev) {
47                 if (dev->type == type)
48                         return dev;
49         }
50         return NULL;
51 }
52
53 void devices_init(void)
54 {
55         GList *elem;
56         const struct device_ops *dev;
57
58         SYS_G_LIST_FOREACH(dev_head, elem, dev) {
59                 _D("[%s] initialize", dev->name);
60                 if (dev->init)
61                         dev->init();
62         }
63 }
64
65 void devices_exit(void)
66 {
67         GList *elem;
68         const struct device_ops *dev;
69
70         SYS_G_LIST_FOREACH(dev_head, elem, dev) {
71                 _D("[%s] deinitialize", dev->name);
72                 if (dev->exit)
73                         dev->exit();
74         }
75 }
76
77 //LCOV_EXCL_START
78 int devices_play(int pattern, bool always)
79 {
80         GList *elem;
81         const struct device_ops *dev;
82         int ret, prev = -EPERM;
83
84         SYS_G_LIST_FOREACH(dev_head, elem, dev) {
85                 if (dev->play) {
86                         ret = dev->play(pattern, always);
87                         if ((prev < 0 && ret == 0) ||
88                             (prev == 0 && ret < 0))
89                                 prev = 0;
90                         else if ((prev < 0 && ret == -ENOTSUP) ||
91                                         (prev == -ENOTSUP && ret < 0))
92                                 prev = -ENOTSUP;
93                         else
94                                 prev = ret;
95                 }
96         }
97
98         return prev;
99 }
100 //LCOV_EXCL_STOP
101
102 //LCOV_EXCL_START
103 int devices_play_soundpath(int pattern, const char *soundpath, bool always)
104 {
105         GList *elem;
106         const struct device_ops *dev;
107         int ret, prev = -EPERM;
108
109         SYS_G_LIST_FOREACH(dev_head, elem, dev) {
110                 if (dev->type == FEEDBACK_TYPE_SOUND) {
111                         if (dev->play_path)
112                                 ret = dev->play_path(pattern, soundpath, always);
113                         else
114                                 continue;       //LCOV_EXCL_LINE
115                 } else {
116                         if (dev->play)
117                                 ret = dev->play(pattern, always);
118                         else
119                                 continue;       //LCOV_EXCL_LINE
120                 }
121
122                 if ((prev < 0 && ret == 0) ||
123                         (prev == 0 && ret < 0))
124                         prev = 0;
125                 else if ((prev < 0 && ret == -ENOTSUP) ||
126                         (prev == -ENOTSUP && ret < 0))
127                         prev = -ENOTSUP;
128                 else
129                         prev = ret;
130         }
131
132         return prev;
133 }
134 //LCOV_EXCL_STOP
135
136 //LCOV_EXCL_START
137 int devices_stop(void)
138 {
139         GList *elem;
140         const struct device_ops *dev;
141         int ret = -ENOTSUP;
142
143         SYS_G_LIST_FOREACH(dev_head, elem, dev) {
144                 if (dev->stop)
145                         ret = dev->stop();
146         }
147
148         return ret;
149 }
150 //LCOV_EXCL_STOP
151
152 int devices_stop_by_type(feedback_type_e feedback_type)
153 {
154         GList *elem;
155         const struct device_ops *dev;
156
157         SYS_G_LIST_FOREACH(dev_head, elem, dev) {
158                 if (dev->type == feedback_type)
159                         return dev->stop();
160         }
161
162         return -ENOTSUP;
163 }