3310ad7e8b5d7c350f0dbee77e43c18df4ca1aa0
[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 "st_things.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(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep)
29 {
30         DBG("Received a GET request on %s\n", req_msg->resource_uri);
31
32         if (req_msg->has_property_key(req_msg, PROP_POWER)) {
33                 resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
34         }
35         else
36                 return false;
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         if (0 != strncmp(str_value, g_switch, strlen(g_switch))) {
55                 strncpy(g_switch, str_value, VALUE_STR_LEN_MAX);
56                 if (0 == strncmp(g_switch, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON))) {
57                         g_switch_is_on = true;
58                 }
59                 else  {
60                         g_switch_is_on = false;
61                 }
62         }
63         resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
64
65         st_things_notify_observers(req_msg->resource_uri);
66
67         free(str_value);
68
69         return true;
70 }