Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / controller / core / test / CCFL-CoreTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 //#include <iostream>
7 #include <string.h>
8 #include <stdint.h>
9
10 #ifdef __linux__
11         #include <thread>
12         #include <pthread.h>
13         #include <chrono>
14 #endif
15
16
17 #include <functional>
18 #include "InternalApi.h"
19 #include "SimpleLogger.h"
20 #include "MockProtocol.h"
21 #include "Core.h"
22 #include "Description.hpp"
23
24 #include <boost/uuid/uuid.hpp>
25 #include <boost/uuid/uuid_generators.hpp>
26 #include <boost/uuid/uuid_io.hpp>
27
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";
31
32 static const char TAG[] = "CCFL-CoreTest.cpp";
33
34 UUID_t toUuid(const std::string& uuidStr) {
35         boost::uuids::string_generator gen;
36         return gen(uuidStr);
37 }
38
39 using namespace std;
40
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());
48          }
49         Intel::CCFL::logDebug(TAG, "\t********leaving main::setPropertyAsync********");
50 }
51
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());
59         }
60         Intel::CCFL::logDebug(TAG, "\t********leaving main::getPropertyAsync********");
61 }
62
63 void getDescriptionAsync(const Intel::CCFL::API::DescriptionGetResult& result) {
64         Intel::CCFL::logDebug(TAG, "\t********entering main::getDescriptionAsync********");
65
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();
69
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();
73                 if (service) {
74                         Intel::CCFL::logDebug(TAG, "\t service = %s", service->getName().c_str());
75
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();
80                                 if (characteristic) {
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());
85                                 }
86                         }
87                 }
88         }
89   }
90   Intel::CCFL::logDebug(TAG, "\t********leaving main::getDescriptionAsync********");
91 }
92
93 void getDevicesAsync(const Intel::CCFL::API::GetDevicesResult& result) {
94         Intel::CCFL::logDebug(TAG, "Entering main::getDevicesAsync");
95
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();
104                                 if (device) {
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());
108
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();
115                                                         if (linkShared) {
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);
122
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);
126
127                                                                 linkShared->getProperty("CHARACTERISTIC_0", propertyGetFunction);
128                                                                 linkShared->getProperty("CHARACTERISTIC_1", propertyGetFunction);
129                                                         }
130                                                 }
131                                         }
132                                 }
133                         }
134         }
135         else {
136                 Intel::CCFL::logDebug(TAG, "\t No devices found");
137         }
138   }
139 }
140
141 #ifdef __ANDROID__
142         int android_jni_entry() {
143 #elif defined __linux__
144                 int main() {
145 #endif
146
147         Intel::CCFL::logDebug(TAG, "Starting CCFL Core Test");
148
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");
152
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();
156         if (modelShared) {
157
158                 mockProtocol->setModel(modelShared);
159
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);
164
165                 Intel::CCFL::API::GetDevicesFunction deviceFunc = getDevicesAsync;
166                 modelShared->getDevices(deviceFunc);
167
168                 mockProtocol->testAddDevice(toUuid(MOCK_DEVICE_ID0), "MockDevice0");
169                 modelShared->getDevices(deviceFunc);
170
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");
183                                                         break;
184                                                 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_ADDED:
185                                                         Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_ADDED");
186                                                         break;
187                                                 case Intel::CCFL::API::DeviceEvent::DeviceChange::DEVICE_REMOVED:
188                                                         Intel::CCFL::logDebug(TAG, "\t Event = DEVICE_REMOVED");
189                                                         break;
190                                                 default:
191                                                         Intel::CCFL::logDebug(TAG, "\t Event = unknown");
192                                                         break;
193                                         }
194                                 }
195                         }
196                 };
197                 modelShared->setDeviceObserver(deviceEventLambda);
198
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);
205
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");
210         }
211
212
213         Intel::CCFL::logDebug(TAG, "Exiting CCFL Core Test");
214         return 0;
215 }