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  *
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 <unistd.h>
23 #include <glib.h>
24 #include <Ecore.h>
25 #include <tizen.h>
26 #include <service_app.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 #define CONNECTIVITY_KEY "opened"
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_sensors_cb(void *data)
46 {
47         uint32_t 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         /**
54          * This is the case when a number of the same sensors are installed.
55          * Each of the five infrared motion sensors will receive the value.
56          */
57         for (i = 0; i < MULTIPLE_SENSOR_NUMBER; i++) {
58                 /**
59                  * Infrared motion sensor outputs 1 if motion is detected, or 0 if motion is not detected.
60                  */
61                 if (resource_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
62                         _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
63                         continue;
64                 }
65                 /**
66                  * If at least one of the five infrared motion sensors detects motion (1),
67                  * it is judged that there is a person (total == 1).
68                  */
69                 total |= value[i];
70         }
71         _I("[5:%d] | [6:%d] | [13:%d] | [19:%d] | [26:%d] = [Total:%d]", value[0], value[1], value[2], value[3], value[4], total);
72
73         /**
74          * Notifies specific clients that resource's attributes have changed.
75          */
76         if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, total) == -1)
77                 _E("Cannot notify message");
78
79         return ECORE_CALLBACK_RENEW;
80 }
81
82 static bool service_app_create(void *data)
83 {
84         app_data *ad = data;
85         int ret = -1;
86
87         /**
88          * No modification required!!!
89          * Access only when modifying internal functions.
90          */
91         controller_init_internal_functions();
92
93         /**
94          * Create a connectivity resource and registers the resource in server.
95          */
96         ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
97         if (ret == -1) _E("Cannot broadcast resource");
98
99         /**
100          * Creates a timer to call the given function in the given period of time.
101          * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
102          */
103         ad->getter_timer = ecore_timer_add(0.5f, control_sensors_cb, ad);
104         if (!ad->getter_timer) {
105                 _E("Failed to add infrared motion getter timer");
106                 return false;
107         }
108
109     return true;
110 }
111
112 static void service_app_terminate(void *data)
113 {
114         app_data *ad = (app_data *)data;
115
116         for (int i = 0; i < PIN_MAX; i++) {
117                 if (ad->getter_timer) {
118                         ecore_timer_del(ad->getter_timer);
119                 }
120         }
121
122         /**
123          * Releases the resource about connectivity.
124          */
125         connectivity_unset_resource(ad->resource_info);
126
127         /**
128          * No modification required!!!
129          * Access only when modifying internal functions.
130          */
131         controller_fini_internal_functions();
132
133         free(ad);
134 }
135
136 static void service_app_control(app_control_h app_control, void *data)
137 {
138     // Todo: add your code here.
139 }
140
141 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
142 {
143         /*APP_EVENT_LANGUAGE_CHANGED*/
144 }
145
146 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
147 {
148         /*APP_EVENT_REGION_FORMAT_CHANGED*/
149 }
150
151 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
152 {
153         /*APP_EVENT_LOW_BATTERY*/
154 }
155
156 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
157 {
158         /*APP_EVENT_LOW_MEMORY*/
159 }
160
161 int main(int argc, char* argv[])
162 {
163         app_data *ad = NULL;
164         int ret = 0;
165         service_app_lifecycle_callback_s event_callback;
166         app_event_handler_h handlers[5] = {NULL, };
167
168         ad = calloc(1, sizeof(app_data));
169         retv_if(!ad, -1);
170
171         event_callback.create = service_app_create;
172         event_callback.terminate = service_app_terminate;
173         event_callback.app_control = service_app_control;
174
175         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
176         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
177         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
178         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
179
180         ret = service_app_main(argc, argv, &event_callback, ad);
181
182         return ret;
183 }