void
mmsServer_handleDefineNamedVariableListRequest(
    MmsServerConnection* connection,
    DefineNamedVariableListRequest_t* request,
    int invokeId,
    ByteBuffer* response)
{
    MmsDevice* device = MmsServer_getDevice(connection->server);

    if (request->variableListName.present == ObjectName_PR_domainspecific) {

        MmsNamedVariableList namedVariableList;
        char* variableListName;
        char* domainName = createStringFromBuffer(
                               request->variableListName.choice.domainspecific.domainId.buf,
                               request->variableListName.choice.domainspecific.domainId.size);

        MmsDomain* domain = MmsDevice_getDomain(device, domainName);

        free(domainName);

        if (domain == NULL) {
            mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_NON_EXISTENT);
            return;
        }

        variableListName = createStringFromBuffer(
                               request->variableListName.choice.domainspecific.itemId.buf,
                               request->variableListName.choice.domainspecific.itemId.size);

        namedVariableList = createNamedVariableList(device,
                            request, variableListName);

        free(variableListName);

        if (namedVariableList != NULL) {
            MmsDomain_addNamedVariableList(domain, namedVariableList);
            createDefineNamedVariableListResponse(invokeId, response);
        }
        else
            mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_ACCESS_UNSUPPORTED);

    }
    else if (request->variableListName.present == ObjectName_PR_aaspecific) {

        char* variableListName = createStringFromBuffer(
                                     request->variableListName.choice.aaspecific.buf,
                                     request->variableListName.choice.aaspecific.size);

        MmsNamedVariableList namedVariableList = createNamedVariableList(device,
                request, variableListName);

        if (namedVariableList != NULL) {
            MmsServerConnection_addNamedVariableList(connection, namedVariableList);
            createDefineNamedVariableListResponse(invokeId, response);
        }
        else
            mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_ACCESS_UNSUPPORTED);

        free(variableListName);
    }
    else
        mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_ACCESS_UNSUPPORTED);
}
void
mmsServer_handleDefineNamedVariableListRequest(
		MmsServerConnection connection,
		uint8_t* buffer, int bufPos, int maxBufPos,
		uint32_t invokeId,
		ByteBuffer* response)
{
	DefineNamedVariableListRequest_t* request = 0;

	MmsPdu_t* mmsPdu = 0;

	asn_dec_rval_t rval = ber_decode(NULL, &asn_DEF_MmsPdu, (void**) &mmsPdu, buffer, maxBufPos);

	if (rval.code != RC_OK) {
	    mmsServer_writeMmsRejectPdu(&invokeId, MMS_ERROR_REJECT_INVALID_PDU, response);
	    goto exit_function;
	}

	request = &(mmsPdu->choice.confirmedRequestPdu.confirmedServiceRequest.choice.defineNamedVariableList);

	MmsDevice* device = MmsServer_getDevice(connection->server);

	if (request->variableListName.present == ObjectName_PR_domainspecific) {

	    char domainName[65];

	    if (request->variableListName.choice.domainspecific.domainId.size > 64) {
	        mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_ACCESS_OBJECT_NON_EXISTENT);
	        goto exit_free_struct;
	    }

	    StringUtils_createStringFromBufferInBuffer(domainName,
	            request->variableListName.choice.domainspecific.domainId.buf,
	            request->variableListName.choice.domainspecific.domainId.size);

		MmsDomain* domain = MmsDevice_getDomain(device, domainName);

		if (domain == NULL) {
			mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_ACCESS_OBJECT_NON_EXISTENT);
			goto exit_free_struct;
		}

		if (LinkedList_size(domain->namedVariableLists) < CONFIG_MMS_MAX_NUMBER_OF_DOMAIN_SPECIFIC_DATA_SETS) {
		    char variableListName[65];

		    if (request->variableListName.choice.domainspecific.itemId.size > 64) {
		        mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_ACCESS_OBJECT_NON_EXISTENT);
                goto exit_free_struct;
		    }

		    StringUtils_createStringFromBufferInBuffer(variableListName,
		            request->variableListName.choice.domainspecific.itemId.buf,
                    request->variableListName.choice.domainspecific.itemId.size);

            if (MmsDomain_getNamedVariableList(domain, variableListName) != NULL) {
                mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_DEFINITION_OBJECT_EXISTS);
            }
            else {
                MmsError mmsError;

                MmsNamedVariableList namedVariableList = createNamedVariableList(domain, device,
                                request, variableListName, &mmsError);

                if (namedVariableList != NULL) {

                    mmsError = mmsServer_callVariableListChangedHandler(true, MMS_DOMAIN_SPECIFIC, domain, variableListName, connection);

                    if (mmsError == MMS_ERROR_NONE) {
                        MmsDomain_addNamedVariableList(domain, namedVariableList);
                        createDefineNamedVariableListResponse(invokeId, response);
                    }
                    else {
                        MmsNamedVariableList_destroy(namedVariableList);
                        mmsServer_createConfirmedErrorPdu(invokeId, response, mmsError);
                    }
                }
                else
                    mmsServer_createConfirmedErrorPdu(invokeId, response, mmsError);
            }
		}
		else
		    mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_RESOURCE_CAPABILITY_UNAVAILABLE);


	}
	else if (request->variableListName.present == ObjectName_PR_aaspecific) {

	    if (LinkedList_size(connection->namedVariableLists) < CONFIG_MMS_MAX_NUMBER_OF_ASSOCIATION_SPECIFIC_DATA_SETS) {

	        char variableListName[65];

	        if (request->variableListName.choice.aaspecific.size > 64) {
                mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_ACCESS_OBJECT_NON_EXISTENT);
                goto exit_free_struct;
            }

	        StringUtils_createStringFromBufferInBuffer(variableListName,
	                request->variableListName.choice.aaspecific.buf,
	                request->variableListName.choice.aaspecific.size);

            if (MmsServerConnection_getNamedVariableList(connection, variableListName) != NULL) {
                mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_DEFINITION_OBJECT_EXISTS);
            }
            else {
                MmsError mmsError;

                MmsNamedVariableList namedVariableList = createNamedVariableList(NULL, device,
                        request, variableListName, &mmsError);

                if (namedVariableList != NULL) {

                    if (mmsServer_callVariableListChangedHandler(true, MMS_ASSOCIATION_SPECIFIC, NULL, variableListName, connection) == MMS_ERROR_NONE) {
                        MmsServerConnection_addNamedVariableList(connection, namedVariableList);
                        createDefineNamedVariableListResponse(invokeId, response);
                    }
                    else {
                        MmsNamedVariableList_destroy(namedVariableList);
                        mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_ACCESS_OBJECT_ACCESS_DENIED);
                    }

                }
                else
                    mmsServer_createConfirmedErrorPdu(invokeId, response, mmsError);
            }
	    }
	    else
	        mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_RESOURCE_CAPABILITY_UNAVAILABLE);
	}
	else
		mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_DEFINITION_TYPE_UNSUPPORTED);

exit_free_struct:
	asn_DEF_MmsPdu.free_struct(&asn_DEF_MmsPdu, mmsPdu, 0);

exit_function:
    return;
}