1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 // ============================================================================
8 // ============================================================================
9 #include "MockProtocol.h"
10 #include "SimpleLogger.h"
11 #include "InternalApi.h"
13 #include "Description.hpp"
14 #include "Service.hpp"
15 #include "Characteristic.hpp"
18 #include <boost/uuid/uuid.hpp>
19 #include <boost/uuid/uuid_generators.hpp>
20 #include <boost/uuid/uuid_io.hpp>
21 // ============================================================================
23 // ============================================================================
28 static const char TAG[] = "MockProtocol";
30 const std::string MockProtocol::MOCK_DEVICE_ID = "7c34ad16-ae8c-415b-94cc-d8053f4f9f8e";
31 static std::string characteristic0Value = "CHARACTERISTIC_0_VALUE";
33 // ============================================================================
34 // PropertyGetResult Implementation Class
35 // ============================================================================
36 class PropertyGetResultImpl : public API::PropertyGetResult {
38 PropertyGetResultImpl() {
42 virtual ~PropertyGetResultImpl() {}
45 virtual API::QueryResultType getResult() const {
46 return API::QueryResultType::SUCCESS;
49 virtual const std::string& getName() const {
53 virtual const std::string& getValue() const {
62 // ============================================================================
63 // PropertySetResult Implementation Class
64 // ============================================================================
65 class PropertySetResultImpl : public API::PropertySetResult {
67 PropertySetResultImpl() {
71 virtual ~PropertySetResultImpl() {}
74 virtual API::QueryResultType getResult() const {
75 return API::QueryResultType::SUCCESS;
78 virtual const std::string& getName() const {
82 virtual const std::string& getValue() const {
92 // ============================================================================
93 // Characteristic Implementation Class
94 // ============================================================================
95 class CharacteristicImpl : public API::Characteristic {
97 CharacteristicImpl() {
103 virtual ~CharacteristicImpl() {}
106 virtual const std::string& getName() const {
109 virtual bool isReadable() const {
112 virtual bool isWritable() const {
115 virtual bool isConstant() const {
126 // ============================================================================
127 // Service Implementation Class
128 // ============================================================================
129 class ServiceImpl : public API::Service {
135 virtual ~ServiceImpl() {}
139 virtual const std::string& getName() const {
143 virtual const std::set<API::Characteristic::SharedPtr>& getCharacteristics() const {
144 return characteristicSet_;
148 std::set<API::Characteristic::SharedPtr> characteristicSet_;
150 // ============================================================================
151 // DescriptionGetResult Implementation Class
152 // ============================================================================
153 class DescriptionGetResultImpl : public API::DescriptionGetResult {
155 virtual ~DescriptionGetResultImpl() {}
158 virtual API::QueryResultType getResult() const {
159 return API::QueryResultType::SUCCESS;
162 virtual const std::set<API::Service::SharedPtr>& getServices() const {
167 std::set<API::Service::SharedPtr> serviceSet_;
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");
176 PropertySetResultImpl result;
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;
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";
190 asyncReturnFunc(result);
192 // ============================================================================
193 // RegisteredGetPropFunction
194 // ============================================================================
195 void getPropertyFunction(const std::string& propertyName, const API::PropertyGetFunction& asyncReturnFunc) {
196 logDebug(TAG, "Entering MockProtocol::getPropertyFunction");
198 PropertyGetResultImpl result;
200 if (propertyName == "CHARACTERISTIC_0") {
201 result.name_ = "CHARACTERISTIC_0";
202 result.value_ = characteristic0Value;
204 else if (propertyName == "CHARACTERISTIC_1") {
205 result.name_ = "CHARACTERISTIC_1";
206 result.value_ = "CHARACTERISTIC_1_VALUE";
209 asyncReturnFunc(result);
212 // ============================================================================
213 // RegisteredGetDescriptionFunction
214 // ============================================================================
215 void getDescriptionFunction(const API::DescriptionGetFunction& asyncReturnFunc) {
216 logDebug(TAG, "Entering MockProtocol::getDescriptionFunction");
218 DescriptionGetResultImpl result;
220 std::shared_ptr<ServiceImpl> service = std::make_shared<ServiceImpl>();
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);
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;
235 service->characteristicSet_.insert(characteristic1);
238 result.serviceSet_.insert(service);
242 // Invoke the callback
243 asyncReturnFunc(result);
246 // ============================================================================
248 // ============================================================================
249 MockProtocol::MockProtocol()
251 logDebug(TAG, "Entering MockProtocol::MockProtocol");
253 name_ = "Mock Protocol";
254 handle_ = Protocol::INVALID_HANDLE;
257 MockProtocol::~MockProtocol()
259 logDebug(TAG, "Entering MockProtocol::~MockProtocol");
262 void MockProtocol::setModel(const std::shared_ptr<Intel::CCFL::API::Model>& model) {
263 logDebug(TAG, "Entering MockProtocol::setModel");
267 const Intel::CCFL::Protocols::Protocol::Handle MockProtocol::getHandle() {
268 logDebug(TAG, "Entering MockProtocol::getHandle");
272 void MockProtocol::setHandle(const Handle handle) {
273 logDebug(TAG, "Entering MockProtocol::setHandle");
277 const std::string& MockProtocol::getName() {
278 // logDebug(TAG, "Entering MockProtocol::getName");
282 void MockProtocol::setName(const std::string& name)
284 logDebug(TAG, "Entering MockProtocol::setName, name = %s", name.c_str());
288 void MockProtocol::forceDeviceDiscovery() {
289 logDebug(TAG, "Entering MockProtocol::forceDeviceDiscovery");
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());
296 std::shared_ptr<API::Model> sharedModel = model_.lock();
298 API::Device::SharedPtr device = sharedModel->getDevice(deviceId);
300 device->setName(deviceName);
301 // Add a link to the device
302 API::Link::SharedPtr link = std::make_shared<API::Link>();
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);
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);
319 void MockProtocol::testRemoveDevice(const UUID_t& deviceId) {
320 logDebug(TAG, "Entering MockProtocol::testRemoveDevice, device UUID = %s", to_string(deviceId).c_str());
322 std::shared_ptr<API::Model> sharedModel = model_.lock();
324 API::Device::SharedPtr device = sharedModel->getDevice(deviceId);
327 uint32_t linkCnt = device->getLinkCount();
328 logDebug(TAG, "MockProtocol::testRemoveDevice, link count = %d", linkCnt);
330 API::Device::LinkList linkList = device->getLinks();
331 API::Link::SharedPtr link = linkList.front();
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);