// adapter.inc - adapter related shell sections

//
//	Make list of adapters based on list hardware names.
//
//		$0 = Product type list.
//		$1 = Want adapter keys (boolean)
//
//		$R0 = Adapter data list (list of: ServiceName, CardNumber, AdapterKey)
//
[EnumAdapters]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants

	set ProductTypeList = $($0)
	set WantAdapterKeys = $($1)
	_Enter("EnumAdapters", _Show(ProductTypeList)_Show(WantAdapterKeys))

	set ProductNameList = EMPTY_LIST
	ForListDo $(ProductTypeList)
		read-syms DriverConstants-$($)
		set ProductNameList = >($(ProductNameList), $(ProductHardwareName))
	EndForListDo
	_Whisper("EnumAdapters", _Show(ProductNameList))

	set AdapterDataList = EMPTY_LIST

	_OpenRegKey_HKLM($(KEY_NETWORKCARDS), KeyNetCards)
	_IfValidKey($(KeyNetCards))
		EnumRegKey $(KeyNetCards) KeyNameList
		ForListDo $(KeyNameList)
			set NetCardNum = _RegKeyName($($))
			_Whisper("EnumAdapters", _Show(NetCardNum))
			_OpenRegKey($(KeyNetCards), $(NetCardNum), KeyNetCard)
			_IfValidKey($(KeyNetCard))
				_GetRegSzByKey $(KeyNetCard) "ProductName"
            _Assert($($R0))
				set ProductName = $($R1)
				_Whisper("EnumAdapters", _Show(ProductName))

				Ifcontains(i) $(ProductName) in $(ProductNameList)
					_GetRegSzByKey $(KeyNetCard) DXB_REGVAL_SERVICENAME
               _Assert($($R0))
					set ServiceName = $($R1)
					_Whisper("EnumAdapters", _Show(ServiceName))
					
					_IfNot($(WantAdapterKeys))
						CloseRegKey $(KeyNetCard)
						set KeyNetCard = KEY_NULL
					endif
	
					set AdapterData = { $(ServiceName), $(NetCardNum), $(KeyNetCard) }
					_Whisper("Found match: ", _Show1(AdapterData))
					set AdapterDataList = >($(AdapterDataList), $(AdapterData))
				else
					CloseRegKey $(KeyNetCard)
				endif
			endif
		EndForListDo
		CloseRegKey $(KeyNetCards)
	endif

Exit = +

	_Exit("EnumAdapters", _Show(AdapterDataList))
	Return $(AdapterDataList)

//
//	Finds the first instance of an adapter belonging to a specific driver.
//
//		$0 = Driver name.
//
//		$R0 = Boolean indicating success.
//		$R1 = Adapter number.
//
[FindAdapter]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants

	set ProductHardwareName = $($0)
	_Enter("FindAdapter", _Show(ProductHardwareName))

	set Success = FALSE
	set AdapterNumber = EMPTY_STR

	_OpenRegKey_HKLM($(KEY_NETWORKCARDS), KeyNetCards)
	_IfValidKey($(KeyNetCards))
		EnumRegKey $(KeyNetCards) NetworkCardList
		ForListDo $(NetworkCardList)
			_IfNot($(Success))
				set NetworkCardKeyName = _RegKeyName($($))
				_Whisper("FindAdapter", _Show(NetworkCardKeyName))
				_OpenRegKey($(KeyNetCards), $(NetworkCardKeyName), NetworkCardKey)
				_IfValidKey($(NetworkCardKey))
					_GetRegSzByKey $(NetworkCardKey) "ProductName"
               _Assert($($R0))
					set ProductName = $($R1)
					_Whisper("FindAdapter", _Show(ProductName))
  	  				ifstr(i) $(ProductName) == $(ProductHardwareName)
						set AdapterNumber = $(NetworkCardKeyName)
						set Success = TRUE
					endif
				endif
			endif
		EndForListDo
	endif

Exit = +

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

//
//	Adds an instance of a driver
//
//		$0 = Type
//		$1 = Option
//    $2 = StringsOverride (optional)
//    $3 = Adapter number starting point (optional)
//    $4 = Additional NetCardValues (optional)
//    $5 = Additional AdapterRules (optional)
//    $6 = Additional Parameters (optional)
//
//		$R0 = Boolean indicating success
//		$R1 = Driver Instance
//
[AddAdapter]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants

	set Type = $($0)
	set Option = $($1)
	set StringsOverride = $($2)
   set AdapterNumberStartingPoint = $($3)
	set AdditionalNetCardValues = $($4)
	set AdditionalAdapterRules = $($5)
	set AdditionalParameters = $($6)
	_Enter("AddAdapter", _Show(Type)_Show(Option)_Show(StringsOverride)_Show(AdapterNumberStartingPoint)_Show(AdditionalNetCardValues)_Show(AdditionalAdapterRules)_Show(AdditionalParameters))

	read-syms DriverConstants-$(Type)
	read-syms $(Type)-Strings-$(!STF_LANGUAGE)

   _IfNotEmptyStr($(StringsOverride))
	   read-syms $(StringsOverride)
   endif

	set Success = FALSE
	set AdapterNumber = 0

	set KeyNetCard = KEY_NULL
	set KeyAdapterRules = KEY_NULL
	set KeyParameters = KEY_NULL

	set HardwareInstalled = FALSE

	_UtilityShell AssignAdapterNumber EMPTY_STR $(AdapterNumberStartingPoint)
   _OnShellErrorGoto(Error)
   _OnUtilityShellErrorGoto($($R0), Error)

	set AssignedAdapterNumber = $($R1)

   //
   // From UTILITY.INF
   //
   // [AddHardwareComponent]
   //    set SvcName       = $($0)
   //    set Infname       = $($1)
   //    set SoftwareName  = $($2)
   //    set KeyServicesArea = $($3)
   //    set KeyNetcardsArea = $($4)
   //    set AdapNum       = $($5)

	_UtilityShell AddHardwareComponent +
      $(ProductHardwareName) +
      $(STF_CONTEXTINFNAME) +
      $(ProductKeyName) +
      EMPTY_STR +
      EMPTY_STR +
      $(AssignedAdapterNumber)
   _OnShellErrorGoto(Error)
   _OnUtilityShellErrorGoto($($R0), Error)

	set KeyNetCard = $($R1)
	set KeyAdapterRules = $($R2)
	set KeyParameters = $($R3)
	set AdapterNumber = $($R4)
	set AdapterServiceName = $($R5)

	set HardwareInstalled = TRUE
   set NewAdapterTitle = "["$(AdapterNumber)"] "$(AdapterTitle)

   _LocalShell SetRefCountByKey $(KeyNetCard) 1
   _AssertGoto($($R0), Error)

	_LocalShell GetElapsedTime
	set CurrentTime = $($R0)

   // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetwordCards\$(AdapterNumber)
	set NetCardValueList = { +
		_MakeSzData("Manufacturer", $(Manufacturer)), +
		_MakeSzData("Title", $(NewAdapterTitle)), +
		_MakeSzData("Description", $(AdapterDescription)), +
		_MakeSzData("ProductName", $(ProductHardwareName)), +
		_MakeSzData(DXB_REGVAL_SERVICENAME, $(AdapterServiceName)), +
		_MakeDwordData("InstallDate", $(CurrentTime)) }

   // The duplicated SetValuesFromLists below may seem to be 
   // unnecessary repetition but will ensure default values
   // get overwritten by additional values passed in.

	_LocalShell SetValuesFromList $(KeyNetCard) $(NetCardValueList)
   _AssertGoto($($R0), Error)

	_LocalShell SetValuesFromList $(KeyNetCard) $(AdditionalNetCardValues)
   _AssertGoto($($R0), Error)

   // HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetwordCards\$(AdapterNumber)\NetRules
	set TempProdName = """"$(ProductHardwareName)$(AdapterNumber)""""
	set TempBindForm = $(TempProdName)$(NetRuleHardwareBindForm)
	set AdapterRulesValueList = { +
		_MakeSzData("type", $(NetRuleHardwareType)), +
		_MakeSzData("bindform", $(TempBindForm)), +
		_MakeMultiSzData("class", $(NetRuleHardwareClass)), +
		_MakeSzData("InfOption", $(Option)) }

	_LocalShell SetValuesFromList $(KeyAdapterRules) $(AdapterRulesValueList)
   _AssertGoto($($R0), Error)

	_LocalShell SetValuesFromList $(KeyAdapterRules) $(AdditionalAdapterRules)
   _AssertGoto($($R0), Error)

	set DeviceName = $(AdapterServiceName)
#if defined(SUPPORT_SYNC570)
	ifcontains(i) $(Option) in $(OptionsThatNeedStreams)
		set DeviceName = $(StreamsDevice)
	endif
#endif

   // HKLM\SYSTEM\CurrentControlSet\Services\$(AdapterServiceName)\Parameters
	set ParametersValueList = { +
		_MakeSzData(DXB_REGVAL_SERVICENAME, $(AdapterServiceName)), +
		_MakeSzData(DXB_REGVAL_DEVICE, $(DeviceName)), +
        _MakeSzData(DXB_REGVAL_NDIS_ADAPTER_INSTANCE, $(AdapterNumber)) }

	_LocalShell SetValuesFromList $(KeyParameters) $(ParametersValueList)
   _AssertGoto($($R0), Error)

	_LocalShell SetValuesFromList $(KeyParameters) $(AdditionalParameters)
   _AssertGoto($($R0), Error)

	set Success = TRUE
	goto Exit

Error = +

	_If($(HardwareInstalled))
	   _UtilityShell RemoveHardwareComponent $(Manufacturer) $(ProductHardwareName) $(KEY_NETWORKCARDS)"\\"$(AdapterNumber)
#if defined(_DEBUG)
      _ShellError(DontCare)
      _UtilityShellError($($R0), DontCare)
#endif
	endif

Exit = +

	set KeyCloseList = { $(KeyNetCard), $(KeyAdapterRules), $(KeyParameters) }
	_CloseRegKeyList($(KeyCloseList))

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