int main(int argc, char* argv[]) { int opt; while ((opt = getopt(argc, argv, "u:t:c:")) != -1) { switch(opt) { case 'u': UnicastDiscovery = atoi(optarg); break; case 't': TestCase = atoi(optarg); break; case 'c': Connectivity = atoi(optarg); break; default: PrintUsage(); return -1; } } if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) || (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) || (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT)) { PrintUsage(); return -1; } if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack init error"); return 0; } #ifdef ROUTING_GATEWAY /* * Before invoking Discover resource, we process the gateway requests * and form the routing table. */ for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++) { if (OC_STACK_OK != OCProcess()) { OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } usleep(SLEEP_DURATION); } #endif if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP) { ConnType = CT_ADAPTER_IP; } else { OIC_LOG(INFO, TAG, "Default Connectivity type selected..."); PrintUsage(); } discoveryAddr[0] = '\0'; if (UnicastDiscovery) { OIC_LOG(INFO, TAG, "Enter IP address of server with optional port number"); OIC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n"); OIC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n"); if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin)) { //Strip newline char from ipv4addr StripNewLineChar(discoveryAddr); } else { OIC_LOG(ERROR, TAG, "!! Bad input for IP address. !!"); return OC_STACK_INVALID_PARAM; } } if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ) { InitDeviceDiscovery(OC_LOW_QOS); } else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ) { InitPlatformDiscovery(OC_LOW_QOS); } else { InitDiscovery(OC_LOW_QOS); } // Break from loop with Ctrl+C OIC_LOG(INFO, TAG, "Entering occlient main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } #ifndef ROUTING_GATEAWAY sleep(1); #endif } OIC_LOG(INFO, TAG, "Exiting occlient main loop..."); if (OCStop() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack stop error"); } return 0; }
int main(int argc, char* argv[]) { int opt; while ((opt = getopt(argc, argv, "u:t:c:")) != -1) { switch(opt) { case 'u': UNICAST_DISCOVERY = atoi(optarg); break; case 't': TEST_CASE = atoi(optarg); break; case 'c': OC_CONNTYPE = OCConnectivityType(atoi(optarg)); break; default: PrintUsage(); return -1; } } if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) || (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) ) { PrintUsage(); return -1; } /* Initialize OCStack*/ if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack init error"); return 0; } if (UNICAST_DISCOVERY) { printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n"); if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin)) { //Strip newline char from ipv4addr StripNewLineChar(ipv4addr); } else { OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!"); return OC_STACK_INVALID_PARAM; } } if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ) { InitDeviceDiscovery(OC_LOW_QOS); } else { InitDiscovery(OC_LOW_QOS); } // Break from loop with Ctrl+C OC_LOG(INFO, TAG, "Entering occlient main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); return 0; } sleep(2); } OC_LOG(INFO, TAG, "Exiting occlient main loop..."); if (OCStop() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack stop error"); } return 0; }
// This is a function called back when a device is discovered OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully"); } if (clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); std::string connectionType = getConnectivityType (clientResponse->connType); OIC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str()); OIC_LOG_V(INFO, TAG, "Device =============> Discovered @ %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); ConnType = clientResponse->connType; serverAddr = clientResponse->devAddr; OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload; if (!payload) { return OC_STACK_DELETE_TRANSACTION; } OCResourcePayload *resource = (OCResourcePayload*) payload->resources; if (!resource) { OIC_LOG_V (INFO, TAG, "No resources in payload"); return OC_STACK_DELETE_TRANSACTION; } coapServerResource = resource->uri; switch(TestCase) { case TEST_GET_REQ_NON: InitGetRequest(OC_LOW_QOS, 0, 0); break; case TEST_GET_REQ_NON_WITH_FILTERS: InitGetRequest(OC_LOW_QOS, 0, 1); break; case TEST_PUT_REQ_NON: InitPutRequest(OC_LOW_QOS); break; case TEST_POST_REQ_NON: InitPostRequest(OC_LOW_QOS); break; case TEST_DELETE_REQ_NON: InitDeleteRequest(OC_LOW_QOS); break; case TEST_OBS_REQ_NON: case TEST_OBS_REQ_NON_CANCEL_IMM: InitObserveRequest(OC_LOW_QOS); break; case TEST_GET_UNAVAILABLE_RES_REQ_NON: InitGetRequestToUnavailableResource(OC_LOW_QOS); break; case TEST_GET_REQ_CON: InitGetRequest(OC_HIGH_QOS, 0, 0); break; case TEST_POST_REQ_CON: InitPostRequest(OC_HIGH_QOS); break; case TEST_DELETE_REQ_CON: InitDeleteRequest(OC_HIGH_QOS); break; case TEST_OBS_REQ_CON: InitObserveRequest(OC_HIGH_QOS); break; #ifdef WITH_PRESENCE case TEST_OBS_PRESENCE: case TEST_OBS_PRESENCE_WITH_FILTER: case TEST_OBS_PRESENCE_WITH_FILTERS: case TEST_OBS_MULTICAST_PRESENCE: InitPresence(); break; #endif case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS: InitGetRequest(OC_LOW_QOS, 1, 0); break; case TEST_DISCOVER_PLATFORM_REQ: InitPlatformDiscovery(OC_LOW_QOS); break; case TEST_DISCOVER_DEV_REQ: InitDeviceDiscovery(OC_LOW_QOS); break; default: PrintUsage(); break; } } else { OIC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse"); } return OC_STACK_KEEP_TRANSACTION; }
// This is a function called back when a device is discovered OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { uint8_t remoteIpAddr[4]; uint16_t remotePortNu; if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully"); } if (clientResponse) { OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr, remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3); OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu); std::string connectionType = getConnectivityType (clientResponse->connType); OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str()); OC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %d.%d.%d.%d:%d", clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu); parseClientResponse(clientResponse); switch(TEST_CASE) { case TEST_GET_REQ_NON: InitGetRequest(OC_LOW_QOS, 0, 0); break; case TEST_GET_REQ_NON_WITH_FILTERS: InitGetRequest(OC_LOW_QOS, 0, 1); break; case TEST_PUT_REQ_NON: InitPutRequest(OC_LOW_QOS); break; case TEST_POST_REQ_NON: InitPostRequest(OC_LOW_QOS); break; case TEST_DELETE_REQ_NON: InitDeleteRequest(OC_LOW_QOS); break; case TEST_OBS_REQ_NON: case TEST_OBS_REQ_NON_CANCEL_IMM: InitObserveRequest(OC_LOW_QOS); break; case TEST_GET_UNAVAILABLE_RES_REQ_NON: InitGetRequestToUnavailableResource(OC_LOW_QOS); break; case TEST_GET_REQ_CON: InitGetRequest(OC_HIGH_QOS, 0, 0); break; case TEST_POST_REQ_CON: InitPostRequest(OC_HIGH_QOS); break; case TEST_DELETE_REQ_CON: InitDeleteRequest(OC_HIGH_QOS); break; case TEST_OBS_REQ_CON: InitObserveRequest(OC_HIGH_QOS); break; #ifdef WITH_PRESENCE case TEST_OBS_PRESENCE: case TEST_OBS_PRESENCE_WITH_FILTER: case TEST_OBS_PRESENCE_WITH_FILTERS: case TEST_OBS_MULTICAST_PRESENCE: InitPresence(); break; #endif case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS: InitGetRequest(OC_LOW_QOS, 1, 0); break; case TEST_DISCOVER_DEV_REQ: InitDeviceDiscovery(OC_LOW_QOS); break; default: PrintUsage(); break; } } else { OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse"); } return OC_STACK_KEEP_TRANSACTION; }