Define Data Elements and ActionFactory.

Data Elements describe the custom data entities that can be added
to an advertisement.
ActionFactory takes a list of Data Elements and creates an Action
for the base NP advertisement.

PiperOrigin-RevId: 460557059
This commit is contained in:
jsobczak
2022-07-12 14:48:42 -07:00
committed by Copybara-Service
parent 73264d8feb
commit b92881a460
5 changed files with 251 additions and 0 deletions
+27
View File
@@ -44,6 +44,7 @@ cc_library(
],
hdrs = [
"broadcast_options.h",
"data_element.h",
"device_motion.h",
"discovery_filter.h",
"discovery_options.h",
@@ -95,6 +96,17 @@ cc_library(
],
)
cc_library(
name = "action_factory",
srcs = ["action_factory.cc"],
hdrs = ["action_factory.h"],
deps = [
":broadcast_request",
":types",
"//internal/platform:logging",
],
)
cc_test(
name = "broadcast_request_test",
size = "small",
@@ -121,6 +133,21 @@ cc_test(
],
)
cc_test(
name = "action_factory_test",
size = "small",
srcs = ["action_factory_test.cc"],
deps = [
":action_factory",
":broadcast_request",
":types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "types_test",
size = "small",
+66
View File
@@ -0,0 +1,66 @@
// Copyright 2022 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.
#include "third_party/nearby/presence/action_factory.h"
#include <algorithm>
#include <vector>
#include "internal/platform/logging.h"
namespace nearby {
namespace presence {
constexpr int kContentTimestampMask = 0x0F;
constexpr int kContentTimestampShift = 12;
constexpr int kEmptyMask = 0;
int ActionFactory::GetMask(const DataElement& element) {
int type = element.GetType();
switch (type) {
case DataElement::kContextTimestamp: {
auto value = element.GetValue();
if (!value.empty()) {
return (value[0] & kContentTimestampMask) << kContentTimestampShift;
} else {
NEARBY_LOG(WARNING, "Context timestamp Data Element without value");
return kEmptyMask;
}
}
case DataElement::kActiveUnlock:
case DataElement::kTapToTransfer:
case DataElement::kNearbyShare:
case DataElement::kFastPair:
case DataElement::kFitCast:
case DataElement::kPresenceManager:
return type;
}
NEARBY_LOG(WARNING, "Data Element 0x%x not supported in base advertisement",
type);
return kEmptyMask;
}
Action ActionFactory::createAction(
const std::vector<DataElement>& data_elements) {
Action action = {.action = 0};
std::for_each(data_elements.begin(), data_elements.end(),
[&](const auto& element) {
int mask = GetMask(element);
action.action |= mask;
});
return action;
}
} // namespace presence
} // namespace nearby
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2022 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_ACTION_FACTORY_H_
#define THIRD_PARTY_NEARBY_PRESENCE_ACTION_FACTORY_H_
#include <vector>
#include "third_party/nearby/presence/broadcast_request.h"
#include "third_party/nearby/presence/data_element.h"
namespace nearby {
namespace presence {
/** Defines the mapping between Data Elements and Actions in the Base NP
* advertisement. */
class ActionFactory {
public:
/** Returns an Action for Base NP advertisement from a collection of Data
* Elements. Data Elements unsupported in the Base NP advertisement are
* ignored.
*/
static Action createAction(const std::vector<DataElement>& data_elements);
private:
static int GetMask(const DataElement& element);
};
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_ACTION_FACTORY_H_
+65
View File
@@ -0,0 +1,65 @@
// Copyright 2022 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.
#include "third_party/nearby/presence/action_factory.h"
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/escaping.h"
#include "third_party/nearby/presence/broadcast_request.h"
#include "third_party/nearby/presence/data_element.h"
namespace nearby {
namespace presence {
namespace {
TEST(ActionFactory, CreateActiveUnlockAction) {
std::vector<DataElement> data_elements;
data_elements.emplace_back(DataElement::kActiveUnlock, "");
Action action = ActionFactory::createAction(data_elements);
EXPECT_EQ(action.action, 1 << 11);
}
TEST(ActionFactory, CreateContextTimestamp) {
const std::string kTimestamp = absl::HexStringToBytes("0B");
std::vector<DataElement> data_elements;
data_elements.emplace_back(DataElement::kContextTimestamp, kTimestamp);
Action action = ActionFactory::createAction(data_elements);
EXPECT_EQ(action.action, 0x0B << 12);
}
TEST(ActionFactory, CreateContextTimestampAndFastPair) {
const std::string kTimestamp = absl::HexStringToBytes("0B");
std::vector<DataElement> data_elements;
data_elements.emplace_back(DataElement::kContextTimestamp, kTimestamp);
data_elements.emplace_back(DataElement::kFastPair, "");
Action action = ActionFactory::createAction(data_elements);
EXPECT_EQ(action.action, (0x0B << 12) | 0x100);
}
} // namespace
} // namespace presence
} // namespace nearby
+51
View File
@@ -0,0 +1,51 @@
// Copyright 2022 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_DATA_ELEMENT_H_
#define THIRD_PARTY_NEARBY_PRESENCE_DATA_ELEMENT_H_
#include <stdint.h>
#include <string>
#include "absl/strings/string_view.h"
namespace nearby {
namespace presence {
/** Describes a custom Data element in NP advertisement. */
class DataElement {
public:
DataElement(uint16_t type, absl::string_view value)
: type_(type), value_(value) {}
uint16_t GetType() const { return type_; }
absl::string_view GetValue() const { return value_; }
static constexpr int kContextTimestamp = 1 << 12;
// The values below match the bitmasks in Base NP Intent.
static constexpr int kActiveUnlock = 1 << 11;
static constexpr int kTapToTransfer = 1 << 10;
static constexpr int kNearbyShare = 1 << 9;
static constexpr int kFastPair = 1 << 8;
static constexpr int kFitCast = 1 << 7;
static constexpr int kPresenceManager = 1 << 6;
private:
uint16_t type_;
std::string value_;
};
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_DATA_ELEMENT_H_