1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
25 #include "iotivity_config.h"
35 #include <condition_variable>
37 #include "OCPlatform.h"
43 #include "ocpayload.h"
47 namespace PH = std::placeholders;
49 static const char* SVR_DB_FILE_NAME = "./oic_svr_db_server.dat";
51 void * ChangeLightRepresentation (void *param);
52 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest);
54 // Set of strings for each of platform Info fields
55 std::string platformId = "0A3E0D6F-DBF5-404E-8719-D6880042463A";
56 std::string manufacturerName = "OCF";
57 std::string manufacturerLink = "https://www.iotivity.org";
58 std::string modelNumber = "myModelNumber";
59 std::string dateOfManufacture = "2016-01-15";
60 std::string platformVersion = "myPlatformVersion";
61 std::string operatingSystemVersion = "myOS";
62 std::string hardwareVersion = "myHardwareVersion";
63 std::string firmwareVersion = "1.0";
64 std::string supportLink = "https://www.iotivity.org";
65 std::string systemTime = "2016-01-15T11.01";
67 // Set of strings for each of device info fields
68 std::string deviceName = "IoTivity Simple Server";
69 std::string deviceType = "oic.wk.tv";
70 std::string specVersion = "core.1.1.0";
71 std::vector<std::string> dataModelVersions = {"res.1.1.0", "sh.1.1.0"};
72 std::string protocolIndependentID = "fa008167-3bbf-4c9d-8604-c9bcb96cb712";
74 // OCPlatformInfo Contains all the platform info to be stored
75 OCPlatformInfo platformInfo;
77 // Specifies where to notify all observers or list of observers
78 // false: notifies all observers
79 // true: notifies list of observers
80 bool isListOfObservers = false;
82 // Specifies secure or non-secure
83 // false: non-secure resource
84 // true: secure resource
85 bool isSecure = false;
87 /// Specifies whether Entity handler is going to do slow response or not
88 bool isSlowResponse = false;
90 // Forward declaring the entityHandler
92 /// This class represents a single resource named 'lightResource'. This resource has
93 /// two simple properties named 'state' and 'power'
99 /// Access this property from a TB client
103 std::string m_lightUri;
104 OCResourceHandle m_resourceHandle;
105 OCRepresentation m_lightRep;
106 ObservationIds m_interestedObservers;
111 :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light"),
112 m_resourceHandle(nullptr) {
113 // Initialize representation
114 m_lightRep.setUri(m_lightUri);
116 m_lightRep.setValue("state", m_state);
117 m_lightRep.setValue("power", m_power);
118 m_lightRep.setValue("name", m_name);
121 /* Note that this does not need to be a member function: for classes you do not have
122 access to, you can accomplish this with a free function: */
124 /// This function internally calls registerResource API.
125 void createResource()
127 //URI of the resource
128 std::string resourceURI = m_lightUri;
129 //resource type name. In this case, it is light
130 std::string resourceTypeName = "core.light";
131 // resource interface.
132 std::string resourceInterface = DEFAULT_INTERFACE;
134 // OCResourceProperty is defined ocstack.h
135 uint8_t resourceProperty;
138 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
142 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
144 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
146 // This will internally create and register the resource.
147 OCStackResult result = OCPlatform::registerResource(
148 m_resourceHandle, resourceURI, resourceTypeName,
149 resourceInterface, cb, resourceProperty);
151 if (OC_STACK_OK != result)
153 cout << "Resource creation was unsuccessful\n";
157 OCStackResult createResource1()
159 // URI of the resource
160 std::string resourceURI = "/a/light1";
161 // resource type name. In this case, it is light
162 std::string resourceTypeName = "core.light";
163 // resource interface.
164 std::string resourceInterface = DEFAULT_INTERFACE;
166 // OCResourceProperty is defined ocstack.h
167 uint8_t resourceProperty;
170 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
174 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
176 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
178 OCResourceHandle resHandle;
180 // This will internally create and register the resource.
181 OCStackResult result = OCPlatform::registerResource(
182 resHandle, resourceURI, resourceTypeName,
183 resourceInterface, cb, resourceProperty);
185 if (OC_STACK_OK != result)
187 cout << "Resource creation was unsuccessful\n";
193 OCResourceHandle getHandle()
195 return m_resourceHandle;
198 // Puts representation.
199 // Gets values from the representation and
200 // updates the internal state
201 void put(OCRepresentation& rep)
204 if (rep.getValue("state", m_state))
206 cout << "\t\t\t\t" << "state: " << m_state << endl;
210 cout << "\t\t\t\t" << "state not found in the representation" << endl;
213 if (rep.getValue("power", m_power))
215 cout << "\t\t\t\t" << "power: " << m_power << endl;
219 cout << "\t\t\t\t" << "power not found in the representation" << endl;
224 cout << e.what() << endl;
229 // Post representation.
230 // Post can create new resource or simply act like put.
231 // Gets values from the representation and
232 // updates the internal state
233 OCRepresentation post(OCRepresentation& rep)
235 static int first = 1;
237 // for the first time it tries to create a resource
242 if(OC_STACK_OK == createResource1())
244 OCRepresentation rep1;
245 rep1.setValue("createduri", std::string("/a/light1"));
251 // from second time onwards it just puts
257 // gets the updated representation.
258 // Updates the representation with latest internal state before
260 OCRepresentation get()
262 m_lightRep.setValue("state", m_state);
263 m_lightRep.setValue("power", m_power);
268 void addType(const std::string& type) const
270 OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
271 if (OC_STACK_OK != result)
273 cout << "Binding TypeName to Resource was unsuccessful\n";
277 void addInterface(const std::string& iface) const
279 OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
280 if (OC_STACK_OK != result)
282 cout << "Binding TypeName to Resource was unsuccessful\n";
287 // This is just a sample implementation of entity handler.
288 // Entity handler can be implemented in several ways by the manufacturer
289 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
291 cout << "\tIn Server CPP entity handler:\n";
292 OCEntityHandlerResult ehResult = OC_EH_ERROR;
295 // Get the request type and request flag
296 std::string requestType = request->getRequestType();
297 int requestFlag = request->getRequestHandlerFlag();
299 if(requestFlag & RequestHandlerFlag::RequestFlag)
301 cout << "\t\trequestFlag : Request\n";
302 auto pResponse = std::make_shared<OC::OCResourceResponse>();
303 pResponse->setRequestHandle(request->getRequestHandle());
304 pResponse->setResourceHandle(request->getResourceHandle());
306 // Check for query params (if any)
307 QueryParamsMap queries = request->getQueryParameters();
309 if (!queries.empty())
311 std::cout << "\nQuery processing upto entityHandler" << std::endl;
313 for (auto it : queries)
315 std::cout << "Query key: " << it.first << " value : " << it.second
319 // If the request type is GET
320 if(requestType == "GET")
322 cout << "\t\t\trequestType : GET\n";
323 if(isSlowResponse) // Slow response case
325 static int startedThread = 0;
328 std::thread t(handleSlowResponse, (void *)this, request);
332 ehResult = OC_EH_SLOW;
334 else // normal response case.
337 pResponse->setResponseResult(OC_EH_OK);
338 pResponse->setResourceRepresentation(get());
339 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
345 else if(requestType == "PUT")
347 cout << "\t\t\trequestType : PUT\n";
348 OCRepresentation rep = request->getResourceRepresentation();
350 // Do related operations related to PUT request
351 // Update the lightResource
354 pResponse->setResponseResult(OC_EH_OK);
355 pResponse->setResourceRepresentation(get());
356 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
361 else if(requestType == "POST")
363 cout << "\t\t\trequestType : POST\n";
365 OCRepresentation rep = request->getResourceRepresentation();
367 // Do related operations related to POST request
368 OCRepresentation rep_post = post(rep);
369 pResponse->setResourceRepresentation(rep_post);
371 if(rep_post.hasAttribute("createduri"))
373 pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
374 pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
378 pResponse->setResponseResult(OC_EH_OK);
381 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
386 else if(requestType == "DELETE")
388 cout << "Delete request received" << endl;
392 if(requestFlag & RequestHandlerFlag::ObserverFlag)
394 ObservationInfo observationInfo = request->getObservationInfo();
395 if(ObserveAction::ObserveRegister == observationInfo.action)
397 m_interestedObservers.push_back(observationInfo.obsId);
399 else if(ObserveAction::ObserveUnregister == observationInfo.action)
401 m_interestedObservers.erase(std::remove(
402 m_interestedObservers.begin(),
403 m_interestedObservers.end(),
404 observationInfo.obsId),
405 m_interestedObservers.end());
410 HANDLE threadHandle = INVALID_HANDLE_VALUE;
415 cout << "\t\trequestFlag : Observer\n";
417 static int startedThread = 0;
419 // Observation happens on a different thread in ChangeLightRepresentation function.
420 // If we have not created the thread already, we will create one here.
424 threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ChangeLightRepresentation, (void*)this, 0, &threadId);
426 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this);
435 std::cout << "Request invalid" << std::endl;
443 // ChangeLightRepresentaion is an observation function,
444 // which notifies any changes to the resource to stack
445 // via notifyObservers
446 void * ChangeLightRepresentation (void *param)
448 LightResource* lightPtr = (LightResource*) param;
450 // This function continuously monitors for the changes
457 // If under observation if there are any changes to the light resource
458 // we call notifyObservors
460 // For demostration we are changing the power value and notifying.
461 lightPtr->m_power += 10;
463 cout << "\nPower updated to : " << lightPtr->m_power << endl;
464 cout << "Notifying observers with resource handle: " << lightPtr->getHandle() << endl;
466 OCStackResult result = OC_STACK_OK;
468 if(isListOfObservers)
470 std::shared_ptr<OCResourceResponse> resourceResponse =
471 {std::make_shared<OCResourceResponse>()};
473 resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
475 result = OCPlatform::notifyListOfObservers( lightPtr->getHandle(),
476 lightPtr->m_interestedObservers,
481 result = OCPlatform::notifyAllObservers(lightPtr->getHandle());
484 if(OC_STACK_NO_OBSERVERS == result)
486 cout << "No More observers, stopping notifications" << endl;
495 void DeletePlatformInfo()
497 delete[] platformInfo.platformID;
498 delete[] platformInfo.manufacturerName;
499 delete[] platformInfo.manufacturerUrl;
500 delete[] platformInfo.modelNumber;
501 delete[] platformInfo.dateOfManufacture;
502 delete[] platformInfo.platformVersion;
503 delete[] platformInfo.operatingSystemVersion;
504 delete[] platformInfo.hardwareVersion;
505 delete[] platformInfo.firmwareVersion;
506 delete[] platformInfo.supportUrl;
507 delete[] platformInfo.systemTime;
510 void DuplicateString(char ** targetString, std::string sourceString)
512 *targetString = new char[sourceString.length() + 1];
513 strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
516 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
517 std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
518 std::string platformVersion, std::string operatingSystemVersion,
519 std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,
520 std::string systemTime)
522 DuplicateString(&platformInfo.platformID, platformID);
523 DuplicateString(&platformInfo.manufacturerName, manufacturerName);
524 DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
525 DuplicateString(&platformInfo.modelNumber, modelNumber);
526 DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
527 DuplicateString(&platformInfo.platformVersion, platformVersion);
528 DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
529 DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
530 DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
531 DuplicateString(&platformInfo.supportUrl, supportUrl);
532 DuplicateString(&platformInfo.systemTime, systemTime);
537 OCStackResult SetDeviceInfo()
539 OCStackResult result = OC_STACK_ERROR;
541 OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
544 cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << endl;
548 result = OCBindResourceTypeToResource(handle, deviceType.c_str());
549 if (result != OC_STACK_OK)
551 cout << "Failed to add device type" << endl;
555 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName);
556 if (result != OC_STACK_OK)
558 cout << "Failed to set device name" << endl;
562 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
564 if (result != OC_STACK_OK)
566 cout << "Failed to set data model versions" << endl;
570 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
571 if (result != OC_STACK_OK)
573 cout << "Failed to set spec version" << endl;
577 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
578 protocolIndependentID);
579 if (result != OC_STACK_OK)
581 cout << "Failed to set piid" << endl;
588 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest)
590 // This function handles slow response case
591 LightResource* lightPtr = (LightResource*) param;
592 // Induce a case for slow response by using sleep
593 std::cout << "SLOW response" << std::endl;
596 auto pResponse = std::make_shared<OC::OCResourceResponse>();
597 pResponse->setRequestHandle(pRequest->getRequestHandle());
598 pResponse->setResourceHandle(pRequest->getResourceHandle());
599 pResponse->setResourceRepresentation(lightPtr->get());
601 pResponse->setResponseResult(OC_EH_OK);
603 // Set the slow response flag back to false
604 isSlowResponse = false;
605 OCPlatform::sendResponse(pResponse);
611 std::cout << std::endl;
612 std::cout << "Usage : simpleserver <value>\n";
613 std::cout << " Default - Non-secure resource and notify all observers\n";
614 std::cout << " 1 - Non-secure resource and notify list of observers\n\n";
615 std::cout << " 2 - Secure resource and notify all observers\n";
616 std::cout << " 3 - Secure resource and notify list of observers\n\n";
617 std::cout << " 4 - Non-secure resource, GET slow response, notify all observers\n";
620 static FILE* client_open(const char* /*path*/, const char *mode)
622 return fopen(SVR_DB_FILE_NAME, mode);
625 int main(int argc, char* argv[])
628 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
632 isListOfObservers = false;
637 int value = atoi(argv[1]);
641 isListOfObservers = true;
645 isListOfObservers = false;
649 isListOfObservers = true;
653 isSlowResponse = true;
664 // Create PlatformConfig object
666 OC::ServiceType::InProc,
667 OC::ModeType::Server,
668 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
669 0, // Uses randomly available port
670 OC::QualityOfService::LowQos,
674 OCPlatform::Configure(cfg);
675 std::cout << "Starting server & setting platform info\n";
677 OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink,
678 modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
679 hardwareVersion, firmwareVersion, supportLink, systemTime);
681 result = OCPlatform::registerPlatformInfo(platformInfo);
683 if (result != OC_STACK_OK)
685 std::cout << "Platform Registration failed\n";
689 result = SetDeviceInfo();
691 if (result != OC_STACK_OK)
693 std::cout << "Device Registration failed\n";
699 // Create the instance of the resource class
700 // (in this case instance of class 'LightResource').
701 LightResource myLight;
703 // Invoke createResource function of class light.
704 myLight.createResource();
705 std::cout << "Created resource." << std::endl;
707 myLight.addType(std::string("core.brightlight"));
708 myLight.addInterface(std::string(LINK_INTERFACE));
709 std::cout << "Added Interface and Type" << std::endl;
711 DeletePlatformInfo();
713 // A condition variable will free the mutex it is given, then do a non-
714 // intensive block until 'notify' is called on it. In this case, since we
715 // don't ever call cv.notify, this should be a non-processor intensive version
718 std::condition_variable cv;
719 std::unique_lock<std::mutex> lock(blocker);
720 std::cout <<"Waiting" << std::endl;
721 cv.wait(lock, []{return false;});
723 catch(OCException &e)
725 std::cout << "OCException in main : " << e.what() << endl;
728 // No explicit call to stop the platform.
729 // When OCPlatform::destructor is invoked, internally we do platform cleanup