Skip to content

Commit

Permalink
[OTA] Use CASESessionManager to establish CASE sessions from OTAReque…
Browse files Browse the repository at this point in the history
…stor class
  • Loading branch information
carol-apple committed Dec 6, 2021
1 parent 186c004 commit bc59676
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@

#include <app-common/zap-generated/cluster-objects.h>
#include <app/clusters/ota-provider/ota-provider-delegate.h>
#include <app/server/Server.h>
#include <app/util/af.h>
#include <credentials/FabricTable.h>
#include <crypto/RandUtils.h>
#include <lib/core/CHIPTLV.h>
#include <lib/support/CHIPMemString.h>
#include <protocols/secure_channel/PASESession.h> // For chip::kTestDeviceNodeId

#include <string.h>

using chip::ByteSpan;
using chip::CharSpan;
using chip::FabricIndex;
using chip::FabricInfo;
using chip::NodeId;
using chip::Optional;
using chip::Server;
using chip::Span;
using chip::app::Clusters::OTAProviderDelegate;
using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands;
Expand All @@ -57,10 +62,9 @@ void GenerateUpdateToken(uint8_t * buf, size_t bufSize)
}
}

bool GenerateBdxUri(const Span<char> & fileDesignator, Span<char> outUri, size_t availableSize)
bool GenerateBdxUri(NodeId nodeId, const Span<char> & fileDesignator, Span<char> outUri, size_t availableSize)
{
static constexpr char bdxPrefix[] = "bdx://";
chip::NodeId nodeId = chip::kTestDeviceNodeId; // TODO: read this dynamically
size_t nodeIdHexStrLen = sizeof(nodeId) * 2;
size_t expectedLength = strlen(bdxPrefix) + nodeIdHexStrLen + 1 + fileDesignator.size();

Expand Down Expand Up @@ -115,9 +119,15 @@ EmberAfStatus OTAProviderExample::HandleQueryImage(chip::app::CommandHandler * c

if (strlen(mOTAFilePath))
{
// TODO: This uses the current node as the provider to supply the OTA image. This can be configurable such that the provider
// supplying the response is not the provider supplying the OTA image.
FabricIndex fabricIndex = commandObj->GetExchangeContext()->GetSessionHandle().GetFabricIndex();
FabricInfo * fabricInfo = Server::GetInstance().GetFabricTable().FindFabricWithIndex(fabricIndex);
NodeId nodeId = fabricInfo->GetPeerId().GetNodeId();

// Only doing BDX transport for now
GenerateBdxUri(Span<char>(mOTAFilePath, strlen(mOTAFilePath)), Span<char>(uriBuf, 0), kUriMaxLen);
ChipLogDetail(SoftwareUpdate, "generated URI: %s", uriBuf);
GenerateBdxUri(nodeId, Span<char>(mOTAFilePath, strlen(mOTAFilePath)), Span<char>(uriBuf, kUriMaxLen), kUriMaxLen);
ChipLogDetail(SoftwareUpdate, "generated URI: %.*s", static_cast<int>(strlen(uriBuf)), uriBuf);
}

// Set Status for the Query Image Response
Expand Down
24 changes: 3 additions & 21 deletions examples/ota-requestor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* limitations under the License.
*/

#include <app/OperationalDeviceProxy.h>
#include <app/server/Server.h>
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
Expand All @@ -31,19 +30,20 @@
using chip::BDXDownloader;
using chip::ByteSpan;
using chip::CharSpan;
using chip::DeviceProxy;
using chip::EndpointId;
using chip::FabricIndex;
using chip::GetRequestorInstance;
using chip::LinuxOTAImageProcessor;
using chip::NodeId;
using chip::OnDeviceConnected;
using chip::OnDeviceConnectionFailure;
using chip::OTADownloader;
using chip::OTAImageProcessorParams;
using chip::OTARequestor;
using chip::PeerId;
using chip::Server;
using chip::VendorId;
using chip::Callback::Callback;
using chip::Inet::IPAddress;
using chip::System::Layer;
using chip::Transport::PeerAddress;
using namespace chip::ArgParser;
Expand All @@ -62,10 +62,8 @@ constexpr uint16_t kOptionProviderNodeId = 'n';
constexpr uint16_t kOptionProviderFabricIndex = 'f';
constexpr uint16_t kOptionUdpPort = 'u';
constexpr uint16_t kOptionDiscriminator = 'd';
constexpr uint16_t kOptionIPAddress = 'i';
constexpr uint16_t kOptionDelayQuery = 'q';

const char * ipAddress = NULL;
NodeId providerNodeId = 0x0;
FabricIndex providerFabricIndex = 1;
uint16_t requestorSecurePort = 0;
Expand All @@ -77,8 +75,6 @@ OptionDef cmdLineOptionsDef[] = {
{ "providerFabricIndex", chip::ArgParser::kArgumentRequired, kOptionProviderFabricIndex },
{ "udpPort", chip::ArgParser::kArgumentRequired, kOptionUdpPort },
{ "discriminator", chip::ArgParser::kArgumentRequired, kOptionDiscriminator },
// TODO: This can be removed once OperationalDeviceProxy can resolve the IP Address from Node ID
{ "ipaddress", chip::ArgParser::kArgumentRequired, kOptionIPAddress },
{ "delayQuery", chip::ArgParser::kArgumentRequired, kOptionDelayQuery },
{},
};
Expand All @@ -95,8 +91,6 @@ OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS"
" -d/--discriminator <discriminator>\n"
" A 12-bit value used to discern between multiple commissionable CHIP device\n"
" advertisements. If none is specified, default value is 3840.\n"
" -i/--ipaddress <IP Address>\n"
" The IP Address of the OTA Provider to connect to. This value must be supplied.\n"
" -q/--delayQuery <Time in seconds>\n"
" From boot up, the amount of time to wait before triggering the QueryImage\n"
" command. If none or zero is supplied, QueryImage will not be triggered.\n" };
Expand Down Expand Up @@ -144,10 +138,6 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,
retval = false;
}
break;
case kOptionIPAddress:
ipAddress = aValue;
ChipLogError(SoftwareUpdate, "IP Address = %s", aValue);
break;
case kOptionDelayQuery:
delayQueryTimeInSec = static_cast<uint16_t>(strtol(aValue, NULL, 0));
break;
Expand Down Expand Up @@ -221,14 +211,6 @@ int main(int argc, char * argv[])
requestorCore.SetBDXDownloader(&downloader);
// Initialize and interconnect the Requestor and Image Processor objects -- END

// Pass the IP Address to the OTARequestor object: Use of explicit IP address is temporary
// until the Exchange Layer implements address resolution
{
IPAddress ipAddr;
IPAddress::FromString(ipAddress, ipAddr);
requestorCore.SetIpAddress(ipAddr);
}

// Test Mode operation: If a delay is provided, QueryImage after the timer expires
if (delayQueryTimeInSec > 0)
{
Expand Down
3 changes: 3 additions & 0 deletions src/app/CASESessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver
VerifyOrReturn(params.sessionInitParams.Validate() == CHIP_NO_ERROR);

mConfig = params;

// TODO: Revisit who should be set as the resolver delegate
Dnssd::Resolver::Instance().SetResolverDelegate(this);
}

virtual ~CASESessionManager() {}
Expand Down
2 changes: 1 addition & 1 deletion src/app/clusters/ota-requestor/ClusterInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool emberAfOtaSoftwareUpdateRequestorClusterAnnounceOtaProviderCallback(
const chip::app::Clusters::OtaSoftwareUpdateRequestor::Commands::AnnounceOtaProvider::DecodableType & commandData)
{
EmberAfStatus status;
OTARequestorInterface * requestor = GetRequestorInstance();
chip::OTARequestorInterface * requestor = chip::GetRequestorInstance();

if (requestor != nullptr)
{
Expand Down
Loading

0 comments on commit bc59676

Please sign in to comment.