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