[IOT-1538] Add support for Protocol-Independent ID
[platform/upstream/iotivity.git] / resource / examples / simpleserverHQ.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 ///
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
24 ///
25
26 #include "iotivity_config.h"
27 #include <functional>
28
29 #ifdef HAVE_PTHREAD_H
30 #include <pthread.h>
31 #endif
32 #include <mutex>
33 #include <condition_variable>
34
35 #include "OCPlatform.h"
36 #include "OCApi.h"
37
38 #ifdef HAVE_WINDOWS_H
39 #include <windows.h>
40 #endif
41
42 #include "ocpayload.h"
43
44 using namespace OC;
45 using namespace std;
46 namespace PH = std::placeholders;
47
48 int gObservation = 0;
49 void * ChangeLightRepresentation (void *param);
50
51 // Set of strings for each of platform Info fields
52 std::string  platformId = "0A3E0D6F-DBF5-404E-8719-D6880042463A";
53 std::string  manufacturerName = "OCF";
54 std::string  manufacturerLink = "https://www.iotivity.org";
55 std::string  modelNumber = "myModelNumber";
56 std::string  dateOfManufacture = "2016-01-15";
57 std::string  platformVersion = "myPlatformVersion";
58 std::string  operatingSystemVersion = "myOS";
59 std::string  hardwareVersion = "myHardwareVersion";
60 std::string  firmwareVersion = "1.0";
61 std::string  supportLink = "https://www.iotivity.org";
62 std::string  systemTime = "2016-01-15T11.01";
63
64 // Set of strings for each of device info fields
65 std::string  deviceName = "IoTivity Simple Server HQ";
66 std::string  specVersion = "core.1.1.0";
67 std::vector<std::string> dataModelVersions = {"res.1.1.0"};
68 std::string  protocolIndependentID = "88b3584f-d7bc-4e56-9210-0e8b305202c3";
69
70 // OCPlatformInfo Contains all the platform info to be stored
71 OCPlatformInfo platformInfo;
72
73 // Specifies where to notify all observers or list of observers
74 // 0 - notifies all observers
75 // 1 - notifies list of observers
76 int isListOfObservers = 0;
77
78 /// This class represents a single resource named 'lightResource'. This resource has
79 /// two simple properties named 'state' and 'power'
80
81 class LightResource
82 {
83
84 public:
85     /// Access this property from a TB client
86     std::string m_name;
87     bool m_state;
88     int m_power;
89     std::string m_lightUri;
90     OCResourceHandle m_resourceHandle;
91     OCRepresentation m_lightRep;
92     ObservationIds m_interestedObservers;
93
94 public:
95     /// Constructor
96     LightResource(PlatformConfig& /*cfg*/)
97         :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
98         // Initialize representation
99         m_lightRep.setUri(m_lightUri);
100
101         m_lightRep.setValue("state", m_state);
102         m_lightRep.setValue("power", m_power);
103         m_lightRep.setValue("name", m_name);
104     }
105
106     /* Note that this does not need to be a member function: for classes you do not have
107     access to, you can accomplish this with a free function: */
108
109     /// This function internally calls registerResource API.
110     void createResource()
111     {
112         std::string resourceURI = m_lightUri; // URI of the resource
113         // resource type name. In this case, it is light
114         std::string resourceTypeName = "core.light";
115         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
116
117         // OCResourceProperty is defined ocstack.h
118         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
119
120         EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
121
122         // This will internally create and register the resource.
123         OCStackResult result = OCPlatform::registerResource(
124                                     m_resourceHandle, resourceURI, resourceTypeName,
125                                     resourceInterface, cb, resourceProperty);
126
127         if (OC_STACK_OK != result)
128         {
129             cout << "Resource creation was unsuccessful\n";
130         }
131     }
132
133     OCStackResult createResource1()
134     {
135         std::string resourceURI = "/a/light1"; // URI of the resource
136         std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
137         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
138
139         // OCResourceProperty is defined ocstack.h
140         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
141
142         EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
143
144         OCResourceHandle resHandle;
145
146         // This will internally create and register the resource.
147         OCStackResult result = OCPlatform::registerResource(
148                                     resHandle, resourceURI, resourceTypeName,
149                                     resourceInterface, cb, resourceProperty);
150
151         if (OC_STACK_OK != result)
152         {
153             cout << "Resource creation was unsuccessful\n";
154         }
155
156         return result;
157     }
158
159     OCResourceHandle getHandle()
160     {
161         return m_resourceHandle;
162     }
163
164     // Puts representation.
165     // Gets values from the representation and
166     // updates the internal state
167     void put(OCRepresentation& rep)
168     {
169         try {
170             if (rep.getValue("state", m_state))
171             {
172                 cout << "\t\t\t\t" << "state: " << m_state << endl;
173             }
174             else
175             {
176                 cout << "\t\t\t\t" << "state not found in the representation" << endl;
177             }
178
179             if (rep.getValue("power", m_power))
180             {
181                 cout << "\t\t\t\t" << "power: " << m_power << endl;
182             }
183             else
184             {
185                 cout << "\t\t\t\t" << "power not found in the representation" << endl;
186             }
187         }
188         catch (exception& e)
189         {
190             cout << e.what() << endl;
191         }
192
193     }
194
195     // Post representation.
196     // Post can create new resource or simply act like put.
197     // Gets values from the representation and
198     // updates the internal state
199     OCRepresentation post(OCRepresentation& rep)
200     {
201         static int first = 1;
202
203         std::cout << "In POST\n";
204
205         // for the first time it tries to create a resource
206         if(first)
207         {
208             std::cout << "In POST/First\n";
209
210             first = 0;
211
212             if(OC_STACK_OK == createResource1())
213             {
214                 std::cout << "Created a new resource\n";
215                 OCRepresentation rep1;
216                 rep1.setValue("createduri", std::string("/a/light1"));
217
218                 return rep1;
219             }
220         }
221
222         // from second time onwards it just puts
223         put(rep);
224         return get();
225     }
226
227
228     // gets the updated representation.
229     // Updates the representation with latest internal state before
230     // sending out.
231     OCRepresentation get()
232     {
233         m_lightRep.setValue("state", m_state);
234         m_lightRep.setValue("power", m_power);
235
236         return m_lightRep;
237     }
238
239     void addType(const std::string& type) const
240     {
241         OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
242         if (OC_STACK_OK != result)
243         {
244             cout << "Binding TypeName to Resource was unsuccessful\n";
245         }
246     }
247
248     void addInterface(const std::string& iface) const
249     {
250         OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
251         if (OC_STACK_OK != result)
252         {
253             cout << "Binding TypeName to Resource was unsuccessful\n";
254         }
255     }
256
257 private:
258
259 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
260 {
261     auto pResponse = std::make_shared<OC::OCResourceResponse>();
262     pResponse->setRequestHandle(pRequest->getRequestHandle());
263     pResponse->setResourceHandle(pRequest->getResourceHandle());
264     pResponse->setResourceRepresentation(get());
265
266     pResponse->setResponseResult(OC_EH_OK);
267
268     return OCPlatform::sendResponse(pResponse);
269 }
270
271 OCStackResult sendPostResponse(std::shared_ptr<OCResourceRequest> pRequest)
272 {
273     auto pResponse = std::make_shared<OC::OCResourceResponse>();
274     pResponse->setRequestHandle(pRequest->getRequestHandle());
275     pResponse->setResourceHandle(pRequest->getResourceHandle());
276
277     OCRepresentation rep = pRequest->getResourceRepresentation();
278     OCRepresentation rep_post = post(rep);
279
280     pResponse->setResourceRepresentation(rep_post);
281
282     pResponse->setResponseResult(OC_EH_OK);
283
284     return OCPlatform::sendResponse(pResponse);
285 }
286
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)
290 {
291     cout << "\tIn Server CPP entity handler:\n";
292     OCEntityHandlerResult ehResult = OC_EH_ERROR;
293
294     if(request)
295     {
296         // Get the request type and request flag
297         std::string requestType = request->getRequestType();
298         int requestFlag = request->getRequestHandlerFlag();
299
300         if(requestFlag & RequestHandlerFlag::RequestFlag)
301         {
302             cout << "\t\trequestFlag : Request\n";
303
304             // If the request type is GET
305             if(requestType == "GET")
306             {
307                 cout << "\t\t\trequestType : GET\n";
308                 if(OC_STACK_OK == sendResponse(request))
309                 {
310                     ehResult = OC_EH_OK;
311                 }
312             }
313             else if(requestType == "PUT")
314             {
315                 cout << "\t\t\trequestType : PUT\n";
316
317                 OCRepresentation rep = request->getResourceRepresentation();
318                 // Do related operations related to PUT request
319                 // Update the lightResource
320                 put(rep);
321                 if(OC_STACK_OK == sendResponse(request))
322                 {
323                     ehResult = OC_EH_OK;
324                 }
325             }
326             else if(requestType == "POST")
327             {
328                 cout << "\t\t\trequestType : POST\n";
329                 if(OC_STACK_OK == sendPostResponse(request))
330                 {
331                     ehResult = OC_EH_OK;
332                 }
333             }
334             else if(requestType == "DELETE")
335             {
336                 // DELETE request operations
337             }
338         }
339
340         if(requestFlag & RequestHandlerFlag::ObserverFlag)
341         {
342             ObservationInfo observationInfo = request->getObservationInfo();
343             if(ObserveAction::ObserveRegister == observationInfo.action)
344             {
345                 m_interestedObservers.push_back(observationInfo.obsId);
346             }
347             else if(ObserveAction::ObserveUnregister == observationInfo.action)
348             {
349                 m_interestedObservers.erase(std::remove(
350                                                             m_interestedObservers.begin(),
351                                                             m_interestedObservers.end(),
352                                                             observationInfo.obsId),
353                                                             m_interestedObservers.end());
354             }
355
356             pthread_t threadId;
357
358             cout << "\t\trequestFlag : Observer\n";
359             gObservation = 1;
360             static int startedThread = 0;
361
362             // Observation happens on a different thread in ChangeLightRepresentation function.
363             // If we have not created the thread already, we will create one here.
364             if(!startedThread)
365             {
366                 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this);
367                 startedThread = 1;
368             }
369             ehResult = OC_EH_OK;
370         }
371     }
372     else
373     {
374         std::cout << "Request invalid" << std::endl;
375     }
376
377     return ehResult;
378 }
379
380 };
381
382 // ChangeLightRepresentaion is an observation function,
383 // which notifies any changes to the resource to stack
384 // via notifyObservers
385 void * ChangeLightRepresentation (void *param)
386 {
387     LightResource* lightPtr = (LightResource*) param;
388
389     // This function continuously monitors for the changes
390     while (1)
391     {
392         sleep (3);
393
394         if (gObservation)
395         {
396             // If under observation if there are any changes to the light resource
397             // we call notifyObservors
398             //
399             // For demostration we are changing the power value and notifying.
400             lightPtr->m_power += 10;
401
402             cout << "\nPower updated to : " << lightPtr->m_power << endl;
403             cout << "Notifying observers with resource handle: " << lightPtr->getHandle() << endl;
404
405             OCStackResult result = OC_STACK_OK;
406
407             if(isListOfObservers)
408             {
409                 std::shared_ptr<OCResourceResponse> resourceResponse =
410                             std::make_shared<OCResourceResponse>();
411
412                 resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
413
414                 result = OCPlatform::notifyListOfObservers(
415                                                             lightPtr->getHandle(),
416                                                             lightPtr->m_interestedObservers,
417                                                             resourceResponse,
418                                                             OC::QualityOfService::HighQos);
419             }
420             else
421             {
422                 result = OCPlatform::notifyAllObservers(lightPtr->getHandle(),
423                                                             OC::QualityOfService::HighQos);
424             }
425
426             if(OC_STACK_NO_OBSERVERS == result)
427             {
428                 cout << "No More observers, stopping notifications" << endl;
429                 gObservation = 0;
430             }
431         }
432     }
433
434     return NULL;
435 }
436
437 void DeletePlatformInfo()
438 {
439     delete[] platformInfo.platformID;
440     delete[] platformInfo.manufacturerName;
441     delete[] platformInfo.manufacturerUrl;
442     delete[] platformInfo.modelNumber;
443     delete[] platformInfo.dateOfManufacture;
444     delete[] platformInfo.platformVersion;
445     delete[] platformInfo.operatingSystemVersion;
446     delete[] platformInfo.hardwareVersion;
447     delete[] platformInfo.firmwareVersion;
448     delete[] platformInfo.supportUrl;
449     delete[] platformInfo.systemTime;
450 }
451
452 void DuplicateString(char ** targetString, std::string sourceString)
453 {
454     *targetString = new char[sourceString.length() + 1];
455     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
456 }
457
458 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
459         std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
460         std::string platformVersion, std::string operatingSystemVersion,
461         std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,
462         std::string systemTime)
463 {
464     DuplicateString(&platformInfo.platformID, platformID);
465     DuplicateString(&platformInfo.manufacturerName, manufacturerName);
466     DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
467     DuplicateString(&platformInfo.modelNumber, modelNumber);
468     DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
469     DuplicateString(&platformInfo.platformVersion, platformVersion);
470     DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
471     DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
472     DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
473     DuplicateString(&platformInfo.supportUrl, supportUrl);
474     DuplicateString(&platformInfo.systemTime, systemTime);
475
476     return OC_STACK_OK;
477 }
478
479 OCStackResult SetDeviceInfo()
480 {
481     OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME,
482                                                         deviceName);
483     if (result != OC_STACK_OK)
484     {
485         cout << "Failed to set device name" << endl;
486         return result;
487     }
488
489     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
490                                           dataModelVersions);
491     if (result != OC_STACK_OK)
492     {
493         cout << "Failed to set data model versions" << endl;
494         return result;
495     }
496
497     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
498     if (result != OC_STACK_OK)
499     {
500         cout << "Failed to set spec version" << endl;
501         return result;
502     }
503
504     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
505                                           protocolIndependentID);
506     if (result != OC_STACK_OK)
507     {
508         cout << "Failed to set piid" << endl;
509         return result;
510     }
511
512     return OC_STACK_OK;
513 }
514
515 void PrintUsage()
516 {
517     std::cout << std::endl;
518     std::cout << "Usage : simpleserverHQ <ObserveType>\n";
519     std::cout << "   ObserveType : 0 - Observe All\n";
520     std::cout << "   ObserveType : 1 - Observe List of observers\n\n";
521 }
522
523
524 int main(int argc, char* argv[])
525 {
526     PrintUsage();
527
528     if (argc == 1)
529     {
530         isListOfObservers = 0;
531     }
532     else if (argc == 2)
533     {
534         int value = atoi(argv[1]);
535         if (value == 1)
536             isListOfObservers = 1;
537         else
538             isListOfObservers = 0;
539     }
540     else
541     {
542         return -1;
543     }
544
545     // Create PlatformConfig object
546     PlatformConfig cfg {
547         OC::ServiceType::InProc,
548         OC::ModeType::Server,
549         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
550         0,         // Uses randomly available port
551         OC::QualityOfService::LowQos
552     };
553
554     OCPlatform::Configure(cfg);
555     std::cout << "Starting server & setting platform info\n";
556
557     OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink,
558             modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
559             hardwareVersion, firmwareVersion, supportLink, systemTime);
560
561     result = OCPlatform::registerPlatformInfo(platformInfo);
562
563     if (result != OC_STACK_OK)
564     {
565         std::cout << "Platform Registration failed\n";
566         return -1;
567     }
568
569     result = SetDeviceInfo();
570
571     if (result != OC_STACK_OK)
572     {
573         std::cout << "Device Registration failed\n";
574         return -1;
575     }
576
577     try
578     {
579         // Create the instance of the resource class
580         // (in this case instance of class 'LightResource').
581         LightResource myLight(cfg);
582
583         // Invoke createResource function of class light.
584         myLight.createResource();
585
586         myLight.addType(std::string("core.brightlight"));
587         myLight.addInterface(std::string(LINK_INTERFACE));
588
589         DeletePlatformInfo();
590
591         // A condition variable will free the mutex it is given, then do a non-
592         // intensive block until 'notify' is called on it.  In this case, since we
593         // don't ever call cv.notify, this should be a non-processor intensive version
594         // of while(true);
595         std::mutex blocker;
596         std::condition_variable cv;
597         std::unique_lock<std::mutex> lock(blocker);
598         cv.wait(lock, []{return false;});
599     }
600     catch(OCException& e)
601     {
602         oclog() << "Exception in main: "<< e.what();
603     }
604
605     // No explicit call to stop the platform.
606     // When OCPlatform destructor is invoked, internally we do platform cleanup
607
608     return 0;
609 }
610