Decouple and cleanup the legacy wml based protocols (#4292)

diff --git a/src/lib/protocols/common/CHIPMessage.h b/src/lib/protocols/common/CHIPMessage.h
deleted file mode 100644
index 128a9a0..0000000
--- a/src/lib/protocols/common/CHIPMessage.h
+++ /dev/null
@@ -1,350 +0,0 @@
-/*
- *
- *    Copyright (c) 2020 Project CHIP Authors
- *    Copyright (c) 2013-2017 Nest Labs, Inc.
- *    All rights reserved.
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-
-/**
- *    @file
- *      This file defines macros and objects commonly used for the
- *      processing of CHIP messages.
- *
- */
-
-#ifndef _CHIP_MESSAGE_H
-#define _CHIP_MESSAGE_H
-
-#include <core/CHIPEncoding.h>
-#include <core/CHIPError.h>
-#include <core/CHIPTLV.h>
-#include <support/DLLUtil.h>
-
-#include <system/SystemPacketBuffer.h>
-
-/*
- * these macros are the guts of the packing and parsing stuff and they're used
- * everywhere.
- *
- * here are the writers
- * parameters:
- * - PTR, a pointer into a buffer of type uint8_t
- * - VAL, a value to write
- */
-#define WRITEBYTE(PTR, VAL)                                                                                                        \
-    do                                                                                                                             \
-    {                                                                                                                              \
-        *(PTR)++ = (uint8_t)(VAL);                                                                                                 \
-    } while (0)
-
-#define WRITE16(PTR, VAL)                                                                                                          \
-    do                                                                                                                             \
-    {                                                                                                                              \
-        WRITEBYTE((PTR), ((VAL) >> 0));                                                                                            \
-        WRITEBYTE((PTR), ((VAL) >> 8));                                                                                            \
-    } while (0)
-
-#define WRITE32(PTR, VAL)                                                                                                          \
-    do                                                                                                                             \
-    {                                                                                                                              \
-        WRITEBYTE((PTR), ((VAL) >> 0));                                                                                            \
-        WRITEBYTE((PTR), ((VAL) >> 8));                                                                                            \
-        WRITEBYTE((PTR), ((VAL) >> 16));                                                                                           \
-        WRITEBYTE((PTR), ((VAL) >> 24));                                                                                           \
-    } while (0)
-
-/*
- * and the readers
- * parameter: PTR, a pointer into a buffer of type uint8_t
- * value: the value read form the buffer
- */
-#define READBYTE(PTR) (*(uint8_t *) (PTR)++)
-/*
- * parameters:
- * - PTR, a pointer into a buffer of type uint8_t
- * - DEST, where to put what's read from *p
- */
-#define READ16(PTR, DEST)                                                                                                          \
-    do                                                                                                                             \
-    {                                                                                                                              \
-        uint16_t __byte0 = (uint16_t) READBYTE(PTR);                                                                               \
-        uint16_t __byte1 = (uint16_t) READBYTE(PTR);                                                                               \
-        DEST             = __byte0 + (__byte1 << 8);                                                                               \
-    } while (0)
-
-#define READ32(PTR, DEST)                                                                                                          \
-    do                                                                                                                             \
-    {                                                                                                                              \
-        uint16_t __byte0 = (uint16_t) READBYTE(PTR);                                                                               \
-        uint16_t __byte1 = (uint16_t) READBYTE(PTR);                                                                               \
-        uint16_t __byte2 = (uint16_t) READBYTE(PTR);                                                                               \
-        uint16_t __byte3 = (uint16_t) READBYTE(PTR);                                                                               \
-        DEST             = __byte0 + (__byte1 << 8) + (__byte2 << 16) + (__byte3 << 24);                                           \
-    } while (0)
-
-/*
- * the message iterator class allows packet handling to operate in
- * a regular manner, do a bit of work, check the result, get out if
- * it's not good and so on. this results in a bunch of boilerplate
- * code, which is captured here.
- */
-#define TRY(OPERATION)                                                                                                             \
-    do                                                                                                                             \
-    {                                                                                                                              \
-        CHIP_ERROR e = (OPERATION);                                                                                                \
-        if (e != CHIP_NO_ERROR)                                                                                                    \
-            return e;                                                                                                              \
-    } while (0)
-
-#define RESCUE(ERR, OPERATION, OUT)                                                                                                \
-    do                                                                                                                             \
-    {                                                                                                                              \
-        ERR = (OPERATION);                                                                                                         \
-        if (ERR != CHIP_NO_ERROR)                                                                                                  \
-            goto OUT;                                                                                                              \
-    } while (0)
-
-namespace chip {
-
-namespace Protocols {
-
-/**
- *  @class RetainedPacketBuffer
- *
- *  @brief
- *    This is a base class that serves as a convenience object for
- *    automatically reference counting a System::PacketBuffer.
- *
- */
-class DLL_EXPORT RetainedPacketBuffer
-{
-public:
-    // Con/destructors
-    RetainedPacketBuffer();
-    RetainedPacketBuffer(const RetainedPacketBuffer & aRetainedPacketBuffer);
-    ~RetainedPacketBuffer();
-
-    RetainedPacketBuffer & operator=(const RetainedPacketBuffer & aRetainedPacketBuffer);
-
-    virtual bool IsRetaining() const;
-
-    void Retain(System::PacketBuffer * aBuffer);
-    virtual void Release();
-
-    inline System::PacketBuffer * GetBuffer() { return (mBuffer); }
-
-protected:
-    System::PacketBuffer * mBuffer; ///< A pointer to the retained packet buffer.
-};
-
-/**
- * In order to use message buffers sensibly, we define this iterator,
- * which can be used to keep track of boundaries and so on.
- */
-class DLL_EXPORT MessageIterator : public RetainedPacketBuffer
-{
-public:
-    // constructor
-    MessageIterator(System::PacketBuffer *);
-    // reading and writing
-    CHIP_ERROR readByte(uint8_t *);
-    CHIP_ERROR read16(uint16_t *);
-    CHIP_ERROR read32(uint32_t *);
-    CHIP_ERROR read64(uint64_t *);
-    CHIP_ERROR readString(uint16_t, char *);
-    CHIP_ERROR readBytes(uint16_t, uint8_t *);
-    CHIP_ERROR writeByte(uint8_t);
-    CHIP_ERROR write16(uint16_t);
-    CHIP_ERROR write32(uint32_t);
-    CHIP_ERROR write64(uint64_t);
-    CHIP_ERROR writeString(uint16_t, char *);
-    CHIP_ERROR writeBytes(uint16_t, uint8_t *);
-    // standard iterator operations
-    MessageIterator & operator++();
-    MessageIterator & operator+(uint16_t);
-    MessageIterator & operator-(uint16_t);
-    bool operator==(const MessageIterator &);
-    bool operator!=(const MessageIterator &);
-    uint8_t & operator*();
-    void append();
-    // size checking
-    bool hasData(uint16_t);
-    bool hasRoom(uint16_t);
-    // finishing
-    void finishWriting();
-    // data members
-    uint8_t * thePoint;
-};
-/**
- * Here's how to handle strings in CHIP. This class has 8-bit
- * and 16-bit variants.
- */
-class DLL_EXPORT ReferencedString : public RetainedPacketBuffer
-{
-public:
-    // constructor
-    ReferencedString();
-    // initializers
-    CHIP_ERROR init(uint16_t aLength, char * aString, System::PacketBuffer * aBuffer);
-    CHIP_ERROR init(uint16_t aLength, char * aString);
-    CHIP_ERROR init(uint8_t aLength, char * aString, System::PacketBuffer * aBuffer);
-    CHIP_ERROR init(uint8_t aLength, char * aString);
-    // pack and parse
-    CHIP_ERROR pack(MessageIterator &);
-    static CHIP_ERROR parse(MessageIterator &, ReferencedString &);
-    // comparison
-    bool operator==(const ReferencedString &) const;
-    // print string generation (for testing)
-    char * printString();
-    // data members
-    uint16_t theLength;
-    char * theString;
-    bool isShort;
-};
-/**
- * Similarly, we need to be able to represent a big old blob
- * of TLV data.
- */
-class DLL_EXPORT ReferencedTLVData : public RetainedPacketBuffer
-{
-public:
-    /*
-     * under certain circumstances, e.g. when we don't want to blow out the
-     * stack by writing some big thing into it in preparation for sending
-     * a message with a referenced extent in it, we want to only write the
-     * bits AFTER we've obtained the outgoing buffer. we do that using one
-     * of these.
-     *
-     * NOTE!!! the handler set up below does NOT return an error and, in
-     * the case where one is supplied, the pack method will not return an
-     * error either. this means that the NHL-supplied handler MUST handler
-     * all errors that arise from the formatting of TLV.
-     *
-     * parameters:
-     * - TLVWriter&, a TLV writer to use to write things out
-     * - void*, a state object known to the application
-     */
-
-    typedef void (*TLVWriteCallback)(TLV::TLVWriter & aWriter, void * aAppState);
-
-    // constructor
-
-    ReferencedTLVData();
-
-    // initializers
-
-    CHIP_ERROR init(System::PacketBuffer * aBuffer);
-    CHIP_ERROR init(MessageIterator & i);
-    CHIP_ERROR init(uint16_t aLength, uint16_t aMaxLength, uint8_t * aByteString);
-    CHIP_ERROR init(TLVWriteCallback aWriteCallback, void * anAppState);
-
-    /*
-     * ReferencedTLVData needs to override the free() and isFree()
-     * methods because "freedom", in this case, is more than nothing
-     * left to lose.
-     */
-
-    void free();
-
-    bool isFree();
-
-    /**
-     * Check if a ReferencedTLVData object has anything in it.
-     *
-     * There are two possible ways such an object could "have"
-     * something. Either it could have 0 length OR it could have no
-     * write callback.
-     *
-     * @return true if the data set has 0 length or there's no write
-     * callback in hand, false otherwise.
-     */
-
-    inline bool isEmpty() { return (theLength == 0 && theWriteCallback == nullptr); }
-
-    // packing and parsing
-
-    /**
-     * @fn CHIP_ERROR ReferencedTLVData::pack(System::PacketBuffer *buff)
-     *
-     * @brief Pack a ReferencedTLVData object directly into a PacketBuffer
-     *
-     * @param [in] buff the buffer to fill.
-     *
-     * @return a CHIP_ERROR reflecting the success of the underlying
-     * pack call.
-     */
-
-    inline CHIP_ERROR pack(System::PacketBuffer * buff)
-    {
-        MessageIterator i(buff);
-
-        return pack(i);
-    }
-
-    CHIP_ERROR pack(MessageIterator & i, uint32_t maxLen = 0xFFFFFFFFUL);
-
-    /**
-     * Return the data length assuming that the object has been packed
-     * into a buffer.
-     *
-     * @return the integer length of the packed data.
-     */
-
-    inline uint16_t packedLength() { return theLength; }
-
-    /**
-     * @fn CHIP_ERROR parse(System::PacketBuffer *buff, ReferencedTLVData &aTarget)
-     *
-     * @brief Parse a ReferencedTLVData object from a supplied PacketBuffer
-     *
-     * Parse a ReferencedTLVData object out of an inet buffer
-     * (assuming it just contains TLV).
-     *
-     * @param [in] buff the buffer to read from.
-     *
-     * @param [out] aTarget a ReferencedTLVData object to "fill" with the result.
-     *
-     * @return a CHIP_ERROR reflecting the success of the underlying
-     * parse call.
-     */
-
-    static inline CHIP_ERROR parse(System::PacketBuffer * buff, ReferencedTLVData & aTarget)
-    {
-        MessageIterator i(buff);
-
-        return parse(i, aTarget);
-    }
-
-    static CHIP_ERROR parse(MessageIterator & i, ReferencedTLVData & aTarget);
-
-    // comparison
-
-    bool operator==(const ReferencedTLVData &) const;
-
-    // data members
-
-    uint16_t theLength;
-    uint16_t theMaxLength;
-    uint8_t * theData;
-
-private:
-    TLVWriteCallback theWriteCallback;
-    void * theAppState;
-};
-
-} // namespace Protocols
-} // namespace chip
-
-#endif // _CHIP_MESSAGE_H
diff --git a/src/lib/protocols/common/CommonProtocol.h b/src/lib/protocols/common/CommonProtocol.h
deleted file mode 100644
index a81201d..0000000
--- a/src/lib/protocols/common/CommonProtocol.h
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- *
- *    Copyright (c) 2020 Project CHIP Authors
- *    Copyright (c) 2013-2017 Nest Labs, Inc.
- *    All rights reserved.
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-
-/**
- *    @file
- *      The defines constants for the CHIP Common Protocol, present in
- *      every CHIP device.
- *
- */
-
-#ifndef _COMMON_PROTOCOL_H
-#define _COMMON_PROTOCOL_H
-
-/**
- *   @namespace chip::Protocols::Common
- *
- *   @brief
- *     This namespace includes all interfaces within CHIP for the
- *     CHIP Common profile.
- *
- *     The interfaces define message types and status codes.
- */
-
-namespace chip {
-namespace Protocols {
-namespace Common {
-
-/**
- * Common Profile Message Types
- */
-enum
-{
-    kMsgType_StatusReport = 1,
-    kMsgType_Null         = 2,
-
-    // Reliable Messaging Protocol Message Types
-    kMsgType_RMP_Delayed_Delivery = 3,
-    kMsgType_RMP_Throttle_Flow    = 4
-};
-
-/**
- * Common Protocol Status Codes
- */
-enum
-{
-    kStatus_Success                = 0,      /**< The operation completed without error. */
-    kStatus_Canceled               = 1,      /**< The operation was canceled. */
-    kStatus_BadRequest             = 0x0010, /**< The request was unrecognized or malformed. */
-    kStatus_UnsupportedMessage     = 0x0011, /**< An unrecognized or unsupported message was received. */
-    kStatus_UnexpectedMessage      = 0x0012, /**< A message was received at an unexpected time or in an unexpected context. */
-    kStatus_AuthenticationRequired = 0x0013, /**< The request can only be made by an authenticated party. */
-    kStatus_AccessDenied           = 0x0014, /**< The request can only be made by a party with sufficient access. */
-    kStatus_OutOfMemory =
-        0x0017, /**< The sender is low on memory resources and cannot perform the requested operation at the current time. */
-    kStatus_NotAvailable       = 0x0018, /**< The requested operation cannot be performed given the current state of the sender. */
-    kStatus_LocalSetupRequired = 0x0019, /**< The requested operation could not be performed because one or more necessary local
-                                            configuration steps have not been completed. */
-    kStatus_InternalServerProblem = 0x0020, /**< Request could not be completeted because of problems on the server. */
-    kStatus_Relocated             = 0x0030, /**< Request was made to the wrong endpoint. Client should query its
-                                                 directory server for an updated endpoint list and try again. */
-    kStatus_Busy          = 0x0040, /**< The sender is busy and cannot perform the requested operation at the current time. */
-    kStatus_Timeout       = 0x0041, /**< The operation or protocol interaction failed to complete in the allotted time. */
-    kStatus_InternalError = 0x0050, /**< An internal failure prevented an operation from completing. */
-    kStatus_Continue      = 0x0090, /**< Context-specific signal to proceed. */
-};
-
-/**
- * Common Protocol MIME file type
- */
-enum
-{
-    kMIMEType_AppOctetStream = 0x0000, /**< The most general unrestricted set of bytes, same as "unspecified." */
-    kMIMEType_TextPlain      = 0x0001, /**< Plain text, uncompressed. */
-    kMIMEType_AppGzip        = 0x0002, /**< gzip-compressed data. */
-};
-
-/**
- * @var   kTag_SystemErrorCode
- * @brief [uint] System-specific error.
- *
- * Tag Category: Protocol-specific.  Constraints: none
- *
- * @var   kTag_SchemaVersion
- * @brief [uint] Schema revision.
- *
- * Tag Category: Protocol-specific.  Constraints: 0-255
- *
- * @var   kTag_MIMEType
- * @brief [uint] MIME type.
- *
- * Tag Category: Protocol-agnostic.  Constraints: none
- */
-
-/**
- * Common Protocol Data Tags
- */
-enum
-{
-    //                                      Value       Tag Category      Element Type      Constraints  Description
-    //                                      ------------------------------------------------------------------------
-    kTag_SystemErrorCode = 0x0001, // Protocol-specific  Unsigned Integer  -            System-specific error
-    kTag_SchemaVersion   = 0x0002, // Protocol-specific  Unsigned Integer  0-255        Schema revision
-    kTag_MIMEType        = 0x0004, // Protocol-agnostic  Unsigned Integer  -            MIME type
-};
-
-} // namespace Common
-} // namespace Protocols
-} // namespace chip
-
-#endif // _COMMON_PROTOCOL_H
diff --git a/src/messaging/ExchangeContext.cpp b/src/messaging/ExchangeContext.cpp
index b111d23..ee1a91d 100644
--- a/src/messaging/ExchangeContext.cpp
+++ b/src/messaging/ExchangeContext.cpp
@@ -37,7 +37,7 @@
 #include <messaging/ExchangeContext.h>
 #include <messaging/ExchangeMgr.h>
 #include <protocols/Protocols.h>
-#include <protocols/common/CommonProtocol.h>
+#include <protocols/secure_channel/SecureChannelProtocol.h>
 #include <support/logging/CHIPLogging.h>
 #include <system/SystemTimer.h>
 
@@ -394,8 +394,9 @@
         SuccessOrExit(err);
     }
 
-    //  The Common::Null message type is only used for CRMP; do not pass such messages to the application layer.
-    if ((protocolId == Protocols::kProtocol_Protocol_Common) && (messageType == Protocols::Common::kMsgType_Null))
+    //  The SecureChannel::StandaloneAck message type is only used for CRMP; do not pass such messages to the application layer.
+    if ((protocolId == Protocols::kProtocol_SecureChannel) &&
+        (messageType == static_cast<uint8_t>(Protocols::SecureChannel::MsgType::StandaloneAck)))
     {
         ExitNow(err = CHIP_NO_ERROR);
     }
diff --git a/src/messaging/ExchangeMgr.cpp b/src/messaging/ExchangeMgr.cpp
index 113ec2f..4e19d4d 100644
--- a/src/messaging/ExchangeMgr.cpp
+++ b/src/messaging/ExchangeMgr.cpp
@@ -38,7 +38,6 @@
 #include <messaging/ExchangeContext.h>
 #include <messaging/ExchangeMgr.h>
 #include <protocols/Protocols.h>
-#include <protocols/common/CommonProtocol.h>
 #include <support/CHIPFaultInjection.h>
 #include <support/CodeUtils.h>
 #include <support/RandUtils.h>
diff --git a/src/messaging/ReliableMessageContext.cpp b/src/messaging/ReliableMessageContext.cpp
index 922d40b..46b2366 100644
--- a/src/messaging/ReliableMessageContext.cpp
+++ b/src/messaging/ReliableMessageContext.cpp
@@ -31,7 +31,7 @@
 #include <messaging/Flags.h>
 #include <messaging/ReliableMessageManager.h>
 #include <protocols/Protocols.h>
-#include <protocols/common/CommonProtocol.h>
+#include <protocols/secure_channel/SecureChannelProtocol.h>
 #include <support/CodeUtils.h>
 
 namespace chip {
@@ -193,8 +193,8 @@
 
     if (IsAckPending())
     {
-        // Send the acknowledgment as a Common::Null message
-        err = SendCommonNullMessage();
+        // Send the acknowledgment as a SecureChannel::StandStandaloneAck message
+        err = SendStandaloneAckMessage();
 
         if (err == CHIP_NO_ERROR)
         {
@@ -292,8 +292,8 @@
         // Set the pending ack id.
         mPendingPeerAckId = MessageId;
 
-        // Send the Ack for the duplication message in a Common::Null message.
-        err = SendCommonNullMessage();
+        // Send the Ack for the duplication message in a SecureChannel::StandaloneAck message.
+        err = SendStandaloneAckMessage();
 
         // If there was pending ack for a different message id.
         if (wasAckPending)
@@ -314,8 +314,8 @@
             ChipLogProgress(ExchangeManager, "Pending ack queue full; forcing tx of solitary ack for MsgId:%08" PRIX32,
                             mPendingPeerAckId);
 #endif
-            // Send the Ack for the currently pending Ack in a Common::Null message.
-            err = SendCommonNullMessage();
+            // Send the Ack for the currently pending Ack in a SecureChannel::StandaloneAck message.
+            err = SendStandaloneAckMessage();
             SuccessOrExit(err);
         }
 
@@ -364,7 +364,7 @@
 }
 
 /**
- *  Send a Common::Null message.
+ *  Send a SecureChannel::StandaloneAck message.
  *
  *  @note  When sent via UDP, the null message is sent *without* requesting an acknowledgment,
  *  even in the case where the auto-request acknowledgment feature has been enabled on the
@@ -375,7 +375,7 @@
  *  @retval  other                    Another critical error returned by SendMessage().
  *
  */
-CHIP_ERROR ReliableMessageContext::SendCommonNullMessage()
+CHIP_ERROR ReliableMessageContext::SendStandaloneAckMessage()
 {
     CHIP_ERROR err = CHIP_NO_ERROR;
 
@@ -386,7 +386,8 @@
     // Send the null message
     if (mExchange != nullptr)
     {
-        err = mExchange->SendMessage(Protocols::kProtocol_Protocol_Common, Protocols::Common::kMsgType_Null, std::move(msgBuf),
+        err = mExchange->SendMessage(Protocols::kProtocol_SecureChannel,
+                                     static_cast<uint8_t>(Protocols::SecureChannel::MsgType::StandaloneAck), std::move(msgBuf),
                                      BitFlags<uint16_t, SendMessageFlags>{ SendMessageFlags::kSendFlag_NoAutoRequestAck });
     }
     else
diff --git a/src/messaging/ReliableMessageContext.h b/src/messaging/ReliableMessageContext.h
index 6f4d798..43beaf2 100644
--- a/src/messaging/ReliableMessageContext.h
+++ b/src/messaging/ReliableMessageContext.h
@@ -68,7 +68,7 @@
     uint64_t GetPeerNodeId();
     uint64_t GetCurrentRetransmitTimeoutTick();
 
-    CHIP_ERROR SendCommonNullMessage();
+    CHIP_ERROR SendStandaloneAckMessage();
 
     bool AutoRequestAck() const;
     void SetAutoRequestAck(bool autoReqAck);
diff --git a/src/messaging/ReliableMessageManager.cpp b/src/messaging/ReliableMessageManager.cpp
index f0e46fb..74b1831 100644
--- a/src/messaging/ReliableMessageManager.cpp
+++ b/src/messaging/ReliableMessageManager.cpp
@@ -132,8 +132,8 @@
 #if defined(RMP_TICKLESS_DEBUG)
                 ChipLogProgress(ExchangeManager, "ReliableMessageManager::ExecuteActions sending ACK");
 #endif
-                // Send the Ack in a Common::Null message
-                rc->SendCommonNullMessage();
+                // Send the Ack in a SecureChannel::StandaloneAck message
+                rc->SendStandaloneAckMessage();
                 rc->SetAckPending(false);
             }
         }