e1ce4f3240c42efb42ff19d811aa318781667cc9
[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         /**
43          * This class represents Remote Enrollee device instance. What operation the class provides:
44          * 1) Ownership transfer for enabling secured communication between Mediator and Enrollee
45          * devices.
46          * 2) Provision WiFi AP information used for which Enrollee is going to connect to the AP
47          * 3) Provision Device confiruation setting, i.e. language, country, and etc
48          * 4) Provision Cloud information used for which Enrollee is going to register to the cloud
49          */
50         class RemoteEnrollee
51         {
52         public:
53             ~RemoteEnrollee() = default;
54
55 #ifdef __WITH_DTLS__
56             /**
57              * Register Security status and other information callback handlers.
58              *
59              * @param secProvisioningDbCb Callback to be invoked when the stack expects a
60              *        path for the provisioning db.
61              * @param securityPinCb Callback to get security pin during pin based ownership transfer.
62              *
63              * @throws InvalidParameterException If callback is an empty function or null.
64              * @throws ESBadRequestException If registration is already completed.
65              *
66              * @see SecProvisioningResult
67              */
68             ESResult registerSecurityCallbackHandler(const SecurityPinCb securityPinCb,
69                     const SecProvisioningDbPathCb secProvisioningDbPathCb);
70 #endif //__WITH_DTLS__
71
72             /**
73              * Get an Enrollee's status which includes provisioning status and last error code
74              *
75              * @param callback will give the requested status
76              *
77              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
78              *
79              * @see GetStatusCb
80              */
81             void getStatus(const GetStatusCb callback);
82
83             /**
84              * Get an Enrollee's configuration which includes WiFi supported frequency and device name
85              *
86              * @param callback will give the requested configuration
87              *
88              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
89              *
90              * @see GetConfigurationStatusCb
91              */
92             void getConfiguration(const GetConfigurationStatusCb callback);
93
94              /**
95              * Do security provisioning such as ownership tranfer to Enrollee.
96              *
97              * @param callback will give the result if the security provisioning succeeds or fails for some reasons
98              *
99              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
100              *
101              * @see SecurityProvStatusCb
102              */
103             void provisionSecurity(const SecurityProvStatusCb callback);
104
105             /**
106              * Provision WiFi AP information and device configuration to Enrollee
107              * 1. WiFi AP information includes a SSID, password, auth type, and encryption type.
108              * 2. Device configuration includes a language (IETF language tags) and country (ISO 3166-1 Alpha-2)
109              *
110              * @param devProp a data structure storing the above information to be delivered
111              * @param callback will give the result if the provisioning succeeds or fails
112              *
113              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
114              *
115              * @see DeviceProp
116              * @see DevicePropProvStatusCb
117              */
118             void provisionDeviceProperties(const DeviceProp& devProp,
119                                                const DevicePropProvStatusCb callback);
120
121             /**
122              * Provision Cloud information to Enrollee, which includes Auth code, auth provider,
123              * Cloud interface server URL, and etc.
124              * In this function, Discovery for the Enrollee will happen again in a given network.
125              * Because, this function is expected to call *AFTER* the Enrollee disconnects its Soft AP
126              * and successfully connects to the certain WiFi AP. In that case, Mediator should discover
127              * the Enrollee with a certain Device ID in the network.
128              *
129              * @param cloudProp a data structure storing the above information to be delivered
130              * @param callback will give the result if the provisioning succeeds or fails
131              *
132              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
133              *
134              * @see CloudProp
135              * @see CloudPropProvStatusCb
136              */
137             void provisionCloudProperties(const CloudProp& cloudProp,
138                                               const CloudPropProvStatusCb callback);
139
140         private:
141             RemoteEnrollee(const std::shared_ptr< OC::OCResource > resource);
142
143             ESResult discoverResource();
144             void onDeviceDiscovered(const std::shared_ptr<OC::OCResource> resource);
145             void initCloudResource();
146
147             void getStatusHandler
148                 (const std::shared_ptr< GetEnrolleeStatus > status) const;
149             void getConfigurationStatusHandler
150                 (const std::shared_ptr< GetConfigurationStatus > status) const;
151             void devicePropProvisioningStatusHandler
152                 (const std::shared_ptr< DevicePropProvisioningStatus > status) const;
153             void cloudPropProvisioningStatusHandler
154                 (const std::shared_ptr< CloudPropProvisioningStatus > status) const;
155             void securityStatusHandler
156                 (const std::shared_ptr< SecProvisioningStatus > status) const;
157
158         private:
159             std::shared_ptr< OC::OCResource > m_ocResource;
160             std::shared_ptr< EnrolleeResource > m_enrolleeResource;
161             std::shared_ptr< EnrolleeSecurity > m_enrolleeSecurity;
162             std::shared_ptr< CloudResource > m_cloudResource;
163
164             std::string  m_deviceId;
165             bool m_discoveryResponse;
166
167             std::mutex m_discoverymtx;
168             std::condition_variable m_cond;
169
170             SecurityProvStatusCb m_securityProvStatusCb;
171             GetStatusCb m_getStatusCb;
172             GetConfigurationStatusCb m_getConfigurationStatusCb;
173             SecurityPinCb m_securityPinCb;
174             SecProvisioningDbPathCb m_secProvisioningDbPathCb;
175             DevicePropProvStatusCb m_devicePropProvStatusCb;
176             CloudPropProvStatusCb m_cloudPropProvStatusCb;
177
178             friend class EasySetup;
179         };
180     }
181 }
182
183 #endif //REMOTE_ENROLLEE_H_