{"record":{"id":"1f3bfce3f7388ff9","repo":"mgth/LittleBigMouse","slug":"vidaa-returned-an-invalid-mqtt-connection-response","errorCode":null,"errorMessage":"VIDAA returned an invalid MQTT connection response.","messagePattern":"VIDAA returned an invalid MQTT connection response\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"LittleBigMouse.Plugins/LittleBigMouse.Plugin.Vcp.Avalonia/HisenseVidaa/VidaaMqttConnection.cs","lineNumber":86,"sourceCode":"        {\n            throw new AuthenticationException(\n                \"VIDAA rejected TLS. Select the PKCS#12 client certificate extracted from the official app.\", e);\n        }\n\n        _stream = ssl;\n        await WritePacketAsync(BuildConnectPacket(clientId, username, password), cancellationToken)\n            .ConfigureAwait(false);\n        (byte Header, byte[] Payload) connack;\n        try\n        {\n            connack = await ReadPacketAsync(_stream, cancellationToken).ConfigureAwait(false);\n        }\n        catch (IOException e) when (certificates.Count == 0)\n        {\n            throw VidaaCertificate.MissingException(e);\n        }\n        if ((connack.Header >> 4) != 2 || connack.Payload.Length < 2)\n            throw new IOException(\"VIDAA returned an invalid MQTT connection response.\");\n        if (connack.Payload[1] != 0)\n            throw new UnauthorizedAccessException(MqttError(connack.Payload[1]));\n\n        _connected = true;\n        // The caller token limits the handshake only. Once established, MQTT has\n        // its own lifetime so a completed UI command cannot tear down the session.\n        _lifetime = new CancellationTokenSource();\n        _readerTask = ReadLoopAsync(_lifetime.Token);\n        _pingTask = PingLoopAsync(_lifetime.Token);\n    }\n\n    public async Task SubscribeAsync(IEnumerable<string> topics, CancellationToken cancellationToken)\n    {\n        foreach (var topic in topics)\n        {\n            var id = unchecked(++_packetId);\n            if (id == 0) id = unchecked(++_packetId);\n            using var body = new MemoryStream();","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/mgth/LittleBigMouse/blob/7a42f01d47d99d223b8ee33ba4019af82adf1c48/LittleBigMouse.Plugins/LittleBigMouse.Plugin.Vcp.Avalonia/HisenseVidaa/VidaaMqttConnection.cs#L68-L104","documentation":"Thrown in VidaaMqttConnection.ConnectAsync when the CONNACK packet returned by the Hisense VIDAA TV is not a valid MQTT connection acknowledgment: either the packet type (high nibble of the header) is not 2 (CONNACK) or the payload is shorter than 2 bytes. The library treats this as a protocol violation by the TV's MQTT broker rather than an authentication problem, so it surfaces as IOException. It means the device responded, but not with a recognizable MQTT handshake.","triggerScenarios":"ConnectAsync (called via OpenMqttAsync) reads the first packet after connecting to the TV's MQTT-over-TLS port; if ((connack.Header >> 4) != 2 || connack.Payload.Length < 2) the IOException is thrown — e.g. the TV replied with a plain-HTTP error page, an MQTT PUBACK instead of CONNACK, or a truncated/empty CONNACK.","commonSituations":"Wrong target port on the TV (connecting to an HTTP endpoint instead of the MQTT broker), a non-VIDAA device or non-standard firmware on that port, a proxy/middlebox mangling the TLS stream, or an incompatible VIDAA firmware version that returns a non-standard handshake response.","solutions":["Verify the host and port point at the VIDAA TV's MQTT broker endpoint (not its HTTP API port).","Power-cycle the TV and retry — a wedged broker can emit garbage instead of CONNACK.","Confirm the TV model/firmware actually supports the VIDAA MQTT API this plugin targets; update or downgrade firmware if it changed the protocol.","Capture traffic (or log the first bytes) on the socket to see what the TV actually returns before CONNACK is expected."],"exampleFix":"// before: any exception bubbles up as-is\nawait connection.OpenMqttAsync(token);\n// after: distinguish protocol failure from auth failure\ntry { await connection.OpenMqttAsync(token); }\ncatch (IOException e) when (e.Message.Contains(\"invalid MQTT connection response\"))\n{ /* wrong port/device or non-MQTT endpoint; fix host/port and retry */ }","handlingStrategy":"try-catch","validationCode":"// Verify the endpoint speaks MQTT-TLS before connecting\ncanConnect = Uri.TryCreate(tvHost, UriKind.Absolute, out var uri) && uri.Port == mqttTlsPort;","typeGuard":"bool IsValidConnack(byte header, byte[] payload) => (header >> 4) == 2 && payload.Length >= 2;","tryCatchPattern":"try { await connection.OpenMqttAsync(token); }\ncatch (IOException e) when (e.Message.Contains(\"invalid MQTT connection response\"))\n{ // endpoint is not answering MQTT: fix host/port or firmware before retrying }","preventionTips":["Confirm the port used is the VIDAA MQTT broker, not the TV's HTTP API","Pin/verify TV model and firmware support before shipping the integration","Power-cycle and re-check the TV after firmware updates","Log raw first bytes on handshake failure to diagnose protocol mismatch"],"tags":["mqtt","protocol-violation","network","iot"],"backgroundTag":"unexpected-response-shape","analyzedSha":"7a42f01d47d99d223b8ee33ba4019af82adf1c48","analyzedAt":"2026-09-16T00:35:00.514Z","contentChangedAt":"2026-09-16T00:35:00.514Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}