Merge branch 'easysetup'
[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             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);
131
132             if(isRunningTimeOut)
133             {
134                 std::unique_lock<std::mutex> lock(timeoutMutex);
135                 condition.wait(lock);
136             }
137             presenceTimer.cancel(presenceTimerHandle);
138
139             switch(ret)
140             {
141                 case OC_STACK_OK:
142                 case OC_STACK_RESOURCE_CREATED:
143                 case OC_STACK_CONTINUE:
144                 {
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);
150                     presenceTimerHandle
151                     = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
152                     break;
153                 }
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:
161                 {
162                     setDeviceState(DEVICE_STATE::LOST_SIGNAL);
163                     changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
164                     break;
165                 }
166                 default:
167                 {
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);
171                     break;
172                 }
173             }
174         }
175
176         void DevicePresence::timeOutCB(TimerID /*id*/)
177         {
178             OIC_LOG_V(DEBUG,BROKER_TAG,"timeOutCB()");
179             std::unique_lock<std::mutex> lock(timeoutMutex);
180             isRunningTimeOut = true;
181
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);
186
187             isRunningTimeOut = false;
188             condition.notify_all();
189         }
190     } // namespace Service
191 } // namespace OIC