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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
33 #include "InternalApi.h"
34 #include "SimpleLogger.h"
35 #include "MockProtocol.h"
37 #include "Description.hpp"
39 #include <boost/uuid/uuid.hpp>
40 #include <boost/uuid/uuid_generators.hpp>
41 #include <boost/uuid/uuid_io.hpp>
43 const std::string MOCK_DEVICE_ID0 = "7c34ad16-ae8c-415b-94cc-d8053f4f9f8e";
44 const std::string MOCK_DEVICE_ID1 = "646344d0-271c-49dd-8b13-3a7bec1237bd";
45 const std::string MOCK_DEVICE_ID2 = "74c406ca-1cfb-4106-a82f-6123e3fe3555";
47 static const char TAG[] = "CCFL-CoreTest.cpp";
49 UUID_t toUuid(const std::string& uuidStr) {
50 boost::uuids::string_generator gen;
56 void setPropertyAsync(const Intel::CCFL::API::PropertySetResult& result) {
57 Intel::CCFL::logDebug(TAG, "\t********entering main::setPropertyAsync********");
58 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
59 Intel::CCFL::logDebug(TAG, "\t QueryResultType::SUCCESS");
60 Intel::CCFL::logDebug(TAG, "\t property:");
61 Intel::CCFL::logDebug(TAG, "\t\t name = %s", result.getName().c_str());
62 Intel::CCFL::logDebug(TAG, "\t\t value = %s", result.getValue().c_str());
64 Intel::CCFL::logDebug(TAG, "\t********leaving main::setPropertyAsync********");
67 void getPropertyAsync(const Intel::CCFL::API::PropertyGetResult& result) {
68 Intel::CCFL::logDebug(TAG, "\t********entering main::getPropertyAsync********");
69 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
70 Intel::CCFL::logDebug(TAG, "\t QueryResultType::SUCCESS");
71 Intel::CCFL::logDebug(TAG, "\t property:");
72 Intel::CCFL::logDebug(TAG, "\t\t name = %s", result.getName().c_str());
73 Intel::CCFL::logDebug(TAG, "\t\t value = %s", result.getValue().c_str());
75 Intel::CCFL::logDebug(TAG, "\t********leaving main::getPropertyAsync********");
78 void getDescriptionAsync(const Intel::CCFL::API::DescriptionGetResult& result) {
79 Intel::CCFL::logDebug(TAG, "\t********entering main::getDescriptionAsync********");
81 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
82 Intel::CCFL::logDebug(TAG, "\t QueryResultType::SUCCESS");
83 std::set<Intel::CCFL::API::Service::SharedPtr> serviceSet = result.getServices();
85 for (auto iter = serviceSet.begin(); iter != serviceSet.end(); ++iter) {
86 Intel::CCFL::API::Service::WeakPtr serviceWeak = *iter;
87 Intel::CCFL::API::Service::SharedPtr service = serviceWeak.lock();
89 Intel::CCFL::logDebug(TAG, "\t service = %s", service->getName().c_str());
91 std::set<Intel::CCFL::API::Characteristic::SharedPtr> characteristicSet = service->getCharacteristics();
92 for (auto iter = characteristicSet.begin(); iter != characteristicSet.end(); ++iter) {
93 Intel::CCFL::API::Characteristic::WeakPtr characteristicWeak = *iter;
94 Intel::CCFL::API::Characteristic::SharedPtr characteristic = characteristicWeak.lock();
96 Intel::CCFL::logDebug(TAG, "\t\t characteristic = %s", characteristic->getName().c_str());
97 Intel::CCFL::logDebug(TAG, "\t\t readable = %d", characteristic->isReadable());
98 Intel::CCFL::logDebug(TAG, "\t\t writable = %d", characteristic->isWritable());
99 Intel::CCFL::logDebug(TAG, "\t\t constant = %d", characteristic->isConstant());
105 Intel::CCFL::logDebug(TAG, "\t********leaving main::getDescriptionAsync********");
108 void getDevicesAsync(const Intel::CCFL::API::GetDevicesResult& result) {
109 Intel::CCFL::logDebug(TAG, "Entering main::getDevicesAsync");
111 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
112 Intel::CCFL::logDebug(TAG, "main::getDevicesAsync, QueryResultType::SUCCESS");
113 std::list<Intel::CCFL::API::Device::SharedPtr> deviceList = result.getDeviceList();
114 // Search list for the device based on device id
115 if (deviceList.size() > 0) {
116 for (auto iter = deviceList.begin(); iter != deviceList.end(); ++iter) {
117 Intel::CCFL::API::Device::WeakPtr deviceWeak = *iter;
118 Intel::CCFL::API::Device::SharedPtr device = deviceWeak.lock();
120 Intel::CCFL::logDebug(TAG, "\tDevice:");
121 Intel::CCFL::logDebug(TAG, "\t name = %s", device->getName().c_str());
122 Intel::CCFL::logDebug(TAG, "\t UUID = %s", to_string(device->getId()).c_str());
124 // Test characteristic stuff
125 if (device->getName() == "MockDevice0") {
126 if (device->getLinkCount() > 0) {
127 Intel::CCFL::API::Device::LinkList linkList = device->getLinks();
128 Intel::CCFL::API::Link::WeakPtr linkWeak = linkList.front();
129 Intel::CCFL::API::Link::SharedPtr linkShared = linkWeak.lock();
131 // Test set/get properties
132 Intel::CCFL::API::DescriptionGetFunction descriptionGetFunction = getDescriptionAsync;
133 linkShared->getDescription(descriptionGetFunction);
134 Intel::CCFL::API::PropertyGetFunction propertyGetFunction = getPropertyAsync;
135 linkShared->getProperty("CHARACTERISTIC_0", propertyGetFunction);
136 linkShared->getProperty("CHARACTERISTIC_1", propertyGetFunction);
138 Intel::CCFL::API::PropertySetFunction propertySetFunction = setPropertyAsync;
139 linkShared->setProperty("CHARACTERISTIC_0", "new_CHARACTERISTIC_0_Value", propertySetFunction);
140 linkShared->setProperty("CHARACTERISTIC_1", "new_CHARACTERISTIC_1_Value", propertySetFunction);
142 linkShared->getProperty("CHARACTERISTIC_0", propertyGetFunction);
143 linkShared->getProperty("CHARACTERISTIC_1", propertyGetFunction);
151 Intel::CCFL::logDebug(TAG, "\t No devices found");
157 int android_jni_entry() {
158 #elif defined __linux__
162 Intel::CCFL::logDebug(TAG, "Starting CCFL Core Test");
164 // Create MockProtocols. Two are created to make sure that multiples are registered and unregistered correctly
165 std::shared_ptr<Intel::CCFL::Protocols::MockProtocol> mockProtocol = std::make_shared<Intel::CCFL::Protocols::MockProtocol>();
166 mockProtocol->setName("MockProtocol");
168 // Get a model from the CCFL API so that we can access the API interface
169 Intel::CCFL::API::Model::WeakPtr modelWeak = Intel::CCFL::API::Model::createModel();
170 Intel::CCFL::API::Model::SharedPtr modelShared = modelWeak.lock();
173 mockProtocol->setModel(modelShared);
175 // Register the MockProtocol
176 Intel::CCFL::Protocols::Protocol::Handle handle;
177 handle = modelShared->registerProtocol(mockProtocol);
178 Intel::CCFL::logDebug(TAG, "Protocol %s handle = %d", mockProtocol->getName().c_str(), (uint32_t)handle);
180 Intel::CCFL::API::GetDevicesFunction deviceFunc = getDevicesAsync;
181 modelShared->getDevices(deviceFunc);
183 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID0), "MockDevice0");
184 modelShared->getDevices(deviceFunc);
186 Intel::CCFL::API::DeviceEventFunction deviceEventLambda = [](const Intel::CCFL::API::DeviceEvent& result)->void {
187 Intel::CCFL::logDebug(TAG, "main::deviceEventAsyncLambda");
188 if (result.getResult() == Intel::CCFL::API::QueryResultType::SUCCESS) {
189 Intel::CCFL::API::DeviceObserverHandle handle = result.getObserverHandle();
190 Intel::CCFL::logDebug(TAG, "main::deviceEventAsyncLambda, observer handle = %ld", (uint32_t)handle);
191 std::list<Intel::CCFL::API::DeviceEvent::DeviceEventInfo> infoList = result.getDeviceIdList();
192 for (auto iter = infoList.begin(); iter != infoList.end(); ++iter) {
193 Intel::CCFL::API::DeviceEvent::DeviceEventInfo info = *iter;
194 Intel::CCFL::logDebug(TAG, "\t Device UUID = %s", to_string(info.deviceId).c_str());
195 switch (info.deviceChange) {
196 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_CURRENT_COLLECTION:
197 Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_CURRENT_COLLECTION");
199 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_ADDED:
200 Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_ADDED");
202 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_REMOVED:
203 Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_REMOVED");
206 Intel::CCFL::logDebug(TAG, "\t Event = unknown");
212 modelShared->setDeviceObserver(deviceEventLambda);
214 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID1), "MockDevice1");
215 modelShared->getDevices(deviceFunc);
216 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID2), "MockDevice2");
217 modelShared->getDevices(deviceFunc);
218 mockProtocol->testRemoveDevice(toUuid(MOCK_DEVICE_ID0));
219 modelShared->getDevices(deviceFunc);
221 Intel::CCFL::logDebug(TAG, "Sleep for 5 sec");
222 std::this_thread::sleep_for(std::chrono::seconds(5));
223 modelShared->unregisterProtocol(handle);
224 Intel::CCFL::logDebug(TAG, "Sleep for 5 sec");
228 Intel::CCFL::logDebug(TAG, "Exiting CCFL Core Test");