Challenge : Create server requried in Question#3
[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 LOCATION_RESOURCE_TYPE "org.tizen.location"
36 #define LOCATION_KEY "location"
37 #define LOCATION_MESSAGE "Dr.Evil is located in NEWYORK"
38 #define NOTIFY_TIME_INTERVAL 5.0f
39
40 typedef struct app_data_s {
41         Ecore_Timer *getter_timer;
42         connectivity_resource_s *resource_info;
43 } app_data;
44
45 static Eina_Bool _control_notify_cb(void *data)
46 {
47         app_data *ad = data;
48
49         if (connectivity_notify_string(ad->resource_info, LOCATION_KEY, LOCATION_MESSAGE) == -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         controller_util_get_path(&path);
71         ret = connectivity_set_resource(path, LOCATION_RESOURCE_TYPE, &ad->resource_info);
72         if (ret == -1) _E("Cannot broadcast resource");
73
74         /**
75          * Creates a timer to call the given function in the given period of time.
76          * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
77          */
78         ad->getter_timer = ecore_timer_add(NOTIFY_TIME_INTERVAL, _control_notify_cb, ad);
79         if (!ad->getter_timer) {
80                 _E("Failed to add infrared motion getter timer");
81                 return false;
82         }
83
84         return true;
85 }
86
87 static void service_app_terminate(void *data)
88 {
89         app_data *ad = (app_data *)data;
90
91         if (ad->getter_timer)
92                 ecore_timer_del(ad->getter_timer);
93
94
95         /**
96          * Releases the resource about connectivity.
97          */
98         connectivity_unset_resource(ad->resource_info);
99
100         /**
101          * No modification required!!!
102          * Access only when modifying internal functions.
103          */
104         controller_fini_internal_functions();
105
106         free(ad);
107 }
108
109 static void service_app_control(app_control_h app_control, void *data)
110 {
111         // Todo: add your code here.
112 }
113
114 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
115 {
116         /*APP_EVENT_LANGUAGE_CHANGED*/
117 }
118
119 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
120 {
121         /*APP_EVENT_REGION_FORMAT_CHANGED*/
122 }
123
124 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
125 {
126         /*APP_EVENT_LOW_BATTERY*/
127 }
128
129 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
130 {
131         /*APP_EVENT_LOW_MEMORY*/
132 }
133
134 int main(int argc, char* argv[])
135 {
136         app_data *ad = NULL;
137         int ret = 0;
138         service_app_lifecycle_callback_s event_callback;
139         app_event_handler_h handlers[5] = {NULL, };
140
141         ad = calloc(1, sizeof(app_data));
142         retv_if(!ad, -1);
143
144         event_callback.create = service_app_create;
145         event_callback.terminate = service_app_terminate;
146         event_callback.app_control = service_app_control;
147
148         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
149         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
150         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
151         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
152
153         ret = service_app_main(argc, argv, &event_callback, ad);
154
155         return ret;
156 }