Merge branch 'master' of ssh://review.tizen.org:29418/apps/native/position-finder...
[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
33 #define CONNECTIVITY_KEY "opened"
34 #define SENSORING_TIME_INTERVAL 5.0f
35
36 typedef struct app_data_s {
37         Ecore_Timer *getter_timer;
38         connectivity_resource_s *resource_info;
39 } app_data;
40
41 static Eina_Bool control_sensors_cb(void *data)
42 {
43         app_data *ad = data;
44
45         if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, true) == -1)
46                 _E("Cannot notify message");
47
48         return ECORE_CALLBACK_RENEW;
49 }
50
51 static bool service_app_create(void *data)
52 {
53         app_data *ad = data;
54         int ret = -1;
55
56         /**
57          * No modification required!!!
58          * Access only when modifying internal functions.
59          */
60         controller_init_internal_functions();
61
62         /**
63          * Create a connectivity resource and registers the resource in server.
64          */
65         ret = connectivity_set_resource(NULL, "org.tizen.door", &ad->resource_info);
66         if (ret == -1) _E("Cannot broadcast resource");
67
68         /**
69          * Creates a timer to call the given function in the given period of time.
70          * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
71          */
72         ad->getter_timer = ecore_timer_add(SENSORING_TIME_INTERVAL, control_sensors_cb, ad);
73         if (!ad->getter_timer) {
74                 _E("Failed to add infrared motion getter timer");
75                 return false;
76         }
77
78     return true;
79 }
80
81 static void service_app_terminate(void *data)
82 {
83         app_data *ad = (app_data *)data;
84
85         if (ad->getter_timer) {
86                 ecore_timer_del(ad->getter_timer);
87         }
88
89         /**
90          * Releases the resource about connectivity.
91          */
92         connectivity_unset_resource(ad->resource_info);
93
94         /**
95          * No modification required!!!
96          * Access only when modifying internal functions.
97          */
98         controller_fini_internal_functions();
99
100         free(ad);
101 }
102
103 static void service_app_control(app_control_h app_control, void *data)
104 {
105     // Todo: add your code here.
106 }
107
108 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
109 {
110         /*APP_EVENT_LANGUAGE_CHANGED*/
111 }
112
113 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
114 {
115         /*APP_EVENT_REGION_FORMAT_CHANGED*/
116 }
117
118 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
119 {
120         /*APP_EVENT_LOW_BATTERY*/
121 }
122
123 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
124 {
125         /*APP_EVENT_LOW_MEMORY*/
126 }
127
128 int main(int argc, char* argv[])
129 {
130         app_data *ad = NULL;
131         int ret = 0;
132         service_app_lifecycle_callback_s event_callback;
133         app_event_handler_h handlers[5] = {NULL, };
134
135         ad = calloc(1, sizeof(app_data));
136         retv_if(!ad, -1);
137
138         event_callback.create = service_app_create;
139         event_callback.terminate = service_app_terminate;
140         event_callback.app_control = service_app_control;
141
142         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
143         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
144         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
145         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
146
147         ret = service_app_main(argc, argv, &event_callback, ad);
148
149         return ret;
150 }