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 * *****************************************************************/
30 #include <condition_variable>
33 #include "oic_malloc.h"
34 #include "oic_string.h"
35 #include "OCPlatform.h"
38 #define MAX_PERMISSION_LENGTH (5)
45 #define PREDEFINED_TIMEOUT (10)
46 #define MAX_OWNED_DEVICE (10)
47 #define TAG "provisioningclient"
49 #define JSON_DB_PATH "./oic_svr_db_client.json"
50 #define DAT_DB_PATH "./oic_svr_db_client.dat"
51 #define DEV_STATUS_ON "DEV_STATUS_ON"
52 #define DEV_STATUS_OFF "DEV_STATUS_OFF"
54 #define DP_DISCOVERY_TIMEOUT 4
55 #define DP_PIN_LENGTH 8
57 #define BOLD_BEGIN "\033[1m"
58 #define RED_BEGIN "\033[1;31m"
59 #define GREEN_BEGIN "\033[1;92m"
60 #define COLOR_END "\033[0m"
65 static PairedDevices discoveredDeviceList, pairedDeviceList;
67 static FILE* client_open(const char* /*fileName*/, const char *mode)
69 return fopen(DAT_DB_PATH, mode);
73 static void printMenu()
75 std::cout << GREEN_BEGIN "Choose an option:" COLOR_END<<std::endl;
76 std::cout << GREEN_BEGIN "# 1 (DP device discovery) : discover Direct-Pairing devices"
78 std::cout << GREEN_BEGIN "# 2 (start Direct-Pairing) : negotiate DP method & start Direct-Pairin"
80 std::cout << GREEN_BEGIN "# 3 (list all device) : list all discovered/paired devices"
82 std::cout << GREEN_BEGIN "# 4 (send data) : send data to device" COLOR_END<<std::endl;
83 std::cout << GREEN_BEGIN "# 9 (quit) : quit test " COLOR_END<<std::endl;
86 static void printPrompt()
88 std::cout << BOLD_BEGIN "IoTivity-DP#" COLOR_END" ";
91 static void printDevices(PairedDevices& list)
93 for (size_t i = 0; i < list.size(); i++)
95 std::cout << "["<< i+1 << "]" << " ID: " << list[i]->getDeviceID() << std::endl;
99 static void findCallback(PairedDevices discoveredDevList)
102 if (0 == discoveredDevList.size())
104 std::cout<< "No Direct-pairing Support device Found" << std::endl;
108 std::cout << "Discovered Direct-Pairing Support Device"<< std::endl;
109 discoveredDeviceList = discoveredDevList;
110 printDevices(discoveredDevList);
118 static bool printPairingMethod(int choice)
120 if (NULL == discoveredDeviceList[choice])// || false == discoveredDeviceList[choice]->edp)
122 std::cout<< "Invalid device or Not support direct-pairing..\n\n" << std::endl;
126 auto prms = discoveredDeviceList[choice]->getPairingMethods();
127 if (0 == prms.size())
129 std::cout << "Not exist any support method..\n\n" << std::endl;
134 std::cout << "\n* List of supported pairing methods *" << std::endl;
136 for (unsigned int i = 0; i < prms.size(); i++)
138 std::cout<< "[" << i+1 << "]";
141 case DP_PRE_CONFIGURED:
142 std::cout<<"Pre-Configured PIN"<<std::endl;;
145 std::cout<<"Random PIN"<<std::endl;;
148 std::cout<<"NOT Allowed ("<< prms[i]<<")"<<std::endl;
152 std::cout<<std::endl;
158 static void resultCallback(std::shared_ptr<OCDirectPairing> ptr, OCStackResult result)
161 if (OC_STACK_OK == result)
163 std::cout << " Direct-Pairing SUCCESS" << std::endl;
164 std::cout << "Taget Add info:" << ptr->getHost() << std::endl;
167 std::cout <<" Direct-Pairing FAILED" << std::endl;
175 static void pairedDevListCB(PairedDevices pairedDevList)
178 if (0 == pairedDevList.size())
180 std::cout << "No Paired Devcie Found" << std::endl;
184 pairedDeviceList = pairedDevList;
185 printDevices(pairedDevList);
193 static void getCallback(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
195 (void)(headerOptions);
198 if (OC_STACK_OK == eCode)
200 std::cout << "Callback Context for GET query recvd successfully" << std::endl;
201 std::cout << "Resource URI: " << rep.getUri() << std::endl;
205 rep.getValue("state", state);
206 rep.getValue("power", power);
208 std::cout << "\tstate: " << state << std::endl;
209 std::cout << "\tpower: " << power << std::endl;
213 std::cout << "getCallback Response error: " << eCode << std::endl;
216 catch(std::exception& e)
218 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
222 static bool InputPIN(std::string& pin)
224 std::cout <<" > Enter PIN Number for authentication (ex - '00000000' [8 digit] ):" ;
228 if (pin.size() != DP_PIN_LENGTH)
230 std::cout<<"Invalid PIN"<<std::endl;
239 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
241 // Create PlatformConfig object
243 OC::ServiceType::InProc,
247 OC::QualityOfService::LowQos,
251 OCPlatform::Configure(cfg);
256 for (int out = 0; !out;)
269 OCStackResult result = OC::OCPlatform::findDirectPairingDevices(
270 DP_DISCOVERY_TIMEOUT, findCallback);
272 if (OC_STACK_NO_RESOURCE == result)
274 std::cout << "!! No Direct-Pairing Support Device found"<<std::endl;
278 if (OC_STACK_OK != result)
280 std::cout << "!!Error - findDirectPairingDevices failed."<<std::endl;
287 unsigned int pMethodIDx = -1;
290 std::cout << "- Negotiate DP method & Start Direct-Pairing - ";
291 std::cout << "* List of discovered device" << std::endl;
292 printDevices(discoveredDeviceList);
293 std::cout << " > Enter Peer Device Number to initiate Direct-Pairing:" << std::endl;
296 if (choice < 1 || choice > discoveredDeviceList.size())
298 std::cout << "!!Device Number is incorrect, Try Again" << std::endl;
301 OCPrm_t pmSel = DP_NOT_ALLOWED;
303 if (false == printPairingMethod(choice))
305 std::cout << "Target does not support the Direct-Pairing" << std::endl;
309 std::cout << " > Enter pairing method: "<< std::endl;
311 std::cin >> pMethodIDx;
312 auto prms = discoveredDeviceList[choice]->getPairingMethods();
313 if (0 >= pMethodIDx || prms.size() < pMethodIDx)
315 std::cout <<"Invalid mode selection" << std::endl;
319 pmSel = prms[pMethodIDx - 1];
320 if (false == InputPIN(pin))
325 OCStackResult result = OC::OCPlatform::doDirectPairing(discoveredDeviceList[choice], pmSel, pin, resultCallback);
327 if (OC_STACK_OK != result)
329 std::cout << "!!Error - doDirectPairing failed." << std::endl;
336 std::cout << "- List all discovered and paired devices) -";
337 std::cout << " > List of discovered devices" << std::endl;
338 printDevices(discoveredDeviceList);
339 std::cout << std::endl;
341 std::cout << " > List of paired devices" << std::endl;
342 OCStackResult result = OC::OCPlatform::getDirectPairedDevices(pairedDevListCB);
343 printDevices(pairedDeviceList);
344 std::cout << std::endl;
346 if (OC_STACK_NO_RESOURCE == result)
348 std::cout << "!! No Paired device found"<<std::endl;
351 if (OC_STACK_OK != result)
353 std::cout << "!!Error - getDirectPairedDevices failed."<<std::endl;
359 std::cout << "- Send data(GET Request) to device(led server) -" << std::endl;
360 printDevices(pairedDeviceList);
361 pairedDeviceList = discoveredDeviceList;
363 std::cout << "Enter device number to GET data: ";
367 std::vector<std::string> ledTypes = {"core.led"};
368 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
370 OCConnectivityType ct = pairedDeviceList[choice]->getConnType();
372 std::cout << "\n\n HOST address is : " << pairedDeviceList[choice]->getHost() << "\n\n";
373 OCResource::Ptr led = OC::OCPlatform::constructResourceObject(
374 pairedDeviceList[choice]->getHost(),
375 "/a/led", ct, false, ledTypes, ifaces);
379 std::cout << "Error: Led Object construction returned null" << std::endl;
382 OCStackResult res = led->get(QueryParamsMap(), getCallback);
384 if (OC_STACK_OK != res)
386 std::cout << "Error: get Failed for Led" << std::endl;
397 std::cout << GREEN_BEGIN "Wrong Option : Try Again" COLOR_END << std::endl;
405 catch(OCException& e)
407 oclog() << "Exception in main: "<< e.what();