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