1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
//******************************************************************
//
// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
// Copyright 2014 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
///
/// This sample provides steps to define an interface for a resource
/// (properties and methods) and host this resource on the server.
///
#include <functional>
#include <pthread.h>
#include <mutex>
#include <condition_variable>
#include "OCPlatform.h"
#include "OCApi.h"
using namespace OC;
using namespace std;
namespace PH = std::placeholders;
int gObservation = 0;
void * ChangeLightRepresentation(void *param);
void * handleSlowResponse(void *param, std::shared_ptr< OCResourceRequest > pRequest);
// Specifies secure or non-secure
// false: non-secure resource
// true: secure resource
bool isSecure = false;
/// Specifies whether Entity handler is going to do slow response or not
bool isSlowResponse = false;
// Forward declaring the entityHandler
/// This class represents a single resource named 'lightResource'. This resource has
/// two simple properties named 'state' and 'power'
class LightResource
{
public:
/// Access this property from a TB client
std::string m_power;
std::string m_lightUri;
OCResourceHandle m_resourceHandle;
OCRepresentation m_lightRep;
public:
/// Constructor
LightResource() :
m_power(""), m_lightUri("/a/light"), m_resourceHandle(0)
{
// Initialize representation
m_lightRep.setUri(m_lightUri);
m_lightRep.setValue("power", m_power);
}
/* Note that this does not need to be a member function: for classes you do not have
access to, you can accomplish this with a free function: */
/// This function internally calls registerResource API.
void createResource()
{
std::string resourceURI = m_lightUri; //URI of the resource
std::string resourceTypeName = "core.light"; //resource type name. In this case, it is light
std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
EntityHandler cb = std::bind(&LightResource::entityHandler, this, PH::_1);
// This will internally create and register the resource.
OCStackResult result = OCPlatform::registerResource(m_resourceHandle, resourceURI,
resourceTypeName, resourceInterface, cb, OC_DISCOVERABLE | OC_OBSERVABLE);
if (OC_STACK_OK != result)
{
cout << "Resource creation was unsuccessful\n";
}
else
{
cout << "Resource URI : " << resourceURI << endl;
cout << "\tResource Type Name : " << resourceTypeName << endl;
cout << "\tResource Interface : " << DEFAULT_INTERFACE << endl;
cout << "\tResource creation is successful with resource handle : " << m_resourceHandle
<< endl;
}
}
OCResourceHandle getHandle()
{
return m_resourceHandle;
}
// Puts representation.
// Gets values from the representation and
// updates the internal state
void put(OCRepresentation& rep)
{
try
{
if (rep.getValue("power", m_power))
{
cout << "\t\t\t\t" << "power: " << m_power << endl;
}
else
{
cout << "\t\t\t\t" << "power not found in the representation" << endl;
}
}
catch (exception& e)
{
cout << e.what() << endl;
}
}
// Post representation.
// Post can create new resource or simply act like put.
// Gets values from the representation and
// updates the internal state
OCRepresentation post(OCRepresentation& rep)
{
put(rep);
return get();
}
// gets the updated representation.
// Updates the representation with latest internal state before
// sending out.
OCRepresentation get()
{
m_lightRep.setValue("power", m_power);
return m_lightRep;
}
void addType(const std::string& type) const
{
OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
if (OC_STACK_OK != result)
{
cout << "Binding TypeName to Resource was unsuccessful\n";
}
}
void addInterface(const std::string& interface) const
{
OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
if (OC_STACK_OK != result)
{
cout << "Binding TypeName to Resource was unsuccessful\n";
}
}
private:
// This is just a sample implementation of entity handler.
// Entity handler can be implemented in several ways by the manufacturer
OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
{
cout << "\tIn Server CPP entity handler:\n";
OCEntityHandlerResult ehResult = OC_EH_ERROR;
if (request)
{
// Get the request type and request flag
std::string requestType = request->getRequestType();
int requestFlag = request->getRequestHandlerFlag();
if (requestFlag & RequestHandlerFlag::InitFlag)
{
cout << "\t\trequestFlag : Init\n";
// entity handler to perform resource initialization operations
}
if (requestFlag & RequestHandlerFlag::RequestFlag)
{
cout << "\t\trequestFlag : Request\n";
auto pResponse = std::make_shared< OC::OCResourceResponse >();
pResponse->setRequestHandle(request->getRequestHandle());
pResponse->setResourceHandle(request->getResourceHandle());
// If the request type is GET
if (requestType == "GET")
{
cout << "\t\t\trequestType : GET\n";
if (isSlowResponse) // Slow response case
{
static int startedThread = 0;
if (!startedThread)
{
std::thread t(handleSlowResponse, (void *) this, request);
startedThread = 1;
t.detach();
}
ehResult = OC_EH_SLOW;
}
else // normal response case.
{
pResponse->setErrorCode(200);
pResponse->setResponseResult(OC_EH_OK);
pResponse->setResourceRepresentation(get());
if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
{
ehResult = OC_EH_OK;
}
}
}
else if (requestType == "PUT")
{
cout << "\t\t\trequestType : PUT\n";
OCRepresentation rep = request->getResourceRepresentation();
// Do related operations related to PUT request
// Update the lightResource
put(rep);
pResponse->setErrorCode(200);
pResponse->setResponseResult(OC_EH_OK);
pResponse->setResourceRepresentation(get());
if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
{
ehResult = OC_EH_OK;
}
}
else if (requestType == "POST")
{
cout << "\t\t\trequestType : POST\n";
OCRepresentation rep = request->getResourceRepresentation();
// Do related operations related to POST request
OCRepresentation rep_post = post(rep);
pResponse->setResourceRepresentation(rep_post);
pResponse->setErrorCode(200);
if (rep_post.hasAttribute("createduri"))
{
pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
pResponse->setNewResourceUri(
rep_post.getValue< std::string >("createduri"));
}
if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
{
ehResult = OC_EH_OK;
}
}
else if (requestType == "DELETE")
{
// DELETE request operations
}
}
}
else
{
std::cout << "Request invalid" << std::endl;
}
return ehResult;
}
};
void * handleSlowResponse(void *param, std::shared_ptr< OCResourceRequest > pRequest)
{
// This function handles slow response case
LightResource* lightPtr = (LightResource*) param;
// Induce a case for slow response by using sleep
std::cout << "SLOW response" << std::endl;
sleep(10);
auto pResponse = std::make_shared< OC::OCResourceResponse >();
pResponse->setRequestHandle(pRequest->getRequestHandle());
pResponse->setResourceHandle(pRequest->getResourceHandle());
pResponse->setResourceRepresentation(lightPtr->get());
pResponse->setErrorCode(200);
pResponse->setResponseResult(OC_EH_OK);
// Set the slow response flag back to false
isSlowResponse = false;
OCPlatform::sendResponse(pResponse);
return NULL;
}
int main(int argc, char* argv[])
{
// Create PlatformConfig object
PlatformConfig cfg
{ OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0",
// By setting to "0.0.0.0", it binds to all available interfaces
0,// Uses randomly available port
OC::QualityOfService::LowQos };
OCPlatform::Configure(cfg);
try
{
// Create the instance of the resource class
// (in this case instance of class 'LightResource').
LightResource myLight;
// Invoke createResource function of class light.
myLight.createResource();
// A condition variable will free the mutex it is given, then do a non-
// intensive block until 'notify' is called on it. In this case, since we
// don't ever call cv.notify, this should be a non-processor intensive version
// of while(true);
std::mutex blocker;
std::condition_variable cv;
std::unique_lock < std::mutex > lock(blocker);
cv.wait(lock);
}
catch (OCException e)
{
//log(e.what());
}
// No explicit call to stop the platform.
// When OCPlatform::destructor is invoked, internally we do platform cleanup
return 0;
}
|