summaryrefslogtreecommitdiff
path: root/src/co2.c
blob: 82d164f2174ed7a37adde4fa88ef88ee05012c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* ****************************************************************
 *
 * Copyright 2017 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************/

#include <tizen.h>
#include <service_app.h>
#include <string.h>
#include <stdlib.h>
#include <Ecore.h>

#include "st_things.h"
#include "log.h"
#include "sensor-data.h"
#include "co2-sensor.h"

#define JSON_NAME "device_def.json"
#define SENSOR_URI_CO2 "/capability/airQualitySensor/main/0"
#define SENSOR_KEY_CO2 "airQuality"
#define SENSOR_KEY_RANGE "range"

#define SENSOR_CH_CO2 (0)
#define SENSOR_GATHER_INTERVAL (0.05f)
#define SENSOR_GATHER_COUNT (60)

//#define USE_ST_SDK

typedef struct app_data_s {
	Ecore_Timer *getter_co2;
	sensor_data *co2_data;
} app_data;

static app_data *g_ad = NULL;

static Eina_Bool __get_co2(void *data)
{
	int ret = 0;
	unsigned int value = 0;
	static unsigned int sum = 0;
	static unsigned int count = 0;

	app_data *ad = data;

	if (!ad) {
		_E("failed to get app_data");
		service_app_exit();
		return ECORE_CALLBACK_CANCEL;
	}

	if (!ad->co2_data) {
		_E("failed to get co2_data");
		service_app_exit();
		ad->getter_co2 = NULL;
		return ECORE_CALLBACK_CANCEL;
	}

	ret = co2_sensor_read(SENSOR_CH_CO2, &value);
	retv_if(ret != 0, ECORE_CALLBACK_RENEW);

	count++;
	sum += value;

	if (count == SENSOR_GATHER_COUNT) {
		unsigned int avg = 0;
		avg = sum/SENSOR_GATHER_COUNT;

		_D("co2 avg - [%u], [%u ppm]", avg, co2_sensor_value_to_ppm(avg));

		sensor_data_set_uint(ad->co2_data, avg);

#ifdef USE_ST_SDK
		st_things_notify_observers(SENSOR_URI_CO2);
#endif
		count = 0;
		sum = 0;
	}

	return ECORE_CALLBACK_RENEW;
}

static void gathering_stop(void *data)
{
	app_data *ad = data;
	ret_if(!ad);

	if (ad->getter_co2) {
		ecore_timer_del(ad->getter_co2);
		ad->getter_co2 = NULL;
	}
}

static void gathering_start(void *data)
{
	app_data *ad = data;
	ret_if(!ad);

	ad->getter_co2 = ecore_timer_add(SENSOR_GATHER_INTERVAL, __get_co2, ad);
	if (!ad->getter_co2)
		_E("Failed to add getter_co2");
}

#ifdef USE_ST_SDK
static bool handle_reset_request(void)
{
	_D("Received a request for RESET.");
	return false;
}

static void handle_reset_result(bool result)
{
	_D("Reset %s.\n", result ? "succeeded" : "failed");
}

static bool handle_ownership_transfer_request(void)
{
	_D("Received a request for Ownership-transfer.");
	return true;
}

static void handle_things_status_change(st_things_status_e things_status)
{
	_D("Things status is changed: %d\n", things_status);

	if (things_status == ST_THINGS_STATUS_REGISTERED_TO_CLOUD)
		ecore_main_loop_thread_safe_call_async(gathering_start, g_ad);
	else
		ecore_main_loop_thread_safe_call_async(gathering_stop, g_ad);
}

static bool handle_get_request(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
{
	_D("resource_uri [%s]", req_msg->resource_uri);
	retv_if(!g_ad, false);

	if (0 == strcmp(req_msg->resource_uri, SENSOR_URI_CO2)) {
		_D("query : %s, property: %s", req_msg->query, req_msg->property_key);

		if (req_msg->has_property_key(req_msg, SENSOR_KEY_CO2)) {
			unsigned int value = 0;
			sensor_data_get_uint(g_ad->co2_data, &value);
			resp_rep->set_int_value(resp_rep, SENSOR_KEY_CO2, value);
		}
		if (req_msg->has_property_key(req_msg, SENSOR_KEY_RANGE)) {
			const double range[2] = { 0.0, 1024.0 };
			resp_rep->set_double_array_value(resp_rep, SENSOR_KEY_RANGE, range, 2);
		}
		return true;
	}
	_E("not supported uri");
	return false;
}

static bool handle_set_request(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
{
	_D("resource_uri [%s]", req_msg->resource_uri);
	return false;
}

static int __things_init(void)
{
	bool easysetup_complete = false;
	char app_json_path[128] = {'\0', };
	char *app_res_path = NULL;
	char *app_data_path = NULL;

	app_res_path = app_get_resource_path();
	if (!app_res_path) {
		_E("app_res_path is NULL!!");
		return -1;
	}

	app_data_path = app_get_data_path();
	if (!app_data_path) {
		_E("app_data_path is NULL!!");
		free(app_res_path);
		return -1;
	}

	if (0 != st_things_set_configuration_prefix_path(app_res_path, app_data_path)) {
		_E("st_things_set_configuration_prefix_path() failed!!");
		free(app_res_path);
		free(app_data_path);
		return -1;
	}
	free(app_data_path);

	snprintf(app_json_path, sizeof(app_json_path), "%s%s", app_res_path, JSON_NAME);
	free(app_res_path);

	if (0 != st_things_initialize(app_json_path, &easysetup_complete)) {
		_E("st_things_initialize() failed!!");
		return -1;
	}

	_D("easysetup_complete:[%d] ", easysetup_complete);

	st_things_register_request_cb(handle_get_request, handle_set_request);
	st_things_register_reset_cb(handle_reset_request, handle_reset_result);
	st_things_register_user_confirm_cb(handle_ownership_transfer_request);
	st_things_register_things_status_change_cb(handle_things_status_change);

	return 0;
}

static int __things_deinit(void)
{
	st_things_deinitialize();
	return 0;
}

static int __things_start(void)
{
	st_things_start();
	return 0;
}

static int __things_stop(void)
{
	st_things_stop();
	return 0;
}
#endif /* USE_ST_SDK */

static bool service_app_create(void *user_data)
{
	app_data *ad = (app_data *)user_data;

	ad->co2_data = sensor_data_new(SENSOR_DATA_TYPE_UINT);
	if (!ad->co2_data)
		return false;

#ifdef USE_ST_SDK
	if (__things_init())
		return false;
#endif

	return true;
}

static void service_app_control(app_control_h app_control, void *user_data)
{
#ifdef USE_ST_SDK
	__things_stop();
	__things_start();
#else
	gathering_stop(user_data);
	gathering_start(user_data);
#endif
}

static void service_app_terminate(void *user_data)
{
	app_data *ad = (app_data *)user_data;

	if (!ad)
		return;

#ifdef USE_ST_SDK
	__things_stop();
	__things_deinit();
#endif

	sensor_data_free(ad->co2_data);
	co2_sensor_close();
	free(ad);

	FN_END;
}

int main(int argc, char *argv[])
{
	app_data *ad = NULL;
	service_app_lifecycle_callback_s event_callback;

	ad = calloc(1, sizeof(app_data));
	retv_if(!ad, -1);

	g_ad = ad;

	event_callback.create = service_app_create;
	event_callback.terminate = service_app_terminate;
	event_callback.app_control = service_app_control;

	return service_app_main(argc, argv, &event_callback, ad);
}