Imported Upstream version 1.0.0
[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                 OC_LOG_V(DEBUG,BROKER_TAG,"unsubscribed presence.");
45                 presenceSubscriber.unsubscribe();
46             }
47             resourcePresenceList.clear();
48             OC_LOG_V(DEBUG,BROKER_TAG,"destroy Timer.");
49         }
50
51         void DevicePresence::initializeDevicePresence(PrimitiveResourcePtr pResource)
52         {
53             OC_LOG_V(DEBUG, BROKER_TAG, "initializeDevicePresence()");
54             address = pResource->getHost();
55
56             OC_LOG_V(DEBUG, BROKER_TAG, "%s",address.c_str());
57
58             try
59             {
60                 OC_LOG_V(DEBUG, BROKER_TAG, "subscribe Presence");
61                 presenceSubscriber
62                 = PresenceSubscriber(address, BROKER_TRANSPORT, pSubscribeRequestCB);
63             } catch(RCSPlatformException &e)
64             {
65                 OC_LOG_V(DEBUG, BROKER_TAG,
66                         "exception in subscribe Presence %s", e.getReason().c_str());
67                 throw;
68             }
69             presenceTimerHandle
70             = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
71         }
72
73         DEVICE_STATE DevicePresence::getDeviceState() const noexcept
74         {
75             return static_cast< DEVICE_STATE >(state.load());
76         }
77
78         void DevicePresence::setDeviceState(DEVICE_STATE newState)
79         {
80             state = static_cast< int >(newState);
81         }
82
83         const std::string DevicePresence::getAddress() const
84         {
85             OC_LOG_V(DEBUG, BROKER_TAG, "getAddress()");
86             return address;
87         }
88
89         void DevicePresence::addPresenceResource(ResourcePresence * rPresence)
90         {
91             OC_LOG_V(DEBUG, BROKER_TAG, "addPresenceResource()");
92             resourcePresenceList.push_back(rPresence);
93         }
94
95         void DevicePresence::removePresenceResource(ResourcePresence * rPresence)
96         {
97             OC_LOG_V(DEBUG, BROKER_TAG, "removePresenceResource()");
98             resourcePresenceList.remove(rPresence);
99         }
100
101         void DevicePresence::changeAllPresenceMode(BROKER_MODE mode)
102         {
103             OC_LOG_V(DEBUG, BROKER_TAG, "changeAllPresenceMode()");
104             if(!resourcePresenceList.empty())
105             {
106                 for(auto it : resourcePresenceList)
107                 {
108                     it->changePresenceMode(mode);
109                 }
110             }
111         }
112
113         bool DevicePresence::isEmptyResourcePresence() const
114         {
115             OC_LOG_V(DEBUG, BROKER_TAG, "isEmptyResourcePresence()");
116             return resourcePresenceList.empty();
117         }
118
119         void DevicePresence::subscribeCB(OCStackResult ret,
120                 const unsigned int seq, const std::string & hostAddress)
121         {
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);
125
126             if(isRunningTimeOut)
127             {
128                 std::unique_lock<std::mutex> lock(timeoutMutex);
129                 condition.wait(lock);
130             }
131             presenceTimer.cancel(presenceTimerHandle);
132
133             switch(ret)
134             {
135                 case OC_STACK_OK:
136                 case OC_STACK_RESOURCE_CREATED:
137                 case OC_STACK_CONTINUE:
138                 {
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);
144                     presenceTimerHandle
145                     = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
146                     break;
147                 }
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:
155                 {
156                     setDeviceState(DEVICE_STATE::LOST_SIGNAL);
157                     changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
158                     break;
159                 }
160                 default:
161                 {
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);
165                     break;
166                 }
167             }
168         }
169
170         void DevicePresence::timeOutCB(TimerID /*id*/)
171         {
172             OC_LOG_V(DEBUG,BROKER_TAG,"timeOutCB()");
173             std::unique_lock<std::mutex> lock(timeoutMutex);
174             isRunningTimeOut = true;
175
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);
180
181             isRunningTimeOut = false;
182             condition.notify_all();
183         }
184     } // namespace Service
185 } // namespace OIC