{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "/ai/protocol-profile.schema.json",
  "title": "Meow Serial Protocol Profile",
  "description": "Direct-import schema for Meow Serial protocol JSON. If an AI starts from https://s.mpas.top, read /ai/agent-route.json before generating this JSON.",
  "$comment": "AI route: /ai/agent-route.json. Protocol JSON is declarative data; do not embed executable parser code.",
  "x-ai-route": "/ai/agent-route.json",
  "x-ai-output-rule": "Return exactly one JSON object for direct import. No Markdown, comments, or executable code.",
  "type": "object",
  "additionalProperties": true,
  "required": ["name", "kind", "defaultBaudRate"],
  "properties": {
    "id": {
      "type": "string",
      "description": "Optional stable profile id. The app can generate one when absent."
    },
    "name": {
      "type": "string",
      "minLength": 1
    },
    "description": {
      "type": "string"
    },
    "kind": {
      "type": "string",
      "enum": ["line-values", "json-lines", "tlv"]
    },
    "defaultBaudRate": {
      "type": "number",
      "minimum": 1
    },
    "heldChannels": {
      "type": "array",
      "items": { "type": "string" },
      "default": []
    },
    "heldWindowMs": {
      "type": "number",
      "minimum": 0,
      "default": 3000
    },
    "line": {
      "type": "object",
      "additionalProperties": true,
      "required": ["channelNames"],
      "properties": {
        "separatorPattern": {
          "type": "string",
          "default": "[,\\t; ]+"
        },
        "channelNames": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "json": {
      "type": "object",
      "additionalProperties": true,
      "required": ["fieldPaths"],
      "properties": {
        "fieldPaths": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string" },
          "description": "Dotted numeric field paths. Empty means all top-level numeric fields."
        }
      }
    },
    "tlv": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "headerSize",
        "packetLengthOffset",
        "packetLengthType",
        "packetLengthEndian",
        "tlvCountOffset",
        "tlvCountType",
        "tlvCountEndian",
        "tlvHeaderSize",
        "tlvTypeOffset",
        "tlvTypeType",
        "tlvLengthOffset",
        "tlvLengthType",
        "tlvHeaderEndian",
        "tlvLengthIncludesHeader",
        "mappings"
      ],
      "properties": {
        "magicWordHex": { "type": "string" },
        "headerSize": { "type": "number", "minimum": 8 },
        "packetLengthOffset": { "type": "number", "minimum": 0 },
        "packetLengthType": { "type": "string", "enum": ["u16", "u32"] },
        "packetLengthEndian": { "type": "string", "enum": ["little", "big"] },
        "tlvCountOffset": { "type": "number", "minimum": -1 },
        "tlvCountType": { "type": "string", "enum": ["u16", "u32"] },
        "tlvCountEndian": { "type": "string", "enum": ["little", "big"] },
        "tlvHeaderSize": { "type": "number", "minimum": 4 },
        "tlvTypeOffset": { "type": "number", "minimum": 0 },
        "tlvTypeType": { "type": "string", "enum": ["u16", "u32"] },
        "tlvLengthOffset": { "type": "number", "minimum": 0 },
        "tlvLengthType": { "type": "string", "enum": ["u16", "u32"] },
        "tlvHeaderEndian": { "type": "string", "enum": ["little", "big"] },
        "tlvLengthIncludesHeader": { "type": "boolean" },
        "mappings": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": true,
            "required": ["label", "tlvType", "valueOffset", "valueType", "endian", "scale"],
            "properties": {
              "id": { "type": "string" },
              "label": { "type": "string", "minLength": 1 },
              "tlvType": { "type": "number", "minimum": 0 },
              "valueOffset": { "type": "number", "minimum": 0 },
              "valueType": {
                "type": "string",
                "enum": ["u8", "i8", "u16", "i16", "u32", "i32", "f32", "f64"]
              },
              "endian": { "type": "string", "enum": ["little", "big"] },
              "scale": { "type": "number" },
              "unit": { "type": "string" }
            }
          }
        },
        "fields": {
          "description": "Alias accepted by protocolDocument parser; prefer mappings for direct profile JSON.",
          "type": "array",
          "items": { "type": "object" }
        }
      }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "kind": { "const": "line-values" } } },
      "then": { "required": ["line"] }
    },
    {
      "if": { "properties": { "kind": { "const": "json-lines" } } },
      "then": { "required": ["json"] }
    },
    {
      "if": { "properties": { "kind": { "const": "tlv" } } },
      "then": { "required": ["tlv"] }
    }
  ]
}
