MCC Pattern : Initial version
[apps/native/rcc.git] / src / model.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
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 #include <stdlib.h>
20 #include <unistd.h>
21 #include <peripheral_io.h>
22 #include <sys/time.h>
23 #include <Eina.h>
24
25 #include "log.h"
26 #include "model.h"
27 #include "model/model_ultrasonic_sensor.h"
28 #include "model/model_infrared_motion_sensor.h"
29 #include "model/model_infrared_obstacle_avoidance_sensor.h"
30 #include "model/model_touch_sensor.h"
31
32 struct _model_sensor_s {
33         char *id;
34         sensor_type_e sensor_type;
35         int gpio_num[2];
36         void *peripheral_info;
37 };
38 static struct {
39         Eina_List *list;
40 } model_s;
41
42 int model_init(const char *id, sensor_type_e sensor_type, int gpio_num1, int gpio_num2, model_sensor_h *out_info)
43 {
44         model_sensor_h info = NULL;
45         int ret = -1;
46
47         retv_if(!id, -1);
48
49         info = calloc(1, sizeof(model_sensor_s));
50         retv_if(!info, -1);
51         *out_info = info;
52
53         info->sensor_type = sensor_type;
54         info->id = strdup(id);
55         goto_if(!info->id, error);
56
57         info->gpio_num[0] = gpio_num1;
58         info->gpio_num[1] = gpio_num2;
59         goto_if(gpio_num1 == -1, error);
60
61         switch (sensor_type) {
62         case SENSOR_TYPE_ULTRASONIC:
63                 goto_if(gpio_num2 == -1, error);
64                 ret = model_init_ultrasonic_sensor(gpio_num1, gpio_num2, &info->peripheral_info);
65                 break;
66         case SENSOR_TYPE_INFRARED_MOTION:
67                 ret = model_init_infrared_motion_sensor(gpio_num1, &info->peripheral_info);
68                 break;
69         case SENSOR_TYPE_INFRARED_OBSTACLE_AVOIDANCE:
70                 ret = model_init_infrared_obstacle_avoidance_sensor(gpio_num1, &info->peripheral_info);
71                 break;
72         case SENSOR_TYPE_TOUCH:
73                 ret = model_init_touch_sensor(gpio_num1, &info->peripheral_info);
74                 break;
75         default:
76                 break;
77         }
78
79         goto_if(ret != 0, error);
80
81         return 0;
82
83 error:
84         if (info->id) free(info->id);
85         if (info) free(info);
86         *out_info = NULL;
87         return -1;
88 }
89
90 void model_fini(model_sensor_h info)
91 {
92         ret_if(!info);
93
94         switch (info->sensor_type) {
95         case SENSOR_TYPE_ULTRASONIC:
96                 model_fini_ultrasonic_sensor(info->peripheral_info);
97                 break;
98         case SENSOR_TYPE_INFRARED_MOTION:
99                 model_fini_infrared_motion_sensor(info->peripheral_info);
100                 break;
101         case SENSOR_TYPE_INFRARED_OBSTACLE_AVOIDANCE:
102                 model_fini_infrared_obstacle_avoidance_sensor(info->peripheral_info);
103                 break;
104         case SENSOR_TYPE_TOUCH:
105                 model_fini_touch_sensor(info->peripheral_info);
106                 break;
107         default:
108                 break;
109         }
110
111         free(info->id);
112         free(info);
113 }
114
115 int model_read_int_value(model_sensor_h info, int *out_value)
116 {
117         int ret = 0;
118
119         switch (info->sensor_type) {
120         case SENSOR_TYPE_ULTRASONIC:
121                 ret = model_read_infrared_obstacle_avoidance_sensor(info, out_value);
122                 break;
123         case SENSOR_TYPE_INFRARED_MOTION:
124                 ret = model_read_infrared_motion_sensor(info, out_value);
125                 break;
126         case SENSOR_TYPE_TOUCH:
127                 ret = model_read_touch_sensor(info, out_value);
128                 break;
129         default:
130                 break;
131         }
132
133         if (ret < 0) {
134                 _E("Something wrong in the result[%d]", ret);
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 int model_read_double_value(model_sensor_h info, double *out_value)
142 {
143         int ret = 0;
144
145         switch (info->sensor_type) {
146         case SENSOR_TYPE_ULTRASONIC:
147                 ret = model_read_ultrasonic_sensor(info, out_value);
148                 break;
149         default:
150                 _E("There is no data to retrieve");
151                 break;
152         }
153
154         if (ret < 0) {
155                 _E("Something wrong in the result[%d]", ret);
156                 return -1;
157         }
158
159         return 0;
160 }
161
162 int model_list_add_sensor(model_sensor_h info)
163 {
164         Eina_List *l, *ln;
165         model_sensor_h temp = NULL;
166
167         retv_if(!info, -1);
168         retv_if(!info->id, -1);
169
170         EINA_LIST_FOREACH_SAFE(model_s.list, l, ln, temp) {
171                 retv_if(!temp->id, -1);
172                 if (!strcmp(temp->id, info->id)) {
173                         _E("That id[%s] already exists.", info->id);
174                         return -1;
175                 }
176         }
177         model_s.list = eina_list_append(model_s.list, info);
178         return 0;
179 }
180
181 int model_list_remove_sensor(model_sensor_h info)
182 {
183         retv_if(!info, -1);
184         model_s.list = eina_list_remove(model_s.list, info);
185         return 0;
186 }
187
188 int model_list_get_sensor(const char *id, model_sensor_h *out_info)
189 {
190         Eina_List *l, *ln;
191         model_sensor_h temp = NULL;
192
193         retv_if(!id, -1);
194
195         EINA_LIST_FOREACH_SAFE(model_s.list, l, ln, temp) {
196                 retv_if(!temp->id, -1);
197                 if (!strcmp(temp->id, id)) {
198                         _E("That id[%s] already exists.", id);
199                         *out_info = temp;
200                         return 0;
201                 }
202         }
203
204         *out_info = NULL;
205         return -1;
206 }
207
208 int model_list_foreach(void (*cb)(model_sensor_h info))
209 {
210         Eina_List *l, *ln;
211         model_sensor_h temp = NULL;
212
213         retv_if(!cb, -1);
214
215         EINA_LIST_FOREACH_SAFE(model_s.list, l, ln, temp) {
216                 cb(temp);
217         }
218
219         return 0;
220 }