[IOT-1065][IOT-1069] RC - Tizen: sample application fix
[platform/upstream/iotivity.git] / service / scene-manager / src / SceneUtils.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 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 #include "SceneCommons.h"
22
23 #include <string>
24 #include "ocrandom.h"
25 #include "oic_malloc.h"
26 #include "RCSException.h"
27 #include "cainterface.h"
28 #include "OCPlatform.h"
29
30 namespace OIC
31 {
32     namespace Service
33     {
34         std::string SceneUtils::OICGenerateUUIDStr()
35         {
36             uint8_t uuid[UUID_SIZE] = { 0, };
37             char uuidStr[UUID_STRING_SIZE] = { 0, };
38             if (RAND_UUID_OK == OCGenerateUuid(uuid))
39             {
40                 if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
41                 {
42                     return std::string(uuidStr);
43                 }
44             }
45
46             throw RCSException("Failed to generate UUID");
47         }
48
49         void SceneUtils::getHostUriString(
50                 const std::string address, std::string *host, std::string *uri)
51         {
52             unsigned int nextStartIndex = 0;
53             int indexOfStr = 3;
54
55             if (address.find(COAP_TAG) == std::string::npos)
56             {
57                 indexOfStr = 1;
58             }
59
60             for (int i = 0; i < indexOfStr; i++)
61             {
62                 nextStartIndex
63                     = address.find_first_of("/", nextStartIndex);
64                 if (nextStartIndex == std::string::npos)
65                 {
66                     throw RCSInvalidParameterException("address is invalid");
67                 }
68                 nextStartIndex += 1;
69             }
70
71             *host = address.substr(0, nextStartIndex - 1);
72             *uri = address.substr(nextStartIndex - 1, std::string::npos);
73         }
74
75         std::string SceneUtils::getNetAddress()
76         {
77             CAEndpoint_t ** netInfo = (CAEndpoint_t **)OICMalloc(sizeof(CAEndpoint_t*)*5);
78
79             if(netInfo == nullptr)
80             {
81                 throw RCSException("memory allocation fail");
82             }
83
84             uint32_t size = 0;
85             CAGetNetworkInformation(netInfo, &size);
86
87             if (size == 0)
88             {
89                 OICFree(netInfo);
90                 throw RCSException("Disabled Network");
91             }
92
93             for (uint32_t i = 0; i < size; ++i)
94             {
95                 if (netInfo[i]->adapter == CATransportAdapter_t::CA_ADAPTER_IP)
96                 {
97                     std::string retAddress
98                         = std::string(netInfo[i]->addr) + ":" + std::to_string(netInfo[i]->port);
99
100                     OICFree(netInfo);
101                     return retAddress;
102                 }
103             }
104
105             OICFree(netInfo);
106             throw RCSException("Not supported Network");
107         }
108
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)
112         {
113             try
114             {
115                 std::string hostaddress, uri;
116                 SceneUtils::getHostUriString(address, &hostaddress, &uri);
117
118                 OC::OCResource::Ptr pOCResource =
119                     OC::OCPlatform::constructResourceObject(
120                         hostaddress, uri, ct, false, vecRT, vecIF);
121
122                 return RCSRemoteResourceObject::fromOCResource(pOCResource);
123             }
124             catch (const std::exception &e)
125             {
126                 throw RCSException("Fail to create RCSResourceObject");
127             }
128         }
129
130     }
131 }