// virtual.inc - support for virtual adapters

//	********************************************************************************
//	Local Shells
//	********************************************************************************

//
//	Set adapter parameters.
//
//		$0 = Adapter parameters key
//		$1 = Data Link Control Identifier for this adapter
//		$2 = List of all DLCI's
//		$3 = List of PVC keys
//		$4 = Net card key.
//
//		$R0 = Boolean indicating success
//		$R1 = Boolean indicating binding review needed
//
[SetParams]

	set AdapterParametersKey = $($0)
	set DLCI = $($1)
	set DLCIList = $($2)
	set KeyPVCList = $($3)
	set KeyNetCard = $($4)

	_Enter("SetParams", _Show(AdapterParametersKey)_Show(DLCI)_Show(DLCIList)_Show(KeyPVCList)_Show(KeyNetCard))

	read-syms GlobalConstants

	set Success = FALSE
	set NeedBindingReview = FALSE

	ForListDo $(DLCIList)
		ifint $(DLCI) == $($)
			set ItemNumber = $(#)
		endif
	EndForListDo

	set KeyPVC = *($(KeyPVCList), $(ItemNumber))

	_GetRegSzByKey $(KeyPVC) "Desc"
   _Assert($($R0))
	set Desc = $($R1)
	_SetRegSzByKey $(KeyNetCard) "Title" $(Desc)
   _Assert($($R0))

	set ValueNameList = { "TCPIP", "IPX", "Ethernet" }
	ForListDo $(ValueNameList)
		set ValueName = $($)

		_LocalShell GetRegValueByKey $(TYPECHECK_NONE) $(KeyPVC) $(ValueName)
      _Assert($($R0))
		_ToBoolean(ifint $($R1) != 0, PVCHasConnections)

		_LocalShell GetRegValueByKey $(TYPECHECK_NONE) $(AdapterParametersKey) $(ValueName)
		_ToBoolean(ifint $($R1) != 0, AdapterHasConnections)

		_XOR($(PVCHasConnections), $(AdapterHasConnections))
		_If($($R0))
			set NeedBindingReview = TRUE
		endif
	EndForListDo

	_LocalShell CopyRegTree $(KeyPVC) $(AdapterParametersKey)
   _AssertGoto($($R0), Exit)

	set Success = TRUE

Exit = +

	_Exit("SetParams", _Show(Success)_Show(NeedBindingReview))
	Return $(Success) $(NeedBindingReview)

// Global

//
//	Create virtual adapter of given type on given port.
//
//		$0 = Type
//		$1 = Line
//		$2 = Adapter service name
//		$3 = Option
//
//		$R0 = Boolean indicating success
//		$R1 = Needs binding review
//
[CreateVirtualAdapters]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants

	set Type = $($0)
	set Line = $($1)
	set AdapterNameNumber = $($2)
   set Option = $($3)
	_Enter("CreateVirtualAdapters", _Show(Type)_Show(Line)_Show(AdapterNameNumber)_Show(Option))

	read-syms DriverConstants-$(Type)

	set Success = FALSE
	set NeedBindingReview = FALSE

   set KeyParams = KEY_NULL
   _OpenServiceParamKey($(AdapterNameNumber), KeyParams)
	_IfNotValidKey($(KeyParams))
      _OnUtilityShellErrorGoto("UNABLE_ACCESS_SOFTWARE_REG", Exit)
	endif

	set Key = KEY_NULL
	set KeySubPVCList = EMPTY_LIST

	_OpenRegKey($(KeyParams), $(LineServiceKeyName)\$(Line), Key)
	_IfNotValidKey($(Key))
      _OnUtilityShellErrorGoto("UNABLE_ACCESS_SOFTWARE_REG", Exit)
	endif

	set DLCIList = EMPTY_LIST
	EnumRegKey $(Key) SubKeyList
	ForListDo $(SubKeyList)
		set SubKeyName = _RegKeyName($($))
		_OpenRegKey($(Key), $(SubKeyName), KeySubPVC)
		_GetRegDwordByKey $(KeySubPVC) $(CircuitName)
      _AssertGoto($($R0), NextSubKey1)

		set DLCI = $($R1)
		set DLCIList = >($(DLCIList), $(DLCI))
		set KeySubPVCList = >($(KeySubPVCList), $(KeySubPVC))

NextSubKey1 = +
	EndForListDo
	
	set ProcessedDLCIList = EMPTY_LIST
	
	_LocalShell EnumAdapters { $(Type) } TRUE
	set AdapterDataList = $($R0)

	ForListDo $(AdapterDataList)
		set ServiceName = *($($), 1)
		set NetCardNum = *($($), 2)
		set KeyNetworkCard = *($($), 3)

		set KeyVirtualAdapterService = KEY_NULL

		_OpenServiceParamKey($(ServiceName), KeyVirtualAdapterService)
		_GetRegSzByKey $(KeyVirtualAdapterService) DXB_REGVAL_ADAPTER
      _AssertGoto($($R0), NextNetCard)
		set AdapterName = $($R1)
		ifstr(i) $(AdapterName) != $(AdapterNameNumber)
			goto NextNetCard
		endif

		_GetRegSzByKey $(KeyVirtualAdapterService) "Line"
      _AssertGoto($($R0), NextNetCard)
		set AdapterLine = $($R1)
		ifstr(i) $(AdapterLine) != $(Line)
			goto NextNetCard
		endif

		_GetRegDwordByKey $(KeyVirtualAdapterService) $(CircuitName)
      _AssertGoto($($R0), NextNetCard)
		set DLCI = $($R1)

		Ifcontains $(DLCI) in $(DLCIList)
			_LocalShell SetParams $(KeyVirtualAdapterService) $(DLCI) $(DLCIList) $(KeySubPVCList) $(KeyNetworkCard)
         _AssertGoto($($R0), NextNetCard)

			_If($($R1))
				set NeedBindingReview = TRUE
			endif
		else
			_UtilityShell RemoveHardwareComponent $(Manufacturer) $(ProductHardwareName) $(KEY_NETWORKCARDS)"\\"$(NetCardNum)
         _OnShellErrorGoto(NextNetCard)
         _OnUtilityShellErrorGoto($($R0), NextNetCard)
			set NeedBindingReview = TRUE
		endif

		set ProcessedDLCIList = >($(ProcessedDLCIList), $(DLCI))

NextNetCard = +

		set KeyCloseList = { $(KeyNetworkCard), $(KeyVirtualAdapterService) }
		_CloseRegKeyList($(KeyCloseList))

	EndForListDo

	ForListDo $(SubKeyList)
		set SubKeyName = _RegKeyName($($))

		_OpenRegKey($(Key), $(SubKeyName), KeySubPVC)
		_GetRegDwordByKey $(KeySubPVC) $(CircuitName)
      _AssertGoto($($R0), NextSubKey2)

		set DLCI = $($R1)
		Ifcontains $(DLCI) not-in $(ProcessedDLCIList)
         set RegPath = $(AdapterNameNumber)"\\Parameters\\"$(LineServiceKeyName)"\\"$(Line)"\\"$(SubKeyName)
			_LocalShell AddVirtualAdapter $(Type) $(Line) $(AdapterNameNumber) $(DLCI) $(Option) $(RegPath)
         _Assert($($R0))
			set VAdapterNum = $($R1)
			_OpenRegKey_HKLM($(KEY_NETWORKCARDS)"\\"$(VAdapterNum), KeyNetcard)
			_OpenServiceParamKey($(ProductHardwareName)$(VAdapterNum), KeyParameters)
	  		_LocalShell SetParams $(KeyParameters) $(DLCI) $(DLCIList) $(KeySubPVCList) $(KeyNetcard)
			CloseRegKey $(KeyParameters)
			CloseRegKey $(KeyNetcard)
			set NeedBindingReview = TRUE
		endif

NextSubKey2 = +

		CloseRegKey $(KeySubPVC)
	EndForListDo

	set Success = TRUE

Exit = +

	set KeyCloseList = >($(KeySubPVCList), $(Key))
	_CloseRegKeyList($(KeyCloseList))

	_Exit("CreateVirtualAdapters", _Show(Success)_Show(NeedBindingReview))
	Return $(Success) $(NeedBindingReview)

//
//	Adds a virtual adapter
//
//		$0 = Type
//		$1 = Line
//		$2 = Parent adapter service name
//		$3 = Circuit
//		$4 = Option
//    $5 = RegPath
//
//		$R0 = Boolean indicating success
//		$R1 = Virtual adapter number
//
[AddVirtualAdapter]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants

	set Type = $($0)
	set Line = $($1)
	set ParentAdapterServiceName = $($2)
	set Circuit = $($3)
	set Option = $($4)
   set RegPath = $($5)
	_Enter("AddVirtualAdapter", _Show(Type)_Show(Line)_Show(ParentAdapterServiceName)_Show(Circuit)_Show(Option)_Show(RegPath))

	set Success = FALSE
    set AdapterNumber = 0

	set	PhysicalAdapterServiceName = $(ParentAdapterServiceName)  

	ifcontains(i) $(Option) in $(OptionsThatNeedStreams)

		read-syms DriverConstants-Streams
		set ParentAdapterServiceName = $(ProductSoftwareName)

	endif

	set NetCardValueList = { +
		_MakeSzData(DXB_REGVAL_ADAPTER, $(ParentAdapterServiceName)), +
		_MakeDwordData("Hidden", TRUE) }

	ifstr(i) $(Type) == "X25" 

		set ParametersValueList = { +
			_MakeSzData(DXB_REGVAL_DEVICE, $(ParentAdapterServiceName)), +
			_MakeSzData("Line", $(Line)), +
			_MakeSzData("Protocol", $(Type)), +
		  _MakeSzData(DXB_REGVAL_REGPATH, $(RegPath)) }

	else

		set ParametersValueList = { +
			_MakeSzData(DXB_REGVAL_ADAPTER, $(PhysicalAdapterServiceName)), +
			_MakeSzData(DXB_REGVAL_DEVICE, $(ParentAdapterServiceName)), +
			_MakeSzData("Line", $(Line)), +
			_MakeSzData("Protocol", $(Type)), +
		  _MakeSzData(DXB_REGVAL_REGPATH, $(RegPath)) }

	endif

   _IfNotEmptyStr($(CircuitName))
	   set ParametersValueList = >($(ParametersValueList), _MakeDwordData($(CircuitName), $(Circuit)))
   endif

	_LocalShell AddAdapter $(Type) $(Option) EMPTY_STR "1000" $(NetCardValueList) EMPTY_LIST $(ParametersValueList)

	set Success = $($R0)
   set AdapterNumber = $($R1)

Exit = +

	_Exit("AddVirtualAdapter", _Show(Success)_Show(AdapterNumber))
	Return $(Success) $(AdapterNumber)

//
//	Remove virtual adapter.
//
//		$0 = Type
//		$1 = virtual adapter number
//		$2 = Manufacturer key name
//
[RemoveVirtual]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants

	set Type = $($0)
	set VAdapterNum = $($1)
	set Manufacturer = $($2)
	_Enter("RemoveVirtual", _Show(Type)_Show(VAdapterNum)_Show(Manufacturer))

	read-syms DriverConstants-$(Type)

	set AdapterKeyName = $(KEY_NETWORKCARDS)"\\"$(VAdapterNum)

	_UtilityShell DecrementRefCount $(AdapterKeyName)
	_UtilityShell IsRefCountEqualZero $(AdapterKeyName)
	_IfNot($($R1))
		goto Exit
	endif

	_UtilityShell RemoveHardwareComponent $(Manufacturer) $(ProductHardwareName) $(AdapterKeyName)
   _OnShellErrorGoto(Exit)
   _OnUtilityShellErrorGoto($($R0), Exit)

Exit = +

	_Exit("RemoveVirtual", "")
	Return

//
//	Remove virtual adapters.
//
//		$0 = Type
//		$1 = Line
//		$2 = Adapter name
//		$3 = Manufacturer key name
//

[RemoveVirtualAdapters]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants

	set Type = $($0)
	set Line = $($1)
	set AdapterNameNumber = $($2)
	set Manufacturer = $($3)
	_Enter("RemoveVirtualAdapters", _Show(Type)_Show(Line)_Show(AdapterNameNumber)_Show(Manufacturer))

	read-syms DriverConstants-$(Type)

	_LocalShell EnumAdapters { $(Type) } FALSE
	set AdapterDataList = $($R0)

	ForListDo $(AdapterDataList)
		set ServiceName = *($($), 1)
		set NetCardNum = *($($), 2)

      _Trace("Checking to see if we should remove ", _Show(ServiceName)_Show(NetCardNum))

		_OpenServiceParamKey($(ServiceName), KeyService)
		_IfValidKey($(KeyService)) 
			_GetRegSzByKey $(KeyService) DXB_REGVAL_ADAPTER
         _AssertGoto($($R0), NextNetCard)

			set AdapterName = $($R1)
			ifstr(i) $(AdapterName) != $(AdapterNameNumber)
				goto NextNetCard
			endif

			_GetRegSzByKey $(KeyService) "Line"
         _AssertGoto($($R0), NextNetCard)

			set AdapterLine = $($R1)
			ifstr(i) $(AdapterLine) != $(Line)
				goto NextNetCard
			endif
		endif

		_UtilityShell RemoveHardwareComponent $(Manufacturer) $(ProductHardwareName) $(KEY_NETWORKCARDS)"\\"$(NetCardNum)
      _OnShellErrorGoto(NextNetCard)
      _OnUtilityShellErrorGoto($($R0), NextNetCard)

NextNetCard = +

		_CloseRegKey($(KeyService))

	EndForListDo

	_Exit("RemoveVirtualAdapters", "")
	Return
