tizen 5.0 migration
[apps/native/co2-meter.git] / src / capability / capability_switch.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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 "smartthings_resource.h"
18 #include "log.h"
19
20 #define VALUE_STR_LEN_MAX 32
21
22 static const char *PROP_POWER = "power";
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";
26 bool g_switch_is_on = false;
27
28 bool handle_get_request_on_resource_capability_switch_main_0(smartthings_payload_h resp_payload, void *user_data)
29 {
30         int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
31
32         error = smartthings_payload_set_string(resp_payload, PROP_POWER, g_switch);
33         if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
34                 _E("smartthings_payload_set_string() failed, [%d]", error);
35                 return false;
36         }
37         _D("get switch : %s", g_switch);
38
39         return true;
40 }
41
42 bool handle_set_request_on_resource_capability_switch_main_0(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data)
43 {
44         char *str_value = NULL;
45         int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
46
47     error = smartthings_payload_get_string(payload, PROP_POWER, &str_value);
48         if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
49                 _E("smartthings_payload_get_string() failed, [%d]", error);
50                 return false;
51         }
52         _D("set switch : %s", str_value);
53
54         /* check validation */
55         if ((0 != strncmp(str_value, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON)))
56                 && (0 != strncmp(str_value, VALUE_SWITCH_OFF, strlen(VALUE_SWITCH_OFF)))) {
57                 _E("Not supported value!!");
58                 free(str_value);
59                 return false;
60         }
61
62         if (0 != strncmp(str_value, g_switch, strlen(g_switch))) {
63                 strncpy(g_switch, str_value, VALUE_STR_LEN_MAX);
64                 if (0 == strncmp(g_switch, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON))) {
65                         g_switch_is_on = true;
66                 } else {
67                         g_switch_is_on = false;
68                 }
69         }
70         free(str_value);
71
72         error = smartthings_payload_set_string(resp_payload, PROP_POWER, g_switch);
73         if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
74                 _E("smartthings_payload_set_string() failed, [%d]", error);
75                 return false;
76         }
77
78         return true;
79 }