add not implemented fuctions for co2 sensor to use as self study part
[apps/native/st-things-co2-meter.git] / src / co2.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <tizen.h>
18 #include <service_app.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <Ecore.h>
22
23 #include "st_things.h"
24 #include "log.h"
25 #include "sensor-data.h"
26 #include "co2-sensor.h"
27
28 #define JSON_PATH "device_def.json"
29 #define SENSOR_URI_CO2 "/capability/airQualitySensor/main/0"
30 #define SENSOR_KEY_CO2 "airQuality"
31 #define SENSOR_KEY_RANGE "range"
32
33 #define SENSOR_CH_CO2 (0)
34 #define SENSOR_THRESHOLD_CO2 (650)
35
36 #define SENSOR_GATHER_INTERVAL (0.05f)
37 #define SENSOR_GATHER_COUNT (5)
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         }
61
62         if (!ad->co2_data) {
63                 _E("failed to get co2_data");
64                 service_app_exit();
65         }
66
67         ret = co2_sensor_read(SENSOR_CH_CO2, &value);
68         retv_if(ret != 0, ECORE_CALLBACK_RENEW);
69
70         count++;
71         sum += value;
72
73         if (count == SENSOR_GATHER_COUNT) {
74                 unsigned int avg = 0;
75                 avg = sum/SENSOR_GATHER_COUNT;
76
77                 _D("co2 avg value - %u", avg);
78                 _D("co2 avg voltage - %.2lfv", co2_sensor_value_to_voltage(avg));
79
80                 sensor_data_set_uint(ad->co2_data, avg);
81
82                 if (avg <= SENSOR_THRESHOLD_CO2) {
83         #ifdef USE_ST_SDK
84                         st_things_notify_observers(SENSOR_URI_CO2);
85         #endif
86                 }
87
88                 count = 0;
89                 sum = 0;
90         }
91
92         return ECORE_CALLBACK_RENEW;
93 }
94
95 void gathering_start(void *data)
96 {
97         app_data *ad = data;
98         ret_if(!ad);
99
100         if (ad->getter_co2)
101                 ecore_timer_del(ad->getter_co2);
102
103         ad->getter_co2 = ecore_timer_add(SENSOR_GATHER_INTERVAL, __get_co2, ad);
104         if (!ad->getter_co2)
105                 _E("Failed to add getter_co2");
106
107         return;
108 }
109
110 #ifdef USE_ST_SDK
111 static bool handle_reset_request(void)
112 {
113         _D("Received a request for RESET.");
114         return false;
115 }
116
117 static void handle_reset_result(bool result)
118 {
119         _D("Reset %s.\n", result ? "succeeded" : "failed");
120 }
121
122 static bool handle_ownership_transfer_request(void)
123 {
124         _D("Received a request for Ownership-transfer.");
125         return true;
126 }
127
128 static void handle_things_status_change(st_things_status_e things_status)
129 {
130         _D("Things status is changed: %d\n", things_status);
131
132         if (things_status == ST_THINGS_STATUS_REGISTERED_TO_CLOUD)
133                 ecore_main_loop_thread_safe_call_async(gathering_start, g_ad);
134 }
135
136 static bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
137 {
138         _D("resource_uri [%s]", req_msg->resource_uri);
139         retv_if(!g_ad, false);
140
141         if (0 == strcmp(req_msg->resource_uri, SENSOR_URI_CO2)) {
142                 _D("query : %s, property: %s", req_msg->query, req_msg->property_key);
143
144                 if (req_msg->has_property_key(req_msg, SENSOR_KEY_CO2)) {
145                         unsigned int value = 0;
146                         sensor_data_get_uint(g_ad->co2_data, &value);
147                         resp_rep->set_int_value(resp_rep, SENSOR_KEY_CO2, value);
148                 }
149                 if (req_msg->has_property_key(req_msg, SENSOR_KEY_RANGE)) {
150                         const double range[2] = { 0.0, 1024.0 };
151                         resp_rep->set_double_array_value(resp_rep, SENSOR_KEY_RANGE, range, 2);
152                 }
153                 return true;
154         }
155         _E("not supported uri");
156         return false;
157 }
158
159 static bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
160 {
161         _D("resource_uri [%s]", req_msg->resource_uri);
162         return false;
163 }
164
165 static int __things_init(void)
166 {
167         bool easysetup_complete = false;
168         char app_json_path[128] = {'\0', };
169         char *app_res_path = NULL;
170         char *app_data_path = NULL;
171
172         app_res_path = app_get_resource_path();
173         if (!app_res_path) {
174                 _E("app_res_path is NULL!!");
175                 return -1;
176         }
177
178         app_data_path = app_get_data_path();
179         if (!app_data_path) {
180                 _E("app_data_path is NULL!!");
181                 free(app_res_path);
182                 return -1;
183         }
184
185         snprintf(app_json_path, sizeof(app_json_path), "%s%s", app_res_path, JSON_PATH);
186
187         if (0 != st_things_set_configuration_prefix_path(app_res_path, app_data_path)) {
188                 _E("st_things_set_configuration_prefix_path() failed!!");
189                 free(app_res_path);
190                 free(app_data_path);
191                 return -1;
192         }
193
194         free(app_res_path);
195         free(app_data_path);
196
197         if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
198                 _E("st_things_initialize() failed!!");
199                 return -1;
200         }
201
202         _D("easysetup_complete:[%d] ", easysetup_complete);
203
204         st_things_register_request_cb(handle_get_request, handle_set_request);
205         st_things_register_reset_cb(handle_reset_request, handle_reset_result);
206         st_things_register_user_confirm_cb(handle_ownership_transfer_request);
207         st_things_register_things_status_change_cb(handle_things_status_change);
208
209         return 0;
210 }
211
212 static int __things_deinit(void)
213 {
214         st_things_deinitialize();
215         return 0;
216 }
217
218 static int __things_start(void)
219 {
220         st_things_start();
221         return 0;
222 }
223
224 static int __things_stop(void)
225 {
226         st_things_stop();
227         return 0;
228 }
229 #endif /* USE_ST_SDK */
230
231 static bool service_app_create(void *user_data)
232 {
233         app_data *ad = (app_data *)user_data;
234
235         ad->co2_data = sensor_data_new(SENSOR_DATA_TYPE_UINT);
236         if (!ad->co2_data)
237                 return false;
238
239 #ifdef USE_ST_SDK
240         if (__things_init())
241                 return false;
242 #endif
243
244         return true;
245 }
246
247 static void service_app_control(app_control_h app_control, void *user_data)
248 {
249 #ifdef USE_ST_SDK
250         __things_start();
251 #else
252         gathering_start(user_data);
253 #endif
254 }
255
256 static void service_app_terminate(void *user_data)
257 {
258         app_data *ad = (app_data *)user_data;
259
260         if (ad->getter_co2)
261                 ecore_timer_del(ad->getter_co2);
262
263 #ifdef USE_ST_SDK
264         __things_stop();
265         __things_deinit();
266 #endif
267
268         sensor_data_free(ad->co2_data);
269         co2_sensor_close();
270         free(ad);
271
272         FN_END;
273 }
274
275 int main(int argc, char *argv[])
276 {
277         app_data *ad = NULL;
278         service_app_lifecycle_callback_s event_callback;
279
280         ad = calloc(1, sizeof(app_data));
281         retv_if(!ad, -1);
282
283         g_ad = ad;
284
285         event_callback.create = service_app_create;
286         event_callback.terminate = service_app_terminate;
287         event_callback.app_control = service_app_control;
288
289         return service_app_main(argc, argv, &event_callback, ad);
290 }
291