b3db6e2b9c2c8dfb144be3e1d10bba0d6217d802
[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 <unistd.h>
20 #include <peripheral_io.h>
21 #include <sys/time.h>
22
23 #include "log.h"
24 #include "model.h"
25 #include "model/model_ultrasonic_sensor.h"
26 #include "model/model_infrared_motion_sensor.h"
27 #include "model/model_infrared_obstacle_avoidance_sensor.h"
28 #include "model/model_touch_sensor.h"
29
30 struct _model_s {
31         sensor_type_e sensor_type;
32 };
33 static struct _model_s model_s;
34
35 void model_fini(void)
36 {
37         switch (model_s.sensor_type) {
38         case SENSOR_TYPE_ULTRASONIC:
39                 model_fini_ultrasonic_sensor();
40                 break;
41         case SENSOR_TYPE_INFRARED_MOTION:
42                 model_fini_infrared_motion_sensor();
43                 break;
44         case SENSOR_TYPE_INFRARED_OBSTACLE_AVOIDANCE:
45                 model_fini_infrared_obstacle_avoidance_sensor();
46                 break;
47         case SENSOR_TYPE_TOUCH:
48                 model_fini_touch_sensor();
49                 break;
50         default:
51                 break;
52         }
53 }
54
55 int model_init(sensor_type_e sensor_type)
56 {
57         int ret = 0;
58         model_s.sensor_type = sensor_type;
59
60         switch (sensor_type) {
61         case SENSOR_TYPE_ULTRASONIC:
62                 ret = model_init_ultrasonic_sensor();
63                 break;
64         case SENSOR_TYPE_INFRARED_MOTION:
65                 ret = model_init_infrared_motion_sensor();
66                 break;
67         case SENSOR_TYPE_INFRARED_OBSTACLE_AVOIDANCE:
68                 ret = model_init_infrared_obstacle_avoidance_sensor();
69                 break;
70         case SENSOR_TYPE_TOUCH:
71                 model_init_touch_sensor();
72                 break;
73         default:
74                 break;
75         }
76
77         goto_if(ret != 0, error);
78
79         return 0;
80
81 error:
82         model_fini();
83         return -1;
84 }
85
86 int model_alloc(void **data)
87 {
88         switch (model_s.sensor_type) {
89         case SENSOR_TYPE_ULTRASONIC:
90                 break;
91         case SENSOR_TYPE_INFRARED_MOTION:
92         case SENSOR_TYPE_INFRARED_OBSTACLE_AVOIDANCE:
93         case SENSOR_TYPE_TOUCH:
94                 _E("No function for allocation");
95                 break;
96         default:
97                 break;
98         }
99
100         return 0;
101 }
102
103 int model_read_int_value(int *out_value)
104 {
105         int ret = 0;
106
107         switch (model_s.sensor_type) {
108         case SENSOR_TYPE_ULTRASONIC:
109                 ret = model_read_infrared_obstacle_avoidance_sensor(out_value);
110                 break;
111         case SENSOR_TYPE_INFRARED_MOTION:
112                 ret = model_read_infrared_motion_sensor(out_value);
113                 break;
114         case SENSOR_TYPE_TOUCH:
115                 ret = model_read_touch_sensor(out_value);
116                 break;
117         default:
118                 break;
119         }
120
121         if (ret < 0) {
122                 _E("Something wrong in the result[%d]", ret);
123                 return -1;
124         }
125
126         return 0;
127 }
128
129 int model_read_double_value(double *out_value)
130 {
131         int ret = 0;
132
133         switch (model_s.sensor_type) {
134         case SENSOR_TYPE_ULTRASONIC:
135                 ret = model_read_ultrasonic_sensor(out_value);
136                 break;
137         default:
138                 _E("There is no data to retrieve");
139                 break;
140         }
141
142         if (ret < 0) {
143                 _E("Something wrong in the result[%d]", ret);
144                 return -1;
145         }
146
147         return 0;
148 }
149
150 int model_write(void *data)
151 {
152         switch (model_s.sensor_type) {
153         case SENSOR_TYPE_ULTRASONIC:
154         case SENSOR_TYPE_INFRARED_MOTION:
155         case SENSOR_TYPE_TOUCH:
156                 _E("No function for writing");
157                 break;
158         default:
159                 break;
160         }
161
162         return 0;
163 }