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; }
OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully"); } if (clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d", gNumPresenceNotifies); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); OIC_LOG(INFO, TAG, ("=============> Presence Response")); gNumPresenceNotifies++; if (gNumPresenceNotifies == 20) { if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Presence cancel error"); } return OC_STACK_DELETE_TRANSACTION; } } else { OIC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse"); } return OC_STACK_KEEP_TRANSACTION; }
/** * This internal method handles RD publish request. * Responds with the RD success message. */ static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *ehRequest) { OCEntityHandlerResult ehResult = OC_EH_OK; if (!ehRequest) { OIC_LOG(DEBUG, TAG, "Invalid request pointer"); return OC_EH_ERROR; } OIC_LOG_V(DEBUG, TAG, "Received OC_REST_PUT from client with query: %s.", ehRequest->query); OCRDPayload *payload = (OCRDPayload *)ehRequest->payload; if (payload && payload->rdPublish) { OCRDStorePublishedResources(payload->rdPublish, &ehRequest->devAddr); } OCRDPayload *rdPayload = OCRDPayloadCreate(); if (!rdPayload) { OIC_LOG(ERROR, TAG, "Failed allocating memory."); return OC_STACK_NO_MEMORY; } OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload); if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Sending response failed."); ehResult = OC_EH_ERROR; } return ehResult; }
/** * This internal method handles RD discovery request. * Responds with the RD discovery payload message. */ static OCEntityHandlerResult handleGetRequest(const OCEntityHandlerRequest *ehRequest) { if (!ehRequest) { OIC_LOG(DEBUG, TAG, "Invalid request pointer."); return OC_EH_ERROR; } OCEntityHandlerResult ehResult = OC_EH_OK; OIC_LOG_V(DEBUG, TAG, "Received OC_REST_GET from client with query: %s.", ehRequest->query); OCRDPayload *rdPayload = OCRDPayloadCreate(); if (!rdPayload) { return OC_STACK_NO_MEMORY; } rdPayload->rdDiscovery = OCRDDiscoveryPayloadCreate(NULL, OCGetServerInstanceIDString(), OC_RD_DISC_SEL); if (!rdPayload->rdDiscovery) { OCRDPayloadDestroy(rdPayload); return OC_STACK_NO_MEMORY; } OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload); if (sendResponse(ehRequest, rdPayload) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Sending response failed."); ehResult = OC_EH_ERROR; } return ehResult; }
// This is a function called back when a device is discovered OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { OIC_LOG(INFO, TAG, "Entering discoveryReqCB (Application Layer CB)"); OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OIC_LOG_V(INFO, TAG, "Callback Context recvd successfully"); } OIC_LOG_V(INFO, TAG, "Device =============> Discovered @ %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); ConnType = clientResponse->connType; if(TestType == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TestType == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\ TestType == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST) { InitGetRequestToUnavailableResource(clientResponse); } else { InitGetRequest(clientResponse); } return OC_STACK_KEEP_TRANSACTION; }
int InitObserveRequest(OCClientResponse * clientResponse) { OCStackResult ret; OCCallbackData cbData; OCDoHandle handle; std::ostringstream obsReg; obsReg << getQueryStrForGetPut(); cbData.cb = getReqCB; cbData.context = (void*)DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; OIC_LOG_V(INFO, TAG, "OBSERVE payload from client ="); OCPayload* payload = putPayload(); OIC_LOG_PAYLOAD(INFO, payload); OCPayloadDestroy(payload); ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), &clientResponse->devAddr, 0, ConnType, OC_LOW_QOS, &cbData, NULL, 0); if (ret != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack resource error"); } else { gObserveDoHandle = handle; } return ret; }
OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context recvd successfully"); } if (clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber); OIC_LOG_V(INFO, TAG, "PRESENCE notification %d recvd", gNumPresenceNotifies); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); gNumPresenceNotifies++; if (gNumPresenceNotifies == maxNotification) { if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Presence cancel error"); } return OC_STACK_DELETE_TRANSACTION; } } else { OIC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse"); } SET_BUT_NOT_USED(handle); return OC_STACK_KEEP_TRANSACTION; }
int InitPutRequest(OCClientResponse * clientResponse) { OCStackResult ret; OCCallbackData cbData; //* Make a PUT query*/ std::ostringstream getQuery; getQuery << "coap://" << clientResponse->devAddr.addr << ":" << clientResponse->devAddr.port << "/a/room" << queryInterface[TestType].text; cbData.cb = putReqCB; cbData.context = (void*)DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; OIC_LOG_V(INFO, TAG, "PUT payload from client = "); OCPayload* payload = putPayload(); OIC_LOG_PAYLOAD(INFO, payload); OCPayloadDestroy(payload); ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(), &clientResponse->devAddr, putPayload(), ConnType, OC_LOW_QOS, &cbData, NULL, 0); if (ret != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack resource error"); } return ret; }
OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if (ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully"); } if (clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OIC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d", gNumObserveNotifies); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); OIC_LOG(INFO, TAG, ("=============> Obs Response")); gNumObserveNotifies++; if (gNumObserveNotifies > 15) //large number to test observing in DELETE case. { if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON) { OIC_LOG(ERROR, TAG, "Cancelling with LOW QOS"); if (OCCancel (handle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Observe cancel error"); } return OC_STACK_DELETE_TRANSACTION; } else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM) { OIC_LOG(ERROR, TAG, "Cancelling with HIGH QOS"); if (OCCancel (handle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Observe cancel error"); } } } if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER) { OIC_LOG(INFO, TAG, "This also serves as a registration confirmation"); } else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER) { OIC_LOG(INFO, TAG, "This also serves as a deregistration confirmation"); return OC_STACK_DELETE_TRANSACTION; } else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION) { OIC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed"); return OC_STACK_DELETE_TRANSACTION; } } else { OIC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse"); } return OC_STACK_KEEP_TRANSACTION; }
OCStackApplicationResult postReqCB(void *, OCDoHandle, OCClientResponse *clientResponse) { OIC_LOG(INFO, TAG, "Callback Context for POST recvd successfully"); if(clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); OIC_LOG(INFO, TAG, "=============> Post Response"); } return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult getReqCB(void*, OCDoHandle, OCClientResponse * clientResponse) { OIC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully"); if(clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); OIC_LOG(INFO, TAG, "=============> Get Response"); } return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if(!clientResponse) { OIC_LOG_V(INFO, TAG, "obsReqCB received NULL response"); } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context recvd successfully"); } OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OIC_LOG_V(INFO, TAG, "OBSERVE notification %d recvd", gNumObserveNotifies); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); gNumObserveNotifies++; if (gNumObserveNotifies == maxNotification) { if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Observe cancel error"); } return OC_STACK_DELETE_TRANSACTION; } if (gNumObserveNotifies == 1 && TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM) { if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Observe cancel error"); } } if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER) { OIC_LOG(INFO, TAG, "Registration confirmed"); } else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER) { OIC_LOG(INFO, TAG, "de-registration confirmed"); return OC_STACK_DELETE_TRANSACTION; } else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION) { OIC_LOG(INFO, TAG, "Registration/deregistration failed"); return OC_STACK_DELETE_TRANSACTION; } SET_BUT_NOT_USED(handle); return OC_STACK_KEEP_TRANSACTION; }
OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { if(clientResponse == NULL) { OIC_LOG(INFO, TAG, "The clientResponse is NULL"); return OC_STACK_DELETE_TRANSACTION; } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully"); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); } 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, "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*, 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 ; }
OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully"); } if(clientResponse) { OIC_LOG_PAYLOAD(INFO, clientResponse->payload); } else { OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse"); } SET_BUT_NOT_USED(handle); return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully"); } if (clientResponse) { OIC_LOG(INFO, TAG, ("Discovery Response:")); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); } else { OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse"); } return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION; }
OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/, OCClientResponse *clientResponse) { if (ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context for POST recvd successfully"); } if (clientResponse) { OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); OIC_LOG(INFO, TAG, ("=============> Post Response")); } else { OIC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse"); } return OC_STACK_DELETE_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 ; }
// 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); } } return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { if (clientResponse == NULL) { OIC_LOG(INFO, TAG, "getReqCB received NULL clientResponse"); return OC_STACK_DELETE_TRANSACTION; } if (ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully"); } OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); OIC_LOG(INFO, TAG, ("=============> Get Response")); if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0) { OIC_LOG (INFO, TAG, "Received vendor specific options"); uint8_t i = 0; OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions; for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++) { if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID) { OIC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with", ((OCHeaderOption)rcvdOptions[i]).optionID ); OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData, MAX_HEADER_OPTION_DATA_LENGTH); } } } return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult restRequestCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if(clientResponse == NULL) { OIC_LOG(INFO, TAG, "Received NULL response"); return OC_STACK_DELETE_TRANSACTION; } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OIC_LOG(INFO, TAG, "Callback Context recvd successfully"); } OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OIC_LOG_PAYLOAD(INFO, clientResponse->payload); if(clientResponse->numRcvdVendorSpecificHeaderOptions > 0) { OIC_LOG (INFO, TAG, "Received vendor specific options. Ignoring"); } SET_BUT_NOT_USED(handle); return OC_STACK_DELETE_TRANSACTION; }
OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPayload) { CborValue *rdCBORPayload = (CborValue *)cborPayload; CborError cborFindResult; OCRDPayload *rdPayload = OCRDPayloadCreate(); if (!rdPayload) { goto no_memory; } if (cbor_value_is_array(rdCBORPayload)) { OCLinksPayload *linksPayload = NULL; OCTagsPayload *tagsPayload = NULL; while (cbor_value_is_container(rdCBORPayload)) { // enter tags map CborValue tags; cborFindResult = cbor_value_enter_container(rdCBORPayload, &tags); if (cborFindResult != CborNoError) { goto cbor_error; } if (OC_STACK_OK != OCTagsCborToPayload(&tags, &tagsPayload)) { OCFreeTagsResource(tagsPayload); goto cbor_error; } OCTagsLog(DEBUG, tagsPayload); if (OC_STACK_OK != OCLinksCborToPayload(&tags, &linksPayload)) { OCFreeLinksResource(linksPayload); OCFreeTagsResource(tagsPayload); goto cbor_error; } OCLinksLog(DEBUG, linksPayload); // Move from tags payload to links array. if (CborNoError != cbor_value_advance(rdCBORPayload)) { OIC_LOG(DEBUG, TAG, "Failed advancing from tags payload to links."); OCFreeLinksResource(linksPayload); OCFreeTagsResource(tagsPayload); goto cbor_error; } } rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload); if (!rdPayload->rdPublish) { goto cbor_error; } } else if (cbor_value_is_map(rdCBORPayload)) { char *name = NULL; if (CborNoError != FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_NAME, &name)) { goto cbor_error; } char *id = NULL; if (CborNoError != FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_ID, &id)) { goto cbor_error; } uint64_t biasFactor = 0; if (CborNoError != FindIntInMap(rdCBORPayload, OC_RSRVD_RD_DISCOVERY_SEL, &biasFactor)) { goto cbor_error; } rdPayload->rdDiscovery = OCRDDiscoveryPayloadCreate(name, id, (uint8_t)biasFactor); if (!rdPayload->rdDiscovery) { goto no_memory; } OICFree(id); OICFree(name); cborFindResult = cbor_value_advance(rdCBORPayload); if (CborNoError != cborFindResult) { goto cbor_error; } } OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload); *outPayload = (OCPayload *)rdPayload; return OC_STACK_OK; no_memory: OIC_LOG(ERROR, TAG, "Failed allocating memory."); OCRDPayloadDestroy(rdPayload); return OC_STACK_NO_MEMORY; cbor_error: OCRDPayloadDestroy(rdPayload); return OC_STACK_ERROR; }
// 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; }
OCStackResult OCRDCborToPayload(const CborValue *cborPayload, OCPayload **outPayload) { CborValue *rdCBORPayload = (CborValue *)cborPayload; OCStackResult ret = OC_STACK_NO_MEMORY; CborError cborFindResult; OCRDPayload *rdPayload = OCRDPayloadCreate(); VERIFY_PARAM_NON_NULL(TAG, rdPayload, "Failed allocating rdPayload"); ret = OC_STACK_MALFORMED_RESPONSE; if (cbor_value_is_array(rdCBORPayload)) { OCLinksPayload *linksPayload = NULL; OCTagsPayload *tagsPayload = NULL; while (cbor_value_is_container(rdCBORPayload)) { // enter tags map CborValue tags; cborFindResult = cbor_value_enter_container(rdCBORPayload, &tags); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed entering tags container."); cborFindResult= OCTagsCborToPayload(&tags, &tagsPayload); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing tags payload."); OCTagsLog(DEBUG, tagsPayload); cborFindResult = OCLinksCborToPayload(&tags, &linksPayload); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed parsing links payload."); OCLinksLog(DEBUG, linksPayload); // Move from tags payload to links array. cborFindResult = cbor_value_advance(rdCBORPayload); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCborPayload."); } rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload); VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdPublish, "Failed allocating rdPayload->rdPublish"); } else if (cbor_value_is_map(rdCBORPayload)) { rdPayload->rdDiscovery = (OCRDDiscoveryPayload *)OICCalloc(1, sizeof(OCRDDiscoveryPayload)); VERIFY_PARAM_NON_NULL(TAG, rdPayload->rdDiscovery, "Failed allocating discoveryPayload"); cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_NAME, &rdPayload->rdDiscovery->n.deviceName); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_DEVICE_NAME."); char *deviceId = NULL; cborFindResult = FindStringInMap(rdCBORPayload, OC_RSRVD_DEVICE_ID, &deviceId); if (deviceId) { memcpy(rdPayload->rdDiscovery->di.id, deviceId, strlen(deviceId)); OICFree(deviceId); } VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_DEVICE_ID."); { uint64_t value = 0; cborFindResult = FindIntInMap(rdCBORPayload, OC_RSRVD_RD_DISCOVERY_SEL, &value); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed finding OC_RSRVD_RD_DISCOVERY_SEL."); rdPayload->rdDiscovery->sel = value; } cborFindResult = cbor_value_advance(rdCBORPayload); VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed advancing rdCBORPayload."); } OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload); *outPayload = (OCPayload *)rdPayload; return OC_STACK_OK; exit: OCRDPayloadDestroy(rdPayload); return ret; }