MCC Pattern : Initial version
[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  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <tizen.h>
21 #include <Ecore.h>
22 #include <service_app.h>
23 #include <unistd.h>
24 #include <glib.h>
25
26 #include "log.h"
27 #include "controller.h"
28 #include "model.h"
29
30 #define GPIO_NOT_USED -1
31 #define GPIO_ULTRASONIC_TRIG_NUM 20
32 #define GPIO_ULTRASONIC_ECHO_NUM 21
33 #define GPIO_INFRARED_MOTION_NUM 4
34
35 typedef struct app_data_s {
36         model_sensor_h sensor_info;
37         Ecore_Timer *getter_timer;
38         void *event;
39 } app_data;
40
41 static Eina_Bool _ultrasonic_getter_timer(void *data)
42 {
43         app_data *ad = data;
44
45 #if 1
46         int value = 0;
47         retv_if(model_read_int_value(ad->sensor_info, &value) == -1, ECORE_CALLBACK_CANCEL);
48         _I("Ultrasonic Value is [%d]", value);
49 #else
50         double value = 0.0;
51         retv_if(model_read_double_value(ad->sensor_info, &value) == -1, ECORE_CALLBACK_RENEW);
52         _I("Value is [%f]", value);
53 #endif
54
55         return ECORE_CALLBACK_RENEW;
56 }
57
58 static Eina_Bool _infrared_motion_getter_timer(void *data)
59 {
60         app_data *ad = data;
61
62 #if 1
63         int value = 0;
64         retv_if(model_read_int_value(ad->sensor_info, &value) == -1, ECORE_CALLBACK_CANCEL);
65         _I("Infrared Motion Value is [%d]", value);
66 #else
67         double value = 0.0;
68         retv_if(model_read_double_value(ad->sensor_info, &value) == -1, ECORE_CALLBACK_RENEW);
69         _I("Value is [%f]", value);
70 #endif
71
72         return ECORE_CALLBACK_RENEW;
73 }
74
75 static bool service_app_create(void *data)
76 {
77         model_sensor_h sensor_info = NULL;
78         app_data *ad = data;
79
80         retv_if(model_init("Ultrasonic", SENSOR_TYPE_ULTRASONIC, GPIO_ULTRASONIC_TRIG_NUM, GPIO_ULTRASONIC_ECHO_NUM, &sensor_info) == -1, false);
81         ad->sensor_info = sensor_info;
82
83         ad->getter_timer = ecore_timer_add(3.0, _ultrasonic_getter_timer, ad);
84         if (!ad->getter_timer) {
85                 _D("Failed to add getter timer");
86                 return false;
87         }
88
89         retv_if(model_init("Infrared_motion", SENSOR_TYPE_INFRARED_MOTION, GPIO_INFRARED_MOTION_NUM, GPIO_NOT_USED, &sensor_info) == -1, false);
90         ad->sensor_info = sensor_info;
91
92         ad->getter_timer = ecore_timer_add(3.0, _infrared_motion_getter_timer, ad);
93         if (!ad->getter_timer) {
94                 _D("Failed to add getter timer");
95                 return false;
96         }
97
98     return true;
99 }
100
101 static void service_app_terminate(void *data)
102 {
103         app_data *ad = (app_data *)data;
104         ecore_timer_del(ad->getter_timer);
105         model_fini(ad->sensor_info);
106         free(ad);
107 }
108
109 static void service_app_control(app_control_h app_control, void *data)
110 {
111     // Todo: add your code here.
112 }
113
114 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
115 {
116         /*APP_EVENT_LANGUAGE_CHANGED*/
117 }
118
119 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
120 {
121         /*APP_EVENT_REGION_FORMAT_CHANGED*/
122 }
123
124 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
125 {
126         /*APP_EVENT_LOW_BATTERY*/
127 }
128
129 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
130 {
131         /*APP_EVENT_LOW_MEMORY*/
132 }
133
134 int main(int argc, char* argv[])
135 {
136         app_data *ad = NULL;
137         int ret = 0;
138         service_app_lifecycle_callback_s event_callback;
139         app_event_handler_h handlers[5] = {NULL, };
140
141         ad = calloc(1, sizeof(app_data));
142
143         event_callback.create = service_app_create;
144         event_callback.terminate = service_app_terminate;
145         event_callback.app_control = service_app_control;
146
147         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
148         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
149         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
150         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
151
152         ret = service_app_main(argc, argv, &event_callback, ad);
153
154         return ret;
155 }