/**
********************************************************************************
\brief  Destructor

Destructs a POWERLINK object.
*******************************************************************************/
Api::~Api()
{
    tOplkError          ret;

    ret = oplk_execNmtCommand(kNmtEventSwitchOff);
    pProcessThread->waitForNmtStateOff();
    ret = oplk_freeProcessImage();
    ret = oplk_shutdown();
}
Example #2
0
/**
********************************************************************************
\brief  Destructor

Destructs a POWERLINK object.
*******************************************************************************/
Api::~Api()
{
    tOplkError  ret;

    pDataInOutThread->stop();
    pDataInOutThread->wait(100);            // wait until thread terminates (max 100ms)

    ret = oplk_execNmtCommand(kNmtEventSwitchOff);
    pProcessThread->waitForNmtStateOff();

    ret = oplk_freeProcessImage();
    ret = oplk_destroy();
    oplk_exit();
}
//------------------------------------------------------------------------------
tOplkError oplk_allocProcessImage(UINT sizeProcessImageIn_p, UINT sizeProcessImageOut_p)
{
    tOplkError      ret = kErrorOk;

    TRACE("%s(): Alloc(%u, %u)\n", __func__, sizeProcessImageIn_p,
                                   sizeProcessImageOut_p);

    if (!ctrlu_stackIsInitialized())
        return kErrorApiNotInitialized;

    if ((instance_l.inputImage.pImage != NULL) || (instance_l.outputImage.pImage != NULL))
    {
        return kErrorApiPIAlreadyAllocated;
    }

    if (sizeProcessImageIn_p != 0)
    {
        instance_l.inputImage.pImage = OPLK_MALLOC(sizeProcessImageIn_p);
        if (instance_l.inputImage.pImage == NULL)
        {
            return kErrorApiPIOutOfMemory;
        }
        instance_l.inputImage.imageSize = sizeProcessImageIn_p;
        OPLK_MEMSET(instance_l.inputImage.pImage, 0x00, sizeProcessImageIn_p);
    }

    if (sizeProcessImageOut_p != 0)
    {
        instance_l.outputImage.pImage = OPLK_MALLOC(sizeProcessImageOut_p);
        if (instance_l.outputImage.pImage == NULL)
        {
            // Output image allocation failed, therefore we free input image to be consistent
            oplk_freeProcessImage();
            return kErrorApiPIOutOfMemory;
        }
        instance_l.outputImage.imageSize = sizeProcessImageOut_p;
        OPLK_MEMSET(instance_l.outputImage.pImage, 0x00, sizeProcessImageOut_p);
    }

    TRACE("%s: Alloc(%p, %u, %p, %u)\n", __func__,
          instance_l.inputImage.pImage,  instance_l.inputImage.imageSize,
          instance_l.outputImage.pImage, instance_l.outputImage.imageSize);

    return ret;
}
//------------------------------------------------------------------------------
void shutdownApp(void)
{
    oplk_freeProcessImage();
}
//------------------------------------------------------------------------------
tOplkError ProcessThread::processStateChangeEvent(tOplkApiEventType eventType_p,
                                                  tOplkApiEventArg* pEventArg_p,
                                                  void* pUserArg_p)
{
    tOplkError                  ret = kErrorOk;
    tEventNmtStateChange*       pNmtStateChange = &pEventArg_p->nmtStateChange;
#if !defined(CONFIG_INCLUDE_CFM)
    UINT                        varLen;
#endif
    QString                     str;

    UNUSED_PARAMETER(eventType_p);
    UNUSED_PARAMETER(pUserArg_p);

    sigNmtState(pNmtStateChange->newNmtState);

    sigPrintLog(QString("StateChangeEvent %1: %2 -> %3")
                        .arg(debugstr_getNmtEventStr(pNmtStateChange->nmtEvent))
                        .arg(debugstr_getNmtStateStr(pNmtStateChange->oldNmtState))
                        .arg(debugstr_getNmtStateStr(pNmtStateChange->newNmtState)));

    switch (pNmtStateChange->newNmtState)
    {
        case kNmtGsOff:
            pProcessThread_g->sigOplkStatus(0);

            // NMT state machine was shut down,
            // because of user signal (CTRL-C) or critical POWERLINK stack error
            // -> also shut down oplk_process()
            ret = kErrorShutdown;
            // and unblock DataInDataOutThread
            oplk_freeProcessImage(); //jba do we need it here?

            reachedNmtStateOff();
            break;

        case kNmtGsResetCommunication:
#if !defined(CONFIG_INCLUDE_CFM)
            ret = setDefaultNodeAssignment();
#endif
            pProcessThread_g->sigOplkStatus(1);
            break;

        case kNmtGsResetConfiguration:
#if !defined(CONFIG_INCLUDE_CFM)
        // Configuration Manager is not available,
        // so fetch object 0x1006 NMT_CycleLen_U32 from local OD
        // (in little endian byte order)
        // for configuration of remote CN
            varLen = sizeof(UINT32);
            ret = oplk_readObject(NULL, 0, 0x1006, 0x00, &cycleLen_g,
                                  &varLen, kSdoTypeAsnd, NULL);
            if (ret != kErrorOk)
            {
                sigPrintLog(QString("  oplk_readObject() failed with 0x%1\n\"2\"")
                                    .arg(ret)
                                    .arg(debugstr_getRetValStr(ret)));
                break;
            }
#endif
            sigOplkStatus(1);
            break;

        case kNmtCsNotActive:
        case kNmtMsNotActive:
        case kNmtRmsNotActive:
        case kNmtGsInitialising:
        case kNmtGsResetApplication:
        case kNmtCsPreOperational1:
        case kNmtMsPreOperational1:
        case kNmtCsPreOperational2:
        case kNmtMsPreOperational2:
        case kNmtCsReadyToOperate:
        case kNmtMsReadyToOperate:
        case kNmtCsBasicEthernet:
        case kNmtMsBasicEthernet:
            sigOplkStatus(1);
            break;

        case kNmtCsOperational:
        case kNmtMsOperational:
            sigOplkStatus(2);
            break;


        default:
            sigOplkStatus(-1);
            break;
    }

    return ret;
}