Cleanup IM message def (#5967)

Summary of Changes:
--Remove extra header check in all im message def headers.
--Run im schema check only under CheckSchemaValidity in debug linux/mac build
--Remove redundant schema check
diff --git a/src/app/BUILD.gn b/src/app/BUILD.gn
index 9783175..e7cc61d 100644
--- a/src/app/BUILD.gn
+++ b/src/app/BUILD.gn
@@ -16,12 +16,23 @@
 import("//build_overrides/nlio.gni")
 import("common_flags.gni")
 
+declare_args() {
+  # Enable strict schema checks.
+  chip_enable_schema_check =
+      is_debug && (current_os == "linux" || current_os == "mac")
+}
+
 config("app_config") {
   include_dirs = [
     "util",
     ".",
     "${target_gen_dir}/include",
   ]
+  if (chip_enable_schema_check) {
+    defines = [ "CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK=1" ]
+  } else {
+    defines = [ "CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK=0" ]
+  }
 }
 
 static_library("app") {
diff --git a/src/app/Command.cpp b/src/app/Command.cpp
index d1d43e4..2e7c1bc 100644
--- a/src/app/Command.cpp
+++ b/src/app/Command.cpp
@@ -93,9 +93,10 @@
     err = invokeCommandParser.Init(reader);
     SuccessOrExit(err);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = invokeCommandParser.CheckSchemaValidity();
     SuccessOrExit(err);
-
+#endif
     err = invokeCommandParser.GetCommandList(&commandListParser);
     SuccessOrExit(err);
 
@@ -111,9 +112,6 @@
         err = commandElement.Init(commandListReader);
         SuccessOrExit(err);
 
-        err = commandElement.CheckSchemaValidity();
-        SuccessOrExit(err);
-
         err = ProcessCommandDataElement(commandElement);
         SuccessOrExit(err);
     }
diff --git a/src/app/CommandSender.cpp b/src/app/CommandSender.cpp
index aeecbf4..161ff74 100644
--- a/src/app/CommandSender.cpp
+++ b/src/app/CommandSender.cpp
@@ -148,9 +148,6 @@
     err = aCommandElement.GetStatusElement(&statusElementParser);
     if (CHIP_NO_ERROR == err)
     {
-        // Response has status element since either there is error in command response or it is empty response
-        err = statusElementParser.CheckSchemaValidity();
-        SuccessOrExit(err);
         err = statusElementParser.DecodeStatusElement(&generalCode, &protocolId, &protocolCode);
         SuccessOrExit(err);
         if (mpDelegate != nullptr)
diff --git a/src/app/MessageDef/AttributeDataElement.h b/src/app/MessageDef/AttributeDataElement.h
index e72a5b7..8294fd6 100644
--- a/src/app/MessageDef/AttributeDataElement.h
+++ b/src/app/MessageDef/AttributeDataElement.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_ELEMENT_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_ELEMENT_H
-
 #include "AttributePath.h"
 #include "Builder.h"
 #include "Parser.h"
@@ -58,6 +55,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -72,6 +70,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the AttributePath. Next() must be called before accessing them.
@@ -174,5 +173,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_ELEMENT_H
diff --git a/src/app/MessageDef/AttributeDataList.h b/src/app/MessageDef/AttributeDataList.h
index f88d0ad..793cc30 100644
--- a/src/app/MessageDef/AttributeDataList.h
+++ b/src/app/MessageDef/AttributeDataList.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_LIST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_LIST_H
-
 #include "AttributeDataElement.h"
 #include "ListBuilder.h"
 #include "ListParser.h"
@@ -41,6 +38,7 @@
 class Parser : public ListParser
 {
 public:
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -55,6 +53,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 };
 
 class Builder : public ListBuilder
@@ -81,5 +80,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_LIST_H
diff --git a/src/app/MessageDef/AttributeDataVersionList.h b/src/app/MessageDef/AttributeDataVersionList.h
index 6ed49b3..dcaf76e 100644
--- a/src/app/MessageDef/AttributeDataVersionList.h
+++ b/src/app/MessageDef/AttributeDataVersionList.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_VERSION_LIST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_VERSION_LIST_H
-
 #include "AttributeDataElement.h"
 #include "ListBuilder.h"
 #include "ListParser.h"
@@ -42,6 +39,7 @@
 class Parser : public ListParser
 {
 public:
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -56,6 +54,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Check if this element is valid
@@ -112,5 +111,3 @@
 }; // namespace AttributeDataVersionList
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_VERSION_LIST_H
diff --git a/src/app/MessageDef/AttributePath.h b/src/app/MessageDef/AttributePath.h
index d06d473..c9fac4d 100644
--- a/src/app/MessageDef/AttributePath.h
+++ b/src/app/MessageDef/AttributePath.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_PATH_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_PATH_H
-
 #include "Builder.h"
 #include "Parser.h"
 #include <core/CHIPCore.h>
@@ -58,6 +55,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -72,7 +70,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
-
+#endif
     /**
      *  @brief Get a TLVReader for the NodeId. Next() must be called before accessing them.
      *
@@ -212,5 +210,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_PATH_H
diff --git a/src/app/MessageDef/AttributePathList.h b/src/app/MessageDef/AttributePathList.h
index 438a061..a20769c 100644
--- a/src/app/MessageDef/AttributePathList.h
+++ b/src/app/MessageDef/AttributePathList.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_PATH_LIST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_PATH_LIST_H
-
 #include "AttributePath.h"
 #include "ListBuilder.h"
 #include "ListParser.h"
@@ -42,6 +39,7 @@
 class Parser : public ListParser
 {
 public:
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -56,6 +54,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 };
 
 class Builder : public ListBuilder
@@ -81,5 +80,3 @@
 }; // namespace AttributePathList
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_PATH_LIST_H
diff --git a/src/app/MessageDef/AttributeStatusElement.h b/src/app/MessageDef/AttributeStatusElement.h
index 6cb7a74..e18b1e8 100644
--- a/src/app/MessageDef/AttributeStatusElement.h
+++ b/src/app/MessageDef/AttributeStatusElement.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_STATUS_ELEMENT_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_STATUS_ELEMENT_H
-
 #include "AttributePath.h"
 #include "Builder.h"
 #include "Parser.h"
@@ -94,7 +91,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -109,6 +106,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the AttributePath. Next() must be called before accessing them.
@@ -136,5 +134,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_STATUS_ELEMENT_H
diff --git a/src/app/MessageDef/AttributeStatusList.cpp b/src/app/MessageDef/AttributeStatusList.cpp
index e6643c4..bb73350 100644
--- a/src/app/MessageDef/AttributeStatusList.cpp
+++ b/src/app/MessageDef/AttributeStatusList.cpp
@@ -53,7 +53,7 @@
     EndOfContainer();
     return *this;
 }
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
 CHIP_ERROR AttributeStatusList::Parser::CheckSchemaValidity() const
 {
     CHIP_ERROR err                  = CHIP_NO_ERROR;
@@ -108,5 +108,6 @@
 
     return err;
 }
+#endif
 }; // namespace app
 }; // namespace chip
diff --git a/src/app/MessageDef/AttributeStatusList.h b/src/app/MessageDef/AttributeStatusList.h
index 171eda8..576562a 100644
--- a/src/app/MessageDef/AttributeStatusList.h
+++ b/src/app/MessageDef/AttributeStatusList.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_STATUS_LIST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_STATUS_LIST_H
-
 #include "AttributeStatusElement.h"
 #include "ListBuilder.h"
 #include "ListParser.h"
@@ -63,6 +60,7 @@
 class Parser : public ListParser
 {
 public:
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -77,10 +75,9 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 };
 }; // namespace AttributeStatusList
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_STATUS_LIST_H
diff --git a/src/app/MessageDef/Builder.h b/src/app/MessageDef/Builder.h
index cd35e02..2817a95 100644
--- a/src/app/MessageDef/Builder.h
+++ b/src/app/MessageDef/Builder.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_BUILDER_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_BUILDER_H
-
 #include <core/CHIPCore.h>
 #include <core/CHIPTLV.h>
 #include <support/CodeUtils.h>
@@ -100,5 +97,3 @@
 };
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_BUILDER_H
diff --git a/src/app/MessageDef/CommandDataElement.h b/src/app/MessageDef/CommandDataElement.h
index d941013..2da5007 100644
--- a/src/app/MessageDef/CommandDataElement.h
+++ b/src/app/MessageDef/CommandDataElement.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_DATA_ELEMENT_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_DATA_ELEMENT_H
-
 #include "Builder.h"
 #include "CommandPath.h"
 
@@ -59,6 +56,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -73,6 +71,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the CommandPath. Next() must be called before accessing them.
@@ -151,5 +150,3 @@
 }; // namespace CommandDataElement
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_DATA_ELEMENT_H
diff --git a/src/app/MessageDef/CommandList.h b/src/app/MessageDef/CommandList.h
index dc55b13..bad57b0 100644
--- a/src/app/MessageDef/CommandList.h
+++ b/src/app/MessageDef/CommandList.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_LIST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_LIST_H
-
 #include "CommandDataElement.h"
 #include "ListBuilder.h"
 #include "ListParser.h"
@@ -42,6 +39,7 @@
 class Parser : public ListParser
 {
 public:
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -56,6 +54,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 };
 
 class Builder : public ListBuilder
@@ -81,5 +80,3 @@
 }; // namespace CommandList
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_LIST_H
diff --git a/src/app/MessageDef/CommandPath.h b/src/app/MessageDef/CommandPath.h
index 4a0ffdf..6e7dac4 100644
--- a/src/app/MessageDef/CommandPath.h
+++ b/src/app/MessageDef/CommandPath.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_PATH_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_PATH_H
-
 #include "Builder.h"
 #include "Parser.h"
 #include <core/CHIPCore.h>
@@ -57,6 +54,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -71,6 +69,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the EndpointId. Next() must be called before accessing them.
@@ -190,5 +189,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_PATH_H
diff --git a/src/app/MessageDef/EventDataElement.h b/src/app/MessageDef/EventDataElement.h
index 6f25594..f389c36 100644
--- a/src/app/MessageDef/EventDataElement.h
+++ b/src/app/MessageDef/EventDataElement.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_DATA_ELEMENT_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_DATA_ELEMENT_H
-
 #include "Builder.h"
 #include "EventPath.h"
 
@@ -63,6 +60,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -77,6 +75,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the EventPath. Next() must be called before accessing them.
@@ -265,5 +264,3 @@
 }; // namespace EventDataElement
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_DATA_ELEMENT_H
diff --git a/src/app/MessageDef/EventList.h b/src/app/MessageDef/EventList.h
index e7b1b39..945d2d9 100644
--- a/src/app/MessageDef/EventList.h
+++ b/src/app/MessageDef/EventList.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_LIST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_LIST_H
-
 #include "EventDataElement.h"
 #include "ListBuilder.h"
 #include "ListParser.h"
@@ -42,6 +39,7 @@
 class Parser : public ListParser
 {
 public:
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -56,6 +54,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 };
 
 class Builder : public ListBuilder
@@ -82,5 +81,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_LIST_H
diff --git a/src/app/MessageDef/EventPath.h b/src/app/MessageDef/EventPath.h
index de6e3a1..d16313c 100644
--- a/src/app/MessageDef/EventPath.h
+++ b/src/app/MessageDef/EventPath.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_PATH_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_PATH_H
-
 #include "Builder.h"
 #include "Parser.h"
 #include <core/CHIPCore.h>
@@ -57,6 +54,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -71,6 +69,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the NodeId. Next() must be called before accessing them.
@@ -189,5 +188,3 @@
 }; // namespace EventPath
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_PATH_H
diff --git a/src/app/MessageDef/EventPathList.h b/src/app/MessageDef/EventPathList.h
index 1ccd3ed..bde8a0d 100644
--- a/src/app/MessageDef/EventPathList.h
+++ b/src/app/MessageDef/EventPathList.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_PATH_LIST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_PATH_LIST_H
-
 #include "EventPath.h"
 #include "EventPathList.h"
 #include "ListBuilder.h"
@@ -43,6 +40,7 @@
 class Parser : public ListParser
 {
 public:
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -57,6 +55,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 };
 
 class Builder : public ListBuilder
@@ -82,5 +81,3 @@
 }; // namespace EventPathList
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_EVENT_PATH_LIST_H
diff --git a/src/app/MessageDef/InvokeCommand.h b/src/app/MessageDef/InvokeCommand.h
index 29ded8b..48e5832 100644
--- a/src/app/MessageDef/InvokeCommand.h
+++ b/src/app/MessageDef/InvokeCommand.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_INVOKE_COMMAND_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_INVOKE_COMMAND_H
-
 #include <core/CHIPCore.h>
 #include <core/CHIPTLV.h>
 #include <support/CodeUtils.h>
@@ -57,6 +54,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -71,6 +69,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the CommandList. Next() must be called before accessing them.
@@ -122,5 +121,3 @@
 }; // namespace InvokeCommand
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_INVOKE_COMMAND_H
diff --git a/src/app/MessageDef/ListBuilder.h b/src/app/MessageDef/ListBuilder.h
index 4672b54..e7d8543 100644
--- a/src/app/MessageDef/ListBuilder.h
+++ b/src/app/MessageDef/ListBuilder.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_LIST_BUILDER_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_LIST_BUILDER_H
-
 #include "Builder.h"
 #include "Parser.h"
 #include <core/CHIPCore.h>
@@ -66,5 +63,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_LIST_BUILDER_H
diff --git a/src/app/MessageDef/ListParser.h b/src/app/MessageDef/ListParser.h
index 53ae4df..e890b3a 100644
--- a/src/app/MessageDef/ListParser.h
+++ b/src/app/MessageDef/ListParser.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_LIST_PARSER_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_LIST_PARSER_H
-
 #include "Builder.h"
 #include "Parser.h"
 #include <core/CHIPCore.h>
@@ -71,5 +68,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_LIST_PARSER_H
diff --git a/src/app/MessageDef/MessageDefHelper.h b/src/app/MessageDef/MessageDefHelper.h
index d72bf54..2572b05 100644
--- a/src/app/MessageDef/MessageDefHelper.h
+++ b/src/app/MessageDef/MessageDefHelper.h
@@ -23,37 +23,15 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_HELPER_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_HELPER_H
-
-// __STDC_FORMAT_MACROS must be defined for PRIX64 to be defined for pre-C++11 clib
-#ifndef __STDC_FORMAT_MACROS
-#define __STDC_FORMAT_MACROS
-#endif // __STDC_FORMAT_MACROS
-
-// __STDC_LIMIT_MACROS must be defined for UINT8_MAX and INT32_MAX to be defined for pre-C++11 clib
-#ifndef __STDC_LIMIT_MACROS
-#define __STDC_LIMIT_MACROS
-#endif // __STDC_LIMIT_MACROS
-
-// __STDC_CONSTANT_MACROS must be defined for INT64_C and UINT64_C to be defined for pre-C++11 clib
-#ifndef __STDC_CONSTANT_MACROS
-#define __STDC_CONSTANT_MACROS
-#endif // __STDC_CONSTANT_MACROS
-
 #include <algorithm>
 #include <inttypes.h>
 #include <stdarg.h>
 #include <stdio.h>
 
-#ifndef CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
-#define CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK 1
-#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
-
 namespace chip {
 namespace app {
 
-#if CHIP_DETAIL_LOGGING
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK && CHIP_DETAIL_LOGGING
 
 namespace {
 // this is used to run in signle thread for IM message debug purpose
@@ -138,16 +116,13 @@
 
     va_end(args);
 }
-#else // CHIP_DETAIL_LOGGING
+#else
 #define PRETTY_PRINT_CHECKPOINT()
 #define PRETTY_PRINT(fmt, ...)
 #define PRETTY_PRINT(fmt, ...)
 #define PRETTY_PRINT_SAMELINE(fmt, ...)
 #define PRETTY_PRINT_INCDEPTH()
 #define PRETTY_PRINT_DECDEPTH()
-#endif // CHIP_DETAIL_LOGGING
-
+#endif
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_HELPER_H
diff --git a/src/app/MessageDef/Parser.h b/src/app/MessageDef/Parser.h
index 5d157f8..22989fb 100644
--- a/src/app/MessageDef/Parser.h
+++ b/src/app/MessageDef/Parser.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_PARSER_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_PARSER_H
-
 #include <core/CHIPCore.h>
 #include <core/CHIPTLV.h>
 #include <support/CodeUtils.h>
@@ -100,5 +97,3 @@
 };
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_PARSER_H
diff --git a/src/app/MessageDef/ReadRequest.h b/src/app/MessageDef/ReadRequest.h
index 934941d..c1ba347 100644
--- a/src/app/MessageDef/ReadRequest.h
+++ b/src/app/MessageDef/ReadRequest.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_READ_REQUEST_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_READ_REQUEST_H
-
 #include "AttributeDataVersionList.h"
 #include "AttributePathList.h"
 #include "Builder.h"
@@ -60,7 +57,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -75,6 +72,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Get a TLVReader for the AttributePathList. Next() must be called before accessing them.
@@ -172,5 +170,3 @@
 }; // namespace ReadRequest
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_READ_REQUEST_H
diff --git a/src/app/MessageDef/ReportData.h b/src/app/MessageDef/ReportData.h
index 22a1f28..e9f8100 100644
--- a/src/app/MessageDef/ReportData.h
+++ b/src/app/MessageDef/ReportData.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_REPORT_DATA_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_REPORT_DATA_H
-
 #include <core/CHIPCore.h>
 #include <core/CHIPTLV.h>
 #include <support/CodeUtils.h>
@@ -63,6 +60,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -77,6 +75,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
      *  @brief Check whether a response (a StatusReponse specifically) is to be sent back to the request.
@@ -222,5 +221,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_REPORT_DATA_H
diff --git a/src/app/MessageDef/StatusElement.h b/src/app/MessageDef/StatusElement.h
index c23c689..b3ccc57 100644
--- a/src/app/MessageDef/StatusElement.h
+++ b/src/app/MessageDef/StatusElement.h
@@ -23,9 +23,6 @@
 
 #pragma once
 
-#ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_STATUS_ELEMENT_H
-#define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_STATUS_ELEMENT_H
-
 #include "ListBuilder.h"
 #include "ListParser.h"
 
@@ -59,6 +56,7 @@
      */
     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     /**
      *  @brief Roughly verify the message is correctly formed
      *   1) all mandatory tags are present
@@ -73,6 +71,7 @@
      *  @return #CHIP_NO_ERROR on success
      */
     CHIP_ERROR CheckSchemaValidity() const;
+#endif
 
     /**
     `* Read the GeneralCode, ProtocolId, ProtocolCode, ClusterId
@@ -138,5 +137,3 @@
 
 }; // namespace app
 }; // namespace chip
-
-#endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_STATUS_ELEMENT_H
diff --git a/src/app/ReadClient.cpp b/src/app/ReadClient.cpp
index 98ab563..c4fd9583 100644
--- a/src/app/ReadClient.cpp
+++ b/src/app/ReadClient.cpp
@@ -183,8 +183,10 @@
     err = report.Init(reader);
     SuccessOrExit(err);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = report.CheckSchemaValidity();
     SuccessOrExit(err);
+#endif
 
     err = report.GetSuppressResponse(&suppressResponse);
     if (CHIP_END_OF_TLV == err)
diff --git a/src/app/ReadHandler.cpp b/src/app/ReadHandler.cpp
index b7e3286..81aa1be 100644
--- a/src/app/ReadHandler.cpp
+++ b/src/app/ReadHandler.cpp
@@ -112,9 +112,10 @@
     err = readRequestParser.Init(reader);
     SuccessOrExit(err);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = readRequestParser.CheckSchemaValidity();
     SuccessOrExit(err);
-
+#endif
     err = readRequestParser.GetEventPathList(&eventPathListParser);
     if (err == CHIP_END_OF_TLV)
     {
diff --git a/src/app/tests/TestMessageDef.cpp b/src/app/tests/TestMessageDef.cpp
index 0b53c06..98d089a 100644
--- a/src/app/tests/TestMessageDef.cpp
+++ b/src/app/tests/TestMessageDef.cpp
@@ -85,10 +85,10 @@
 
     err = attributePathParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = attributePathParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = attributePathParser.GetNodeId(&nodeId);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR && nodeId == 1);
 
@@ -124,8 +124,10 @@
     err = attributePathListParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = attributePathListParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+#endif
 }
 
 void BuildEventPath(nlTestSuite * apSuite, EventPath::Builder & aEventPathBuilder)
@@ -144,9 +146,10 @@
     chip::ClusterId clusterId   = 3;
     chip::EventId eventId       = 4;
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = aEventPathParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = aEventPathParser.GetNodeId(&nodeId);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR && nodeId == 1);
 
@@ -177,9 +180,10 @@
 
     err = eventPathListParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = eventPathListParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+#endif
 }
 
 void BuildCommandPath(nlTestSuite * apSuite, CommandPath::Builder & aCommandPathBuilder)
@@ -199,9 +203,10 @@
     err = commandPathParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = commandPathParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = commandPathParser.GetEndpointId(&endpointId);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR && endpointId == 1);
 
@@ -256,9 +261,10 @@
     uint64_t deltaUTCTimestamp    = 0;
     uint64_t deltaSystemTimestamp = 0;
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = aEventDataElementParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     {
         {
             EventPath::Parser eventPath;
@@ -315,9 +321,10 @@
 
     err = eventListParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = eventListParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+#endif
 }
 
 void BuildStatusElement(nlTestSuite * apSuite, StatusElement::Builder & aStatusElementBuilder)
@@ -339,9 +346,10 @@
     uint32_t protocolId                                           = 0;
     uint16_t protocolCode                                         = 0;
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = aStatusElementParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = aStatusElementParser.DecodeStatusElement(&generalCode, &protocolId, &protocolCode);
     NL_TEST_ASSERT(apSuite,
                    err == CHIP_NO_ERROR &&
@@ -370,9 +378,10 @@
     AttributePath::Parser attributePathParser;
     StatusElement::Parser statusElementParser;
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = aAttributeStatusElementParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = aAttributeStatusElementParser.GetAttributePath(&attributePathParser);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
@@ -398,8 +407,10 @@
     err = attributeStatusParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = attributeStatusParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+#endif
 }
 
 void BuildAttributeDataElement(nlTestSuite * apSuite, AttributeDataElement::Builder & aAttributeDataElementBuilder)
@@ -444,10 +455,10 @@
     StatusElement::Parser statusElementParser;
     chip::DataVersion version = 0;
     bool moreClusterDataFlag  = false;
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = aAttributeDataElementParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = aAttributeDataElementParser.GetAttributePath(&attributePathParser);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
@@ -493,9 +504,10 @@
 
     err = attributeDataListParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = attributeDataListParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+#endif
 }
 
 void BuildAttributeDataVersionList(nlTestSuite * apSuite, AttributeDataVersionList::Builder & aAttributeDataVersionListBuilder)
@@ -515,9 +527,10 @@
     err = attributeDataVersionListParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = attributeDataVersionListParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     attributeDataVersionListParser.GetVersion(&version);
 }
 
@@ -552,10 +565,10 @@
 {
     CHIP_ERROR err = CHIP_NO_ERROR;
     CommandPath::Parser commandPathParser;
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = aCommandDataElementParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = aCommandDataElementParser.GetCommandPath(&commandPathParser);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
@@ -597,9 +610,10 @@
     CHIP_ERROR err = CHIP_NO_ERROR;
     CommandPath::Parser commandPathParser;
     StatusElement::Parser statusElementParser;
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = aCommandDataElementParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = aCommandDataElementParser.GetCommandPath(&commandPathParser);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
@@ -621,9 +635,13 @@
 {
     CHIP_ERROR err = CHIP_NO_ERROR;
     CommandList::Parser commandListParser;
-    commandListParser.Init(aReader);
+    err = commandListParser.Init(aReader);
+    NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = commandListParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+#endif
 }
 
 void BuildReportData(nlTestSuite * apSuite, chip::TLV::TLVWriter & aWriter)
@@ -669,9 +687,10 @@
     bool moreChunkedMessages = false;
     reportDataParser.Init(aReader);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = reportDataParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = reportDataParser.GetSuppressResponse(&suppressResponse);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR && suppressResponse);
 
@@ -717,9 +736,10 @@
     err = invokeCommandParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = invokeCommandParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = invokeCommandParser.GetCommandList(&commandListParser);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 }
@@ -763,10 +783,10 @@
 
     err = readRequestParser.Init(aReader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
     err = readRequestParser.CheckSchemaValidity();
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-
+#endif
     err = readRequestParser.GetAttributePathList(&attributePathListParser);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
@@ -1248,8 +1268,11 @@
 
     err = attributeDataListParser.Init(reader);
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-    attributeDataListParser.CheckSchemaValidity();
 
+#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
+    err = attributeDataListParser.CheckSchemaValidity();
+    NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
+#endif
     while (CHIP_NO_ERROR == (err = attributeDataListParser.Next()))
     {
         ++NumDataElement;