replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / examples / simpleserverHQ.cpp
index f5efacc..b50bc5b 100644 (file)
 /// (properties and methods) and host this resource on the server.
 ///
 
+#include "iotivity_config.h"
 #include <functional>
 
+#ifdef HAVE_PTHREAD_H
 #include <pthread.h>
+#endif
 #include <mutex>
 #include <condition_variable>
 
 #include "OCPlatform.h"
 #include "OCApi.h"
 
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
+
+#include "ocpayload.h"
+
 using namespace OC;
 using namespace std;
 namespace PH = std::placeholders;
@@ -39,6 +48,30 @@ namespace PH = std::placeholders;
 int gObservation = 0;
 void * ChangeLightRepresentation (void *param);
 
+// Set of strings for each of platform Info fields
+std::string  platformId = "0A3E0D6F-DBF5-404E-8719-D6880042463A";
+std::string  manufacturerName = "OCF";
+std::string  manufacturerLink = "https://www.iotivity.org";
+std::string  modelNumber = "myModelNumber";
+std::string  dateOfManufacture = "2016-01-15";
+std::string  platformVersion = "myPlatformVersion";
+std::string  operatingSystemVersion = "myOS";
+std::string  hardwareVersion = "myHardwareVersion";
+std::string  firmwareVersion = "1.0";
+std::string  supportLink = "https://www.iotivity.org";
+std::string  systemTime = "2016-01-15T11.01";
+
+// Set of strings for each of device info fields
+std::string  deviceName = "IoTivity Simple Server HQ";
+std::string  specVersion = "core.1.1.0";
+std::string  dataModelVersions = "res.1.1.0";
+
+// OCPlatformInfo Contains all the platform info to be stored
+OCPlatformInfo platformInfo;
+
+// OCDeviceInfo Contains all the device info to be stored
+OCDeviceInfo deviceInfo;
+
 // Specifies where to notify all observers or list of observers
 // 0 - notifies all observers
 // 1 - notifies list of observers
@@ -62,7 +95,7 @@ public:
 
 public:
     /// Constructor
-    LightResource(PlatformConfig& cfg)
+    LightResource(PlatformConfig& /*cfg*/)
         :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
         // Initialize representation
         m_lightRep.setUri(m_lightUri);
@@ -214,9 +247,9 @@ public:
         }
     }
 
-    void addInterface(const std::string& interface) const
+    void addInterface(const std::string& iface) const
     {
-        OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
+        OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
         if (OC_STACK_OK != result)
         {
             cout << "Binding TypeName to Resource was unsuccessful\n";
@@ -266,11 +299,6 @@ OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
         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";
@@ -363,7 +391,7 @@ void * ChangeLightRepresentation (void *param)
     // This function continuously monitors for the changes
     while (1)
     {
-        sleep (5);
+        sleep (3);
 
         if (gObservation)
         {
@@ -380,7 +408,8 @@ void * ChangeLightRepresentation (void *param)
 
             if(isListOfObservers)
             {
-                std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
+                std::shared_ptr<OCResourceResponse> resourceResponse =
+                            std::make_shared<OCResourceResponse>();
 
                 resourceResponse->setErrorCode(200);
                 resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
@@ -408,6 +437,72 @@ void * ChangeLightRepresentation (void *param)
     return NULL;
 }
 
+void DeletePlatformInfo()
+{
+    delete[] platformInfo.platformID;
+    delete[] platformInfo.manufacturerName;
+    delete[] platformInfo.manufacturerUrl;
+    delete[] platformInfo.modelNumber;
+    delete[] platformInfo.dateOfManufacture;
+    delete[] platformInfo.platformVersion;
+    delete[] platformInfo.operatingSystemVersion;
+    delete[] platformInfo.hardwareVersion;
+    delete[] platformInfo.firmwareVersion;
+    delete[] platformInfo.supportUrl;
+    delete[] platformInfo.systemTime;
+}
+
+void DeleteDeviceInfo()
+{
+    delete[] deviceInfo.deviceName;
+    delete[] deviceInfo.specVersion;
+    OCFreeOCStringLL(deviceInfo.dataModelVersions);
+}
+
+void DuplicateString(char ** targetString, std::string sourceString)
+{
+    *targetString = new char[sourceString.length() + 1];
+    strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
+}
+
+OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
+        std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
+        std::string platformVersion, std::string operatingSystemVersion,
+        std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,
+        std::string systemTime)
+{
+    DuplicateString(&platformInfo.platformID, platformID);
+    DuplicateString(&platformInfo.manufacturerName, manufacturerName);
+    DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
+    DuplicateString(&platformInfo.modelNumber, modelNumber);
+    DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
+    DuplicateString(&platformInfo.platformVersion, platformVersion);
+    DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
+    DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
+    DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
+    DuplicateString(&platformInfo.supportUrl, supportUrl);
+    DuplicateString(&platformInfo.systemTime, systemTime);
+
+    return OC_STACK_OK;
+}
+
+OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions)
+{
+    DuplicateString(&deviceInfo.deviceName, deviceName);
+
+    if (!specVersion.empty())
+    {
+        DuplicateString(&deviceInfo.specVersion, specVersion);
+    }
+
+    if (!dataModelVersions.empty())
+    {
+        OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str());
+    }
+
+    return OC_STACK_OK;
+}
+
 void PrintUsage()
 {
     std::cout << std::endl;
@@ -448,6 +543,30 @@ int main(int argc, char* argv[])
     };
 
     OCPlatform::Configure(cfg);
+    std::cout << "Starting server & setting platform info\n";
+
+    OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink,
+            modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
+            hardwareVersion, firmwareVersion, supportLink, systemTime);
+
+    result = OCPlatform::registerPlatformInfo(platformInfo);
+
+    if (result != OC_STACK_OK)
+    {
+        std::cout << "Platform Registration failed\n";
+        return -1;
+    }
+
+    result = SetDeviceInfo(deviceName, specVersion, dataModelVersions);
+    OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+
+    result = OCPlatform::registerDeviceInfo(deviceInfo);
+
+    if (result != OC_STACK_OK)
+    {
+        std::cout << "Device Registration failed\n";
+        return -1;
+    }
 
     try
     {
@@ -459,7 +578,10 @@ int main(int argc, char* argv[])
         myLight.createResource();
 
         myLight.addType(std::string("core.brightlight"));
-        myLight.addInterface(std::string("oc.mi.ll"));
+        myLight.addInterface(std::string(LINK_INTERFACE));
+
+        DeletePlatformInfo();
+        DeleteDeviceInfo();
 
         // 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