//This function takes the request as an input and returns the response //in JSON format. OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest) { LEDResource *currLEDResource = &LED; OC_LOG(INFO, TAG, "Entering constructResponse"); if (ehRequest->resource == gLedInstance[0].handle) { OC_LOG(INFO, TAG, "handle 0"); currLEDResource = &gLedInstance[0]; gResourceUri = const_cast<char *>("a/led/0"); } else if (ehRequest->resource == gLedInstance[1].handle) { OC_LOG(INFO, TAG, "handle 1"); currLEDResource = &gLedInstance[1]; gResourceUri = const_cast<char *>("a/led/1"); } if(OC_REST_PUT == ehRequest->method) { if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) { OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation")); return nullptr; } OCRepPayload *putPayload = reinterpret_cast<OCRepPayload*> (ehRequest->payload); int64_t power; bool state; if (OCRepPayloadGetPropBool(putPayload, "state", &state)) { currLEDResource->state = state; } if (OCRepPayloadGetPropInt (putPayload, "power", &power)) { currLEDResource->power = power; } } OCRepPayload *response = OCRepPayloadCreate(); if (!response) { OC_LOG_V(ERROR, TAG, "Memory allocation for response payload failed."); } OCRepPayloadSetUri (response, gResourceUri); OCRepPayloadSetPropBool(response, "state", currLEDResource->state); OCRepPayloadSetPropInt(response, "power", currLEDResource->power); return response; }
//This function takes the request as an input and returns the response OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest) { if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) { OIC_LOG(ERROR, TAG, "Incoming payload not a representation"); return NULL; } OCRepPayload* input = (OCRepPayload*)(ehRequest->payload); LEDResource *currLEDResource = &LED; if (ehRequest->resource == gLedInstance[0].handle) { currLEDResource = &gLedInstance[0]; gResourceUri = (char *) "/a/led/0"; } else if (ehRequest->resource == gLedInstance[1].handle) { currLEDResource = &gLedInstance[1]; gResourceUri = (char *) "/a/led/1"; } if(OC_REST_PUT == ehRequest->method) { // Get pointer to query int64_t pow; if(OCRepPayloadGetPropInt(input, "power", &pow)) { currLEDResource->power =pow; } bool state; if(OCRepPayloadGetPropBool(input, "state", &state)) { currLEDResource->state = state; } } return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state); }
OCStackResult HandleKeepAlivePUTRequest(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo) { VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM); VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM); // Get entry from KeepAlive table. uint32_t index = 0; KeepAliveEntry_t *entry = GetEntryFromEndpoint(endPoint, &index); if (!entry) { OIC_LOG(ERROR, TAG, "Received the first keepalive message from client"); entry = AddKeepAliveEntry(endPoint, OC_SERVER, NULL); if (!entry) { OIC_LOG(ERROR, TAG, "Failed to add new keepalive entry"); return OC_STACK_ERROR; } } OCPayload *ocPayload = NULL; OCParsePayload(&ocPayload, PAYLOAD_TYPE_REPRESENTATION, requestInfo->info.payload, requestInfo->info.payloadSize); OCRepPayload *repPayload = (OCRepPayload *)ocPayload; int64_t interval = 0; OCRepPayloadGetPropInt(repPayload, INTERVAL, &interval); entry->interval = interval; OIC_LOG_V(DEBUG, TAG, "Received interval is [%d]", entry->interval); entry->timeStamp = OICGetCurrentTime(TIME_IN_US); // Send response message. SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_CHANGED, requestInfo->info.type, requestInfo->info.numOptions, requestInfo->info.options, requestInfo->info.token, requestInfo->info.tokenLength, requestInfo->info.resourceUri); return OC_STACK_OK; }
OCStackApplicationResult ProvisionEnrolleeResponse(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse) { OIC_LOG_V(DEBUG, TAG, "INSIDE ProvisionEnrolleeResponse"); // If user stopped the process then return from this function; if (IsSetupStopped()) { ErrorCallback(DEVICE_NOT_PROVISIONED); ClearMemory(); return OC_STACK_DELETE_TRANSACTION; } ProvisioningInfo *provInfo; if (!ValidateEnrolleResponse(clientResponse)) { ErrorCallback(DEVICE_NOT_PROVISIONED); return OC_STACK_DELETE_TRANSACTION; } char *tnn; char *cd; OCRepPayload *input = (OCRepPayload * )(clientResponse->payload); while (input) { int64_t ps; if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps)) { if (ps == 1) { input = input->next; continue; } else { OIC_LOG_V(DEBUG, TAG, "PS is NOT proper"); goto Error; } } if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn)) { if (!strcmp(tnn, netProvInfo->netAddressInfo.WIFI.ssid)) { OIC_LOG_V(DEBUG, TAG, "SSID is proper"); input = input->next; continue; } else { OIC_LOG_V(DEBUG, TAG, "SSID is NOT proper"); goto Error; } } if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd)) { if (!strcmp(cd, netProvInfo->netAddressInfo.WIFI.pwd)) { OIC_LOG_V(DEBUG, TAG, "Password is proper"); input = input->next; continue; } else { OIC_LOG_V(DEBUG, TAG, "Password is NOT proper"); goto Error; } } LogProvisioningResponse(input->values); input = input->next; } SuccessCallback(clientResponse); return OC_STACK_KEEP_TRANSACTION; Error: { ErrorCallback(DEVICE_NOT_PROVISIONED); return OC_STACK_DELETE_TRANSACTION; } }
OCEntityHandlerResult UpnpRenderingControl::processPutRequest(OCEntityHandlerRequest *ehRequest, string uri, string resourceType, OCRepPayload *payload) { (void) uri; if (!ehRequest || !ehRequest->payload || ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) { throw "Incoming payload is NULL or not a representation"; } OCRepPayload *input = reinterpret_cast<OCRepPayload *>(ehRequest->payload); if (!input) { throw "PUT payload is null"; } if (UPNP_OIC_TYPE_AUDIO == resourceType) { //TODO use async version with callback GError *error = NULL; // set mute bool muteValue = false; if (OCRepPayloadGetPropBool(input, mutePropertyName, &muteValue)) { DEBUG_PRINT("New " << mutePropertyName << ": " << (muteValue ? "true" : "false")); if (! gupnp_service_proxy_send_action(m_proxy, setMuteAction, &error, // IN args instanceIdParamName, G_TYPE_UINT, defaultInstanceID, channelParamName, G_TYPE_STRING, defaultChannel, desiredMuteParamName, G_TYPE_BOOLEAN, muteValue, NULL, // OUT args (none) NULL)) { ERROR_PRINT(setMuteAction << " action failed"); if (error) { DEBUG_PRINT("Error message: " << error->message); g_error_free(error); } return OC_EH_ERROR; } if (!OCRepPayloadSetPropBool(payload, mutePropertyName, muteValue)) { throw "Failed to set mute value in payload"; } DEBUG_PRINT(mutePropertyName << ": " << (muteValue ? "true" : "false")); } // set volume int64_t volumeValue = 0; if (OCRepPayloadGetPropInt(input, volumePropertyName, &volumeValue)) { DEBUG_PRINT("New " << volumePropertyName << ": " << volumeValue); int upnpVolumeValue = volumeValue; if (! gupnp_service_proxy_send_action(m_proxy, setVolumeAction, &error, // IN args instanceIdParamName, G_TYPE_UINT, defaultInstanceID, channelParamName, G_TYPE_STRING, defaultChannel, desiredVolumeParamName, G_TYPE_UINT, upnpVolumeValue, NULL, // OUT args (none) NULL)) { ERROR_PRINT(setVolumeAction << " action failed"); if (error) { DEBUG_PRINT("Error message: " << error->message); g_error_free(error); } return OC_EH_ERROR; } if (!OCRepPayloadSetPropInt(payload, volumePropertyName, volumeValue)) { throw "Failed to set volume value in payload"; } DEBUG_PRINT(volumePropertyName << ": " << volumeValue); } } else { throw "Failed due to unknown resource type"; } return OC_EH_OK; }
OCStackApplicationResult GetProvisioningStatusResponse(void* /*ctx*/, OCDoHandle /*handle*/, OCClientResponse *clientResponse) { // If user stopped the process then return from this function; if (IsSetupStopped()) { ErrorCallback(DEVICE_NOT_PROVISIONED); ClearMemory(); return OC_STACK_DELETE_TRANSACTION; } if (!ValidateEnrolleeResponse(clientResponse)) { ErrorCallback(DEVICE_NOT_PROVISIONED); ClearMemory(); return OC_STACK_DELETE_TRANSACTION; } OCRepPayload *input = (OCRepPayload *) (clientResponse->payload); char resUri[MAX_URI_LENGTH] = { '\0' }; OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", clientResponse->resourceUri); OICStrcpy(resUri, sizeof(resUri), clientResponse->resourceUri); while (input) { int64_t ps; if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps)) { if (ps == ES_PS_NEED_PROVISIONING) { input = input->next; continue; } else { ErrorCallback(DEVICE_NOT_PROVISIONED); ClearMemory(); return OC_STACK_DELETE_TRANSACTION; } } LogProvisioningResponse(input->values); input = input->next; } char query[OIC_STRING_MAX_VALUE] = { '\0' }; #ifdef REMOTE_ARDUINO_ENROLEE //Arduino Enrollee needs mediator application provide IP and port55555 which is specific // to Arduino WiFi enrollee // REMOTE_ARDUINO_ENROLEE has to be defined if Mediator is being tested with Arduino snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT, resUri); #else snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, clientResponse->addr->port, resUri); #endif if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0) != OC_STACK_OK) { OIC_LOG(INFO, ES_WIFI_PROV_TAG, "GetProvisioningStatusResponse received NULL clientResponse"); ErrorCallback(DEVICE_NOT_PROVISIONED); ClearMemory(); return OC_STACK_DELETE_TRANSACTION; } return OC_STACK_KEEP_TRANSACTION; }
bool getZigBeeAttributesIfValid (char * OICResourceType, AttributeList *attributeList, OCRepPayload *payload) // Put { if(!OICResourceType) { return false; } if(strcmp(OICResourceType, OIC_TEMPERATURE_SENSOR) == 0) { // Cant really PUT on the temp sensor, but the code is still there. int64_t temperature = 0; // TODO: This if should only look for attributes it supports and ignore the rest // or examine every attribute in the payload and complain about unsupported attributes? if(OCRepPayloadGetPropInt(payload, OIC_TEMPERATURE_ATTRIBUTE, &temperature)) { attributeList->count = 1; attributeList->list[0].oicAttribute = OICStrdup(OIC_TEMPERATURE_ATTRIBUTE); attributeList->list[0].zigBeeAttribute = ZB_TEMPERATURE_ATTRIBUTE_ID; attributeList->list[0].oicType = OIC_ATTR_DOUBLE; attributeList->list[0].val.d = temperature; attributeList->list[0].zigbeeType = ZB_16_SINT; attributeList->CIEMask = (CIECommandMask) 0; return true; } } else if (strcmp (OICResourceType, OIC_DIMMABLE_LIGHT) == 0) { int64_t onLevel = 0; if(OCRepPayloadGetPropInt(payload, OIC_DIMMING_ATTRIBUTE, &onLevel)) { attributeList->count = 1; attributeList->list[0].oicAttribute = OICStrdup(OIC_DIMMING_ATTRIBUTE); attributeList->list[0].zigBeeAttribute = ZB_ON_LEVEL_ATTRIBUTE; attributeList->list[0].oicType = OIC_ATTR_INT; attributeList->list[0].val.i = onLevel; attributeList->list[0].zigbeeType = ZB_8_UINT; // Level control cluster is dealing with level in the PUT payload. attributeList->CIEMask = attributeList->CIEMask | CIE_MOVE_TO_LEVEL; return true; } } else if (strcmp (OICResourceType, OIC_CONTACT_SENSOR) == 0) { int64_t value = 0; if(OCRepPayloadGetPropInt(payload, OIC_CONTACT_ATTRIBUTE, &value)) { attributeList->count = 1; attributeList->list[0].oicAttribute = OICStrdup(OIC_CONTACT_ATTRIBUTE); attributeList->list[0].zigBeeAttribute = ZB_CONTACT_ATTRIBUTE_ID; attributeList->list[0].oicType = OIC_ATTR_BOOL; attributeList->list[0].val.i = value; attributeList->list[0].zigbeeType = ZB_BOOL; attributeList->CIEMask = (CIECommandMask) 0; return true; } } else if (strcmp (OICResourceType, OIC_BINARY_SWITCH) == 0) { bool value = 0; if(OCRepPayloadGetPropBool(payload, OIC_ON_OFF_ATTRIBUTE, &value)) { attributeList->count = 1; attributeList->list[0].oicAttribute = OICStrdup(OIC_ON_OFF_ATTRIBUTE); attributeList->list[0].zigBeeAttribute = ZB_ON_OFF_ATTRIBUTE_ID; attributeList->list[0].oicType = OIC_ATTR_BOOL; attributeList->list[0].val.b = value; attributeList->list[0].zigbeeType = ZB_BOOL; attributeList->CIEMask = attributeList->CIEMask | CIE_RON_OFF; return true; } } return false; }
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; }
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload) { OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter"); OCEntityHandlerResult ehResult = OC_EH_ERROR; if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) { OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation"); return ehResult; } OCRepPayload* input = (OCRepPayload*) (ehRequest->payload); if (!input) { OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse"); return ehResult; } int64_t tr; if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_TR, &tr)) { // Triggering gProvResource.tr = tr; } //ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed. // A new request for provisioning means overriding existing network provisioning information. if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED && tr == ES_PS_TRIGGER_CONNECTION) { OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed." "Tiggering the network connection"); if (gNetworkInfoProvEventCb) { gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES); ehResult = OC_EH_OK; } else { OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL." "Network handler not registered. Failed to connect to the network"); ehResult = OC_EH_ERROR; } return ehResult; } else if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED) { OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. " "This a request to override the existing the network provisioning information"); } else { OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee."); } OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), ""); OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), ""); char* tnn; if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn)) { OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), tnn); OIC_LOG(INFO, ES_RH_TAG, "got ssid"); } OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.tnn %s", gProvResource.tnn); char* cd; if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd)) { OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), cd); OIC_LOG(INFO, ES_RH_TAG, "got password"); }OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd); gProvResource.ps = ES_PS_PROVISIONING_COMPLETED; OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %lld", gProvResource.ps); OCRepPayload *getResp = constructResponse(ehRequest); if (!getResp) { OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed"); return OC_EH_ERROR; } *payload = getResp; ehResult = OC_EH_OK; return ehResult; }
/** * @brief Called when a REST PUT is request * * @param OCBaseResource base resource attributes * * @return result of the entityHandler */ OCEntityHandlerResult putRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload* payload, OCBaseResourceT *resource) { // Set the new states OCRepPayload* inputPayload = (OCRepPayload*)(ehRequest->payload); OCAttributeT *current = resource->attribute; while(current != NULL) { switch(current->value.dataType) { case INT: { int64_t value(0); if(OCRepPayloadGetPropInt(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: Type is int: %i", (int) value); //*((int*)current->value.data) = (int) value; current->value.data.i = value; } OCRepPayloadSetPropInt(payload, current->name, value); break; } case DOUBLE: { double value(0); if(OCRepPayloadGetPropDouble(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: type is double: &d", value); //*((double*)current->value.data) = value; current->value.data.d = value; } OCRepPayloadSetPropDouble(payload, current->name, value); break; } case STRING: { char* value(""); if(OCRepPayloadGetPropString(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: type is string: %s", value); //*((char**)current->value.data) = value; current->value.data.str = value; } OCRepPayloadSetPropString(payload, current->name, value); } case BOOL: { bool value(false); if(OCRepPayloadGetPropBool(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: Type is bool: %s", value ? "true" : "false"); //*((bool*)current->value.data) = value; current->value.data.b = value; } OCRepPayloadSetPropBool(payload, current->name, value); break; } } current = current->next; } // Set the output pins if(resource->OCIOhandler) { OIC_LOG_V(DEBUG, TAG, "Value of underObservation is: %s", resource->underObservation ? "true" : "false"); resource->OCIOhandler(resource->attribute, OUTPUT, resource->handle, &resource->underObservation); } else { OIC_LOG(ERROR, TAG, "Resource OutputHandler has not been set"); } OIC_LOG(DEBUG, TAG, "Leaving putRequest"); return OC_EH_OK; }
OCEntityHandlerResult BuzzerOCEntityHandlerCb(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/buzzer"); OCRepPayloadSetPropInt(payload, "tone", buzzer.tone); } else if(OC_REST_PUT == entityHandlerRequest->method) { int64_t tone; OC_LOG(INFO, TAG, ("PUT request")); OCRepPayload *rep = (OCRepPayload *)entityHandlerRequest->payload; OCRepPayloadGetPropInt(rep, "tone", &tone); if(tone > 1915) tone = 1915; else if(tone < 956) tone = 956; buzzer.tone = (int)tone; OC_LOG_V(INFO, TAG, "Buzzer tone: %d", buzzer.tone); buzzer_put(); OCRepPayloadSetPropInt(payload, "tone", buzzer.tone); } 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 bool add_property_in_post_req_msg(st_things_set_request_message_s *req_msg, OCRepPayload *req_payload, things_attribute_info_s *prop) { RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep->payload); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_payload); RET_FALSE_IF_PARAM_IS_NULL(TAG, prop); OCRepPayload *resp_payload = req_msg->rep->payload; THINGS_LOG_D(TAG, "Property Key is %s", prop->key); THINGS_LOG_D(TAG, "Property type is %d", prop->type); // Based on the property type, call appropriate methods to copy // the property from request payload to request representation. bool result = false; switch (prop->type) { case BOOL_ID: { bool value = false; if (OCRepPayloadGetPropBool(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropBool(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the boolean value of '%s' in request message.", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the boolean value of '%s' for request message.", prop->key); } } break; case INT_ID: { int64_t value = 0; if (OCRepPayloadGetPropInt(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropInt(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the integer value of '%s' in request message", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the integer value of '%s' for request message", prop->key); } } break; case DOUBLE_ID: { double value = 0.0; if (OCRepPayloadGetPropDouble(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropDouble(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the double value of '%s' in request message", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the double value of '%s' for request message", prop->key); } } break; case STRING_ID: { char *value = NULL; if (OCRepPayloadGetPropString(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropString(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the string value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the string value of '%s' for request message", prop->key); } } break; case OBJECT_ID: { OCRepPayload *value = NULL; if (OCRepPayloadGetPropObject(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropObject(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the object value of '%s' in request message", prop->key); } OCRepPayloadDestroy(value); } else { THINGS_LOG_E(TAG, "Failed to get the object value of '%s' for request message", prop->key); } } break; case BYTE_ID: { OCByteString byte_value; if (OCRepPayloadGetPropByteString(req_payload, prop->key, &byte_value)) { result = OCRepPayloadSetPropByteString(resp_payload, prop->key, byte_value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the byte string value of '%s' in request message", prop->key); } things_free(byte_value.bytes); } else { THINGS_LOG_E(TAG, "Failed to get the byte string value of '%s' for request message", prop->key); } } break; case INT_ARRAY_ID: { int64_t *value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetIntArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetIntArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the integer array value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the integer array value of '%s' for request message", prop->key); } } break; case DOUBLE_ARRAY_ID: { double *value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetDoubleArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetDoubleArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the double array value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the double array value of '%s' for request message", prop->key); } } break; case STRING_ARRAY_ID: { char **value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetStringArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetStringArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the string array value of '%s' in request message", prop->key); } size_t len = calcDimTotal(dimensions); things_free_str_array(value, len); } else { THINGS_LOG_E(TAG, "Failed to get the string array value of '%s' for request message", prop->key); } } break; case OBJECT_ARRAY_ID: { OCRepPayload **value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetPropObjectArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetPropObjectArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the object array value of '%s' in request message", prop->key); } size_t len = calcDimTotal(dimensions); for (size_t index = 0; index < len; index++) { OCRepPayloadDestroy(value[index]); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the object array value of '%s' for request message", prop->key); } } break; default: THINGS_LOG_E(TAG, "Invalid property type (%d).", prop->type); break; } return result; }
bool things_get_int_value(struct things_representation_s *rep, char *key, int64_t *value) { return OCRepPayloadGetPropInt((OCRepPayload *) rep->payload, key, value); }