/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { MatterDefinition } from "../MatterDefinition.js";
import {
  ClusterElement as Cluster,
  AttributeElement as Attribute,
  CommandElement as Command,
  FieldElement as Field,
  DatatypeElement as Datatype
} from "../../elements/index.js";
const GeneralCommissioning = Cluster(
  {
    name: "GeneralCommissioning",
    id: 48,
    classification: "node",
    pics: "CGEN",
    details: "This cluster is used to manage basic commissioning lifecycle.\nThis cluster also represents responsibilities related to commissioning that don\u2019t well fit other commissioning clusters, like Section 11.9, \u201CNetwork Commissioning Cluster\u201D. It also hosts functionalities those other clusters may depend on.",
    xref: { document: "core", section: "11.10" }
  },
  Attribute({ name: "ClusterRevision", id: 65533, type: "ClusterRevision", default: 1 }),
  Attribute({
    name: "Breadcrumb",
    id: 0,
    type: "uint64",
    access: "RW VA",
    conformance: "M",
    default: 0,
    details: "This attribute allows for the storage of a client-provided small payload which Administrators and Commissioners may write and then subsequently read, to keep track of their own progress. This may be used by the Commissioner to avoid repeating already-executed actions upon re-establishing a commissioning link after an error.\nOn start/restart of the server, such as when a device is power-cycled, this attribute shall be reset to zero.\nSome commands related to commissioning also have a side-effect of updating or resetting this attribute and this is specified in their respective functional descriptions.\nThe format of the value within this attribute is unspecified and its value is not otherwise used by the functioning of any cluster, other than being set as a side-effect of commands where this behavior is described.",
    xref: { document: "core", section: "11.10.5.1" }
  }),
  Attribute({
    name: "BasicCommissioningInfo",
    id: 1,
    type: "BasicCommissioningInfo",
    access: "R V",
    conformance: "M",
    constraint: "desc",
    quality: "F",
    details: "This attribute shall describe critical parameters needed at the beginning of commissioning flow. See BasicCommissioningInfo for more information.",
    xref: { document: "core", section: "11.10.5.2" }
  }),
  Attribute({
    name: "RegulatoryConfig",
    id: 2,
    type: "RegulatoryLocationTypeEnum",
    access: "R V",
    conformance: "M",
    default: { type: "reference", name: "LocationCapability" },
    details: "Indicates the regulatory configuration for the product.\nNote that the country code is part of Basic Information Cluster and therefore NOT listed on the RegulatoryConfig attribute.",
    xref: { document: "core", section: "11.10.5.3" }
  }),
  Attribute({
    name: "LocationCapability",
    id: 3,
    type: "RegulatoryLocationTypeEnum",
    access: "R V",
    conformance: "M",
    default: 2,
    quality: "F",
    details: 'LocationCapability is statically set by the manufacturer and indicates if this Node needs to be told an exact RegulatoryLocation. For example a Node which is "Indoor Only" would not be certified for outdoor use at all, and thus there is no need for a commissioner to set or ask the user about whether the device will be used inside or outside. However a device which states its capability is "Indoor/Outdoor" means it would like clarification if possible.\nFor Nodes without radio network interfaces (e.g. Ethernet-only devices), the value IndoorOutdoor shall always be used.\nThe default value of the RegulatoryConfig attribute is the value of LocationCapability attribute. This means devices always have a safe default value, and Commissioners which choose to implement smarter handling can.',
    xref: { document: "core", section: "11.10.5.4" }
  }),
  Attribute({
    name: "SupportsConcurrentConnection",
    id: 4,
    type: "bool",
    access: "R V",
    conformance: "M",
    default: true,
    quality: "F",
    details: 'Indicates whether this device supports "concurrent connection flow" commissioning mode (see Section 5.5, \u201CCommissioning Flows\u201D). If false, the device only supports "non-concurrent connection flow" mode.',
    xref: { document: "core", section: "11.10.5.5" }
  }),
  Command(
    {
      name: "ArmFailSafe",
      id: 0,
      access: "A",
      conformance: "M",
      direction: "request",
      response: "ArmFailSafeResponse",
      details: `Success or failure of this command shall be communicated by the ArmFailSafeResponse command, unless some data model validations caused a failure status code to be issued during the processing of the command.
If the fail-safe timer is not currently armed, the commissioning window is open, and the command was received over a CASE session, the command shall leave the current fail-safe state unchanged and immediately respond with an ArmFailSafeResponse containing an ErrorCode value of BusyWithOtherAdmin. This is done to allow commissioners, which use PASE connections, the opportunity to use the failsafe during the relatively short commissioning window.
Otherwise, the command shall arm or re-arm the "fail-safe timer" with an expiry time set for a duration of ExpiryLengthSeconds, or disarm it, depending on the situation:
  \u2022 If ExpiryLengthSeconds is 0 and the fail-safe timer was already armed and the accessing fabric     matches the Fabric currently associated with the fail-safe context, then the fail-safe timer
shall be immediately expired (see further below for side-effects of expiration).
  \u2022 If ExpiryLengthSeconds is 0 and the fail-safe timer was not armed, then this command invocation     shall lead to a success response with no side-effects against the fail-safe context.
  \u2022 If ExpiryLengthSeconds is non-zero and the fail-safe timer was not currently armed, then the     fail-safe timer shall be armed for that duration.
  \u2022 If ExpiryLengthSeconds is non-zero and the fail-safe timer was currently armed, and the     accessing Fabric matches the fail-safe context\u2019s associated Fabric, then the fail-safe timer     shall be re- armed to expire in ExpiryLengthSeconds.
  \u2022 Otherwise, the command shall leave the current fail-safe state unchanged and immediately     respond with ArmFailSafeResponse containing an ErrorCode value of BusyWithOtherAdmin,     indicating a likely conflict between commissioners.
The value of the Breadcrumb field shall be written to the Breadcrumb Attribute on successful execution of the command.
If the receiver restarts unexpectedly (e.g., power interruption, software crash, or other reset) the receiver shall behave as if the fail-safe timer expired and perform the sequence of clean-up steps listed below.
On successful execution of the command, the ErrorCode field of the ArmFailSafeResponse shall be set to OK.
### Fail Safe Context
When first arming the fail-safe timer, a 'Fail Safe Context' shall be created on the receiver, to track the following state information while the fail-safe is armed:
  \u2022 The fail-safe timer duration.
  \u2022 The state of all Network Commissioning Networks attribute configurations, to allow recovery of     connectivity after Fail-Safe expiry.
  \u2022 Whether an AddNOC command or UpdateNOC command has taken place.
  \u2022 A Fabric Index for the fabric-scoping of the context, starting at the accessing fabric index     for the ArmFailSafe command, and updated with the Fabric Index associated with an AddNOC     command or an UpdateNOC command being invoked successfully during the ongoing Fail-Safe timer     period.
  \u2022 The operational credentials associated with any Fabric whose configuration is affected by the     UpdateNOC command.
  \u2022 Optionally: the previous state of non-fabric-scoped data that is mutated during the fail-safe     period.
Note the following to assist in understanding the above state-keeping, which summarizes other normative requirements in the respective sections:
  \u2022 The AddNOC command can only be invoked once per contiguous non-expiring fail-safe timer period,     and only if no UpdateNOC command was previously processed within the same fail-safe timer     period.
  \u2022 The UpdateNOC command can only be invoked once per contiguous non-expiring fail-safe timer     period, can only be invoked over a CASE session, and only if no AddNOC command was previously     processed in the same fail-safe timer period.
On creation of the Fail Safe Context a second timer shall be created to expire at MaxCumulativeFailsafeSeconds as specified in BasicCommissioningInfo. This Cumulative Fail Safe Context timer (CFSC timer) serves to limit the lifetime of any particular Fail Safe Context; it shall NOT be extended or modified on subsequent invocations of ArmFailSafe associated with this Fail Safe Context. Upon expiry of the CFSC timer, the receiver shall execute cleanup behavior equivalent to that of fail-safe timer expiration as detailed in Section 11.10.6.2.2, \u201CBehavior on expiry of Fail-Safe timer\u201D. Termination of the session prior to the expiration of that timer for any reason (including a successful end of commissioning or an expiry of a fail-safe timer) shall also delete the CFSC timer.
### Behavior on expiry of Fail-Safe timer
If the fail-safe timer expires before the CommissioningComplete command is successfully invoked, the following sequence of clean-up steps shall be executed, in order, by the receiver:
  1. Terminate any open PASE secure session by clearing any associated Secure Session Context at       the Server.
  2. Revoke the temporary administrative privileges granted to any open PASE session (see Section       6.6.2.8, \u201CBootstrapping of the Access Control Cluster\u201D) at the Server.
  3. If an AddNOC or UpdateNOC command has been successfully invoked, terminate all CASE sessions       associated with the Fabric whose Fabric Index is recorded in the Fail-Safe context (see       Section 11.10.6.2, \u201CArmFailSafe Command\u201D) by clearing any associated Secure Session Context       at the Server.
  4. Reset the configuration of all Network Commissioning Networks attribute to their state prior       to the Fail-Safe being armed.
  5. If an UpdateNOC command had been successfully invoked, revert the state of operational key       pair, NOC and ICAC for that Fabric to the state prior to the Fail-Safe timer being armed, for       the Fabric Index that was the subject of the UpdateNOC command.
  6. If an AddNOC command had been successfully invoked, achieve the equivalent effect of invoking       the RemoveFabric command against the Fabric Index stored in the Fail-Safe Context for the       Fabric Index that was the subject of the AddNOC command. This shall remove all associations       to that Fabric including all fabric-scoped data, and may possibly factory-reset the device       depending on current device state. This shall only apply to Fabrics added during the       fail-safe period as the result of the AddNOC command.
  7. If the CSRRequest command had been successfully invoked, but no AddNOC or UpdateNOC command       had been successfully invoked, then the new operational key pair temporarily generated for       the purposes of NOC addition or update (see Node Operational CSR Procedure) shall be removed       as it is no longer needed.
  8. Remove any RCACs added by the AddTrustedRootCertificate command that are not currently       referenced by any entry in the Fabrics attribute.
  9. Reset the Breadcrumb attribute to zero.
  10. Optionally: if no factory-reset resulted from the previous steps, it is recommended that the
Node rollback the state of all non fabric-scoped data present in the Fail-Safe context.`,
      xref: { document: "core", section: "11.10.6.2" }
    },
    Field({ name: "ExpiryLengthSeconds", id: 0, type: "uint16", conformance: "M", default: 900 }),
    Field({ name: "Breadcrumb", id: 1, type: "uint64", conformance: "M" })
  ),
  Command(
    {
      name: "ArmFailSafeResponse",
      id: 1,
      conformance: "M",
      direction: "response",
      xref: { document: "core", section: "11.10.6.3" }
    },
    Field({
      name: "ErrorCode",
      id: 0,
      type: "CommissioningErrorEnum",
      conformance: "M",
      default: 0,
      details: "This field shall contain the result of the operation, based on the behavior specified in the functional description of the ArmFailSafe command.",
      xref: { document: "core", section: "11.10.6.3.1" }
    }),
    Field({
      name: "DebugText",
      id: 1,
      type: "string",
      conformance: "M",
      constraint: "max 128",
      default: "",
      details: "See Section 11.10.6.1, \u201CCommon fields in General Commissioning cluster responses\u201D.",
      xref: { document: "core", section: "11.10.6.3.2" }
    })
  ),
  Command(
    {
      name: "SetRegulatoryConfig",
      id: 2,
      access: "A",
      conformance: "M",
      direction: "request",
      response: "SetRegulatoryConfigResponse",
      details: "This shall add or update the regulatory configuration in the RegulatoryConfig Attribute to the value provided in the NewRegulatoryConfig field.\nSuccess or failure of this command shall be communicated by the SetRegulatoryConfigResponse command, unless some data model validations caused a failure status code to be issued during the processing of the command.\nThe CountryCode field shall conforms to ISO 3166-1 alpha-2 and shall be used to set the Location attribute reflected by the Basic Information Cluster.\nIf the server limits some of the values (e.g. locked to a particular country, with no regulatory data for others), then setting regulatory information outside a valid country or location shall still set the Location attribute reflected by the Basic Information Cluster configuration, but the SetRegulatoryConfigResponse replied shall have the ErrorCode field set to ValueOutsideRange error.\nIf the LocationCapability attribute is not Indoor/Outdoor and the NewRegulatoryConfig value received does not match either the Indoor or Outdoor fixed value in LocationCapability, then the SetRegulatoryConfigResponse replied shall have the ErrorCode field set to ValueOutsideRange error and the RegulatoryConfig attribute and associated internal radio configuration shall remain unchanged.\nIf the LocationCapability attribute is set to Indoor/Outdoor, then the RegulatoryConfig attribute shall be set to match the NewRegulatoryConfig field.\nOn successful execution of the command, the ErrorCode field of the SetRegulatoryConfigResponse shall be set to OK.\nThe Breadcrumb field shall be used to atomically set the Breadcrumb attribute on success of this command, when SetRegulatoryConfigResponse has the ErrorCode field set to OK. If the command fails, the Breadcrumb attribute shall be left unchanged.",
      xref: { document: "core", section: "11.10.6.4" }
    },
    Field({ name: "NewRegulatoryConfig", id: 0, type: "RegulatoryLocationTypeEnum", conformance: "M" }),
    Field({ name: "CountryCode", id: 1, type: "string", conformance: "M", constraint: "2" }),
    Field({ name: "Breadcrumb", id: 2, type: "uint64", conformance: "M" })
  ),
  Command(
    {
      name: "SetRegulatoryConfigResponse",
      id: 3,
      conformance: "M",
      direction: "response",
      xref: { document: "core", section: "11.10.6.5" }
    },
    Field({
      name: "ErrorCode",
      id: 0,
      type: "CommissioningErrorEnum",
      conformance: "M",
      default: 0,
      details: "This field shall contain the result of the operation, based on the behavior specified in the functional description of the SetRegulatoryConfig command.",
      xref: { document: "core", section: "11.10.6.5.1" }
    }),
    Field({
      name: "DebugText",
      id: 1,
      type: "string",
      conformance: "M",
      default: "",
      details: "See Section 11.10.6.1, \u201CCommon fields in General Commissioning cluster responses\u201D.",
      xref: { document: "core", section: "11.10.6.5.2" }
    })
  ),
  Command({
    name: "CommissioningComplete",
    id: 4,
    access: "F A",
    conformance: "M",
    direction: "request",
    response: "CommissioningCompleteResponse",
    details: "This command has no data.\nSuccess or failure of this command shall be communicated by the CommissioningCompleteResponse command, unless some data model validations caused a failure status code to be issued during the processing of the command.\nThis command signals the Server that the Commissioner or Administrator has successfully completed all steps needed during the Fail-Safe period, such as commissioning (see Section 5.5, \u201CCommissioning Flows\u201D) or other Administrator operations requiring usage of the Fail Safe timer. It ensures that the Server is configured in a state such that it still has all necessary elements to be fully operable within a Fabric, such as ACL entries (see Access Control Cluster) and operational credentials (see Section 6.4, \u201CNode Operational Credentials Specification\u201D), and that the Node is reach\nable using CASE (see Section 4.14.2, \u201CCertificate Authenticated Session Establishment (CASE)\u201D) over an operational network.\nAn ErrorCode of NoFailSafe shall be responded to the invoker if the CommissioningComplete command was received when no Fail-Safe context exists.\nThis command is fabric-scoped, so cannot be issued over a session that does not have an associated fabric, i.e. over PASE session prior to an AddNOC command. In addition, this command is only permitted over CASE and must be issued by a node associated with the ongoing Fail-Safe context. An ErrorCode of InvalidAuthentication shall be responded to the invoker if the CommissioningComplete command was received outside a CASE session (e.g., over Group messaging, or PASE session after AddNOC), or if the accessing fabric is not the one associated with the ongoing Fail-Safe context.\nThis command shall only result in success with an ErrorCode value of OK in the CommissioningCompleteResponse if received over a CASE session and the accessing fabric index matches the Fabric Index associated with the current Fail-Safe context. In other words:\n  \u2022 If no AddNOC command had been successfully invoked, the CommissioningComplete command must     originate from the Fabric that initiated the Fail-Safe context.\n  \u2022 After an AddNOC command has been successfully invoked, the CommissioningComplete command must     originate from the Fabric which was joined through the execution of that command, which updated     the Fail-Safe context\u2019s Fabric Index.\nOn successful execution of the CommissioningComplete command, where the CommissioningCompleteResponse has an ErrorCode of OK, the following actions shall be undertaken on the Server:\n  1. The Fail-Safe timer associated with the current Fail-Safe context shall be disarmed.\n  2. The commissioning window at the Server shall be closed.\n  3. Any temporary administrative privileges automatically granted to any open PASE session shall      be revoked (see Section 6.6.2.8, \u201CBootstrapping of the Access Control Cluster\u201D).\n  4. The Secure Session Context of any PASE session still established at the Server shall be      cleared.\n  5. The Breadcrumb attribute shall be reset to zero.\nAfter receipt of a CommissioningCompleteResponse with an ErrorCode value of OK, a client cannot expect any previously established PASE session to still be usable, due to the server having cleared such sessions.",
    xref: { document: "core", section: "11.10.6.6" }
  }),
  Command(
    {
      name: "CommissioningCompleteResponse",
      id: 5,
      conformance: "M",
      direction: "response",
      xref: { document: "core", section: "11.10.6.7" }
    },
    Field({
      name: "ErrorCode",
      id: 0,
      type: "CommissioningErrorEnum",
      conformance: "M",
      default: 0,
      details: "This field shall contain the result of the operation, based on the behavior specified in the functional description of the CommissioningComplete command.",
      xref: { document: "core", section: "11.10.6.7.1" }
    }),
    Field({
      name: "DebugText",
      id: 1,
      type: "string",
      conformance: "M",
      default: "",
      details: "See Section 11.10.6.1, \u201CCommon fields in General Commissioning cluster responses\u201D.",
      xref: { document: "core", section: "11.10.6.7.2" }
    })
  ),
  Datatype(
    {
      name: "CommissioningErrorEnum",
      type: "enum8",
      details: "This enumeration is used by several response commands in this cluster to indicate particular errors.",
      xref: { document: "core", section: "11.10.4.1" }
    },
    Field({ name: "Ok", id: 0, conformance: "M", description: "No error" }),
    Field({
      name: "ValueOutsideRange",
      id: 1,
      conformance: "M",
      description: "Attempting to set regulatory configuration to a region or indoor/outdoor mode for which the server does not have proper configuration."
    }),
    Field({
      name: "InvalidAuthentication",
      id: 2,
      conformance: "M",
      description: "Executed CommissioningComplete outside CASE session."
    }),
    Field({
      name: "NoFailSafe",
      id: 3,
      conformance: "M",
      description: "Executed CommissioningComplete when there was no active Fail-Safe context."
    }),
    Field({
      name: "BusyWithOtherAdmin",
      id: 4,
      conformance: "M",
      description: "Attempting to arm fail- safe or execute CommissioningComplete from a fabric different than the one associated with the current fail- safe context."
    })
  ),
  Datatype(
    {
      name: "RegulatoryLocationTypeEnum",
      type: "enum8",
      details: "This enumeration is used by the RegulatoryConfig and LocationCapability attributes to indicate possible radio usage.",
      xref: { document: "core", section: "11.10.4.2" }
    },
    Field({ name: "Indoor", id: 0, conformance: "M", description: "Indoor only" }),
    Field({ name: "Outdoor", id: 1, conformance: "M", description: "Outdoor only" }),
    Field({ name: "IndoorOutdoor", id: 2, conformance: "M", description: "Indoor/Outdoor" })
  ),
  Datatype(
    {
      name: "BasicCommissioningInfo",
      type: "struct",
      details: "This structure provides some constant values that may be of use to all commissioners.",
      xref: { document: "core", section: "11.10.4.3" }
    },
    Field({
      name: "FailSafeExpiryLengthSeconds",
      id: 0,
      type: "uint16",
      conformance: "M",
      details: "This field shall contain a conservative initial duration (in seconds) to set in the FailSafe for the commissioning flow to complete successfully. This may vary depending on the speed or sleepiness of the Commissionee. This value, if used in the ArmFailSafe command\u2019s ExpiryLengthSeconds field SHOULD allow a Commissioner to proceed with a nominal commissioning without having to-rearm the fail-safe, with some margin.",
      xref: { document: "core", section: "11.10.4.3.1" }
    }),
    Field({
      name: "MaxCumulativeFailsafeSeconds",
      id: 1,
      type: "uint16",
      conformance: "M",
      constraint: "desc",
      details: "This field shall contain a conservative value in seconds denoting the maximum total duration for which a fail safe timer can be re-armed. See Section 11.10.6.2.1, \u201CFail Safe Context\u201D.\nThe value of this field shall be greater than or equal to the FailSafeExpiryLengthSeconds. Absent additional guidelines, it is recommended that the value of this field be aligned with the initial Announcement Duration and default to 900 seconds.",
      xref: { document: "core", section: "11.10.4.3.2" }
    })
  )
);
MatterDefinition.children.push(GeneralCommissioning);
export {
  GeneralCommissioning
};
//# sourceMappingURL=GeneralCommissioning.js.map
