replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / inc / RemoteEnrollee.h
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 #ifndef REMOTE_ENROLLEE_H_
22 #define REMOTE_ENROLLEE_H_
23
24 #include <memory>
25 #include <iostream>
26 #include <condition_variable>
27
28 #include "ESRichCommon.h"
29 #include "OCApi.h"
30
31 using namespace OC;
32
33 namespace OIC
34 {
35     namespace Service
36     {
37         class OCResource;
38         class EnrolleeResource;
39         class CloudResource;
40         class EnrolleeSecurity;
41
42         typedef std::function<void(const std::shared_ptr<OC::OCResource> resource)>  onDeviceDiscoveredCb;
43
44         /**
45          * This class represents Remote Enrollee device instance. What operation the class provides:
46          * 1) Ownership transfer for enabling secured communication between Mediator and Enrollee
47          * devices.
48          * 2) Provision WiFi AP information used for which Enrollee is going to connect to the AP
49          * 3) Provision Device confiruation setting, i.e. language, country, and etc
50          * 4) Provision Cloud information used for which Enrollee is going to register to the cloud
51          */
52         class RemoteEnrollee : public std::enable_shared_from_this<RemoteEnrollee>
53         {
54         public:
55             ~RemoteEnrollee() = default;
56
57             /**
58              * Get an Enrollee's status which includes provisioning status and last error code
59              *
60              * @param callback will give the requested status
61              *
62              * @throws ESInvalidParameterException If callback is null.
63              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
64              *
65              * @see GetStatusCb
66              */
67             void getStatus(const GetStatusCb callback);
68
69             /**
70              * Get an Enrollee's configuration which includes WiFi supported frequency and device name
71              *
72              * @param callback will give the requested configuration
73              *
74              * @throws ESInvalidParameterException If callback is null.
75              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
76              *
77              * @see GetConfigurationStatusCb
78              */
79             void getConfiguration(const GetConfigurationStatusCb callback);
80
81             /**
82              * Do security provisioning such as ownership tranfer to Enrollee.
83              *
84              * @param callback will give the result if the security provisioning succeeds or fails for some reasons
85              *
86              * @throws ESInvalidParameterException If callback is null.
87              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
88              *
89              * @see SecurityProvStatusCb
90              */
91             void provisionSecurity(const SecurityProvStatusCb callback);
92
93             /**
94              * Do security provisioning such as ownership tranfer to Enrollee which may require more
95              * specific user selections like a type of ownership transfer method or pre-configured
96              * pin number used to Pre-configured pin-based MOT.
97              *
98              * @param callback will give the result if the security provisioning succeeds or fails for some reasons.
99              *
100              * @throws ESInvalidParameterException If callback is null.
101              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
102              *
103              * @see SecurityProvStatusCb
104              */
105             void provisionSecurity(const SecurityProvStatusCbWithOption callback);
106
107             /**
108              * Provision WiFi AP information and device configuration to Enrollee
109              * 1. WiFi AP information includes a SSID, password, auth type, and encryption type.
110              * 2. Device configuration includes a language (IETF language tags) and country (ISO 3166-1 Alpha-2)
111              *
112              * @param devProp a data structure storing the above information to be delivered
113              * @param callback will give the result if the provisioning succeeds or fails
114              *
115              * @throws ESInvalidParameterException If callback is null.
116              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
117              *
118              * @see DeviceProp
119              * @see DevicePropProvStatusCb
120              */
121             void provisionDeviceProperties(const DeviceProp& devProp,
122                                                const DevicePropProvStatusCb callback);
123
124             /**
125              * Provision Cloud information to Enrollee, which includes Auth code, auth provider,
126              * Cloud interface server URL, and etc.
127              * In this function, Discovery for the Enrollee will happen again in a given network.
128              * Because, this function is expected to call *AFTER* the Enrollee disconnects its Soft AP
129              * and successfully connects to the certain WiFi AP. In that case, Mediator should discover
130              * the Enrollee with a certain Device ID in the network.
131              *
132              * @param cloudProp a data structure storing the above information to be delivered
133              * @param callback will give the result if the provisioning succeeds or fails
134              *
135              * @throws ESInvalidParameterException If callback is null.
136              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
137              *
138              * @see CloudProp
139              * @see CloudPropProvStatusCb
140              */
141             void provisionCloudProperties(const CloudProp& cloudProp,
142                                               const CloudPropProvStatusCb callback);
143
144             /**
145              * Provision Cloud information to Enrollee, which includes Auth code, auth provider,
146              * Cloud interface server URL, and etc.
147              * Note that, this API is skipping finding Enrollee in a given network. Instead, an OCResource
148              * given as a first parameter will be considered to the Enrollee for cloud provisioning.
149              *
150              * @param resource an OCResource corresponding to a target Enrollee for cloud provisioning
151              * @param cloudProp a data structure storing the above information to be delivered
152              * @param callback will give the result if the provisioning succeeds or fails
153              *
154              * @throws ESInvalidParameterException If callback is null.
155              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
156              *
157              * @see CloudProp
158              * @see CloudPropProvStatusCb
159              */
160             void provisionCloudProperties(const std::shared_ptr< OC::OCResource > resource,
161                                             const CloudProp& cloudProp,
162                                             const CloudPropProvStatusCb callback);
163
164             /**
165              * Notify an Enrollee to Connect WiFi/Cloud
166              *
167              * @param connectTypes Target configurations to be connected. E.g. WiFi and coap cloud server
168              * @param callback will give the result if the connect request succeeds or fails
169              *
170              * @see ES_CONNECT_TYPE
171              * @see ConnectRequestStatusCb
172              */
173             void requestToConnect(const std::vector<ES_CONNECT_TYPE> &connectTypes, const ConnectRequestStatusCb callback);
174
175         private:
176             RemoteEnrollee(const std::shared_ptr< OC::OCResource > resource);
177
178             ESResult discoverResource();
179
180             static void onDiscoveredCallback(const std::shared_ptr<OC::OCResource> resource,
181                 std::weak_ptr<RemoteEnrollee> this_ptr);
182
183             static void onGetStatusHandlerCallback(
184                 const std::shared_ptr< GetEnrolleeStatus > status,
185                 std::weak_ptr<RemoteEnrollee> this_ptr);
186
187             static void onGetConfigurationStatusHandlerCallback(
188                 const std::shared_ptr< GetConfigurationStatus > status,
189                 std::weak_ptr<RemoteEnrollee> this_ptr);
190
191             static void onDevicePropProvisioningStatusHandlerCallback(
192                 const std::shared_ptr< DevicePropProvisioningStatus > status,
193                 std::weak_ptr<RemoteEnrollee> this_ptr);
194
195             static void onCloudPropProvisioningStatusHandlerCallback(
196                 const std::shared_ptr< CloudPropProvisioningStatus > status,
197                 std::weak_ptr<RemoteEnrollee> this_ptr);
198
199             static void onConnectRequestStatusHandlerCallback(
200                 const std::shared_ptr< ConnectRequestStatus > status,
201                 std::weak_ptr<RemoteEnrollee> this_ptr);
202
203             static void onSecurityStatusHandlerCallback(
204                 const std::shared_ptr< SecProvisioningStatus > status,
205                 std::weak_ptr<RemoteEnrollee> this_ptr);
206
207             static ESOwnershipTransferData onSecurityStatusWithOptionHandlerCallback(
208                 const std::shared_ptr< SecProvisioningStatus > status,
209                 std::weak_ptr<RemoteEnrollee> this_ptr);
210
211             void onDeviceDiscovered(const std::shared_ptr<OC::OCResource> resource);
212             void initCloudResource();
213
214             void getStatusHandler
215                 (const std::shared_ptr< GetEnrolleeStatus > status) const;
216             void getConfigurationStatusHandler
217                 (const std::shared_ptr< GetConfigurationStatus > status) const;
218             void devicePropProvisioningStatusHandler
219                 (const std::shared_ptr< DevicePropProvisioningStatus > status) const;
220             void cloudPropProvisioningStatusHandler
221                 (const std::shared_ptr< CloudPropProvisioningStatus > status) const;
222             void connectRequestStatusHandler(
223                 const std::shared_ptr< ConnectRequestStatus > status) const;
224             void securityStatusHandler
225                 (const std::shared_ptr< SecProvisioningStatus > status) const;
226             ESOwnershipTransferData securityStatusWithOptionHandler
227                 (const std::shared_ptr< SecProvisioningStatus > status) const;
228
229         private:
230             std::shared_ptr< OC::OCResource > m_ocResource;
231             std::shared_ptr< EnrolleeResource > m_enrolleeResource;
232             std::shared_ptr< EnrolleeSecurity > m_localEnrolleeSecurity;
233             std::shared_ptr< EnrolleeSecurity > m_cloudEnrolleeSecurity;
234             std::shared_ptr< CloudResource > m_cloudResource;
235
236             std::string  m_deviceId;
237             bool m_discoveryResponse;
238
239             std::mutex m_discoverymtx;
240             std::condition_variable m_cond;
241
242             SecurityProvStatusCb m_securityProvStatusCb;
243             SecurityProvStatusCbWithOption m_securityProvStatusCbWithOption;
244             GetStatusCb m_getStatusCb;
245             GetConfigurationStatusCb m_getConfigurationStatusCb;
246             SecurityPinCb m_securityPinCb;
247             SecProvisioningDbPathCb m_secProvisioningDbPathCb;
248             DevicePropProvStatusCb m_devicePropProvStatusCb;
249             CloudPropProvStatusCb m_cloudPropProvStatusCb;
250             ConnectRequestStatusCb m_connectRequestStatusCb;
251
252             friend class EasySetup;
253         };
254     }
255 }
256
257 #endif //REMOTE_ENROLLEE_H_