replace : iotivity -> iotivity-sec
[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::onCloudProvResponseSafetyCb(const HeaderOptions& headerOptions,
44                                                                 const OCRepresentation& rep,
45                                                                 const int eCode,
46                                                                 ESCloudResourceCb cb,
47                                                                 std::weak_ptr<CloudResource> this_ptr)
48         {
49             OIC_LOG(DEBUG, ES_CLOUD_RES_TAG, "onCloudProvResponseSafetyCb");
50             std::shared_ptr<CloudResource> Ptr = this_ptr.lock();
51             if(Ptr)
52             {
53                 cb(headerOptions, rep, eCode);
54             }
55         }
56
57
58         void CloudResource::provisionProperties(const CloudProp& cloudProp)
59         {
60             OIC_LOG(DEBUG, ES_CLOUD_RES_TAG, "provisionProperties IN");
61
62             OCRepresentation provisioningRepresentation = cloudProp.toOCRepresentation();
63
64             ESCloudResourceCb cb = std::bind(&CloudResource::onCloudProvResponseSafetyCb,
65                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
66                             static_cast<ESCloudResourceCb>(
67                             std::bind(&CloudResource::onCloudProvResponse, this,
68                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
69                             shared_from_this());
70
71             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE,
72                         provisioningRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos);
73
74             OIC_LOG(DEBUG, ES_CLOUD_RES_TAG, "provisionProperties OUT");
75         }
76
77         void CloudResource::onCloudProvResponse(const HeaderOptions& /*headerOptions*/,
78                 const OCRepresentation& /*rep*/, const int eCode)
79         {
80             OIC_LOG_V(DEBUG, ES_CLOUD_RES_TAG, "onCloudProvResponse : eCode = %d",
81                         eCode);
82
83             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
84             {
85                 ESResult result = ESResult::ES_ERROR;
86
87                 OIC_LOG(DEBUG, ES_CLOUD_RES_TAG,"onCloudProvResponse : onCloudProvResponse is failed ");
88
89                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
90                 {
91                     OIC_LOG(DEBUG, ES_CLOUD_RES_TAG,
92                             "can't receive any response from Enrollee by a timeout threshold.");
93                     result = ESResult::ES_COMMUNICATION_ERROR;
94                 }
95
96                 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
97                         CloudPropProvisioningStatus >(result);
98                 m_cloudPropProvStatusCb(provStatus);
99             }
100             else
101             {
102                 OIC_LOG(DEBUG, ES_CLOUD_RES_TAG,"onCloudProvResponse : onCloudProvResponse is success ");
103                 std::shared_ptr< CloudPropProvisioningStatus > provStatus =
104                     std::make_shared<CloudPropProvisioningStatus >(ESResult::ES_OK);
105                 m_cloudPropProvStatusCb(provStatus);
106             }
107         }
108
109         void CloudResource::registerCloudPropProvisioningStatusCallback(
110             const CloudPropProvStatusCb callback)
111         {
112             m_cloudPropProvStatusCb = callback;
113         }
114     }
115 }