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