Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / examples / devicediscoveryserver.cpp
index 9db7736..dba1515 100644 (file)
@@ -19,8 +19,8 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 ///
-///This sample demonstrates the device discovery feature
-///The server sets the device related info. which can
+///This sample demonstrates platform and device discovery feature
+///The server sets the platform and device related info. which can
 ///be later retrieved by a client.
 ///
 
 
 #include "OCPlatform.h"
 #include "OCApi.h"
+#include "ocpayload.h"
 
 using namespace OC;
 
-//Set of strings for each of deviceInfo fields
-std::string contentType = "myContentType";
+//Set of strings for each of platform Info fields
 std::string dateOfManufacture = "myDateOfManufacture";
-std::string deviceName = "myDeviceName";
-std::string deviceUUID = "myDeviceUUID";
-std::string firmwareVersion = "myFirmwareVersion";
-std::string hostName = "myHostName";
-std::string manufacturerName = "myManufacturerNa";
-std::string manufacturerUrl = "myManufacturerUrl";
+std::string firmwareVersion = "my.Firmware.Version";
+std::string manufacturerName = "myName";
+std::string operatingSystemVersion = "myOS";
+std::string hardwareVersion = "myHardwareVersion";
+std::string platformID = "myPlatformID";
+std::string manufacturerUrl = "www.myurl.com";
 std::string modelNumber = "myModelNumber";
-std::string platformVersion = "myPlatformVersion";
-std::string supportUrl = "mySupportUrl";
-std::string version = "myVersion";
+std::string platformVersion = "platformVersion";
+std::string supportUrl = "www.mysupporturl.com";
+std::string systemTime = "mySystemTime";
+
+//Set of strings for each of device info fields
+std::string deviceName = "Bill's Battlestar";
+std::string specVersion = "myDeviceSpecVersion";
+std::string dataModelVersions = "myDeviceModelVersion";
+
+//OCPlatformInfo Contains all the platform info to be stored
+OCPlatformInfo platformInfo;
 
 //OCDeviceInfo Contains all the device info to be stored
 OCDeviceInfo deviceInfo;
 
+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.contentType;
-    delete[] deviceInfo.dateOfManufacture;
     delete[] deviceInfo.deviceName;
-    delete[] deviceInfo.deviceUUID;
-    delete[] deviceInfo.firmwareVersion;
-    delete[] deviceInfo.hostName;
-    delete[] deviceInfo.manufacturerName;
-    delete[] deviceInfo.manufacturerUrl;
-    delete[] deviceInfo.modelNumber;
-    delete[] deviceInfo.platformVersion;
-    delete[] deviceInfo.supportUrl;
-    delete[] deviceInfo.version;
+    delete[] deviceInfo.specVersion;
+    OCFreeOCStringLL(deviceInfo.dataModelVersions);
 }
 
 void DuplicateString(char ** targetString, std::string sourceString)
@@ -71,52 +86,42 @@ void DuplicateString(char ** targetString, std::string sourceString)
     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
 }
 
-OCStackResult SetDeviceInfo(std::string contentType, std::string dateOfManufacture,
-                std::string deviceName, std::string deviceUUID, std::string firmwareVersion,
-                std::string hostName, std::string manufacturerName, std::string manufacturerUrl,
-                std::string modelNumber, std::string platformVersion, std::string supportUrl,
-                std::string version)
+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)
 {
-    if(manufacturerName.length() > MAX_MANUFACTURER_NAME_LENGTH)
-    {
-        return OC_STACK_INVALID_PARAM;
+    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;
+}
 
-    }
 
-    if(manufacturerUrl.length() > MAX_MANUFACTURER_URL_LENGTH)
-    {
-        return OC_STACK_INVALID_PARAM;
+OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions)
+{
+    DuplicateString(&deviceInfo.deviceName, deviceName);
 
-    }
+    if (!specVersion.empty())
+        DuplicateString(&deviceInfo.specVersion, specVersion);
 
-    try
-    {
-        DuplicateString(&deviceInfo.contentType, contentType);
-        DuplicateString(&deviceInfo.dateOfManufacture, dateOfManufacture);
-        DuplicateString(&deviceInfo.deviceName, deviceName);
-        DuplicateString(&deviceInfo.deviceUUID, deviceUUID);
-        DuplicateString(&deviceInfo.firmwareVersion, firmwareVersion);
-        DuplicateString(&deviceInfo.hostName, hostName);
-        DuplicateString(&deviceInfo.manufacturerName, manufacturerName);
-        DuplicateString(&deviceInfo.manufacturerUrl, manufacturerUrl);
-        DuplicateString(&deviceInfo.modelNumber, modelNumber);
-        DuplicateString(&deviceInfo.platformVersion, platformVersion);
-        DuplicateString(&deviceInfo.supportUrl, supportUrl);
-        DuplicateString(&deviceInfo.version, version);
-    }catch(exception &e)
-    {
-        std::cout<<"String Copy failed!!\n";
-        return OC_STACK_ERROR;
-    }
+    if (!dataModelVersions.empty())
+        OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str());
 
     return OC_STACK_OK;
 }
 
 
-
 int main()
 {
-
     // Create PlatformConfig object
     PlatformConfig cfg {
         OC::ServiceType::InProc,
@@ -128,18 +133,25 @@ int main()
 
     OCPlatform::Configure(cfg);
 
-    std::cout<<"Starting server & setting device info\n";
+    std::cout<<"Starting server & setting platform info\n";
+
+    OCStackResult result = SetPlatformInfo(platformID, manufacturerName, manufacturerUrl,
+            modelNumber, dateOfManufacture, platformVersion,  operatingSystemVersion,
+            hardwareVersion, firmwareVersion,  supportUrl, systemTime);
 
-    OCStackResult result = SetDeviceInfo(contentType, dateOfManufacture, deviceName,
-            deviceUUID, firmwareVersion, hostName, manufacturerName, manufacturerUrl,
-            modelNumber, platformVersion, supportUrl, version);
+    result = OCPlatform::registerPlatformInfo(platformInfo);
 
     if(result != OC_STACK_OK)
     {
-        std::cout << "Device Registration failed\n";
+        std::cout << "Platform Registration failed\n";
         return -1;
     }
 
+
+    result = SetDeviceInfo(deviceName, specVersion, dataModelVersions);
+    OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+    OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
+
     result = OCPlatform::registerDeviceInfo(deviceInfo);
 
     if(result != OC_STACK_OK)
@@ -148,6 +160,7 @@ int main()
         return -1;
     }
 
+    DeletePlatformInfo();
     DeleteDeviceInfo();
 
     // A condition variable will free the mutex it is given, then do a non-
@@ -157,7 +170,7 @@ int main()
     std::mutex blocker;
     std::condition_variable cv;
     std::unique_lock<std::mutex> lock(blocker);
-    cv.wait(lock);
+    cv.wait(lock, []{return false;});
 
     // No explicit call to stop the platform.
     // When OCPlatform::destructor is invoked, internally we do platform cleanup
@@ -165,6 +178,3 @@ int main()
     return 0;
 
 }
-
-
-