--¶«ÁÕ

HexMode = 0 -- 0 Text mode, 1 Hex mode, global

local btnew = ByteArray.btnew
local btget = ByteArray.btget
local btset = ByteArray.btset
local bttostring = ByteArray.bttostring
local bttoarray = ByteArray.bttoarray
local btarraycopy = ByteArray.btarraycopy
local btmemcmp = ByteArray.btmemcmp
local btmemset = ByteArray.btmemset

local DeviceCount

local ErrTable = {
	{0x20, "0060"}, --ºìÂÌµÆ
	{0x01, "0061"},
	{0x02, "0062"},
	{0x03, "0063"},
	{0x04, "0064"},
	{0x05, "0065"},
	{0x06, "0066"},
	{0x07, "0067"},
	{0x08, "0068"},
	{0x09, "0069"},
	{0x11, "0051"},--Bin 1B

	{0x82, "0032"},
	{0x83, "0030"},
	{0x85, "0040"},
	{0x86, "0041"},
	{0x87, "0033"}, --Unknown Flash
	{0x88, "0002"},
	{0x89, "0030"},
	{0x8D, "0031"}, --RB Abnormal

	{0x94, "0003"}, --Sort Time out
	{0x96, "0034"}, --No Flash
	--{0x96, "7777"}, --Device Lost
	{0x97, "0255"}, --No Test
	{0x95, "ffff"}, --AddDisk Time out
}

function RecvMessage(command, contype)
	local startStr = ""
	if contype == 1 then --COM mode @START_TEST 00
		startStr = "@START_TEST 00 "
	elseif contype == 2 then  --TCP/IP @START_TEST
		startStr = "@START_TEST "
	else
		return false
	end

	local pos = string.find(command, startStr)
	if pos == nil or pos ~= 1 then
		return false
	end

	if string.sub(command, string.len(command)) ~= "+" then
		return false
	end

	command = string.gsub(command, startStr, "")
	command = string.gsub(command, " ", "")
	command = string.gsub(command, "+", "")

	DeviceCount = string.len(command)


	local result = btnew(DeviceCount, 0)
	for i = 0, DeviceCount - 1 do
		if string.sub(command, i + 1, i + 1) == "1" then
			btset(result, i, 1)
		else
			btset(result, i, 0)
		end
	end

	return true, DeviceCount, result
end

function AnswerControler(buf, len, contype)
	local result = ""
	if contype == 1 then --COM mode @START_TEST 00
		result = result .. "@TEST_DONE 00"
	elseif contype == 2 then  --TCP/IP @START_TEST
		result = result .. "@TEST_DONE"
	else
		return 0
	end

	for i = 0, len - 1 do
		result = result.." "..GetCode(btget(buf, i))
		--[[
		if (btget(buf, i) & 0x80) == 0 then
			result = result .. " 0001"
		else
			result = result .. " 0000"
		end
		]]
	end

	result = result .. "+"
	return 1, result
end

function TimeOut(contype)
	return "@TIMEOUT+"
end

function GetCode(bt)
	for i = 1, #ErrTable do
		if ErrTable[i][1] == bt then
			return ErrTable[i][2]
		end
	end

	if (bt & 0x80) == 0 then
		return "0001"
	else
		return "0000"
	end

	return "XXXX" --Unknown Error
end

function ConfigPath()
	return "D:\\IP\\DGHandlerIP.ini"
end

function IsAutoDownload(command)
	local startStr = "@AUTO_DOWNLOAD "

	local pos = string.find(command, startStr)
	if pos == nil or pos ~= 1 then
		return false
	end

	if string.sub(command, string.len(command)) ~= "+" then
		return false
	end

	command = string.gsub(command, startStr, "")
	command = string.gsub(command, " ", "")
	command = string.gsub(command, "+", "")

	return true, command
end

function AutoDownload(state)
	if state > 0 then
		return "@AUTO_DOWNLOAD OK+"
	else
		return "@AUTO_DOWNLOAD FAIL+"
	end
end

