Replaced zigbee nodeId in URI with zigbee eui.
authorJoseph Morrow <joseph.l.morrow@intel.com>
Wed, 9 Dec 2015 18:08:53 +0000 (10:08 -0800)
committerJon A. Cruz <jonc@osg.samsung.com>
Tue, 15 Dec 2015 00:29:39 +0000 (00:29 +0000)
Change-Id: Ib2df8d45ceb9ecd62bc8fab3978ac8d30f3571e8
Signed-off-by: Joseph Morrow <joseph.l.morrow@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3785
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
plugins/include/internal/pluginlist.h
plugins/include/internal/plugintranslatortypes.h
plugins/src/plugininterface.c
plugins/src/pluginlist.c
plugins/zigbee_wrapper/src/zigbee_wrapper.c

index ea57da0..0dda827 100644 (file)
@@ -42,8 +42,11 @@ OCStackResult DeletePluginList();
 OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResource,
                                     OCResourceHandle * resourceHandle);
 
-OCStackResult GetResourceFromURI(PIPluginBase * plugin, PIResource ** piResource,
-                                    const char * uri);
+OCStackResult GetResourceFromZigBeeNodeId(PIPluginBase * plugin,
+                                          PIResource_Zigbee ** piResource,
+                                          const char * nodeId,
+                                          const char * endpointId,
+                                          const char * clusterId);
 
 OCStackResult AddResourceToPlugin(PIPluginBase * plugin, PIResourceBase * resource);
 
index 901f343..c5a8580 100644 (file)
@@ -58,7 +58,7 @@ typedef void (* PINewResourceFound)(struct PIPluginBase * plugin,
  *
  */
 typedef void (* PIObserveNotificationUpdate)(struct PIPluginBase * plugin,
-                                            const char * uri);
+                                             OCResourceHandle resourceHandle);
 
 /**
  *
index 489ded0..d5b200f 100644 (file)
@@ -138,22 +138,14 @@ void piNewResourceCB(PIPluginBase * p_plugin, PIResourceBase * r_newResource)
     result = AddResourceToPlugin(p_plugin, r_newResource);
 }
 
-void piObserveNotificationUpdate(PIPluginBase * plugin, const char * uri)
+void piObserveNotificationUpdate(PIPluginBase * plugin, OCResourceHandle resourceHandle)
 {
-    if(!plugin || !uri)
+    if(!plugin)
     {
         return;
     }
-    PIResource * piResource = NULL;
 
-    OCStackResult result = GetResourceFromURI(plugin, &piResource, uri);
-    if(result != OC_STACK_OK)
-    {
-        OC_LOG(ERROR, TAG, "Failed to find a matching URI based on observe notification update.");
-        return;
-    }
-
-    result = OCNotifyAllObservers(piResource->resourceHandle, OC_LOW_QOS);
+    OCStackResult result = OCNotifyAllObservers(resourceHandle, OC_LOW_QOS);
     if(result != OC_STACK_OK && result != OC_STACK_NO_OBSERVERS)
     {
         OC_LOG_V(ERROR, TAG, "Failed to notify observers of update. Result: %d", result);
index cf9a331..5c3b456 100644 (file)
@@ -101,28 +101,72 @@ OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResou
     return OC_STACK_NO_RESOURCE;
 }
 
-OCStackResult GetResourceFromURI(PIPluginBase * plugin, PIResource ** piResource,
-                                    const char * uri)
+static bool ZigbeeStrEquals(const char * s1, const char * s2, size_t s1_length, size_t s2_length)
 {
-    if (!plugin || !piResource || !uri)
+    if (!s1 || !s2 || s1_length == 0 || s2_length == 0)
     {
+        return false;
+    }
+    if (s1_length == s2_length && memcmp(s1, s2, (s1_length + 1)) == 0)
+    {
+        return true;
+    }
+    return false;
+}
+
+OCStackResult GetResourceFromZigBeeNodeId(PIPluginBase * plugin,
+                                          PIResource_Zigbee ** piResource,
+                                          const char * nodeId,
+                                          const char * endpointId,
+                                          const char * clusterId)
+{
+    OC_LOG(INFO, TAG, "Entered GetResourceFromZigBeeNodeId().");
+    if(!plugin || !piResource || !nodeId || !clusterId || !endpointId)
+    {
+        OC_LOG(ERROR, TAG, "Invalid param.");
         return OC_STACK_INVALID_PARAM;
     }
+    if(plugin->type != PLUGIN_ZIGBEE)
+    {
+        OC_LOG(ERROR, TAG, "Plugin Type is not Zigbee.");
+        return OC_STACK_INVALID_PARAM;
+    }
+
     PIResourceBase * out = NULL;
     PIResourceBase * tmp = NULL;
-    size_t checkUriLength = strlen(uri);
-    size_t indexUriLength = 0;
-    size_t minLength = 0;
+    size_t checkNodeIdLength = strlen(nodeId);
+    size_t checkEndpointIdLength = strlen(endpointId);
+    size_t checkClusterIdLength = strlen(clusterId);
+    size_t indexLength = 0;
     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
     {
-        indexUriLength = strlen(out->piResource.uri);
-        minLength = indexUriLength > checkUriLength ? checkUriLength : indexUriLength;
-        if ((checkUriLength == indexUriLength) &&
-            memcmp(out->piResource.uri, uri, minLength + 1) == 0)
+        indexLength = strlen(((PIResource_Zigbee *)out)->nodeId);
+        if(ZigbeeStrEquals(nodeId,
+                           ((PIResource_Zigbee *)out)->nodeId,
+                           checkNodeIdLength,
+                           indexLength) == false)
         {
-            *piResource = (PIResource *) out;
-            return OC_STACK_OK;
+            continue;
+        }
+        indexLength = strlen(((PIResource_Zigbee *)out)->endpointId);
+        if(ZigbeeStrEquals(endpointId,
+                           ((PIResource_Zigbee *)out)->endpointId,
+                           checkEndpointIdLength,
+                           indexLength) == false)
+        {
+            continue;
+        }
+        indexLength = strlen(((PIResource_Zigbee *)out)->clusterId);
+        if(ZigbeeStrEquals(clusterId,
+                           ((PIResource_Zigbee *)out)->clusterId,
+                           checkClusterIdLength,
+                           indexLength) == false)
+        {
+            continue;
         }
+        OC_LOG_V(INFO, TAG, "Found a match! URI = %s", out->piResource.uri);
+        *piResource = (PIResource_Zigbee *) out;
+        return OC_STACK_OK;
     }
     *piResource = NULL;
     return OC_STACK_NO_RESOURCE;
index 57cc6d8..6b1ca7e 100644 (file)
@@ -249,18 +249,18 @@ const char * getResourceTypeForIASZoneType(TWDevice *device, PIPluginBase* plugi
 
 OCStackResult buildURI(char ** output,
                        const char * prefix,
-                       const char * nodeId,
+                       const char * eui,
                        const char * endpointId,
                        const char * clusterId)
 {
-    if (!output || !prefix || !nodeId || !endpointId || !clusterId)
+    if(!output || !prefix || !eui || !endpointId || !clusterId)
     {
         return OC_STACK_INVALID_PARAM;
     }
     const char LEN_SEPARATOR[] = "/";
     size_t lenSeparatorSize = sizeof(LEN_SEPARATOR) - 1;
     size_t newUriSize = strlen(prefix) + lenSeparatorSize +
-                        strlen(nodeId) + lenSeparatorSize +
+                        strlen(eui) + lenSeparatorSize +
                         strlen(endpointId) + lenSeparatorSize +
                         strlen(clusterId)
                         + 1; // NULL Terminator
@@ -282,7 +282,7 @@ OCStackResult buildURI(char ** output,
     {
         goto exit;
     }
-    temp = OICStrcat(*output, newUriSize, nodeId);
+    temp = OICStrcat(*output, newUriSize, eui);
     if (temp != *output)
     {
         goto exit;
@@ -336,7 +336,7 @@ void foundZigbeeCallback(TWDevice *device, PIPlugin_Zigbee* plugin)
 
         OCStackResult result = buildURI(&piResource->header.piResource.uri,
                                 PI_ZIGBEE_PREFIX,
-                                device->nodeId,
+                                device->eui,
                                 device->endpointOfInterest->endpointId,
                                 device->endpointOfInterest->clusterList->clusterIds[i].clusterId);
 
@@ -400,20 +400,20 @@ void zigbeeZoneStatusUpdate(TWUpdate * update, PIPlugin_Zigbee* plugin)
         return;
     }
 
-    char * uri = NULL;
-    OCStackResult result = buildURI(&uri,
-                                    PI_ZIGBEE_PREFIX,
-                                    update->nodeId,
-                                    update->endpoint,
-                                    ZB_IAS_ZONE_CLUSTER);
-    if (result != OC_STACK_OK || !uri)
-    {
-        OC_LOG_V(ERROR, TAG, "Failed to build URI with result: %d", result);
+    PIResource_Zigbee * piResource = NULL;
+    OCStackResult result = GetResourceFromZigBeeNodeId((PIPluginBase *)plugin,
+                                                    &piResource,
+                                                    update->nodeId,
+                                                    update->endpoint,
+                                                    ZB_IAS_ZONE_CLUSTER);
+    if (result != OC_STACK_OK || !piResource)
+    {
+        OC_LOG_V(ERROR, TAG, "Failed to retrieve resource handle with result: %d", result);
         return;
     }
 
-    plugin->header.ObserveNotificationUpdate((PIPluginBase *)plugin, uri);
-    OICFree(uri);
+    plugin->header.ObserveNotificationUpdate((PIPluginBase *)plugin,
+                                                 piResource->header.piResource.resourceHandle);
 }
 
 OCStackResult ZigbeeInit(const char * comPort, PIPlugin_Zigbee ** plugin,