f6b79ddd0f833baf6b0488e26a5a9a5d32a44b8f
[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 GPIO_ULTRASONIC_TRIG_NUM_1 20
37 #define GPIO_ULTRASONIC_ECHO_NUM_1 21
38 #define MULTIPLE_SENSOR_NUMBER 5
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_read_sensors_cb(void *data)
46 {
47         int value[MULTIPLE_SENSOR_NUMBER] = { 0, };
48         int total = 0;
49         int gpio_num[MULTIPLE_SENSOR_NUMBER] = { 5, 6, 13, 19, 26 };
50         int i = 0;
51         app_data *ad = data;
52
53         for (i = 0; i < MULTIPLE_SENSOR_NUMBER; i++) {
54                 if (resource_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
55                         _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
56                         continue;
57                 }
58                 total |= value[i];
59         }
60
61         if (connectivity_notify(ad->resource_info, total) == -1)
62                 _E("Cannot notify message");
63
64         _I("[5:%d] | [6:%d] | [13:%d] | [19:%d] | [26:%d] = [Total:%d]", value[0], value[1], value[2], value[3], value[4], total);
65
66         return ECORE_CALLBACK_RENEW;
67 }
68
69 static bool service_app_create(void *data)
70 {
71         app_data *ad = data;
72         int ret = -1;
73
74         controller_init_internal_functions();
75         ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
76         if (ret == -1) _E("Cannot broadcast resource");
77
78         ad->getter_timer = ecore_timer_add(0.5f, control_read_sensors_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         for (int i = 0; i < PIN_MAX; i++) {
92                 if (ad->getter_timer) {
93                         ecore_timer_del(ad->getter_timer);
94                 }
95         }
96
97         connectivity_unset_resource(ad->resource_info);
98         controller_fini_internal_functions();
99
100         free(ad);
101 }
102
103 static void service_app_control(app_control_h app_control, void *data)
104 {
105     // Todo: add your code here.
106 }
107
108 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
109 {
110         /*APP_EVENT_LANGUAGE_CHANGED*/
111 }
112
113 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
114 {
115         /*APP_EVENT_REGION_FORMAT_CHANGED*/
116 }
117
118 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
119 {
120         /*APP_EVENT_LOW_BATTERY*/
121 }
122
123 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
124 {
125         /*APP_EVENT_LOW_MEMORY*/
126 }
127
128 int main(int argc, char* argv[])
129 {
130         app_data *ad = NULL;
131         int ret = 0;
132         service_app_lifecycle_callback_s event_callback;
133         app_event_handler_h handlers[5] = {NULL, };
134
135         ad = calloc(1, sizeof(app_data));
136
137         event_callback.create = service_app_create;
138         event_callback.terminate = service_app_terminate;
139         event_callback.app_control = service_app_control;
140
141         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
142         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
143         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
144         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
145
146         ret = service_app_main(argc, argv, &event_callback, ad);
147
148         return ret;
149 }