30ef55cc1a0e1f058a27a15f1ca2ca1dd0de447f
[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 ESBadRequestException If RemoteEnrollee device not created prior to this call.
63              *
64              * @see GetStatusCb
65              */
66             void getStatus(const GetStatusCb callback);
67
68             /**
69              * Get an Enrollee's configuration which includes WiFi supported frequency and device name
70              *
71              * @param callback will give the requested configuration
72              *
73              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
74              *
75              * @see GetConfigurationStatusCb
76              */
77             void getConfiguration(const GetConfigurationStatusCb callback);
78
79              /**
80              * Do security provisioning such as ownership tranfer to Enrollee.
81              *
82              * @param callback will give the result if the security provisioning succeeds or fails for some reasons
83              *
84              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
85              *
86              * @see SecurityProvStatusCb
87              */
88             void provisionSecurity(const SecurityProvStatusCb callback);
89
90             /**
91              * Provision WiFi AP information and device configuration to Enrollee
92              * 1. WiFi AP information includes a SSID, password, auth type, and encryption type.
93              * 2. Device configuration includes a language (IETF language tags) and country (ISO 3166-1 Alpha-2)
94              *
95              * @param devProp a data structure storing the above information to be delivered
96              * @param callback will give the result if the provisioning succeeds or fails
97              *
98              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
99              *
100              * @see DeviceProp
101              * @see DevicePropProvStatusCb
102              */
103             void provisionDeviceProperties(const DeviceProp& devProp,
104                                                const DevicePropProvStatusCb callback);
105
106             /**
107              * Provision Cloud information to Enrollee, which includes Auth code, auth provider,
108              * Cloud interface server URL, and etc.
109              * In this function, Discovery for the Enrollee will happen again in a given network.
110              * Because, this function is expected to call *AFTER* the Enrollee disconnects its Soft AP
111              * and successfully connects to the certain WiFi AP. In that case, Mediator should discover
112              * the Enrollee with a certain Device ID in the network.
113              *
114              * @param cloudProp a data structure storing the above information to be delivered
115              * @param callback will give the result if the provisioning succeeds or fails
116              *
117              * @throws ESBadRequestException If RemoteEnrollee device not created prior to this call.
118              *
119              * @see CloudProp
120              * @see CloudPropProvStatusCb
121              */
122             void provisionCloudProperties(const CloudProp& cloudProp,
123                                               const CloudPropProvStatusCb callback);
124
125         private:
126             RemoteEnrollee(const std::shared_ptr< OC::OCResource > resource);
127
128             ESResult discoverResource();
129
130             static void onDiscoveredCallback(const std::shared_ptr<OC::OCResource> resource,
131             std::weak_ptr<RemoteEnrollee> this_ptr);
132
133             void onDeviceDiscovered(const std::shared_ptr<OC::OCResource> resource);
134             void initCloudResource();
135
136             void getStatusHandler
137                 (const std::shared_ptr< GetEnrolleeStatus > status) const;
138             void getConfigurationStatusHandler
139                 (const std::shared_ptr< GetConfigurationStatus > status) const;
140             void devicePropProvisioningStatusHandler
141                 (const std::shared_ptr< DevicePropProvisioningStatus > status) const;
142             void cloudPropProvisioningStatusHandler
143                 (const std::shared_ptr< CloudPropProvisioningStatus > status) const;
144             void securityStatusHandler
145                 (const std::shared_ptr< SecProvisioningStatus > status) const;
146
147         private:
148             std::shared_ptr< OC::OCResource > m_ocResource;
149             std::shared_ptr< EnrolleeResource > m_enrolleeResource;
150             std::shared_ptr< EnrolleeSecurity > m_enrolleeSecurity;
151             std::shared_ptr< CloudResource > m_cloudResource;
152
153             std::string  m_deviceId;
154             bool m_discoveryResponse;
155
156             std::mutex m_discoverymtx;
157             std::condition_variable m_cond;
158
159             SecurityProvStatusCb m_securityProvStatusCb;
160             GetStatusCb m_getStatusCb;
161             GetConfigurationStatusCb m_getConfigurationStatusCb;
162             SecurityPinCb m_securityPinCb;
163             SecProvisioningDbPathCb m_secProvisioningDbPathCb;
164             DevicePropProvStatusCb m_devicePropProvStatusCb;
165             CloudPropProvStatusCb m_cloudPropProvStatusCb;
166
167             friend class EasySetup;
168         };
169     }
170 }
171
172 #endif //REMOTE_ENROLLEE_H_