main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021 Project CHIP Authors
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <platform/CHIPDeviceLayer.h>
  18. #include <platform/PlatformManager.h>
  19. #include <app/clusters/diagnostic-logs-server/diagnostic-logs-server.h>
  20. #include <app/server/Server.h>
  21. #include <app/util/util.h>
  22. #include <credentials/DeviceAttestationCredsProvider.h>
  23. #include <credentials/examples/DeviceAttestationCredsExample.h>
  24. #include <lib/core/CHIPError.h>
  25. #include <lib/support/CHIPArgParser.hpp>
  26. #include <lib/support/CHIPMem.h>
  27. #include <lib/support/logging/CHIPLogging.h>
  28. #include <fstream>
  29. #include <iostream>
  30. #include <unistd.h>
  31. using chip::BitFlags;
  32. using chip::ArgParser::HelpOptions;
  33. using chip::ArgParser::OptionDef;
  34. using chip::ArgParser::OptionSet;
  35. using chip::ArgParser::PrintArgError;
  36. using chip::Messaging::ExchangeManager;
  37. bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
  38. {
  39. // No option yet
  40. return true;
  41. }
  42. OptionDef cmdLineOptionsDef[] = {
  43. {},
  44. };
  45. OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS" };
  46. HelpOptions helpOptions("log-source-app", "Usage: log-source-app [options]", "1.0");
  47. OptionSet * allOptions[] = { &cmdLineOptions, &helpOptions, nullptr };
  48. static constexpr size_t kMaxLogMessageLength = 512;
  49. DiagnosticLogsCommandHandler & GetLogProvider()
  50. {
  51. static DiagnosticLogsCommandHandler LogProvider;
  52. return LogProvider;
  53. }
  54. void ENFORCE_FORMAT(3, 0) LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args)
  55. {
  56. // Print the log on console for debug
  57. va_list argsCopy;
  58. va_copy(argsCopy, args);
  59. chip::Logging::Platform::LogV(module, category, msg, argsCopy);
  60. // Feed the log entry into the internal circular buffer
  61. char buffer1[kMaxLogMessageLength];
  62. char buffer2[kMaxLogMessageLength];
  63. int s1 = vsnprintf(buffer1, sizeof(buffer1), msg, args);
  64. int s2 = snprintf(buffer2, sizeof(buffer2), "%s:%.*s", module, s1, buffer1);
  65. size_t len = chip::CanCastTo<size_t>(s2) ? static_cast<size_t>(s2) : SIZE_MAX;
  66. GetLogProvider().PushLog(chip::ByteSpan(reinterpret_cast<uint8_t *>(buffer2), len));
  67. }
  68. int main(int argc, char * argv[])
  69. {
  70. if (chip::Platform::MemoryInit() != CHIP_NO_ERROR)
  71. {
  72. fprintf(stderr, "FAILED to initialize memory\n");
  73. return 1;
  74. }
  75. chip::Logging::SetLogRedirectCallback(&LoggingCallback);
  76. if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR)
  77. {
  78. fprintf(stderr, "FAILED to initialize chip stack\n");
  79. return 1;
  80. }
  81. if (!chip::ArgParser::ParseArgs(argv[0], argc, argv, allOptions))
  82. {
  83. return 1;
  84. }
  85. chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();
  86. static chip::CommonCaseDeviceServerInitParams initParams;
  87. (void) initParams.InitializeStaticResourcesBeforeServerInit();
  88. chip::Server::GetInstance().Init(initParams);
  89. // Initialize device attestation config
  90. SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());
  91. chip::app::InteractionModelEngine::GetInstance()->RegisterCommandHandler(&GetLogProvider());
  92. chip::DeviceLayer::PlatformMgr().RunEventLoop();
  93. return 0;
  94. }