Encapsulate pre-defined functions into controller_internal.c
[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 0
43 #define MULTIPLE_SENSOR_NUMBER 5
44
45 typedef struct app_data_s {
46         Ecore_Timer *getter_timer[PIN_MAX];
47         connectivity_resource_s *resource_info;
48 } app_data;
49
50 static Eina_Bool _infrared_motion_getter_timer(void *data)
51 {
52 #if USE_MULTIPLE_SENSOR
53         int gpio_num[MULTIPLE_SENSOR_NUMBER] = { 5, 6, 13, 19, 26 };
54         int i = 0;
55         int value[MULTIPLE_SENSOR_NUMBER] = { 0, };
56         int detected = 0;
57         app_data *ad = data;
58
59         for (i = 0; i < MULTIPLE_SENSOR_NUMBER; 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                 detected |= value[i];
65         }
66         _I("[5:%d][6:%d][13:%d][19:%d][26:%d]", value[0], value[1], value[2], value[3], value[4]);
67
68         if (connectivity_notify(ad->resource_info, detected) == -1)
69                 _E("Cannot notify message");
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 #if (!USE_MULTIPLE_SENSOR)
81 static void ultrasonic_sensor_read_cb(double value, void *data)
82 {
83         _I("Distance : %.2fcm", value);
84 }
85
86 static Eina_Bool _ultrasonic_getter_timer(void *data)
87 {
88         double value = 0;
89
90         retv_if(resource_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, ultrasonic_sensor_read_cb, NULL) == -1, ECORE_CALLBACK_CANCEL);
91
92         return ECORE_CALLBACK_RENEW;
93 }
94
95 static Eina_Bool _illuminance_getter_timer(void *data)
96 {
97         int value = 0;
98
99         retv_if(resource_read_illuminance_sensor(I2C_BUS_1, &value) == -1, ECORE_CALLBACK_CANCEL);
100         _I("Illuminance sensor is [%d lux]", value);
101
102         return ECORE_CALLBACK_RENEW;
103 }
104 #endif
105
106 static bool service_app_create(void *data)
107 {
108         app_data *ad = data;
109         int ret = -1;
110
111         controller_init_internal_functions();
112
113         ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
114         if (ret == -1) _E("Cannot broadcast resource");
115
116 #if USE_MULTIPLE_SENSOR
117         ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(3.0f, _infrared_motion_getter_timer, ad);
118         if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
119                 _E("Failed to add infrared motion getter timer");
120                 return false;
121         }
122 #else
123         /* One Pin Sensor */
124         ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(1.0f, _infrared_motion_getter_timer, ad);
125         if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
126                 _D("Failed to add infrared motion getter timer");
127                 return false;
128         }
129
130         /* Two Pins Sensor */
131         ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1] = ecore_timer_add(1.0f, _ultrasonic_getter_timer, ad);
132         if (!ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1]) {
133                 _D("Failed to add ultra sonic getter timer");
134                 return false;
135         }
136
137         /* I2C Protocol Sensor */
138         ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1] = ecore_timer_add(1.0f, _illuminance_getter_timer, ad);
139         if (!ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1]) {
140                 _D("Failed to add illuminance getter timer");
141                 return false;
142         }
143 #endif
144
145     return true;
146 }
147
148 static void service_app_terminate(void *data)
149 {
150         app_data *ad = (app_data *)data;
151
152         for (int i = 0; i < PIN_MAX; i++) {
153                 if (ad->getter_timer[i]) {
154                         ecore_timer_del(ad->getter_timer[i]);
155                 }
156         }
157
158         connectivity_unset_resource(ad->resource_info);
159         controller_fini_internal_functions();
160
161         free(ad);
162 }
163
164 static void service_app_control(app_control_h app_control, void *data)
165 {
166     // Todo: add your code here.
167 }
168
169 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
170 {
171         /*APP_EVENT_LANGUAGE_CHANGED*/
172 }
173
174 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
175 {
176         /*APP_EVENT_REGION_FORMAT_CHANGED*/
177 }
178
179 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
180 {
181         /*APP_EVENT_LOW_BATTERY*/
182 }
183
184 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
185 {
186         /*APP_EVENT_LOW_MEMORY*/
187 }
188
189 int main(int argc, char* argv[])
190 {
191         app_data *ad = NULL;
192         int ret = 0;
193         service_app_lifecycle_callback_s event_callback;
194         app_event_handler_h handlers[5] = {NULL, };
195
196         ad = calloc(1, sizeof(app_data));
197
198         event_callback.create = service_app_create;
199         event_callback.terminate = service_app_terminate;
200         event_callback.app_control = service_app_control;
201
202         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
203         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
204         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
205         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
206
207         ret = service_app_main(argc, argv, &event_callback, ad);
208
209         return ret;
210 }