Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / src / OCDirectPairing.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 "OCDirectPairing.h"
22 #include <iomanip>
23
24 namespace OC
25 {
26     static const char COAP[] = "coap://";
27     static const char COAPS[] = "coaps://";
28     static const int UUID_LENGTH = (128/8); //UUID length
29
30     OCDirectPairing::OCDirectPairing(OCDPDev_t *ptr):m_devPtr(ptr)
31     {
32     }
33
34     std::string OCDirectPairing::getHost()
35     {
36         bool ipv6 = false;
37         std::ostringstream host("");
38         if (m_devPtr->connType & CT_IP_USE_V6)
39         {
40             ipv6 = true;
41         }
42
43         host << COAPS << (ipv6?"[":"") << m_devPtr->endpoint.addr;
44         host << (ipv6?"]:":":") << m_devPtr->securePort;
45
46         return host.str();
47     }
48
49     std::string OCDirectPairing::getDeviceID()
50     {
51         std::ostringstream deviceId("");
52
53         for (int i = 0; i < UUID_LENGTH; i++)
54         {
55             if (i == 4 || i == 6 || i == 8 || i == 10)
56             {
57                 deviceId << '-';
58             }
59             deviceId << std::hex << std::setfill('0') << std::setw(2) << static_cast<unsigned>(m_devPtr->deviceID.id[i]);
60         }
61
62         return deviceId.str();
63     }
64
65     std::vector<OCPrm_t> OCDirectPairing::getPairingMethods()
66     {
67         std::vector<OCPrm_t> prms;
68
69         for (size_t i = 0; i < m_devPtr->prmLen; i++)
70         {
71             prms.push_back(m_devPtr->prm[i]);
72         }
73         return prms;
74     }
75
76     OCConnectivityType OCDirectPairing::getConnType()
77     {
78         return m_devPtr->connType;
79     }
80
81     OCDPDev_t* OCDirectPairing::getDev()
82     {
83         return m_devPtr;
84     }
85 }