1 //****************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef OC_PROVISIONINGMANAGER_CXX_H_
22 #define OC_PROVISIONINGMANAGER_CXX_H_
26 #include "pinoxmcommon.h"
27 #include "ocprovisioningmanager.h"
29 #include "OCPlatform_impl.h"
33 class OCSecureResource;
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;
40 struct ProvisionContext
42 ResultCallBack callback;
43 ProvisionContext(ResultCallBack cb) : callback(cb){}
47 * This class is for credential's to be set to devices.
48 * The types supported are
50 * 1: symmetric pair-wise key
51 * 2: symmetric group key
53 * 8: signed asymmetric key (aka certificate)
58 OicSecCredType_t type;
61 Credential() = default;
62 Credential(OicSecCredType_t type, size_t size) : type(type), keySize(size)
66 * API to get credential type of device.
67 * @return credential type of device.
69 OicSecCredType_t getCredentialType() const
75 * API to get size of credential key type.
76 * @return size of credential key type.
78 size_t getCredentialKeySize() const
84 * API to set credential type of device.
85 * Device can have following credential types
86 * - symmetric pair-wise key
87 * - symmetric group key
89 * - signed asymmetric key (aka certificate)
91 * @param type credential type.
93 void setCredentialType(OicSecCredType_t type)
99 * API to set size of credential key type.
100 * @param keySize credential key size.
101 * @note can be either 128 or 256 for symmetric pair-wise key
103 void setCredentialKeySize(size_t keySize)
105 this->keySize = keySize;
113 * The API is responsible for initialization of the provisioning manager. It will load
114 * provisioning database which have owned device's list and their linked status.
116 * @param dbPath file path of the sqlite3 database.
118 * @return ::OC_STACK_OK in case of success and other value otherwise.
120 static OCStackResult provisionInit(const std::string& dbPath);
123 * API is responsible for discovery of devices in it's subnet. It will list
124 * all the device in subnet which are not yet owned.
126 * @param timeout Timeout in seconds, time until which function will listen to
127 * responses from server before returning the list of devices.
128 * @param list List of candidate devices to be provisioned.
129 * @return ::OC_STACK_OK in case of success and other value otherwise.
131 static OCStackResult discoverUnownedDevices(unsigned short timeout,
135 * API is responsible for discovery of devices in it's subnet. It will list
136 * all the device in subnet which are already owned by calling provisioning client.
138 * @param timeout Timeout in seconds, time until which function will listen to
139 * responses from server before returning the list of devices.
140 * @param list List of owned devices.
141 * @return ::OC_STACK_OK in case of success and other value otherwise.
143 static OCStackResult discoverOwnedDevices(unsigned short timeout,
147 * API is responsible for discovery of devices in specified endpoint.
148 * It will return when found one or more device even though timeout is not exceeded
150 * @param timeout Timeout in seconds, time until which function will listen to
151 * responses from server before returning the list of devices.
152 * @param host address of target endpoint
153 * @param connType connectivity type of endpoint
154 * @param list List of devices.
155 * @return ::OC_STACK_OK in case of success and other value otherwise.
157 static OCStackResult discoverSecureResource(unsigned short timeout,
158 const std::string& host,
159 OCConnectivityType connType,
163 * API for registering Ownership transfer methods for a particular transfer Type.
165 * @param oxm Ownership transfer method.
166 * @param callbackData CallbackData Methods for ownership transfer.
167 * @param inputPin Callback method to input pin for verification.
168 * @return ::OC_STACK_OK in case of success and other value otherwise.
170 static OCStackResult setOwnerTransferCallbackData(OicSecOxm_t oxm,
171 OTMCallbackData_t* callbackData, InputPinCallback inputPin);
174 * API to get status of all the devices in current subnet. The status include endpoint
175 * information and doxm information which can be extracted during owned and unowned
176 * discovery. Along with this information, API will provide information about
178 * Device can have following states
179 * - ON/OFF: Device is switched on or off.
181 * @param timeout Wait time for the API.
182 * @param ownedDevList List of owned devices.
183 * @param unownedDevList List of unowned devices.
184 * @return ::OC_STACK_OK in case of success and other value otherwise.
186 static OCStackResult getDevInfoFromNetwork(unsigned short timeout,
187 DeviceList_t &ownedDevList,
188 DeviceList_t &unownedDevList);
190 * Server API to register callback to display stack generated PIN.
192 * @param displayPin Callback Method to Display generated PIN.
193 * @return ::OC_STACK_OK in case of success and other value otherwise.
195 static OCStackResult setDisplayPinCB(GeneratePinCallback displayPin);
198 * API to remove device credential and ACL from all devices in subnet.
200 * @param resultCallback Callback provided by API user, callback will be called when
201 * credential revocation is finished.
202 * @param uuid Device uuid to be revoked.
203 * @param waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device
204 * discovery in seconds.
205 * @return ::OC_STACK_OK in case of success and other value otherwise.
207 static OCStackResult removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery,
209 ResultCallBack resultCallback);
214 * This class represents a secure virtual device, which can be provisioned by the
215 * provisioning client.
217 class OCSecureResource
220 std::weak_ptr<std::recursive_mutex> m_csdkLock;
221 OCProvisionDev_t *devPtr; // pointer to device.
225 OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock, OCProvisionDev_t *dPtr);
230 * API to provision credentials between two devices and ACLs for the devices who
233 * @param cred Type of credentials & key size to be provisioned to the device.
234 * @param acl1 ACL for device 1. If this is not required set NULL.
235 * @param device2 Second device to be provisioned.
236 * @param acl2 ACL for device 2. If this is not required set NULL.
237 * @param resultCallback Callback will be called when provisioning request receives
238 * a response from first resource server.
239 * @return ::OC_STACK_OK in case of success and other value otherwise.
241 OCStackResult provisionPairwiseDevices(const Credential &cred, const OicSecAcl_t* acl1,
242 const OCSecureResource &device2, const OicSecAcl_t* acl2,
243 ResultCallBack resultCallback);
246 * API to do ownership transfer for un-owned device.
248 * @param resultCallback Result callback function to be invoked when
249 * ownership transfer finished.
250 * @return ::OC_STACK_OK in case of success and other value otherwise.
252 OCStackResult doOwnershipTransfer(ResultCallBack resultCallback);
255 * API to send ACL information to resource.
257 * @param acl ACL to provision.
258 * @param resultCallback Callback will be called when provisioning request
259 * receives a response from resource server.
260 * @return ::OC_STACK_OK in case of success and other value otherwise.
262 OCStackResult provisionACL(const OicSecAcl_t* acl,
263 ResultCallBack resultCallback);
266 * API to provision credential to devices.
268 * @param cred Type of credentials to be provisioned to the device.
269 * @param device2 Second device' instance, representing resource to be provisioned.
270 * @param resultCallback Callback will be called when provisioning request receives
271 * a response from first resource server.
272 * @return ::OC_STACK_OK in case of success and other value otherwise.
274 OCStackResult provisionCredentials(const Credential &cred,
275 const OCSecureResource &device2,
276 ResultCallBack resultCallback);
279 * API to remove the credential & relationship between the two devices.
281 * @param device2 Second device information to be unlinked.
282 * @param resultCallback Callback provided by API user, callback will be called when
283 * device unlink is finished.
284 * @return ::OC_STACK_OK in case of success and other value otherwise.
286 OCStackResult unlinkDevices(const OCSecureResource &device2,
287 ResultCallBack resultCallback);
290 * API to remove device credential from all devices in subnet.
292 * @param resultCallback Callback provided by API user, callback will be called when
293 * credential revocation is finished.
294 * @param waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device
295 * discovery in seconds.
296 * @return ::OC_STACK_OK in case of success and other value otherwise.
298 OCStackResult removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
299 ResultCallBack resultCallback);
302 * API to provision DirectPairing to devices.
304 * @param pconf pointer to PCONF (Pairing Configuration).
305 * @param resultCallback Callback will be called when provisioning request receives
306 * a response from first resource server.
307 * @return ::OC_STACK_OK in case of success and other value otherwise.
309 OCStackResult provisionDirectPairing(const OicSecPconf_t *pconf,
310 ResultCallBack resultCallback);
313 * This method is used to get linked devices' IDs.
315 * @param uuidList Information about the list of linked devices uuids.
316 * @return ::OC_STACK_OK in case of success and other value otherwise.
318 OCStackResult getLinkedDevices(UuidList_t &uuidList);
321 * API to get the device ID of this resource.
324 std::string getDeviceID();
327 * API to get the information of device for provisioning.
328 * @return @ref OCProvisionDev_t Reference provides information of device for provisioning.
330 OCProvisionDev_t* getDevPtr()const;
333 * This function returns the device's IP address.
334 * @return device address.
336 std::string getDevAddr();
339 * This function returns the device's Status.
340 * @return Device status (1 = ON and 2 = OFF).
342 int getDeviceStatus();
345 * This function provides the owned status of the device.
346 * @return Device owned status.
348 bool getOwnedStatus();
352 * Common callback wrapper, which will be called from OC-APIs.
354 static void callbackWrapper(void* ctx, int nOfRes,
355 OCProvisionResult_t *arr, bool hasError);
358 void validateSecureResource();
362 #endif // OC_PROVISIONINGMANAGER_CXX_H_