Resolve Klocwork c++ issues
[platform/upstream/iotivity.git] / resource / examples / groupserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 "OCPlatform.h"
22 #include "OCApi.h"
23
24 #include <functional>
25 #include <pthread.h>
26 #include <iostream>
27
28 using namespace std;
29 using namespace OC;
30
31 namespace PH = std::placeholders;
32
33 OCResourceHandle resourceHandle;
34 std::vector< OCResourceHandle > resourceHandleVector;
35
36 void foundResource(std::shared_ptr< OCResource > resource)
37 {
38
39     std::string resourceURI;
40     std::string hostAddress;
41
42     try
43     {
44         cout << "FOUND RESOURCE" << endl;
45
46         if (resource)
47         {
48             resourceURI = resource->uri();
49             hostAddress = resource->host();
50
51             cout << "\tResource URI : " << resourceURI << endl;
52             cout << "\tResource Host : " << hostAddress << endl;
53             cout << "\tResource Interfaces : " << resource->getResourceInterfaces().front() << endl;
54             cout << "\tResource Type : " << resource->getResourceTypes().front() << endl;
55             if (resourceURI == "/a/light" || resourceURI == "/a/fan")
56             {
57                 OCResourceHandle foundResourceHandle;
58                 OCStackResult result = OCPlatform::registerResource(foundResourceHandle, resource);
59                 cout << "\tresource registed!" << endl;
60                 if (result == OC_STACK_OK)
61                 {
62                     OCPlatform::bindResource(resourceHandle, foundResourceHandle);
63                     resourceHandleVector.push_back(foundResourceHandle);
64                 }
65                 else
66                 {
67                     cout << "\tresource Error!" << endl;
68                 }
69             }
70
71             // p_platform.bindResource(resourceHandle, foundResourceHandle);
72
73         }
74     }
75     catch (std::exception& e)
76     {
77         std::cout << "" << std::endl;
78     }
79
80 }
81
82 int main(int argc, char* argv[])
83 {
84     ostringstream requestURI;
85
86 #ifdef CA_INT
87     OCConnectivityType connectivityType = OC_WIFI;
88
89     if(argc == 2)
90     {
91         try
92         {
93             std::size_t inputValLen;
94             int optionSelected = stoi(argv[1], &inputValLen);
95
96             if(inputValLen == strlen(argv[1]))
97             {
98                 if(optionSelected == 0)
99                 {
100                     connectivityType = OC_ETHERNET;
101                 }
102                 else if(optionSelected == 1)
103                 {
104                     connectivityType = OC_WIFI;
105                 }
106                 else
107                 {
108                     std::cout << "Invalid connectivity type selected. Using default WIFI"
109                         << std::endl;
110                 }
111             }
112             else
113             {
114                 std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
115             }
116         }
117         catch(exception& e)
118         {
119             std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
120         }
121     }
122     else
123     {
124         std::cout<<"Usage: groupclient <ConnectivityType(0|1)>\n";
125         std::cout<<"ConnectivityType: Default WIFI\n";
126         std::cout<<"ConnectivityType 0: ETHERNET\n";
127         std::cout<<"ConnectivityType 1: WIFI\n";
128     }
129 #endif
130
131     PlatformConfig config
132     { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
133
134     try
135     {
136         string resourceURI = "/core/a/collection";
137         string resourceTypeName = "a.collection";
138         string resourceInterface = BATCH_INTERFACE;
139         OCPlatform::Configure(config);
140
141         // EntityHandler cb = std::bind(, PH::_1, PH::_2);
142
143         OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
144                 resourceInterface,
145                 NULL,
146                 //&entityHandler, // entityHandler
147                 OC_DISCOVERABLE);
148
149         cout << "registerResource is called." << endl;
150
151         requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
152
153 #ifdef CA_INT
154         OCPlatform::findResource("", requestURI.str(),
155                                  connectivityType, &foundResource);
156 #else
157         OCPlatform::findResource("", requestURI.str(), &foundResource);
158 #endif
159
160         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
161         OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
162
163         int selectedMenu;
164         bool isRun = true;
165         while (isRun)
166         {
167             cout << endl << "0 :: Quit 1 :: UNREGISTER RESOURCES\n" << endl;
168             std::cin >> selectedMenu;
169
170             switch(selectedMenu)
171             {
172             case 0:
173                 isRun = false;
174                 break;
175             case 1:
176                 std::cout << "Unregistering resources" << std::endl;
177                 for (unsigned int i = 0; i < resourceHandleVector.size(); ++i)
178                 {
179                     OCPlatform::unregisterResource(resourceHandleVector.at(i));
180                 }
181                 break;
182             default:
183                 cout << "Invalid option" << endl;
184             }
185
186         }
187     }
188     catch (OCException& e)
189     {
190
191     }
192
193     return 0;
194 }