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 state = 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(PlatformException &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);
72 DEVICE_STATE DevicePresence::getDeviceState() const
76 const std::string DevicePresence::getAddress() const
78 OC_LOG_V(DEBUG, BROKER_TAG, "getAddress()");
82 void DevicePresence::addPresenceResource(ResourcePresence * rPresence)
84 OC_LOG_V(DEBUG, BROKER_TAG, "addPresenceResource()");
85 resourcePresenceList.push_back(rPresence);
88 void DevicePresence::removePresenceResource(ResourcePresence * rPresence)
90 OC_LOG_V(DEBUG, BROKER_TAG, "removePresenceResource()");
91 resourcePresenceList.remove(rPresence);
94 void DevicePresence::changeAllPresenceMode(BROKER_MODE mode)
96 OC_LOG_V(DEBUG, BROKER_TAG, "changeAllPresenceMode()");
97 if(!resourcePresenceList.empty())
99 for(auto it : resourcePresenceList)
101 it->changePresenceMode(mode);
106 bool DevicePresence::isEmptyResourcePresence() const
108 OC_LOG_V(DEBUG, BROKER_TAG, "isEmptyResourcePresence()");
109 return resourcePresenceList.empty();
112 void DevicePresence::subscribeCB(OCStackResult ret,
113 const unsigned int seq, const std::string & hostAddress)
115 OC_LOG_V(DEBUG, BROKER_TAG, "subscribeCB()");
116 OC_LOG_V(DEBUG, BROKER_TAG, "Received presence CB from: %s",hostAddress.c_str());
117 OC_LOG_V(DEBUG, BROKER_TAG, "In subscribeCB: %d",ret);
121 std::unique_lock<std::mutex> lock(timeoutMutex);
122 condition.wait(lock);
124 presenceTimer.cancel(presenceTimerHandle);
129 case OC_STACK_RESOURCE_CREATED:
130 case OC_STACK_CONTINUE:
132 OC_LOG_V(DEBUG, BROKER_TAG, "SEQ# %d",seq);
133 state = DEVICE_STATE::ALIVE;
134 OC_LOG_V(DEBUG, BROKER_TAG, "device state : %d",
135 (int)(state.load(boost::memory_order_consume)));
136 changeAllPresenceMode(BROKER_MODE::DEVICE_PRESENCE_MODE);
138 = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
141 case OC_STACK_INVALID_REQUEST_HANDLE:
142 case OC_STACK_RESOURCE_DELETED:
143 case OC_STACK_TIMEOUT:
144 case OC_STACK_COMM_ERROR:
145 case OC_STACK_PRESENCE_STOPPED:
146 case OC_STACK_PRESENCE_TIMEOUT:
147 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
149 state = DEVICE_STATE::LOST_SIGNAL;
150 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
155 OC_LOG_V(DEBUG, BROKER_TAG, "Presence Lost Signal because unknown type");
156 state = DEVICE_STATE::LOST_SIGNAL;
157 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
163 void DevicePresence::timeOutCB(TimerID /*id*/)
165 OC_LOG_V(DEBUG,BROKER_TAG,"timeOutCB()");
166 std::unique_lock<std::mutex> lock(timeoutMutex);
167 isRunningTimeOut = true;
169 OC_LOG_V(DEBUG, BROKER_TAG,
170 "Timeout execution. will be discard after receiving cb message");
171 state = DEVICE_STATE::LOST_SIGNAL;
172 changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
174 isRunningTimeOut = false;
175 condition.notify_all();
177 } // namespace Service