Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceBroker / src / DeviceAssociation.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 "DeviceAssociation.h"
22 #include "DevicePresence.h"
23
24
25 namespace OIC
26 {
27     namespace Service
28     {
29         DeviceAssociation * DeviceAssociation::s_instance = nullptr;
30         std::mutex DeviceAssociation::s_mutexForCreation;
31         std::list< DevicePresencePtr >  DeviceAssociation::s_deviceList;
32
33         DeviceAssociation::DeviceAssociation()
34         {
35             // TODO Auto-generated constructor stub
36         }
37
38         DeviceAssociation::~DeviceAssociation()
39         {
40             // TODO Auto-generated destructor stub
41         }
42
43         DeviceAssociation * DeviceAssociation::getInstance()
44         {
45             if (!s_instance)
46             {
47                 s_mutexForCreation.lock();
48                 if (!s_instance)
49                 {
50                     s_instance = new DeviceAssociation();
51                 }
52                 s_mutexForCreation.unlock();
53             }
54             return s_instance;
55         }
56
57         DevicePresencePtr DeviceAssociation::findDevice(const std::string & address)
58         {
59             OC_LOG_V(DEBUG,BROKER_TAG,"findDevice()");
60             DevicePresencePtr retDevice = nullptr;
61             for(auto it : s_deviceList)
62             {
63                 if(address == it->getAddress())
64                 {
65                     OC_LOG_V(DEBUG,BROKER_TAG,"find device in deviceList");
66                     retDevice = it;
67                     break;
68                 }
69             }
70
71             return retDevice;
72         }
73
74         void DeviceAssociation::addDevice(DevicePresencePtr dPresence)
75         {
76             OC_LOG_V(DEBUG,BROKER_TAG,"addDevice()");
77             DevicePresencePtr foundDevice = findDevice(dPresence->getAddress());
78             if(foundDevice == nullptr)
79             {
80                 OC_LOG_V(DEBUG,BROKER_TAG,"add device in deviceList");
81                 s_deviceList.push_back(dPresence);
82             }
83         }
84
85         void DeviceAssociation::removeDevice(DevicePresencePtr dPresence)
86         {
87             OC_LOG_V(DEBUG,BROKER_TAG,"removeDevice()");
88             DevicePresencePtr foundDevice = findDevice(dPresence->getAddress());
89             if(foundDevice != nullptr)
90             {
91                 OC_LOG_V(DEBUG,BROKER_TAG,"remove device in deviceList");
92                 s_deviceList.remove(foundDevice);
93                 foundDevice.reset();
94             }
95         }
96
97         bool DeviceAssociation::isEmptyDeviceList()
98         {
99             OC_LOG_V(DEBUG,BROKER_TAG,"isEmptyDeviceList()");
100             return s_deviceList.empty();
101         }
102     } // namespace Service
103 } // namespace OIC