Do not open functions for closing GPIO
[apps/native/rcc.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 "log.h"
30 #include "controller.h"
31 #include "resource.h"
32
33 #define I2C_BUS_1 0x1
34 #define GPIO_NOT_USED -1
35 #define GPIO_ULTRASONIC_TRIG_NUM_1 20
36 #define GPIO_ULTRASONIC_ECHO_NUM_1 21
37 #define GPIO_INFRARED_MOTION_NUM_1 4
38 #define I2C_ILLUMINANCE_FIRST_PIN_1 3
39
40 typedef struct app_data_s {
41         Ecore_Timer *getter_timer[PIN_MAX];
42 } app_data;
43
44 static Eina_Bool _infrared_motion_getter_timer(void *data)
45 {
46         int value = 0;
47
48         retv_if(resource_read_infrared_motion_sensor(GPIO_INFRARED_MOTION_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
49         _I("Infrared Motion Value is [%d]", value);
50
51         return ECORE_CALLBACK_RENEW;
52 }
53
54 static Eina_Bool _ultrasonic_getter_timer(void *data)
55 {
56         double value = 0;
57
58         retv_if(resource_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
59         _I("Ultra Sonic Distance is [%d cm]", value);
60
61         return ECORE_CALLBACK_RENEW;
62 }
63
64 static Eina_Bool _illuminance_getter_timer(void *data)
65 {
66         int value = 0;
67
68         retv_if(resource_read_illuminance_sensor(I2C_BUS_1, &value) == -1, ECORE_CALLBACK_CANCEL);
69         _I("Ultra Sonic Distance is [%d lux]", value);
70
71         return ECORE_CALLBACK_RENEW;
72 }
73
74 static bool service_app_create(void *data)
75 {
76         app_data *ad = data;
77
78         /* One Pin Sensor */
79         ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(3.0, _infrared_motion_getter_timer, ad);
80         if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
81                 _D("Failed to add infrared motion getter timer");
82                 return false;
83         }
84
85         /* Two Pins Sensor */
86         ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1] = ecore_timer_add(1.0, _ultrasonic_getter_timer, ad);
87         if (!ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1]) {
88                 _D("Failed to add ultra sonic getter timer");
89                 return false;
90         }
91
92         /* I2C Protocol Sensor */
93         ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1] = ecore_timer_add(1.0, _illuminance_getter_timer, ad);
94         if (!ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1]) {
95                 _D("Failed to add ultra sonic getter timer");
96                 return false;
97         }
98
99     return true;
100 }
101
102 static void service_app_terminate(void *data)
103 {
104         app_data *ad = (app_data *)data;
105
106         for (int i = 0; i < PIN_MAX; i++) {
107                 if (ad->getter_timer[i]) {
108                         ecore_timer_del(ad->getter_timer[i]);
109                 }
110         }
111         resource_close_all();
112         free(ad);
113 }
114
115 static void service_app_control(app_control_h app_control, void *data)
116 {
117     // Todo: add your code here.
118 }
119
120 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
121 {
122         /*APP_EVENT_LANGUAGE_CHANGED*/
123 }
124
125 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
126 {
127         /*APP_EVENT_REGION_FORMAT_CHANGED*/
128 }
129
130 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
131 {
132         /*APP_EVENT_LOW_BATTERY*/
133 }
134
135 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
136 {
137         /*APP_EVENT_LOW_MEMORY*/
138 }
139
140 int main(int argc, char* argv[])
141 {
142         app_data *ad = NULL;
143         int ret = 0;
144         service_app_lifecycle_callback_s event_callback;
145         app_event_handler_h handlers[5] = {NULL, };
146
147         ad = calloc(1, sizeof(app_data));
148
149         event_callback.create = service_app_create;
150         event_callback.terminate = service_app_terminate;
151         event_callback.app_control = service_app_control;
152
153         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
154         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
155         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
156         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
157
158         ret = service_app_main(argc, argv, &event_callback, ad);
159
160         return ret;
161 }