2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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.
17 #include <smartthings_resource.h>
20 static const char *PROP_POWER = "power";
22 #define VALUE_STR_LEN_MAX 32
23 static const char *VALUE_SWITCH_ON = "on";
24 static const char *VALUE_SWITCH_OFF = "off";
25 static char g_switch[VALUE_STR_LEN_MAX] = "off";
27 extern void set_switch_status(bool status);
29 bool handle_get_request_on_resource_capability_switch_main_0(smartthings_payload_h resp_payload, void *user_data)
31 int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
33 _D("Received a GET request\n");
35 error = smartthings_payload_set_string(resp_payload, PROP_POWER, g_switch);
36 if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
37 _E("smartthings_payload_set_string() failed, [%d]", error);
39 _D("Power : %s", g_switch);
44 bool handle_set_request_on_resource_capability_switch_main_0(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data)
46 int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
47 char *str_value = NULL;
49 error = smartthings_payload_get_string(payload, PROP_POWER, &str_value);
50 if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
51 _E("smartthings_payload_get_string() failed, [%d]", error);
55 /* check validation */
56 if ((0 != strncmp(str_value, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON)))
57 && (0 != strncmp(str_value, VALUE_SWITCH_OFF, strlen(VALUE_SWITCH_OFF)))) {
58 _E("Not supported value!!");
63 if (0 != strncmp(str_value, g_switch, strlen(g_switch))) {
64 strncpy(g_switch, str_value, VALUE_STR_LEN_MAX);
65 if (0 == strncmp(g_switch, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON))) {
66 set_switch_status(true);
69 set_switch_status(false);
73 error = smartthings_payload_set_string(resp_payload, PROP_POWER, g_switch);
74 if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
75 _E("smartthings_payload_set_string() failed, [%d]", error);