Merge "Partial Implementation of US1574:"
[platform/upstream/iotivity.git] / csdk / controller / core / include / core / Device.hpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 DEVICE_H_
22 #define DEVICE_H_
23
24 // ============================================================================
25 // Includes
26 // ============================================================================
27 #include <string>
28 #include <list>
29 #include <cassert>
30 #include <stdint.h>
31
32 #include "Core.h"
33 #include "UUIDLess.hpp"
34 #include "Link.hpp"
35 #include "SimpleLogger.h"
36
37 // ============================================================================
38 // Namespace
39 // ============================================================================
40 namespace Intel {
41 namespace CCFL {
42 namespace API {
43
44 static const char DEVICE_TAG[] = "Device";
45
46 class Device {
47         // ============================================================
48         // Type Definition(s)
49         // ============================================================
50 public:
51                 typedef std::shared_ptr<Device> SharedPtr;
52                 typedef std::weak_ptr<Device> WeakPtr;
53                 typedef std::list<Link::SharedPtr> LinkList;
54
55 public:
56         Device(const UUID_t& deviceId) {
57                 deviceId_ = deviceId;
58                 name_ = "";
59         }
60         virtual ~Device() {
61                 linkList_.clear();
62         };
63
64 public:
65         const UUID_t& getId() {
66                 mutex_lock_guard lock(deviceMutex_);
67                 return deviceId_;
68         };
69
70         const std::string& getName() {
71                 mutex_lock_guard lock(deviceMutex_);
72                 return name_;
73         };
74
75         void setName(const std::string& name) {
76                 mutex_lock_guard lock(deviceMutex_);
77                 name_ = name;
78         };
79
80         void addLink(const Link::SharedPtr& link) {
81                 logDebug(DEVICE_TAG, "Entering Link::addLink");
82                 assert(link);
83                 mutex_lock_guard lock(deviceMutex_);
84
85                 // If list is empty, go ahead and add
86                 if (linkList_.empty())
87           {
88                         logDebug(DEVICE_TAG, "Link::addLink, linkList empty, adding");
89                         linkList_.push_back(link);
90                         return;
91           }
92
93                 // Search list for the link. If already in list, return
94                 for (auto iter = linkList_.begin(); iter != linkList_.end(); ++iter) {
95                         if (*iter == link) {
96                                 logDebug(DEVICE_TAG, "Link::addLink, link already in linkList");
97                                 return;
98                         }
99                 }
100
101                 // Not already in list, so add
102                 logDebug(DEVICE_TAG, "Link::addLink, adding link");
103                 linkList_.push_back(link);
104         }
105
106         bool removeLink(const Link::SharedPtr& link) {
107                 logDebug(DEVICE_TAG, "Entering Link::removeLink");
108                 assert(link);
109
110                 mutex_lock_guard lock(deviceMutex_);
111                 // Return immediately if the list is empty
112                 if (linkList_.empty()) {
113                         logDebug(DEVICE_TAG, "Link::removeLink, linkList empty");
114                         return false;
115                 }
116
117                 // Search list for the link. If in the list, return
118                 for (auto iter = linkList_.begin(); iter != linkList_.end(); ++iter) {
119                         if (*iter == link) {
120                                 linkList_.erase(iter);
121                                 logDebug(DEVICE_TAG, "Link::removeLink, removing link");
122                                 return true;
123                         }
124                 }
125
126                 return false;
127         }
128
129         LinkList getLinks() {
130                 mutex_lock_guard lock(deviceMutex_);
131                 return linkList_;
132         }
133
134         uint32_t getLinkCount() {
135                 mutex_lock_guard lock(deviceMutex_);
136                 return (uint32_t)linkList_.size();
137         }
138
139 private:
140         UUID_t deviceId_;
141         std::string name_;
142         LinkList linkList_;
143         mutex deviceMutex_;
144 };
145
146 }
147 }
148 }
149
150
151
152 #endif /* DEVICE_H_ */