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 #include "SceneCommons.h"
25 #include "oic_malloc.h"
26 #include "RCSException.h"
27 #include "cainterface.h"
28 #include "OCPlatform.h"
34 std::string SceneUtils::OICGenerateUUIDStr()
36 uint8_t uuid[UUID_SIZE] = { 0, };
37 char uuidStr[UUID_STRING_SIZE] = { 0, };
38 if (RAND_UUID_OK == OCGenerateUuid(uuid))
40 if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
42 return std::string(uuidStr);
46 throw RCSException("Failed to generate UUID");
49 void SceneUtils::getHostUriString(
50 const std::string address, std::string *host, std::string *uri)
52 unsigned int nextStartIndex = 0;
55 if (address.find(COAP_TAG) == std::string::npos)
60 for (int i = 0; i < indexOfStr; i++)
63 = address.find_first_of("/", nextStartIndex);
64 if (nextStartIndex == std::string::npos)
66 throw RCSInvalidParameterException("address is invalid");
71 *host = address.substr(0, nextStartIndex - 1);
72 *uri = address.substr(nextStartIndex - 1, std::string::npos);
75 std::string SceneUtils::getNetAddress()
77 CAEndpoint_t ** netInfo = (CAEndpoint_t **)OICMalloc(sizeof(CAEndpoint_t*)*5);
79 if(netInfo == nullptr)
81 throw RCSException("memory allocation fail");
85 CAGetNetworkInformation(netInfo, &size);
90 throw RCSException("Disabled Network");
93 for (uint32_t i = 0; i < size; ++i)
95 if (netInfo[i]->adapter == CATransportAdapter_t::CA_ADAPTER_IP)
97 std::string retAddress
98 = std::string(netInfo[i]->addr) + ":" + std::to_string(netInfo[i]->port);
106 throw RCSException("Not supported Network");
109 RCSRemoteResourceObject::Ptr SceneUtils::createRCSResourceObject(
110 const std::string &address, const OCConnectivityType ct,
111 const std::vector< std::string > &vecRT, const std::vector< std::string > &vecIF)
115 std::string hostaddress, uri;
116 SceneUtils::getHostUriString(address, &hostaddress, &uri);
118 OC::OCResource::Ptr pOCResource =
119 OC::OCPlatform::constructResourceObject(
120 hostaddress, uri, ct, false, vecRT, vecIF);
122 return RCSRemoteResourceObject::fromOCResource(pOCResource);
124 catch (const std::exception &e)
126 throw RCSException("Fail to create RCSResourceObject");