MyErrorCode HttpConnect::ProcessRequest(const HttpRequest& cRequest, HttpResponse& cResponse) { MyErrorCode eResult = E_INVALID_REQ; switch (cRequest.m_eReqType) { case HttpRequest::HTTP_GET: eResult = ProcessGetRequest(cRequest, cResponse); break; case HttpRequest::HTTP_POST: eResult = ProcessPostRequest(cRequest, cResponse); break; default: eResult = E_INVALID_REQ; break; } #ifdef _DEBUG std::cout << "Request url:" << std::endl << "\t" << cRequest.m_strUrl << std::endl << "Response:" << std::endl << "\t" << cResponse.m_strResponse << std::endl << std::endl; #endif return eResult; }
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; }
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, 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; }