Skip to content

Commit a648ef7

Browse files
authored
Reference OTA Provider app (project-chip#8963)
* new example application ota-provider-app * restlying * add ota-provider-app/linux to example linux workflows * regen for missing files * call out more specific deps * restyle * apply suggestions from Andre - use ChipMemString for string copying - add missing error checks * fix access() return value check * remove "using namespace" statements * add constexpr for endpoint id and fixup TODO message * merge master and regen
1 parent 1f10212 commit a648ef7

32 files changed

+6957
-7
lines changed

.github/workflows/examples-linux-standalone.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ jobs:
8787
timeout-minutes: 5
8888
run:
8989
scripts/examples/gn_build_example.sh examples/bridge-app/linux out/bridge_debug
90+
- name: Build example OTA Provider
91+
timeout-minutes: 5
92+
run:
93+
scripts/examples/gn_build_example.sh examples/ota-provider-app/linux out/ota_provider_debug
9094
- name: Binary artifact suffix
9195
id: outsuffix
9296
uses: haya14busa/action-cond@v1.0.0

examples/ota-provider-app/linux/.gn

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2020 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/build.gni")
16+
17+
# The location of the build configuration file.
18+
buildconfig = "${build_root}/config/BUILDCONFIG.gn"
19+
20+
# CHIP uses angle bracket includes.
21+
check_system_includes = true
22+
23+
default_args = {
24+
import("//args.gni")
25+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) 2020 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/build.gni")
16+
import("//build_overrides/chip.gni")
17+
18+
executable("chip-ota-provider-app") {
19+
sources = [ "main.cpp" ]
20+
21+
deps = [
22+
"${chip_root}/examples/ota-provider-app/ota-provider-common",
23+
"${chip_root}/examples/platform/linux:app-main",
24+
"${chip_root}/src/app/server",
25+
"${chip_root}/src/lib",
26+
"${chip_root}/src/protocols/bdx",
27+
]
28+
29+
cflags = [ "-Wconversion" ]
30+
31+
output_dir = root_out_dir
32+
}
33+
34+
group("linux") {
35+
deps = [ ":chip-ota-provider-app" ]
36+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ota-provider-app
2+
3+
This is a reference application that implements an example of an OTA Provider
4+
Cluster Server.
5+
6+
## Usage
7+
8+
`./ota-provider-app [--filepath \<filepath\>]`
9+
10+
If `-filepath` is provided, `ota-provider-app` will automatically serve that
11+
file to the Requestor (SoftwareVersion will be Requester version + 1).
12+
13+
If no `-filepath` is provided, `ota-provider-app` will respond to `QueryImage`
14+
with `NotAvailable` status.
15+
16+
## Current Features/Limitations
17+
18+
### Features
19+
20+
- can provide local filepath to serve as OTA image
21+
- can complete full BDX transfer
22+
- supports variable-length / startoffset for BDX transfer
23+
24+
### Limitations:
25+
26+
- Synchronous BDX transfer only
27+
- using hardcoded test values for local and peer Node IDs
28+
- does not check VID/PID
29+
- no configuration for `Busy`/`DelayedActionTime`
30+
- no configuration for `AwaitNextAction`
31+
- only one transfer at a time (does not check incoming `UpdateTokens`)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) 2020 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/chip.gni")
16+
17+
import("${chip_root}/config/standalone/args.gni")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../build_overrides
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2021 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/chip.gni")
16+
17+
import("${chip_root}/src/app/chip_data_model.gni")
18+
19+
chip_data_model("ota-provider-common") {
20+
zap_file = "ota-provider-app.zap"
21+
22+
zap_pregenerated_dir = "gen"
23+
24+
sources = [
25+
"BdxOtaSender.cpp",
26+
"OTAProviderExample.cpp",
27+
]
28+
29+
deps = [ "${chip_root}/src/protocols/bdx" ]
30+
31+
is_server = true
32+
}

0 commit comments

Comments
 (0)