1 //******************************************************************
3 // Copyright 2016 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 ES_COMMON_RICH_H_
22 #define ES_COMMON_RICH_H_
31 #include "OCPlatform.h"
35 #include "securevirtualresourcetypes.h"
36 #include "OCProvisioningManager.hpp"
51 * @brief Properties of easysetup resource. It includes a provisioning status and last
61 EnrolleeStatus(const OCRepresentation& rep)
66 EnrolleeStatus(const EnrolleeStatus& enrolleeStatus) :
67 m_rep(enrolleeStatus.getRepresentation())
71 EnrolleeStatus(const EnrolleeStatus&& enrolleeStatus) :
72 m_rep(std::move(enrolleeStatus.getRepresentation()))
77 * Get a provisioning status property of Enrollee.
79 * @return a provisioning status property of Enrollee
81 ProvStatus getProvStatus() const
83 if(m_rep.hasAttribute(OC_RSRVD_ES_PROVSTATUS))
85 return static_cast<ProvStatus>(
86 m_rep.getValue<int>(OC_RSRVD_ES_PROVSTATUS));
92 * Get a last error code property of Enrollee.
94 * @return a last error code property of Enrollee.
96 ESErrorCode getLastErrCode() const
98 if(m_rep.hasAttribute(OC_RSRVD_ES_LAST_ERRORCODE))
100 return static_cast<ESErrorCode>(
101 m_rep.getValue<int>(OC_RSRVD_ES_LAST_ERRORCODE));
103 return ES_ERRCODE_NO_ERROR;
107 * Get OCRepresentation object
109 * @return OCRepresentation object
111 const OCRepresentation& getRepresentation() const
116 OCRepresentation m_rep;
120 * @brief Data class stored for provisioning of coap cloud server properties
135 CloudProp(const CloudProp& cloudProp) :
136 m_rep(cloudProp.toOCRepresentation()),
137 m_cloudID(cloudProp.getCloudID()),
138 m_credID(cloudProp.getCredID())
142 CloudProp(const CloudProp&& cloudProp) :
143 m_rep(std::move(cloudProp.toOCRepresentation())),
144 m_cloudID(cloudProp.getCloudID()),
145 m_credID(cloudProp.getCredID())
150 * Constructor with OCRepresentation object. This is used for JNI communication.
152 CloudProp(const OCRepresentation &rep)
160 * Set CoapCloudConf resource properties to be delivered to Enrollee
162 * @param authCode Auth code issued by OAuth2.0-compatible account server
163 * @param authProvider Auth provider ID
164 * @param ciServer Cloud interface server URL which an Enrollee is going to registered
166 void setCloudProp(string authCode, string authProvider, string ciServer)
168 m_rep.setValue(OC_RSRVD_ES_AUTHCODE, authCode);
169 m_rep.setValue(OC_RSRVD_ES_AUTHPROVIDER, authProvider);
170 m_rep.setValue(OC_RSRVD_ES_CISERVER, ciServer);
174 * Set CoapCloudConf resource properties with Access token to be delivered to Enrollee
176 * @param accessToken Access token which is given in a return of auth code issued by
177 * OAuth2.0-compatible account server
178 * @param tokenType Access token type, i.e. "bearer"
179 * @param authProvider Auth provider ID
180 * @param ciServer Cloud interface server URL which an Enrollee is going to registered
182 * @see OAUTH_TOKENTYPE
184 void setCloudPropWithAccessToken(string accessToken, OAUTH_TOKENTYPE tokenType,
185 string authProvider, string ciServer)
187 m_rep.setValue(OC_RSRVD_ES_ACCESSTOKEN, accessToken);
188 m_rep.setValue(OC_RSRVD_ES_ACCESSTOKEN_TYPE, tokenType);
189 m_rep.setValue(OC_RSRVD_ES_AUTHPROVIDER, authProvider);
190 m_rep.setValue(OC_RSRVD_ES_CISERVER, ciServer);
194 * Set CloudServer's UUID
196 * @param cloudID Cloud Interface server's UUID
198 void setCloudID(string cloudID)
204 * Set CloudServer's credential ID of certificate
206 * @param credID Cloud Interface server's credential ID of certificate
208 void setCredID(int credID)
214 * Get an auth code to be delivered.
216 * @return an auth code to be delivered.
218 std::string getAuthCode() const
220 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHCODE))
222 return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHCODE);
224 return std::string("");
228 * Get an auth provider which issued an auth code
230 * @return an auth provider which issued an auth code
232 std::string getAuthProvider() const
234 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHPROVIDER))
236 return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHPROVIDER);
238 return std::string("");
242 * Get a CI server to be delivered
244 * @return a CI server to be delivered
246 std::string getCiServer() const
248 if(m_rep.hasAttribute(OC_RSRVD_ES_CISERVER))
250 return m_rep.getValue<std::string>(OC_RSRVD_ES_CISERVER);
252 return std::string("");
256 * Get a CI server's Uuid to be delivered
258 * @return a CI server's Uuid to be delivered
260 std::string getCloudID() const
266 * Get a CI server's credential ID of certificate
268 * @return a CI server's credential ID of certificated
270 int getCredID() const
276 * Get an access token to be delivered.
278 * @return an access token to be delivered.
280 std::string getAccessToken() const
282 if(m_rep.hasAttribute(OC_RSRVD_ES_ACCESSTOKEN))
284 return m_rep.getValue<std::string>(OC_RSRVD_ES_ACCESSTOKEN);
286 return std::string("");
290 * Get an access token type to be delivered.
292 * @return an access token type to be delivered.
294 OAUTH_TOKENTYPE getAccessTokenType() const
297 if(m_rep.hasAttribute(OC_RSRVD_ES_ACCESSTOKEN_TYPE))
299 return static_cast<OAUTH_TOKENTYPE>(
300 m_rep.getValue<int>(OC_RSRVD_ES_ACCESSTOKEN_TYPE));
302 return NONE_OAUTH_TOKENTYPE;
306 * Get OCRepresentation object
308 * @return OCRepresentation object
310 const OCRepresentation &toOCRepresentation() const
315 OCRepresentation m_rep;
316 std::string m_cloudID;
321 * @brief Data class stored for provisioning of Device properties which includes
322 * properties of WiFiConf resource and DevConf resource
335 DeviceProp(const DeviceProp& deviceProp) :
336 m_rep(deviceProp.toOCRepresentation())
340 DeviceProp(const DeviceProp&& deviceProp) :
341 m_rep(std::move(deviceProp.toOCRepresentation()))
346 * Constructor with OCRepresentation object. This is used for JNI communication.
348 DeviceProp(const OCRepresentation &rep)
354 * Set WiFiConf resource properties to be delivered to Enrollee
356 * @param ssid Ssid of the Enroller
357 * @param pwd Pwd of the Enrolle
358 * @param authtype Auth type of the Enroller
359 * @param enctype Encryption type of the Enroller
364 void setWiFiProp(string ssid, string pwd, WIFI_AUTHTYPE authtype, WIFI_ENCTYPE enctype)
366 m_rep.setValue(OC_RSRVD_ES_SSID, ssid);
367 m_rep.setValue(OC_RSRVD_ES_CRED, pwd);
368 m_rep.setValue(OC_RSRVD_ES_AUTHTYPE, authtype);
369 m_rep.setValue(OC_RSRVD_ES_ENCTYPE, enctype);
373 * Get a SSID of Enroller
375 * @return a SSID of enroller
377 std::string getSsid() const
379 if(m_rep.hasAttribute(OC_RSRVD_ES_SSID))
381 return m_rep.getValue<std::string>(OC_RSRVD_ES_SSID);
383 return std::string("");
387 * Get a password of Enroller
389 * @return a password of enroller
391 std::string getPassword() const
393 if(m_rep.hasAttribute(OC_RSRVD_ES_CRED))
395 return m_rep.getValue<std::string>(OC_RSRVD_ES_CRED);
397 return std::string("");
401 * Get an auth type of Enroller
403 * @return an auth type of enroller
407 WIFI_AUTHTYPE getAuthType() const
409 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHTYPE))
411 return static_cast<WIFI_AUTHTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_AUTHTYPE));
417 * Get an encryption type of Enroller
419 * @return an encryption type of enroller
423 WIFI_ENCTYPE getEncType() const
425 if(m_rep.hasAttribute(OC_RSRVD_ES_ENCTYPE))
427 return static_cast<WIFI_ENCTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_ENCTYPE));
433 * Get OCRepresentation object
435 * @return OCRepresentation object
437 const OCRepresentation &toOCRepresentation() const
443 OCRepresentation m_rep;
447 * Security Provisioning Status
449 class SecProvisioningStatus
452 SecProvisioningStatus(string deviceUUID, ESResult result) :
453 m_devUUID(deviceUUID), m_result(result)
456 m_selectedOTMethod = OIC_JUST_WORKS;
457 m_isMOTEnabled = false;
463 SecProvisioningStatus(std::shared_ptr<OCSecureResource> resource, ESResult result) :
466 m_isMOTEnabled = false;
467 if(resource.get() != nullptr)
469 m_devUUID = resource->getDeviceID();
470 m_isOwned = resource->getOwnedStatus();
471 #ifdef MULTIPLE_OWNER
472 m_isMOTEnabled = resource->isMOTEnabled();
475 if( OC_STACK_OK != resource->getOTMethod(&m_selectedOTMethod) )
477 m_selectedOTMethod = OIC_OXM_COUNT; // Out-of-range
480 if(resource->getOwnedStatus())
482 char uuidString[UUID_STRING_SIZE] = {};
483 if(RAND_UUID_OK == OCConvertUuidToString(resource->getDevPtr()->doxm->owner.id, uuidString))
485 m_ownerID = uuidString;
495 OicSecOxm_t getSelectedOTMethod() const
497 return m_selectedOTMethod;
500 bool isMOTEnabled() const
502 return m_isMOTEnabled;
505 bool isOwnedDevice() const
510 const std::string getOwnerID()
515 const std::string getDeviceUUID()
520 * Get a result for about security provisioning is success or not.
523 * ::ES_SEC_OPERATION_IS_NOT_SUPPORTED\n
524 * ::ES_SECURE_RESOURCE_DISCOVERY_FAILURE\n
525 * ::ES_OWNERSHIP_TRANSFER_FAILURE\n
528 ESResult getESResult()
536 OicSecOxm_t m_selectedOTMethod;
539 std::string m_ownerID;
544 * @breif This provide a set of getter APIs from received response for getConfiguration().
545 * Received information includes a device name, WiFi supported mode, and frequency.
546 * Additionally, you can know if Enrollee can be access to cloud server with this
554 * The expected OCRepresentation is one for collection resource and has several child
555 * OCRepresentation object corresponding to WiFiConf, DevConf, and CoapCloudConf
556 * resources' representations.
558 EnrolleeConf(const OCRepresentation& rep) :
563 EnrolleeConf(const EnrolleeConf& enrolleeConf) :
564 m_EasySetupRep(enrolleeConf.getEasySetupRep())
568 EnrolleeConf(const EnrolleeConf&& enrolleeConf) :
569 m_EasySetupRep(std::move(enrolleeConf.getEasySetupRep()))
574 * Get a device name of Enrollee. It is Device's human-friendly name like device model
577 * @return a device name of Enrollee
579 std::string getDeviceName() const
581 std::vector<OCRepresentation> children = m_EasySetupRep.getChildren();
582 for(auto child = children.begin(); child != children.end(); ++child)
584 if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
586 OCRepresentation rep;
587 if(child->hasAttribute(OC_RSRVD_REPRESENTATION))
589 rep = child->getValue<OCRepresentation>(OC_RSRVD_REPRESENTATION);
593 return std::string("");
596 if(rep.hasAttribute(OC_RSRVD_ES_DEVNAME))
598 return rep.getValue<std::string>(OC_RSRVD_ES_DEVNAME);
602 return std::string("");
606 * Get a set of WiFi supported modes of Enrollee
608 * @return a set of WiFi supported modes of Enrollee
612 vector<WIFI_MODE> getWiFiModes() const
614 vector<WIFI_MODE> modes;
617 std::vector<OCRepresentation> children = m_EasySetupRep.getChildren();
618 for(auto child = children.begin(); child != children.end(); ++child)
620 if(child->getUri().find(OC_RSRVD_ES_URI_WIFICONF) != std::string::npos)
622 OCRepresentation rep;
623 if(child->hasAttribute(OC_RSRVD_REPRESENTATION))
625 rep = child->getValue<OCRepresentation>(OC_RSRVD_REPRESENTATION);
632 if(rep.hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
634 for(auto it : rep.getValue
635 <std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
637 modes.push_back(static_cast<WIFI_MODE>(it));
646 * Get a WiFi supported frequency of Enrollee
648 * @return a WiFi supported frequency of Enrollee
652 WIFI_FREQ getWiFiFreq() const
654 std::vector<OCRepresentation> children = m_EasySetupRep.getChildren();
655 for(auto child = children.begin(); child != children.end(); ++child)
657 if(child->getUri().find(OC_RSRVD_ES_URI_WIFICONF) != std::string::npos)
659 OCRepresentation rep;
660 if(child->hasAttribute(OC_RSRVD_REPRESENTATION))
662 rep = child->getValue<OCRepresentation>(OC_RSRVD_REPRESENTATION);
666 return WIFI_FREQ_NONE;
669 if(rep.hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
671 return static_cast<WIFI_FREQ>(
672 rep.getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
676 return WIFI_FREQ_NONE;
680 * Get a provisioning status property of Enrollee.
682 * @return a provisioning status property of Enrollee
684 ProvStatus getProvStatus() const
686 OCRepresentation rep;
687 if(m_EasySetupRep.hasAttribute(OC_RSRVD_REPRESENTATION))
689 rep = m_EasySetupRep.getValue<OCRepresentation>(OC_RSRVD_REPRESENTATION);
693 return ES_STATE_INIT;
696 if(rep.hasAttribute(OC_RSRVD_ES_PROVSTATUS))
698 return static_cast<ProvStatus>(
699 rep.getValue<int>(OC_RSRVD_ES_PROVSTATUS));
701 return ES_STATE_INIT;
705 * Get a last error code property of Enrollee.
707 * @return a last error code property of Enrollee.
709 ESErrorCode getLastErrCode() const
711 OCRepresentation rep;
712 if(m_EasySetupRep.hasAttribute(OC_RSRVD_REPRESENTATION))
714 rep = m_EasySetupRep.getValue<OCRepresentation>(OC_RSRVD_REPRESENTATION);
718 return ES_ERRCODE_NO_ERROR;
721 if(rep.hasAttribute(OC_RSRVD_ES_LAST_ERRORCODE))
723 return static_cast<ESErrorCode>(
724 rep.getValue<int>(OC_RSRVD_ES_LAST_ERRORCODE));
726 return ES_ERRCODE_NO_ERROR;
730 * Get an accessibility to cloud server of an Enrollee
732 * @return an accessibility to cloud server of an Enrollee
734 bool isCloudAccessible() const
736 std::vector<OCRepresentation> children = m_EasySetupRep.getChildren();
737 for(auto child = children.begin(); child != children.end(); ++child)
739 for(auto rt : child->getResourceTypes())
741 if(0 == rt.compare(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF))
751 * Get OCRepresentation object
753 * @return OCRepresentation object
755 const OCRepresentation& getEasySetupRep() const
757 return m_EasySetupRep;
761 OCRepresentation m_EasySetupRep;
765 * Status object for getStatus API. This object is given to application
766 * when a response for GET request to provisioning resource at Enrollee is arrived.
767 * It returns a result of the API and requested data delivered in the response which includes
768 * a provisioning status and last error code stored in Enrollee.
770 * @see EnrolleeStatus
772 class GetEnrolleeStatus
778 GetEnrolleeStatus(ESResult result, const EnrolleeStatus& status) :
779 m_result(result), m_enrolleeStatus(status)
784 * Get a result of getting provisioning status and last error code of Enrollee
787 * ::ES_COMMUNICATION_ERROR\n
791 ESResult getESResult()
797 * Get Enrollee's status and last error code properties
799 * @return Enrollee's status and last error code properties
801 * @see EnrolleeStatus
803 const EnrolleeStatus& getEnrolleeStatus()
805 return m_enrolleeStatus;
810 EnrolleeStatus m_enrolleeStatus;
814 * Status object for getConfiguration API. This object is given to application
815 * when a response for GET request to provisioning resource at Enrollee is arrived.
816 * It returns a result of the API and requested data delivered in the response which includes
817 * WiFi configuration and device configuration stored in Enrollee.
821 class GetConfigurationStatus
827 GetConfigurationStatus(ESResult result, const EnrolleeConf& conf) :
828 m_result(result), m_enrolleeConf(conf)
833 * Get a result of getting preconfiguration of Enrollee
836 * ::ES_COMMUNICATION_ERROR\n
841 ESResult getESResult()
847 * Get Enrollee's pre-configuration properties
849 * @return Enrollee's pre-configuration properties
853 EnrolleeConf& getEnrolleeConf()
855 return m_enrolleeConf;
860 EnrolleeConf m_enrolleeConf;
864 * Status object for provisionDeviceProperties API. This object is given to application
865 * when a response for GET request to provisioning resource at Enrollee is arrived.
866 * It returns a result of the request.
868 class DevicePropProvisioningStatus
874 DevicePropProvisioningStatus(ESResult result) :
880 * Get a result of Device property provisioning
883 * ::ES_COMMUNICATION_ERROR\n
888 ESResult getESResult()
898 * Status object for provisionCloudProperties API. This object is given to application
899 * when a response for GET request to provisioning resource at Enrollee is arrived.
900 * It returns a result of the request and status of this provisioning. The status provides
901 * an information if the enrollee is found in a given network and the provisioning is
904 class CloudPropProvisioningStatus
910 CloudPropProvisioningStatus(ESResult result) :
916 * Get a result of Cloud property provisioning
919 * ::ES_ENROLLEE_DISCOVERY_FAILURE\n
920 * ::ES_SECURE_RESOURCE_DISCOVERY_FAILURE\n
921 * ::ES_ACL_PROVISIONING_FAILURE\n
922 * ::ES_CERT_PROVISIONING_FAILURE\n
923 * ::ES_COMMUNICATION_ERROR\n
928 ESResult getESResult()
938 * Status object for connect API. This object is given to application
939 * when a response for 'Connect' request from Enrollee is arrived.
941 class ConnectRequestStatus
947 ConnectRequestStatus(ESResult result) :
953 * Get a result of Connect request
956 * ::ES_COMMUNICATION_ERROR\n
961 ESResult getESResult()
970 class ESOwnershipTransferData
974 ESOwnershipTransferData() :
975 m_MOTMethod(OIC_OXM_COUNT), m_preconfiguredPin("")
979 ESOwnershipTransferData(const ESOwnershipTransferData& data) :
980 m_MOTMethod(data.getMOTMethod()),
981 m_preconfiguredPin(data.getPreConfiguredPin())
985 ESResult setMOTMethod(OicSecOxm_t method)
987 #ifdef MULTIPLE_OWNER
988 if(OIC_RANDOM_DEVICE_PIN != method)
993 m_MOTMethod = method;
1002 ESResult setMOTMethod(OicSecOxm_t method, const std::string& pin)
1004 #ifdef MULTIPLE_OWNER
1005 if(OIC_PRECONFIG_PIN != method || pin.empty())
1010 m_preconfiguredPin = pin;
1011 m_MOTMethod = method;
1021 OicSecOxm_t getMOTMethod() const
1026 std::string getPreConfiguredPin() const
1028 return m_preconfiguredPin;
1032 OicSecOxm_t m_MOTMethod;
1033 std::string m_preconfiguredPin;
1038 * Callback function definition for providing Enrollee status
1040 typedef function< void(shared_ptr< GetEnrolleeStatus >) > GetStatusCb;
1043 * Callback function definition for providing Enrollee configuration status
1045 typedef function< void(shared_ptr< GetConfigurationStatus >) > GetConfigurationStatusCb;
1048 * Callback function definition for providing Enrollee device property provisioning status
1050 typedef function< void(shared_ptr< DevicePropProvisioningStatus >) > DevicePropProvStatusCb;
1053 * Callback function definition for providing Enrollee cloud property provisioning status
1055 typedef function< void(shared_ptr< CloudPropProvisioningStatus >) > CloudPropProvStatusCb;
1058 * Callback function definition for providing 'Connect' request status
1060 typedef function< void(shared_ptr< ConnectRequestStatus >) > ConnectRequestStatusCb;
1064 * Callback function definition for providing Enrollee security provisioning status
1066 typedef function< void(shared_ptr<SecProvisioningStatus>) > SecurityProvStatusCb;
1069 * Callback function definition for providing Enrollee security provisioning status.
1070 * This callback is an overloaded version of SecurityProvStatusCb, which has
1071 * ESOwnershipTransferData as a return value.
1073 typedef function< ESOwnershipTransferData(shared_ptr<SecProvisioningStatus>) >
1074 SecurityProvStatusCbWithOption;
1077 * Callback definition to be invoked when the security stack expects a pin from application
1079 typedef function< void(string&) > SecurityPinCb;
1082 * Callback definition to be invoked when the stack expects a db path
1084 typedef function< void(string&) > SecProvisioningDbPathCb;
1088 #endif //WITH_ARDUINO
1090 #endif //ES_COMMON_RICH_H_