Add an indent for consistency
[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
31 #define SENSORING_TIME_INTERVAL 5.0f
32
33 typedef struct app_data_s {
34         Ecore_Timer *getter_timer;
35 } app_data;
36
37 static Eina_Bool control_sensors_cb(void *data)
38 {
39         uint32_t value = 0;
40         int ret = 0;
41
42         ret = resource_read_infrared_motion_sensor(12, &value);
43         if (ret < 0) {
44                 _D("Cannot sense from your sensor");
45                 return ECORE_CALLBACK_RENEW;
46         }
47
48         _D("PIR Value : %d", value);
49
50         ret = resource_write_led(26, value);
51         if (ret < 0) {
52                 _D("Cannot write a value to your LED");
53                 return ECORE_CALLBACK_RENEW;
54         }
55
56         return ECORE_CALLBACK_RENEW;
57 }
58
59 static bool service_app_create(void *data)
60 {
61         app_data *ad = data;
62
63         /**
64          * Creates a timer to call the given function in the given period of time.
65          * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
66          */
67         ad->getter_timer = ecore_timer_add(SENSORING_TIME_INTERVAL, control_sensors_cb, ad);
68         if (!ad->getter_timer) {
69                 _E("Failed to add infrared motion getter timer");
70                 return false;
71         }
72
73         return true;
74 }
75
76 static void service_app_terminate(void *data)
77 {
78         app_data *ad = (app_data *)data;
79
80         if (ad->getter_timer)
81                 ecore_timer_del(ad->getter_timer);
82
83         free(ad);
84 }
85
86 static void service_app_control(app_control_h app_control, void *data)
87 {
88         // Todo: add your code here.
89 }
90
91 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
92 {
93         /*APP_EVENT_LANGUAGE_CHANGED*/
94 }
95
96 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
97 {
98         /*APP_EVENT_REGION_FORMAT_CHANGED*/
99 }
100
101 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
102 {
103         /*APP_EVENT_LOW_BATTERY*/
104 }
105
106 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
107 {
108         /*APP_EVENT_LOW_MEMORY*/
109 }
110
111 int main(int argc, char* argv[])
112 {
113         app_data *ad = NULL;
114         int ret = 0;
115         service_app_lifecycle_callback_s event_callback;
116         app_event_handler_h handlers[5] = {NULL, };
117
118         ad = calloc(1, sizeof(app_data));
119         retv_if(!ad, -1);
120
121         event_callback.create = service_app_create;
122         event_callback.terminate = service_app_terminate;
123         event_callback.app_control = service_app_control;
124
125         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
126         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
127         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
128         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
129
130         ret = service_app_main(argc, argv, &event_callback, ad);
131
132         return ret;
133 }