e377c0c73075926df4262c9911dd1298d34c7673
[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 "st_things.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(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
30 {
31         DBG("Received a GET request on %s\n", req_msg->resource_uri);
32
33         if (req_msg->has_property_key(req_msg, PROP_POWER)) {
34                 resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
35         }
36
37         return true;
38 }
39
40 bool handle_set_request_on_resource_capability_switch(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep)
41 {
42         DBG("Received a SET request on %s\n", req_msg->resource_uri);
43
44         char *str_value = NULL;
45         req_msg->rep->get_str_value(req_msg->rep, PROP_POWER, &str_value);
46
47         /* check validation */
48         if ((0 != strncmp(str_value, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON)))
49                 && (0 != strncmp(str_value, VALUE_SWITCH_OFF, strlen(VALUE_SWITCH_OFF)))) {
50                 ERR("Not supported value!!");
51                 free(str_value);
52                 return false;
53         }
54
55         if (0 != strncmp(str_value, g_switch, strlen(g_switch))) {
56                 strncpy(g_switch, str_value, VALUE_STR_LEN_MAX);
57                 if (0 == strncmp(g_switch, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON))) {
58                         set_switch_status(true);
59                 }
60                 else  {
61                         set_switch_status(false);
62                 }
63         }
64         resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
65
66         st_things_notify_observers(req_msg->resource_uri);
67
68         free(str_value);
69
70         return true;
71 }