Change the lisence : apache -> flora
[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 <tizen.h>
23 #include <Ecore.h>
24 #include <service_app.h>
25 #include <unistd.h>
26 #include <glib.h>
27
28 #include <iotcon.h> // Please remove this after test
29
30 #include "log.h"
31 #include "resource.h"
32 #include "connectivity.h"
33 #include "controller.h"
34
35 #define GPIO_ULTRASONIC_TRIG_NUM_1 20
36 #define GPIO_ULTRASONIC_ECHO_NUM_1 21
37 #define MULTIPLE_SENSOR_NUMBER 5
38
39 typedef struct app_data_s {
40         Ecore_Timer *getter_timer;
41         connectivity_resource_s *resource_info;
42 } app_data;
43
44 static Eina_Bool control_read_sensors_cb(void *data)
45 {
46         int value[MULTIPLE_SENSOR_NUMBER] = { 0, };
47         int total = 0;
48         int gpio_num[MULTIPLE_SENSOR_NUMBER] = { 5, 6, 13, 19, 26 };
49         int i = 0;
50         app_data *ad = data;
51
52         for (i = 0; i < MULTIPLE_SENSOR_NUMBER; i++) {
53                 if (resource_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
54                         _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
55                         continue;
56                 }
57                 total |= value[i];
58         }
59
60         if (connectivity_notify(ad->resource_info, total) == -1)
61                 _E("Cannot notify message");
62
63         _I("[5:%d] | [6:%d] | [13:%d] | [19:%d] | [26:%d] = [Total:%d]", value[0], value[1], value[2], value[3], value[4], total);
64
65         return ECORE_CALLBACK_RENEW;
66 }
67
68 static bool service_app_create(void *data)
69 {
70         app_data *ad = data;
71         int ret = -1;
72
73         controller_init_internal_functions();
74         ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
75         if (ret == -1) _E("Cannot broadcast resource");
76
77         ad->getter_timer = ecore_timer_add(0.5f, control_read_sensors_cb, ad);
78         if (!ad->getter_timer) {
79                 _E("Failed to add infrared motion getter timer");
80                 return false;
81         }
82
83     return true;
84 }
85
86 static void service_app_terminate(void *data)
87 {
88         app_data *ad = (app_data *)data;
89
90         for (int i = 0; i < PIN_MAX; i++) {
91                 if (ad->getter_timer) {
92                         ecore_timer_del(ad->getter_timer);
93                 }
94         }
95
96         connectivity_unset_resource(ad->resource_info);
97         controller_fini_internal_functions();
98
99         free(ad);
100 }
101
102 static void service_app_control(app_control_h app_control, void *data)
103 {
104     // Todo: add your code here.
105 }
106
107 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
108 {
109         /*APP_EVENT_LANGUAGE_CHANGED*/
110 }
111
112 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
113 {
114         /*APP_EVENT_REGION_FORMAT_CHANGED*/
115 }
116
117 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
118 {
119         /*APP_EVENT_LOW_BATTERY*/
120 }
121
122 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
123 {
124         /*APP_EVENT_LOW_MEMORY*/
125 }
126
127 int main(int argc, char* argv[])
128 {
129         app_data *ad = NULL;
130         int ret = 0;
131         service_app_lifecycle_callback_s event_callback;
132         app_event_handler_h handlers[5] = {NULL, };
133
134         ad = calloc(1, sizeof(app_data));
135
136         event_callback.create = service_app_create;
137         event_callback.terminate = service_app_terminate;
138         event_callback.app_control = service_app_control;
139
140         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
141         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
142         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
143         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
144
145         ret = service_app_main(argc, argv, &event_callback, ad);
146
147         return ret;
148 }