summaryrefslogtreecommitdiff
path: root/src/illuminance_sensor.c
blob: f76d2fdf071ec07383e5ad4de154fb42d67a426a (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 *
 * 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Ecore.h>

#include "log.h"

#include "smartthings.h"
#include "smartthings_resource.h"
#include "smartthings_payload.h"

// Duration for a timer
#define TIMER_SENSOR_INTERVAL (.250f)
Ecore_Timer *sensor_event_timer = NULL;

#define THING_RESOURCE_FILE_NAME	"resource.json"

static smartthings_resource_h st_handle = NULL;
static bool is_init = false;
static bool is_resource_init = false;
static smartthings_h st_h;

smartthings_status_e st_things_status = -1;

static const char *RES_CAPABILITY_SWITCH_MAIN_0 = "/capability/switch/main/0";
static const char *RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0 = "/capability/illuminanceMeasurement/main/0";
static const char *PROP_ILLUMINANCE = "illuminance";

/* get and set request handlers */
extern bool handle_get_request_on_resource_capability_switch_main_0(smartthings_payload_h resp_payload, void *user_data);
extern bool handle_set_request_on_resource_capability_switch_main_0(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data);
extern bool handle_get_request_on_resource_capability_illuminancemeasurement_main_0(smartthings_payload_h resp_payload, void *user_data);

extern int resource_read_illuminance_sensor(uint16_t *out_value);
extern int resource_close_illuminance_sensor(void);

static Eina_Bool _get_sensor_data(void *user_data)
{
	int ret = 0;
	uint16_t value = 0;

	int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
	smartthings_payload_h resp_payload = NULL;

	if (st_things_status != SMARTTHINGS_STATUS_REGISTERED_TO_CLOUD)
		return ECORE_CALLBACK_RENEW;

	if (!st_handle) {
		_E("st_handle is NULL");
		return ECORE_CALLBACK_RENEW;
	}

	ret = resource_read_illuminance_sensor(&value);
	if (ret != 0) {
		_E("cannot read data from the sensor");
		return ECORE_CALLBACK_CANCEL;
	}

	_D("Detected sensor value is: %u", value);

	error = smartthings_payload_create(&resp_payload);
	if (error != SMARTTHINGS_RESOURCE_ERROR_NONE || !resp_payload) {
		_E("smartthings_payload_create() failed, [%d]", error);
		return ECORE_CALLBACK_CANCEL;
	}

	error = smartthings_payload_set_int(resp_payload, PROP_ILLUMINANCE, (int)value);
	if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
		_E("smartthings_payload_set_int() failed, [%d]", error);

	error = smartthings_resource_notify(st_handle, RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0, resp_payload);
	if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
		_E("smartthings_resource_notify() failed, [%d]", error);

	if (smartthings_payload_destroy(resp_payload))
		_E("smartthings_payload_destroy() failed");

	return ECORE_CALLBACK_RENEW;
}

void _send_response_result_cb(smartthings_resource_error_e result, void *user_data)
{
	_D("app_control reply callback for send_response : result=[%d]", result);
}

void _notify_result_cb(smartthings_resource_error_e result, void *user_data)
{
	_D("app_control reply callback for notify : result=[%d]", result);
}

void _request_cb(smartthings_resource_h st_h, int req_id, const char *uri, smartthings_resource_req_type_e req_type,
				smartthings_payload_h payload, void *user_data)
{
	START;

	smartthings_payload_h resp_payload = NULL;

	smartthings_payload_create(&resp_payload);
	if (!resp_payload) {
		_E("Response payload is NULL");
		return;
	}

	bool result = false;

	if (req_type == SMARTTHINGS_RESOURCE_REQUEST_GET) {
		if (0 == strncmp(uri, RES_CAPABILITY_SWITCH_MAIN_0, strlen(RES_CAPABILITY_SWITCH_MAIN_0))) {
			result = handle_get_request_on_resource_capability_switch_main_0(resp_payload, user_data);
		}
		if (0 == strncmp(uri, RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0, strlen(RES_CAPABILITY_ILLUMINANCEMEASUREMENT_MAIN_0))) {
			result = handle_get_request_on_resource_capability_illuminancemeasurement_main_0(resp_payload, user_data);
		}
	} else if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) {
		if (0 == strncmp(uri, RES_CAPABILITY_SWITCH_MAIN_0, strlen(RES_CAPABILITY_SWITCH_MAIN_0))) {
			result = handle_set_request_on_resource_capability_switch_main_0(payload, resp_payload, user_data);
		}
	} else {
		_E("Invalid request type");
		smartthings_payload_destroy(resp_payload);
		END;
		return;
	}

	int error = SMARTTHINGS_RESOURCE_ERROR_NONE;

	error = smartthings_resource_send_response(st_h, req_id, uri, resp_payload, result);
	if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
			smartthings_payload_destroy(resp_payload);
			_E("smartthings_resource_send_response() failed, err=[%d]", error);
			END;
			return;
	}

	if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) {
			error = smartthings_resource_notify(st_h, uri, resp_payload);
			if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
				_E("smartthings_resource_notify() failed, err=[%d]", error);
			}
	}

	if (smartthings_payload_destroy(resp_payload) != 0) {
		_E("smartthings_payload_destroy failed");
	}

	END;
	return;
}

static void _resource_connection_status_cb(smartthings_resource_h handle, smartthings_resource_connection_status_e status, void *user_data)
{
	START;

	if (status == SMARTTHINGS_RESOURCE_CONNECTION_STATUS_CONNECTED) {
		if (smartthings_resource_set_request_cb(st_handle, _request_cb, NULL) != 0) {
			_E("smartthings_resource_set_request_cb() is failed");
			return;
		}
	} else {
		_I("connection failed, status=[%d]", status);
	}

	END;
	return;
}

int init_resource_app()
{
	START;

	if (is_resource_init) {
		_I("Already initialized!");
		return 0;
	}

	if (smartthings_resource_initialize(&st_handle, _resource_connection_status_cb, NULL) != 0) {
		_E("smartthings_resource_initialize() is failed");
		goto _out;
	}

	is_resource_init = true;

	END;
	return 0;

_out :
	END;
	return -1;
}

int deinit_resource_app()
{
	START;

	if (!st_handle)
		return 0;

	smartthings_resource_unset_request_cb(st_handle);

	if (smartthings_resource_deinitialize(st_handle) != 0)
		return -1;

	is_resource_init = false;

	END;
	return 0;
}


void _user_confirm_cb(smartthings_h handle, void *user_data)
{
	START;

	if (smartthings_send_user_confirm(handle, true) != 0)
		_E("smartthings_send_user_confirm() is failed");

	END;
	return;
}

void _reset_confirm_cb(smartthings_h handle, void *user_data)
{
	START;

	if (smartthings_send_reset_confirm(handle, true) != 0)
		_E("smartthings_send_reset_confirm() is failed");

	END;
	return;
}

static void _reset_result_cb(smartthings_h handle, bool result, void *user_data)
{
	START;

	_I("reset result = [%d]", result);

	END;
	return;
}

static void _thing_status_cb(smartthings_h handle, smartthings_status_e status, void *user_data)
{
	START;

	_D("Received status changed cb : status = [%d]", status);
	st_things_status = status;

	switch (status) {
	case SMARTTHINGS_STATUS_NOT_READY:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_NOT_READY");
			break;
	case SMARTTHINGS_STATUS_INIT:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_INIT");
			break;
	case SMARTTHINGS_STATUS_ES_STARTED:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_ES_STARTED");
			break;
	case SMARTTHINGS_STATUS_ES_DONE:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_ES_DONE");
			break;
	case SMARTTHINGS_STATUS_ES_FAILED_ON_OWNERSHIP_TRANSFER:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_ES_FAILED_ON_OWNERSHIP_TRANSFER");
			break;
	case SMARTTHINGS_STATUS_CONNECTING_TO_AP:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_CONNECTING_TO_AP");
			break;
	case SMARTTHINGS_STATUS_CONNECTED_TO_AP:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_CONNECTED_TO_AP");
			break;
	case SMARTTHINGS_STATUS_CONNECTING_TO_AP_FAILED:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_CONNECTING_TO_AP_FAILED");
			break;
	case SMARTTHINGS_STATUS_REGISTERING_TO_CLOUD:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERING_TO_CLOUD");
			break;
	case SMARTTHINGS_STATUS_REGISTERED_TO_CLOUD:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERED_TO_CLOUD");
			break;
	case SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_SIGN_IN:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_SIGN_IN");
			break;
	case SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_PUB_RES:
			_I("status: [%d] [%s]", status, "SMARTTHINGS_STATUS_REGISTERING_FAILED_ON_PUB_RES");
			break;
	default:
			_E("status: [%d][%s]", status, "Unknown Status");
			break;
	}
	END;
	return;
}

static void _things_connection_status_cb(smartthings_h handle, smartthings_connection_status_e status, void *user_data)
{
	START;

	_D("Received connection status changed cb : status = [%d]", status);

	if (status == SMARTTHINGS_CONNECTION_STATUS_CONNECTED) {
		const char* dev_name = "IoT Test Device";
		int wifi_mode = SMARTTHINGS_WIFI_MODE_11B | SMARTTHINGS_WIFI_MODE_11G | SMARTTHINGS_WIFI_MODE_11N;
		int wifi_freq = SMARTTHINGS_WIFI_FREQ_24G | SMARTTHINGS_WIFI_FREQ_5G;

		if (smartthings_set_device_property(handle, dev_name, wifi_mode, wifi_freq) != 0) {
			_E("smartthings_initialize() is failed");
			return;
		}

		if (smartthings_set_certificate_file(handle, "certificate.pem", "privatekey.der") != 0) {
			_E("smartthings_set_certificate_file() is failed");
			return;
		}

		if (smartthings_set_user_confirm_cb(st_h, _user_confirm_cb, NULL) != 0) {
			_E("smartthings_set_user_confirm_cb() is failed");
			return;
		}

		if (smartthings_set_reset_confirm_cb(handle, _reset_confirm_cb, NULL) != 0) {
			_E("smartthings_set_reset_confirm_cb() is failed");
			return;
		}

		if (smartthings_set_reset_result_cb(handle, _reset_result_cb, NULL) != 0) {
			_E("smartthings_set_reset_confirm_cb() is failed");
			return;
		}

		if (smartthings_set_status_changed_cb(handle, _thing_status_cb, NULL) != 0) {
			_E("smartthings_set_status_changed_callback() is failed");
			return;
		}

		if (smartthings_start(handle) != 0) {
			_E("smartthings_start() is failed");
			return;
		}

		bool is_es_completed = false;
		if (smartthings_get_easysetup_status(handle, &is_es_completed) != 0) {
			_E("smartthings_get_easysetup_status() is failed");
			return;
		}

		if (is_es_completed == true) {
			_I("Easysetup is already done");
			return;
		}

		if (smartthings_start_easysetup(handle) != 0) {
			_E("smartthings_start_easysetup() is failed");
			return;
		}
	} else {
			_E("connection failed, status=[%d]", status);
	}

	END;
	return;
}

int init_master_app()
{
	START;

	if (is_init) {
		_I("Already initialized!");
		END;
		return 0;
	}

	if (smartthings_initialize(&st_h, _things_connection_status_cb, NULL) != 0) {
		_E("smartthings_initialize() is failed");
		goto _out;
	}

	is_init = true;

	END;
	return 0;

_out :
	END;
	return -1;
}

int deinit_master_app()
{
	START;

	is_init = false;

	if (!st_h) {
		_I("handle is already NULL");
		END;
		return 0;
	}

	smartthings_unset_user_confirm_cb(st_h);
	smartthings_unset_reset_confirm_cb(st_h);
	smartthings_unset_reset_result_cb(st_h);
	smartthings_unset_status_changed_cb(st_h);

	if (smartthings_deinitialize(st_h) != 0)  {
		_E("smartthings_deinitialize() is failed");
		END;
		return -1;
	}

	END;
	return 0;
}

static bool service_app_create(void *user_data)
{
	if (sensor_event_timer)
		ecore_timer_del(sensor_event_timer);

	sensor_event_timer = ecore_timer_add(TIMER_SENSOR_INTERVAL, _get_sensor_data, NULL);
	if (!sensor_event_timer)
		_E("Failed to add a timer");

	return true;
}

static void service_app_terminate(void *user_data)
{
	// clear timer resource
	if (sensor_event_timer) {
		ecore_timer_del(sensor_event_timer);
		sensor_event_timer = NULL;
	}

	// close sensor resource
	resource_close_illuminance_sensor();

	/*terminate resource*/
	if (deinit_resource_app() != 0) {
		_E("deinit_resource_app failed");
	}

	/*terminate master*/
	if (deinit_master_app() != 0) {
		_E("deinit_master_app failed");
	}

	return;
}

static void service_app_control(app_control_h app_control, void *user_data)
{
	if (app_control == NULL) {
		_E("app_control is NULL");
		return;
	}

	init_master_app();
	init_resource_app();

	return;
}

int main(int argc, char *argv[])
{
	service_app_lifecycle_callback_s event_callback;

	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, NULL);
}