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 OC_LOG_V(DEBUG,BROKER_TAG,"unsubscribed presence.");
45 presenceSubscriber.unsubscribe();
47 resourcePresenceList.clear();
48 OC_LOG_V(DEBUG,BROKER_TAG,"destroy Timer.");
51 void DevicePresence::initializeDevicePresence(PrimitiveResourcePtr pResource)
53 OC_LOG_V(DEBUG, BROKER_TAG, "initializeDevicePresence()");
54 address = pResource->getHost();
56 OC_LOG_V(DEBUG, BROKER_TAG, "%s",address.c_str());
60 OC_LOG_V(DEBUG, BROKER_TAG, "subscribe Presence");
62 = PresenceSubscriber(address, BROKER_TRANSPORT, pSubscribeRequestCB);
63 } catch(RCSPlatformException &e)
65 OC_LOG_V(DEBUG, BROKER_TAG,
66 "exception in subscribe Presence %s", e.getReason().c_str());
70 = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
73 DEVICE_STATE DevicePresence::getDeviceState() const noexcept
75 return static_cast< DEVICE_STATE >(state.load());
78 void DevicePresence::setDeviceState(DEVICE_STATE newState)
80 state = static_cast< int >(newState);
83 const std::string DevicePresence::getAddress() const
85 OC_LOG_V(DEBUG, BROKER_TAG, "getAddress()");
89 void DevicePresence::addPresenceResource(ResourcePresence * rPresence)
91 OC_LOG_V(DEBUG, BROKER_TAG, "addPresenceResource()");
92 resourcePresenceList.push_back(rPresence);
95 void DevicePresence::removePresenceResource(ResourcePresence * rPresence)
97 OC_LOG_V(DEBUG, BROKER_TAG, "removePresenceResource()");
98 resourcePresenceList.remove(rPresence);
101 void DevicePresence::changeAllPresenceMode(BROKER_MODE mode)
103 OC_LOG_V(DEBUG, BROKER_TAG, "changeAllPresenceMode()");
104 if(!resourcePresenceList.empty())
106 for(auto it : resourcePresenceList)
108 it->changePresenceMode(mode);
113 bool DevicePresence::isEmptyResourcePresence() const
115 OC_LOG_V(DEBUG, BROKER_TAG, "isEmptyResourcePresence()");
116 return resourcePresenceList.empty();
119 void DevicePresence::subscribeCB(OCStackResult ret,
120 const unsigned int seq, const std::string & hostAddress)
122 OC_LOG_V(DEBUG, BROKER_TAG, "subscribeCB()");
123 OC_LOG_V(DEBUG, BROKER_TAG, "Received presence CB from: %s",hostAddress.c_str());
124 OC_LOG_V(DEBUG, BROKER_TAG, "In subscribeCB: %d",ret);
128 std::unique_lock<std::mutex> lock(timeoutMutex);
129 condition.wait(lock);
131 presenceTimer.cancel(presenceTimerHandle);
136 case OC_STACK_RESOURCE_CREATED:
137 case OC_STACK_CONTINUE:
139 OC_LOG_V(DEBUG, BROKER_TAG, "SEQ# %d",seq);
140 setDeviceState(DEVICE_STATE::ALIVE);
141 OC_LOG_V(DEBUG, BROKER_TAG, "device state : %d",
142 (int)getDeviceState());
143 changeAllPresenceMode(BROKER_MODE::DEVICE_PRESENCE_MODE);
145 = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
148 case OC_STACK_INVALID_REQUEST_HANDLE:
149 case OC_STACK_RESOURCE_DELETED:
150 case OC_STACK_TIMEOUT:
151 case OC_STACK_COMM_ERROR:
152 case OC_STACK_PRESENCE_STOPPED:
153 case OC_STACK_PRESENCE_TIMEOUT:
154 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
156 setDeviceState(DEVICE_STATE::LOST_SIGNAL);
157 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
162 OC_LOG_V(DEBUG, BROKER_TAG, "Presence Lost Signal because unknown type");
163 setDeviceState(DEVICE_STATE::LOST_SIGNAL);
164 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
170 void DevicePresence::timeOutCB(TimerID /*id*/)
172 OC_LOG_V(DEBUG,BROKER_TAG,"timeOutCB()");
173 std::unique_lock<std::mutex> lock(timeoutMutex);
174 isRunningTimeOut = true;
176 OC_LOG_V(DEBUG, BROKER_TAG,
177 "Timeout execution. will be discard after receiving cb message");
178 setDeviceState(DEVICE_STATE::LOST_SIGNAL);
179 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
181 isRunningTimeOut = false;
182 condition.notify_all();
184 } // namespace Service