[Update] change Api desgin and modify function & data structure naming issue.
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / src / CloudResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <functional>
22
23 #include "CloudResource.h"
24
25 #include "OCPlatform.h"
26 #include "ESException.h"
27 #include "OCResource.h"
28 #include "logger.h"
29
30 namespace OIC
31 {
32     namespace Service
33     {
34         #define ES_CLOUD_RES_TAG "ES_CLOUD_RESOURCE"
35
36         static const char ES_BASE_RES_URI[] = "/oic/res";
37
38         CloudResource::CloudResource(std::shared_ptr< OC::OCResource > resource)
39         {
40             m_ocResource = resource;
41         }
42
43         void CloudResource::provisionEnrollee(const CloudProp& cloudProp)
44         {
45             OIC_LOG_V (DEBUG, ES_CLOUD_RES_TAG, "Enter provisionEnrollee.");
46
47             OCRepresentation provisioningRepresentation;
48
49             provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHCODE, cloudProp.authCode);
50             provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHPROVIDER, cloudProp.authProvider);
51             provisioningRepresentation.setValue(OC_RSRVD_ES_CISERVER, cloudProp.ciServer);
52
53             OIC_LOG_V (DEBUG, ES_CLOUD_RES_TAG, "provisionEnrollee : authCode - %s",
54                     (cloudProp.authCode).c_str());
55             OIC_LOG_V (DEBUG, ES_CLOUD_RES_TAG, "provisionEnrollee : authProvider - %s",
56                     (cloudProp.authProvider).c_str());
57             OIC_LOG_V (DEBUG, ES_CLOUD_RES_TAG, "provisionEnrollee : ciServer - %s",
58                     (cloudProp.ciServer).c_str());
59
60             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
61                         provisioningRepresentation, QueryParamsMap(),
62                         std::function<
63                                 void(const HeaderOptions& headerOptions,
64                                         const OCRepresentation& rep, const int eCode) >(
65                         std::bind(&CloudResource::onCloudProvResponse, this,
66                         std::placeholders::_1, std::placeholders::_2,
67                         std::placeholders::_3)));
68         }
69
70         void CloudResource::onCloudProvResponse(const HeaderOptions& /*headerOptions*/,
71                 const OCRepresentation& rep, const int eCode)
72         {
73             OIC_LOG_V (DEBUG, ES_CLOUD_RES_TAG, "onCloudProvResponse : %s, eCode = %d",
74                     rep.getUri().c_str(), eCode);
75
76             if (eCode != 0)
77             {
78                 ESResult result  = ESResult::ES_ERROR;
79
80                 OIC_LOG(DEBUG, ES_CLOUD_RES_TAG,"onCloudProvResponse : onCloudProvResponse is failed ");
81
82                 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
83                 {
84                     OIC_LOG(DEBUG, ES_CLOUD_RES_TAG, "Mediator is unauthorized from Enrollee.");
85                     result = ESResult::ES_UNAUTHORIZED;
86                 }
87
88                 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
89                         CloudPropProvisioningStatus >(result, ESCloudProvState::ES_CLOUD_PROVISIONING_ERROR);
90                 m_cloudPropProvStatusCb(provStatus);
91             }
92             else
93             {
94                 OIC_LOG(DEBUG, ES_CLOUD_RES_TAG,"onCloudProvResponse : onCloudProvResponse is success ");
95                 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
96                         CloudPropProvisioningStatus >(ESResult::ES_OK, ESCloudProvState::ES_CLOUD_PROVISIONING_SUCCESS);
97                 m_cloudPropProvStatusCb(provStatus);
98             }
99         }
100
101         void CloudResource::registerCloudPropProvisioningStatusCallback(CloudPropProvStatusCb callback)
102         {
103             OIC_LOG_V (DEBUG, ES_CLOUD_RES_TAG, "Enter registerCloudPropProvisioningStatusCallback.");
104             m_cloudPropProvStatusCb = callback;
105         }
106     }
107 }