예제 #1
0
static int ProcessSetOperation(AwaClientSetOperation * operation)
{
    int result = 0;
    if (AwaClientSetOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT) != AwaError_Success)
    {
        Error("AwaClientSetOperation_Perform failed\n");
        result = 1;
    }
    else
    {
        Verbose("Set operation completed successfully.\n");
    }

    const AwaClientSetResponse * response = AwaClientSetOperation_GetResponse(operation);
    if (response != NULL)
    {
        AwaPathIterator * iterator = AwaClientSetResponse_NewPathIterator(response);
        while (AwaPathIterator_Next(iterator))
        {
            const char * path = AwaPathIterator_Get(iterator);
            const AwaPathResult * result = AwaClientSetResponse_GetPathResult(response, path);
            AwaError error = AwaPathResult_GetError(result);
            if (error != AwaError_Success)
            {
                Error("Failed to set on path %s: %s\n", path, AwaError_ToString(error));
            }
        }
        AwaPathIterator_Free(&iterator);
    }
    return result;
}
예제 #2
0
static void ClientSubscribe_PerformSuccessfulCallback(const char * path, void * value, void * context)
{
    AwaClientSubscription * subscription = (AwaClientSubscription *)value;
    PerformSuccessfulCallbackContext * successContext = (PerformSuccessfulCallbackContext *)context;

    const AwaPathResult * result = AwaClientSubscribeResponse_GetPathResult(successContext->Response, subscription->Path);
    if (!subscription->Cancel)
    {
        if (AwaPathResult_GetError(result) == AwaError_Success)
        {
            Map_Put(ClientSession_GetSubscribers(successContext->Session), subscription->Path, subscription);

            // map the subscription to this session.
            subscription->Session = successContext->Session;
        }
        else
        {
            LogErrorWithEnum(AwaPathResult_GetError(result), "Failed to subscribe to path %s\n", subscription->Path);
            successContext->Result = AwaError_Response;
        }
    }
}
static int ProcessExecuteOperation(const AwaServerSession * session, AwaServerExecuteOperation * operation, const char * clientID)
{
    int result = 0;
    if (AwaServerExecuteOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT) != AwaError_Success)
    {
        Error("AwaServerExecuteOperation_Perform failed\n");
        result = 1;
    }

    const AwaServerExecuteResponse * response = AwaServerExecuteOperation_GetResponse(operation, clientID);
    if (response != NULL)
    {
        AwaPathIterator * pathIterator = AwaServerExecuteResponse_NewPathIterator(response);
        if (pathIterator != NULL)
        {
            while (AwaPathIterator_Next(pathIterator))
            {
                const char * path = AwaPathIterator_Get(pathIterator);
                const AwaPathResult * result = AwaServerExecuteResponse_GetPathResult(response, path);
                AwaError error = AwaPathResult_GetError(result);
                if (error == AwaError_Success)
                {
                    printf("Target %s executed successfully\n", path);
                }
                else
                {
                    if (error != AwaError_LWM2MError)
                    {
                        Error("Failed to execute target %s: %s\n", path, AwaError_ToString(error));
                    }
                    else
                    {
                        Error("Failed to execute target %s: %s\n", path, AwaLWM2MError_ToString(AwaPathResult_GetLWM2MError(result)));
                    }
                }
            }
            AwaPathIterator_Free(&pathIterator);
        }
        else
        {
            Error("AwaServerExecuteResponse_NewPathIterator returned NULL");
        }
    }
    else
    {
        Error("AwaServerExecuteOperation_GetResponse returned NULL");
    }
    return result;
}
예제 #4
0
static int ProcessDeleteOperation(AwaClientDeleteOperation * operation)
{
    int result = 0;
    if (AwaClientDeleteOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT) != AwaError_Success)
    {
        Error("AwaClientDeleteOperation_Perform failed\n");
        result = 1;
    }

    const AwaClientDeleteResponse * response = AwaClientDeleteOperation_GetResponse(operation);
    if (response != NULL)
    {
        AwaPathIterator * pathIterator = AwaClientDeleteResponse_NewPathIterator(response);
        if (pathIterator != NULL)
        {
            while (AwaPathIterator_Next(pathIterator))
            {
                const char * path = AwaPathIterator_Get(pathIterator);
                const AwaPathResult * result = AwaClientDeleteResponse_GetPathResult(response, path);
                AwaError error = AwaPathResult_GetError(result);
                if (error == AwaError_Success)
                {
                    Verbose("Target %s deleted\n", path);
                }
                else
                {
                    Error("Failed to delete target %s: %s\n", path, AwaError_ToString(error));
                }
            }
            AwaPathIterator_Free(&pathIterator);
        }
        else
        {
            Error("AwaClientDeleteResponse_NewPathIterator returned NULL");
        }
    }
    else
    {
        Error("AwaClientDeleteOperation_GetResponse returned NULL");
    }

    return result;
}
예제 #5
0
static char * ResponseToCString(const AwaClientSession * session, const AwaClientGetResponse * response, bool quiet)
{
    char * cstring = strdup("");
    AwaObjectID lastObjectID = AWA_INVALID_ID;
    AwaObjectInstanceID lastObjectInstanceID = AWA_INVALID_ID;
    AwaPathIterator * iterator = AwaClientGetResponse_NewPathIterator(response);
    while (AwaPathIterator_Next(iterator))
    {
        const char * path = AwaPathIterator_Get(iterator);
        const AwaPathResult * result = AwaClientGetResponse_GetPathResult(response, path);
        AwaError error = AwaPathResult_GetError(result);
        if (error != AwaError_Success)
        {
            Error("Failed to retrieve %s: %s\n", path, AwaError_ToString(error));
        }
        else
        {
            Client_AddPathToCString(&cstring, path, session, (void *)response, ResponseType_GetResponse, quiet, &lastObjectID, &lastObjectInstanceID);
        }
    }
    AwaPathIterator_Free(&iterator);
    return cstring;
}
int main(int argc, char ** argv)
{
    int result = 0;
    AwaServerObserveOperation * operation;
    struct gengetopt_args_info ai;
    AwaServerSession * session = NULL;
    Target ** targets = NULL;

    // Catch CTRL-C to ensure clean-up
    signal(SIGINT, INThandler);

    if (cmdline_parser(argc, argv, &ai) != 0)
    {
        exit(1);
    }

    g_logLevel = ai.debug_given ? 2 : (ai.verbose_given ? 1 : 0);
    AwaLog_SetLevel(ai.debug_given ? AwaLogLevel_Debug : (ai.verbose_given ? AwaLogLevel_Verbose : AwaLogLevel_Warning));

    if (ai.inputs_num == 0)
    {
        Error("Specify one or more resource paths.\n");
        result = 1;
        goto cleanup;
    }

    session = Server_EstablishSession(ai.ipcAddress_arg, ai.ipcPort_arg);
    if (session != NULL)
    {
        operation = AwaServerObserveOperation_New(session);
        if (operation == NULL)
        {
            Error("Failed to create observe operation\n");
            exit(1);
        }

        targets = malloc(ai.inputs_num * sizeof(Target *));

        ObserveContext observeContext;
        observeContext.targets = targets;
        observeContext.numTargets = ai.inputs_num;
        observeContext.quiet = ai.quiet_given; // pass the quiet parameter as our context
        int count = 0;
        int i = 0;
        for (i = 0; i < ai.inputs_num; ++i)
        {
            targets[i] = CreateTarget(ai.inputs[i]);
            if (targets[i] != NULL)
            {
                count = ObserveTarget(operation, ai.clientID_arg, targets[i], &observeContext) ? count + 1 : count;
            }
        }

        if (AwaServerObserveOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT) != AwaError_Success)
        {
            Error("Failed to perform observe operation\n");
            goto cleanup;
        }

        int validCount = count;
        const AwaServerObserveResponse * response = AwaServerObserveOperation_GetResponse(operation, ai.clientID_arg);
        ObservationNode * currentObservation = g_observationListHead;
        ObservationNode * nextObservation;
        while (currentObservation != NULL)
        {
            nextObservation = currentObservation->next;
            const AwaPathResult * pathResult = NULL;

            AwaServerObservation * observation = (AwaServerObservation *)(currentObservation->observation);
            const char * path = AwaServerObservation_GetPath(observation);

            pathResult = AwaServerObserveResponse_GetPathResult(response, path);
            if (AwaPathResult_GetError(pathResult) != AwaError_Success)
            {
                Error("Failed to observe to %s: %s\n", path, AwaError_ToString(AwaPathResult_GetError(pathResult)));
                validCount--;
            }
            currentObservation = nextObservation;
        }

        AwaServerObserveOperation_Free(&operation);

        // Wait if there's something to wait for
        Debug("count %d\n", count);
        if (count > 0)
        {
            if (validCount > 0)
            {
                Wait(session, ai.waitTime_arg, ai.waitCount_arg);
            }
            CancelObservationFromTargets(session, ai.clientID_arg);
        }
    }
    else
    {
        Error("Failed to establish Awa Session\n");
        result = 1;
    }
cleanup:
    if (session)
    {
        FreeObservationList();
        Server_ReleaseSession(&session);
    }
    if (targets)
    {
        int i;
        for (i = 0; i < ai.inputs_num; ++i)
        {
            if (targets[i] != NULL)
            {
                FreeTarget(&targets[i]);
            }
        }
        free(targets);
    }
    cmdline_parser_free(&ai);
    return result;
}
static void CancelObservationFromTargets(AwaServerSession * session, const char * clientID)
{
    AwaServerObserveOperation * operation = AwaServerObserveOperation_New(session);
    if (operation == NULL)
    {
        Error("Failed to create observe operation\n");
        exit(1);
    }

    // Add cancels for each observation
    ObservationNode * currentObservation = g_observationListHead;
    ObservationNode * nextObservation;
    while(currentObservation != NULL)
    {
        nextObservation = currentObservation->next;

        if (AwaServerObserveOperation_AddCancelObservation(operation, currentObservation->observation) != AwaError_Success)
        {
            Error("AwaServerObserveOperation_AddCancelChangeObservation() failed for path %s\n", AwaServerObservation_GetPath(currentObservation->observation));
        }
        currentObservation = nextObservation;
    }

    if (AwaServerObserveOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT) != AwaError_Success)
    {
        Error("Failed to perform observe operation\n");
    }

    // Check response for each observation
    const AwaServerObserveResponse * response = AwaServerObserveOperation_GetResponse(operation, clientID);
    currentObservation = g_observationListHead;
    while(currentObservation != NULL)
    {
        nextObservation = currentObservation->next;
        const AwaPathResult * result = NULL;

        AwaServerObservation * observation = (AwaServerObservation *)(currentObservation->observation);
        const char * path = AwaServerObservation_GetPath(observation);

        result = AwaServerObserveResponse_GetPathResult(response, path);
        if (AwaPathResult_GetError(result) != AwaError_Success)
        {
            Error("Failed to cancel observation to %s: %s\n", path, AwaError_ToString(AwaPathResult_GetError(result)));
        }

        currentObservation = nextObservation;
    }
    AwaServerObserveOperation_Free(&operation);

    // Finally free each observation
    currentObservation = g_observationListHead;
    while(currentObservation != NULL)
    {
        nextObservation = currentObservation->next;

        AwaServerObservation * observation = (AwaServerObservation *)(currentObservation->observation);
        if (AwaServerObservation_Free(&observation) != AwaError_Success)
        {
            Error("AwaServerObservation_Free() failed for path %s\n", AwaServerObservation_GetPath(observation));
        }
        currentObservation = nextObservation;
    }
}
int main(int argc, char ** argv)
{
    int result = 0;
    AwaClientSubscribeOperation * operation;
    struct gengetopt_args_info ai;
    AwaClientSession * session = NULL;

    // Catch CTRL-C to ensure clean-up
    signal(SIGINT, INThandler);

    if (cmdline_parser(argc, argv, &ai) != 0)
    {
        exit(1);
    }

    g_logLevel = ai.debug_given ? 2 : (ai.verbose_given ? 1 : 0);
    AwaLog_SetLevel(ai.debug_given ? AwaLogLevel_Debug : (ai.verbose_given ? AwaLogLevel_Verbose : AwaLogLevel_Warning));

    if (ai.inputs_num == 0)
    {
        Error("Specify one or more resource paths.\n");
        result = 1;
        goto cleanup;
    }

    session = Client_EstablishSession(ai.ipcAddress_arg, ai.ipcPort_arg);
    if (session != NULL)
    {
        operation = AwaClientSubscribeOperation_New(session);
        if (operation == NULL)
        {
            Error("Failed to create subscribe operation\n");
            exit(1);
        }

        void * context = &ai.quiet_given; // pass the quiet parameter as our context

        int count = 0;
        int i = 0;
        for (i = 0; i < ai.inputs_num; ++i)
        {
            Target * target = CreateTarget(ai.inputs[i]);
            if (target != NULL)
            {
                count = SubscribeToTarget(session, operation, target, context) ? count + 1 : count;
                FreeTarget(&target);
            }
        }

        if (AwaClientSubscribeOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT) != AwaError_Success)
        {
            Error("Failed to perform subscribe operation\n");
            goto cleanup;
        }

        int validCount = count;
        const AwaClientSubscribeResponse * response = AwaClientSubscribeOperation_GetResponse(operation);
        SubscriptionNode * currentSubscription = g_subscriptionListHead;
        SubscriptionNode * nextSubscription;
        while (currentSubscription != NULL)
        {
            nextSubscription = currentSubscription->next;

            const AwaPathResult * pathResult = NULL;
            const char * path = NULL;

            switch (currentSubscription->type)
            {
                case AwaSubscribeType_Change:
                {
                    AwaClientChangeSubscription * subscription = (AwaClientChangeSubscription *)(currentSubscription->subscription);
                    path = AwaClientChangeSubscription_GetPath(subscription);
                    break;
                }
                case AwaSubscribeType_Execute:
                {
                    AwaClientExecuteSubscription * subscription = (AwaClientExecuteSubscription *)(currentSubscription->subscription);
                    path = AwaClientExecuteSubscription_GetPath(subscription);
                    break;
                }
                default:
                    Error("Unsupported subscribe type: %d for path %s\n", currentSubscription->type, path);
                    break;
            }

            pathResult = AwaClientSubscribeResponse_GetPathResult(response, path);
            if (AwaPathResult_GetError(pathResult) != AwaError_Success)
            {
                Error("Failed to subscribe to %s: %s\n", path, AwaError_ToString(AwaPathResult_GetError(pathResult)));
                validCount--;
            }

            currentSubscription = nextSubscription;
        }

        AwaClientSubscribeOperation_Free(&operation);

        Debug("count %d\n", count);
        // Wait if there's something to wait for
        if (count > 0)
        {
            if (validCount > 0)
            {
                Wait(session, ai.waitTime_arg, ai.waitCount_arg);
            }
            UnsubscribeFromTargets(session);
        }
    }
    else
    {
        Error("Failed to establish Awa Session\n");
        result = 1;
    }
cleanup:
    if (session)
    {
        FreeSubscriptionList();
        Client_ReleaseSession(&session);
    }
    cmdline_parser_free(&ai);
    return result;
}
static void UnsubscribeFromTargets(AwaClientSession * session)
{ 
    AwaClientSubscribeOperation * operation = AwaClientSubscribeOperation_New(session);
    if (operation == NULL)
    {
        Error("Failed to create subscribe operation\n");
        exit(1);
    }

    // Add cancels for each subscription
    SubscriptionNode * currentSubscription = g_subscriptionListHead;
    SubscriptionNode * nextSubscription;
    while(currentSubscription != NULL)
    {
        nextSubscription = currentSubscription->next;

        switch (currentSubscription->type)
        {
            case AwaSubscribeType_Change:
                if (AwaClientSubscribeOperation_AddCancelChangeSubscription(operation, currentSubscription->subscription) != AwaError_Success)
                {
                    Error("AwaClientSubscribeOperation_AddCancelChangeSubscription() failed for path %s\n", AwaClientChangeSubscription_GetPath(currentSubscription->subscription));
                }
                break;
            case AwaSubscribeType_Execute:
                if (AwaClientSubscribeOperation_AddCancelExecuteSubscription(operation, currentSubscription->subscription) != AwaError_Success)
                {
                    Error("AwaClientSubscribeOperation_AddCancelExecuteSubscription() failed for path %s\n", AwaClientExecuteSubscription_GetPath(currentSubscription->subscription));
                }
                break;
            default:
                Error("Unsupported subscribe type: %d\n", currentSubscription->type);
                break;
        }
        currentSubscription = nextSubscription;
    }

    if (AwaClientSubscribeOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT) != AwaError_Success)
    {
        Error("Failed to perform subscribe operation\n");
    }

    // Check response for each subscription
    const AwaClientSubscribeResponse * response = AwaClientSubscribeOperation_GetResponse(operation);
    currentSubscription = g_subscriptionListHead;
    while (currentSubscription != NULL)
    {
        nextSubscription = currentSubscription->next;

        const AwaPathResult * result = NULL;
        const char * path = NULL;

        switch (currentSubscription->type)
        {
            case AwaSubscribeType_Change:
            {
                AwaClientChangeSubscription * subscription = (AwaClientChangeSubscription *)(currentSubscription->subscription);
                path = AwaClientChangeSubscription_GetPath(subscription);
                break;
            }
            case AwaSubscribeType_Execute:
            {
                AwaClientExecuteSubscription * subscription = (AwaClientExecuteSubscription *)(currentSubscription->subscription);
                path = AwaClientExecuteSubscription_GetPath(subscription);
                break;
            }
            default:
                Error("Unsupported subscribe type: %d for path %s\n", currentSubscription->type, path);
                break;
        }

        result = AwaClientSubscribeResponse_GetPathResult(response, path);
        if (AwaPathResult_GetError(result) != AwaError_Success)
        {
            Error("Failed to cancel subscription to %s: %s\n", path, AwaError_ToString(AwaPathResult_GetError(result)));
        }

        currentSubscription = nextSubscription;
    }
    AwaClientSubscribeOperation_Free(&operation);

    // Finally free each subscription
    currentSubscription = g_subscriptionListHead;
    while(currentSubscription != NULL)
    {
        nextSubscription = currentSubscription->next;

        switch (currentSubscription->type)
        {
            case AwaSubscribeType_Change:
            {
                AwaClientChangeSubscription * subscription = (AwaClientChangeSubscription *)(currentSubscription->subscription);
                if (AwaClientChangeSubscription_Free(&subscription) != AwaError_Success)
                {
                    Error("AwaClientChangeSubscription_Free() failed for path %s\n", AwaClientChangeSubscription_GetPath(subscription));
                }
                break;
            }
            case AwaSubscribeType_Execute:
            {
                AwaClientExecuteSubscription * subscription = (AwaClientExecuteSubscription *)(currentSubscription->subscription);
                if (AwaClientExecuteSubscription_Free(&subscription) != AwaError_Success)
                {
                    Error("AwaClientExecuteSubscription_Free() failed\n for path %s\n", AwaClientExecuteSubscription_GetPath(subscription));
                }
                break;
            }
            default:
                Error("Unsupported subscribe type: %d\n", currentSubscription->type);
                break;
        }
        currentSubscription = nextSubscription;
    }
}