Change parameter name for user_data and remove unnecessary return
[apps/native/smart-ruler.git] / src / controller.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Ecore.h>
18 #include <tizen.h>
19 #include <service_app.h>
20
21 #include "log.h"
22 #include "resource.h"
23 #include "sensor-data.h"
24
25 #define SENSORING_TIME_INTERVAL (0.5)
26
27 // For using SmartThings SDK
28 #define USE_ST_SDK
29
30 #ifdef USE_ST_SDK
31 #include "st-master.h"
32 #include "st-resource.h"
33 #endif /* USE_ST_SDK */
34
35 typedef struct app_data_s {
36         Ecore_Timer *getter_timer;
37         sensor_data *lidar_data;
38 } app_data;
39
40 static Eina_Bool _lidar_value_read_cb(void *user_data)
41 {
42         app_data *ad = user_data;
43
44         int ret = 0;
45         unsigned int lidar_value = 0;
46
47         if (!ad) {
48                 _E("app_data is NULL");
49                 service_app_exit();
50         }
51
52         resource_write_led(5, 1);
53         ret = resource_read_lidar_v3(&lidar_value);
54         retv_if(ret != 0, ECORE_CALLBACK_RENEW);
55         resource_write_led(5, 0);
56
57         _D("distance : %u cm", lidar_value);
58         sensor_data_set_uint(ad->lidar_data, lidar_value);
59
60         return ECORE_CALLBACK_RENEW;
61 }
62
63 static void service_app_control(app_control_h app_control, void *user_data)
64 {
65         app_data *ad = user_data;
66
67         if (ad->getter_timer)
68                 ecore_timer_del(ad->getter_timer);
69
70         ad->getter_timer = ecore_timer_add(SENSORING_TIME_INTERVAL, _lidar_value_read_cb, ad);
71         if (!ad->getter_timer) {
72                 _E("Failed to add getter timer");
73                 return;
74         }
75 }
76
77 static bool service_app_create(void *user_data)
78 {
79         app_data *ad = user_data;
80
81         ad->lidar_data = sensor_data_new(SENSOR_DATA_TYPE_UINT);
82         retv_if(!ad->lidar_data, false);
83
84 #ifdef USE_ST_SDK
85         if (st_master_create())
86                 return false;
87
88         if (st_resource_create(ad->lidar_data)) {
89                 st_master_destroy();
90                 return false;
91         }
92 #endif
93
94         return true;
95 }
96
97 static void service_app_terminate(void *user_data)
98 {
99         app_data *ad = user_data;
100
101 #ifdef USE_ST_SDK
102         st_resource_destroy();
103         st_master_destroy();
104 #endif
105
106         sensor_data_free(ad->lidar_data);
107         ad->lidar_data = NULL;
108
109         if (ad->getter_timer) {
110                 ecore_timer_del(ad->getter_timer);
111                 ad->getter_timer = NULL;
112         }
113
114         resource_close_all();
115 }
116
117 int main(int argc, char* argv[])
118 {
119         app_data ad;
120         int ret = 0;
121         service_app_lifecycle_callback_s event_callback;
122
123         ad.getter_timer = NULL;
124         ad.lidar_data = NULL;
125
126         event_callback.create = service_app_create;
127         event_callback.terminate = service_app_terminate;
128         event_callback.app_control = service_app_control;
129
130         ret = service_app_main(argc, argv, &event_callback, &ad);
131
132         return ret;
133 }