tizen 5.0 migration
[apps/native/blind-motor.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 static const char *PROP_POWER = "power";
21
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";
26
27 extern void set_switch_status(bool status);
28
29 bool handle_get_request_on_resource_capability_switch_main_0(smartthings_payload_h resp_payload, void *user_data)
30 {
31         int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
32
33     _D("Received a GET request\n");
34
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);
38
39         _D("Power : %s", g_switch);
40
41     return true;
42 }
43
44 bool handle_set_request_on_resource_capability_switch_main_0(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data)
45 {
46         int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
47         char *str_value = NULL;
48
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);
52                 return false;
53         }
54
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!!");
59                 free(str_value);
60                 return false;
61         }
62
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);
67                 }
68                 else  {
69                         set_switch_status(false);
70                 }
71         }
72
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);
76                 free(str_value);
77                 return false;
78         }
79         free(str_value);
80
81     return true;
82 }
83