1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "DevicePresence.h"
22 #include "RCSException.h"
28 DevicePresence::DevicePresence()
30 setDeviceState(DEVICE_STATE::REQUESTED);
32 presenceTimerHandle = 0;
33 isRunningTimeOut = false;
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);
40 DevicePresence::~DevicePresence()
42 if(presenceSubscriber.isSubscribing())
44 OIC_LOG_V(DEBUG,BROKER_TAG,"unsubscribed presence.");
47 presenceSubscriber.unsubscribe();
48 } catch (std::exception & e)
50 OIC_LOG_V(DEBUG,BROKER_TAG,"unsubscribed presence : %s", e.what());
53 resourcePresenceList.clear();
54 OIC_LOG_V(DEBUG,BROKER_TAG,"destroy Timer.");
57 void DevicePresence::initializeDevicePresence(PrimitiveResourcePtr pResource)
59 OIC_LOG_V(DEBUG, BROKER_TAG, "initializeDevicePresence()");
60 address = pResource->getHost();
62 OIC_LOG_V(DEBUG, BROKER_TAG, "%s",address.c_str());
66 OIC_LOG_V(DEBUG, BROKER_TAG, "subscribe Presence");
68 = PresenceSubscriber(address, BROKER_TRANSPORT, pSubscribeRequestCB);
69 } catch(RCSPlatformException &e)
71 OIC_LOG_V(DEBUG, BROKER_TAG,
72 "exception in subscribe Presence %s", e.getReason().c_str());
76 = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
79 DEVICE_STATE DevicePresence::getDeviceState() const noexcept
81 return static_cast< DEVICE_STATE >(state.load());
84 void DevicePresence::setDeviceState(DEVICE_STATE newState)
86 state = static_cast< int >(newState);
89 const std::string DevicePresence::getAddress() const
91 OIC_LOG_V(DEBUG, BROKER_TAG, "getAddress()");
95 void DevicePresence::addPresenceResource(ResourcePresence * rPresence)
97 OIC_LOG_V(DEBUG, BROKER_TAG, "addPresenceResource()");
98 resourcePresenceList.push_back(rPresence);
101 void DevicePresence::removePresenceResource(ResourcePresence * rPresence)
103 OIC_LOG_V(DEBUG, BROKER_TAG, "removePresenceResource()");
104 resourcePresenceList.remove(rPresence);
107 void DevicePresence::changeAllPresenceMode(BROKER_MODE mode)
109 OIC_LOG_V(DEBUG, BROKER_TAG, "changeAllPresenceMode()");
110 if(!resourcePresenceList.empty())
112 for(auto it : resourcePresenceList)
114 it->changePresenceMode(mode);
119 bool DevicePresence::isEmptyResourcePresence() const
121 OIC_LOG_V(DEBUG, BROKER_TAG, "isEmptyResourcePresence()");
122 return resourcePresenceList.empty();
125 void DevicePresence::subscribeCB(OCStackResult ret,
126 const unsigned int seq, const std::string & hostAddress)
128 OIC_LOG_V(DEBUG, BROKER_TAG, "subscribeCB()");
129 OIC_LOG_V(DEBUG, BROKER_TAG, "Received presence CB from: %s",hostAddress.c_str());
130 OIC_LOG_V(DEBUG, BROKER_TAG, "In subscribeCB: %d",ret);
134 std::unique_lock<std::mutex> lock(timeoutMutex);
135 condition.wait(lock);
137 presenceTimer.cancel(presenceTimerHandle);
142 case OC_STACK_RESOURCE_CREATED:
143 case OC_STACK_CONTINUE:
145 OIC_LOG_V(DEBUG, BROKER_TAG, "SEQ# %d",seq);
146 setDeviceState(DEVICE_STATE::ALIVE);
147 OIC_LOG_V(DEBUG, BROKER_TAG, "device state : %d",
148 (int)getDeviceState());
149 changeAllPresenceMode(BROKER_MODE::DEVICE_PRESENCE_MODE);
151 = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
154 case OC_STACK_INVALID_REQUEST_HANDLE:
155 case OC_STACK_RESOURCE_DELETED:
156 case OC_STACK_TIMEOUT:
157 case OC_STACK_COMM_ERROR:
158 case OC_STACK_PRESENCE_STOPPED:
159 case OC_STACK_PRESENCE_TIMEOUT:
160 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
162 setDeviceState(DEVICE_STATE::LOST_SIGNAL);
163 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
168 OIC_LOG_V(DEBUG, BROKER_TAG, "Presence Lost Signal because unknown type");
169 setDeviceState(DEVICE_STATE::LOST_SIGNAL);
170 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
176 void DevicePresence::timeOutCB(TimerID /*id*/)
178 OIC_LOG_V(DEBUG,BROKER_TAG,"timeOutCB()");
179 std::unique_lock<std::mutex> lock(timeoutMutex);
180 isRunningTimeOut = true;
182 OIC_LOG_V(DEBUG, BROKER_TAG,
183 "Timeout execution. will be discard after receiving cb message");
184 setDeviceState(DEVICE_STATE::LOST_SIGNAL);
185 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
187 isRunningTimeOut = false;
188 condition.notify_all();
190 } // namespace Service