1c422b9c5dbad8b51810819c929f9fdff222fad4
[platform/upstream/iotivity.git] / resource / provisioning / src / OCCloudProvisioning.cpp
1 /* *****************************************************************
2  *
3  * Copyright 2016 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 "ocstack.h"
22 #include "OCCloudProvisioning.h"
23
24 namespace OC
25 {
26     void OCCloudProvisioning::callbackWrapper(void *ctx,
27             OCStackResult result,
28             void *data)
29     {
30         CloudProvisionContext* context = static_cast<CloudProvisionContext*>(ctx);
31
32         std::thread exec(context->callback, result, data);
33         exec.detach();
34
35         delete context;
36     }
37
38     void OCCloudProvisioning::aclIdResponseWrapper(void *ctx,
39             OCStackResult result,
40             void *data)
41     {
42         std::string aclId = "";
43         AclIdContext* context = static_cast<AclIdContext*>(ctx);
44
45         if ((OC_STACK_OK == result) && data)
46         {
47             aclId = (char *)data;
48         }
49         std::thread exec(context->callback, result, aclId);
50         exec.detach();
51
52         delete context;
53     }
54
55     OCCloudProvisioning::OCCloudProvisioning(std::string& ipAddr, uint16_t port)
56     {
57         memset(&m_devAddr, 0, sizeof(m_devAddr));
58         memcpy(m_devAddr.addr, ipAddr.c_str(), MAX_ADDR_STR_SIZE);
59         m_devAddr.port = port;
60         m_csdkLock = OCPlatform_impl::Instance().csdkLock();
61     }
62
63     OCCloudProvisioning::~OCCloudProvisioning(void)
64     {
65     }
66
67     OCStackResult OCCloudProvisioning::requestCertificate(ResponseCallBack callback)
68     {
69         if (!callback)
70         {
71             oclog() <<"Result callback can't be null";
72             return OC_STACK_INVALID_CALLBACK;
73         }
74
75         OCStackResult result;
76         auto cLock = m_csdkLock.lock();
77
78         if (cLock)
79         {
80             CloudProvisionContext *context = new CloudProvisionContext(callback);
81
82             std::lock_guard<std::recursive_mutex> lock(*cLock);
83             result = OCCloudCertificateIssueRequest(static_cast<void*>(context), &m_devAddr,
84                     &OCCloudProvisioning::callbackWrapper);
85         }
86         else
87         {
88             oclog() <<"Mutex not found";
89             result = OC_STACK_ERROR;
90         }
91         return result;
92     }
93
94     OCStackResult OCCloudProvisioning::getIndividualAclInfo(const std::string& aclId,
95                         ResponseCallBack callback)
96     {
97         if (!callback)
98         {
99             oclog() <<"Result callback can't be null";
100             return OC_STACK_INVALID_CALLBACK;
101         }
102
103         OCStackResult result;
104         auto cLock = m_csdkLock.lock();
105
106         if (cLock)
107         {
108             CloudProvisionContext *context = new CloudProvisionContext(callback);
109
110             std::lock_guard<std::recursive_mutex> lock(*cLock);
111             result = OCCloudAclIndividualGetInfo(static_cast<void*>(context), aclId.c_str(),
112                     &m_devAddr,
113                     &OCCloudProvisioning::callbackWrapper);
114         }
115         else
116         {
117             oclog() <<"Mutex not found";
118             result = OC_STACK_ERROR;
119         }
120         return result;
121     }
122
123     OCStackResult OCCloudProvisioning::getAclIdByDevice(const std::string& deviceId,
124                                                         AclIdResponseCallBack callback)
125     {
126         if (!callback)
127         {
128             oclog() <<"Result callback can't be null";
129             return OC_STACK_INVALID_CALLBACK;
130         }
131
132         OCStackResult result;
133         auto cLock = m_csdkLock.lock();
134
135         if (cLock)
136         {
137             AclIdContext *context = new AclIdContext(callback);
138
139             std::lock_guard<std::recursive_mutex> lock(*cLock);
140             result = OCCloudGetAclIdByDevice(static_cast<void*>(context), deviceId.c_str(),
141                     &m_devAddr,
142                     &OCCloudProvisioning::aclIdResponseWrapper);
143         }
144         else
145         {
146             oclog() <<"Mutex not found";
147             result = OC_STACK_ERROR;
148         }
149         return result;
150     }
151
152     OCStackResult OCCloudProvisioning::getCRL(ResponseCallBack callback)
153     {
154         if (!callback)
155         {
156             oclog() <<"Result callback can't be null";
157             return OC_STACK_INVALID_CALLBACK;
158         }
159
160         OCStackResult result;
161         auto cLock = m_csdkLock.lock();
162
163         if (cLock)
164         {
165             CloudProvisionContext *context = new CloudProvisionContext(callback);
166
167             std::lock_guard<std::recursive_mutex> lock(*cLock);
168             result = OCCloudGetCRL(static_cast<void*>(context), &m_devAddr,
169                     &OCCloudProvisioning::callbackWrapper);
170         }
171         else
172         {
173             oclog() <<"Mutex not found";
174             result = OC_STACK_ERROR;
175         }
176         return result;
177     }
178
179     OCStackResult OCCloudProvisioning::postCRL(const std::string& thisUpdate,
180                                               const std::string& nextUpdate,
181                                               const OCByteString *crl,
182                                               const stringArray_t *serialNumbers,
183                                               ResponseCallBack callback)
184     {
185         if (!callback)
186         {
187             oclog() <<"Result callback can't be null";
188             return OC_STACK_INVALID_CALLBACK;
189         }
190
191         OCStackResult result;
192         auto cLock = m_csdkLock.lock();
193
194         if (cLock)
195         {
196             CloudProvisionContext *context = new CloudProvisionContext(callback);
197
198             std::lock_guard<std::recursive_mutex> lock(*cLock);
199             result = OCCloudPostCRL(static_cast<void*>(context), thisUpdate.c_str(),
200                     nextUpdate.c_str(), crl, serialNumbers, &m_devAddr,
201                     &OCCloudProvisioning::callbackWrapper);
202         }
203         else
204         {
205             oclog() <<"Mutex not found";
206             result = OC_STACK_ERROR;
207         }
208         return result;
209     }
210
211 }