Fix error(46 : OCF_UNAUTHORIZED_REQ) 96/145296/1
authorhhk86.heo <hhk86.heo@samsung.com>
Tue, 22 Aug 2017 04:41:05 +0000 (13:41 +0900)
committerhhk86.heo <hhk86.heo@samsung.com>
Tue, 22 Aug 2017 04:41:05 +0000 (13:41 +0900)
- no ACL in each resources(firmware, capability)

Change-Id: If606756d9fff88a127cd86199d7b1e3c84274c19

inc/controlee_firmware_resource_internal.h
src/controlee_firmware_resource_internal.c
test/CMakeLists.txt
test/controlee_firmware_test.c

index 53d064500950b9a29c3ec45d520ba6003d8bc6dc..04fe3c149a177b074cfe0c85c382dd72bde3c9c1 100644 (file)
@@ -36,7 +36,7 @@
 #define LOG_TAG "FIRMWARE_RESOURCE"
 #endif
 
-#define FWR_LOGD(fmt, args...)  LOGD(fmt, ##args)
+#define FWR_LOGD(fmt, args...)  LOGI(fmt, ##args)
 #define FWR_LOGW(fmt, args...)  LOGW(fmt, ##args)
 #define FWR_LOGI(fmt, args...)  LOGI(fmt, ##args)
 #define FWR_LOGE(fmt, args...)  LOGE(fmt, ##args)
index 0c4082e7413713a3d339f21f11c804b8f2ce73d5..4bc6013ab5fff4f298304419d07cbb4ccade78fd 100644 (file)
@@ -72,8 +72,9 @@ void controlee_propagate_firmware_resource(void)
 
 void _handle_update_command(int64_t update_type)
 {
-       if ((g_firmware_resource->state == 0 && update_type == CONTROLEE_FIRMWARE_EXEC_DOWNLOAD) ||
-                       (g_firmware_resource->state == 0 && update_type == CONTROLEE_FIRMWARE_EXEC_DOWNLOAD_AND_UPGRADE)) {
+       if (g_strcmp0(g_firmware_resource->package_uri, "") && (
+                       (g_firmware_resource->state == 0 && update_type == CONTROLEE_FIRMWARE_EXEC_DOWNLOAD) ||
+                       (g_firmware_resource->state == 0 && update_type == CONTROLEE_FIRMWARE_EXEC_DOWNLOAD_AND_UPGRADE))) {
                if (g_firmware_resource->new_version && \
                                g_strcmp0(g_firmware_resource->current_version, g_firmware_resource->new_version)) {
                        FWR_LOGD("***Downloading image from [%s] ***", g_firmware_resource->package_uri);
@@ -132,6 +133,11 @@ OCRepPayload* _construct_response_of_firmware()
                return NULL;
        }
 
+       OCRepPayloadSetUri(payload, OC_RSRVD_CONTROLEE_FIRMWARE_URI);
+
+       OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
+       OCRepPayloadAddResourceType(payload, OC_RSRVD_FIRMWARE_RES_TYPE);
+
        OCRepPayloadSetPropString(payload, OC_RSRVD_FIRMWARE_CURRENT_VERSION, g_firmware_resource->current_version);
        OCRepPayloadSetPropString(payload, OC_RSRVD_FIRMWARE_NEW_VERSION, g_firmware_resource->new_version);
        OCRepPayloadSetPropInt(payload, OC_RSRVD_FIRMWARE_STATE, (int64_t)g_firmware_resource->state);
index f00103b2f626fc227294b2d4d57662d3ce378c6d..3bc4a3939bfc9817c6466144fecda5cbfc6920e6 100644 (file)
@@ -1,7 +1,7 @@
 set(testapp "controlee_firmware_test")
 
-#ADD_DEFINITIONS(-D__WITH_DTLS__ -D__WITH_TLS__ -DRD_CLIENT)
-ADD_DEFINITIONS(-DRD_CLIENT)
+ADD_DEFINITIONS(-D__WITH_DTLS__ -D__WITH_TLS__ -DRD_CLIENT -D_USE_WWST_)
+#ADD_DEFINITIONS(-DRD_CLIENT)
 
 SET(CONTROLEE_FIRMWARE_TEST_SRCS
     ${CMAKE_CURRENT_SOURCE_DIR}/controlee_firmware_test.c
index 8081f5746c35b7f61e174115b5dd8491fbfdb13c..a2d5d6345ad8bd81ffd42d812bef36f7f54b580e 100644 (file)
@@ -80,7 +80,7 @@ typedef struct
 #endif
 } CloudLoginInfo;
 
-
+bool g_auto_publish;
 bool bSignin;
 bool isExit;
 uint16_t g_cred_id = 0;
@@ -168,6 +168,7 @@ static FILE *_client_open(const char *path, const char *mode)
        else if(!strcmp(mode, "wb"))
                mode = strdup("w");
 
+       printf("path [%s], mode[%s]", path, mode);
        return fopen(OC_CONTROLEE_DAT_FILE_PATH, mode);
 }
 
@@ -319,9 +320,14 @@ void _set_device_info()
        OCDeviceInfo devInfoAirConditioner;
        OCStringLL deviceType;
 
-       deviceType.value = "oic.d.airconditioner";
+#ifdef _USE_WWST_
+       deviceType.value = g_strdup("oic.d.switch");
+#else
+       deviceType.value = g_strdup("oic.d.airconditioner");
+#endif
+
        deviceType.next = NULL;
-       devInfoAirConditioner.deviceName = "FAC_2016";
+       devInfoAirConditioner.deviceName = g_strdup("FAC_2016");
        devInfoAirConditioner.types = &deviceType;
        devInfoAirConditioner.specVersion = NULL;
        devInfoAirConditioner.dataModelVersions = NULL;
@@ -438,6 +444,12 @@ static OCStackApplicationResult _handle_signin_response(void *ctx,
                bSignin = true;
                printf("Sign-In Success!!!\n");
 
+               sleep(1);
+               if (g_auto_publish) {
+                       if (_publish_fw_res_to_rd() != 0)
+                               printf("publish failed!!!\n");
+               }
+
 #ifdef _USE_WWST_
                pthread_t pingThread;
                pthread_mutex_lock(&pingcycle_mutex);
@@ -505,8 +517,8 @@ static OCStackApplicationResult _handle_signout_response(void *ctx,
 int _signin()
 {
        char uri[MAX_URI_LENGTH] = { 0 };
-       snprintf(uri, MAX_URI_LENGTH, "%s%s%s",
-                       CLOUD_PROTOCOL, CLOUD_STG_SERVER, OC_RSRVD_ACCOUNT_SESSION_URI);
+       snprintf(uri, MAX_URI_LENGTH, "%s%s%s?rt=%s;if=%s",
+                       CLOUD_PROTOCOL, CLOUD_STG_SERVER, OC_RSRVD_ACCOUNT_SESSION_URI, CLOUD_SESSION_RES_TYPE, OC_RSRVD_INTERFACE_DEFAULT);
        printf("server uri=[%s]\n", uri);
 
        OCCallbackData cbData;
@@ -553,8 +565,8 @@ int _signup()
        _set_auth_cert_info();
 
        char uri[MAX_URI_LENGTH] = { 0 };
-       snprintf(uri, MAX_URI_LENGTH, "%s%s%s",
-                       CLOUD_PROTOCOL, CLOUD_STG_SERVER, OC_RSRVD_ACCOUNT_URI);
+       snprintf(uri, MAX_URI_LENGTH, "%s%s%s?rt=%s;if=%s",
+                       CLOUD_PROTOCOL, CLOUD_STG_SERVER, OC_RSRVD_ACCOUNT_URI, CLOUD_ACCOUNT_RES_TYPE, OC_RSRVD_INTERFACE_DEFAULT);
        printf("server uri=[%s]\n", uri);
 
        OCCallbackData cbData;
@@ -671,11 +683,6 @@ OCRepPayload * _construct_response_of_device(OCEntityHandlerRequest *ehRequest)
 {
        OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
 
-       if (!input) {
-               printf("Failed to parse\n");
-               return NULL;
-       }
-
        if (OC_REST_PUT == ehRequest->method || OC_REST_POST == ehRequest->method) {
                /* Get pointer to query */
                bool power;
@@ -692,6 +699,7 @@ OCRepPayload * _construct_response_of_device(OCEntityHandlerRequest *ehRequest)
                return NULL;
        }
 
+       printf("Client Set payload Power : %d\n", switch_power);
        OCRepPayloadSetPropBool(payload, "value", switch_power);
 
        return payload;
@@ -773,6 +781,7 @@ OCEntityHandlerResult _handle_oc_entity(OCEntityHandlerFlag flag,
                                                                                OCEntityHandlerRequest *entityHandlerRequest,
                                                                                void *callbackParam)
 {
+       printf("_handle_oc_entity\n");
        OCEntityHandlerResult ehRet = OC_EH_OK;
        OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, \
                                                                                { }, { 0 }, false };
@@ -783,19 +792,28 @@ OCEntityHandlerResult _handle_oc_entity(OCEntityHandlerFlag flag,
                        printf("Received OC_REST_GET request\n");
                        ehRet = _handle_get_request(entityHandlerRequest, &payload);
                } else if (OC_REST_PUT == entityHandlerRequest->method) {
-                       printf("Received OC_REST_PUT request, nothing to do\n");
+                       printf("Received OC_REST_PUT request, from client\n");
+                       ehRet = _handle_post_request(entityHandlerRequest, &payload);
                } else if (OC_REST_POST == entityHandlerRequest->method) {
                        printf("Received OC_REST_POST from client\n");
                        ehRet = _handle_post_request(entityHandlerRequest, &payload);
+               } else {
+                       //OC_REST_DISCOVER, OC_REST_PRESENCE, OC_REST_OBSERVE_ALL, OC_REST_OBSERVE, OC_REST_DELETE, OC_REST_NOMETHOD
+                       printf("Received unsupported method %d from client\n", entityHandlerRequest->method);
+                       ehRet = OC_EH_ERROR;
                }
-
-               if (ehRet == OC_EH_OK) {
+               if (!((ehRet == OC_EH_ERROR) || (ehRet == OC_EH_FORBIDDEN))) {
                        response.requestHandle = entityHandlerRequest->requestHandle;
                        response.resourceHandle = entityHandlerRequest->resource;
                        response.ehResult = ehRet;
                        response.payload = (OCPayload*)payload;
                        response.persistentBufferFlag = 0;
 
+                       response.numSendVendorSpecificHeaderOptions = 0;
+                       memset(response.sendVendorSpecificHeaderOptions, 0,
+                                       sizeof(response.sendVendorSpecificHeaderOptions));
+                       memset(response.resourceUri, 0, sizeof(response.resourceUri));
+
                        // Send the response
                        if (OCDoResponse(&response) != OC_STACK_OK) {
                                printf("Error sending response\n");
@@ -812,14 +830,15 @@ OCEntityHandlerResult _handle_oc_entity(OCEntityHandlerFlag flag,
        }
 
        if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) {
-               printf("Flag includes OC_OBSERVE_FLAG");
+               printf("Flag includes OC_OBSERVE_FLAG\n");
                if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) {
-                       printf("Received OC_OBSERVE_REGISTER from client");
+                       printf("Received OC_OBSERVE_REGISTER from client\n");
                } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) {
-                       printf("Received OC_OBSERVE_DEREGISTER from client");
+                       printf("Received OC_OBSERVE_DEREGISTER from client\n");
                }
        }
 
+       OCPayloadDestroy(response.payload);
        return ehRet;
 }
 
@@ -829,10 +848,11 @@ OCStackApplicationResult _handle_rd_publish_callback(void *ctx, OCDoHandle handl
 {
        /* Debug */
        if (clientResponse) {
-               printf("RD resource response received, code: %d\n", clientResponse->result);
+               printf("RD resource response received, code: [%d]\n", clientResponse->result);
+               printf("############## Publish resource response received [%s]\n", clientResponse->resourceUri);
        }
 
-       _cond_signal();
+//     _cond_signal();
 
        return OC_STACK_KEEP_TRANSACTION;
 }
@@ -844,9 +864,10 @@ OCStackApplicationResult _handle_rd_delete_callback(void *ctx, OCDoHandle handle
        /* Debug */
        if (clientResponse) {
                printf("RD resource response received, code: %d\n", clientResponse->result);
+               printf("############## Publish resource response received [%s]\n", clientResponse->resourceUri);
        }
 
-       _cond_signal();
+//     _cond_signal();
 
        return OC_STACK_KEEP_TRANSACTION;
 }
@@ -920,15 +941,26 @@ int  _init_default_test_res()
 #endif
 
        /* Create air conditioner resource handle */
-       if (OCCreateResource(&res_handle[0], "x.com.samsung.da.device",
+       if (OCCreateResource(&res_handle[0],
+#ifdef _USE_WWST_
+                                               "oic.r.switch.binary",
+                                               "oic.if.baseline",
+                                               "/capability/switch/0",
+                                               _handle_oc_entity, NULL,
+                                               OC_DISCOVERABLE|OC_OBSERVABLE) != OC_STACK_OK) {
+#else
+                                               "x.com.samsung.da.device",
                                                OC_RSRVD_INTERFACE_DEFAULT,
                                                "/sec/aircon/0",
                                                _handle_oc_entity, NULL,
                                                OC_DISCOVERABLE) != OC_STACK_OK) {
+#endif
                printf("OCCreateResource() failed\n");
                return -1;
        }
 
+/* oic.if.b, oic.if.ll */
+/*
        if (OCBindResourceInterfaceToResource(res_handle[0], OC_RSRVD_INTERFACE_BATCH) != OC_STACK_OK) {
                printf("OCBindResourceInterfaceToResource() [%s] failed\n", OC_RSRVD_INTERFACE_BATCH);
        }
@@ -936,6 +968,7 @@ int  _init_default_test_res()
        if (OCBindResourceInterfaceToResource(res_handle[0], OC_RSRVD_INTERFACE_LL) != OC_STACK_OK) {
                printf("OCBindResourceInterfaceToResource() [%s] failed\n", OC_RSRVD_INTERFACE_LL);
        }
+*/
 
 //     if (OCBindResource(aircon_handle, binary_switch_handle) != OC_STACK_OK) {
 //             printf("OCBindResource() failed\n");
@@ -969,21 +1002,42 @@ int _publish_fw_res_to_rd()
        snprintf(uri, MAX_URI_LENGTH, "%s%s", CLOUD_PROTOCOL, CLOUD_STG_SERVER);
 
 #ifdef _USE_WWST_
-       if (OCRDPublish(NULL, uri, CT_ADAPTER_TCP|CT_IP_USE_V4,
-                       NULL, 0,
-                       &cb_data, OC_LOW_QOS) != OC_STACK_OK) {
-               printf("OCRDPublish() failed\n");
+//     if (OCRDPublish(NULL, uri, CT_ADAPTER_TCP|CT_IP_USE_V4,
+//                     NULL, 0,
+//                     &cb_data, OC_LOW_QOS) != OC_STACK_OK) {
+//             printf("OCRDPublish() failed\n");
+//             return -1;
+//     }
+//
+////   _lock_and_wait();
+//
+//     if (OCRDPublish(NULL, uri, CT_ADAPTER_TCP|CT_IP_USE_V4,
+//                     res_handle, 2,
+//                     &cb_data, OC_LOW_QOS) != OC_STACK_OK) {
+//             printf("OCRDPublish() failed\n");
+//             return -1;
+//     }
+
+       int ocResult = OC_STACK_OK;
+       ocResult = OCRDPublish(NULL, uri, (CT_ADAPTER_TCP | CT_IP_USE_V4), NULL, 0, &cb_data, OC_LOW_QOS);
+       if (ocResult != OC_STACK_OK) {
+               printf("Publish Resource with null handle failed");
                return -1;
        }
 
-       _lock_and_wait();
+       ocResult = OCRDPublish(NULL, uri, (CT_ADAPTER_TCP | CT_IP_USE_V4), &res_handle[0], 1, &cb_data, OC_LOW_QOS);
+       if (ocResult != OC_STACK_OK) {
+               printf("Publish Resource failed [%d]", ocResult);
+               return -1;
+       }
 
-       if (OCRDPublish(NULL, uri, CT_ADAPTER_TCP|CT_IP_USE_V4,
-                       res_handle, 2,
-                       &cb_data, OC_LOW_QOS) != OC_STACK_OK) {
-               printf("OCRDPublish() failed\n");
+       ocResult = OCRDPublish(NULL, uri, (CT_ADAPTER_TCP | CT_IP_USE_V4), &res_handle[1], 1, &cb_data, OC_LOW_QOS);
+       if (ocResult != OC_STACK_OK) {
+               printf("Publish firmware Resource failed [%d]", ocResult);
                return -1;
        }
+
+
 #else
        if (OCRDPublish(uri, CT_ADAPTER_TCP|CT_IP_USE_V4,
                        NULL, 0,
@@ -992,7 +1046,7 @@ int _publish_fw_res_to_rd()
                return -1;
        }
 
-       _lock_and_wait();
+//     _lock_and_wait();
 
        if (OCRDPublish(uri, CT_ADAPTER_TCP|CT_IP_USE_V4,
                        res_handle, 2,
@@ -1002,7 +1056,7 @@ int _publish_fw_res_to_rd()
        }
 #endif
 
-       _lock_and_wait();
+//     _lock_and_wait();
 
        controlee_check_firmware_upgraded();
 
@@ -1035,7 +1089,7 @@ int _delete_fw_res_from_rd()
        }
 #endif
 
-       _lock_and_wait();
+//     _lock_and_wait();
 
        return 0;
 }
@@ -1066,8 +1120,9 @@ void _show_menu()
        printf("============================================\n");
        printf("=== Controlee firmware resource test app ===\n");
        printf("============================================\n");
-       printf(" [A] Sign Up/In\n");
-       printf(" [Z] Sign Out\n");
+       printf(" [A] Create Resource & Sign Up/In & publish\n");
+       printf(" [U] Sign Up/In\n");
+       printf(" [O] Sign Out\n");
        printf(" [1] Create default resource\n");
        printf(" [2] Create firmware resource\n");
        printf(" [3] Publish resources to RD\n");
@@ -1119,10 +1174,18 @@ void* _main_thread(void *data)
                switch (str_ret[0]) {
                case 'A' :
                case 'a' :
+                       rv = _init_default_test_res();
+                       rv = _create_fw_res();
+                       g_auto_publish = true;
+                       rv = _signup();
+                       break;
+               case 'U' :
+               case 'u' :
+                       g_auto_publish = false;
                        rv = _signup();
                        break;
-               case 'Z' :
-               case 'z' :
+               case 'O' :
+               case 'o' :
                        rv = _signout();
                        break;
                case '1':
@@ -1191,6 +1254,7 @@ int main(int argc, char *argv[])
        bSignin = false;
        isExit = false;
        ps = NULL;
+       switch_power = true;
 
        mainloop = g_main_loop_new(NULL, false);