Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / controller / core / test / MockProtocol.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 // ============================================================================
7 // Includes
8 // ============================================================================
9 #include "MockProtocol.h"
10 #include "SimpleLogger.h"
11 #include "InternalApi.h"
12 #include "Link.hpp"
13 #include "Description.hpp"
14 #include "Service.hpp"
15 #include "Characteristic.hpp"
16 #include <set>
17
18 #include <boost/uuid/uuid.hpp>
19 #include <boost/uuid/uuid_generators.hpp>
20 #include <boost/uuid/uuid_io.hpp>
21 // ============================================================================
22 // Namespace
23 // ============================================================================
24 namespace Intel {
25 namespace CCFL {
26 namespace Protocols {
27
28 static const char TAG[] = "MockProtocol";
29
30 const std::string MockProtocol::MOCK_DEVICE_ID = "7c34ad16-ae8c-415b-94cc-d8053f4f9f8e";
31 static std::string characteristic0Value = "CHARACTERISTIC_0_VALUE";
32
33 // ============================================================================
34 // PropertyGetResult Implementation Class
35 // ============================================================================
36 class PropertyGetResultImpl : public API::PropertyGetResult {
37 public:
38         PropertyGetResultImpl() {
39                 name_ = "";
40                 value_ = "";
41         }
42         virtual ~PropertyGetResultImpl() {}
43
44 public:
45         virtual API::QueryResultType getResult() const {
46                 return API::QueryResultType::SUCCESS;
47         }
48
49         virtual const std::string& getName() const {
50                 return name_;
51         }
52
53         virtual const std::string& getValue() const {
54                 return value_;
55         }
56
57 public:
58         std::string name_;
59         std::string value_;
60 };
61
62 // ============================================================================
63 // PropertySetResult Implementation Class
64 // ============================================================================
65 class PropertySetResultImpl : public API::PropertySetResult {
66 public:
67         PropertySetResultImpl() {
68                 name_ = "";
69                 value_ = "";
70         }
71         virtual ~PropertySetResultImpl() {}
72
73 public:
74         virtual API::QueryResultType getResult() const {
75                 return API::QueryResultType::SUCCESS;
76         }
77
78         virtual const std::string& getName() const {
79                 return name_;
80         }
81
82         virtual const std::string& getValue() const {
83                 return value_;
84         }
85
86 public:
87         std::string name_;
88         std::string value_;
89
90 };
91
92 // ============================================================================
93 // Characteristic Implementation Class
94 // ============================================================================
95 class CharacteristicImpl : public API::Characteristic {
96 public:
97         CharacteristicImpl() {
98                 name_ = "";
99                 isReadable_ = true;
100                 isWritable_ = true;
101                 isConstant_ = false;
102         }
103         virtual ~CharacteristicImpl() {}
104
105 public:
106         virtual const std::string& getName() const {
107                 return name_;
108         }
109         virtual bool isReadable() const {
110                 return isReadable_;
111         }
112         virtual bool isWritable() const {
113                 return isWritable_;
114         }
115         virtual bool isConstant() const {
116                 return isConstant_;
117         }
118
119 public:
120         std::string name_;
121         bool isReadable_;
122         bool isWritable_;
123         bool isConstant_;
124 };
125
126 // ============================================================================
127 // Service Implementation Class
128 // ============================================================================
129 class ServiceImpl : public API::Service {
130 public:
131         ServiceImpl() {
132                 name_ = "";
133         }
134
135         virtual ~ServiceImpl() {}
136
137 public:
138
139         virtual const std::string& getName() const {
140                 return name_;
141         }
142
143         virtual const std::set<API::Characteristic::SharedPtr>& getCharacteristics() const {
144                 return characteristicSet_;
145         }
146 public:
147         std::string name_;
148         std::set<API::Characteristic::SharedPtr> characteristicSet_;
149 };
150 // ============================================================================
151 // DescriptionGetResult Implementation Class
152 // ============================================================================
153 class DescriptionGetResultImpl : public API::DescriptionGetResult {
154 public:
155         virtual ~DescriptionGetResultImpl() {}
156
157 public:
158         virtual API::QueryResultType getResult() const {
159                 return API::QueryResultType::SUCCESS;
160         };
161
162         virtual const std::set<API::Service::SharedPtr>& getServices() const {
163                 return serviceSet_;
164         }
165
166 public:
167         std::set<API::Service::SharedPtr> serviceSet_;
168 };
169
170 // ============================================================================
171 // RegisteredSetPropFunction
172 // ============================================================================
173 void setPropertyFunction(const std::string& propertyName, const std::string& propertyValue, const API::PropertySetFunction& asyncReturnFunc) {
174         logDebug(TAG, "Entering MockProtocol::setPropertyFunction");
175
176         PropertySetResultImpl result;
177
178         if (propertyName == "CHARACTERISTIC_0") {
179                 // CHARACTERISTIC_0 is read/write, non-constant, so allow it to be set to new value
180                 result.name_ = "CHARACTERISTIC_0";
181                 characteristic0Value = propertyValue;
182                 result.value_ = characteristic0Value;
183         }
184         else if (propertyName == "CHARACTERISTIC_1") {
185                 // CHARACTERISTIC_1 is constant, so ignore new value
186                 result.name_ = "CHARACTERISTIC_1";
187                 result.value_ = "CHARACTERISTIC_1_VALUE";
188         }
189
190         asyncReturnFunc(result);
191 }
192 // ============================================================================
193 // RegisteredGetPropFunction
194 // ============================================================================
195 void getPropertyFunction(const std::string& propertyName, const API::PropertyGetFunction& asyncReturnFunc) {
196         logDebug(TAG, "Entering MockProtocol::getPropertyFunction");
197
198         PropertyGetResultImpl result;
199
200         if (propertyName == "CHARACTERISTIC_0") {
201                 result.name_ = "CHARACTERISTIC_0";
202                 result.value_ = characteristic0Value;
203         }
204         else if (propertyName == "CHARACTERISTIC_1") {
205                 result.name_ = "CHARACTERISTIC_1";
206                 result.value_ = "CHARACTERISTIC_1_VALUE";
207         }
208
209         asyncReturnFunc(result);
210 }
211
212 // ============================================================================
213 // RegisteredGetDescriptionFunction
214 // ============================================================================
215 void getDescriptionFunction(const API::DescriptionGetFunction& asyncReturnFunc) {
216         logDebug(TAG, "Entering MockProtocol::getDescriptionFunction");
217
218         DescriptionGetResultImpl result;
219
220         std::shared_ptr<ServiceImpl> service = std::make_shared<ServiceImpl>();
221         if (service) {
222                 service->name_ = "SERVICE_A";
223                 std::shared_ptr<CharacteristicImpl> characteristic0 = std::make_shared<CharacteristicImpl>();
224                 if (characteristic0) {
225                         characteristic0->name_ = "CHARACTERISTIC_0";
226                         service->characteristicSet_.insert(characteristic0);
227                 }
228                 std::shared_ptr<CharacteristicImpl> characteristic1 = std::make_shared<CharacteristicImpl>();
229                 if (characteristic1) {
230                         characteristic1->name_ = "CHARACTERISTIC_1";
231                         characteristic1->isReadable_ = true;
232                         characteristic1->isWritable_ = false;
233                         characteristic1->isConstant_ = true;
234
235                         service->characteristicSet_.insert(characteristic1);
236                 }
237
238                 result.serviceSet_.insert(service);
239         }
240
241
242   // Invoke the callback
243   asyncReturnFunc(result);
244 }
245
246 // ============================================================================
247 // Class
248 // ============================================================================
249 MockProtocol::MockProtocol()
250 {
251         logDebug(TAG, "Entering MockProtocol::MockProtocol");
252
253         name_ = "Mock Protocol";
254         handle_ = Protocol::INVALID_HANDLE;
255 }
256
257 MockProtocol::~MockProtocol()
258 {
259         logDebug(TAG, "Entering MockProtocol::~MockProtocol");
260 }
261
262 void MockProtocol::setModel(const std::shared_ptr<Intel::CCFL::API::Model>& model) {
263         logDebug(TAG, "Entering MockProtocol::setModel");
264         model_ = model;
265 }
266
267 const Intel::CCFL::Protocols::Protocol::Handle MockProtocol::getHandle() {
268         logDebug(TAG, "Entering MockProtocol::getHandle");
269         return 0;
270 }
271
272 void MockProtocol::setHandle(const Handle handle) {
273         logDebug(TAG, "Entering MockProtocol::setHandle");
274         handle_ = handle;
275 }
276
277 const std::string& MockProtocol::getName() {
278 //      logDebug(TAG, "Entering MockProtocol::getName");
279         return name_;
280 }
281
282 void MockProtocol::setName(const std::string& name)
283 {
284         logDebug(TAG, "Entering MockProtocol::setName, name = %s", name.c_str());
285         name_ = name;
286 }
287
288 void MockProtocol::forceDeviceDiscovery() {
289         logDebug(TAG, "Entering MockProtocol::forceDeviceDiscovery");
290 }
291
292 // Test interface
293 void MockProtocol::testAddDevice(const UUID_t& deviceId, const std::string deviceName) {
294         logDebug(TAG, "Entering MockProtocol::testAddDevice, device name = %s, device UUID = %s", deviceName.c_str(), to_string(deviceId).c_str());
295
296         std::shared_ptr<API::Model> sharedModel = model_.lock();
297         if (sharedModel) {
298                 API::Device::SharedPtr device = sharedModel->getDevice(deviceId);
299                 if (device) {
300                         device->setName(deviceName);
301                         // Add a link to the device
302                         API::Link::SharedPtr link = std::make_shared<API::Link>();
303
304                         // Register property functions for the link
305                         API::Link::RegisteredGetDescriptionFunction regGetDescriptionFunction = getDescriptionFunction;
306                         link->registerGetDescriptionFunction(regGetDescriptionFunction);
307                         API::Link::RegisteredGetPropFunction regGetPropFunction = getPropertyFunction;
308                         link->registerGetPropertyFunction(regGetPropFunction);
309                         API::Link::RegisteredSetPropFunction regSetPropFunction = setPropertyFunction;
310                         link->registerSetPropertyFunction(regSetPropFunction);
311
312                         // Add the link to the device and signal the model that the device changed
313                         device->addLink(link);
314                         sharedModel->signalDeviceChange(deviceId, API::DeviceEvent::DeviceChange::DEVICE_ADDED);
315                 }
316         }
317 }
318
319 void MockProtocol::testRemoveDevice(const UUID_t& deviceId) {
320         logDebug(TAG, "Entering MockProtocol::testRemoveDevice, device UUID = %s", to_string(deviceId).c_str());
321
322         std::shared_ptr<API::Model> sharedModel = model_.lock();
323         if (sharedModel) {
324                 API::Device::SharedPtr device = sharedModel->getDevice(deviceId);
325                 if (device) {
326                         // Remove the link
327                         uint32_t linkCnt = device->getLinkCount();
328                         logDebug(TAG, "MockProtocol::testRemoveDevice, link count = %d", linkCnt);
329                         if (linkCnt > 0) {
330                                 API::Device::LinkList linkList = device->getLinks();
331                                 API::Link::SharedPtr link = linkList.front();
332                                 if (link) {
333                                         logDebug(TAG, "MockProtocol::testRemoveDevice, removing link");
334                                         if (device->removeLink(link)) {
335                                                 logDebug(TAG, "MockProtocol::testRemoveDevice, link removed, signaling device change");
336                                                 sharedModel->signalDeviceChange(deviceId, API::DeviceEvent::DeviceChange::DEVICE_REMOVED);
337                                         }
338                                 }
339                         }
340                 }
341         }
342 }
343
344 }
345 }
346 }