OCStackResult InProcClientWrapper::GetDirectPairedDevices(GetDirectPairedCallback& callback) { if (!callback) { return OC_STACK_INVALID_PARAM; } OCStackResult result = OC_STACK_ERROR; const OCDPDev_t *list = nullptr; PairedDevices dpDeviceList; auto cLock = m_csdkLock.lock(); if (cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); list = OCGetDirectPairedDevices(); if (NULL == list) { result = OC_STACK_NO_RESOURCE; oclog() << "findDirectPairingDevices(): No device found for direct pairing" << std::flush; } else { convert(list, dpDeviceList); std::thread exec(callback, dpDeviceList); exec.detach(); result = OC_STACK_OK; } } else { result = OC_STACK_ERROR; } return result; }
void *CLInterface(void *data) { printf(RED_BEGIN"#Ready to operation ('h' for help)#\n"COLOR_END); (void)data; OCStackResult ret; char query[MAX_LINE] = {0,}; const char prompt[] = BOLD_BEGIN"IoTivity-DP#"COLOR_END" "; const char* helpmsg[6] = { GREEN_BEGIN"# h (or help) : show help message"COLOR_END, GREEN_BEGIN"# dd (DP device discovery) : discover Direct-Pairing devices"COLOR_END, GREEN_BEGIN"# dp (start Direct-Pairing) : negotiate DP method & start Direct-Pairing"COLOR_END, GREEN_BEGIN"# sd (send data) : send data to device"COLOR_END, GREEN_BEGIN"# ll (list all device) : list all discovered/paired devices"COLOR_END, GREEN_BEGIN"# q (quit) : quit test"COLOR_END, }; for (size_t i=0; i<(sizeof(helpmsg)/sizeof(char*)); i++) { fprintf(stderr, "%s\n", helpmsg[i]); } printf("\n"); // cli for (;;) { const char *input = readline(prompt, NULL); if (!input) { continue; } strncpy(query, input, MAX_LINE); if (!strlen(query)) { continue; } else if (!strcmp(query, "h") || !strcmp(query, "help")) { for (size_t i=0; i<(sizeof(helpmsg)/sizeof(char*)); i++) { fprintf(stderr, "%s\n", helpmsg[i]); } continue; } else { if (!strcmp(query, "dd")) { OIC_LOG(INFO, TAG, "- Direct-Pairing device discovery -"); ret = DirectPairingDiscovery(); if (OC_STACK_OK != ret) { OIC_LOG(ERROR, TAG, "Error in DirectPairingDiscovery()"); } } else if (!strcmp(query, "dp")) { OIC_LOG(INFO, TAG, "- Negotiate DP method & Start Direct-Pairing -"); printf("\n * List of discovered device\n"); printList(discoveredDevs); // target peer OCDPDev_t *peer = NULL; long peerIdx; input = readline(" > Enter Peer Device Number to initiate Direct-Pairing: ", NULL); if (!input || !strlen(input)) { continue; } char *ptr; peerIdx = strtol(input, &ptr, 10); peer = getDev(discoveredDevs, (uint32_t)peerIdx); if (NULL == peer) { OIC_LOG(ERROR, TAG, "Not found the peer in discovered list"); continue; } // get pairing method long pmIdx; OCPrm_t pmSel = DP_NOT_ALLOWED; if (false == printPairingMethod(peer)) { OIC_LOG(ERROR, TAG, "Target does not support the Direct-Pairing"); continue; } input = readline(" > Enter pairing method: ", NULL); if (!input || !strlen(input)) { continue; } pmIdx = strtol(input, &ptr, 10); printf("\n"); if (0 >= pmIdx || peer->prmLen+1 < (size_t)pmIdx) { OIC_LOG(ERROR, TAG, "Invalid mode selection"); continue; } pmSel = peer->prm[pmIdx-1]; // get PIN char pinNumber[DP_PIN_LENGTH+1]; input = readline(" > Enter PIN Number for authentication (ex - '00000000' [8 digit] ): ", NULL); if (!input || DP_PIN_LENGTH != strlen(input)) { OIC_LOG(ERROR, TAG, "Invalid PIN"); continue; } sscanf(input, "%9s", pinNumber); printf("\n"); ret = DoDirectPairing(peer, pmSel, pinNumber); if (OC_STACK_OK != ret) { OIC_LOG(ERROR, TAG, "Error in DoDirectPairing()"); } } else if (!strcmp(query, "sd")) { OIC_LOG(INFO, TAG, "- Send data(GET Request) to device(led server) -"); //pairedDevs = OCGetDirectPairedDevices(); //printList(pairedDevs); printList(discoveredDevs); // target peer OCDPDev_t *peer = NULL; long peerIdx; input = readline(" > Enter Peer Device Number to initiate Direct-Pairing: ", NULL); if (!input || !strlen(input)) { continue; } char *ptr; peerIdx = strtol(input, &ptr, 10); //peer = getDev(pairedDevs, peerIdx); peer = getDev(discoveredDevs, (uint32_t)peerIdx); if (NULL == peer) { OIC_LOG(ERROR, TAG, "Not found the peer in discovered list"); continue; } // send Get Req ret = SendGetRequest(peer); if (OC_STACK_OK != ret) { OIC_LOG(ERROR, TAG, "Error in SendGetRequest()"); } } else if (!strcmp(query, "ll")) { OIC_LOG(INFO, TAG, "- List all discovered and paired devices) -"); printf(" > List of discovered devices\n"); printList(discoveredDevs); printf("\n"); printf(" > List of paired devices\n"); pairedDevs = OCGetDirectPairedDevices(); printList(pairedDevs); printf("\n"); } else if (!strcmp(query, "q")) { printf("QUIT\n"); gQuitFlag = 1; break; } } } return 0; }