|
| 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 | + |
| 18 | +#include <platform/CHIPDeviceLayer.h> |
| 19 | +#include <platform/PlatformManager.h> |
| 20 | + |
| 21 | +#include <app/clusters/diagnostic-logs-server/diagnostic-logs-server.h> |
| 22 | +#include <app/server/Server.h> |
| 23 | +#include <app/util/util.h> |
| 24 | +#include <credentials/DeviceAttestationCredsProvider.h> |
| 25 | +#include <credentials/examples/DeviceAttestationCredsExample.h> |
| 26 | +#include <lib/core/CHIPError.h> |
| 27 | +#include <lib/support/CHIPArgParser.hpp> |
| 28 | +#include <lib/support/CHIPMem.h> |
| 29 | +#include <lib/support/logging/CHIPLogging.h> |
| 30 | + |
| 31 | +#include <fstream> |
| 32 | +#include <iostream> |
| 33 | +#include <unistd.h> |
| 34 | + |
| 35 | +using chip::BitFlags; |
| 36 | +using chip::ArgParser::HelpOptions; |
| 37 | +using chip::ArgParser::OptionDef; |
| 38 | +using chip::ArgParser::OptionSet; |
| 39 | +using chip::ArgParser::PrintArgError; |
| 40 | +using chip::Messaging::ExchangeManager; |
| 41 | + |
| 42 | +bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue) |
| 43 | +{ |
| 44 | + // No option yet |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
| 48 | +OptionDef cmdLineOptionsDef[] = { |
| 49 | + {}, |
| 50 | +}; |
| 51 | + |
| 52 | +OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS" }; |
| 53 | + |
| 54 | +HelpOptions helpOptions("log-source-app", "Usage: log-source-app [options]", "1.0"); |
| 55 | + |
| 56 | +OptionSet * allOptions[] = { &cmdLineOptions, &helpOptions, nullptr }; |
| 57 | + |
| 58 | +static constexpr size_t kMaxLogMessageLength = 512; |
| 59 | + |
| 60 | +DiagnosticLogsCommandHandler & GetLogProvider() |
| 61 | +{ |
| 62 | + static DiagnosticLogsCommandHandler LogProvider; |
| 63 | + return LogProvider; |
| 64 | +} |
| 65 | + |
| 66 | +void LoggingCallback(const char * module, uint8_t category, const char * msg, va_list args) |
| 67 | +{ |
| 68 | + // Print the log on console for debug |
| 69 | + va_list argsCopy; |
| 70 | + va_copy(argsCopy, args); |
| 71 | + chip::Logging::Platform::LogV(module, category, msg, argsCopy); |
| 72 | + |
| 73 | + // Feed the log entry into the internal circular buffer |
| 74 | + char buffer1[kMaxLogMessageLength]; |
| 75 | + char buffer2[kMaxLogMessageLength]; |
| 76 | + int s1 = vsnprintf(buffer1, sizeof(buffer1), msg, args); |
| 77 | + int s2 = snprintf(buffer2, sizeof(buffer2), "%s:%.*s", module, s1, buffer1); |
| 78 | + GetLogProvider().PushLog(chip::ByteSpan(reinterpret_cast<uint8_t *>(buffer2), s2)); |
| 79 | +} |
| 80 | + |
| 81 | +int main(int argc, char * argv[]) |
| 82 | +{ |
| 83 | + if (chip::Platform::MemoryInit() != CHIP_NO_ERROR) |
| 84 | + { |
| 85 | + fprintf(stderr, "FAILED to initialize memory\n"); |
| 86 | + return 1; |
| 87 | + } |
| 88 | + |
| 89 | + chip::Logging::SetLogRedirectCallback(&LoggingCallback); |
| 90 | + |
| 91 | + if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR) |
| 92 | + { |
| 93 | + fprintf(stderr, "FAILED to initialize chip stack\n"); |
| 94 | + return 1; |
| 95 | + } |
| 96 | + |
| 97 | + if (!chip::ArgParser::ParseArgs(argv[0], argc, argv, allOptions)) |
| 98 | + { |
| 99 | + return 1; |
| 100 | + } |
| 101 | + |
| 102 | + chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig(); |
| 103 | + chip::Server::GetInstance().Init(); |
| 104 | + |
| 105 | + // Initialize device attestation config |
| 106 | + SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider()); |
| 107 | + |
| 108 | + chip::app::InteractionModelEngine::GetInstance()->RegisterCommandHandler(&GetLogProvider()); |
| 109 | + |
| 110 | + chip::DeviceLayer::PlatformMgr().RunEventLoop(); |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
0 commit comments