Merge test folder of test branch
[iotivity.git] / test / src / tc / ic / gtest / src / helper / ICBinarySwitchResource.cpp
1 /******************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "ICHelper.h"
22
23 ICBinarySwitchResource::ICBinarySwitchResource(string uri, vector< string > rt,
24         vector< string > itf) :
25         ICResource(uri, rt, itf)
26 {
27     m_value = false;
28     m_representation.setValue("value", m_value);
29 }
30
31 void ICBinarySwitchResource::setBinarySwitchRepresentation(OCRepresentation &rep)
32 {
33     bool value;
34     if (rep.getValue("value", value))
35     {
36         m_value = value;
37         m_representation.setValue("value", m_value);
38         cout << "\t\t\t\t" << "value: " << m_value << endl;
39
40         propagate();
41     }
42 }
43
44 OCEntityHandlerResult ICBinarySwitchResource::entityHandler(shared_ptr< OCResourceRequest > request)
45 {
46     IOTIVITYTEST_LOG(INFO, "\tIn Server Binaryswitch entity handler:\n");
47     OCEntityHandlerResult ehResult = OC_EH_ERROR;
48
49     if (request)
50     {
51         // Get the request type and request flag
52         string requestType = request->getRequestType();
53         int requestFlag = request->getRequestHandlerFlag();
54
55         if (requestFlag & RequestHandlerFlag::RequestFlag)
56         {
57             IOTIVITYTEST_LOG(INFO, "\t\trequestFlag : Request\n");
58
59             // If the request type is GET
60             if (requestType == "GET")
61             {
62                 IOTIVITYTEST_LOG(INFO, "\t\t\trequestType : GET\n");
63                 if (OC_STACK_OK == sendRepresentation(request))
64                 {
65                     ehResult = OC_EH_OK;
66                 }
67             }
68             else if (requestType == "PUT")
69             {
70                 IOTIVITYTEST_LOG(INFO, "\t\t\trequestType : PUT\n");
71                 // PUT request operations
72             }
73             else if (requestType == "POST")
74             {
75                 IOTIVITYTEST_LOG(INFO, "\t\t\trequestType : POST\n");
76                 // POST request operations
77                 OCRepresentation rep = request->getResourceRepresentation();
78                 setBinarySwitchRepresentation(rep);
79
80                 if (OC_STACK_OK == sendRepresentation(request))
81                 {
82                     ehResult = OC_EH_OK;
83                 }
84             }
85             else if (requestType == "DELETE")
86             {
87                 IOTIVITYTEST_LOG(INFO, "\t\t\trequestType : DELETE\n");
88                 // DELETE request operations
89             }
90         }
91
92         if (requestFlag & RequestHandlerFlag::ObserverFlag)
93         {
94             IOTIVITYTEST_LOG(INFO, "\t\trequestFlag : Observer\n");
95
96             ObservationInfo observationInfo = request->getObservationInfo();
97             if (ObserveAction::ObserveRegister == observationInfo.action)
98             {
99                 m_interestedObservers.push_back(observationInfo.obsId);
100             }
101             else if (ObserveAction::ObserveUnregister == observationInfo.action)
102             {
103                 m_interestedObservers.erase(
104                         remove(m_interestedObservers.begin(), m_interestedObservers.end(),
105                                 observationInfo.obsId), m_interestedObservers.end());
106             }
107         }
108     }
109     else
110     {
111         IOTIVITYTEST_LOG(INFO, "Request invalid\n");
112     }
113
114     return ehResult;
115 }