adecd89908b4d149becb09752625b7c595766aa5
[platform/upstream/iotivity.git] / resource / include / OCProvisioningManager.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 _OCPROVISIONINGMANAGER_CXX_H
22 #define _OCPROVISIONINGMANAGER_CXX_H
23
24 #include <thread>
25
26 #include "pinoxmcommon.h"
27 #include "ocprovisioningmanager.h"
28 #include "OCApi.h"
29 #include "OCPlatform_impl.h"
30
31 namespace OC
32 {
33     class OCSecureResource;
34
35     typedef std::vector<std::shared_ptr<OCSecureResource>> DeviceList_t;
36     typedef std::vector<OicUuid_t> UuidList_t;
37     typedef std::vector<OCProvisionResult_t> PMResultList_t;
38     typedef std::function<void(PMResultList_t *result, int hasError)> ResultCallBack;
39
40     struct ProvisionContext
41     {
42         ResultCallBack callback;
43         ProvisionContext(ResultCallBack cb) : callback(cb){}
44     };
45
46     /**
47      * @brief: This class is for credential's to be set to devices.
48      * The types supported are
49      *              0:  no security mode
50      *              1:  symmetric pair-wise key
51      *              2:  symmetric group key
52      *              4:  asymmetric key
53      *              8:  signed asymmetric key (aka certificate)
54      *              16: PIN /password
55      */
56     class Credential
57     {
58             OicSecCredType_t type;
59             size_t keySize;
60         public:
61             Credential() = default;
62             Credential(OicSecCredType_t type, size_t size) : type(type), keySize(size)
63             {}
64
65             OicSecCredType_t getCredentialType() const
66             {
67                 return type;
68             }
69
70             size_t getCredentialKeySize() const
71             {
72                 return keySize;
73             }
74
75             void setCredentialType(OicSecCredType_t type)
76             {
77                 this->type = type;
78             }
79
80             void setCredentialKeySize(size_t keySize)
81             {
82                 this->keySize = keySize;
83             }
84     };
85
86     class OCSecure
87     {
88         public:
89             /**
90              * The API is responsible for initializaton of the provisioning manager. It will load
91              * provisioning database which have owned device's list and their linked status.
92              *
93              * @param[in] dbPath file path of the sqlite3 db
94              *
95              * @return OC_STACK_OK in case of success and other value otherwise.
96              */
97             static OCStackResult provisionInit(const std::string& dbPath);
98
99             /**
100              * API is responsible for discovery of devices in it's subnet. It will list
101              * all the device in subnet which are not yet owned.
102              *
103              * @param[in] timeout Timeout in seconds, time util which function will listen to
104              *                    responses from client before returning the list of devices.
105              * @param[out] list List of candidate devices to be provisioned
106              * @return OC_STACK_OK in case of success and other value otherwise.
107              */
108             static OCStackResult discoverUnownedDevices(unsigned short timeout,
109                     DeviceList_t &list);
110
111             /**
112              * API is responsible for discovery of devices in it's subnet. It will list
113              * all the device in subnet which are already owned by calling provioning client
114              *
115              * @param[in] timeout Timeout in seconds, time util which function will listen to
116              *                    responses from client before returning the list of devices.
117              * @param[out] list List of owned devices
118              * @return OC_STACK_OK in case of success and other value otherwise.
119              */
120             static OCStackResult discoverOwnedDevices(unsigned short timeout,
121                     DeviceList_t &list);
122
123             /**
124              * API for registering Ownershipt transfer methods for a particular transfer Type
125              *
126              * @param[in] oxm Ownership transfer method
127              * @param[in] callbackData Methods for ownership transfer
128              * @param[in] InputPinCallback Method to input pin for verification
129              * @return OC_STACK_OK in case of success and other value otherwise.
130              */
131             static OCStackResult setOwnerTransferCallbackData(OicSecOxm_t oxm,
132                     OTMCallbackData_t* callbackData, InputPinCallback inputPin);
133
134             /**
135              * API to get status of all the devices in current subnet. The status include endpoint
136              * information and doxm information which can be extracted duing owned and unowned
137              * discovery. Along with this information, API will provide information about
138              * devices' status.
139              * Device can have following states
140              *  - ON/OFF: Device is switched on or off.
141              *
142              * @param[in] timeout waitime for the API.
143              * @param[out] pOwnedDevList  list of owned devices.
144              * @param[out] pUnownedDevList  list of unowned devices.
145              * @return OC_STACK_OK in case of success and other value otherwise.
146              */
147             static OCStackResult getDevInfoFromNetwork(unsigned short timeout,
148                     DeviceList_t &ownedDevList,
149                     DeviceList_t &unownedDevList);
150             /**
151              * Server API to register callback to display stack generated PIN.
152              *
153              * @param[in] GeneratePinCallback Method to display generated PIN.
154              * @return OC_STACK_OK in case of success and other value otherwise.
155              */
156             static OCStackResult setDisplayPinCB(GeneratePinCallback);
157     };
158
159     /**
160      * This class represents a Secure virtual Device, which can be provisioned by the
161      * provisioning client.
162      */
163     class OCSecureResource
164     {
165         private:
166             std::weak_ptr<std::recursive_mutex> m_csdkLock;
167             OCProvisionDev_t *devPtr;   // pointer to device.
168
169         public:
170             OCSecureResource();
171             OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock, OCProvisionDev_t *dPtr);
172
173             ~OCSecureResource();
174
175             /**
176              * API to provision credentials between two devices and ACLs for the devices who
177              * act as a server.
178              *
179              * @param[in] cred  Type of credentials & key size to be provisioned to the device.
180              * @param[in] acl1  ACL for device 1. If this is not required set NULL.
181              * @param[in] device2  Second device to be provsioned.
182              * @param[in] acl2  ACL for device 2. If this is not required set NULL.
183              * @param[in] resultCallback Callback will be called when provisioning request recieves
184              *                           a response from first resource server.
185              * @return  OC_STACK_OK in case of success and other value otherwise.
186              */
187             OCStackResult provisionPairwiseDevices(const Credential &cred, const OicSecAcl_t* acl1,
188                     const OCSecureResource &device2, const OicSecAcl_t* acl2,
189                     ResultCallBack resultCallback);
190
191             /**
192              * API to do ownership transfer for un-owned device.
193              *
194              * @param[in] resultCallback Result callback function to be invoked when
195              *                           ownership transfer finished.
196              * @return OC_STACK_OK in case of success and other value otherwise.
197              */
198             OCStackResult doOwnershipTransfer(ResultCallBack resultCallback);
199
200             /**
201              * API to send ACL information to resource.
202              *
203              * @param[in] acl ACL to provision.
204              * @param[in] resultCallback callback will be called when provisioning request
205              *                           recieves a response from resource server.
206              * @return  OC_STACK_OK in case of success and other value otherwise.
207              */
208             OCStackResult provisionACL(const OicSecAcl_t* acl,
209                     ResultCallBack resultCallback);
210
211             /**
212              * API to provision credential to devices.
213              *
214              * @param[in] cred Type of credentials to be provisioned to the device.
215              * @param[in] device2 Second device' instance,respresenting resourceto be provsioned.
216              * @param[in] resultCallback callback will be called when provisioning request recieves
217              *                           a response from first resource server.
218              * @return  OC_STACK_OK in case of success and other value otherwise.
219              */
220             OCStackResult provisionCredentials(const Credential &cred,
221                     const OCSecureResource &device2,
222                     ResultCallBack resultCallback);
223
224             /*
225             * API to remove the credential & relasionship between the two devices.
226             *
227             * @param[in] pTargetDev2 second device information to be unlinked.
228             * @param[in] resultCallback callback provided by API user, callback will be called when
229             *            device unlink is finished.
230              * @return  OC_STACK_OK in case of success and other value otherwise.
231             */
232             OCStackResult unlinkDevices(const OCSecureResource &device2,
233                     ResultCallBack resultCallback);
234
235             /*
236              * API to remove device credential from all devices in subnet.
237              *
238              * @param[in] resultCallback callback provided by API user, callback will be called when
239              *            credential revocation is finished.
240              * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device
241              *            discovery.(seconds)
242              * @return  OC_STACK_OK in case of success and other value otherwise.
243              */
244             OCStackResult removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
245                     ResultCallBack resultCallback);
246
247             /**
248              * This method is used to get linked devices' IDs.
249              *
250              * @param[out] uuidList information about the list of linked devices' uuids.
251              * @param[out] numOfDevices total number of linked devices.
252              * @return  OC_STACK_OK in case of success and other value otherwise.
253              */
254             OCStackResult getLinkedDevices(UuidList_t &uuidList);
255
256             /**
257              * API to get the DeviceID of this resource
258              */
259             std::string getDeviceID();
260
261             OCProvisionDev_t* getDevPtr()const;
262
263             /**
264              * This function returns the Device's IP addr.
265              */
266             std::string getDevAddr();
267
268             /**
269              * This function returns the Device's Status
270              */
271             int getDeviceStatus();
272
273             /**
274              * This function provides the OWNED status of the device.
275              */
276             bool getOwnedStatus();
277
278         private:
279             /**
280              * Common callback wrapper, which will be called from OC-APIs.
281              */
282             static void callbackWrapper(void* ctx, int nOfRes,
283                     OCProvisionResult_t *arr, bool hasError);
284
285             void validateSecureResource();
286     };
287
288 }
289 #endif //_OCPROVISIONINGMANAGER_CXX_H