<commit_before>local lifx = Proto("lifx", "LIFX wifi lightbulb")

local onOffStrings = {
	[0x0000] = "Off",
	[0x0001] = "On",
	[0xffff] = "On"
}

local F = lifx.fields

F.size       = ProtoField.uint16("lifx.size"         , "Packet size"                 , base.HEX)
F.protocol   = ProtoField.uint16("lifx.protocol"     , "LIFX protocol"               , base.HEX)
F.reserved   = ProtoField.bytes("lifx.reserved"      , "Reserved"                    , base.HEX)
F.targetAddr = ProtoField.bytes("lifx.targetAddr"    , "Target address"              , base.HEX)
F.site       = ProtoField.bytes("lifx.site"          , "Site address"                , base.HEX)
F.timestamp  = ProtoField.uint64("lifx.timestamp"    , "Timestamp"                   , base.HEX)
F.packetType = ProtoField.uint16("lifx.packetType"   , "Packet type"                 , base.HEX)

F.unknown1   = ProtoField.uint8("lifx.unknown1"      , "Unknown - always 1 or 2"     , base.HEX)
F.unknown2   = ProtoField.uint32("lifx.unknown2"     , "Unknown - always 7c dd 00 00", base.HEX)
F.onoffReq   = ProtoField.uint8("lifx.onoff"         , "On/off setting"              , base.HEX, onOffStrings)
F.bulbName   = ProtoField.string("lifx.bulbName"     , "Bulb name"                   , base.HEX)
F.hue        = ProtoField.uint16("lifx.hue"          , "Hue"                         , base.HEX)
F.saturation = ProtoField.uint16("lifx.saturation"   , "Saturation"                  , base.HEX)
F.luminance  = ProtoField.uint16("lifx.luminance"    , "Luminance"                   , base.HEX)
F.colourTemp = ProtoField.uint16("lifx.colourTemp"   , "Colour temperature"          , base.HEX)
F.fadeTime   = ProtoField.uint16("lifx.fadeTime"     , "Fade time"                   , base.HEX)
F.fadeTime   = ProtoField.uint16("lifx.fadeTime"     , "Fade time"                   , base.HEX)
F.onoffRes   = ProtoField.uint16("lifx.onoffResponse", "On/off"                      , base.HEX, onOffStrings)

function switch(t)
	t.case = function (self, pType, buffer, pinfo, tree)
		local f=self[pType] or self.default
		if f then
			if type(f)=="function" then
				f(buffer, pinfo, tree, self)
			else
				error("case "..tostring(pType).." not a function")
			end
		end
	end
	return t
end

function unknownPacket(buffer, pinfo, tree)
	print("Unknown packet "..buffer(0,2):uint().." in packet number "..pinfo.number)
	tree:append_text(" (Unknown packet)")
end

-- 0x03 or 768
function deviceState(buffer, pinfo, tree)
	tree:append_text(" (Device state response)")
	tree:add(F.unknown1, buffer(0, 1))
	tree:add(F.unknown2, buffer(1, 4))
end

-- 0x02 or 512
function getPANgateway(buffer, pinfo, tree)
	tree:append_text(" (Get PAN gateway)")
end

-- 0x15 or 5376
function onoffRequest(buffer, pinfo, tree)
	tree:append_text(" (On/off request)")
	tree:add(F.onoffReq, buffer(0, 1))
end

-- 0x18 or 6144
function changeName(buffer, pinfo, tree)
	tree:append_text(" (Change name request)")
	tree:add(F.bulbName, buffer(0))
end

-- 0x65 or 25856
function statusRequest(buffer, pinfo, tree)
	tree:append_text(" (Status request)")
end

-- 0x66 or 26112
function setBulbState(buffer, pinfo, tree)
	tree:append_text(" (Set bulb state)")
	tree:add_le(F.hue, buffer(1,2))
	tree:add_le(F.saturation, buffer(3,2))
	tree:add_le(F.luminance,  buffer(5,2))
	tree:add_le(F.colourTemp, buffer(7,2))
	tree:add_le(F.fadeTime,   buffer(8,2))
end

-- 0x16 or 5632
function onoffResponse(buffer, pinfo, tree)
	tree:append_text(" (On/off response)")
	tree:add(F.onoffRes, buffer(0, 2))
end

-- 0x19 or 6400
function changeNameResponse(buffer, pinfo, tree)
	tree:append_text(" (Change name response)")
	tree:add(F.bulbName, buffer(0))
end

-- 0x6b or 27392
function statusResponse(buffer, pinfo, tree)
	tree:append_text(" (Status response)")
	tree:add_le(F.hue       , buffer(0 , 2))
	tree:add_le(F.saturation, buffer(2 , 2))
	tree:add_le(F.luminance , buffer(4 , 2))
	tree:add_le(F.colourTemp, buffer(6 , 2))
	tree:add(F.reserved     , buffer(8 , 2))
	tree:add(F.onoffRes     , buffer(10, 2))
	tree:add(F.bulbName     , buffer(12))
end

-- 0x68 or 26624
function setDimAbsolute(buffer, pinfo, tree)
	tree:append_text(" (Set dim - absolute)")
end

-- 0x69 or 26880
function setDimRelative(buffer, pinfo, tree)
	tree:append_text(" (Set dim - relative)")
end

-- 0x12f or 12033
function wifiState(buffer, pinfo, tree)
	tree:append_text(" (Wifi state)")
end

packetTable = switch {
	[768]   = deviceState,
	[512]   = getPANgateway,
	[5376]  = onoffRequest,
	[6144]  = changeName,
	[25856] = statusRequest,
	[26112] = setBulbState,
	[5632]  = onoffResponse,
	[6400]  = changeNameResponse,
	[27392] = statusResponse,
	[26624] = setDimAbsolute,
	[26880] = setDimRelative,
	[12033] = wifiState,
	default = unknownPacket
}

function lifx.dissector(buffer, pinfo, tree)
	analyse(buffer, pinfo, tree)
end

function analyse(buffer, pinfo, tree)
	pinfo.cols.info = "LIFX"

	local lifxlength = buffer(0,1):uint() + (buffer(1,1):uint() * 256)

	local subtree = tree:add(buffer(0, lifxlength), "LIFX")
	subtree:add_le(F.size, buffer(0,2))
	subtree:add(F.protocol, buffer(2,2))
	subtree:add(F.reserved, buffer(4,4))
	subtree:add(F.targetAddr, buffer(8,6))
	subtree:add(F.reserved, buffer(14,2))
	subtree:add(F.site, buffer(16,6))
	subtree:add(F.reserved, buffer(22,2))
	subtree:add(F.timestamp, buffer(24,8))<commit_msg>UI improvements to the wireshark dissector<commit_after>local lifx = Proto("lifx", "LIFX wifi lightbulb")

local onOffStrings = {
	[0x0000] = "Off",
	[0x0001] = "On",
	[0xffff] = "On"
}

local F = lifx.fields

F.size       = ProtoField.uint16("lifx.size"         , "Packet size"                 , base.HEX)
F.protocol   = ProtoField.uint16("lifx.protocol"     , "LIFX protocol"               , base.HEX)
F.reserved   = ProtoField.bytes("lifx.reserved"      , "Reserved"                    , base.HEX)
F.targetAddr = ProtoField.bytes("lifx.targetAddr"    , "Target address"              , base.HEX)
F.site       = ProtoField.bytes("lifx.site"          , "Site address"                , base.HEX)
F.timestamp  = ProtoField.uint64("lifx.timestamp"    , "Timestamp"                   , base.HEX)
F.packetType = ProtoField.uint16("lifx.packetType"   , "Packet type"                 , base.HEX)

F.unknown1   = ProtoField.uint8("lifx.unknown1"      , "Unknown - always 1 or 2"     , base.HEX)
F.unknown2   = ProtoField.uint32("lifx.unknown2"     , "Unknown - always 7c dd 00 00", base.HEX)
F.onoffReq   = ProtoField.uint8("lifx.onoff"         , "On/off setting"              , base.HEX, onOffStrings)
F.bulbName   = ProtoField.string("lifx.bulbName"     , "Bulb name"                   , base.HEX)
F.hue        = ProtoField.uint16("lifx.hue"          , "Hue"                         , base.HEX)
F.saturation = ProtoField.uint16("lifx.saturation"   , "Saturation"                  , base.HEX)
F.luminance  = ProtoField.uint16("lifx.luminance"    , "Luminance"                   , base.HEX)
F.colourTemp = ProtoField.uint16("lifx.colourTemp"   , "Colour temperature"          , base.HEX)
F.fadeTime   = ProtoField.uint16("lifx.fadeTime"     , "Fade time"                   , base.HEX)
F.onoffRes   = ProtoField.uint16("lifx.onoffResponse", "On/off"                      , base.HEX, onOffStrings)

function switch(t)
	t.case = function (self, pType, buffer, pinfo, tree)
		local f=self[pType] or self.default
		if f then
			local subtree = tree:add(f[2])
			pinfo.cols.info = f[2]
			if type(f[1])=="function" then
				f[1](buffer, pinfo, subtree, self)
			end
		end
	end
	return t
end

function unknownPacket(buffer, pinfo, tree)
	print("Unknown packet "..buffer(0,2):uint().." in packet number "..pinfo.number)
end

-- 0x03 or 768
function deviceState(buffer, pinfo, tree)
	tree:add(F.unknown1, buffer(0, 1))
	tree:add(F.unknown2, buffer(1, 4))
end

-- 0x15 or 5376
function onoffRequest(buffer, pinfo, tree)
	tree:add(F.onoffReq, buffer(0, 1))
end

-- 0x18 or 6144
function changeName(buffer, pinfo, tree)
	tree:add(F.bulbName, buffer(0))
end

-- 0x66 or 26112
function setBulbState(buffer, pinfo, tree)
	tree:add_le(F.hue, buffer(1,2))
	tree:add_le(F.saturation, buffer(3,2))
	tree:add_le(F.luminance,  buffer(5,2))
	tree:add_le(F.colourTemp, buffer(7,2))
	tree:add_le(F.fadeTime,   buffer(8,2))
end

-- 0x16 or 5632
function onoffResponse(buffer, pinfo, tree)
	tree:add(F.onoffRes, buffer(0, 2))
end

-- 0x19 or 6400
function changeNameResponse(buffer, pinfo, tree)
	tree:add(F.bulbName, buffer(0))
end

-- 0x6b or 27392
function statusResponse(buffer, pinfo, tree)
	tree:add_le(F.hue       , buffer(0 , 2))
	tree:add_le(F.saturation, buffer(2 , 2))
	tree:add_le(F.luminance , buffer(4 , 2))
	tree:add_le(F.colourTemp, buffer(6 , 2))
	tree:add(F.reserved     , buffer(8 , 2))
	tree:add(F.onoffRes     , buffer(10, 2))
	tree:add(F.bulbName     , buffer(12))
end

packetTable = switch {
	[768]   = { deviceState       , "Device state response"},
	[512]   = { nil               , "Get PAN Gateway"},
	[5376]  = { onoffRequest      , "On/off request"},
	[6144]  = { changeName        , "Change name request"},
	[25856] = { nil               , "Status request"},
	[26112] = { setBulbState      , "Set bulb state"},
	[5632]  = { onoffResponse     , "On/off response"},
	[6400]  = { changeNameResponse, "Change name response"},
	[27392] = { statusResponse    , "Status response"},
	[26624] = { nil               , "Set dim - absolute"},
	[26880] = { nil               , "Set dim - relative"},
	[12033] = { nil               , "Wifi state"},
	default = { unknownPacket     , "Unknown packet"}
}

function lifx.dissector(buffer, pinfo, tree)
	analyse(buffer, pinfo, tree)
end

function analyse(buffer, pinfo, tree)
	pinfo.cols.info = "LIFX"

	local lifxlength = buffer(0,1):uint() + (buffer(1,1):uint() * 256)

	local subtree = tree:add(buffer(0, lifxlength), "LIFX packet")
	subtree:add_le(F.size, buffer(0,2))
	subtree:add(F.protocol, buffer(2,2))
	subtree:add(F.reserved, buffer(4,4))
	subtree:add(F.targetAddr, buffer(8,6))
	subtree:add(F.reserved, buffer(14,2))
	subtree:add(F.site, buffer(16,6))
	subtree:add(F.reserved, buffer(22,2))
	subtree:add(F.timestamp, buffer(24,8))
{'fraction_non_alphanumeric': 0.15254909740130926, 'fraction_numerical': 0.04681610791509621, 'mean_word_length': 3.20125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6157099', 'n_tokens_mistral': 4177, 'n_tokens_neox': 3826, 'n_words': 772}