Adding fix for Sonarqube major issues for IoTivity 1.2-rel
[platform/upstream/iotivity.git] / resource / examples / presenceserver.cpp
index 63d63da..c8c7f9c 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 <array>
 #include <mutex>
 #include <condition_variable>
 
 #include "OCPlatform.h"
 #include "OCApi.h"
+#include "ocpayload.h"
+
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
 
 using namespace OC;
 using namespace std;
 
+#define numPresenceResources (2)
+
+// 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 Presence Server";
+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;
+
 // Forward declaring the entityHandler
 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
 
@@ -138,9 +173,9 @@ public:
         }
     }
 
-    void addInterface(const std::string& interface) const
+    void addInterface(const std::string& iface) const
     {
-        OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
+        OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
         if (OC_STACK_OK != result)
         {
             cout << "Binding TypeName to Resource was unsuccessful\n";
@@ -149,15 +184,110 @@ public:
 
 };
 
+void createPresenceResources()
+{
+    std::array<std::string, numPresenceResources> resourceURI { {
+        "/a/fan",
+        "/a/led" } };
+    std::array<std::string, numPresenceResources> resourceTypeName { {
+        "core.fan",
+        "core.led" } };
+
+    std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
+    OCResourceHandle handle;
+    // OCResourceProperty is defined ocstack.h
+    uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+
+    // This will internally create and register the resource.
+    OCStackResult result = OC_STACK_OK;
+    for(int i=0; i<numPresenceResources; i++)
+    {
+        result = OCPlatform::registerResource(handle,
+                resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
+                &entityHandler, resourceProperty);
+        if (result != OC_STACK_OK)
+        {
+            cout << "Resource creation was unsuccessful with resource URI "
+                    << resourceURI.at(i);
+        }
+    }
+}
+
 // Create the instance of the resource class (in this case instance of class 'LightResource').
 LightResource myLightResource;
 
-OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
+OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
 {
     cout << "\tIn Server CPP entity handler:\n";
     return OC_EH_OK;
 }
 
+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;
+}
+
 int main()
 {
     // Create PlatformConfig object
@@ -170,6 +300,30 @@ int main()
     };
 
     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
     {
         using namespace OC::OCPlatform;
@@ -178,26 +332,39 @@ int main()
 
         // Invoke createResource function of class light.
         myLightResource.createResource();
+        std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
 
-        printf("\nEnter a key to create the second resource\n");
-        getchar();
+        std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
+
+        sleep(10);
+
+        std :: cout << "\nCreating the second resource of type \"core.light\"" <<  std :: endl;
+        sleep(1);
 
         myLightResource.createResource2();
 
-        printf("\nEnter a key to stop the presence\n");
-        getchar();
+        std :: cout << "Stopping presence\n" << std :: endl;
+        sleep(1);
         stopPresence();
 
-        printf("\nEnter a key to restart the presence\n");
-        getchar();
+        std :: cout << "Restarting presence\n" << std :: endl;
+        sleep(1);
 
         startPresence(30);
 
-        printf("\nEnter a key to create the third resource\n");
-        getchar();
+        std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
+        sleep(1);
 
         myLightResource.createResource3();
 
+        std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
+        sleep(1);
+
+        createPresenceResources();
+
+        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
         // don't ever call cv.notify, this should be a non-processor intensive version
@@ -207,9 +374,9 @@ int main()
         std::unique_lock<std::mutex> lock(blocker);
         cv.wait(lock);
     }
-    catch(OCException e)
+    catch(OCException& e)
     {
-        //log(e.what());
+        oclog() << "Exception in main: "<< e.what();
     }
 
     // No explicit call to stop the platform.
@@ -217,3 +384,4 @@ int main()
 
     return 0;
 }
+