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; }
// 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}; // MAX_RESPONSE_LENGTH for arduino is 256, for demo 64 is enough char payload[64] = {0}; if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { char *getResp = constructJsonResponse(entityHandlerRequest); size_t responsePayloadGetLength = strlen(getResp); if (responsePayloadGetLength < (sizeof(payload) - 1)) { strncpy(payload, getResp, responsePayloadGetLength); } else { ehRet = OC_EH_ERROR; } free(getResp); } else if(OC_REST_PUT == entityHandlerRequest->method) { //Do something with the 'put' payload char *putResp = constructJsonResponse(entityHandlerRequest); size_t responsePayloadPutLength = strlen(putResp); if (responsePayloadPutLength < (sizeof(payload) - 1)) { strncpy((char *)payload, putResp, responsePayloadPutLength); } else { ehRet = OC_EH_ERROR; } free(putResp); } 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")); ProcessObserveRegister(entityHandlerRequest); } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client")); ProcessObserveDeregister(entityHandlerRequest); } } return ehRet; }