[IOT-1538] Add support for Protocol-Independent ID
[platform/upstream/iotivity.git] / cloud / samples / client / airconditioner / aircon_controlee.cpp
index 6b53aea..a18f1fd 100644 (file)
@@ -9,7 +9,7 @@
 
 #include "ocstack.h"
 #include "ocpayload.h"
-#include "oicresourcedirectory.h"
+#include "RDClient.h"
 
 #include <OCApi.h>
 #include <OCPlatform.h>
@@ -85,7 +85,6 @@ class Resource
                 pResponse->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
             }
 
-            pResponse->setErrorCode(200);
             pResponse->setResponseResult(OC_EH_OK);
 
             return OCPlatform::sendResponse(pResponse);
@@ -98,7 +97,6 @@ class Resource
                 shared_ptr<OCResourceResponse> resourceResponse =
                 { make_shared<OCResourceResponse>() };
 
-                resourceResponse->setErrorCode(200);
                 resourceResponse->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
 
                 return OCPlatform::notifyListOfObservers(m_handle,
@@ -502,6 +500,46 @@ static FILE *client_open(const char * /*path*/, const char *mode)
     return fopen("./aircon_controlee.dat", mode);
 }
 
+OCStackResult SetDeviceInfo()
+{
+    OCStackResult result = OC_STACK_ERROR;
+
+    OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
+
+    if (handle == NULL)
+    {
+        cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << endl;
+        return result;
+    }
+
+    result = OCBindResourceTypeToResource(handle, "oic.d.airconditioner");
+
+    if (result != OC_STACK_OK)
+    {
+        cout << "Failed to add device type" << endl;
+        return result;
+    }
+
+    result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, "FAC_2016");
+
+    if (result != OC_STACK_OK)
+    {
+        cout << "Failed to set device name" << endl;
+        return result;
+    }
+
+    result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
+                                          "d7d2b492-83ac-4783-9dcc-b1b54587ebed");
+
+    if (result != OC_STACK_OK)
+    {
+        cout << "Failed to set piid" << endl;
+        return result;
+    }
+
+    return OC_STACK_OK;
+}
+
 int main(int argc, char *argv[])
 {
     if (argc != 4 && argc != 5)
@@ -608,6 +646,11 @@ int main(int argc, char *argv[])
                                                   , &binarySwitch, placeholders::_1),
                                           OC_OBSERVABLE);
 
+    if (result != OC_STACK_OK)
+    {
+        exit(EXIT_FAILURE);
+    }
+
     uri = temperature.getResourceUri();
     rt = temperature.getResourceType()[0];
     itf = temperature.getInterfaces()[0];
@@ -620,28 +663,37 @@ int main(int argc, char *argv[])
                                                   , &temperature, placeholders::_1),
                                           OC_OBSERVABLE);
 
+    if (result != OC_STACK_OK)
+    {
+        exit(EXIT_FAILURE);
+    }
+
     result = airConditioner.addChildResource(&binarySwitch);
 
-    result = airConditioner.addChildResource(&temperature);
+    if (result != OC_STACK_OK)
+    {
+        exit(EXIT_FAILURE);
+    }
 
-    cout << "Publishing resources to cloud ";
+    result = airConditioner.addChildResource(&temperature);
 
+    if (result != OC_STACK_OK)
+    {
+        exit(EXIT_FAILURE);
+    }
 
-    ResourceHandles resourceHandles;
+    cout << "Publishing resources to cloud ";
 
-    OCDeviceInfo        devInfoAirConditioner;
-    OCStringLL          deviceType;
+    result = SetDeviceInfo();
 
-    deviceType.value = "oic.d.airconditioner";
-    deviceType.next = NULL;
-    devInfoAirConditioner.deviceName = "FAC_2016";
-    devInfoAirConditioner.types = &deviceType;
-    devInfoAirConditioner.specVersion = NULL;
-    devInfoAirConditioner.dataModelVersions = NULL;
+    if (result != OC_STACK_OK)
+    {
+        exit(EXIT_FAILURE);
+    }
 
-    OCPlatform::registerDeviceInfo(devInfoAirConditioner);
+    ResourceHandles resourceHandles;
 
-    result = OCPlatform::publishResourceToRD(host, OCConnectivityType::CT_ADAPTER_TCP,
+    result = RDClient::Instance().publishResourceToRD(host, OCConnectivityType::CT_ADAPTER_TCP,
              resourceHandles,
              &onPublish);
 
@@ -649,7 +701,7 @@ int main(int argc, char *argv[])
 
     resourceHandles.push_back(airConditioner.m_handle);
 
-    result = OCPlatform::publishResourceToRD(host, OCConnectivityType::CT_ADAPTER_TCP,
+    result = RDClient::Instance().publishResourceToRD(host, OCConnectivityType::CT_ADAPTER_TCP,
              resourceHandles,
              &onPublish);
 
@@ -681,7 +733,6 @@ int main(int argc, char *argv[])
 
             case 'q':
                 goto exit;
-                break;
         }
     }