Merge "Partial Implementation of US1574:"
[platform/upstream/iotivity.git] / csdk / controller / core / test / CCFL-CoreTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 //#include <iostream>
22 #include <string.h>
23 #include <stdint.h>
24
25 #ifdef __linux__
26         #include <thread>
27         #include <pthread.h>
28         #include <chrono>
29 #endif
30
31
32 #include <functional>
33 #include "InternalApi.h"
34 #include "SimpleLogger.h"
35 #include "MockProtocol.h"
36 #include "Core.h"
37 #include "Description.hpp"
38
39 #include <boost/uuid/uuid.hpp>
40 #include <boost/uuid/uuid_generators.hpp>
41 #include <boost/uuid/uuid_io.hpp>
42
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";
46
47 static const char TAG[] = "CCFL-CoreTest.cpp";
48
49 UUID_t toUuid(const std::string& uuidStr) {
50         boost::uuids::string_generator gen;
51         return gen(uuidStr);
52 }
53
54 using namespace std;
55
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());
63          }
64         Intel::CCFL::logDebug(TAG, "\t********leaving main::setPropertyAsync********");
65 }
66
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());
74         }
75         Intel::CCFL::logDebug(TAG, "\t********leaving main::getPropertyAsync********");
76 }
77
78 void getDescriptionAsync(const Intel::CCFL::API::DescriptionGetResult& result) {
79         Intel::CCFL::logDebug(TAG, "\t********entering main::getDescriptionAsync********");
80
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();
84
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();
88                 if (service) {
89                         Intel::CCFL::logDebug(TAG, "\t service = %s", service->getName().c_str());
90
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();
95                                 if (characteristic) {
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());
100                                 }
101                         }
102                 }
103         }
104   }
105   Intel::CCFL::logDebug(TAG, "\t********leaving main::getDescriptionAsync********");
106 }
107
108 void getDevicesAsync(const Intel::CCFL::API::GetDevicesResult& result) {
109         Intel::CCFL::logDebug(TAG, "Entering main::getDevicesAsync");
110
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();
119                                 if (device) {
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());
123
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();
130                                                         if (linkShared) {
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);
137
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);
141
142                                                                 linkShared->getProperty("CHARACTERISTIC_0", propertyGetFunction);
143                                                                 linkShared->getProperty("CHARACTERISTIC_1", propertyGetFunction);
144                                                         }
145                                                 }
146                                         }
147                                 }
148                         }
149         }
150         else {
151                 Intel::CCFL::logDebug(TAG, "\t No devices found");
152         }
153   }
154 }
155
156 #ifdef __ANDROID__
157         int android_jni_entry() {
158 #elif defined __linux__
159                 int main() {
160 #endif
161
162         Intel::CCFL::logDebug(TAG, "Starting CCFL Core Test");
163
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");
167
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();
171         if (modelShared) {
172
173                 mockProtocol->setModel(modelShared);
174
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);
179
180                 Intel::CCFL::API::GetDevicesFunction deviceFunc = getDevicesAsync;
181                 modelShared->getDevices(deviceFunc);
182
183                 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID0), "MockDevice0");
184                 modelShared->getDevices(deviceFunc);
185
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");
198                                                         break;
199                                                 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_ADDED:
200                                                         Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_ADDED");
201                                                         break;
202                                                 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_REMOVED:
203                                                         Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_REMOVED");
204                                                         break;
205                                                 default:
206                                                         Intel::CCFL::logDebug(TAG, "\t Event = unknown");
207                                                         break;
208                                         }
209                                 }
210                         }
211                 };
212                 modelShared->setDeviceObserver(deviceEventLambda);
213
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);
220
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");
225         }
226
227
228         Intel::CCFL::logDebug(TAG, "Exiting CCFL Core Test");
229         return 0;
230 }