remove random gen, add more error handling
[apps/native/st-things-co2-meter.git] / src / co2.c
1 /* ****************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ******************************************************************/
18
19 #include <tizen.h>
20 #include <service_app.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <Ecore.h>
24
25 #include "st_things.h"
26 #include "log.h"
27 #include "sensor-data.h"
28 #include "co2-sensor.h"
29
30 #define JSON_NAME "device_def.json"
31 #define SENSOR_URI_CO2 "/capability/airQualitySensor/main/0"
32 #define SENSOR_KEY_CO2 "airQuality"
33 #define SENSOR_KEY_RANGE "range"
34
35 #define SENSOR_CH_CO2 (0)
36 #define SENSOR_GATHER_INTERVAL (0.05f)
37 #define SENSOR_GATHER_COUNT (60)
38
39 //#define USE_ST_SDK
40
41 typedef struct app_data_s {
42         Ecore_Timer *getter_co2;
43         sensor_data *co2_data;
44 } app_data;
45
46 static app_data *g_ad = NULL;
47
48 static Eina_Bool __get_co2(void *data)
49 {
50         int ret = 0;
51         unsigned int value = 0;
52         static unsigned int sum = 0;
53         static unsigned int count = 0;
54
55         app_data *ad = data;
56
57         if (!ad) {
58                 _E("failed to get app_data");
59                 service_app_exit();
60                 return ECORE_CALLBACK_CANCEL;
61         }
62
63         if (!ad->co2_data) {
64                 _E("failed to get co2_data");
65                 service_app_exit();
66                 ad->getter_co2 = NULL;
67                 return ECORE_CALLBACK_CANCEL;
68         }
69
70         ret = co2_sensor_read(SENSOR_CH_CO2, &value);
71         retv_if(ret != 0, ECORE_CALLBACK_RENEW);
72
73         count++;
74         sum += value;
75
76         if (count == SENSOR_GATHER_COUNT) {
77                 unsigned int avg = 0;
78                 avg = sum/SENSOR_GATHER_COUNT;
79
80                 _D("co2 avg - [%u], [%u ppm]", avg, co2_sensor_value_to_ppm(avg));
81
82                 sensor_data_set_uint(ad->co2_data, avg);
83
84 #ifdef USE_ST_SDK
85                 st_things_notify_observers(SENSOR_URI_CO2);
86 #endif
87                 count = 0;
88                 sum = 0;
89         }
90
91         return ECORE_CALLBACK_RENEW;
92 }
93
94 void gathering_start(void *data)
95 {
96         app_data *ad = data;
97         ret_if(!ad);
98
99         if (ad->getter_co2)
100                 ecore_timer_del(ad->getter_co2);
101
102         ad->getter_co2 = ecore_timer_add(SENSOR_GATHER_INTERVAL, __get_co2, ad);
103         if (!ad->getter_co2)
104                 _E("Failed to add getter_co2");
105
106         return;
107 }
108
109 #ifdef USE_ST_SDK
110 static bool handle_reset_request(void)
111 {
112         _D("Received a request for RESET.");
113         return false;
114 }
115
116 static void handle_reset_result(bool result)
117 {
118         _D("Reset %s.\n", result ? "succeeded" : "failed");
119 }
120
121 static bool handle_ownership_transfer_request(void)
122 {
123         _D("Received a request for Ownership-transfer.");
124         return true;
125 }
126
127 static void handle_things_status_change(st_things_status_e things_status)
128 {
129         _D("Things status is changed: %d\n", things_status);
130
131         if (things_status == ST_THINGS_STATUS_REGISTERED_TO_CLOUD)
132                 ecore_main_loop_thread_safe_call_async(gathering_start, g_ad);
133 }
134
135 static bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
136 {
137         _D("resource_uri [%s]", req_msg->resource_uri);
138         retv_if(!g_ad, false);
139
140         if (0 == strcmp(req_msg->resource_uri, SENSOR_URI_CO2)) {
141                 _D("query : %s, property: %s", req_msg->query, req_msg->property_key);
142
143                 if (req_msg->has_property_key(req_msg, SENSOR_KEY_CO2)) {
144                         unsigned int value = 0;
145                         sensor_data_get_uint(g_ad->co2_data, &value);
146                         resp_rep->set_int_value(resp_rep, SENSOR_KEY_CO2, value);
147                 }
148                 if (req_msg->has_property_key(req_msg, SENSOR_KEY_RANGE)) {
149                         const double range[2] = { 0.0, 1024.0 };
150                         resp_rep->set_double_array_value(resp_rep, SENSOR_KEY_RANGE, range, 2);
151                 }
152                 return true;
153         }
154         _E("not supported uri");
155         return false;
156 }
157
158 static bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
159 {
160         _D("resource_uri [%s]", req_msg->resource_uri);
161         return false;
162 }
163
164 static int __things_init(void)
165 {
166         bool easysetup_complete = false;
167         char app_json_path[128] = {'\0', };
168         char *app_res_path = NULL;
169         char *app_data_path = NULL;
170
171         app_res_path = app_get_resource_path();
172         if (!app_res_path) {
173                 _E("app_res_path is NULL!!");
174                 return -1;
175         }
176
177         app_data_path = app_get_data_path();
178         if (!app_data_path) {
179                 _E("app_data_path is NULL!!");
180                 free(app_res_path);
181                 return -1;
182         }
183
184         if (0 != st_things_set_configuration_prefix_path(app_res_path, app_data_path)) {
185                 _E("st_things_set_configuration_prefix_path() failed!!");
186                 free(app_res_path);
187                 free(app_data_path);
188                 return -1;
189         }
190         free(app_data_path);
191
192         snprintf(app_json_path, sizeof(app_json_path), "%s%s", app_res_path, JSON_NAME);
193         free(app_res_path);
194
195         if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
196                 _E("st_things_initialize() failed!!");
197                 return -1;
198         }
199
200         _D("easysetup_complete:[%d] ", easysetup_complete);
201
202         st_things_register_request_cb(handle_get_request, handle_set_request);
203         st_things_register_reset_cb(handle_reset_request, handle_reset_result);
204         st_things_register_user_confirm_cb(handle_ownership_transfer_request);
205         st_things_register_things_status_change_cb(handle_things_status_change);
206
207         return 0;
208 }
209
210 static int __things_deinit(void)
211 {
212         st_things_deinitialize();
213         return 0;
214 }
215
216 static int __things_start(void)
217 {
218         st_things_start();
219         return 0;
220 }
221
222 static int __things_stop(void)
223 {
224         st_things_stop();
225         return 0;
226 }
227 #endif /* USE_ST_SDK */
228
229 static bool service_app_create(void *user_data)
230 {
231         app_data *ad = (app_data *)user_data;
232
233         ad->co2_data = sensor_data_new(SENSOR_DATA_TYPE_UINT);
234         if (!ad->co2_data)
235                 return false;
236
237 #ifdef USE_ST_SDK
238         if (__things_init())
239                 return false;
240 #endif
241
242         return true;
243 }
244
245 static void service_app_control(app_control_h app_control, void *user_data)
246 {
247 #ifdef USE_ST_SDK
248         __things_start();
249 #else
250         gathering_start(user_data);
251 #endif
252 }
253
254 static void service_app_terminate(void *user_data)
255 {
256         app_data *ad = (app_data *)user_data;
257
258         if (!ad)
259                 return;
260
261         if (ad->getter_co2)
262                 ecore_timer_del(ad->getter_co2);
263
264 #ifdef USE_ST_SDK
265         __things_stop();
266         __things_deinit();
267 #endif
268
269         sensor_data_free(ad->co2_data);
270         co2_sensor_close();
271         free(ad);
272
273         FN_END;
274 }
275
276 int main(int argc, char *argv[])
277 {
278         app_data *ad = NULL;
279         service_app_lifecycle_callback_s event_callback;
280
281         ad = calloc(1, sizeof(app_data));
282         retv_if(!ad, -1);
283
284         g_ad = ad;
285
286         event_callback.create = service_app_create;
287         event_callback.terminate = service_app_terminate;
288         event_callback.app_control = service_app_control;
289
290         return service_app_main(argc, argv, &event_callback, ad);
291 }
292