1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // ============================================================================
23 // ============================================================================
24 #include "MockProtocol.h"
25 #include "SimpleLogger.h"
26 #include "InternalApi.h"
28 #include "Description.hpp"
29 #include "Service.hpp"
30 #include "Characteristic.hpp"
33 #include <boost/uuid/uuid.hpp>
34 #include <boost/uuid/uuid_generators.hpp>
35 #include <boost/uuid/uuid_io.hpp>
36 // ============================================================================
38 // ============================================================================
43 static const char TAG[] = "MockProtocol";
45 const std::string MockProtocol::MOCK_DEVICE_ID = "7c34ad16-ae8c-415b-94cc-d8053f4f9f8e";
46 static std::string characteristic0Value = "CHARACTERISTIC_0_VALUE";
48 // ============================================================================
49 // PropertyGetResult Implementation Class
50 // ============================================================================
51 class PropertyGetResultImpl : public API::PropertyGetResult {
53 PropertyGetResultImpl() {
57 virtual ~PropertyGetResultImpl() {}
60 virtual API::QueryResultType getResult() const {
61 return API::QueryResultType::SUCCESS;
64 virtual const std::string& getName() const {
68 virtual const std::string& getValue() const {
77 // ============================================================================
78 // PropertySetResult Implementation Class
79 // ============================================================================
80 class PropertySetResultImpl : public API::PropertySetResult {
82 PropertySetResultImpl() {
86 virtual ~PropertySetResultImpl() {}
89 virtual API::QueryResultType getResult() const {
90 return API::QueryResultType::SUCCESS;
93 virtual const std::string& getName() const {
97 virtual const std::string& getValue() const {
107 // ============================================================================
108 // Characteristic Implementation Class
109 // ============================================================================
110 class CharacteristicImpl : public API::Characteristic {
112 CharacteristicImpl() {
118 virtual ~CharacteristicImpl() {}
121 virtual const std::string& getName() const {
124 virtual bool isReadable() const {
127 virtual bool isWritable() const {
130 virtual bool isConstant() const {
141 // ============================================================================
142 // Service Implementation Class
143 // ============================================================================
144 class ServiceImpl : public API::Service {
150 virtual ~ServiceImpl() {}
154 virtual const std::string& getName() const {
158 virtual const std::set<API::Characteristic::SharedPtr>& getCharacteristics() const {
159 return characteristicSet_;
163 std::set<API::Characteristic::SharedPtr> characteristicSet_;
165 // ============================================================================
166 // DescriptionGetResult Implementation Class
167 // ============================================================================
168 class DescriptionGetResultImpl : public API::DescriptionGetResult {
170 virtual ~DescriptionGetResultImpl() {}
173 virtual API::QueryResultType getResult() const {
174 return API::QueryResultType::SUCCESS;
177 virtual const std::set<API::Service::SharedPtr>& getServices() const {
182 std::set<API::Service::SharedPtr> serviceSet_;
185 // ============================================================================
186 // RegisteredSetPropFunction
187 // ============================================================================
188 void setPropertyFunction(const std::string& propertyName, const std::string& propertyValue, const API::PropertySetFunction& asyncReturnFunc) {
189 logDebug(TAG, "Entering MockProtocol::setPropertyFunction");
191 PropertySetResultImpl result;
193 if (propertyName == "CHARACTERISTIC_0") {
194 // CHARACTERISTIC_0 is read/write, non-constant, so allow it to be set to new value
195 result.name_ = "CHARACTERISTIC_0";
196 characteristic0Value = propertyValue;
197 result.value_ = characteristic0Value;
199 else if (propertyName == "CHARACTERISTIC_1") {
200 // CHARACTERISTIC_1 is constant, so ignore new value
201 result.name_ = "CHARACTERISTIC_1";
202 result.value_ = "CHARACTERISTIC_1_VALUE";
205 asyncReturnFunc(result);
207 // ============================================================================
208 // RegisteredGetPropFunction
209 // ============================================================================
210 void getPropertyFunction(const std::string& propertyName, const API::PropertyGetFunction& asyncReturnFunc) {
211 logDebug(TAG, "Entering MockProtocol::getPropertyFunction");
213 PropertyGetResultImpl result;
215 if (propertyName == "CHARACTERISTIC_0") {
216 result.name_ = "CHARACTERISTIC_0";
217 result.value_ = characteristic0Value;
219 else if (propertyName == "CHARACTERISTIC_1") {
220 result.name_ = "CHARACTERISTIC_1";
221 result.value_ = "CHARACTERISTIC_1_VALUE";
224 asyncReturnFunc(result);
227 // ============================================================================
228 // RegisteredGetDescriptionFunction
229 // ============================================================================
230 void getDescriptionFunction(const API::DescriptionGetFunction& asyncReturnFunc) {
231 logDebug(TAG, "Entering MockProtocol::getDescriptionFunction");
233 DescriptionGetResultImpl result;
235 std::shared_ptr<ServiceImpl> service = std::make_shared<ServiceImpl>();
237 service->name_ = "SERVICE_A";
238 std::shared_ptr<CharacteristicImpl> characteristic0 = std::make_shared<CharacteristicImpl>();
239 if (characteristic0) {
240 characteristic0->name_ = "CHARACTERISTIC_0";
241 service->characteristicSet_.insert(characteristic0);
243 std::shared_ptr<CharacteristicImpl> characteristic1 = std::make_shared<CharacteristicImpl>();
244 if (characteristic1) {
245 characteristic1->name_ = "CHARACTERISTIC_1";
246 characteristic1->isReadable_ = true;
247 characteristic1->isWritable_ = false;
248 characteristic1->isConstant_ = true;
250 service->characteristicSet_.insert(characteristic1);
253 result.serviceSet_.insert(service);
257 // Invoke the callback
258 asyncReturnFunc(result);
261 // ============================================================================
263 // ============================================================================
264 MockProtocol::MockProtocol()
266 logDebug(TAG, "Entering MockProtocol::MockProtocol");
268 name_ = "Mock Protocol";
269 handle_ = Protocol::INVALID_HANDLE;
272 MockProtocol::~MockProtocol()
274 logDebug(TAG, "Entering MockProtocol::~MockProtocol");
277 void MockProtocol::setModel(const std::shared_ptr<Intel::CCFL::API::Model>& model) {
278 logDebug(TAG, "Entering MockProtocol::setModel");
282 const Intel::CCFL::Protocols::Protocol::Handle MockProtocol::getHandle() {
283 logDebug(TAG, "Entering MockProtocol::getHandle");
287 void MockProtocol::setHandle(const Handle handle) {
288 logDebug(TAG, "Entering MockProtocol::setHandle");
292 const std::string& MockProtocol::getName() {
293 // logDebug(TAG, "Entering MockProtocol::getName");
297 void MockProtocol::setName(const std::string& name)
299 logDebug(TAG, "Entering MockProtocol::setName, name = %s", name.c_str());
303 void MockProtocol::forceDeviceDiscovery() {
304 logDebug(TAG, "Entering MockProtocol::forceDeviceDiscovery");
308 void MockProtocol::testAddDevice(const UUID_t& deviceId, const std::string deviceName) {
309 logDebug(TAG, "Entering MockProtocol::testAddDevice, device name = %s, device UUID = %s", deviceName.c_str(), to_string(deviceId).c_str());
311 std::shared_ptr<API::Model> sharedModel = model_.lock();
313 API::Device::SharedPtr device = sharedModel->getDevice(deviceId);
315 device->setName(deviceName);
316 // Add a link to the device
317 API::Link::SharedPtr link = std::make_shared<API::Link>();
319 // Register property functions for the link
320 API::Link::RegisteredGetDescriptionFunction regGetDescriptionFunction = getDescriptionFunction;
321 link->registerGetDescriptionFunction(regGetDescriptionFunction);
322 API::Link::RegisteredGetPropFunction regGetPropFunction = getPropertyFunction;
323 link->registerGetPropertyFunction(regGetPropFunction);
324 API::Link::RegisteredSetPropFunction regSetPropFunction = setPropertyFunction;
325 link->registerSetPropertyFunction(regSetPropFunction);
327 // Add the link to the device and signal the model that the device changed
328 device->addLink(link);
329 sharedModel->signalDeviceChange(deviceId, API::DeviceEvent::DeviceChange::DEVICE_ADDED);
334 void MockProtocol::testRemoveDevice(const UUID_t& deviceId) {
335 logDebug(TAG, "Entering MockProtocol::testRemoveDevice, device UUID = %s", to_string(deviceId).c_str());
337 std::shared_ptr<API::Model> sharedModel = model_.lock();
339 API::Device::SharedPtr device = sharedModel->getDevice(deviceId);
342 uint32_t linkCnt = device->getLinkCount();
343 logDebug(TAG, "MockProtocol::testRemoveDevice, link count = %d", linkCnt);
345 API::Device::LinkList linkList = device->getLinks();
346 API::Link::SharedPtr link = linkList.front();
348 logDebug(TAG, "MockProtocol::testRemoveDevice, removing link");
349 if (device->removeLink(link)) {
350 logDebug(TAG, "MockProtocol::testRemoveDevice, link removed, signaling device change");
351 sharedModel->signalDeviceChange(deviceId, API::DeviceEvent::DeviceChange::DEVICE_REMOVED);