[CONPRO-1337] Disabled Presence Feature
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceBroker / src / DevicePresence.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 "DevicePresence.h"
22 #include "RCSException.h"
23
24 namespace OIC
25 {
26     namespace Service
27     {
28         DevicePresence::DevicePresence()
29         {
30             setDeviceState(DEVICE_STATE::REQUESTED);
31
32             presenceTimerHandle = 0;
33             isRunningTimeOut = false;
34
35             pSubscribeRequestCB = std::bind(&DevicePresence::subscribeCB, this,
36                         std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
37             pTimeoutCB = std::bind(&DevicePresence::timeOutCB, this, std::placeholders::_1);
38         }
39
40         DevicePresence::~DevicePresence()
41         {
42             if(presenceSubscriber.isSubscribing())
43             {
44                 OIC_LOG_V(DEBUG,BROKER_TAG,"unsubscribed presence.");
45                 try
46                 {
47                     presenceSubscriber.unsubscribe();
48                 } catch (std::exception & e)
49                 {
50                     OIC_LOG_V(DEBUG,BROKER_TAG,"unsubscribed presence : %s", e.what());
51                 }
52             }
53             resourcePresenceList.clear();
54             OIC_LOG_V(DEBUG,BROKER_TAG,"destroy Timer.");
55         }
56
57         void DevicePresence::initializeDevicePresence(PrimitiveResourcePtr pResource)
58         {
59             OIC_LOG_V(DEBUG, BROKER_TAG, "initializeDevicePresence()");
60             address = pResource->getHost();
61
62             OIC_LOG_V(DEBUG, BROKER_TAG, "%s",address.c_str());
63
64             try
65             {
66                 OIC_LOG_V(DEBUG, BROKER_TAG, "subscribe Presence");
67                 presenceSubscriber
68                 = PresenceSubscriber(address, BROKER_TRANSPORT, pSubscribeRequestCB);
69             } catch(RCSPlatformException &e)
70             {
71                 OIC_LOG_V(DEBUG, BROKER_TAG,
72                         "exception in subscribe Presence %s", e.getReason().c_str());
73                 throw;
74             }
75             presenceTimerHandle
76             = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
77         }
78
79         DEVICE_STATE DevicePresence::getDeviceState() const noexcept
80         {
81             return static_cast< DEVICE_STATE >(state.load());
82         }
83
84         void DevicePresence::setDeviceState(DEVICE_STATE newState)
85         {
86             state = static_cast< int >(newState);
87         }
88
89         const std::string DevicePresence::getAddress() const
90         {
91             OIC_LOG_V(DEBUG, BROKER_TAG, "getAddress()");
92             return address;
93         }
94
95         void DevicePresence::addPresenceResource(ResourcePresence * rPresence)
96         {
97             OIC_LOG_V(DEBUG, BROKER_TAG, "addPresenceResource()");
98             resourcePresenceList.push_back(rPresence);
99         }
100
101         void DevicePresence::removePresenceResource(ResourcePresence * rPresence)
102         {
103             OIC_LOG_V(DEBUG, BROKER_TAG, "removePresenceResource()");
104             resourcePresenceList.remove(rPresence);
105         }
106
107         void DevicePresence::changeAllPresenceMode(BROKER_MODE mode)
108         {
109             OIC_LOG_V(DEBUG, BROKER_TAG, "changeAllPresenceMode()");
110             if(!resourcePresenceList.empty())
111             {
112                 for(auto it : resourcePresenceList)
113                 {
114                     it->changePresenceMode(mode);
115                 }
116             }
117         }
118
119         bool DevicePresence::isEmptyResourcePresence() const
120         {
121             OIC_LOG_V(DEBUG, BROKER_TAG, "isEmptyResourcePresence()");
122             return resourcePresenceList.empty();
123         }
124
125         void DevicePresence::subscribeCB(OCStackResult ret,
126                 const unsigned int seq, const std::string & hostAddress)
127         {
128             OC_UNUSED(seq);
129             OC_UNUSED(hostAddress);
130
131             OIC_LOG_V(DEBUG, BROKER_TAG, "subscribeCB()");
132             OIC_LOG_V(DEBUG, BROKER_TAG, "Received presence CB from: %s",hostAddress.c_str());
133             OIC_LOG_V(DEBUG, BROKER_TAG, "In subscribeCB: %d",ret);
134
135             if(isRunningTimeOut)
136             {
137                 std::unique_lock<std::mutex> lock(timeoutMutex);
138                 condition.wait(lock);
139             }
140             presenceTimer.cancel(presenceTimerHandle);
141
142             switch(ret)
143             {
144                 case OC_STACK_OK:
145                 case OC_STACK_RESOURCE_CREATED:
146                 case OC_STACK_CONTINUE:
147                 {
148                     OIC_LOG_V(DEBUG, BROKER_TAG, "SEQ# %d",seq);
149                     setDeviceState(DEVICE_STATE::ALIVE);
150                     OIC_LOG_V(DEBUG, BROKER_TAG, "device state : %d",
151                             (int)getDeviceState());
152                     changeAllPresenceMode(BROKER_MODE::DEVICE_PRESENCE_MODE);
153                     presenceTimerHandle
154                     = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
155                     break;
156                 }
157                 case OC_STACK_INVALID_REQUEST_HANDLE:
158                 case OC_STACK_RESOURCE_DELETED:
159                 case OC_STACK_TIMEOUT:
160                 case OC_STACK_COMM_ERROR:
161 #ifdef WITH_PRESENCE
162                 case OC_STACK_PRESENCE_STOPPED:
163                 case OC_STACK_PRESENCE_TIMEOUT:
164                 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
165 #endif
166                 {
167                     setDeviceState(DEVICE_STATE::LOST_SIGNAL);
168                     changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
169                     break;
170                 }
171                 default:
172                 {
173                     OIC_LOG_V(DEBUG, BROKER_TAG, "Presence Lost Signal because unknown type");
174                     setDeviceState(DEVICE_STATE::LOST_SIGNAL);
175                     changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
176                     break;
177                 }
178             }
179         }
180
181         void DevicePresence::timeOutCB(TimerID /*id*/)
182         {
183             OIC_LOG_V(DEBUG,BROKER_TAG,"timeOutCB()");
184             std::unique_lock<std::mutex> lock(timeoutMutex);
185             isRunningTimeOut = true;
186
187             OIC_LOG_V(DEBUG, BROKER_TAG,
188                     "Timeout execution. will be discard after receiving cb message");
189             setDeviceState(DEVICE_STATE::LOST_SIGNAL);
190             changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
191
192             isRunningTimeOut = false;
193             condition.notify_all();
194         }
195     } // namespace Service
196 } // namespace OIC