2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "smartthings.h"
19 #include "smartthings_resource.h"
20 #include "smartthings_payload.h"
21 #include "sensor-data.h"
24 // URI and key information
25 #define SENSOR_LIDAR_URI "/capability/illuminanceMeasurement/main/0"
26 #define SENSOR_LIDAR_KEY "illuminance"
28 static smartthings_resource_h st_res_h;
29 static smartthings_resource_connection_status_e st_res_status;
30 static int st_cloud_reg_status;
31 static sensor_data *resource_sd;
33 static const char * _resource_error_to_str(smartthings_resource_error_e err)
35 const char *err_str = NULL;
38 case SMARTTHINGS_RESOURCE_ERROR_NONE:
39 err_str = "SMARTTHINGS_RESOURCE_ERROR_NONE";
41 case SMARTTHINGS_RESOURCE_ERROR_INVALID_PARAMETER:
42 err_str = "SMARTTHINGS_RESOURCE_ERROR_INVALID_PARAMETER";
44 case SMARTTHINGS_RESOURCE_ERROR_OUT_OF_MEMORY:
45 err_str = "SMARTTHINGS_RESOURCE_ERROR_OUT_OF_MEMORY";
47 case SMARTTHINGS_RESOURCE_ERROR_PERMISSION_DENIED:
48 err_str = "SMARTTHINGS_RESOURCE_ERROR_PERMISSION_DENIED";
50 case SMARTTHINGS_RESOURCE_ERROR_NO_DATA:
51 err_str = "SMARTTHINGS_RESOURCE_ERROR_NO_DATA";
53 case SMARTTHINGS_RESOURCE_ERROR_NOT_SUPPORTED:
54 err_str = "SMARTTHINGS_RESOURCE_ERROR_NOT_SUPPORTED";
56 case SMARTTHINGS_RESOURCE_ERROR_OPERATION_FAILED:
57 err_str = "SMARTTHINGS_RESOURCE_ERROR_NOT_SUPPORTED";
59 case SMARTTHINGS_RESOURCE_ERROR_SERVICE_UNAVAILABLE:
60 err_str = "SMARTTHINGS_RESOURCE_ERROR_SERVICE_UNAVAILABLE";
63 err_str = "Unknown error";
70 static bool _handle_get_lidar(smartthings_payload_h resp_payload, void *user_data)
72 int err = SMARTTHINGS_RESOURCE_ERROR_NONE;
73 unsigned int value = 0;
75 _D("Received a GET request for LIDAR");
77 if (sensor_data_get_uint(resource_sd, &value))
80 err = smartthings_payload_set_double(resp_payload, SENSOR_LIDAR_KEY, value);
81 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE)
82 _E("smartthings_payload_set_double() failed, [%s]",
83 _resource_error_to_str(err));
88 static void __request_cb(smartthings_resource_h handle, int id,
89 const char *uri, smartthings_resource_req_type_e req_type,
90 smartthings_payload_h payload, void *user_data)
92 smartthings_payload_h resp_payload = NULL;
94 int err = SMARTTHINGS_RESOURCE_ERROR_NONE;
96 _D("request on %s, type[%d], id[%d]", uri, req_type, id);
98 err = smartthings_payload_create(&resp_payload);
99 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE || ! resp_payload)
100 _E("smartthings_payload_create() failed, [%s]",
101 _resource_error_to_str(err));
103 if (req_type == SMARTTHINGS_RESOURCE_REQUEST_GET) {
104 if (!strncmp(uri, SENSOR_LIDAR_URI, strlen(SENSOR_LIDAR_URI)))
105 result = _handle_get_lidar(resp_payload, user_data);
107 _E("No matching Resource uri to get");
108 } else if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) {
109 _E("No matching Resource uri to set");
111 _E("Invalid request type - %d", req_type);
112 smartthings_payload_destroy(resp_payload);
116 err = smartthings_resource_send_response(handle, id, uri, resp_payload, result);
117 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE) {
118 _E("smartthings_resource_send_response() failed, [%s]",
119 _resource_error_to_str(err));
120 smartthings_payload_destroy(resp_payload);
124 if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) {
125 err = smartthings_resource_notify(handle, uri, resp_payload);
126 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE)
127 _E("smartthings_resource_notify() failed, [%s]",
128 _resource_error_to_str(err));
131 if (smartthings_payload_destroy(resp_payload))
132 _E("smartthings_payload_destroy() failed");
135 void _cloud_reg_status_cb(smartthings_resource_h handle, bool is_registered, void *user_data)
137 _D("registered [%d]", is_registered);
138 st_cloud_reg_status = is_registered;
141 static void _resource_connection_status_cb(
142 smartthings_resource_h handle,
143 smartthings_resource_connection_status_e status, void *user_data)
145 int err = SMARTTHINGS_RESOURCE_ERROR_NONE;
146 st_res_status = status;
147 _D("status=[%d]", status);
149 if (status != SMARTTHINGS_RESOURCE_CONNECTION_STATUS_CONNECTED) {
150 _E("connection failed");
154 err = smartthings_resource_set_request_cb(handle, __request_cb, NULL);
155 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE) {
156 _E("smartthings_resource_set_request_cb() is failed, [%s]",
157 _resource_error_to_str(err));
161 err = smartthings_resource_set_cloud_registration_status_cb(handle, _cloud_reg_status_cb, NULL);
162 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE) {
163 _E("smartthings_resource_set_cloud_registration_status_cb() is failed, [%s]",
164 _resource_error_to_str(err));
170 __data_changed_cb(sensor_data *sd, sensor_data_type_e type, void *user_data)
172 unsigned int value = 0;
173 int err = SMARTTHINGS_RESOURCE_ERROR_NONE;
174 smartthings_payload_h resp_payload = NULL;
176 if (st_res_status != SMARTTHINGS_RESOURCE_CONNECTION_STATUS_CONNECTED
177 || !st_cloud_reg_status) {
178 _D("resource not connected yet");
182 sensor_data_get_uint(sd, &value);
184 err = smartthings_payload_create(&resp_payload);
185 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE || !resp_payload) {
186 _E("smartthings_payload_create() failed, [%s]",
187 _resource_error_to_str(err));
191 err = smartthings_payload_set_double(resp_payload, SENSOR_LIDAR_KEY, value);
192 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE) {
193 _E("smartthings_payload_set_bool() failed, [%s]",
194 _resource_error_to_str(err));
195 smartthings_payload_destroy(resp_payload);
199 err = smartthings_resource_notify(st_res_h, SENSOR_LIDAR_URI, resp_payload);
200 if (err != SMARTTHINGS_RESOURCE_ERROR_NONE)
201 _E("smartthings_resource_notify() failed, [%s]",
202 _resource_error_to_str(err));
204 smartthings_payload_destroy(resp_payload);
207 int st_resource_destroy(void)
212 smartthings_resource_unset_request_cb(st_res_h);
213 smartthings_resource_unset_cloud_registration_status_cb(st_res_h);
214 smartthings_resource_deinitialize(st_res_h);
216 sensor_data_changed_cb_set(resource_sd, NULL, NULL);
219 st_res_status = SMARTTHINGS_RESOURCE_CONNECTION_STATUS_DISCONNECTED;
220 st_cloud_reg_status = 0;
225 int st_resource_create(sensor_data *sd)
227 smartthings_resource_h res_h = NULL;
232 _I("Already initialized!");
236 err = smartthings_resource_initialize(&res_h,
237 _resource_connection_status_cb, NULL);
239 _E("smartthings_resource_initialize() is failed, [%s]",
240 _resource_error_to_str(err));
245 st_res_status = SMARTTHINGS_RESOURCE_CONNECTION_STATUS_DISCONNECTED;
246 st_cloud_reg_status = 0;
249 sensor_data_changed_cb_set(sd, __data_changed_cb, st_res_h);