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); }
OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest, char *payload, uint16_t maxPayloadSize) { OCEntityHandlerResult ehResult; char *putResp = constructJsonResponse(ehRequest); if (maxPayloadSize > strlen ((char *)putResp)) { strncpy(payload, putResp, strlen((char *)putResp)); ehResult = OC_EH_OK; } else { OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small", maxPayloadSize); ehResult = OC_EH_ERROR; } free(putResp); return ehResult; }
// This method is used to display 'Observe' functionality of OC Stack. void *ChangeFanRepresentation () { uint8_t j = 0; OCObservationId obsNotify[MAX_NUM_OBSERVATIONS]; OCStackResult result = OC_STACK_ERROR; if (gFanUnderObservation) { OC_LOG_V(INFO, TAG, " ===> Notifying stack of new Fan state %d\n", Fan.state); // Notify list of observers. Alternate observers on the list will be notified. j = 0; for (uint8_t i = 0; i < MAX_NUM_OBSERVATIONS; i++) { if (interestedObservers[i].valid == true) { obsNotify[j] = interestedObservers[i].observationId; j++; } } // MAX_RESPONSE_LENGTH for arduino is 256, for demo 64 is enough char obsResp[64] = {0}; char *getResp = constructJsonResponse(NULL); size_t responsePayloadGetLength = strlen(getResp); strncpy(obsResp, getResp, responsePayloadGetLength); free(getResp); result = OCNotifyListOfObservers(Fan.handle, obsNotify, j, (unsigned char *)obsResp, OC_NA_QOS); if ((OC_STACK_NO_OBSERVERS == result) || (OC_STACK_CONTINUE == result)) { gFanUnderObservation = 0; } } return NULL; }
OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, OCEntityHandlerResponse *response, char *payload, uint16_t maxPayloadSize) { OCEntityHandlerResult ehResult = OC_EH_OK; char *respPLPost_light = NULL; cJSON *json; cJSON *format; /* * The entity handler determines how to process a POST request. * Per the REST paradigm, POST can also be used to update representation of existing * resource or create a new resource. * In the sample below, if the POST is for /a/light then a new instance of the Light * resource is created with default representation (if representation is included in * POST payload it can be used as initial values) as long as the instance is * lesser than max new instance count. Once max instance count is reached, POST on * /a/light updated the representation of /a/light (just like PUT) */ if (ehRequest->resource == Light.handle) { if (gCurrLightInstance < SAMPLE_MAX_NUM_POST_INSTANCE) { // Create new Light instance char newLightUri[URI_MAXSIZE]; snprintf(newLightUri, URI_MAXSIZE, "/a/light/%d", gCurrLightInstance); json = cJSON_CreateObject(); cJSON_AddStringToObject(json,"href",gResourceUri); cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject()); cJSON_AddStringToObject(format, "createduri", (char *) newLightUri); if (0 == createLightResource (newLightUri, &gLightInstance[gCurrLightInstance])) { OC_LOG (INFO, TAG, "Created new Light instance\n"); gLightInstance[gCurrLightInstance].state = 0; gLightInstance[gCurrLightInstance].power = 0; gCurrLightInstance++; respPLPost_light = cJSON_Print(json); strncpy ((char *)response->resourceUri, newLightUri, MAX_URI_LENGTH); ehResult = OC_EH_RESOURCE_CREATED; } cJSON_Delete(json); } else { // Update repesentation of /a/light Light.state = true; Light.power = 11; respPLPost_light = constructJsonResponse(ehRequest); } } else { for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++) { if (ehRequest->resource == gLightInstance[i].handle) { gLightInstance[i].state = true; gLightInstance[i].power = 22; if (i == 0) { respPLPost_light = constructJsonResponse(ehRequest); break; } else if (i == 1) { respPLPost_light = constructJsonResponse(ehRequest); } } } } if ((respPLPost_light != NULL) && (maxPayloadSize > strlen ((char *)respPLPost_light))) { strncpy(payload, respPLPost_light, strlen((char *)respPLPost_light)); } else { OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small", maxPayloadSize); ehResult = OC_EH_ERROR; } free(respPLPost_light); 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; }