Example #1
0
void foundZigbeeCallback(TWDevice *device)
{
    int count = device->endpointOfInterest->clusterList->count;
    size_t lenSeparator = strlen ("/");
    int ret = 0;
    for(int i=0; i < count; i++)
    {
        PIResource_Zigbee *piResource = (PIResource_Zigbee *) OICMalloc(sizeof(*piResource));
        if (!piResource)
        {
            OC_LOG (ERROR, TAG, "Out of memory");
            return;
        }
        piResource->header.plugin = (PIPluginBase *)gPlugin;
        size_t newUriSize = strlen(PI_ZIGBEE_PREFIX) + lenSeparator +
                        sizeof(device->nodeId) + lenSeparator +
                        sizeof(device->endpointOfInterest->endpointId) + lenSeparator +
                        sizeof(device->endpointOfInterest->clusterList->clusterIds[i].clusterId)
                        + 1; // NULL Terminator
        char * newUri = (char *) OICCalloc(newUriSize, 1);

        if (!newUri)
        {
            OC_LOG (ERROR, TAG, "Out of memory");
            return;
        }

        ret = snprintf(newUri, newUriSize, "%s/%s/%s/%s",
                      PI_ZIGBEE_PREFIX,
                      device->nodeId,
                      device->endpointOfInterest->endpointId,
                      device->endpointOfInterest->clusterList->clusterIds[i].clusterId);
        if(ret < 0)
        {
            OC_LOG (ERROR, TAG, "Encoding error occurred trying to build Zigbee URI.");
        }
        else if(ret > newUriSize)
        {
            OC_LOG_V (ERROR, TAG, "Did not allocate enough memory to build URI. Required Size: %d",
                      ret);
        }

        piResource->header.piResource.uri = newUri;
        piResource->header.piResource.resourceTypeName =

            (char *) ZigBeeClusterIDToOICResourceType(
                device->endpointOfInterest->clusterList->clusterIds[i].clusterId);

        if(piResource->header.piResource.resourceTypeName == NULL)
        {
            OC_LOG_V (ERROR, TAG, "unsupported clusterId : %d",
                device->endpointOfInterest->clusterList->clusterIds[i].clusterId);
            OICFree(piResource->header.piResource.uri);
            OICFree(piResource);
            continue;
        }
        piResource->header.piResource.resourceInterfaceName =
                            OC_RSRVD_INTERFACE_DEFAULT;

        piResource->header.piResource.callbackParam = NULL;
        piResource->header.piResource.resourceProperties = 0;
        piResource->eui = OICStrdup(device->eui);
        piResource->nodeId = OICStrdup(device->nodeId);
        piResource->endpointId = OICStrdup(device->endpointOfInterest->endpointId);
        piResource->clusterId =
            OICStrdup(device->endpointOfInterest->clusterList->clusterIds[i].clusterId);
        (*gPlugin)->header.NewResourceFoundCB(&(*gPlugin)->header, &piResource->header);
    }
}
Example #2
0
void foundZigbeeCallback(TWDevice *device)
{
    if(!device)
    {
        OC_LOG(ERROR, TAG, "foundZigbeeCallback: Invalid parameter.");
        return;
    }
    int count = device->endpointOfInterest->clusterList->count;
    for(int i=0; i < count; i++)
    {
        PIResource_Zigbee *piResource = (PIResource_Zigbee *) OICMalloc(sizeof(*piResource));
        if (!piResource)
        {
            OC_LOG (ERROR, TAG, "Out of memory");
            return;
        }
        piResource->header.plugin = (PIPluginBase *)gPlugin;

        OCStackResult result = buildURI(&piResource->header.piResource.uri,
                                PI_ZIGBEE_PREFIX,
                                device->nodeId,
                                device->endpointOfInterest->endpointId,
                                device->endpointOfInterest->clusterList->clusterIds[i].clusterId);

        if(result != OC_STACK_OK)
        {
            OICFree(piResource);
            return;
        }

        char * foundClusterID =
            device->endpointOfInterest->clusterList->clusterIds[i].clusterId;

        if (strcmp (foundClusterID, ZB_IAS_ZONE_CLUSTER) == 0)
        {
            piResource->header.piResource.resourceTypeName
                = getResourceTypeForIASZoneType (device);

            OCStackResult ret = TWListenForStatusUpdates (device->nodeId,
                                      device->endpointOfInterest->endpointId);

            if (ret != OC_STACK_OK)
            {
                // Just log it and move on if this fails?
                // or not create this resource at all?
                OC_LOG (ERROR, TAG, "Command to listen for status updates failed");
            }
        }
        else
        {
            piResource->header.piResource.resourceTypeName =
                    (char *) ZigBeeClusterIDToOICResourceType(foundClusterID);
        }

        if(piResource->header.piResource.resourceTypeName == NULL)
        {
            OC_LOG_V (ERROR, TAG, "unsupported clusterId : %s",
                device->endpointOfInterest->clusterList->clusterIds[i].clusterId);
            OICFree(piResource->header.piResource.uri);
            OICFree(piResource);
            continue;
        }
        piResource->header.piResource.resourceInterfaceName =
                            OC_RSRVD_INTERFACE_DEFAULT;

        piResource->header.piResource.callbackParam = NULL;
        piResource->header.piResource.resourceProperties = 0;
        piResource->eui = OICStrdup(device->eui);
        piResource->nodeId = OICStrdup(device->nodeId);
        piResource->endpointId = OICStrdup(device->endpointOfInterest->endpointId);
        piResource->clusterId =
            OICStrdup(device->endpointOfInterest->clusterList->clusterIds[i].clusterId);
        (*gPlugin)->header.NewResourceFoundCB(&(*gPlugin)->header, &piResource->header);
    }
}