1 /* *****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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 * *****************************************************************/
29 #include <condition_variable>
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "OCPlatform.h"
36 #include "OCProvisioningManager.hpp"
37 #include "oxmjustworks.h"
38 #include "oxmrandompin.h"
39 #include "aclresource.h"
42 #define MAX_PERMISSION_LENGTH (5)
43 #define ACL_RESRC_ARRAY_SIZE (3)
50 #define PREDEFINED_TIMEOUT (10)
51 #define MAX_OWNED_DEVICE (10)
52 #define TAG "subownerclient"
54 #define JSON_DB_PATH "./oic_svr_db_subowner_client.json"
55 #define DAT_DB_PATH "./oic_svr_db_subowner_client.dat"
56 #define DEV_STATUS_ON "DEV_STATUS_ON"
57 #define DEV_STATUS_OFF "DEV_STATUS_OFF"
59 #define DISCOVERY_TIMEOUT 5
63 DeviceList_t pMOwnedDeviceList, pMOTEnabledDeviceList;
64 static int transferDevIdx, ask = 1;
66 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
69 return fopen(DAT_DB_PATH, mode);
74 std::cout << "\nChoose an option:"<<std::endl;
75 std::cout << " 1. Discover Multiple Ownership Transfer Enabled Devices"<<std::endl;
76 std::cout << " 2. Discover Multiple Owned Devices"<<std::endl;
77 std::cout << " 3. Perform the Multiple Ownership Transfer"<<std::endl;
78 std::cout << " 4. Get LED resource"<<std::endl;
79 std::cout << " 5. Get PUT resource"<<std::endl;
80 std::cout << " 99. Exit loop"<<std::endl;
83 void moveTransferredDevice()
85 pMOwnedDeviceList.push_back(pMOTEnabledDeviceList[transferDevIdx]);
86 pMOTEnabledDeviceList.erase(pMOTEnabledDeviceList.begin() + transferDevIdx);
89 void InputPinCB(char* pinBuf, size_t bufSize)
93 std::cout <<"INPUT PIN : ";
96 OICStrcpy(pinBuf, bufSize, ptr.c_str());
101 void printUuid(OicUuid_t uuid)
103 for (int i = 0; i < UUID_LENGTH; i++)
105 std::cout <<std::hex << uuid.id[i] << " ";
107 std::cout<<std::endl;
110 void multipleOwnershipTransferCB(PMResultList_t *result, int hasError)
114 std::cout << "Error!!! in MultipleOwnershipTransfer"<<std::endl;
118 std::cout<< "\nTransferred MultipleOwnership successfuly for device : ";
119 printUuid(result->at(0).deviceId);
122 moveTransferredDevice();
127 void printStatus(int status)
129 static std::map<int, std::string> devStatus = {{1<<0, DEV_STATUS_ON}, {1<<1, DEV_STATUS_OFF}};
131 std::cout <<devStatus[status] <<std::endl;
134 void printDevices(DeviceList_t &list)
136 for (unsigned int i = 0; i < list.size(); i++ )
138 std::cout << "Device "<< i+1 <<" ID: ";
139 std::cout << list[i]->getDeviceID() << " From IP: ";
140 std::cout << list[i]->getDevAddr() << std::endl;
144 static void getCallback(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
146 (void)(headerOptions);
149 if (OC_STACK_OK == eCode)
151 std::cout << "Callback Context for GET query recvd successfully" << std::endl;
152 std::cout << "Resource URI: " << rep.getUri() << std::endl;
156 rep.getValue("state", state);
157 rep.getValue("power", power);
159 std::cout << "\tstate: " << state << std::endl;
160 std::cout << "\tpower: " << power << std::endl;
164 std::cout << "getCallback Response error: " << eCode << std::endl;
167 catch(std::exception& e)
169 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
173 void putCallback(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
177 std::cout << "PUT request was successful !!!!!!" << std::endl;
181 std::cout << "onPut Response error !!!!!: " << eCode << std::endl;
187 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
189 // Create PlatformConfig object
191 OC::ServiceType::InProc,
195 OC::QualityOfService::LowQos,
199 OCPlatform::Configure(cfg);
201 //set Input Pin callback
202 OCSecure::setInputPinCallback(InputPinCB);
207 OicSecAcl_t *acl1 = nullptr, *acl2 = nullptr;
208 if (OCSecure::provisionInit("subowner_pdm.db") != OC_STACK_OK)
210 std::cout <<"PM Init failed"<< std::endl;
214 for (int out = 0; !out;)
226 pMOTEnabledDeviceList.clear();
227 std::cout << "Started MOT enabled device discovery..." <<std::endl;
228 OCStackResult result = OCSecure::discoverMultipleOwnerEnabledDevices(
229 DISCOVERY_TIMEOUT, pMOTEnabledDeviceList);
230 if (result != OC_STACK_OK)
232 std::cout<< "!!Error - MOT enabled dev Discovery failed."<<std::endl;
234 else if (pMOTEnabledDeviceList.size())
236 std::cout <<"Found MOT enabled devices, count = " <<
237 pMOTEnabledDeviceList.size() << std::endl;
238 printDevices(pMOTEnabledDeviceList);
242 std::cout <<"NO MOT enabled devices found"<<std::endl;
248 pMOwnedDeviceList.clear();
249 std::cout << "Started Multiple Owned device discovery..." <<std::endl;
250 OCStackResult result = OCSecure::discoverMultipleOwnerEnabledDevices(
251 DISCOVERY_TIMEOUT, pMOwnedDeviceList);
252 if (result != OC_STACK_OK)
254 std::cout<< "!!Error - Multiple Owned dev Discovery failed."<<std::endl;
256 else if (pMOwnedDeviceList.size())
258 std::cout <<"Found Multiple Owned devices, count = " <<
259 pMOwnedDeviceList.size() << std::endl;
260 printDevices(pMOwnedDeviceList);
264 std::cout <<"NO Multiple Owned devices found"<<std::endl;
269 { //Multiple Ownrship Trasfer
272 if (!pMOTEnabledDeviceList.size())
274 std::cout <<"There are no MOT enabled devices"<<std::endl;
278 for (unsigned int i = 0; i < pMOTEnabledDeviceList.size(); i++ )
280 std::cout << i+1 << ": "<< pMOTEnabledDeviceList[i]->getDeviceID();
281 std::cout << " From IP:" << pMOTEnabledDeviceList[i]->getDevAddr() <<std::endl;
284 std::cout <<"Select device number: "<<std::endl;
286 if (devNum > pMOTEnabledDeviceList.size())
288 std::cout <<"Invalid device number"<<std::endl;
291 transferDevIdx = devNum - 1;
294 std::cout << "MOT for : "<<
295 pMOTEnabledDeviceList[devNum-1]->getDeviceID()<<std::endl;
296 if (pMOTEnabledDeviceList[devNum-1]->doMultipleOwnershipTransfer
297 (multipleOwnershipTransferCB)
300 std::cout<<"Multiple OwnershipTransferCallback is failed"<<std::endl;
307 std::cout << "- Send data(GET Request) to device(led server) -" << std::endl;
308 printDevices(pMOwnedDeviceList);
310 std::cout << "Enter device number to GET data: ";
314 std::vector<std::string> ledTypes = {"core.led"};
315 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
317 OCProvisionDev_t *selDev = pMOwnedDeviceList[choice]->getDevPtr();
318 std::stringstream host;
320 host << pMOwnedDeviceList[choice]->getDevAddr();
321 host << selDev->securePort;
323 OCResource::Ptr led = OC::OCPlatform::constructResourceObject(
324 host.str(), "/a/led", selDev->connType, false, ledTypes, ifaces);
328 std::cout << "Error: Led Object construction returned null" << std::endl;
331 OCStackResult res = led->get(QueryParamsMap(), getCallback);
333 if (OC_STACK_OK != res)
335 std::cout << "Error: get Failed for Led" << std::endl;
342 std::cout << "- Send data(Put Request) to device(led server) -" << std::endl;
343 printDevices(pMOwnedDeviceList);
345 std::cout << "Enter device number to GET data: ";
349 std::vector<std::string> ledTypes = {"core.led"};
350 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
353 OCProvisionDev_t *selDev = pMOwnedDeviceList[choice]->getDevPtr();
354 std::stringstream host;
356 host << pMOwnedDeviceList[choice]->getDevAddr();
357 host << selDev->securePort;
359 OCResource::Ptr led = OC::OCPlatform::constructResourceObject(host.str(),
360 "/a/led", selDev->connType, false, ledTypes, ifaces);
364 std::cout << "Error: Led Object construction returned null" << std::endl;
367 OCRepresentation rep;
370 rep.setValue("state", state);
371 rep.setValue("power", power);
372 OCStackResult res = led->put(rep, QueryParamsMap(), putCallback);
374 if (OC_STACK_OK != res)
376 std::cout << "Error: put Failed for Led" << std::endl;
388 catch(OCException& e)
390 oclog() << "Exception in main: "<<e.what();