9adbad9371da800072f990cc4444afb708fe8bf7
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / src / EasySetup.cpp
1 //******************************************************************\r
2 //\r
3 // Copyright 2015 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 \r
21 #include "EasySetup.h"\r
22 \r
23 #include "logger.h"\r
24 #include "ESException.h"\r
25 #include "RemoteEnrollee.h"\r
26 \r
27 namespace OIC\r
28 {\r
29     namespace Service\r
30     {\r
31         #define EASYSETUP_TAG "EASY_SETUP"\r
32 \r
33         EasySetup * EasySetup::s_instance = nullptr;\r
34 \r
35         EasySetup::EasySetup()\r
36         {\r
37 \r
38         }\r
39 \r
40         EasySetup* EasySetup::getInstance ()\r
41         {\r
42             if (s_instance == nullptr)\r
43             {\r
44                 s_instance = new EasySetup ();\r
45             }\r
46             return s_instance;\r
47         }\r
48 \r
49         RemoteEnrollee::shared_ptr EasySetup::findDeviceInProvisioningList (\r
50                             const EnrolleeNWProvInfo& enrolleeNWProvInfo)\r
51         {\r
52             OC_LOG(DEBUG,EASYSETUP_TAG,"Entered findDeviceInProvisioningList ()");\r
53 \r
54             std::vector< std::shared_ptr< RemoteEnrollee > >::iterator it;\r
55 \r
56             std::shared_ptr< RemoteEnrollee > remoteEnrollee = nullptr;\r
57             for(auto it : m_activeEnrolleeList)\r
58             {\r
59                 OC_LOG_V(DEBUG,EASYSETUP_TAG,"entered the iterator");\r
60 \r
61                 if (0 == memcmp(&it->getEnrolleeProvisioningInfo().netAddressInfo,\r
62                                 &enrolleeNWProvInfo.netAddressInfo, sizeof(EnrolleeNWProvInfo)))\r
63                 {\r
64                     remoteEnrollee = it;\r
65                     return remoteEnrollee;\r
66                 }\r
67             }\r
68 \r
69             OC_LOG_V(DEBUG,EASYSETUP_TAG,"Return nullptr for  findDeviceInProvisioningList call");\r
70             return remoteEnrollee;\r
71         }\r
72 \r
73         bool EasySetup::addDeviceToProvisioningList(const RemoteEnrollee::shared_ptr remoteEnrollee)\r
74         {\r
75             for (auto it : m_activeEnrolleeList)\r
76             {\r
77                 if (0 == memcmp(&it->getEnrolleeProvisioningInfo().netAddressInfo,\r
78                                 &remoteEnrollee->getEnrolleeProvisioningInfo().netAddressInfo,\r
79                                 sizeof(EnrolleeNWProvInfo)))\r
80                 {\r
81                     return false;\r
82                 }\r
83             }\r
84 \r
85             OC_LOG_V(DEBUG,EASYSETUP_TAG,"Adding new device RemoteEnrollee list");\r
86             m_activeEnrolleeList.push_back(remoteEnrollee);\r
87             return true;\r
88         }\r
89 \r
90         std::shared_ptr<RemoteEnrollee> EasySetup::createEnrolleeDevice (\r
91                                         const EnrolleeNWProvInfo& enrolleeNWProvInfo)\r
92         {\r
93             if (findDeviceInProvisioningList(enrolleeNWProvInfo) != nullptr)\r
94             {\r
95                 throw ESBadRequestException { "Device already created exception" };\r
96             }\r
97 \r
98             RemoteEnrollee::shared_ptr remoteEnrollee;\r
99 \r
100             remoteEnrollee = std::make_shared< RemoteEnrollee > (enrolleeNWProvInfo);\r
101 \r
102 \r
103             if (!addDeviceToProvisioningList (remoteEnrollee))\r
104             {\r
105                 return nullptr;\r
106             }\r
107 \r
108             return remoteEnrollee;\r
109         }\r
110 \r
111 \r
112     }\r
113 }\r
114 \r