modify for sound level demo on hands-on lab
[apps/native/position-finder-server.git] / src / controller.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *
9  * Licensed under the Flora License, Version 1.1 (the License);
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://floralicense.org/license/
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an AS IS BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <unistd.h>
23 #include <glib.h>
24 #include <Ecore.h>
25 #include <tizen.h>
26 #include <service_app.h>
27
28 #include "log.h"
29 #include "resource.h"
30 #include "connectivity.h"
31 #include "controller.h"
32 #include "controller_util.h"
33 #include "webutil.h"
34
35 #define CONNECTIVITY_KEY "opened"
36 #define SENSORING_TIME_INTERVAL 0.01f
37
38 typedef struct app_data_s {
39         Ecore_Timer *getter_timer;
40         connectivity_resource_s *resource_info;
41 } app_data;
42
43 const char bar[][128] = {
44         ")",
45         "))",
46         ")))",
47         "))))",
48         ")))))",
49         "))))))",
50         ")))))))",
51         "))))))))",
52         ")))))))))",
53         "))))))))))",
54         ")))))))))))",
55         "))))))))))))",
56         ")))))))))))))",
57         "))))))))))))))",
58         ")))))))))))))))",
59         "))))))))))))))))",
60         ")))))))))))))))))",
61         "))))))))))))))))))",
62         ")))))))))))))))))))",
63         "))))))))))))))))))))",
64         ")))))))))))))))))))))",
65         "))))))))))))))))))))))",
66         ")))))))))))))))))))))))",
67         "))))))))))))))))))))))))",
68         ")))))))))))))))))))))))))",
69         "))))))))))))))))))))))))))",
70         ")))))))))))))))))))))))))))",
71         "))))))))))))))))))))))))))))",
72         ")))))))))))))))))))))))))))))",
73         "))))))))))))))))))))))))))))))",
74         ")))))))))))))))))))))))))))))))",
75         "))))))))))))))))))))))))))))))))",
76         ")))))))))))))))))))))))))))))))))",
77         "))))))))))))))))))))))))))))))))))",
78         ")))))))))))))))))))))))))))))))))))",
79         "))))))))))))))))))))))))))))))))))))",
80         ")))))))))))))))))))))))))))))))))))))",
81         "))))))))))))))))))))))))))))))))))))))",
82         ")))))))))))))))))))))))))))))))))))))))",
83         "))))))))))))))))))))))))))))))))))))))))",
84         ")))))))))))))))))))))))))))))))))))))))))",
85         "))))))))))))))))))))))))))))))))))))))))))",
86         ")))))))))))))))))))))))))))))))))))))))))))",
87         "))))))))))))))))))))))))))))))))))))))))))))",
88         ")))))))))))))))))))))))))))))))))))))))))))))",
89         "))))))))))))))))))))))))))))))))))))))))))))))",
90         ")))))))))))))))))))))))))))))))))))))))))))))))",
91         "))))))))))))))))))))))))))))))))))))))))))))))))",
92         ")))))))))))))))))))))))))))))))))))))))))))))))))",
93         "))))))))))))))))))))))))))))))))))))))))))))))))))",
94         ")))))))))))))))))))))))))))))))))))))))))))))))))))",
95         "))))))))))))))))))))))))))))))))))))))))))))))))))))",
96         ")))))))))))))))))))))))))))))))))))))))))))))))))))))",
97         "))))))))))))))))))))))))))))))))))))))))))))))))))))))",
98         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))",
99         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
100         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
101         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
102         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
103         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
104         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
105         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
106         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
107         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
108         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
109         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
110         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
111         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
112         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
113         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
114         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
115         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
116         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
117         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
118         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
119         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
120         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
121         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
122         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
123         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
124         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
125         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
126         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
127         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
128         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
129         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
130         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
131         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
132         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
133         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
134         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
135         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
136         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
137         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
138         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
139         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
140         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
141         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
142         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
143         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
144         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
145         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
146         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
147         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
148         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
149         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
150         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
151         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
152         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
153         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
154         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
155         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
156         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
157         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
158         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
159         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
160         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
161         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
162         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
163         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
164         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
165         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
166         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
167         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
168         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
169         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
170         ")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
171         "))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
172 };
173
174 static Eina_Bool control_sensors_cb(void *data)
175 {
176         unsigned int sl_value = 0;
177         unsigned int index = 0;
178
179         if (resource_read_sound_level_sensor(0, &sl_value) == -1)
180                 _E("failed to resource_read_sound_level_sensor()");
181
182         index = sl_value/8;
183         _D("value : %4u   %s", sl_value, bar[index]);
184
185         return ECORE_CALLBACK_RENEW;
186 }
187
188 static bool service_app_create(void *data)
189 {
190         app_data *ad = data;
191         int ret = -1;
192
193         /**
194          * No modification required!!!
195          * Access only when modifying internal functions.
196          */
197         controller_init_internal_functions();
198
199         /**
200          * Creates a timer to call the given function in the given period of time.
201          * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
202          */
203         ad->getter_timer = ecore_timer_add(SENSORING_TIME_INTERVAL, control_sensors_cb, ad);
204         if (!ad->getter_timer) {
205                 _E("Failed to add infrared motion getter timer");
206                 return false;
207         }
208
209         return true;
210 }
211
212 static void service_app_terminate(void *data)
213 {
214         app_data *ad = (app_data *)data;
215
216         if (ad->getter_timer)
217                 ecore_timer_del(ad->getter_timer);
218
219         /**
220          * No modification required!!!
221          * Access only when modifying internal functions.
222          */
223         controller_fini_internal_functions();
224
225         free(ad);
226 }
227
228 static void service_app_control(app_control_h app_control, void *data)
229 {
230         // Todo: add your code here.
231 }
232
233 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
234 {
235         /*APP_EVENT_LANGUAGE_CHANGED*/
236 }
237
238 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
239 {
240         /*APP_EVENT_REGION_FORMAT_CHANGED*/
241 }
242
243 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
244 {
245         /*APP_EVENT_LOW_BATTERY*/
246 }
247
248 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
249 {
250         /*APP_EVENT_LOW_MEMORY*/
251 }
252
253 int main(int argc, char* argv[])
254 {
255         app_data *ad = NULL;
256         int ret = 0;
257         service_app_lifecycle_callback_s event_callback;
258         app_event_handler_h handlers[5] = {NULL, };
259
260         ad = calloc(1, sizeof(app_data));
261         retv_if(!ad, -1);
262
263         event_callback.create = service_app_create;
264         event_callback.terminate = service_app_terminate;
265         event_callback.app_control = service_app_control;
266
267         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
268         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
269         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
270         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
271
272         ret = service_app_main(argc, argv, &event_callback, ad);
273
274         return ret;
275 }