Merge branch 'simulator'
[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     };
152
153     /**
154      * This class represents a Secure virtual Device, which can be provisioned by the
155      * provisioning client.
156      */
157     class OCSecureResource
158     {
159         private:
160             std::weak_ptr<std::recursive_mutex> m_csdkLock;
161             OCProvisionDev_t *devPtr;   // pointer to device.
162
163         public:
164             OCSecureResource();
165             OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock, OCProvisionDev_t *dPtr);
166
167             ~OCSecureResource();
168
169             /**
170              * API to provision credentials between two devices and ACLs for the devices who
171              * act as a server.
172              *
173              * @param[in] cred  Type of credentials & key size to be provisioned to the device.
174              * @param[in] acl1  ACL for device 1. If this is not required set NULL.
175              * @param[in] device2  Second device to be provsioned.
176              * @param[in] acl2  ACL for device 2. If this is not required set NULL.
177              * @param[in] resultCallback Callback will be called when provisioning request recieves
178              *                           a response from first resource server.
179              * @return  OC_STACK_OK in case of success and other value otherwise.
180              */
181             OCStackResult provisionPairwiseDevices(const Credential &cred, const OicSecAcl_t* acl1,
182                     const OCSecureResource &device2, const OicSecAcl_t* acl2,
183                     ResultCallBack resultCallback);
184
185             /**
186              * API to do ownership transfer for un-owned device.
187              *
188              * @param[in] resultCallback Result callback function to be invoked when
189              *                           ownership transfer finished.
190              * @return OC_STACK_OK in case of success and other value otherwise.
191              */
192             OCStackResult doOwnershipTransfer(ResultCallBack resultCallback);
193
194             /**
195              * API to send ACL information to resource.
196              *
197              * @param[in] acl ACL to provision.
198              * @param[in] resultCallback callback will be called when provisioning request
199              *                           recieves a response from resource server.
200              * @return  OC_STACK_OK in case of success and other value otherwise.
201              */
202             OCStackResult provisionACL(const OicSecAcl_t* acl,
203                     ResultCallBack resultCallback);
204
205             /**
206              * API to provision credential to devices.
207              *
208              * @param[in] cred Type of credentials to be provisioned to the device.
209              * @param[in] device2 Second device' instance,respresenting resourceto be provsioned.
210              * @param[in] resultCallback callback will be called when provisioning request recieves
211              *                           a response from first resource server.
212              * @return  OC_STACK_OK in case of success and other value otherwise.
213              */
214             OCStackResult provisionCredentials(const Credential &cred,
215                     const OCSecureResource &device2,
216                     ResultCallBack resultCallback);
217
218             /*
219             * API to remove the credential & relasionship between the two devices.
220             *
221             * @param[in] pTargetDev2 second device information to be unlinked.
222             * @param[in] resultCallback callback provided by API user, callback will be called when
223             *            device unlink is finished.
224              * @return  OC_STACK_OK in case of success and other value otherwise.
225             */
226             OCStackResult unlinkDevices(const OCSecureResource &device2,
227                     ResultCallBack resultCallback);
228
229             /*
230              * API to remove device credential from all devices in subnet.
231              *
232              * @param[in] resultCallback callback provided by API user, callback will be called when
233              *            credential revocation is finished.
234              * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device
235              *            discovery.(seconds)
236              * @return  OC_STACK_OK in case of success and other value otherwise.
237              */
238             OCStackResult removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
239                     ResultCallBack resultCallback);
240
241             /**
242              * This method is used to get linked devices' IDs.
243              *
244              * @param[out] uuidList information about the list of linked devices' uuids.
245              * @param[out] numOfDevices total number of linked devices.
246              * @return  OC_STACK_OK in case of success and other value otherwise.
247              */
248             OCStackResult getLinkedDevices(UuidList_t &uuidList);
249
250             /**
251              * API to get the DeviceID of this resource
252              */
253             std::string getDeviceID();
254
255             OCProvisionDev_t* getDevPtr()const;
256
257             /**
258              * This function returns the Device's IP addr.
259              */
260             std::string getDevAddr();
261
262             /**
263              * This function returns the Device's Status
264              */
265             int getDeviceStatus();
266
267             /**
268              * This function provides the OWNED status of the device.
269              */
270             bool getOwnedStatus();
271
272         private:
273             /**
274              * Common callback wrapper, which will be called from OC-APIs.
275              */
276             static void callbackWrapper(void* ctx, int nOfRes,
277                     OCProvisionResult_t *arr, bool hasError);
278
279             void validateSecureResource();
280     };
281
282 }
283 #endif //_OCPROVISIONINGMANAGER_CXX_H