int GetUrl(char *url, char **reply_ret, int *len_ret) { char *hostname, *path; int port; XtransConnInfo trans; int status = 0; if (ParseHttpUrl(url, &hostname, &port, &path) != 0) return 1; /* invalid URL */ trans = OpenConnection(hostname, port); if (trans == NULL) { status = 1; /* connection failed */ goto end; } SendGetRequest(trans, path); *len_ret = ReadGetReply(trans, reply_ret); CloseConnection(trans); end: Free(hostname); Free(path); return status; }
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; }