Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceBroker / include / DevicePresence.h
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 #ifndef RB_DEVICEPRESENCE_H_
22 #define RB_DEVICEPRESENCE_H_
23
24 #include <list>
25 #include <string>
26 #include <atomic>
27
28 #include "BrokerTypes.h"
29 #include "ResourcePresence.h"
30 #include "PresenceSubscriber.h"
31 #include "ExpiryTimer.h"
32
33 namespace OIC
34 {
35     namespace Service
36     {
37         class DevicePresence
38         {
39         public:
40             typedef long long TimerID;
41
42             DevicePresence();
43             ~DevicePresence();
44
45             DevicePresence(DevicePresence &&) = delete;
46             DevicePresence(const DevicePresence &) = delete;
47
48             DevicePresence & operator = (DevicePresence &&) = delete;
49             DevicePresence & operator = (const DevicePresence &) = delete;
50
51             void initializeDevicePresence(PrimitiveResourcePtr pResource);
52
53             void addPresenceResource(ResourcePresence * rPresence);
54             void removePresenceResource(ResourcePresence * rPresence);
55
56             bool isEmptyResourcePresence() const;
57             const std::string getAddress() const;
58             DEVICE_STATE getDeviceState() const noexcept;
59
60         private:
61             std::list<ResourcePresence * > resourcePresenceList;
62
63             std::string address;
64             std::atomic_int state;
65             std::atomic_bool isRunningTimeOut;
66
67             std::mutex timeoutMutex;
68             std::condition_variable condition;
69
70             ExpiryTimer presenceTimer;
71             TimerID presenceTimerHandle;
72             TimerCB pTimeoutCB;
73             SubscribeCB pSubscribeRequestCB;
74             PresenceSubscriber presenceSubscriber;
75
76             void changeAllPresenceMode(BROKER_MODE mode);
77             void subscribeCB(OCStackResult ret,const unsigned int seq, const std::string& Hostaddress);
78             void timeOutCB(TimerID id);
79
80             void setDeviceState(DEVICE_STATE);
81         };
82     } // namespace Service
83 } // namespace OIC
84
85 #endif /* RB_DEVICEPRESENCE_H_ */