Add support for setting the flag reader.

PiperOrigin-RevId: 729271203
This commit is contained in:
Anay Wadhera
2025-02-20 15:19:46 -08:00
committed by Copybara-Service
parent bddf053bf3
commit add588c622
4 changed files with 96 additions and 2 deletions
+1 -2
View File
@@ -42,14 +42,13 @@ cc_library(
"//connections:core",
"//connections:core_types",
"//connections/implementation/flags:connections_flags",
"//internal/flags:flag_reader",
"//internal/flags:nearby_flags",
"//internal/platform:base",
"//internal/platform:types",
"//proto:connections_enums_cc_proto",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:span",
],
)
+64
View File
@@ -42,6 +42,8 @@
#include "connections/payload.h"
#include "connections/status.h"
#include "connections/strategy.h"
#include "internal/flags/flag.h"
#include "internal/flags/flag_reader.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/bluetooth_utils.h"
#include "internal/platform/byte_array.h"
@@ -55,6 +57,61 @@ class ServiceControllerRouter;
class OfflineServiceController;
} // namespace nearby::connections
namespace {
class FlagReaderWrapper : public nearby::flags::FlagReader {
public:
explicit FlagReaderWrapper(READER_CONTEXT context,
NC_PHENOTYPE_FLAG_READER phenotype_flag_reader)
: context_(context), phenotype_flag_reader_(phenotype_flag_reader) {}
bool GetBoolFlag(const nearby::flags::Flag<bool>& flag) override {
NC_DATA flag_name = NC_DATA{
.size = static_cast<uint64_t>(flag.name().size()),
.data = (char*)flag.name().data(),
};
return phenotype_flag_reader_.get_bool_flag_value(context_, &flag_name,
flag.default_value());
}
int64_t GetInt64Flag(const nearby::flags::Flag<int64_t>& flag) override {
NC_DATA flag_name = NC_DATA{
.size = static_cast<uint64_t>(flag.name().size()),
.data = (char*)flag.name().data(),
};
return phenotype_flag_reader_.get_long_flag_value(context_, &flag_name,
flag.default_value());
}
double GetDoubleFlag(const nearby::flags::Flag<double>& flag) override {
NC_DATA flag_name = NC_DATA{
.size = static_cast<uint64_t>(flag.name().size()),
.data = (char*)flag.name().data(),
};
return phenotype_flag_reader_.get_double_flag_value(context_, &flag_name,
flag.default_value());
}
std::string GetStringFlag(
const nearby::flags::Flag<absl::string_view>& flag) override {
NC_DATA flag_name = NC_DATA{
.size = static_cast<uint64_t>(flag.name().size()),
.data = (char*)flag.name().data(),
};
NC_DATA default_value = NC_DATA{
.size = static_cast<uint64_t>(flag.default_value().size()),
.data = (char*)flag.default_value().data(),
};
NC_DATA flag_value = phenotype_flag_reader_.get_string_flag_value(
context_, &flag_name, &default_value);
return std::string(flag_value.data, flag_value.size);
}
private:
READER_CONTEXT context_;
NC_PHENOTYPE_FLAG_READER phenotype_flag_reader_;
};
} // namespace
typedef struct NcContext {
::nearby::connections::ServiceControllerRouter* router = nullptr;
::nearby::connections::Core* core = nullptr;
@@ -678,3 +735,10 @@ void NcSetCustomSavePath(NC_INSTANCE instance, const NC_DATA* save_path,
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcSetPhenotypeFlagReader(READER_CONTEXT context,
NC_PHENOTYPE_FLAG_READER phenotype_flag_reader) {
static absl::NoDestructor<FlagReaderWrapper> kNearbyFlags(
context, phenotype_flag_reader);
nearby::NearbyFlags::GetInstance().SetFlagReader(*kNearbyFlags);
}
+4
View File
@@ -184,6 +184,10 @@ NC_API void NcSetCustomSavePath(NC_INSTANCE instance, const NC_DATA* save_path,
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Sets the global instance of the phenotype flag reader for Nearby Connections.
NC_API void NcSetPhenotypeFlagReader(
READER_CONTEXT context, NC_PHENOTYPE_FLAG_READER phenotype_flag_reader);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+27
View File
@@ -28,6 +28,8 @@ typedef int64_t NC_PAYLOAD_ID;
typedef void* CALLER_CONTEXT;
typedef void* READER_CONTEXT;
// NC_DATA is used to define a byte array. Its last byte is not zero.
typedef struct NC_DATA {
uint64_t size;
@@ -312,6 +314,31 @@ typedef struct NC_OUT_OF_BAND_CONNECTION_METADATA {
NC_DATA remote_bluetooth_mac_address;
} NC_OUT_OF_BAND_CONNECTION_METADATA, *PNC_OUT_OF_BAND_CONNECTION_METADATA;
// Defines the minimal function interface for reading phenotype flags. Callbacks
// will be implemented by the client.
typedef struct NC_PHENOTYPE_FLAG_READER {
// Returns the value of the boolean flag with the given name. If not found,
// returns the default.
const bool (*get_bool_flag_value)(READER_CONTEXT context,
const NC_DATA* flag_name,
const bool default_value);
// Returns the value of the long flag with the given name. If not found,
// returns the default.
const int64_t (*get_long_flag_value)(READER_CONTEXT context,
const NC_DATA* flag_name,
const int64_t default_value);
// Returns the value of the double flag with the given name. If not found,
// returns the default.
const double (*get_double_flag_value)(READER_CONTEXT context,
const NC_DATA* flag_name,
const double default_value);
// Returns the value of the string flag with the given name. If not found,
// returns the default.
const NC_DATA (*get_string_flag_value)(READER_CONTEXT context,
const NC_DATA* flag_name,
const NC_DATA* default_value);
} NC_PHENOTYPE_FLAG_READER;
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus