1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
18 #include "InternalApi.h"
19 #include "SimpleLogger.h"
20 #include "MockProtocol.h"
22 #include "Description.hpp"
24 #include <boost/uuid/uuid.hpp>
25 #include <boost/uuid/uuid_generators.hpp>
26 #include <boost/uuid/uuid_io.hpp>
28 const std::string MOCK_DEVICE_ID0 = "7c34ad16-ae8c-415b-94cc-d8053f4f9f8e";
29 const std::string MOCK_DEVICE_ID1 = "646344d0-271c-49dd-8b13-3a7bec1237bd";
30 const std::string MOCK_DEVICE_ID2 = "74c406ca-1cfb-4106-a82f-6123e3fe3555";
32 static const char TAG[] = "CCFL-CoreTest.cpp";
34 UUID_t toUuid(const std::string& uuidStr) {
35 boost::uuids::string_generator gen;
41 void setPropertyAsync(const Intel::CCFL::API::PropertySetResult& result) {
42 Intel::CCFL::logDebug(TAG, "\t********entering main::setPropertyAsync********");
43 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
44 Intel::CCFL::logDebug(TAG, "\t QueryResultType::SUCCESS");
45 Intel::CCFL::logDebug(TAG, "\t property:");
46 Intel::CCFL::logDebug(TAG, "\t\t name = %s", result.getName().c_str());
47 Intel::CCFL::logDebug(TAG, "\t\t value = %s", result.getValue().c_str());
49 Intel::CCFL::logDebug(TAG, "\t********leaving main::setPropertyAsync********");
52 void getPropertyAsync(const Intel::CCFL::API::PropertyGetResult& result) {
53 Intel::CCFL::logDebug(TAG, "\t********entering main::getPropertyAsync********");
54 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
55 Intel::CCFL::logDebug(TAG, "\t QueryResultType::SUCCESS");
56 Intel::CCFL::logDebug(TAG, "\t property:");
57 Intel::CCFL::logDebug(TAG, "\t\t name = %s", result.getName().c_str());
58 Intel::CCFL::logDebug(TAG, "\t\t value = %s", result.getValue().c_str());
60 Intel::CCFL::logDebug(TAG, "\t********leaving main::getPropertyAsync********");
63 void getDescriptionAsync(const Intel::CCFL::API::DescriptionGetResult& result) {
64 Intel::CCFL::logDebug(TAG, "\t********entering main::getDescriptionAsync********");
66 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
67 Intel::CCFL::logDebug(TAG, "\t QueryResultType::SUCCESS");
68 std::set<Intel::CCFL::API::Service::SharedPtr> serviceSet = result.getServices();
70 for (auto iter = serviceSet.begin(); iter != serviceSet.end(); ++iter) {
71 Intel::CCFL::API::Service::WeakPtr serviceWeak = *iter;
72 Intel::CCFL::API::Service::SharedPtr service = serviceWeak.lock();
74 Intel::CCFL::logDebug(TAG, "\t service = %s", service->getName().c_str());
76 std::set<Intel::CCFL::API::Characteristic::SharedPtr> characteristicSet = service->getCharacteristics();
77 for (auto iter = characteristicSet.begin(); iter != characteristicSet.end(); ++iter) {
78 Intel::CCFL::API::Characteristic::WeakPtr characteristicWeak = *iter;
79 Intel::CCFL::API::Characteristic::SharedPtr characteristic = characteristicWeak.lock();
81 Intel::CCFL::logDebug(TAG, "\t\t characteristic = %s", characteristic->getName().c_str());
82 Intel::CCFL::logDebug(TAG, "\t\t readable = %d", characteristic->isReadable());
83 Intel::CCFL::logDebug(TAG, "\t\t writable = %d", characteristic->isWritable());
84 Intel::CCFL::logDebug(TAG, "\t\t constant = %d", characteristic->isConstant());
90 Intel::CCFL::logDebug(TAG, "\t********leaving main::getDescriptionAsync********");
93 void getDevicesAsync(const Intel::CCFL::API::GetDevicesResult& result) {
94 Intel::CCFL::logDebug(TAG, "Entering main::getDevicesAsync");
96 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
97 Intel::CCFL::logDebug(TAG, "main::getDevicesAsync, QueryResultType::SUCCESS");
98 std::list<Intel::CCFL::API::Device::SharedPtr> deviceList = result.getDeviceList();
99 // Search list for the device based on device id
100 if (deviceList.size() > 0) {
101 for (auto iter = deviceList.begin(); iter != deviceList.end(); ++iter) {
102 Intel::CCFL::API::Device::WeakPtr deviceWeak = *iter;
103 Intel::CCFL::API::Device::SharedPtr device = deviceWeak.lock();
105 Intel::CCFL::logDebug(TAG, "\tDevice:");
106 Intel::CCFL::logDebug(TAG, "\t name = %s", device->getName().c_str());
107 Intel::CCFL::logDebug(TAG, "\t UUID = %s", to_string(device->getId()).c_str());
109 // Test characteristic stuff
110 if (device->getName() == "MockDevice0") {
111 if (device->getLinkCount() > 0) {
112 Intel::CCFL::API::Device::LinkList linkList = device->getLinks();
113 Intel::CCFL::API::Link::WeakPtr linkWeak = linkList.front();
114 Intel::CCFL::API::Link::SharedPtr linkShared = linkWeak.lock();
116 // Test set/get properties
117 Intel::CCFL::API::DescriptionGetFunction descriptionGetFunction = getDescriptionAsync;
118 linkShared->getDescription(descriptionGetFunction);
119 Intel::CCFL::API::PropertyGetFunction propertyGetFunction = getPropertyAsync;
120 linkShared->getProperty("CHARACTERISTIC_0", propertyGetFunction);
121 linkShared->getProperty("CHARACTERISTIC_1", propertyGetFunction);
123 Intel::CCFL::API::PropertySetFunction propertySetFunction = setPropertyAsync;
124 linkShared->setProperty("CHARACTERISTIC_0", "new_CHARACTERISTIC_0_Value", propertySetFunction);
125 linkShared->setProperty("CHARACTERISTIC_1", "new_CHARACTERISTIC_1_Value", propertySetFunction);
127 linkShared->getProperty("CHARACTERISTIC_0", propertyGetFunction);
128 linkShared->getProperty("CHARACTERISTIC_1", propertyGetFunction);
136 Intel::CCFL::logDebug(TAG, "\t No devices found");
142 int android_jni_entry() {
143 #elif defined __linux__
147 Intel::CCFL::logDebug(TAG, "Starting CCFL Core Test");
149 // Create MockProtocols. Two are created to make sure that multiples are registered and unregistered correctly
150 std::shared_ptr<Intel::CCFL::Protocols::MockProtocol> mockProtocol = std::make_shared<Intel::CCFL::Protocols::MockProtocol>();
151 mockProtocol->setName("MockProtocol");
153 // Get a model from the CCFL API so that we can access the API interface
154 Intel::CCFL::API::Model::WeakPtr modelWeak = Intel::CCFL::API::Model::createModel();
155 Intel::CCFL::API::Model::SharedPtr modelShared = modelWeak.lock();
158 mockProtocol->setModel(modelShared);
160 // Register the MockProtocol
161 Intel::CCFL::Protocols::Protocol::Handle handle;
162 handle = modelShared->registerProtocol(mockProtocol);
163 Intel::CCFL::logDebug(TAG, "Protocol %s handle = %d", mockProtocol->getName().c_str(), (uint32_t)handle);
165 Intel::CCFL::API::GetDevicesFunction deviceFunc = getDevicesAsync;
166 modelShared->getDevices(deviceFunc);
168 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID0), "MockDevice0");
169 modelShared->getDevices(deviceFunc);
171 Intel::CCFL::API::DeviceEventFunction deviceEventLambda = [](const Intel::CCFL::API::DeviceEvent& result)->void {
172 Intel::CCFL::logDebug(TAG, "main::deviceEventAsyncLambda");
173 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
174 Intel::CCFL::API::DeviceObserverHandle handle = result.getObserverHandle();
175 Intel::CCFL::logDebug(TAG, "main::deviceEventAsyncLambda, observer handle = %ld", (uint32_t)handle);
176 std::list<Intel::CCFL::API::DeviceEvent::DeviceEventInfo> infoList = result.getDeviceIdList();
177 for (auto iter = infoList.begin(); iter != infoList.end(); ++iter) {
178 Intel::CCFL::API::DeviceEvent::DeviceEventInfo info = *iter;
179 Intel::CCFL::logDebug(TAG, "\t Device UUID = %s", to_string(info.deviceId).c_str());
180 switch (info.deviceChange) {
181 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_CURRENT_COLLECTION:
182 Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_CURRENT_COLLECTION");
184 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_ADDED:
185 Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_ADDED");
187 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_REMOVED:
188 Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_REMOVED");
191 Intel::CCFL::logDebug(TAG, "\t Event = unknown");
197 modelShared->setDeviceObserver(deviceEventLambda);
199 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID1), "MockDevice1");
200 modelShared->getDevices(deviceFunc);
201 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID2), "MockDevice2");
202 modelShared->getDevices(deviceFunc);
203 mockProtocol->testRemoveDevice(toUuid(MOCK_DEVICE_ID0));
204 modelShared->getDevices(deviceFunc);
206 Intel::CCFL::logDebug(TAG, "Sleep for 5 sec");
207 std::this_thread::sleep_for(std::chrono::seconds(5));
208 modelShared->unregisterProtocol(handle);
209 Intel::CCFL::logDebug(TAG, "Sleep for 5 sec");
213 Intel::CCFL::logDebug(TAG, "Exiting CCFL Core Test");