Files
nearby/presence/implementation/service_controller_impl.h
T
Janusz Sobczak a92abac83a Update advertisement encoder/decoder
1. Update the API to accept the credentials on input instead of credential manager.
2. Update the test with values using the LDT encryption.

These changes allow us to have a more asynchronous implementaion.
When broadcasting we can:
1. Fetch the private credentials asynchronously.
2. Create the advertisement and start broadcasting when the credentials have been fetched.

When scanning we can:
1. Fetch public credentials asynchronously.
2. Start scanning for advertisements.
3. Update the decoder when new credentials are fetched.

PiperOrigin-RevId: 493998041
2022-12-08 14:36:44 -08:00

115 lines
4.3 KiB
C++

// Copyright 2020 Google LLC
//
// 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.
#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_IMPL_H_
#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_IMPL_H_
#include <memory>
#include <string>
#include <utility>
#include "absl/base/thread_annotations.h"
#include "absl/random/random.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/proto/credential.pb.h"
#include "presence/broadcast_request.h"
#include "presence/data_types.h"
#include "presence/implementation/base_broadcast_request.h"
#include "presence/implementation/credential_manager_impl.h"
#include "presence/implementation/mediums/mediums.h"
#include "presence/implementation/scan_manager.h"
#include "presence/implementation/service_controller.h"
#include "presence/scan_request.h"
/*
* This class implements {@code ServiceController} functions. Owns mediums and
* other managers instances.
*/
namespace nearby {
namespace presence {
class ServiceControllerImpl : public ServiceController {
public:
using SingleThreadExecutor = ::location::nearby::SingleThreadExecutor;
using AdvertisingSession =
::location::nearby::api::ble_v2::BleMedium::AdvertisingSession;
using Runnable = ::location::nearby::Runnable;
using PrivateCredential = internal::PrivateCredential;
ServiceControllerImpl() = default;
~ServiceControllerImpl() override { executor_.Shutdown(); }
absl::StatusOr<ScanSessionId> StartScan(ScanRequest scan_request,
ScanCallback callback) override;
void StopScan(ScanSessionId session_id) override;
absl::StatusOr<BroadcastSessionId> StartBroadcast(
BroadcastRequest broadcast_request, BroadcastCallback callback) override;
void StopBroadcast(BroadcastSessionId) override;
SingleThreadExecutor& GetBackgroundExecutor() { return executor_; }
// Gives tests access to mediums.
Mediums& GetMediums() { return mediums_; }
private:
class BroadcastSessionState {
public:
explicit BroadcastSessionState(BroadcastCallback broadcast_callback,
PowerMode power_mode)
: broadcast_callback_(broadcast_callback), power_mode_(power_mode) {}
void SetAdvertisingSession(std::unique_ptr<AdvertisingSession> session);
void CallStartedCallback(Status status);
void StopAdvertising();
PowerMode GetPowerMode() { return power_mode_; }
private:
BroadcastCallback broadcast_callback_;
PowerMode power_mode_;
std::unique_ptr<AdvertisingSession> advertising_session_;
};
SingleThreadExecutor executor_;
BroadcastSessionId GenerateBroadcastSessionId();
void NotifyStartCallbackStatus(BroadcastSessionId id, Status status);
void RunOnServiceControllerThread(absl::string_view name, Runnable runnable) {
executor_.Execute(std::string(name), std::move(runnable));
}
void FetchCredentials(BroadcastSessionId id,
BaseBroadcastRequest broadcast_request)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(executor_);
void Advertise(BroadcastSessionId id, BaseBroadcastRequest broadcast_request,
std::vector<PrivateCredential> credentials)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(executor_);
Mediums mediums_; // NOLINT: further impl will use it.
CredentialManagerImpl
credential_manager_; // NOLINT: further impl will use it.
ScanManager scan_manager_{mediums_, credential_manager_,
executor_}; // NOLINT: further impl will use it.
absl::flat_hash_map<BroadcastSessionId, BroadcastSessionState> sessions_
ABSL_GUARDED_BY(executor_);
absl::BitGen bit_gen_;
};
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_IMPL_H_