blob: d9f83afff6e6138691a0eec103a274a83564fd10 [file] [log] [blame]
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once
#include "pw_software_update/bundled_update.rpc.pb.h"
#include "pw_software_update/bundled_update_backend.h"
#include "pw_software_update/update_bundle_accessor.h"
#include "pw_status/status.h"
#include "pw_sync/lock_annotations.h"
#include "pw_sync/mutex.h"
#include "pw_work_queue/work_queue.h"
namespace pw::software_update {
// Implementation class for pw.software_update.BundledUpdate.
// See bundled_update.proto for RPC method documentation.
class BundledUpdateService
: public generated::BundledUpdate<BundledUpdateService> {
public:
BundledUpdateService(UpdateBundleAccessor& bundle,
BundledUpdateBackend& backend,
work_queue::WorkQueue& work_queue)
: status_{},
backend_(backend),
bundle_(bundle),
bundle_open_(false),
work_queue_(work_queue),
work_enqueued_(false) {
status_.state = pw_software_update_BundledUpdateState_Enum_INACTIVE;
}
Status GetStatus(ServerContext&,
const pw_protobuf_Empty& request,
pw_software_update_BundledUpdateStatus& response);
// Sync
Status Start(ServerContext&,
const pw_software_update_StartRequest& request,
pw_software_update_BundledUpdateStatus& response);
// Async
Status Verify(ServerContext&,
const pw_protobuf_Empty& request,
pw_software_update_BundledUpdateStatus& response);
// Async
Status Apply(ServerContext&,
const pw_protobuf_Empty& request,
pw_software_update_BundledUpdateStatus& response);
// Currently sync, should be async.
// TODO: Make this async to support aborting verify/apply.
Status Abort(ServerContext&,
const pw_protobuf_Empty& request,
pw_software_update_BundledUpdateStatus& response);
// Sync
Status Reset(ServerContext&,
const pw_protobuf_Empty& request,
pw_software_update_BundledUpdateStatus& response);
// Notify the service that the bundle transfer has completed. The service has
// no way to know when the bundle transfer completes, so users must invoke
// this method in their transfer completion handler.
//
// After this call, the service will be in TRANSFERRED state if and only if
// it was in the TRANSFERRING state.
void NotifyTransferSucceeded();
// TODO:
// VerifyProgress - to update % complete.
// ApplyProgress - to update % complete.
private:
pw_software_update_BundledUpdateStatus status_ PW_GUARDED_BY(mutex_);
BundledUpdateBackend& backend_ PW_GUARDED_BY(mutex_);
UpdateBundleAccessor& bundle_ PW_GUARDED_BY(mutex_);
bool bundle_open_ PW_GUARDED_BY(mutex_);
work_queue::WorkQueue& work_queue_ PW_GUARDED_BY(mutex_);
bool work_enqueued_ PW_GUARDED_BY(mutex_);
sync::Mutex mutex_;
void DoVerify();
void DoApply();
};
} // namespace pw::software_update