int main(int argc, char** argv) {

	IedServer iedServer = IedServer_create(&staticIedModel);

	/* This will create default values for the complete IEC model and installs them in the
	 * MMS server's value cache.
	 */
	IedServer_setAllModelDefaultValues(iedServer);

	/* Get the MMS domain for an IEC 61850 Logical Device */
	MmsDomain* sampleDevice = IedServer_getDomain(iedServer, "SampleIEDDevice1");

	/* Get the reference to the structured value object */
	MmsValue* totW = IedServer_getValue(iedServer, sampleDevice, "MMXU2$MX$TotW");

	/* get references to the relevant elements of the copy */
	MmsValue* totW_mag_f = MmsValue_getStructElementByIndex(MmsValue_getStructElementByIndex(totW, 0), 0);
	MmsValue* totW_t = MmsValue_getStructElementByIndex(totW, 2);

	/* MMS server will be instructed to start listening to client connections. */
	IedServer_start(iedServer);
	running = 1;

	signal(SIGINT, sigint_handler);

	float totW_value = 0.f;

	while (running) {

		/* Lock the data model - access from MMS Clients will be blocked */
		IedServer_lockDataModel(iedServer);

		/* Update measurement values */
		MmsValue_setFloat(totW_mag_f, totW_value);
		MmsValue_setUtcTime(totW_t, time(NULL));

		/* Unlock the data model - access from MMS clients will be processed */
		IedServer_unlockDataModel(iedServer);

		totW_value += 0.1f;
		Thread_sleep(1000);
	}

	/* stop MMS server - close TCP server socket and all client sockets */
	IedServer_stop(iedServer);

	/* Cleanup - free all resources */
	IedServer_destroy(iedServer);
} /* main() */
Esempio n. 2
0
static void
executeControlTask(ControlObject* self)
{
    int state;

executeStateMachine:

    state = getState(self);

    switch (state) {

    case STATE_WAIT_FOR_ACTIVATION_TIME:
    case STATE_WAIT_FOR_EXECUTION:
    {
        ControlHandlerResult dynamicCheckResult = CONTROL_RESULT_OK;
        bool isTimeActivatedControl = false;

        if (state == STATE_WAIT_FOR_ACTIVATION_TIME)
           isTimeActivatedControl = true;

        if (self->waitForExecutionHandler != NULL) {
            dynamicCheckResult = self->waitForExecutionHandler(self->waitForExecutionHandlerParameter, self->ctlVal,
                    self->testMode, self->synchroCheck);
        }

        if (dynamicCheckResult == CONTROL_RESULT_FAILED) {
            if (isTimeActivatedControl) {
                ControlObject_sendLastApplError(self, self->mmsConnection, "Oper",
                        CONTROL_ERROR_NO_ERROR, ADD_CAUSE_BLOCKED_BY_SYNCHROCHECK,
                        self->ctlNum, self->origin, false);
            }
            else
                MmsServerConnection_sendWriteResponse(self->mmsConnection, self->operateInvokeId,
                        DATA_ACCESS_ERROR_OBJECT_ACCESS_DENIED, true);

            abortControlOperation(self);
            exitControlTask(self);
        }
        else if (dynamicCheckResult == CONTROL_RESULT_OK) {
            if (isTimeActivatedControl) {
                ControlObject_sendCommandTerminationPositive(self);

                MmsValue* operTm = getOperParameterOperTime(self->oper);

                MmsValue_setUtcTime(operTm, 0);

            }
            else
                MmsServerConnection_sendWriteResponse(self->mmsConnection, self->operateInvokeId,
                        DATA_ACCESS_ERROR_SUCCESS, true);

            setState(self, STATE_OPERATE);

            goto executeStateMachine;
        }
    }
    break;

    case STATE_OPERATE:
    {
        uint64_t currentTime = Hal_getTimeInMs();

        ControlHandlerResult result = operateControl(self, self->ctlVal, currentTime, self->testMode);

        if (result != CONTROL_RESULT_WAITING) {

            if (result == CONTROL_RESULT_OK) {

                if ((self->ctlModel == 4) || (self->ctlModel == 3)) {
                    ControlObject_sendCommandTerminationPositive(self);
                }
            }
            else {

                if ((self->ctlModel == 4) || (self->ctlModel == 3)) {
                    if (DEBUG_IED_SERVER)
                        printf("IED_SERVER: operate failed!\n");

                    ControlObject_sendCommandTerminationNegative(self);
                }
            }

            abortControlOperation(self);
            exitControlTask(self);
        }
    }
    break;

    }
}