Template control.c with RCC Pattern
[apps/native/position-finder-server.git] / src / controller.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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 Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
23 #include <tizen.h>
24 #include <Ecore.h>
25 #include <service_app.h>
26 #include <unistd.h>
27 #include <glib.h>
28
29 #include <iotcon.h> // Please remove this after test
30
31 #include "log.h"
32 #include "resource.h"
33 #include "connectivity.h"
34 #include "controller.h"
35
36 #define I2C_BUS_1 0x1
37 #define GPIO_NOT_USED -1
38 #define GPIO_ULTRASONIC_TRIG_NUM_1 20
39 #define GPIO_ULTRASONIC_ECHO_NUM_1 21
40 #define GPIO_INFRARED_MOTION_NUM_1 4
41 #define I2C_ILLUMINANCE_FIRST_PIN_1 3
42 #define USE_MULTIPLE_SENSOR 1
43
44 static void _start_internal_function(void);
45 static void _stop_internal_function(void);
46
47 typedef struct app_data_s {
48         Ecore_Timer *getter_timer[PIN_MAX];
49         connectivity_resource_s *resource_info;
50 } app_data;
51
52 static Eina_Bool _infrared_motion_getter_timer(void *data)
53 {
54 #if USE_MULTIPLE_SENSOR
55         int gpio_num[3] = { 16, 23, 26 };
56         int i = 0;
57         int value[3] = { 0, };
58
59         for (i = 0; i < 3; i++) {
60                 if (resource_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
61                         _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
62                         continue;
63                 }
64                 _I("[GPIO:%d] Infrared Motion Value is [%d]", gpio_num[i], value[i]);
65         }
66
67
68         //@TODO: Send the data to Analyzer using connectivity APIs
69
70 #else
71         int value = 0;
72
73         retv_if(resource_read_infrared_motion_sensor(GPIO_INFRARED_MOTION_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
74         _I("Infrared Motion Value is [%d]", value);
75 #endif
76
77         return ECORE_CALLBACK_RENEW;
78 }
79
80 #ifndef USE_MULTIPLE_SENSOR
81 static Eina_Bool _ultrasonic_getter_timer(void *data)
82 {
83         double value = 0;
84
85         retv_if(resource_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
86         _I("Ultra Sonic Distance is [%d cm]", value);
87
88         return ECORE_CALLBACK_RENEW;
89 }
90
91 static Eina_Bool _illuminance_getter_timer(void *data)
92 {
93         int value = 0;
94
95         retv_if(resource_read_illuminance_sensor(I2C_BUS_1, &value) == -1, ECORE_CALLBACK_CANCEL);
96         _I("Ultra Sonic Distance is [%d lux]", value);
97
98         return ECORE_CALLBACK_RENEW;
99 }
100 #endif
101
102 static bool service_app_create(void *data)
103 {
104         app_data *ad = data;
105         int ret = -1;
106
107         _start_internal_function();
108
109         ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
110         if (ret == -1) _E("Cannot broadcast resource");
111
112 #if USE_MULTIPLE_SENSOR
113         ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(3.0, _infrared_motion_getter_timer, ad);
114         if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
115                 _E("Failed to add infrared motion getter timer");
116                 return false;
117         }
118 #else
119         /* One Pin Sensor */
120         ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(1.0f, _infrared_motion_getter_timer, ad);
121         if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
122                 _D("Failed to add infrared motion getter timer");
123                 return false;
124         }
125
126         /* Two Pins Sensor */
127         ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1] = ecore_timer_add(1.0, _ultrasonic_getter_timer, ad);
128         if (!ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1]) {
129                 _D("Failed to add ultra sonic getter timer");
130                 return false;
131         }
132
133         /* I2C Protocol Sensor */
134         ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1] = ecore_timer_add(1.0, _illuminance_getter_timer, ad);
135         if (!ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1]) {
136                 _D("Failed to add ultra sonic getter timer");
137                 return false;
138         }
139 #endif
140
141     return true;
142 }
143
144 static void service_app_terminate(void *data)
145 {
146         app_data *ad = (app_data *)data;
147
148         for (int i = 0; i < PIN_MAX; i++) {
149                 if (ad->getter_timer[i]) {
150                         ecore_timer_del(ad->getter_timer[i]);
151                 }
152         }
153
154         connectivity_unset_resource(ad->resource_info);
155         _stop_internal_function();
156
157         free(ad);
158 }
159
160 static void service_app_control(app_control_h app_control, void *data)
161 {
162     // Todo: add your code here.
163 }
164
165 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
166 {
167         /*APP_EVENT_LANGUAGE_CHANGED*/
168 }
169
170 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
171 {
172         /*APP_EVENT_REGION_FORMAT_CHANGED*/
173 }
174
175 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
176 {
177         /*APP_EVENT_LOW_BATTERY*/
178 }
179
180 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
181 {
182         /*APP_EVENT_LOW_MEMORY*/
183 }
184
185 int main(int argc, char* argv[])
186 {
187         app_data *ad = NULL;
188         int ret = 0;
189         service_app_lifecycle_callback_s event_callback;
190         app_event_handler_h handlers[5] = {NULL, };
191
192         ad = calloc(1, sizeof(app_data));
193
194         event_callback.create = service_app_create;
195         event_callback.terminate = service_app_terminate;
196         event_callback.app_control = service_app_control;
197
198         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
199         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
200         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
201         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
202
203         ret = service_app_main(argc, argv, &event_callback, ad);
204
205         return ret;
206 }
207
208 /* Do not modify codes under this comment */
209 static void _start_internal_function(void)
210 {
211         connectivity_init("iotcon-test-basic-server");
212 }
213
214 static void _stop_internal_function(void)
215 {
216         _I("Terminating...");
217         resource_close_all();
218         connectivity_fini();
219 }