Change peripheral io APIs
[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  *          Jeonghoon Park <jh1979.park@samsung.com>
9  *
10  * Licensed under the Flora License, Version 1.1 (the License);
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://floralicense.org/license/
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an AS IS BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 #include <unistd.h>
24 #include <glib.h>
25 #include <Ecore.h>
26 #include <tizen.h>
27 #include <service_app.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 #define CONNECTIVITY_KEY "opened"
40
41 typedef struct app_data_s {
42         Ecore_Timer *getter_timer;
43         connectivity_resource_s *resource_info;
44 } app_data;
45
46 static Eina_Bool control_sensors_cb(void *data)
47 {
48         uint32_t value[MULTIPLE_SENSOR_NUMBER] = { 0, };
49         int total = 0;
50         int gpio_num[MULTIPLE_SENSOR_NUMBER] = { 5, 6, 13, 19, 26 };
51         int i = 0;
52         app_data *ad = data;
53
54         /**
55          * This is the case when a number of the same sensors are installed.
56          * Each of the five infrared motion sensors will receive the value.
57          */
58         for (i = 0; i < MULTIPLE_SENSOR_NUMBER; i++) {
59                 /**
60                  * Infrared motion sensor outputs 1 if motion is detected, or 0 if motion is not detected.
61                  */
62                 if (resource_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
63                         _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
64                         continue;
65                 }
66                 /**
67                  * If at least one of the five infrared motion sensors detects motion (1),
68                  * it is judged that there is a person (total == 1).
69                  */
70                 total |= value[i];
71         }
72         _I("[5:%d] | [6:%d] | [13:%d] | [19:%d] | [26:%d] = [Total:%d]", value[0], value[1], value[2], value[3], value[4], total);
73
74         /**
75          * Notifies specific clients that resource's attributes have changed.
76          */
77         if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, total) == -1)
78                 _E("Cannot notify message");
79
80         return ECORE_CALLBACK_RENEW;
81 }
82
83 static bool service_app_create(void *data)
84 {
85         app_data *ad = data;
86         int ret = -1;
87
88         /**
89          * No modification required!!!
90          * Access only when modifying internal functions.
91          */
92         controller_init_internal_functions();
93
94         /**
95          * Create a connectivity resource and registers the resource in server.
96          */
97         ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
98         if (ret == -1) _E("Cannot broadcast resource");
99
100         /**
101          * Creates a timer to call the given function in the given period of time.
102          * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
103          */
104         ad->getter_timer = ecore_timer_add(0.5f, control_sensors_cb, ad);
105         if (!ad->getter_timer) {
106                 _E("Failed to add infrared motion getter timer");
107                 return false;
108         }
109
110     return true;
111 }
112
113 static void service_app_terminate(void *data)
114 {
115         app_data *ad = (app_data *)data;
116
117         for (int i = 0; i < PIN_MAX; i++) {
118                 if (ad->getter_timer) {
119                         ecore_timer_del(ad->getter_timer);
120                 }
121         }
122
123         /**
124          * Releases the resource about connectivity.
125          */
126         connectivity_unset_resource(ad->resource_info);
127
128         /**
129          * No modification required!!!
130          * Access only when modifying internal functions.
131          */
132         controller_fini_internal_functions();
133
134         free(ad);
135 }
136
137 static void service_app_control(app_control_h app_control, void *data)
138 {
139     // Todo: add your code here.
140 }
141
142 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
143 {
144         /*APP_EVENT_LANGUAGE_CHANGED*/
145 }
146
147 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
148 {
149         /*APP_EVENT_REGION_FORMAT_CHANGED*/
150 }
151
152 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
153 {
154         /*APP_EVENT_LOW_BATTERY*/
155 }
156
157 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
158 {
159         /*APP_EVENT_LOW_MEMORY*/
160 }
161
162 int main(int argc, char* argv[])
163 {
164         app_data *ad = NULL;
165         int ret = 0;
166         service_app_lifecycle_callback_s event_callback;
167         app_event_handler_h handlers[5] = {NULL, };
168
169         ad = calloc(1, sizeof(app_data));
170         retv_if(!ad, -1);
171
172         event_callback.create = service_app_create;
173         event_callback.terminate = service_app_terminate;
174         event_callback.app_control = service_app_control;
175
176         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
177         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
178         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
179         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
180
181         ret = service_app_main(argc, argv, &event_callback, ad);
182
183         return ret;
184 }