X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fstack%2Fsamples%2Flinux%2FSimpleClientServer%2Focserver.cpp;h=65d707593091ac748518199cf6c2f13fa45a8ac2;hb=966700a71110e415e8d3049cbe59c624929a9dff;hp=68a15fa9d497ca34a006f1b6a4bfe5d6ea138373;hpb=507296ce3f50da0fef131ea59e75b8d6cf2eb842;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp index 68a15fa..65d7075 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp @@ -29,7 +29,7 @@ #include #include "ocstack.h" #include "logger.h" -#include "cJSON.h" +#include "ocpayload.h" #include "ocserver.h" //string length of "/a/light/" + std::numeric_limits::digits10 + '\0'" @@ -55,17 +55,6 @@ static int stopPresenceCount = 10; #define numPresenceResources (2) #endif -//TODO: Follow the pattern used in constructJsonResponse() when the payload is decided. -const char responsePayloadDeleteOk[] = - "{App determines payload: Delete Resource operation succeeded.}"; -const char responsePayloadDeleteNotOK[] = - "{App determines payload: Delete Resource operation failed.}"; -const char responsePayloadResourceDoesNotExist[] = - "{App determines payload: The resource does not exist.}"; -const char responsePayloadDeleteResourceNotSupported[] = - "{App determines payload: The request is received for a non-support resource.}"; - - char *gResourceUri= (char *)"/a/light"; const char *dateOfManufacture = "myDateOfManufacture"; const char *deviceName = "myDeviceName"; @@ -90,13 +79,33 @@ const char *resourceInterface = OC_RSRVD_INTERFACE_DEFAULT; OCPlatformInfo platformInfo; OCDeviceInfo deviceInfo; +OCRepPayload* getPayload(const char* uri, int64_t power, bool state) +{ + OCRepPayload* payload = OCRepPayloadCreate(); + if(!payload) + { + OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload")); + return nullptr; + } + + OCRepPayloadSetUri(payload, uri); + OCRepPayloadSetPropBool(payload, "state", state); + OCRepPayloadSetPropInt(payload, "power", power); + + return payload; +} + //This function takes the request as an input and returns the response -//in JSON format. -char* constructJsonResponse (OCEntityHandlerRequest *ehRequest) +OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest) { - cJSON *json = cJSON_CreateObject(); - cJSON *format; - char *jsonResponse; + if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) + { + OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation")); + return nullptr; + } + + OCRepPayload* input = reinterpret_cast(ehRequest->payload); + LightResource *currLightResource = &Light; if (ehRequest->resource == gLightInstance[0].handle) @@ -112,51 +121,21 @@ char* constructJsonResponse (OCEntityHandlerRequest *ehRequest) if(OC_REST_PUT == ehRequest->method) { - // Get cJSON pointer to query - cJSON *putJson = cJSON_Parse(ehRequest->reqJSONPayload); - - if(!putJson) + // Get pointer to query + int64_t pow; + if(OCRepPayloadGetPropInt(input, "power", &pow)) { - OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload); - return NULL; + currLightResource->power =pow; } - // Get root of JSON payload, then the 1st resource. - cJSON* carrier = cJSON_GetObjectItem(putJson, "oic"); - if (carrier) - { - carrier = cJSON_GetArrayItem(carrier, 0); - carrier = cJSON_GetObjectItem(carrier, "rep"); - - cJSON* prop = cJSON_GetObjectItem(carrier,"power"); - if (prop) - { - currLightResource->power =prop->valueint; - } - - prop = cJSON_GetObjectItem(carrier,"state"); - if (prop) - { - currLightResource->state = prop->valueint; - } - } - else + bool state; + if(OCRepPayloadGetPropBool(input, "state", &state)) { - OC_LOG_V(WARNING, TAG, "Failed to find oic node"); + currLightResource->state = state; } - - cJSON_Delete(putJson); } - cJSON_AddStringToObject(json,"href",gResourceUri); - cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject()); - cJSON_AddBoolToObject(format, "state", currLightResource->state); - cJSON_AddNumberToObject(format, "power", currLightResource->power); - - jsonResponse = cJSON_Print(json); - cJSON_Delete(json); - - return jsonResponse; + return getPayload(gResourceUri, currLightResource->power, currLightResource->state); } /* @@ -200,7 +179,7 @@ OCEntityHandlerResult ValidateQueryParams (OCEntityHandlerRequest *entityHandler } OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest, - char *payload, uint16_t maxPayloadSize) + OCRepPayload **payload) { OCEntityHandlerResult ehResult; bool queryPassed = checkIfQueryForPowerPassed(ehRequest->query); @@ -208,26 +187,15 @@ OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest, // Empty payload if the query has no match. if (queryPassed) { - char *getResp = constructJsonResponse(ehRequest); + OCRepPayload *getResp = constructResponse(ehRequest); if(!getResp) { - OC_LOG(ERROR, TAG, "constructJsonResponse failed"); + OC_LOG(ERROR, TAG, "constructResponse failed"); return OC_EH_ERROR; } - if (maxPayloadSize > strlen (getResp)) - { - strncpy(payload, getResp, strlen(getResp)); - ehResult = OC_EH_OK; - } - else - { - OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small", - maxPayloadSize); - ehResult = OC_EH_ERROR; - } - - free(getResp); + *payload = getResp; + ehResult = OC_EH_OK; } else { @@ -238,10 +206,10 @@ OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest, } OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest, - char *payload, uint16_t maxPayloadSize) + OCRepPayload** payload) { OCEntityHandlerResult ehResult; - char *putResp = constructJsonResponse(ehRequest); + OCRepPayload *putResp = constructResponse(ehRequest); if(!putResp) { @@ -249,30 +217,17 @@ OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest, return OC_EH_ERROR; } - 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); + *payload = putResp; + ehResult = OC_EH_OK; return ehResult; } OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, - OCEntityHandlerResponse *response, char *payload, uint16_t maxPayloadSize) + OCEntityHandlerResponse *response, OCRepPayload** payload) { OCEntityHandlerResult ehResult = OC_EH_OK; - char *respPLPost_light = NULL; - cJSON *json; - cJSON *format; + OCRepPayload *respPLPost_light = nullptr; /* * The entity handler determines how to process a POST request. @@ -293,10 +248,9 @@ OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, 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); + respPLPost_light = OCRepPayloadCreate(); + OCRepPayloadSetUri(respPLPost_light, gResourceUri); + OCRepPayloadSetPropString(respPLPost_light, "createduri", newLightUri); if (0 == createLightResource (newLightUri, &gLightInstance[gCurrLightInstance])) { @@ -304,19 +258,16 @@ OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, 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); + respPLPost_light = constructResponse(ehRequest); } } else @@ -329,34 +280,31 @@ OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, gLightInstance[i].power = 22; if (i == 0) { - respPLPost_light = constructJsonResponse(ehRequest); + respPLPost_light = constructResponse(ehRequest); break; } else if (i == 1) { - respPLPost_light = constructJsonResponse(ehRequest); + respPLPost_light = constructResponse(ehRequest); } } } } - if ((respPLPost_light != NULL) && (maxPayloadSize > strlen ((char *)respPLPost_light))) + if ((respPLPost_light != NULL)) { - strncpy(payload, respPLPost_light, strlen((char *)respPLPost_light)); + *payload = respPLPost_light; } else { - OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small", - maxPayloadSize); + OC_LOG(INFO, TAG, "Payload was NULL"); ehResult = OC_EH_ERROR; } - free(respPLPost_light); return ehResult; } -OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest, - char *payload, uint16_t maxPayloadSize) +OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest) { if(ehRequest == NULL) { @@ -376,8 +324,6 @@ OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest, * 2. optionally, app removes observers out of its array 'interestedObservers' */ - const char* deleteResponse = NULL; - if ((ehRequest != NULL) && (ehRequest->resource == Light.handle)) { //Step 1: Ask stack to do the work. @@ -387,7 +333,6 @@ OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest, { OC_LOG (INFO, TAG, "\n\nDelete Resource operation succeeded."); ehResult = OC_EH_OK; - deleteResponse = responsePayloadDeleteOk; //Step 2: clear observers who wanted to observe this resource at the app level. for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++) @@ -403,13 +348,11 @@ OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest, else if (result == OC_STACK_NO_RESOURCE) { OC_LOG(INFO, TAG, "\n\nThe resource doesn't exist or it might have been deleted."); - deleteResponse = responsePayloadResourceDoesNotExist; ehResult = OC_EH_RESOURCE_DELETED; } else { OC_LOG(INFO, TAG, "\n\nEncountered error from OCDeleteResource()."); - deleteResponse = responsePayloadDeleteNotOK; ehResult = OC_EH_ERROR; } } @@ -418,43 +361,16 @@ OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest, //Let's this app not supporting DELETE on some resources so //consider the DELETE request is received for a non-support resource. OC_LOG_V(INFO, TAG, "\n\nThe request is received for a non-support resource."); - deleteResponse = responsePayloadDeleteResourceNotSupported; ehResult = OC_EH_FORBIDDEN; } - if (maxPayloadSize > strlen ((char *)deleteResponse)) - { - strncpy(payload, deleteResponse, strlen((char *)deleteResponse)); - } - else - { - OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small", - maxPayloadSize); - ehResult = OC_EH_ERROR; - } - return ehResult; } -OCEntityHandlerResult ProcessNonExistingResourceRequest(OCEntityHandlerRequest *ehRequest, - char *payload, uint16_t maxPayloadSize) +OCEntityHandlerResult ProcessNonExistingResourceRequest(OCEntityHandlerRequest * /*ehRequest*/) { OC_LOG_V(INFO, TAG, "\n\nExecuting %s ", __func__); - const char* response = NULL; - response = responsePayloadResourceDoesNotExist; - - if ( (ehRequest != NULL) && - (maxPayloadSize > strlen ((char *)response)) ) - { - strncpy((char *)payload, response, strlen((char *)response)); - } - else - { - OC_LOG_V (ERROR, TAG, "Response buffer: %d bytes is too small", - maxPayloadSize); - } - return OC_EH_RESOURCE_NOT_FOUND; } @@ -498,13 +414,14 @@ void ProcessObserveDeregister (OCEntityHandlerRequest *ehRequest) OCEntityHandlerResult OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag, - OCEntityHandlerRequest *entityHandlerRequest, char* uri, void* callbackParam) + OCEntityHandlerRequest *entityHandlerRequest, + char* uri, + void* /*callbackParam*/) { 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) @@ -517,6 +434,7 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag, memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); + OCRepPayload* payload = nullptr; if (flag & OC_REQUEST_FLAG) @@ -526,23 +444,22 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag 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); + ehResult = ProcessNonExistingResourceRequest(entityHandlerRequest); } else if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); - ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1); + ehResult = ProcessGetRequest (entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); - ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1); + ehResult = ProcessPutRequest (entityHandlerRequest, &payload); } else if (OC_REST_DELETE == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client"); - ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1); + ehResult = ProcessDeleteRequest (entityHandlerRequest); } else { @@ -557,8 +474,7 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag, response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; - response.payload = payload; - response.payloadSize = strlen(payload); + response.payload = reinterpret_cast(payload); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; @@ -587,8 +503,9 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag, } OCEntityHandlerResult -OCNOPEntityHandlerCb (OCEntityHandlerFlag flag, - OCEntityHandlerRequest *entityHandlerRequest, void* callbackParam) +OCNOPEntityHandlerCb (OCEntityHandlerFlag /*flag*/, + OCEntityHandlerRequest * /*entityHandlerRequest*/, + void* /*callbackParam*/) { // This is callback is associated with the 2 presence notification // resources. They are non-operational. @@ -597,13 +514,12 @@ OCNOPEntityHandlerCb (OCEntityHandlerFlag flag, OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, - OCEntityHandlerRequest *entityHandlerRequest, void* callback) + OCEntityHandlerRequest *entityHandlerRequest, void* /*callback*/) { 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}; + OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false }; // Validate pointer if (!entityHandlerRequest) @@ -617,6 +533,7 @@ OCEntityHandlerCb (OCEntityHandlerFlag flag, memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); + OCRepPayload* payload = nullptr; if (flag & OC_REQUEST_FLAG) { @@ -625,22 +542,22 @@ OCEntityHandlerCb (OCEntityHandlerFlag flag, if (OC_REST_GET == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); - ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1); + ehResult = ProcessGetRequest (entityHandlerRequest, &payload); } else if (OC_REST_PUT == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); - ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1); + ehResult = ProcessPutRequest (entityHandlerRequest, &payload); } 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); + ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload); } else if (OC_REST_DELETE == entityHandlerRequest->method) { OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client"); - ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1); + ehResult = ProcessDeleteRequest (entityHandlerRequest); } else { @@ -655,8 +572,7 @@ OCEntityHandlerCb (OCEntityHandlerFlag flag, response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehResult; - response.payload = payload; - response.payloadSize = strlen(payload); + response.payload = reinterpret_cast(payload); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; @@ -717,6 +633,7 @@ OCEntityHandlerCb (OCEntityHandlerFlag flag, } } + OCPayloadDestroy(response.payload); return ehResult; } @@ -758,18 +675,10 @@ void *ChangeLightRepresentation (void *param) } } - cJSON *json = cJSON_CreateObject(); - cJSON *format; - cJSON_AddStringToObject(json,"href",gResourceUri); - cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject()); - cJSON_AddBoolToObject(format, "state", Light.state); - cJSON_AddNumberToObject(format, "power", Light.power); - - char * obsResp = cJSON_Print(json); - cJSON_Delete(json); + OCRepPayload* payload = getPayload(gResourceUri, Light.power, Light.state); result = OCNotifyListOfObservers (Light.handle, obsNotify, j, - obsResp, OC_NA_QOS); - free(obsResp); + payload, OC_NA_QOS); + OCRepPayloadDestroy(payload); } else if (gObserveNotifyType == 0) { @@ -1043,6 +952,18 @@ int main(int argc, char* argv[]) PrintUsage(); return -1; } +#ifdef RA_ADAPTER + OCRAInfo_t rainfo; + rainfo.hostname = "localhost"; + rainfo.port = 5222; + rainfo.xmpp_domain = "localhost"; + rainfo.username = "test1"; + rainfo.password = "intel123"; + rainfo.resource = ""; + rainfo.user_jid = ""; + + OCSetRAInfo(&rainfo); +#endif OC_LOG(DEBUG, TAG, "OCServer is starting..."); @@ -1136,8 +1057,9 @@ int main(int argc, char* argv[]) OC_LOG(ERROR, TAG, "OCStack process error"); return 0; } - +#ifndef ROUTING_GATEWAY sleep(2); +#endif } /*