OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); if(clientResponse->sequenceNumber == 0) { OIC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully"); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); } else { OIC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d", gNumObserveNotifies); OIC_LOG_PAYLOAD(INFO, clientResponse->payload);; gNumObserveNotifies++; if (gNumObserveNotifies == 3) { if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Observe cancel error"); } } } } if(TestType == TEST_PUT_DEFAULT || TestType == TEST_PUT_BATCH || TestType == TEST_PUT_LINK_LIST) { InitPutRequest(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); 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_NON_CON_OP: InitGetRequest(OC_LOW_QOS); InitPutRequest(); InitPostRequest(OC_LOW_QOS); break; case TEST_CON_OP: InitGetRequest(OC_HIGH_QOS); InitPutRequest(); InitPostRequest(OC_HIGH_QOS); break; default: PrintUsage(); break; } } return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; }
// This is a function called back when a device is discovered OCStackApplicationResult discoveryReqCB(void*, OCDoHandle, OCClientResponse * clientResponse) { OIC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully"); if (clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "Device =============> Discovered @ %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); if (clientResponse->result == OC_STACK_OK) { OIC_LOG_PAYLOAD(INFO, clientResponse->payload); ocConnType = clientResponse->connType; endpoint = clientResponse->devAddr; if (parseClientResponse(clientResponse) != -1) { switch(TestCase) { case TEST_NON_CON_OP: InitGetRequest(OC_LOW_QOS); InitPutRequest(OC_LOW_QOS); InitPostRequest(OC_LOW_QOS); break; case TEST_CON_OP: InitGetRequest(OC_HIGH_QOS); InitPutRequest(OC_HIGH_QOS); InitPostRequest(OC_HIGH_QOS); break; } } } } return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; }
// 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)); OIC_LOG_V(INFO, TAG, "Discovered @ %s:%u =============> ", clientResponse->devAddr.addr, clientResponse->devAddr.port); OIC_LOG_PAYLOAD (INFO, clientResponse->payload); endpoint = clientResponse->devAddr; switch(TestCase) { case TEST_NON_CON_OP: InitGetRequest(OC_LOW_QOS); break; case TEST_CON_OP: InitGetRequest(OC_HIGH_QOS); break; case TEST_NON_CON_PUT: InitPutRequest(OC_LOW_QOS); break; case TEST_CON_PUT: InitPutRequest(OC_HIGH_QOS); break; default: PrintUsage(); break; } } return UnicastDiscovery ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; }
void queryResource() { switch(TestCase) { case TEST_DISCOVER_REQ: break; case TEST_NON_CON_OP: InitGetRequest(OC_LOW_QOS); InitPutRequest(OC_LOW_QOS); InitPostRequest(OC_LOW_QOS); break; case TEST_CON_OP: InitGetRequest(OC_HIGH_QOS); InitPutRequest(OC_HIGH_QOS); InitPostRequest(OC_HIGH_QOS); break; default: PrintUsage(); break; } }
// 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, "DISCOVER callback recvd"); } if (!clientResponse) { OIC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse"); } OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); responseAddr = clientResponse->devAddr; switch(TEST_CASE) { OIC_LOG_V(INFO, TAG, "TEST_CASE %u\n", TEST_CASE); case TEST_GET_REQ_NON: InitGetRequest(OC_LOW_QOS); 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; default: PrintUsage(); break; } SET_BUT_NOT_USED(handle); return OC_STACK_KEEP_TRANSACTION; }
// 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; }
int main( int argc, char* argv[] ) { uint8_t addr[20] = {0}; uint8_t* paddr = NULL; uint16_t port = USE_RANDOM_PORT; xmpp_ctx_t *ctx = NULL; xmpp_conn_t* conn = NULL; int reg_act = DMC_REGISTER; int errornum=0; void *ptr; pthread_attr_init(&attr1); pthread_attr_setstacksize(&attr1, PTHREAD_STACK_MIN * 2); if(!setPIN("2345678901")) { fprintf(stderr, "PIN read error\n"); return 1; } else printf("\t\tRead PIN : %s\n", getPIN()); if( argc < 2 ) { exit(-1); } uint8_t *ifname = (uint8_t *)argv[1]; /*Get Ip address on defined interface and initialize coap on it with random port number * this port number will be used as a source port in all coap communications*/ if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)) == ERR_SUCCESS) { OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr); paddr = addr; } /* Initialize OCStack*/ if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack init error"); return 0; } time(&last_discovery_time); InitDiscovery(); // conn = XMPP_Init( xmpp_un , xmpp_pw , xmpp_server, &ctx); // Break from loop with Ctrl+C OC_LOG(INFO, TAG, "Entering occlient main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if( cmd_head != NULL ) { switch( cmd_head->command ) { case COMMAND_DISCOVERY: InitDiscovery(); break; case COMMAND_GET: InitGetRequest(cmd_head); break; case COMMAND_PUT: InitPutRequest(cmd_head); break; case COMMAND_POST: InitPutRequest(cmd_head); break; case COMMAND_OBSERVE: break; default: break; } struct cmd *cmd_temp = cmd_head; cmd_head = cmd_head->next; free(cmd_temp); } else { //do discovery every 10 seconds time_t now; time(&now); if( now - last_discovery_time > DMC_INTERVAL ) { struct dmc_result *res = dmc_discovery_finish(reg_act); if( res->state == REG_SUCCEED ) { reg_act = DMC_UPDATE; snprintf( xmpp_un, sizeof(xmpp_un), "%s", res->username ); snprintf( xmpp_pw, sizeof(xmpp_pw), "%s", res->password ); snprintf( xmpp_server, sizeof(xmpp_server), "%s", res->server ); free( res ); printf("%s\n%s\n%s\n", xmpp_server, xmpp_un, xmpp_pw); if(conn==NULL) { conn = XMPP_Init( \ xmpp_un , xmpp_pw, xmpp_server, &ctx); if(ctx !=NULL) pthread_create(&pid1, \ &attr1, (void*)xmpp_run, (void*)ctx ); else printf("ctx null\n"); } } else free(res); OC_LOG_V( INFO, TAG, "DM client returns state %s", get_dmcresult_char(res->state) ); InitDiscovery(); time(&last_discovery_time); } } if (OCProcess() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); return 0; } usleep(200*1000); } printf("Quit\n"); OC_LOG(INFO, TAG, "Exiting occlient main loop..."); if (OCStop() != OC_STACK_OK) { OC_LOG_V(ERROR, TAG, "OCStack stop error"); } xmpp_stop(ctx); sleep(1); XMPP_Close(ctx, conn); if ((errornum = pthread_cancel(pid1)) != 0) OC_LOG_V(INFO, TAG, "pthread_cancel: %s", strerror(errornum)); if ((errornum = pthread_join(pid1, &ptr)) != 0) OC_LOG_V(INFO, TAG, "pthread_join: %s", strerror(errornum)); printf("Done\n"); return 0; }