Merge branch 'security-CKM' into 'master'
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceClient / RCSDiscoveryManager.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 "RCSDiscoveryManager.h"
22
23 #include "RCSRemoteResourceObject.h"
24 #include "PrimitiveResource.h"
25
26 #include "ScopeLogger.h"
27
28 #define TAG "RCSDiscoveryManager"
29
30 using namespace OIC::Service;
31
32 namespace
33 {
34     void findCallback(std::shared_ptr< PrimitiveResource > primitiveResource,
35             RCSDiscoveryManager::ResourceDiscoveredCallback cb)
36     {
37         SCOPE_LOG_F(DEBUG, TAG);
38
39         if (!primitiveResource)
40         {
41             OC_LOG(ERROR, TAG, "findCallback : primitiveResource is null.");
42             return;
43         }
44
45         cb(std::make_shared< RCSRemoteResourceObject >(primitiveResource));
46     }
47 }
48
49 namespace OIC
50 {
51     namespace Service
52     {
53         RCSDiscoveryManager* RCSDiscoveryManager::getInstance()
54         {
55             static RCSDiscoveryManager instance;
56             return &instance;
57         }
58
59         void RCSDiscoveryManager::discoverResource(const RCSAddress& address,
60                 ResourceDiscoveredCallback cb)
61         {
62             discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, "", std::move(cb));
63         }
64
65         void RCSDiscoveryManager::discoverResource(const RCSAddress& address,
66                 const std::string& relativeURI, ResourceDiscoveredCallback cb)
67         {
68             discoverResourceByType(address, relativeURI, "", std::move(cb));
69         }
70
71         void RCSDiscoveryManager::discoverResourceByType(const RCSAddress& address,
72                 const std::string& resourceType,
73                 ResourceDiscoveredCallback cb)
74         {
75             discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, resourceType, std::move(cb));
76         }
77
78         void RCSDiscoveryManager::discoverResourceByType(const RCSAddress& address,
79                 const std::string& relativeURI, const std::string& resourceType,
80                 ResourceDiscoveredCallback cb)
81         {
82             if (!cb)
83             {
84                 OC_LOG(ERROR, TAG, "discoverResourceByType NULL Callback");
85                 throw InvalidParameterException { "discoverResourceByType NULL Callback'" };
86             }
87             else
88             {
89                 std::string resourceURI = relativeURI + "?rt=" + resourceType;
90                 OIC::Service::discoverResource(address, resourceURI,
91                     std::bind(findCallback, std::placeholders::_1, std::move(cb)));
92             }
93         }
94     }
95 }