static void
handleReadListOfVariablesRequest(
		MmsServerConnection* connection,
		ReadRequest_t* read,
		uint32_t invokeId,
		ByteBuffer* response)
{
	int variableCount = read->variableAccessSpecification.choice.listOfVariable.list.count;

	LinkedList /*<MmsValue>*/ values = LinkedList_create();

	if (isSpecWithResult(read)) { /* add specification to result */
		//TODO add va spec to result
	}

	MmsServer_lockModel(connection->server);

	int i;

	for (i = 0; i < variableCount; i++) {
		VariableSpecification_t varSpec =
			read->variableAccessSpecification.choice.listOfVariable.list.array[i]->variableSpecification;

		AlternateAccess_t* alternateAccess =
			read->variableAccessSpecification.choice.listOfVariable.list.array[i]->alternateAccess;

		if (varSpec.present == VariableSpecification_PR_name) {

			if (varSpec.choice.name.present == ObjectName_PR_domainspecific) {
				char* domainIdStr = mmsMsg_createStringFromAsnIdentifier(
						varSpec.choice.name.choice.domainspecific.domainId);


				char* nameIdStr = mmsMsg_createStringFromAsnIdentifier(
						varSpec.choice.name.choice.domainspecific.itemId);

				MmsDomain* domain = MmsDevice_getDomain(MmsServer_getDevice(connection->server), domainIdStr);

				if (domain == NULL) {
					if (DEBUG) printf("MMS read: domain %s not found!\n", domainIdStr);

					appendErrorToResultList(values, 10 /* object-non-existant*/);
					break;
				}

				MmsTypeSpecification* namedVariable = MmsDomain_getNamedVariable(domain, nameIdStr);

				addNamedVariableToResultList(namedVariable, domain, nameIdStr,
						values, connection, alternateAccess);

				free(domainIdStr);
				free(nameIdStr);
			}
			else {
                appendErrorToResultList(values, 10 /* object-non-existant*/);

				if (DEBUG) printf("MMS read: object name type not supported!\n");
			}
		}
		else {
			//TODO should we send a ConfirmedErrorPdu here?
			if (DEBUG) printf("MMS read: varspec type not supported!\n");
		}
	}

	encodeReadResponse(connection, invokeId, response, values, NULL);

	MmsServer_unlockModel(connection->server);

exit:
    deleteValueList(values);
}
static void
handleReadNamedVariableListRequest(
		MmsServerConnection* connection,
		ReadRequest_t* read,
		int invokeId,
		ByteBuffer* response)
{
	if (read->variableAccessSpecification.choice.variableListName.present ==
			ObjectName_PR_domainspecific)
	{
		char* domainId = mmsMsg_createStringFromAsnIdentifier(
				read->variableAccessSpecification.choice.variableListName.choice.domainspecific.domainId);

		char* listName = mmsMsg_createStringFromAsnIdentifier(
				read->variableAccessSpecification.choice.variableListName.choice.domainspecific.itemId);

		VarAccessSpec accessSpec;

		accessSpec.isNamedVariableList = true;
		accessSpec.specific = 1;
		accessSpec.domainId = domainId;
		accessSpec.itemId = listName;

		MmsDomain* domain = MmsDevice_getDomain(MmsServer_getDevice(connection->server), domainId);

		if (domain == NULL) {
			if (DEBUG) printf("MMS read: domain %s not found!\n", domainId);
			mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_NON_EXISTENT);
		}
		else {
			MmsNamedVariableList namedList = MmsDomain_getNamedVariableList(domain, listName);

			if (namedList != NULL) {
				createNamedVariableListResponse(connection, namedList, invokeId, response, read,
						&accessSpec);
			}
			else {
				if (DEBUG) printf("MMS read: named variable list %s not found!\n", listName);
				mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_NON_EXISTENT);
			}
		}

		free(domainId);
		free(listName);
	}
	else if (read->variableAccessSpecification.choice.variableListName.present ==
				ObjectName_PR_aaspecific)
	{
		char* listName = mmsMsg_createStringFromAsnIdentifier(read->variableAccessSpecification.
				choice.variableListName.choice.aaspecific);

		MmsNamedVariableList namedList = MmsServerConnection_getNamedVariableList(connection, listName);

		VarAccessSpec accessSpec;

		accessSpec.isNamedVariableList = true;
		accessSpec.specific = 2;
		accessSpec.domainId = NULL;
		accessSpec.itemId = listName;

		if (namedList == NULL)
			mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_NON_EXISTENT);
		else {
			createNamedVariableListResponse(connection, namedList, invokeId, response, read, &accessSpec);
		}

		free(listName);
	}
	else
		mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_ACCESS_UNSUPPORTED);
}
void
mmsServer_handleDeleteNamedVariableListRequest(MmsServerConnection* connection,
        DeleteNamedVariableListRequest_t* request,
        int invokeId,
        ByteBuffer* response)
{
    //TODO implement for association specific named variable lists

    MmsDevice* device;
    long scopeOfDelete;
    request->scopeOfDelete;

    asn_INTEGER2long(request->scopeOfDelete, &scopeOfDelete);

    device = MmsServer_getDevice(connection->server);

    if (scopeOfDelete == DeleteNamedVariableListRequest__scopeOfDelete_specific) {
        int numberMatched = 0;
        int numberDeleted = 0;

        int numberItems = request->listOfVariableListName->list.count;

        int i;

        for (i = 0; i < numberItems; i++) {
            if (request->listOfVariableListName->list.array[i]->present == ObjectName_PR_domainspecific) {
                char* domainId = mmsMsg_createStringFromAsnIdentifier(
                                     request->listOfVariableListName->list.array[i]->choice.domainspecific.domainId);

                MmsDomain* domain = MmsDevice_getDomain(device, domainId);

                char* itemId = mmsMsg_createStringFromAsnIdentifier(
                                   request->listOfVariableListName->list.array[i]->choice.domainspecific.itemId);

                MmsNamedVariableList variableList = MmsDomain_getNamedVariableList(domain, itemId);

                if (variableList != NULL) {
                    numberMatched++;

                    if (MmsNamedVariableList_isDeletable(variableList)) {
                        MmsDomain_deleteNamedVariableList(domain, itemId);
                        numberDeleted++;
                    }
                }

                free(domainId);
                free(itemId);
            }
            else if (request->listOfVariableListName->list.array[i]->present == ObjectName_PR_aaspecific) {
                char* itemId = mmsMsg_createStringFromAsnIdentifier(
                                   request->listOfVariableListName->list.array[i]->choice.aaspecific);

                MmsNamedVariableList variableList = MmsServerConnection_getNamedVariableList(connection, itemId);

                if (variableList != NULL) {
                    numberMatched++;
                    numberDeleted++;

                    MmsServerConnection_deleteNamedVariableList(connection, itemId);
                }

                free(itemId);
            }
            //TODO else send error???
        }

        createDeleteNamedVariableListResponse(invokeId, response, numberMatched, numberDeleted);
    }
    else {
        mmsServer_createConfirmedErrorPdu(invokeId, response, MMS_ERROR_TYPE_OBJECT_ACCESS_UNSUPPORTED);
    }
}