OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); if(clientResponse->sequenceNumber == 0) { OC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully"); OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload); } else { OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d", gNumObserveNotifies); OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);; gNumObserveNotifies++; if (gNumObserveNotifies == 3) { if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Observe cancel error"); } } } } if(TEST == TEST_PUT_DEFAULT || TEST == TEST_PUT_BATCH || TEST == 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) { if (ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "\n<====Callback Context for DISCOVERY query " "received successfully====>"); } else { OC_LOG(ERROR, TAG, "\n<====Callback Context for DISCOVERY fail====>"); } if (clientResponse) { OC_LOG_V(INFO, TAG, "Device =============> Discovered @ %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); OC_LOG_PAYLOAD(INFO, clientResponse->payload); collectUniqueResource(clientResponse); } else { OC_LOG(ERROR, TAG, "<====DISCOVERY Callback fail to receive clientResponse====>\n"); } return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ; }
OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context recvd successfully"); } if (clientResponse) { OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber); OC_LOG_V(INFO, TAG, "PRESENCE notification %d recvd", gNumPresenceNotifies); OC_LOG_PAYLOAD(INFO, clientResponse->payload); gNumPresenceNotifies++; if (gNumPresenceNotifies == maxNotification) { if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Presence cancel error"); } return OC_STACK_DELETE_TRANSACTION; } } else { OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse"); } SET_BUT_NOT_USED(handle); return OC_STACK_KEEP_TRANSACTION; }
int InitObserveRequest(OCClientResponse * clientResponse) { OCStackResult ret; OCCallbackData cbData; OCDoHandle handle; std::ostringstream obsReg; obsReg << "coap://" << clientResponse->devAddr.addr << ":" << clientResponse->devAddr.addr << getQueryStrForGetPut(); cbData.cb = getReqCB; cbData.context = (void*)DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; OC_LOG_V(INFO, TAG, "OBSERVE payload from client ="); OC_LOG_PAYLOAD(INFO, TAG, putPayload()); ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS, &cbData, NULL, 0); if (ret != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack resource error"); } else { gObserveDoHandle = handle; } return ret; }
// This is a function called back when a device is discovered OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { OC_LOG(INFO, TAG, "Entering discoveryReqCB (Application Layer CB)"); OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OC_LOG_V(INFO, TAG, "Callback Context recvd successfully"); } OC_LOG_V(INFO, TAG, "Device =============> Discovered @ %s:%d", clientResponse->devAddr.addr, clientResponse->devAddr.port); OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload); if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\ TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST) { InitGetRequestToUnavailableResource(clientResponse); } else { InitGetRequest(clientResponse); } 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; OC_LOG_V(INFO, TAG, "PUT payload from client = "); OCPayload* payload = putPayload(); OC_LOG_PAYLOAD(INFO, TAG, 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) { OC_LOG(ERROR, TAG, "OCStack resource error"); } return ret; }
OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if(!clientResponse) { OC_LOG_V(INFO, TAG, "obsReqCB received NULL response"); } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context recvd successfully"); } OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OC_LOG_V(INFO, TAG, "OBSERVE notification %d recvd", gNumObserveNotifies); OC_LOG_PAYLOAD(INFO, clientResponse->payload); gNumObserveNotifies++; if (gNumObserveNotifies == maxNotification) { if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK) { OC_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) { OC_LOG(ERROR, TAG, "Observe cancel error"); } } if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER) { OC_LOG(INFO, TAG, "Registration confirmed"); } else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER) { OC_LOG(INFO, TAG, "de-registration confirmed"); return OC_STACK_DELETE_TRANSACTION; } else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION) { OC_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) { OC_LOG(INFO, TAG, "The clientResponse is NULL"); return OC_STACK_DELETE_TRANSACTION; } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully"); OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload); } return OC_STACK_KEEP_TRANSACTION; }
OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest, void* callbackParam) { OCEntityHandlerResult result = OC_EH_ERROR; OCEntityHandlerRequest *request = NULL; OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag); if (flag & OC_REQUEST_FLAG) { OC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (entityHandlerRequest) { OC_LOG_V (INFO, TAG, "request query %s from client", entityHandlerRequest->query); OC_LOG_PAYLOAD (INFO, TAG, entityHandlerRequest->payload); // Make deep copy of received request and queue it for slow processing request = CopyRequest(entityHandlerRequest); if (request) { OC_LOG(INFO, TAG, "Scheduling slow response for received request"); gRequestList.push_back(request); // Indicate to the stack that this is a slow response result = OC_EH_SLOW; // Start the slow response alarm alarm(SLOW_RESPONSE_DELAY_SEC); } else { OC_LOG(ERROR, TAG, "Error queuing request for slow response"); // Indicate to the stack that this is a slow response result = OC_EH_ERROR; } } else { OC_LOG(ERROR, TAG, "Invalid request"); result = OC_EH_ERROR; } } return result; }
// 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) { OC_LOG(INFO, TAG, "DISCOVER callback recvd"); } if (!clientResponse) { OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse"); } OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OC_LOG_PAYLOAD(INFO, clientResponse->payload); responseAddr = clientResponse->devAddr; switch(TEST_CASE) { OC_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; }
OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "<====Callback Context for GET received successfully====>"); } else { OC_LOG(ERROR, TAG, "<====Callback Context for GET fail====>"); } if (clientResponse) { OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OC_LOG_PAYLOAD(INFO, clientResponse->payload); OC_LOG(INFO, TAG, ("=============> Get Response")); if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0 ) { OC_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) { OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with", ((OCHeaderOption)rcvdOptions[i]).optionID ); OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData, MAX_HEADER_OPTION_DATA_LENGTH); } } } } else { OC_LOG(ERROR, TAG, "<====GET Callback fail to receive clientResponse====>\n"); } return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if (ctx == (void*) DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully"); } if(clientResponse) { OC_LOG_PAYLOAD(INFO, clientResponse->payload); } else { OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse"); } SET_BUT_NOT_USED(handle); return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/, OCClientResponse * clientResponse) { if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "<====Callback Context for PUT received successfully====>"); } else { OC_LOG(ERROR, TAG, "<====Callback Context for PUT fail====>"); } if(clientResponse) { OC_LOG_PAYLOAD(INFO, clientResponse->payload); OC_LOG(INFO, TAG, ("=============> Put Response")); } else { OC_LOG(ERROR, TAG, "<====PUT Callback fail to receive clientResponse====>\n"); } return OC_STACK_DELETE_TRANSACTION; }
OCStackApplicationResult restRequestCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) { if(clientResponse == NULL) { OC_LOG(INFO, TAG, "Received NULL response"); return OC_STACK_DELETE_TRANSACTION; } if(ctx == (void*)DEFAULT_CONTEXT_VALUE) { OC_LOG(INFO, TAG, "Callback Context recvd successfully"); } OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result)); OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); OC_LOG_PAYLOAD(INFO, clientResponse->payload); if(clientResponse->numRcvdVendorSpecificHeaderOptions > 0) { OC_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)) { OC_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; } } OC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload); *outPayload = (OCPayload *)rdPayload; return OC_STACK_OK; no_memory: OC_LOG(ERROR, TAG, "Failed allocating memory."); OCRDPayloadDestroy(rdPayload); return OC_STACK_NO_MEMORY; cbor_error: OCRDPayloadDestroy(rdPayload); return OC_STACK_ERROR; }
OCStackResult DefaultCollectionEntityHandler (OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest) { if (!ehRequest || !ehRequest->query) { return OC_STACK_INVALID_PARAM; } OC_LOG_V(INFO, TAG, "DefaultCollectionEntityHandler with query %s", ehRequest->query); if (flag != OC_REQUEST_FLAG) { return OC_STACK_ERROR; } OCStackIfTypes ifQueryParam = STACK_IF_INVALID; char *rtQueryParam = NULL; OCStackResult result = ValidateQuery (ehRequest->query, ehRequest->resource, &ifQueryParam, &rtQueryParam); if (result != OC_STACK_OK) { return result; } switch (ehRequest->method) { case OC_REST_GET: switch (ifQueryParam) { case STACK_IF_DEFAULT: // Get attributes of collection resource and properties of contained resources // M1 release does not support attributes for collection resource, so the GET // operation is same as the GET on LL interface. OC_LOG(INFO, TAG, "STACK_IF_DEFAULT"); return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL); case STACK_IF_LL: OC_LOG(INFO, TAG, "STACK_IF_LL"); return HandleLinkedListInterface(ehRequest, STACK_RES_DISCOVERY_NOFILTER, NULL); case STACK_IF_BATCH: OC_LOG(INFO, TAG, "STACK_IF_BATCH"); ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler = HandleAggregateResponse; ((OCServerRequest *)ehRequest->requestHandle)->numResponses = GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1; return HandleBatchInterface(ehRequest); case STACK_IF_GROUP: return BuildCollectionGroupActionCBORResponse(OC_REST_GET/*flag*/, (OCResource *) ehRequest->resource, ehRequest); default: return OC_STACK_ERROR; } case OC_REST_PUT: switch (ifQueryParam) { case STACK_IF_DEFAULT: // M1 release does not support PUT on default interface return OC_STACK_ERROR; case STACK_IF_LL: // LL interface only supports GET return OC_STACK_ERROR; case STACK_IF_BATCH: ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler = HandleAggregateResponse; ((OCServerRequest *)ehRequest->requestHandle)->numResponses = GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1; return HandleBatchInterface(ehRequest); case STACK_IF_GROUP: OC_LOG(INFO, TAG, "IF_COLLECTION PUT with request ::\n"); OC_LOG_PAYLOAD(INFO, ehRequest->payload); return BuildCollectionGroupActionCBORResponse(OC_REST_PUT/*flag*/, (OCResource *) ehRequest->resource, ehRequest); default: return OC_STACK_ERROR; } case OC_REST_POST: switch (ifQueryParam) { case STACK_IF_DEFAULT: // M1 release does not support POST on default interface return OC_STACK_ERROR; case STACK_IF_LL: // LL interface only supports GET return OC_STACK_ERROR; case STACK_IF_BATCH: ((OCServerRequest *)ehRequest->requestHandle)->ehResponseHandler = HandleAggregateResponse; ((OCServerRequest *)ehRequest->requestHandle)->numResponses = GetNumOfResourcesInCollection((OCResource *)ehRequest->resource) + 1; return HandleBatchInterface(ehRequest); case STACK_IF_GROUP: OC_LOG(INFO, TAG, "IF_COLLECTION POST with request ::\n"); OC_LOG_PAYLOAD(INFO, ehRequest->payload); return BuildCollectionGroupActionCBORResponse(OC_REST_POST/*flag*/, (OCResource *) ehRequest->resource, ehRequest); default: return OC_STACK_ERROR; } case OC_REST_DELETE: // TODO implement DELETE accordingly to the desired behavior return OC_STACK_ERROR; default: return OC_STACK_ERROR; } }