// tapi.inc

[TapiConstants]

KEY_DEVICEMAPROOT	= "HARDWARE\\DEVICEMAP"
KEY_TAPIDEVICES	= "TAPI DEVICES"
VALUE_ADDRESS		= "Address"
VALUE_MEDIATYPE   = "Media Type"

#define SETTAPI_ADDRSCHEME_LINE       2
#define SETTAPI_ADDRSCHEME_ENDPOINT   3

//
//	Adds addresses in the "HARDWARE\DEVICEMAP\Tapi Devices\" tree.
//	If total port count is zero all ports are removed.
//
//		$0 = Adapter type.
//		$1 = Virtual adapter number.
//		$2 = Port count.
//    $3 = Addressing scheme
//
//		$R0 = Boolean indicating success.
//
[SetTapi]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
   read-syms DriverConstants
	read-syms TapiConstants

	set SvcType = $($0)
	set VirtualAdapterNumber = $($1)
	set PortCount = $($2)
   set AddrScheme = $($3)
	_Enter("SetTapi", _Show(SvcType)_Show(VirtualAdapterNumber)_Show(PortCount)_Show(AddrScheme))

	read-syms DriverConstants-$(SvcType)

	set Success = FALSE

	set KeyDeviceMap = KEY_NULL
	set KeyTapiDevices = KEY_NULL
	set KeyServiceProvider = KEY_NULL

	StartWait

	_OpenRegKey_HKLM($(KEY_DEVICEMAPROOT), KeyDeviceMap)
	_IfNotValidKey($(KeyDeviceMap))
      _OnUtilityShellErrorGoto("UNABLE_ACCESS_DEVICEMAP_REG", Error)
	endif

	_MakeStdRegKey($(KeyDeviceMap), $(KEY_TAPIDEVICES), KeyTapiDevices)
	_IfNotValidKey($(KeyTapiDevices))
      _OnUtilityShellErrorGoto("UNABLE_ACCESS_TAPIDEVICES_REG", Error)
	endif

	_MakeStdRegKey($(KeyTapiDevices), $(ProductSoftwareName), KeyServiceProvider)
	_IfNotValidKey($(KeyServiceProvider))
      _OnUtilityShellErrorGoto("UNABLE_ACCESS_SOFTWARE_REG", Error)
	endif

	set PreviousAddressList = EMPTY_LIST
	_GetRegMultiSzByKey $(KeyServiceProvider) $(VALUE_ADDRESS)
   _If($($R0)))
	   set PreviousAddressList = $($R1)
   endif

   // Remove any of our previous addresses from the list
	set CurrentAddressList = EMPTY_LIST
	ForListDo $(PreviousAddressList)
      set Address = $($)
		Split-String $(Address) "-" AddressParts

      ifstr(i) *($(AddressParts), 1) != $(VirtualAdapterNumber)
         set CurrentAddressList = >($(CurrentAddressList), $(Address))
      endif

	EndForListDo

   // Now add our new addresses
	set BuildDigit2 = 0
	set BuildDigit3 = 0

   // a little double indirection
   set DigitToInc = BuildDigit$(AddrScheme)

	_Do(Build)
		set BuildAddress = $(VirtualAdapterNumber)"-"$(BuildDigit2)"-"$(BuildDigit3)
		set CurrentAddressList = >($(CurrentAddressList), $(BuildAddress))

	   _Trace("Incrmenting Tapi address", _Show(DigitToInc)_Show($(DigitToInc)))
		set-add $(DigitToInc) = $($(DigitToInc)), 1

	_While(Build, ifint $($(DigitToInc)) < $(PortCount))

   QueryListSize AddressListSize $(CurrentAddressList)

	ifint $(AddressListSize) == 0
	   _Trace("Deleting TAPI DEVICES entry", _Show(ProductSoftwareName))
	   _CloseRegKey($(KeyServiceProvider))
		DeleteRegKey $(KeyTapiDevices) $(ProductSoftwareName)
		goto Exit
	endif

	_Trace("Address to add", _Show(CurrentAddressList))

	set NewValueList = { +
		_MakeSzData($(VALUE_MEDIATYPE), $(MediaType)) +
		_MakeMultiSzData($(VALUE_ADDRESS), $(CurrentAddressList)) }

	_LocalShell SetValuesFromList $(KeyServiceProvider) $(NewValueList)
   _AssertGoto($($R0), Error)


Exit = +
	set Success = TRUE

Error = +
	_CloseRegKey($(KeyDeviceMap))
	_CloseRegKey($(KeyTapiDevices))
	_CloseRegKey($(KeyServiceProvider))

	EndWait

	_Exit("SetTapi", _Show(Success))
	Return $(Success)
