|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2020 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <platform/CHIPDeviceLayer.h> |
| 20 | +#include <platform/PlatformManager.h> |
| 21 | + |
| 22 | +#include <app/clusters/ota-provider/ota-provider-delegate.h> |
| 23 | +#include <app/clusters/ota-provider/ota-provider.h> |
| 24 | +#include <app/server/Server.h> |
| 25 | +#include <app/util/util.h> |
| 26 | +#include <core/CHIPError.h> |
| 27 | +#include <support/CHIPArgParser.hpp> |
| 28 | +#include <support/CHIPMem.h> |
| 29 | +#include <support/RandUtils.h> |
| 30 | +#include <support/logging/CHIPLogging.h> |
| 31 | + |
| 32 | +#include "BdxOtaSender.h" |
| 33 | +#include "OTAProviderExample.h" |
| 34 | + |
| 35 | +#include <fstream> |
| 36 | +#include <iostream> |
| 37 | +#include <unistd.h> |
| 38 | + |
| 39 | +using chip::BitFlags; |
| 40 | +using chip::app::clusters::OTAProviderDelegate; |
| 41 | +using chip::ArgParser::HelpOptions; |
| 42 | +using chip::ArgParser::OptionDef; |
| 43 | +using chip::ArgParser::OptionSet; |
| 44 | +using chip::ArgParser::PrintArgError; |
| 45 | +using chip::bdx::TransferControlFlags; |
| 46 | +using chip::Messaging::ExchangeManager; |
| 47 | + |
| 48 | +// TODO: this should probably be done dynamically |
| 49 | +constexpr chip::EndpointId kOtaProviderEndpoint = 0; |
| 50 | + |
| 51 | +constexpr uint16_t kOptionFilepath = 'f'; |
| 52 | +const char * gOtaFilepath = nullptr; |
| 53 | + |
| 54 | +// Arbitrary BDX Transfer Params |
| 55 | +constexpr uint32_t kMaxBdxBlockSize = 1024; |
| 56 | +constexpr uint32_t kBdxTimeoutMs = 5 * 60 * 1000; // OTA Spec mandates >= 5 minutes |
| 57 | +constexpr uint32_t kBdxPollFreqMs = 500; |
| 58 | + |
| 59 | +bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue) |
| 60 | +{ |
| 61 | + bool retval = true; |
| 62 | + |
| 63 | + switch (aIdentifier) |
| 64 | + { |
| 65 | + case kOptionFilepath: |
| 66 | + if (0 != access(aValue, R_OK)) |
| 67 | + { |
| 68 | + PrintArgError("%s: not permitted to read %s\n", aProgram, aValue); |
| 69 | + retval = false; |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + gOtaFilepath = aValue; |
| 74 | + } |
| 75 | + break; |
| 76 | + default: |
| 77 | + PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName); |
| 78 | + retval = false; |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + return (retval); |
| 83 | +} |
| 84 | + |
| 85 | +OptionDef cmdLineOptionsDef[] = { |
| 86 | + { "filepath", chip::ArgParser::kArgumentRequired, kOptionFilepath }, |
| 87 | + {}, |
| 88 | +}; |
| 89 | + |
| 90 | +OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS", |
| 91 | + " -f <file>\n" |
| 92 | + " --filepath <file>\n" |
| 93 | + " Path to a file containing an OTA image.\n" }; |
| 94 | + |
| 95 | +HelpOptions helpOptions("ota-provider-app", "Usage: ota-provider-app [options]", "1.0"); |
| 96 | + |
| 97 | +OptionSet * allOptions[] = { &cmdLineOptions, &helpOptions, nullptr }; |
| 98 | + |
| 99 | +int main(int argc, char * argv[]) |
| 100 | +{ |
| 101 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 102 | + OTAProviderExample otaProvider; |
| 103 | + BdxOtaSender bdxServer; |
| 104 | + ExchangeManager * exchangeMgr; |
| 105 | + |
| 106 | + if (chip::Platform::MemoryInit() != CHIP_NO_ERROR) |
| 107 | + { |
| 108 | + fprintf(stderr, "FAILED to initialize memory\n"); |
| 109 | + return 1; |
| 110 | + } |
| 111 | + |
| 112 | + if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR) |
| 113 | + { |
| 114 | + fprintf(stderr, "FAILED to initialize chip stack\n"); |
| 115 | + return 1; |
| 116 | + } |
| 117 | + |
| 118 | + if (!chip::ArgParser::ParseArgs(argv[0], argc, argv, allOptions)) |
| 119 | + { |
| 120 | + return 1; |
| 121 | + } |
| 122 | + |
| 123 | + chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig(); |
| 124 | + InitServer(); |
| 125 | + |
| 126 | + exchangeMgr = chip::ExchangeManager(); |
| 127 | + err = exchangeMgr->RegisterUnsolicitedMessageHandlerForProtocol(chip::Protocols::BDX::Id, &bdxServer); |
| 128 | + VerifyOrReturnError(err == CHIP_NO_ERROR, 1); |
| 129 | + |
| 130 | + ChipLogDetail(SoftwareUpdate, "using OTA file: %s", gOtaFilepath ? gOtaFilepath : "(none)"); |
| 131 | + |
| 132 | + if (gOtaFilepath != nullptr) |
| 133 | + { |
| 134 | + otaProvider.SetOTAFilePath(gOtaFilepath); |
| 135 | + bdxServer.SetFilepath(gOtaFilepath); |
| 136 | + } |
| 137 | + |
| 138 | + chip::app::clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, &otaProvider); |
| 139 | + |
| 140 | + BitFlags<TransferControlFlags> bdxFlags; |
| 141 | + bdxFlags.Set(TransferControlFlags::kReceiverDrive); |
| 142 | + err = bdxServer.PrepareForTransfer(&chip::DeviceLayer::SystemLayer, chip::bdx::TransferRole::kSender, bdxFlags, |
| 143 | + kMaxBdxBlockSize, kBdxTimeoutMs, kBdxPollFreqMs); |
| 144 | + if (err != CHIP_NO_ERROR) |
| 145 | + { |
| 146 | + ChipLogError(BDX, "failed to init BDX server: %s", chip::ErrorStr(err)); |
| 147 | + } |
| 148 | + |
| 149 | + chip::DeviceLayer::PlatformMgr().RunEventLoop(); |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
0 commit comments