Пример #1
0
OCStackApplicationResult PingRequestCallback(void* ctx, OCDoHandle handle,
                                             OCClientResponse *clientResponse)
{
    OIC_LOG(DEBUG, TAG, "PingRequestCallback IN");
    (void) ctx;
    (void) handle;
    if (NULL == clientResponse)
    {
        OIC_LOG(ERROR, TAG, "clientResponse is NULL");
        return OC_STACK_KEEP_TRANSACTION;
    }

    CAEndpoint_t endpoint = { .adapter = CA_ADAPTER_TCP };
    CopyDevAddrToEndpoint(&(clientResponse->devAddr), &endpoint);

    HandleKeepAliveResponse(&endpoint, clientResponse->result,
                            (OCRepPayload *)clientResponse->payload);

    OIC_LOG(DEBUG, TAG, "PingRequestCallback OUT");
    return OC_STACK_KEEP_TRANSACTION;
}
Пример #2
0
OCStackResult RMHandleRequestPayload(OCDevAddr devAddr, const uint8_t *reqPayload,
                                     size_t payloadSize)
{
    OIC_LOG(DEBUG, TAG, "RMHandleRequestPayload IN");
    RM_NULL_CHECK_WITH_RET(reqPayload, TAG, "reqPayload");

    uint32_t gatewayId = 0;

    OCStackResult result = RMPParseRequestPayload(reqPayload, payloadSize, &gatewayId);
    RM_VERIFY_SUCCESS(result, OC_STACK_OK);
    OIC_LOG(INFO, TAG, "RMPParseRequestPayload is success");
    // Check if the entry is its own.
    if (gatewayId == g_GatewayID)
    {
        OIC_LOG(INFO, TAG, "Own Request Received!!");
        return OC_STACK_CONTINUE;
    }

    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
    CopyDevAddrToEndpoint(&devAddr, &endpoint);

    OIC_LOG_V(INFO, TAG, "Add the gateway ID: %u", gatewayId);
    RTMDestIntfInfo_t destInterfaces = {.observerId = 0};
    destInterfaces.destIntfAddr = endpoint;
    result = RTMAddGatewayEntry(gatewayId, 0, 1, &destInterfaces, &g_routingGatewayTable);

    if (OC_STACK_OK != result)
    {
        OIC_LOG(DEBUG, TAG, "Gateway was not added to the routing table");
        return result;
    }

    OIC_LOG(INFO, TAG, "Gateway was added");
    // Create a list to add the updated entries and notify the observers
    u_linklist_t *updatedTableList = u_linklist_create();
    if(!updatedTableList)
    {
        OIC_LOG(DEBUG, TAG, "Failure to notify");
        return OC_STACK_NO_MEMORY;
    }

    RTMGatewayId_t gwId = {.gatewayId = gatewayId};
    RTMGatewayEntry_t newNode;
    newNode.destination = &gwId;
    newNode.routeCost = 1;
    u_linklist_add(updatedTableList, (void *)&newNode);
    RTMPrintTable(g_routingGatewayTable, g_routingEndpointTable);

    OCRepPayload *updatedPayload = NULL;

    g_sequenceNumber++;
    result = RMPConstructObserveResPayload(g_GatewayID, g_sequenceNumber,
                                           updatedTableList, false,
                                           &updatedPayload);
    if (OC_STACK_OK != result)
    {
        OIC_LOG_V(ERROR, TAG, "RMPConstructObserveResPayload failed[%d]", result);
        RMPFreePayload(updatedPayload);
        goto exit;
    }

    result = RMSendNotificationToAll(updatedPayload);
    RMPFreePayload(updatedPayload);
    RM_VERIFY_SUCCESS(result, OC_STACK_OK);

exit:
    u_linklist_free(&updatedTableList);
    OIC_LOG(DEBUG, TAG, "RMHandleRequestPayload OUT");
    return result;
}

OCStackResult RMHandleResponsePayload(const OCDevAddr *devAddr, const OCRepPayload *respPayload)
{
    OIC_LOG(DEBUG, TAG, "RMHandleResponsePayload IN");
    RM_NULL_CHECK_WITH_RET(respPayload, TAG, "respPayload");

    // Parse the Payload to get the Gateway ID of neighbouring node.
    uint32_t gatewayId = 0;
    uint32_t seqNum = 0;
    u_linklist_t *gatewayTableList = NULL;
    bool isUpdateSeqNum = false;

    OCStackResult result = RMPParseResponsePayload(respPayload, &gatewayId, &seqNum,
                                                   &gatewayTableList,  &isUpdateSeqNum);
    RM_VERIFY_SUCCESS(result, OC_STACK_OK);
    // Check if the entry is its own.
    if (gatewayId == g_GatewayID)
    {
        OIC_LOG(INFO, TAG, "-------------->Own entry, continue!!");
        RTMFreeGatewayRouteTable(&gatewayTableList);
        return OC_STACK_ERROR;
    }
    // Convert OCDevAddr to endpoint address
    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_FLAGS};
    CopyDevAddrToEndpoint(devAddr, &endpoint);
    RTMDestIntfInfo_t destInterfaces = {.observerId = 0};
    destInterfaces.destIntfAddr = endpoint;
    if (0 < seqNum)
    {
        OIC_LOG_V(DEBUG, TAG, "Sequence Number of Resp payload is %d, Forceupdate: %d",
                 seqNum, isUpdateSeqNum);
        result = RTMUpdateEntryParameters(gatewayId, seqNum, &destInterfaces,
                                          &g_routingGatewayTable, isUpdateSeqNum);
        if (OC_STACK_COMM_ERROR == result)
        {
            OIC_LOG(ERROR, TAG, "Few packet drops are found, sequence number is not matching");
            // Send a observe request to the gateway.
            RMSendObserveRequest(devAddr, NULL);
            RTMFreeGatewayRouteTable(&gatewayTableList);
            return result;
        }
        else if (OC_STACK_DUPLICATE_REQUEST == result)
        {
            OIC_LOG(ERROR, TAG, "Same sequence number is received");
            RTMFreeGatewayRouteTable(&gatewayTableList);
            return result;
        }
    }

    // Check if the payload is for Removal
    bool doRemoveEntry = false;

    if (NULL != gatewayTableList && NULL != gatewayTableList->list)
    {
        RTMGatewayEntry_t *headPtr = u_linklist_get_data(gatewayTableList->list);
        if (headPtr && 0 == headPtr->routeCost)
        {
            OIC_LOG(INFO, TAG, "Remove entry is called");
            doRemoveEntry = true;
        }
    }

    // Create a list to add the updated entries and notify the observers
    u_linklist_t *updatedTableList = u_linklist_create();
    if(!updatedTableList)
    {
        OIC_LOG(DEBUG, TAG, "Failed to allocate memory");
        return OC_STACK_NO_MEMORY;
    }

    u_linklist_t *alternativeRouteList = u_linklist_create();
    if(!alternativeRouteList)
    {
        OIC_LOG(DEBUG, TAG, "Failed to allocate memory");
        return OC_STACK_NO_MEMORY;
    }

    OCRepPayload *updatedPayload = NULL;
    if (false == doRemoveEntry)
    {
        OIC_LOG_V(INFO, TAG, "Add the gateway ID: %u", gatewayId);
        result = RTMAddGatewayEntry(gatewayId, 0, 1, &destInterfaces, &g_routingGatewayTable);
        if (OC_STACK_OK == result)
        {
            OIC_LOG(INFO, TAG, "Node was added");
            RTMGatewayId_t gwId = {.gatewayId = gatewayId};
            RTMGatewayEntry_t newNode;
            newNode.destination = &gwId;
            newNode.routeCost = 1;
            u_linklist_add(updatedTableList, (void *)&newNode);
            RTMPrintTable(g_routingGatewayTable, g_routingEndpointTable);

            if (NULL == gatewayTableList)
            {
                OIC_LOG(INFO, TAG, "Received a Discover Payload");
                g_sequenceNumber++;
                result = RMPConstructObserveResPayload(g_GatewayID, g_sequenceNumber,
                                                       updatedTableList, false,
                                                       &updatedPayload);
                RM_VERIFY_SUCCESS(result, OC_STACK_OK);
                goto sendNotification;
            }
        }
    }