Changed onPublish callback for remote-app-control 41/104441/1
authorkmook <kmook.choi@samsung.com>
Tue, 13 Dec 2016 09:30:28 +0000 (18:30 +0900)
committerkmook <kmook.choi@samsung.com>
Tue, 13 Dec 2016 09:30:28 +0000 (18:30 +0900)
Change-Id: Iff8820775e43fcabc3786efc2ac2998a44d1a0b2
Signed-off-by: kmook <kmook.choi@samsung.com>
daemon/service_provider/RemoteAppControlServiceProvider.cpp
daemon/service_provider/RemoteAppControlServiceProvider.h

index ea0eeb5..059b7fe 100755 (executable)
@@ -215,6 +215,68 @@ static void _app_control_cb(app_control_h request, app_control_h reply, app_cont
        app_control_cb_map.erase(find_iter);
 }
 
+#if !defined(_D2D_INTERNAL_ACL_)
+static void __acl_state_callback(void *userdata, const rdm_device_s* pRdmDevice)
+{
+       IF_FAIL_VOID_TAG(pRdmDevice, _E, "pRdmDevice is NULL");
+    _D("macAddress:%s aclState:%d", pRdmDevice->wifiMac, pRdmDevice->status);
+
+    conv::RemoteAppControlAclRequestInfo *aclInfo = reinterpret_cast<conv::RemoteAppControlAclRequestInfo*>(userdata);
+    string policy;
+
+       IF_FAIL_VOID_TAG(aclInfo, _E, "aclInfo casting failed");
+
+    switch (pRdmDevice->status)
+    {
+       case RDM_STATUS_ALLOW: // permitted
+               _D("Permitted");
+               policy = CONV_ACCESS_CONTROL_PERMITTED;
+               break;
+
+       case RDM_STATUS_DENY: // denied
+               _D("Denied");
+               policy = CONV_ACCESS_CONTROL_DENIED;
+               break;
+
+       case RDM_STATUS_UNDECIDED: // undecided
+               _D("Undecided");
+               policy = CONV_ACCESS_CONTROL_DENIED;
+               break;
+
+       case RDM_STATUS_REMOVED: // device removed
+               _D("Device Removed");
+               policy = CONV_ACCESS_CONTROL_DENIED;
+               break;
+       default:
+               _D("Unknown State");
+               policy = CONV_ACCESS_CONTROL_DENIED;
+               break;
+    }
+
+       iotcon_response_result_e result;
+       result = IOTCON_RESPONSE_OK;
+
+       iotcon_representation_h representation;
+       iotcon_representation_create(&representation);
+       iotcon_attributes_h attributes;
+       iotcon_attributes_create(&attributes);
+
+       iotcon_attributes_add_str(attributes, CONV_JSON_ACCESS_REQUEST_RESULT, (char*)policy.c_str());
+
+       iotcon_representation_set_attributes(representation, attributes);
+
+       _send_response(aclInfo->request, representation, result);
+
+       iotcon_attributes_destroy(attributes);
+       iotcon_representation_destroy(representation);
+
+       rdm_callback_deregister(aclInfo->aclHandler);
+
+       delete aclInfo;
+}
+#endif
+
+
 static int __access_control(iotcon_request_h request, iotcon_attributes_h attributes)
 {
        _D("Access Request Handling");
@@ -281,7 +343,15 @@ static int __access_control(iotcon_request_h request, iotcon_attributes_h attrib
                        policy = CONV_ACCESS_CONTROL_DENIED;
                } else if (ret == RDM_ERROR_INPROGRESS){
                        _D("UNDECIDED");
-                       policy = CONV_ACCESS_CONTROL_DENIED;
+                       conv::RemoteAppControlAclRequestInfo *aclRequest = new(std::nothrow) conv::RemoteAppControlAclRequestInfo();
+                       aclRequest->request = request;
+                       if (rdm_callback_register(__acl_state_callback, aclRequest, &aclRequest->aclHandler) == RDM_ERROR_SUCCESS) {
+                               _D("Register callback for ACL");
+                               return CONV_ERROR_NONE;
+                       } else {
+                               _D("Register callback failed");
+                               policy = CONV_ACCESS_CONTROL_DENIED;
+                       }
                }
                _D("rdm_request_access:%d", ret);
 #endif
@@ -333,6 +403,7 @@ static int __access_control(iotcon_request_h request, iotcon_attributes_h attrib
 static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h attributes)
 {
        char *hostAddress;
+       int req_id = -1;
        int ret = iotcon_request_get_host_address(request, &hostAddress);
        IF_FAIL_RETURN_TAG(ret == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_PARAMETER, _E, "iotcon_request_get_host_address() Fail(%d)", ret);
        string hostAddressString(hostAddress);
@@ -372,7 +443,7 @@ static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h at
            return CONV_ERROR_INVALID_OPERATION;
        }
 #endif
-
+       iotcon_attributes_get_int(attributes, CONV_JSON_REQ_ID, &req_id);
        app_control_h app_control;
        int reply = 0;
        char *appctl_char;
@@ -402,11 +473,8 @@ static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h at
                }
 
                if (!waiting_reply) {
-                       int req_id;
                        long long reply_id = get_req_id();
 
-                       iotcon_attributes_get_int(attributes, CONV_JSON_REQ_ID, &req_id);
-
                        app_control_cb_info_s cb_info;
                        cb_info.req_id = req_id;
                        cb_info.request_handle = request;
@@ -418,7 +486,15 @@ static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h at
                        if (ret != APP_CONTROL_ERROR_NONE) {
                                _E("Launch request failed(%d)", ret);
                                iotcon_response_result_e result = IOTCON_RESPONSE_ERROR;
-                               _send_response(request, NULL, result);
+                               iotcon_attributes_h attr;
+                               iotcon_representation_h rep;
+                               iotcon_attributes_create(&attr);
+                               iotcon_attributes_add_int(attr, CONV_JSON_REQ_ID, req_id);
+                               iotcon_representation_create(&rep);
+                               iotcon_representation_set_attributes(rep, attr);
+                               _send_response(request, rep, result);
+                               iotcon_representation_destroy(rep);
+                               iotcon_attributes_destroy(attr);
                                app_control_cb_map.erase(reply_id);
                        }
                }
@@ -426,6 +502,7 @@ static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h at
                bundle_free(appctl_bundle);
                app_control_destroy(app_control);
        } else {
+               _D("no waiting for reply. send response immediately");
                iotcon_response_result_e result;
                ret = app_control_send_launch_request(app_control, NULL, NULL);
 
@@ -439,7 +516,15 @@ static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h at
                bundle_free(appctl_bundle);
                app_control_destroy(app_control);
 
-               _send_response(request, NULL, result);
+               iotcon_attributes_h attr;
+               iotcon_representation_h rep;
+               iotcon_attributes_create(&attr);
+               iotcon_attributes_add_int(attr, CONV_JSON_REQ_ID, req_id);
+               iotcon_representation_create(&rep);
+               iotcon_representation_set_attributes(rep, attr);
+               _send_response(request, rep, result);
+               iotcon_representation_destroy(rep);
+               iotcon_attributes_destroy(attr);
        }
 
        return CONV_ERROR_NONE;
@@ -448,7 +533,6 @@ static int __app_control_launch(iotcon_request_h request, iotcon_attributes_h at
 //callback function handles put command for remote-app-control resource
 static int __handle_request(iotcon_representation_h rep, iotcon_request_h request)
 {
-#if defined(_D2D_INTERNAL_ACL_)
        iotcon_attributes_h attributes;
        char *requestType;
 
@@ -466,10 +550,6 @@ static int __handle_request(iotcon_representation_h rep, iotcon_request_h reques
        }
 
        return CONV_ERROR_INVALID_PARAMETER;
-#else
-       // temporarily blocked by security issue
-       return CONV_ERROR_INVALID_PARAMETER;
-#endif
 }
 
 //callback function handles requests for remote-app-control resource
@@ -479,43 +559,70 @@ void conv::RemoteAppControlServiceProvider::__iotcon_request_cb(iotcon_resource_
 
        int ret;
        iotcon_request_type_e type;
-       char *host_address;
+       char *hostAddress;
+       int req_id = -1;
+       iotcon_representation_h reqRep = NULL;
 
        IF_FAIL_VOID_TAG(request, _E, "request is NULL");
 
-       ret = iotcon_request_get_host_address(request, &host_address);
-       if (IOTCON_ERROR_NONE != ret) {
-               _E("iotcon_request_get_host_address() Fail(%d)", ret);
-               _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
-               return;
-       }
-       _I("host_address : %s", host_address);
+       ret = iotcon_request_get_representation(request, &reqRep);
+       IF_FAIL_CATCH_TAG(ret == IOTCON_ERROR_NONE, _E, "iotcon_response_get_representation() Failed(%d)", ret);
+
+       ret = iotcon_request_get_host_address(request, &hostAddress);
+       IF_FAIL_CATCH_TAG(ret == IOTCON_ERROR_NONE, _E, "iotcon_request_get_host_address() Failed(%d)", ret);
+
+       _I("host_address : %s", hostAddress);
 
        ret = iotcon_request_get_request_type(request, &type);
-       if (IOTCON_ERROR_NONE != ret) {
-               _E("iotcon_request_get_types() Fail(%d)", ret);
-               _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
-               return;
-       }
+       IF_FAIL_CATCH_TAG(ret == IOTCON_ERROR_NONE, _E, "iotcon_request_get_request_type() Failed(%d)", ret);
        _D("request type : %d", type);
 
        if (IOTCON_REQUEST_PUT == type) {
-               iotcon_representation_h req_repr;
                _I("PUT request");
+               IF_FAIL_CATCH_TAG(__handle_request(reqRep, request) == CONV_ERROR_NONE, _E, "__handle_request() Failed(%d)", ret);
+       }
 
-               ret = iotcon_request_get_representation(request, &req_repr);
-               if (IOTCON_ERROR_NONE != ret) {
-                       _E("iotcon_request_get_representation() Fail(%d)", ret);
-                       _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
-                       return;
-               }
+       if (reqRep) {
+               iotcon_representation_destroy(reqRep);
+               reqRep = NULL;
+       }
 
-               if (__handle_request(req_repr, request) != CONV_ERROR_NONE) {
-                       _E("__handle_request() Fail");
-                       _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
+       return;
+
+CATCH:
+       iotcon_attributes_h attributes;
+       iotcon_representation_h responseRep;
+       iotcon_representation_create(&responseRep);
+       iotcon_attributes_h responseAttributes;
+       iotcon_attributes_create(&responseAttributes);
+
+       if (reqRep != NULL) {
+               ret = iotcon_representation_get_attributes(reqRep, &attributes);
+               if (ret == IOTCON_ERROR_NONE) {
+                       iotcon_attributes_get_int(attributes, CONV_JSON_REQ_ID, &req_id);
                }
+       }
 
-               iotcon_representation_destroy(req_repr);
+       iotcon_attributes_add_int(responseAttributes, CONV_JSON_REQ_ID, req_id);
+       iotcon_representation_set_attributes(responseRep, responseAttributes);
+
+       _send_response(request, responseRep, IOTCON_RESPONSE_ERROR);
+
+       if (attributes) {
+               iotcon_attributes_destroy(attributes);
+               attributes = NULL;
+       }
+       if (responseAttributes) {
+               iotcon_attributes_destroy(responseAttributes);
+               responseAttributes = NULL;
+       }
+       if (reqRep) {
+               iotcon_representation_destroy(reqRep);
+               reqRep = NULL;
+       }
+       if (responseRep) {
+               iotcon_representation_destroy(responseRep);
+               reqRep = NULL;
        }
 }
 
@@ -782,53 +889,46 @@ static void __on_response(iotcon_remote_resource_h resource, iotcon_error_e err,
                iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
 {
        int ret;
+       int appcontrol_result = -1;
        iotcon_response_result_e response_result;
        iotcon_representation_h repr;
-
-       ret = iotcon_response_get_result(response, &response_result);
-       if (IOTCON_ERROR_NONE != ret) {
-               _E("iotcon_response_get_result() Fail(%d)", ret);
-               return;
-       }
-
-       if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) {
-               _E("_on_response_observe Response error(%d)", response_result);
-               return;
-       }
-
-       ret = iotcon_response_get_representation(response, &repr);
-
+       char* appctl_char = NULL;
+       char* appctl_request_char = NULL;
+       int appctl_result = -1;
        int req_id = -1;
        iotcon_attributes_h attributes;
-       iotcon_representation_get_attributes(repr, &attributes);
-       iotcon_attributes_get_int(attributes, CONV_JSON_REQ_ID, &req_id);
+       std::map<int, response_cb_info_s>::iterator find_iter = response_cb_map.end();
+       response_cb_info_s cb_info;
+       cb_info.requestObj = NULL;
 
-       std::map<int, response_cb_info_s>::iterator find_iter = response_cb_map.find(req_id);
+       ret = iotcon_response_get_representation(response, &repr);
+       IF_FAIL_CATCH_TAG(ret == IOTCON_ERROR_NONE, _E, "iotcon_response_get_representation() Failed(%d)", ret);
 
-       IF_FAIL_VOID_TAG(find_iter != response_cb_map.end(), _E, "No callback found for response");
+       iotcon_representation_get_attributes(repr, &attributes);
+       ret = iotcon_attributes_get_int(attributes, CONV_JSON_REQ_ID, &req_id);
+       IF_FAIL_CATCH_TAG(ret == IOTCON_ERROR_NONE, _E, "iotcon_attributes_get_int() Failed(%d)", ret);
 
-       response_cb_info_s cb_info = find_iter->second;
+       find_iter = response_cb_map.find(req_id);
+       IF_FAIL_CATCH_TAG(find_iter != response_cb_map.end(), _E, "No callback found for response");
+       cb_info = find_iter->second;
 
-       char* appctl_char;
+       ret = iotcon_response_get_result(response, &response_result);
+       IF_FAIL_CATCH_TAG(ret == IOTCON_ERROR_NONE, _E, "iotcon_response_get_result() Failed(%d)", ret);
+       IF_FAIL_CATCH_TAG(response_result == IOTCON_RESPONSE_RESOURCE_CHANGED, _E, "not response of PUT method(%d)", response_result);
 
        ret = iotcon_attributes_get_str(attributes, CONV_JSON_APP_CONTROL_REPLY, &appctl_char);
        if (IOTCON_ERROR_NONE != ret) {
                _E("iotcon_attributes_get_str() Fail(%d)", ret);
-               return;
        }
 
-       char* appctl_request_char;
        ret = iotcon_attributes_get_str(attributes, CONV_JSON_APP_CONTROL_REQUEST, &appctl_request_char);
        if (IOTCON_ERROR_NONE != ret) {
                _E("iotcon_attributes_get_str() Fail(%d)", ret);
-               return;
        }
 
-       int appctl_result;
-       ret = iotcon_attributes_get_int(attributes, CONV_JSON_APP_CONTROL_RESULT, &appctl_result);
-       if (IOTCON_ERROR_NONE != ret) {
-               _E("iotcon_attributes_get_int() Fail(%d)", ret);
-               return;
+       appcontrol_result = iotcon_attributes_get_int(attributes, CONV_JSON_APP_CONTROL_RESULT, &appctl_result);
+       if (IOTCON_ERROR_NONE != appcontrol_result) {
+               _E("iotcon_attributes_get_int() Fail(%d)", appcontrol_result);
        }
 
        _D(RED("publishing_response"));
@@ -837,22 +937,59 @@ static void __on_response(iotcon_remote_resource_h resource, iotcon_error_e err,
                conv::Json payload;
                conv::Json description;
 
-               if (cb_info.requestObj == NULL) {
-                       _E("listener_cb is not registered");
-               } else {
-                       string resultStr = to_string(appctl_result);
-                       payload.set(NULL, CONV_JSON_RESULT_TYPE, CONV_JSON_ON_PUBLISH);
+               string resultStr = to_string(appctl_result);
+               payload.set(NULL, CONV_JSON_RESULT_TYPE, CONV_JSON_ON_PUBLISH);
+               if (appctl_char) {
                        payload.set(NULL, CONV_JSON_APP_CONTROL_REPLY, appctl_char);
+               }
+               if (appctl_request_char) {
                        payload.set(NULL, CONV_JSON_APP_CONTROL_REQUEST, appctl_request_char);
+               }
+               if (appcontrol_result == IOTCON_ERROR_NONE) {
                        payload.set(NULL, CONV_JSON_APP_CONTROL_RESULT, resultStr.c_str());
-                       result.set(NULL, CONV_JSON_DESCRIPTION, cb_info.requestObj->getDescription());
-                       result.set(NULL, CONV_JSON_PAYLOAD, payload);
+               }
+               result.set(NULL, CONV_JSON_DESCRIPTION, cb_info.requestObj->getDescription());
+               result.set(NULL, CONV_JSON_PAYLOAD, payload);
+
+               cb_info.requestObj->publish(CONV_ERROR_NONE, result, 0, NULL);
+               _D("response published");
+       } else {
+               _D("request obj is NULL");
+       }
+       if (find_iter != response_cb_map.end()) {
+               response_cb_map.erase(find_iter);
+       }
+       return;
+CATCH:
+       if (cb_info.requestObj) {
+               conv::Json result;
+               conv::Json payload;
+               conv::Json description;
 
-                       cb_info.requestObj->publish(CONV_ERROR_NONE, result, 0, NULL);
-                       _D("response published");
+               string resultStr = to_string(appctl_result);
+               payload.set(NULL, CONV_JSON_RESULT_TYPE, CONV_JSON_ON_PUBLISH);
+               if (appctl_char) {
+                       payload.set(NULL, CONV_JSON_APP_CONTROL_REPLY, appctl_char);
+               }
+               if (appctl_request_char) {
+                       payload.set(NULL, CONV_JSON_APP_CONTROL_REQUEST, appctl_request_char);
+               }
+               if (appcontrol_result == IOTCON_ERROR_NONE) {
+                       payload.set(NULL, CONV_JSON_APP_CONTROL_RESULT, resultStr.c_str());
                }
+               payload.set(NULL, CONV_JSON_APP_CONTROL_RESULT, resultStr.c_str());
+               result.set(NULL, CONV_JSON_DESCRIPTION, cb_info.requestObj->getDescription());
+               result.set(NULL, CONV_JSON_PAYLOAD, payload);
+
+               cb_info.requestObj->publish(CONV_ERROR_INVALID_OPERATION, result, 0, NULL);
+               _D("response error published");
+       } else {
+               _D("request obj is NULL");
        }
-       response_cb_map.erase(find_iter);
+       if (find_iter != response_cb_map.end()) {
+               response_cb_map.erase(find_iter);
+       }
+       return;
 }
 
 int conv::RemoteAppControlServiceProvider::publishRequest(Request* requestObj)
@@ -890,15 +1027,13 @@ int conv::RemoteAppControlServiceProvider::publishRequest(Request* requestObj)
 
        delete[] appControl;
 
-       if (reply == 1) {
-               int req_id = get_req_id();
-               iotcon_attributes_add_int(attributes, CONV_JSON_REQ_ID, req_id);
+       int req_id = get_req_id();
+       iotcon_attributes_add_int(attributes, CONV_JSON_REQ_ID, req_id);
 
-               response_cb_info_s cb_info;
-               cb_info.req_id = req_id;
-               cb_info.requestObj = svcInfo->registeredRequest;
-               response_cb_map[req_id] = cb_info;
-       }
+       response_cb_info_s cb_info;
+       cb_info.req_id = req_id;
+       cb_info.requestObj = svcInfo->registeredRequest;
+       response_cb_map[req_id] = cb_info;
 
        iotcon_representation_set_attributes(representation, attributes);
        svcInfo->iotconInfoObj.iotconRepresentationHandle = representation;
@@ -907,11 +1042,12 @@ int conv::RemoteAppControlServiceProvider::publishRequest(Request* requestObj)
 
        error = iotcon_remote_resource_put(svcInfo->iotconInfoObj.iotconResourceHandle, representation, NULL, __on_response, NULL);
 
-       IF_FAIL_RETURN_TAG(error == IOTCON_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "iotcon_remote_resource_put failed");
-
-       if (reply != 1) {
+       if (error != IOTCON_ERROR_NONE) {
+               _E("iotcon_remote_resource_put failed");
+               response_cb_map.erase(req_id);
                Json result;
-               sendResponse(result, CONV_JSON_ON_PUBLISH, CONV_ERROR_NONE, svcInfo->registeredRequest);
+               sendResponse(result, CONV_JSON_ON_PUBLISH, CONV_ERROR_INVALID_OPERATION, svcInfo->registeredRequest);
+               return CONV_ERROR_INVALID_OPERATION;
        }
 
        return CONV_ERROR_NONE;
index 0c21ed4..455838b 100644 (file)
 #include "RemoteAppControlServiceInfo.h"
 
 namespace conv {
+#if !defined(_D2D_INTERNAL_ACL_)
+       class RemoteAppControlAclRequestInfo {
+               public:
+                       iotcon_request_h request;
+                       int     aclHandler;
+       };
+#endif
        class RemoteAppControlServiceProvider : public IServiceProvider  {
                public:
                        RemoteAppControlServiceProvider();