Added Policy property to web type linking format
authorErich Keane <erich.keane@intel.com>
Fri, 5 Jun 2015 00:05:31 +0000 (17:05 -0700)
committerErich Keane <erich.keane@intel.com>
Fri, 5 Jun 2015 17:22:53 +0000 (17:22 +0000)
This change set is based on OIC core framework standard specification draft A,
Page#30,section 7.2.1.3 (Policy Property Definition).Added necessary changes
to display policy property (Discoverable,Observable, Active, Slow etc) in
JSON format. Modified core framework unit tests and samples to validate.

Change-Id: I406889d21488e3c4d49b9e3fac5d223770d9420e
Signed-off-by: Moumita Ray <moumita.ray@intel.com>
Signed-off-by: Erich Keane <erich.keane@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1110
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
resource/csdk/stack/include/octypes.h
resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp
resource/csdk/stack/src/ocresource.c
resource/include/OCSerialization.h
resource/include/StringConstants.h
resource/src/OCRepresentation.cpp
resource/unittests/OCPlatformTest.cpp

index 76c4ccf..b14b1f2 100644 (file)
@@ -83,7 +83,8 @@ extern "C" {
 #define OC_RSRVD_FW_VERSION             "mnfv"
 #define OC_RSRVD_HOST_NAME              "hn"
 #define OC_RSRVD_VERSION                "icv"
-#define OC_RSRVD_OBSERVABLE             "obs"
+#define OC_RSRVD_POLICY                 "p"
+#define OC_RSRVD_BITMAP                 "bm"
 #define OC_RSRVD_SECURE                 "sec"
 #define OC_RSRVD_HOSTING_PORT           "port"
 #define OC_RSRVD_SERVER_INSTANCE_ID     "sid"
@@ -201,14 +202,17 @@ typedef enum
 
 /**
  * Resource Properties.
+ * The value of a policy property is defined as bitmap.
+ * The LSB represents OC_DISCOVERABLE and Second LSB bit represents OC_OBSERVABLE and so on.
+ * Not including the policy property is equivalent to zero.
  *
  * ::OC_RES_PROP_NONE When none of the bits are set, the resource is non-discoverable &
  *                    non-observable by the client.
+ * ::OC_DISCOVERABLE When this bit is set, the resource is allowed to be discovered by clients.
+ * ::OC_OBSERVABLE   When this bit is set, the resource is allowed to be observed by clients.
  * ::OC_ACTIVE        When this bit is set, the resource is initialized, otherwise the resource
  *                    is 'inactive'. 'inactive' signifies that the resource has been marked for
  *                    deletion or is already deleted.
- * ::OC_DISCOVERABLE  When this bit is set, the resource is allowed to be discovered by clients.
- * ::OC_OBSERVABLE    When this bit is set, the resource is allowed to be observed by clients.
  * ::OC_SLOW          When this bit is set, the resource has been marked as 'slow'. 'slow'
  *                    signifies that responses from this resource can expect delays in
  *                    processing its requests from clients.
@@ -217,9 +221,9 @@ typedef enum
 typedef enum
 {
     OC_RES_PROP_NONE = (0),
-    OC_ACTIVE        = (1 << 0),
-    OC_DISCOVERABLE  = (1 << 1),
-    OC_OBSERVABLE    = (1 << 2),
+    OC_DISCOVERABLE  = (1 << 0),
+    OC_OBSERVABLE    = (1 << 1),
+    OC_ACTIVE        = (1 << 2),
     OC_SLOW          = (1 << 3),
     OC_SECURE        = (1 << 4)
 } OCResourceProperty;
index 7b2889b..b2c5ffd 100644 (file)
@@ -137,7 +137,6 @@ char* constructJsonResponse (OCEntityHandlerRequest *ehRequest)
         {
             currLightResource->state = prop->valueint;
         }
-
         cJSON_Delete(putJson);
     }
 
@@ -757,6 +756,7 @@ void *ChangeLightRepresentation (void *param)
                 cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
                 cJSON_AddBoolToObject(format, "state", Light.state);
                 cJSON_AddNumberToObject(format, "power", Light.power);
+
                 char * obsResp = cJSON_Print(json);
                 cJSON_Delete(json);
                 result = OCNotifyListOfObservers (Light.handle, obsNotify, j,
index 2b1b4e9..0374a6d 100644 (file)
@@ -308,6 +308,7 @@ BuildVirtualResourceResponse(const OCResource *resourcePtr, uint8_t filterOn,
     OCResourceInterface *interfacePtr = NULL;
     cJSON *resObj = NULL;
     cJSON *propObj = NULL;
+    cJSON *policyObj = NULL;
     cJSON *rtArray = NULL;
     char *jsonStr = NULL;
     uint8_t encodeRes = 0;
@@ -366,6 +367,7 @@ BuildVirtualResourceResponse(const OCResource *resourcePtr, uint8_t filterOn,
                                    OC_RSRVD_SERVER_INSTANCE_ID,
                                    cJSON_CreateString(OCGetServerInstanceIDString()));
 
+
             cJSON_AddItemToObject (resObj, OC_RSRVD_PROPERTY, propObj = cJSON_CreateObject());
             // Add resource types
             cJSON_AddItemToObject (propObj, OC_RSRVD_RESOURCE_TYPE, rtArray = cJSON_CreateArray());
@@ -384,25 +386,35 @@ BuildVirtualResourceResponse(const OCResource *resourcePtr, uint8_t filterOn,
                 cJSON_AddItemToArray (rtArray, cJSON_CreateString(interfacePtr->name));
                 interfacePtr = interfacePtr->next;
             }
-            // If resource is observable, set observability flag.
-            // Resources that are not observable will not have the flag.
-            if (resourcePtr->resourceProperties & OC_OBSERVABLE)
-            {
-                cJSON_AddItemToObject (propObj, OC_RSRVD_OBSERVABLE,
-                                       cJSON_CreateNumber(OC_RESOURCE_OBSERVABLE));
-            }
-            // Set secure flag for secure resources
-            if (resourcePtr->resourceProperties & OC_SECURE)
+
+            //Add Policy
+            cJSON_AddItemToObject (propObj, OC_RSRVD_POLICY, policyObj = cJSON_CreateObject());
+
+            if (policyObj)
             {
-                cJSON_AddNumberToObject (propObj, OC_RSRVD_SECURE, OC_RESOURCE_SECURE);
-                //Set the IP port also as secure resources are hosted on a different port
-                uint16_t port = 0;
-                if (GetSecurePortInfo (connType, &port) == OC_STACK_OK)
+                // Policy Property Bitmap
+                // If resource is discoverable, set discoverability flag.
+                // Resources that are not discoverable will not have the flag.
+                cJSON_AddNumberToObject (policyObj, OC_RSRVD_BITMAP,
+                                 resourcePtr->resourceProperties & (OC_OBSERVABLE|OC_DISCOVERABLE));
+
+                // Set secure flag for secure resources
+                if (resourcePtr->resourceProperties & OC_SECURE)
                 {
-                    cJSON_AddNumberToObject (propObj, OC_RSRVD_HOSTING_PORT, port);
+                    cJSON_AddNumberToObject (policyObj, OC_RSRVD_SECURE, OC_RESOURCE_SECURE);
+                    //Set the IP port also as secure resources are hosted on a different port
+                    uint16_t port = 0;
+                    if (GetSecurePortInfo (connType, &port) == OC_STACK_OK)
+                    {
+                        cJSON_AddNumberToObject (policyObj, OC_RSRVD_HOSTING_PORT, port);
+                    }
                 }
             }
-
+            else
+            {
+                cJSON_Delete(resObj);
+                return OC_STACK_NO_MEMORY;
+            }
         }
     }
     jsonStr = cJSON_PrintUnformatted (resObj);
index 898fde7..c9e0ce3 100644 (file)
@@ -38,28 +38,29 @@ namespace OC
 
         class ListenResourceContainer
         {
-            class ListenResourcePropertiesContainer
+            class ListenResourcePolicyContainer
             {
                 friend class cereal::access;
                 friend class ListenResourceContainer;
+                friend class ListenResourcePropertiesContainer;
 
                 template<class Archive>
                 void serialize(Archive& ar)
                 {
                     try
                     {
-                        m_observable=false;
-                        int obsTemp;
-                        ar(cereal::make_nvp(OC::Key::OBSERVABLEKEY, obsTemp));
-                        m_observable = obsTemp != 0;
+                        m_observable = false;
+                        ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
+                        // In case of observable
+                        if(m_bm & OC_OBSERVABLE)
+                        {
+                            m_observable = true;
+                        }
                     }
                     catch(cereal::Exception&)
                     {
-                        // we swallow this exception, since it means the key
-                        // doesn't exist, allowing these to be optional
                         ar.setNextName(nullptr);
                     }
-
                     try
                     {
                         m_secure = false;
@@ -72,6 +73,35 @@ namespace OC
                     }
                     catch(cereal::Exception&)
                     {
+                       ar.setNextName(nullptr);
+                    }
+
+                 }
+
+                 bool m_observable;
+                 uint8_t m_bm;
+                 bool m_secure;
+                 int m_port;
+            };
+
+            class ListenResourcePropertiesContainer
+            {
+                friend class cereal::access;
+                friend class ListenResourceContainer;
+
+                template<class Archive>
+                void serialize(Archive& ar)
+                {
+                    try
+                    {
+                        ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
+
+                    }
+                    catch(cereal::Exception&)
+                    {
+                        // we swallow this exception, since it means the key
+                        // doesn't exist, allowing these to be optional
+                        oclog() << "Invalid POLICYKEY"<<std::flush;
                         ar.setNextName(nullptr);
                     }
 
@@ -93,11 +123,9 @@ namespace OC
                     }
                 }
 
-                bool m_observable;
                 std::vector<std::string> m_resourceTypes;
                 std::vector<std::string> m_interfaces;
-                bool m_secure;
-                int m_port;
+                ListenResourcePolicyContainer m_policy;
             };
 
             public:
@@ -138,8 +166,8 @@ namespace OC
                 {
                     ar.setNextName(nullptr);
                 }
-            }
 
+            }
 
             std::string m_uri;
             std::string m_serverId;
@@ -153,17 +181,17 @@ namespace OC
 
             bool observable() const
             {
-                return m_props.m_observable;
+                return m_props.m_policy.m_observable;
             }
 
             OCSecureType secureType() const
             {
-                return m_props.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4;
+                return m_props.m_policy.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4;
             }
 
             int port() const
             {
-                return m_props.m_port;
+                return m_props.m_policy.m_port;
             }
 
             std::vector<std::string> resourceTypes() const
index 7aea3ad..beb70b2 100644 (file)
@@ -124,7 +124,8 @@ namespace OC
     {
         static const std::string OCKEY                      = "oic";
         static const std::string URIKEY                     = "href";
-        static const std::string OBSERVABLEKEY              = "obs";
+        static const std::string POLICYKEY                  = "p";
+        static const std::string BMKEY                      = "bm";
         static const std::string RESOURCETYPESKEY           = "rt";
         static const std::string INTERFACESKEY              = "if";
         static const std::string PROPERTYKEY                = "prop";
index 6f73435..494f9aa 100644 (file)
@@ -466,7 +466,7 @@ namespace OC
                                     boost::get<std::vector<std::string>>(
                                         parseAttributeValue(itr->value)));
                         }
-                        else if(keyName == OC::Key::PROPERTYKEY)
+                        else if(keyName == OC::Key::INTERFACESKEY)
                         {
                             rep.setResourceInterfaces(
                                     boost::get<std::vector<std::string>>(
index 89034d3..993b1d6 100644 (file)
@@ -540,7 +540,7 @@ namespace OCPlatformTest
     //GetDeviceInfo Test
     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithValidParameters)
     {
-        std::string deviceDiscoveryURI = "/oic/res/d";
+        std::string deviceDiscoveryURI = "/oic/d";
         PlatformConfig cfg;
         OCPlatform::Configure(cfg);
         std::ostringstream requestURI;
@@ -559,7 +559,7 @@ namespace OCPlatformTest
 
     TEST(GetDeviceInfoTest, GetDeviceInfoWithNullDeviceInfoHandler)
     {
-        std::string deviceDiscoveryURI = "/oic/res/d";
+        std::string deviceDiscoveryURI = "/oic/d";
         PlatformConfig cfg;
         OCPlatform::Configure(cfg);
         std::ostringstream requestURI;
@@ -571,7 +571,7 @@ namespace OCPlatformTest
 
     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithLowQos)
     {
-        std::string deviceDiscoveryURI = "/oic/res/d";
+        std::string deviceDiscoveryURI = "/oic/d";
         PlatformConfig cfg;
         OCPlatform::Configure(cfg);
         std::ostringstream requestURI;
@@ -583,7 +583,7 @@ namespace OCPlatformTest
 
     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithMidQos)
     {
-        std::string deviceDiscoveryURI = "/oic/res/d";
+        std::string deviceDiscoveryURI = "/oic/d";
         PlatformConfig cfg;
         OCPlatform::Configure(cfg);
         std::ostringstream requestURI;
@@ -595,7 +595,7 @@ namespace OCPlatformTest
 
     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithHighQos)
     {
-        std::string deviceDiscoveryURI = "/oic/res/d";
+        std::string deviceDiscoveryURI = "/oic/d";
         PlatformConfig cfg;
         OCPlatform::Configure(cfg);
         std::ostringstream requestURI;
@@ -607,7 +607,7 @@ namespace OCPlatformTest
 
     TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoWithNaQos)
     {
-        std::string deviceDiscoveryURI = "/oic/res/d";
+        std::string deviceDiscoveryURI = "/oic/d";
         PlatformConfig cfg;
         OCPlatform::Configure(cfg);
         std::ostringstream requestURI;