void ProcessGetPutRequest (OCEntityHandlerRequest *ehRequest) { OC_LOG(INFO, TAG, "Entering ProcessGetPutRequest"); OCRepPayload *getResp = constructResponse(ehRequest); if(!getResp) { OC_LOG(ERROR, TAG, "Failed to construct response"); return; } OCEntityHandlerResponse response; // Format the response. Note this requires some info about the request response.requestHandle = ehRequest->requestHandle; response.resourceHandle = ehRequest->resource; response.ehResult = OC_EH_OK; response.payload = reinterpret_cast<OCPayload*> (getResp); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof(response.resourceUri)); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); } free(getResp); }
void ProcessGetRequest (OCEntityHandlerRequest *ehRequest) { OC_LOG(INFO, TAG, "Entering ProcessGetRequest"); char *getResp = constructJsonResponse(ehRequest); OC_LOG(INFO, TAG, "After constructJsonResponse"); OCEntityHandlerResponse response; // Format the response. Note this requires some info about the request response.requestHandle = ehRequest->requestHandle; response.resourceHandle = ehRequest->resource; response.ehResult = OC_EH_OK; response.payload = (unsigned char *)getResp; response.payloadSize = strlen(getResp) + 1; response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof(response.resourceUri)); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); } free(getResp); }
/** * @brief handles the response to the entity handler * * @param responrce the entityhandler response * * @parma result of the entityhandler; */ OCEntityHandlerResult responseHandler(OCEntityHandlerResponse *response, OCEntityHandlerRequest *entityHandlerRequest, OCRepPayload *payload, OCEntityHandlerResult ehResult) { OIC_LOG(DEBUG, TAG, "Sending a response"); response->requestHandle = entityHandlerRequest->requestHandle; response->resourceHandle = entityHandlerRequest->resource; response->ehResult = ehResult; response->payload = (OCPayload*)payload; // Indicate that response is NOT in a persistent buffer response->persistentBufferFlag = 0; /*response->numSendVendorSpecificHeaderOptions = 0; memset(response->sendVendorSpecificHeaderOptions, 0, sizeof response->sendVendorSpecificHeaderOptions); memset(response->resourceUri, 0, sizeof response->resourceUri);*/ // Send the response OCStackResult stackResult = OCDoResponse(response); if (stackResult != OC_STACK_OK) { OIC_LOG_V(ERROR, TAG, "Error sending response with error code: %i", stackResult); return(OC_EH_ERROR); } return OC_EH_OK; }
OCEntityHandlerResult LcdOCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { OC_LOG(ERROR, TAG, ("Failed to allocate Payload")); return OC_EH_ERROR; } if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OC_LOG (INFO, TAG, ("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { OCRepPayloadSetUri(payload, "/grove/lcd"); OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str); } else if(OC_REST_PUT == entityHandlerRequest->method) { OC_LOG(INFO, TAG, ("PUT request")); OCRepPayload *rep = (OCRepPayload *)entityHandlerRequest->payload; OCRepPayloadGetPropString(rep, "lcd", &lcd.str); OC_LOG_V(INFO, TAG, "LCD string: %s", lcd.str); lcd_put(); OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str); } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; response.payload = (OCPayload*) payload; response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) { if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, ("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, ("Received OC_OBSERVE_DEREGISTER from client")); gLightUnderObservation = 0; } } OCRepPayloadDestroy(payload); return ehRet; }
static OCStackResult sendResponse(const OCEntityHandlerRequest *ehRequest, OCRDPayload *rdPayload) { OCEntityHandlerResponse response = { 0 }; response.requestHandle = ehRequest->requestHandle; response.resourceHandle = ehRequest->resource; response.ehResult = OC_EH_OK; response.payload = (OCPayload*)(rdPayload); return OCDoResponse(&response); }
// This is the entity handler for the registered resource. // This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult SensorOCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { OC_LOG(ERROR, TAG, ("Failed to allocate Payload")); return OC_EH_ERROR; } if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OC_LOG (INFO, TAG, ("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { //sensor_get(); OCRepPayloadSetUri(payload, "/grove/sensor"); OCRepPayloadSetPropDouble(payload, "temperature", sensor.temp); OCRepPayloadSetPropInt(payload, "light", sensor.light); OCRepPayloadSetPropInt(payload, "sound", sensor.sound); } else if(OC_REST_PUT == entityHandlerRequest->method) { OC_LOG(ERROR, TAG, ("Un-supported request for Sensor: PUT")); } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; response.payload = (OCPayload*) payload; response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) { if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, ("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, ("Received OC_OBSERVE_DEREGISTER from client")); gLightUnderObservation = 0; } } OCRepPayloadDestroy(payload); return ehRet; }
OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * ehRequest) { OCEntityHandlerResult ret = OC_EH_OK; OCEntityHandlerResponse response; char payload[MAX_RESPONSE_LENGTH] = {0}; OC_LOG_V(INFO, TAG, "Callback for Fan"); PrintReceivedMsgInfo(flag, ehRequest ); if(ehRequest && flag == OC_REQUEST_FLAG) { if(OC_REST_GET == ehRequest->method) { ret = HandleCallback(ehRequest, rspGetFanDefault, rspFailureFan, payload, sizeof(payload)); } else if(OC_REST_PUT == ehRequest->method) { ret = HandleCallback(ehRequest, rspPutFanDefault, rspFailureFan, payload, sizeof(payload)); } else { OC_LOG_V (INFO, TAG, "Received unsupported method %d from client", ehRequest->method); ret = OC_EH_ERROR; } if (ret == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = ehRequest->requestHandle; response.resourceHandle = ehRequest->resource; response.ehResult = ret; response.payload = (unsigned char *)payload; response.payloadSize = strlen(payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ret = OC_EH_ERROR; } } } else if (ehRequest && flag == OC_OBSERVE_FLAG) { gLightUnderObservation = 1; } return ret; }
static OCStackResult HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest, uint8_t filterOn, char *filterValue) { (void)filterOn; (void)filterValue; if(!ehRequest) { return OC_STACK_INVALID_PARAM; } OCStackResult ret = OC_STACK_OK; OCResource *collResource = (OCResource *)ehRequest->resource; OCRepPayload* payload = NULL; if(ret == OC_STACK_OK) { ret = BuildResponseRepresentation(collResource, &payload); } if (ret == OC_STACK_OK) { for (int i = 0; i < MAX_CONTAINED_RESOURCES && ret == OC_STACK_OK; i++) { OCResource* temp = collResource->rsrcResources[i]; if (temp) { //TODO : Add resource type filtering once collections // start supporting queries. ret = BuildResponseRepresentation(temp, &payload); } } } if(ret == OC_STACK_OK) { OCEntityHandlerResponse response = {0}; response.ehResult = OC_EH_OK; response.payload = (OCPayload*)payload; response.persistentBufferFlag = 0; response.requestHandle = (OCRequestHandle) ehRequest->requestHandle; response.resourceHandle = (OCResourceHandle) collResource; ret = OCDoResponse(&response); } OCRepPayloadDestroy(payload); return ret; }
OCStackApplicationResult ActionSetCB(void* context, OCDoHandle handle, OCClientResponse* clientResponse) { printf("\n\n\tcallback is called\n\n"); ClientRequstInfo *info = GetClientRequestInfo(clientRequstList, handle); if (info) { int idx; unsigned char *responseJson; responseJson = (unsigned char *) OCMalloc( (unsigned int) (strlen((char *) clientResponse->resJSONPayload) + 1)); // We need the body of response. // Copy the body from the response strcpy((char *) responseJson, ((char *) clientResponse->resJSONPayload + OC_JSON_PREFIX_LEN)); idx = strlen((char *) responseJson) - OC_JSON_SUFFIX_LEN; // And insert NULL at the end of body. (responseJson[idx]) = 0; OCEntityHandlerResponse response = { 0 }; response.ehResult = OC_EH_OK; response.payload = responseJson; response.payloadSize = (unsigned int) strlen((char *) responseJson) + 1; response.persistentBufferFlag = 0; response.requestHandle = (OCRequestHandle) info->ehRequest; response.resourceHandle = (OCResourceHandle) info->collResource; OCDoResponse(&response); RemoveClientRequestInfo(&clientRequstList, info); OCFree(responseJson); } // g_AggregateResponseHandle return OC_STACK_KEEP_TRANSACTION; }
OCEntityHandlerResult send_response(OCRequestHandle request_handle, OCResourceHandle resource, OCEntityHandlerResult result, const char *uri, void *payload) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); OCEntityHandlerResult eh_result = OC_EH_OK; OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, {}, {0}, false }; THINGS_LOG_D(TAG, "Creating Response with Request Handle : %x,Resource Handle : %x", request_handle, resource); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof(response.resourceUri)); if (NULL != request_handle && NULL != resource) { response.requestHandle = request_handle; //eh_request->request_handle; response.resourceHandle = resource; //eh_request->resource; response.persistentBufferFlag = 0; response.ehResult = result; if (payload != NULL) { THINGS_LOG_D(TAG, "Payload is Not NULL"); response.payload = (OCPayload *) payload; } else { THINGS_LOG_D(TAG, "No Payload"); response.payload = NULL; } THINGS_LOG_V(TAG, "\t\t\tRes. : %s ( %d )", (response.ehResult == OC_EH_OK ? "NORMAL" : "ERROR"), response.ehResult); OCStackResult ret = OCDoResponse(&response); THINGS_LOG_V(TAG, "\t\t\tMsg. Out? : (%s)", (ret == OC_STACK_OK) ? "SUCCESS" : "FAIL"); if (ret != OC_STACK_OK) { eh_result = OC_EH_ERROR; } } else { eh_result = OC_EH_ERROR; } // THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return eh_result; }
OCEntityHandlerResult OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest, char* uri) { OC_LOG_V (INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri); OCEntityHandlerResult ehResult = OC_EH_OK; OCEntityHandlerResponse response; char payload[MAX_RESPONSE_LENGTH] = {0}; // Validate pointer if (!entityHandlerRequest) { OC_LOG (ERROR, TAG, "Invalid request pointer"); return OC_EH_ERROR; } // Initialize certain response fields response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); if (flag & OC_INIT_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG"); } if (flag & OC_REQUEST_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (entityHandlerRequest->resource == NULL) { OC_LOG (INFO, TAG, "Received request from client to a non-existing resource"); ehResult = ProcessNonExistingResourceRequest(entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_DELETE == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client"); ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else { OC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); ehResult = OC_EH_ERROR; } // If the result isn't an error or forbidden, send response if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN))) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; response.payload = (unsigned char *)payload; response.payloadSize = strlen(payload); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehResult = OC_EH_ERROR; } } } if (flag & OC_OBSERVE_FLAG) { OC_LOG(INFO, TAG, "Flag includes OC_OBSERVE_FLAG"); if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_REGISTER from client"); } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_DEREGISTER from client"); } } return ehResult; }
OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest) { OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag); OCEntityHandlerResult ehResult = OC_EH_OK; OCEntityHandlerResponse response; char payload[MAX_RESPONSE_LENGTH] = {0}; // Validate pointer if (!entityHandlerRequest) { OC_LOG (ERROR, TAG, "Invalid request pointer"); return OC_EH_ERROR; } // Initialize certain response fields response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); if (flag & OC_INIT_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG"); } if (flag & OC_REQUEST_FLAG) { OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else if (OC_REST_POST == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_POST from client"); ehResult = ProcessPostRequest (entityHandlerRequest, &response, payload, sizeof(payload) - 1); } else if (OC_REST_DELETE == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client"); ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } else { OC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); } // If the result isn't an error or forbidden, send response if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN))) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; response.payload = (unsigned char *)payload; response.payloadSize = strlen(payload); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Handle vendor specific options if(entityHandlerRequest->rcvdVendorSpecificHeaderOptions && entityHandlerRequest->numRcvdVendorSpecificHeaderOptions) { OC_LOG (INFO, TAG, "Received vendor specific options"); uint8_t i = 0; OCHeaderOption * rcvdOptions = entityHandlerRequest->rcvdVendorSpecificHeaderOptions; for( i = 0; i < entityHandlerRequest->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, ((OCHeaderOption)rcvdOptions[i]).optionLength); } } OCHeaderOption * sendOptions = response.sendVendorSpecificHeaderOptions; uint8_t option2[] = {21,22,23,24,25,26,27,28,29,30}; uint8_t option3[] = {31,32,33,34,35,36,37,38,39,40}; sendOptions[0].protocolID = OC_COAP_ID; sendOptions[0].optionID = 2248; memcpy(sendOptions[0].optionData, option2, sizeof(option2)); sendOptions[0].optionLength = 10; sendOptions[1].protocolID = OC_COAP_ID; sendOptions[1].optionID = 2600; memcpy(sendOptions[1].optionData, option3, sizeof(option3)); sendOptions[1].optionLength = 10; response.numSendVendorSpecificHeaderOptions = 2; } // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehResult = OC_EH_ERROR; } } } if (flag & OC_OBSERVE_FLAG) { OC_LOG(INFO, TAG, "Flag includes OC_OBSERVE_FLAG"); if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_REGISTER from client"); ProcessObserveRegister (entityHandlerRequest); } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, "Received OC_OBSERVE_DEREGISTER from client"); ProcessObserveDeregister (entityHandlerRequest); } } return ehResult; }
static OCStackResult HandleBatchInterface(OCEntityHandlerRequest *ehRequest) { if (!ehRequest) { return OC_STACK_INVALID_PARAM; } OCResource * collResource = (OCResource *) ehRequest->resource; OCRepPayload* payload = OCRepPayloadCreate(); if (!payload) { return OC_STACK_NO_MEMORY; } if (collResource) { OCRepPayloadSetUri(payload, collResource->uri); } OCEntityHandlerResponse response = {0}; response.ehResult = OC_EH_OK; response.payload = (OCPayload*)payload; response.persistentBufferFlag = 0; response.requestHandle = (OCRequestHandle) ehRequest->requestHandle; response.resourceHandle = (OCResourceHandle) collResource; OCStackResult stackRet = OCDoResponse(&response); if (stackRet == OC_STACK_OK) { for (uint8_t i = 0; i < MAX_CONTAINED_RESOURCES; i++) { OCResource* temp = collResource->rsrcResources[i]; if (temp) { // Note that all entity handlers called through a collection // will get the same pointer to ehRequest, the only difference // is ehRequest->resource ehRequest->resource = (OCResourceHandle) temp; OCEntityHandlerResult ehResult = temp->entityHandler(OC_REQUEST_FLAG, ehRequest, temp->entityHandlerCallbackParam); // The default collection handler is returning as OK if (stackRet != OC_STACK_SLOW_RESOURCE) { stackRet = OC_STACK_OK; } // if a single resource is slow, then entire response will be treated // as slow response if (ehResult == OC_EH_SLOW) { OC_LOG(INFO, TAG, "This is a slow resource"); ((OCServerRequest *)ehRequest->requestHandle)->slowFlag = 1; stackRet = EntityHandlerCodeToOCStackCode(ehResult); } } else { break; } } ehRequest->resource = (OCResourceHandle) collResource; } return stackRet; }
// This is the entity handler for the registered resource. // This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; char payload[MAX_RESPONSE_LENGTH] = {0}; if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { size_t responsePayloadGetLength = strlen(responsePayloadGet); if (responsePayloadGetLength < (sizeof(payload) - 1)) { strncpy(payload, responsePayloadGet, responsePayloadGetLength); } else { ehRet = OC_EH_ERROR; } } else if(OC_REST_PUT == entityHandlerRequest->method) { //Do something with the 'put' payload size_t responsePayloadPutLength = strlen(responsePayloadPut); if (responsePayloadPutLength < (sizeof(payload) - 1)) { strncpy((char *)payload, responsePayloadPut, responsePayloadPutLength); } else { ehRet = OC_EH_ERROR; } } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; response.payload = (unsigned char *)payload; response.payloadSize = strlen(payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) { if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client")); } } return ehRet; }
// This is the entity handler for the registered resource. // This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload")); return OC_EH_ERROR; } if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { OCRepPayloadSetUri(payload, "/a/light"); OCRepPayloadSetPropBool(payload, "state", true); OCRepPayloadSetPropInt(payload, "power", 10); } else if(OC_REST_PUT == entityHandlerRequest->method) { //Do something with the 'put' payload OCRepPayloadSetUri(payload, "/a/light"); OCRepPayloadSetPropBool(payload, "state", false); OCRepPayloadSetPropInt(payload, "power", 0); } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; response.payload = (OCPayload*) payload; response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) { if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client")); } } return ehRet; }
/** * Entity handler callback that fills the resPayload of the entityHandlerRequest. */ OCEntityHandlerResult PluginInterfaceEntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void* callbackParam) { if (!entityHandlerRequest) { OIC_LOG (ERROR, TAG, "Invalid request pointer"); return OC_EH_ERROR; } OCEntityHandlerResult ehResult = OC_EH_ERROR; OCStackResult result = OC_STACK_ERROR; PIPluginBase * plugin = (PIPluginBase *) callbackParam; OCEntityHandlerResponse * response = (OCEntityHandlerResponse *) OICCalloc(1, sizeof(*response)); if (!response) { return OC_EH_ERROR; } OCRepPayload* payload = (OCRepPayload *) entityHandlerRequest->payload; if (flag & OC_REQUEST_FLAG) { if (plugin->processEHRequest) { ehResult = plugin->processEHRequest(plugin, entityHandlerRequest, &payload); } } // If the result isn't an error or forbidden, send response if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN))) { // Format the response. Note this requires some info about the request response->requestHandle = entityHandlerRequest->requestHandle; response->resourceHandle = entityHandlerRequest->resource; response->ehResult = ehResult; response->payload = (OCPayload*) payload; // Indicate that response is NOT in a persistent buffer response->persistentBufferFlag = 0; result = OCDoResponse(response); if (result != OC_STACK_OK) { OIC_LOG_V(ERROR, TAG, "Error sending response %u", result); ehResult = OC_EH_ERROR; } } else { OIC_LOG_V(ERROR, TAG, "Error handling request %u", ehResult); PIResource * piResource = NULL; result = GetResourceFromHandle(plugin, &piResource, response->resourceHandle); OIC_LOG_V(ERROR, TAG, "Deleting resource \"%s\" because of failed request.", piResource->uri); result = DeleteResource(plugin, piResource); if(result != OC_STACK_OK) { OIC_LOG_V(ERROR, TAG, "Failed to delete resource after failed request."); ehResult = OC_EH_ERROR; } } OCPayloadDestroy(response->payload); OICFree(response); return ehResult; }
/** * This is the entity handler for the registered resource. * This is invoked by OCStack whenever it recevies a request for this resource. */ OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest* entityHandlerRequest, void *callback) { (void) callback; OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { }, { 0 }, false }; OCRepPayload* payload = NULL; if (entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { if (OC_REST_GET == entityHandlerRequest->method) { OIC_LOG(INFO, ES_RH_TAG, "Received GET request"); ehRet = ProcessGetRequest(entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OIC_LOG(INFO, ES_RH_TAG, "Received PUT request"); //PUT request will be handled in the internal implementation if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle) { ehRet = ProcessPutRequest(entityHandlerRequest, &payload); } else { OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put"); ehRet = OC_EH_ERROR; } } else if (OC_REST_POST == entityHandlerRequest->method) { OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client"); if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle) { ehRet = ProcessPostRequest(entityHandlerRequest, &payload); } else { OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put"); ehRet = OC_EH_ERROR; } } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; //response uses OCPaylod while all get,put methodes use OCRepPayload response.payload = (OCPayload*) (payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof(response.sendVendorSpecificHeaderOptions)); memset(response.resourceUri, 0, sizeof(response.resourceUri)); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OIC_LOG(ERROR, ES_RH_TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } return ehRet; }
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { //OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload")); return OC_EH_ERROR; } if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { //OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { OCRepPayloadSetUri(payload, "/iotar/speaker"); OCRepPayloadSetPropInt(payload, "state", speaker.state); OCRepPayloadSetPropInt(payload, "volume",speaker.volume); OCRepPayloadSetPropInt(payload, "time", speaker.time); OCRepPayloadSetPropInt(payload, "present_song",speaker.present_song); OCRepPayloadSetPropInt(payload, "next_song", speaker.next_song); } else if(OC_REST_PUT == entityHandlerRequest->method) { //OC_LOG_V(DEBUG, TAG, "put \n"); //풋 올때 데이터 어떻게 받지?? OCRepPayload* pay = (OCRepPayload*) entityHandlerRequest-> payload; int64_t order = 0; int64_t temp = 0; char in[10]; OCRepPayloadGetPropInt(pay, "order", &order); if(order == 30){ //재생 OCRepPayloadGetPropInt(pay, "temp", &temp); makeString('a', temp, (char*)&in); speaker.present_song = temp; Serial1.println(in); millis_prv = millis(); save_time = 0; speaker.state = 1; }else if(order == 31){ //볼륨 설정 OCRepPayloadGetPropInt(pay, "temp", &temp); makeString('v', temp, (char*)&in); speaker.volume = temp; Serial1.println(in); }else if(order == 32){ //다음 노래 설정 OCRepPayloadGetPropInt(pay, "temp", &temp); makeString('d', temp, (char*)&in); speaker.next_song = temp; Serial1.println(in); }else if(order == 33){ // 현재 아무일 안하고 있으면 0, 재생중 1, 일시정지 2. 로 세팅. Serial1.println('x'); }else if(order == 34) { //정지. Serial1.println('s'); }else if(order == 35) { // +1 볼륨. speaker.volume++; if(speaker.volume >= 100){ speaker.volume = 100; } makeString('v', speaker.volume, (char*)&in); Serial1.println(in); }else if(order == 36) { // -1 볼륨. speaker.volume--; if(speaker.volume < 1){ speaker.volume = 1; } makeString('v', speaker.volume, (char*)&in); Serial1.println(in); }else if(order == 37){ // 일시정지, 일시정지 풀기. Serial1.println('p'); if(speaker.state == 1){ speaker.state = 2; save_time = speaker.time+2; }else{ speaker.state = 1; millis_prv = millis(); } }else if(order == 38){ //다음노래 재생 speaker.present_song++; if(speaker.present_song > MAX_SONG){ speaker.present_song = 1; } makeString('a', speaker.present_song, (char*)&in); Serial1.println(in); millis_prv = millis(); save_time = 0; speaker.state = 1; }else if(order == 39){ // 이전 노래 재생 speaker.present_song--; if(speaker.present_song < 1){ speaker.present_song = MAX_SONG; } makeString('a', speaker.present_song, (char*)&in); Serial1.println(in); millis_prv = millis(); save_time = 0; speaker.state = 1; } OCRepPayloadSetUri(payload, "/iotar/speaker"); OCRepPayloadSetPropInt(payload, "time", speaker.time); } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; response.payload = (OCPayload*) payload; response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { // OC_LOG(ERROR, TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) { if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { // OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { // OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client")); gLightUnderObservation = 0; } } OCRepPayloadDestroy(payload); return ehRet; }
OCStackResult SendListObserverNotification (OCResource * resource, OCObservationId *obsIdList, uint8_t numberOfIds, unsigned char *notificationJSONPayload, uint32_t maxAge, OCQualityOfService qos) { uint8_t numIds = numberOfIds; ResourceObserver *observation = NULL; uint8_t numSentNotification = 0; OCServerRequest * request = NULL; OCStackResult result = OC_STACK_ERROR; OCEntityHandlerResponse ehResponse = {0}; OC_LOG(INFO, TAG, PCF("Entering SendListObserverNotification")); while(numIds) { OC_LOG_V(INFO, TAG, "Need to notify observation id %d", *obsIdList); observation = NULL; observation = GetObserverUsingId (*obsIdList); if(observation) { // Found observation - verify if it matches the resource handle if (observation->resource == resource) { qos = DetermineObserverQoS(OC_REST_GET, observation, qos); result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET, 0, resource->sequenceNum, qos, observation->query, NULL, NULL, &observation->token, observation->addr, observation->resUri, 0); request->observeResult = OC_STACK_OK; if(request && result == OC_STACK_OK) { memset(&ehResponse, 0, sizeof(OCEntityHandlerResponse)); ehResponse.ehResult = OC_EH_OK; ehResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH); if(!ehResponse.payload) { FindAndDeleteServerRequest(request); continue; } strcpy((char *)ehResponse.payload, (const char *)notificationJSONPayload); ehResponse.payloadSize = strlen((const char *)ehResponse.payload) + 1; ehResponse.persistentBufferFlag = 0; ehResponse.requestHandle = (OCRequestHandle) request; ehResponse.resourceHandle = (OCResourceHandle) resource; result = OCDoResponse(&ehResponse); if(result == OC_STACK_OK) { OCFree(ehResponse.payload); FindAndDeleteServerRequest(request); } } else { FindAndDeleteServerRequest(request); } numSentNotification++; } } obsIdList++; numIds--; } if(numSentNotification == numberOfIds) { return OC_STACK_OK; } else if(numSentNotification == 0) { return OC_STACK_NO_OBSERVERS; } else { //TODO: we need to signal that not every one in the // list got an update, should we also indicate who did not receive on? return OC_STACK_OK; } }
OCStackResult BuildCollectionGroupActionJSONResponse(OCMethod method/*OCEntityHandlerFlag flag*/, OCResource *resource, OCEntityHandlerRequest *ehRequest) { OCStackResult stackRet = OC_STACK_ERROR; OC_LOG(INFO, TAG, PCF("Group Action is requested.")); // if (stackRet == OC_STACK_OK) { char *doWhat = NULL; char *details = NULL; size_t bufferLength = 0; unsigned char buffer[MAX_RESPONSE_LENGTH] = { 0 }; unsigned char *bufferPtr = NULL; bufferPtr = buffer; OCResource * collResource = (OCResource *) ehRequest->resource; char *jsonResponse; ExtractKeyValueFromRequest((char *)ehRequest->reqJSONPayload, &doWhat, &details); cJSON *json; cJSON *format; if (method == OC_REST_PUT) { json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "href", resource->uri); cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject()); OC_LOG(INFO, TAG, PCF("Group Action[PUT].")); if(strcmp(doWhat, "ActionSet") == 0) { OCActionSet *actionSet; BuildActionSetFromString(&actionSet, details); if(actionSet != NULL) { AddActionSet(&resource->actionsetHead, actionSet); stackRet = OC_STACK_OK; } else { stackRet = OC_STACK_ERROR; } } else if (strcmp(doWhat, "DelActionSet") == 0) { if (FindAndDeleteActionSet(&resource, details) == OC_STACK_OK) { stackRet = OC_STACK_OK; } else { stackRet = OC_STACK_ERROR; } } jsonResponse = cJSON_Print(json); cJSON_Delete(json); strcat((char *) bufferPtr, jsonResponse); bufferLength = strlen((const char *) buffer); if (bufferLength > 0) { OCEntityHandlerResponse response = { 0 }; response.ehResult = OC_EH_OK; response.payload = buffer; response.payloadSize = bufferLength + 1; response.persistentBufferFlag = 0; response.requestHandle = (OCRequestHandle) ehRequest->requestHandle; response.resourceHandle = (OCResourceHandle) collResource; stackRet = OCDoResponse(&response); } stackRet = OC_STACK_OK; } if (method == OC_REST_POST) { OC_LOG(INFO, TAG, PCF("Group Action[POST].")); OCActionSet *actionset = NULL; json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "href", resource->uri); if (strcmp(doWhat, "DoAction") == 0) { if (GetActionSet(details, resource->actionsetHead, &actionset) != OC_STACK_OK) { OC_LOG(INFO, TAG, PCF("ERROR")); stackRet = OC_STACK_ERROR; } if (actionset == NULL) { OC_LOG(INFO, TAG, PCF("ERROR")); stackRet = OC_STACK_ERROR; } else { OCAction *pointerAction = actionset->head; unsigned int num = GetNumOfTargetResource(pointerAction); ((OCServerRequest *) ehRequest->requestHandle)->ehResponseHandler = HandleAggregateResponse; ((OCServerRequest *) ehRequest->requestHandle)->numResponses = num + 1; while (pointerAction != NULL) { unsigned char actionDesc[MAX_RESPONSE_LENGTH] = { 0 }; unsigned char* actionDescPtr = actionDesc; uint16_t remaining = MAX_RESPONSE_LENGTH; strncpy((char *) actionDescPtr, (const char *) OC_JSON_PREFIX, strlen((const char *) OC_JSON_PREFIX) + 1); BuildActionJSON(pointerAction, actionDescPtr, &remaining); strncat((char *) actionDescPtr, (const char *) OC_JSON_SUFFIX, strlen((const char *) OC_JSON_SUFFIX)); ClientRequstInfo *info = (ClientRequstInfo *) OCMalloc( sizeof(ClientRequstInfo)); memset(info, 0, sizeof(ClientRequstInfo)); info->collResource = resource; info->ehRequest = (OCServerRequest *) ehRequest->requestHandle; SendAction(&info->required, pointerAction->resourceUri, actionDescPtr); AddClientRequestInfo(&clientRequstList, info); pointerAction = pointerAction->next; } stackRet = OC_STACK_OK; } } else if (strcmp(doWhat, "GetActionSet") == 0) { char *plainText = NULL; OCActionSet *actionset = NULL; cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject()); GetActionSet(details, resource->actionsetHead, &actionset); if (actionset != NULL) { BuildStringFromActionSet(actionset, &plainText); if (plainText != NULL) { cJSON_AddStringToObject(format, "ActionSet", plainText); } stackRet = OC_STACK_OK; } } jsonResponse = cJSON_Print(json); cJSON_Delete(json); strcat((char *) bufferPtr, jsonResponse); bufferLength = strlen((const char *) buffer); if (bufferLength > 0) { OCEntityHandlerResponse response = { 0 }; response.ehResult = OC_EH_OK; response.payload = buffer; response.payloadSize = bufferLength + 1; response.persistentBufferFlag = 0; response.requestHandle = (OCRequestHandle) ehRequest->requestHandle; response.resourceHandle = (OCResourceHandle) collResource; stackRet = OCDoResponse(&response); } } OCFree(doWhat); OCFree(details); } return stackRet; }
OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest) { printf ("Inside entity handler - flags: 0x%x\n", flag); OCEntityHandlerResult ehResult = OC_EH_OK; OCEntityHandlerResponse response; // char payload[MAX_RESPONSE_LENGTH] = {0}; std::string payload; // Validate pointer if (!entityHandlerRequest) { printf ("Invalid request pointer\n"); return OC_EH_ERROR; } // Initialize certain response fields response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); if (flag & OC_INIT_FLAG) { printf ("Flag includes OC_INIT_FLAG\n"); } if (flag & OC_REQUEST_FLAG) { printf ("Flag includes OC_REQUEST_FLAG\n"); /* * GET REQUEST HANDLER */ if (OC_REST_GET == entityHandlerRequest->method) { payload = "{\"href\":\"/a/light\",\"rep\":{\"state\":\"off\",\"power\":5}}"; printf ("Received OC_REST_GET from client (%s)\n", payload.c_str()); } /* * PUT REQUEST HANDLER */ if (OC_REST_PUT == entityHandlerRequest->method) { printf ("Received OC_REST_PUT from client (%s)\n", payload.c_str()); } /* * GENRATE RESPONE MESSAGE TO THE CLIENT */ if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN))) { printf ("Sendig RESPONSE\n"); // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; response.payload = (unsigned char *)payload.c_str(); response.payloadSize = strlen(payload.c_str()); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { printf ("Error sending response\n"); ehResult = OC_EH_ERROR; } } } return ehResult; }
OCStackResult SendListObserverNotification (OCResource * resource, OCObservationId *obsIdList, uint8_t numberOfIds, const OCRepPayload *payload, uint32_t maxAge, OCQualityOfService qos) { (void)maxAge; if(!resource || !obsIdList || !payload) { return OC_STACK_INVALID_PARAM; } uint8_t numIds = numberOfIds; ResourceObserver *observer = NULL; uint8_t numSentNotification = 0; OCServerRequest * request = NULL; OCStackResult result = OC_STACK_ERROR; bool observeErrorFlag = false; OC_LOG(INFO, TAG, "Entering SendListObserverNotification"); while(numIds) { observer = GetObserverUsingId (*obsIdList); if(observer) { // Found observer - verify if it matches the resource handle if (observer->resource == resource) { qos = DetermineObserverQoS(OC_REST_GET, observer, qos); result = AddServerRequest(&request, 0, 0, 1, OC_REST_GET, 0, resource->sequenceNum, qos, observer->query, NULL, NULL, observer->token, observer->tokenLength, observer->resUri, 0, observer->acceptFormat, &observer->devAddr); if(request) { request->observeResult = OC_STACK_OK; if(result == OC_STACK_OK) { OCEntityHandlerResponse ehResponse = {0}; ehResponse.ehResult = OC_EH_OK; ehResponse.payload = (OCPayload*)OCRepPayloadCreate(); if(!ehResponse.payload) { FindAndDeleteServerRequest(request); continue; } memcpy(ehResponse.payload, payload, sizeof(*payload)); ehResponse.persistentBufferFlag = 0; ehResponse.requestHandle = (OCRequestHandle) request; ehResponse.resourceHandle = (OCResourceHandle) resource; result = OCDoResponse(&ehResponse); if(result == OC_STACK_OK) { OC_LOG_V(INFO, TAG, "Observer id %d notified.", *obsIdList); // Increment only if OCDoResponse is successful numSentNotification++; OICFree(ehResponse.payload); FindAndDeleteServerRequest(request); } else { OC_LOG_V(INFO, TAG, "Error notifying observer id %d.", *obsIdList); } } else { FindAndDeleteServerRequest(request); } } // Since we are in a loop, set an error flag to indicate // at least one error occurred. if (result != OC_STACK_OK) { observeErrorFlag = true; } } } obsIdList++; numIds--; } if(numSentNotification == numberOfIds && !observeErrorFlag) { return OC_STACK_OK; } else if(numSentNotification == 0) { return OC_STACK_NO_OBSERVERS; } else { OC_LOG(ERROR, TAG, "Observer notification error"); return OC_STACK_ERROR; } }
OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest, void* callbackParam) { OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag); (void)callbackParam; OCEntityHandlerResult ehResult = OC_EH_ERROR; OCEntityHandlerResponse response; memset(&response, 0, sizeof(response)); // Validate pointer if (!entityHandlerRequest) { OIC_LOG (ERROR, TAG, "Invalid request pointer"); return OC_EH_ERROR; } OCRepPayload* payload = NULL; if (flag & OC_REQUEST_FLAG) { OIC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (entityHandlerRequest) { if (OC_REST_GET == entityHandlerRequest->method) { OIC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ehResult = ProcessGetRequest (entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); ehResult = ProcessPutRequest (entityHandlerRequest, &payload); } else if (OC_REST_POST == entityHandlerRequest->method) { OIC_LOG (INFO, TAG, "Received OC_REST_POST from client"); ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload); } else { OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); ehResult = OC_EH_ERROR; } if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; response.payload = (OCPayload*)(payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof(response.sendVendorSpecificHeaderOptions)); memset(response.resourceUri, 0, sizeof(response.resourceUri)); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "Error sending response"); ehResult = OC_EH_ERROR; } } } } OCPayloadDestroy(response.payload); return ehResult; }
OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge, OCQualityOfService qos) #endif { OC_LOG(INFO, TAG, PCF("Entering SendObserverNotification")); OCStackResult result = OC_STACK_ERROR; ResourceObserver * resourceObserver = serverObsList; uint8_t numObs = 0; OCServerRequest * request = NULL; OCEntityHandlerRequest ehRequest = {0}; OCEntityHandlerResult ehResult = OC_EH_ERROR; // Find clients that are observing this resource while (resourceObserver) { if (resourceObserver->resource == resPtr) { numObs++; #ifdef WITH_PRESENCE if(method != OC_REST_PRESENCE) { #endif qos = DetermineObserverQoS(method, resourceObserver, qos); result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET, 0, resPtr->sequenceNum, qos, resourceObserver->query, NULL, NULL, &resourceObserver->token, resourceObserver->addr, resourceObserver->resUri, 0); request->observeResult = OC_STACK_OK; if(request && result == OC_STACK_OK) { result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request, request->method, (OCResourceHandle) resPtr, request->query, request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions, request->rcvdVendorSpecificHeaderOptions, OC_OBSERVE_NO_OPTION, 0); if(result == OC_STACK_OK) { ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest); if(ehResult == OC_EH_ERROR) { FindAndDeleteServerRequest(request); } } } #ifdef WITH_PRESENCE } else { OCEntityHandlerResponse ehResponse = {0}; unsigned char presenceResBuf[MAX_RESPONSE_LENGTH] = {0}; //This is effectively the implementation for the presence entity handler. OC_LOG(DEBUG, TAG, PCF("This notification is for Presence")); result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET, 0, OC_OBSERVE_NO_OPTION, OC_LOW_QOS, NULL, NULL, NULL, &resourceObserver->token, resourceObserver->addr, resourceObserver->resUri, 0); if(result == OC_STACK_OK) { // we create the payload here if(resourceType && resourceType->resourcetypename) { snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u:%s", resPtr->sequenceNum, maxAge, resourceType->resourcetypename); } else { snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u", resPtr->sequenceNum, maxAge); } ehResponse.ehResult = OC_EH_OK; ehResponse.payload = presenceResBuf; ehResponse.payloadSize = strlen((const char *)presenceResBuf) + 1; ehResponse.persistentBufferFlag = 0; ehResponse.requestHandle = (OCRequestHandle) request; ehResponse.resourceHandle = (OCResourceHandle) resPtr; strcpy((char *)ehResponse.resourceUri, (const char *)resourceObserver->resUri); result = OCDoResponse(&ehResponse); } } #endif } resourceObserver = resourceObserver->next; } if (numObs == 0) { OC_LOG(INFO, TAG, PCF("Resource has no observers")); result = OC_STACK_NO_OBSERVERS; } return result; }
// This is the entity handler for the registered resource. // This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest* entityHandlerRequest, void *callback) { (void) callback; OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = { 0 }; OCRepPayload* payload = NULL; if (entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG(INFO, ES_RH_TAG, "Received GET request"); ehRet = ProcessGetRequest(entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG(INFO, ES_RH_TAG, "Received PUT request"); if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle) { ehRet = ProcessPutRequest(entityHandlerRequest, &payload); } else { ehRet = OC_EH_ERROR; } } else if (OC_REST_POST == entityHandlerRequest->method) { // TODO: As of now, POST request will be not received. OC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client"); //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1); } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; //response uses OCPaylod while all get,put methodes use OCRepPayload response.payload = (OCPayload*) (payload); response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, ES_RH_TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (g_flag == 1) { g_cbForResEvent(ES_RECVTRIGGEROFPROVRES); g_flag = 0; } return ehRet; }
OCStackResult InProcServerWrapper::sendResponse( const std::shared_ptr<OCResourceResponse> pResponse) { auto cLock = m_csdkLock.lock(); OCStackResult result = OC_STACK_ERROR; if(!pResponse) { result = OC_STACK_MALFORMED_RESPONSE; throw OCException(OC::Exception::STR_NULL_RESPONSE, OC_STACK_MALFORMED_RESPONSE); } else { OCEntityHandlerResponse response; // OCRepPayload* payLoad = pResponse->getPayload(); HeaderOptions serverHeaderOptions = pResponse->getHeaderOptions(); response.requestHandle = pResponse->getRequestHandle(); response.resourceHandle = pResponse->getResourceHandle(); response.ehResult = pResponse->getResponseResult(); response.payload = reinterpret_cast<OCPayload*>(pResponse->getPayload()); response.persistentBufferFlag = 0; response.numSendVendorSpecificHeaderOptions = serverHeaderOptions.size(); int i = 0; for (auto it=serverHeaderOptions.begin(); it != serverHeaderOptions.end(); ++it) { response.sendVendorSpecificHeaderOptions[i].protocolID = OC_COAP_ID; response.sendVendorSpecificHeaderOptions[i].optionID = static_cast<uint16_t>(it->getOptionID()); response.sendVendorSpecificHeaderOptions[i].optionLength = (it->getOptionData()).length() + 1; std::string optionData = it->getOptionData(); std::copy(optionData.begin(), optionData.end(), response.sendVendorSpecificHeaderOptions[i].optionData); response.sendVendorSpecificHeaderOptions[i].optionData[it->getOptionData().length()] = '\0'; i++; } if(OC_EH_RESOURCE_CREATED == response.ehResult) { pResponse->getNewResourceUri().copy(response.resourceUri, sizeof (response.resourceUri) - 1); response.resourceUri[pResponse->getNewResourceUri().length()] = '\0'; } if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCDoResponse(&response); } else { OICFree(response.payload); result = OC_STACK_ERROR; } if(result != OC_STACK_OK) { oclog() << "Error sending response\n"; } return result; } }
OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge, OCQualityOfService qos) #endif { OC_LOG(INFO, TAG, "Entering SendObserverNotification"); if(!resPtr) { return OC_STACK_INVALID_PARAM; } OCStackResult result = OC_STACK_ERROR; ResourceObserver * resourceObserver = serverObsList; uint8_t numObs = 0; OCServerRequest * request = NULL; OCEntityHandlerRequest ehRequest = {0}; OCEntityHandlerResult ehResult = OC_EH_ERROR; bool observeErrorFlag = false; // Find clients that are observing this resource while (resourceObserver) { if (resourceObserver->resource == resPtr) { numObs++; #ifdef WITH_PRESENCE if(method != OC_REST_PRESENCE) { #endif qos = DetermineObserverQoS(method, resourceObserver, qos); result = AddServerRequest(&request, 0, 0, 1, OC_REST_GET, 0, resPtr->sequenceNum, qos, resourceObserver->query, NULL, NULL, resourceObserver->token, resourceObserver->tokenLength, resourceObserver->resUri, 0, resourceObserver->acceptFormat, &resourceObserver->devAddr); if(request) { request->observeResult = OC_STACK_OK; if(result == OC_STACK_OK) { result = FormOCEntityHandlerRequest( &ehRequest, (OCRequestHandle) request, request->method, &request->devAddr, (OCResourceHandle) resPtr, request->query, PAYLOAD_TYPE_REPRESENTATION, request->payload, request->payloadSize, request->numRcvdVendorSpecificHeaderOptions, request->rcvdVendorSpecificHeaderOptions, OC_OBSERVE_NO_OPTION, 0); if(result == OC_STACK_OK) { ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest, resPtr->entityHandlerCallbackParam); if(ehResult == OC_EH_ERROR) { FindAndDeleteServerRequest(request); } } OCPayloadDestroy(ehRequest.payload); } } #ifdef WITH_PRESENCE } else { OCEntityHandlerResponse ehResponse = {0}; //This is effectively the implementation for the presence entity handler. OC_LOG(DEBUG, TAG, "This notification is for Presence"); result = AddServerRequest(&request, 0, 0, 1, OC_REST_GET, 0, resPtr->sequenceNum, qos, resourceObserver->query, NULL, NULL, resourceObserver->token, resourceObserver->tokenLength, resourceObserver->resUri, 0, resourceObserver->acceptFormat, &resourceObserver->devAddr); if(result == OC_STACK_OK) { OCPresencePayload* presenceResBuf = OCPresencePayloadCreate( resPtr->sequenceNum, maxAge, trigger, resourceType ? resourceType->resourcetypename : NULL); if(!presenceResBuf) { return OC_STACK_NO_MEMORY; } if(result == OC_STACK_OK) { ehResponse.ehResult = OC_EH_OK; ehResponse.payload = (OCPayload*)presenceResBuf; ehResponse.persistentBufferFlag = 0; ehResponse.requestHandle = (OCRequestHandle) request; ehResponse.resourceHandle = (OCResourceHandle) resPtr; OICStrcpy(ehResponse.resourceUri, sizeof(ehResponse.resourceUri), resourceObserver->resUri); result = OCDoResponse(&ehResponse); } OCPresencePayloadDestroy(presenceResBuf); } } #endif // Since we are in a loop, set an error flag to indicate at least one error occurred. if (result != OC_STACK_OK) { observeErrorFlag = true; } } resourceObserver = resourceObserver->next; } if (numObs == 0) { OC_LOG(INFO, TAG, "Resource has no observers"); result = OC_STACK_NO_OBSERVERS; } else if (observeErrorFlag) { OC_LOG(ERROR, TAG, "Observer notification error"); result = OC_STACK_ERROR; } return result; }