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_URI_LENGTH (64)
39 #define MAX_PERMISSION_LENGTH (5)
46 #define PREDEFINED_TIMEOUT (10)
47 #define MAX_OWNED_DEVICE (10)
48 #define TAG "provisioningclient"
50 #define JSON_DB_PATH "./oic_svr_db_client.json"
51 #define DAT_DB_PATH "./oic_svr_db_client.dat"
52 #define DEV_STATUS_ON "DEV_STATUS_ON"
53 #define DEV_STATUS_OFF "DEV_STATUS_OFF"
55 #define DP_DISCOVERY_TIMEOUT 4
56 #define DP_PIN_LENGTH 8
58 #define BOLD_BEGIN "\033[1m"
59 #define RED_BEGIN "\033[1;31m"
60 #define GREEN_BEGIN "\033[1;92m"
61 #define COLOR_END "\033[0m"
66 static PairedDevices discoveredDeviceList, pairedDeviceList;
68 static FILE* client_open(const char* /*fileName*/, const char *mode)
70 return fopen(DAT_DB_PATH, mode);
74 static void printMenu()
76 std::cout << GREEN_BEGIN "Choose an option:" COLOR_END<<std::endl;
77 std::cout << GREEN_BEGIN "# 1 (DP device discovery) : discover Direct-Pairing devices"
79 std::cout << GREEN_BEGIN "# 2 (start Direct-Pairing) : negotiate DP method & start Direct-Pairin"
81 std::cout << GREEN_BEGIN "# 3 (list all device) : list all discovered/paired devices"
83 std::cout << GREEN_BEGIN "# 4 (send data) : send data to device" COLOR_END<<std::endl;
84 std::cout << GREEN_BEGIN "# 9 (quit) : quit test " COLOR_END<<std::endl;
87 static void printPrompt()
89 std::cout << BOLD_BEGIN "IoTivity-DP#" COLOR_END" ";
92 static void printDevices(PairedDevices& list)
94 for (size_t i = 0; i < list.size(); i++)
96 std::cout << "["<< i+1 << "]" << " ID: " << list[i]->getDeviceID() << std::endl;
100 static void findCallback(PairedDevices discoveredDevList)
103 if (0 == discoveredDevList.size())
105 std::cout<< "No Direct-pairing Support device Found" << std::endl;
109 std::cout << "Discovered Direct-Pairing Support Device"<< std::endl;
110 discoveredDeviceList = discoveredDevList;
111 printDevices(discoveredDevList);
119 static bool printPairingMethod(int choice)
121 if (NULL == discoveredDeviceList[choice])// || false == discoveredDeviceList[choice]->edp)
123 std::cout<< "Invalid device or Not support direct-pairing..\n\n" << std::endl;
127 auto prms = discoveredDeviceList[choice]->getPairingMethods();
128 if (0 == prms.size())
130 std::cout << "Not exist any support method..\n\n" << std::endl;
135 std::cout << "\n* List of supported pairing methods *" << std::endl;
137 for (unsigned int i = 0; i < prms.size(); i++)
139 std::cout<< "[" << i+1 << "]";
142 case DP_PRE_CONFIGURED:
143 std::cout<<"Pre-Configured PIN"<<std::endl;;
146 std::cout<<"Random PIN"<<std::endl;;
149 std::cout<<"NOT Allowed ("<< prms[i]<<")"<<std::endl;
153 std::cout<<std::endl;
159 static void resultCallback(std::shared_ptr<OCDirectPairing> ptr, OCStackResult result)
162 if (OC_STACK_OK == result)
164 std::cout << " Direct-Pairing SUCCESS" << std::endl;
165 std::cout << "Taget Add info:" << ptr->getHost() << std::endl;
168 std::cout <<" Direct-Pairing FAILED" << std::endl;
176 static void pairedDevListCB(PairedDevices pairedDevList)
179 if (0 == pairedDevList.size())
181 std::cout << "No Paired Devcie Found" << std::endl;
185 pairedDeviceList = pairedDevList;
186 printDevices(pairedDevList);
194 static void getCallback(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
196 (void)(headerOptions);
199 if (OC_STACK_OK == eCode)
201 std::cout << "Callback Context for GET query recvd successfully" << std::endl;
202 std::cout << "Resource URI: " << rep.getUri() << std::endl;
206 rep.getValue("state", state);
207 rep.getValue("power", power);
209 std::cout << "\tstate: " << state << std::endl;
210 std::cout << "\tpower: " << power << std::endl;
214 std::cout << "getCallback Response error: " << eCode << std::endl;
217 catch(std::exception& e)
219 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
223 static bool InputPIN(std::string& pin)
225 std::cout <<" > Enter PIN Number for authentication (ex - '00000000' [8 digit] ):" ;
229 if (pin.size() != DP_PIN_LENGTH)
231 std::cout<<"Invalid PIN"<<std::endl;
240 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
242 // Create PlatformConfig object
244 OC::ServiceType::InProc,
248 OC::QualityOfService::LowQos,
252 OCPlatform::Configure(cfg);
257 for (int out = 0; !out;)
270 OCStackResult result = OC::OCPlatform::findDirectPairingDevices(
271 DP_DISCOVERY_TIMEOUT, findCallback);
273 if (OC_STACK_NO_RESOURCE == result)
275 std::cout << "!! No Direct-Pairing Support Device found"<<std::endl;
279 if (OC_STACK_OK != result)
281 std::cout << "!!Error - findDirectPairingDevices failed."<<std::endl;
288 unsigned int pMethodIDx = -1;
291 std::cout << "- Negotiate DP method & Start Direct-Pairing - ";
292 std::cout << "* List of discovered device" << std::endl;
293 printDevices(discoveredDeviceList);
294 std::cout << " > Enter Peer Device Number to initiate Direct-Pairing:" << std::endl;
297 if (choice < 1 || choice > discoveredDeviceList.size())
299 std::cout << "!!Device Number is incorrect, Try Again" << std::endl;
302 OCPrm_t pmSel = DP_NOT_ALLOWED;
304 if (false == printPairingMethod(choice))
306 std::cout << "Target does not support the Direct-Pairing" << std::endl;
310 std::cout << " > Enter pairing method: "<< std::endl;
312 std::cin >> pMethodIDx;
313 auto prms = discoveredDeviceList[choice]->getPairingMethods();
314 if (0 >= pMethodIDx || prms.size() < pMethodIDx)
316 std::cout <<"Invalid mode selection" << std::endl;
320 pmSel = prms[pMethodIDx - 1];
321 if (false == InputPIN(pin))
326 OCStackResult result = OC::OCPlatform::doDirectPairing(discoveredDeviceList[choice], pmSel, pin, resultCallback);
328 if (OC_STACK_OK != result)
330 std::cout << "!!Error - doDirectPairing failed." << std::endl;
337 std::cout << "- List all discovered and paired devices) -";
338 std::cout << " > List of discovered devices" << std::endl;
339 printDevices(discoveredDeviceList);
340 std::cout << std::endl;
342 std::cout << " > List of paired devices" << std::endl;
343 OCStackResult result = OC::OCPlatform::getDirectPairedDevices(pairedDevListCB);
344 printDevices(pairedDeviceList);
345 std::cout << std::endl;
347 if (OC_STACK_NO_RESOURCE == result)
349 std::cout << "!! No Paired device found"<<std::endl;
352 if (OC_STACK_OK != result)
354 std::cout << "!!Error - getDirectPairedDevices failed."<<std::endl;
360 std::cout << "- Send data(GET Request) to device(led server) -" << std::endl;
361 printDevices(pairedDeviceList);
362 pairedDeviceList = discoveredDeviceList;
364 std::cout << "Enter device number to GET data: ";
368 std::vector<std::string> ledTypes = {"core.led"};
369 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
371 OCConnectivityType ct = pairedDeviceList[choice]->getConnType();
373 std::cout << "\n\n HOST address is : " << pairedDeviceList[choice]->getHost() << "\n\n";
374 OCResource::Ptr led = OC::OCPlatform::constructResourceObject(
375 pairedDeviceList[choice]->getHost(),
376 "/a/led", ct, false, ledTypes, ifaces);
380 std::cout << "Error: Led Object construction returned null" << std::endl;
383 OCStackResult res = led->get(QueryParamsMap(), getCallback);
385 if (OC_STACK_OK != res)
387 std::cout << "Error: get Failed for Led" << std::endl;
398 std::cout << GREEN_BEGIN "Wrong Option : Try Again" COLOR_END << std::endl;
406 catch(OCException& e)
408 oclog() << "Exception in main: "<< e.what();