merging the embedded/main into nearby SDK

This commit is contained in:
Shweta
2023-06-05 23:19:07 +00:00
parent b7f7e7aeb9
commit ed2552b88b
36 changed files with 4593 additions and 206 deletions
+230
View File
@@ -0,0 +1,230 @@
// 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 <deque>
#include <iostream>
#include <vector>
#include "fakes.h"
#include "gmock/gmock-matchers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "nearby.h"
#include "nearby_advert.h"
#include "nearby_event.h"
#include "nearby_fp_library.h"
#include "nearby_platform_ble.h"
#include "nearby_platform_os.h"
#include "nearby_spot.h"
using ::testing::ElementsAreArray;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic pop
constexpr uint8_t kFpAdvertisement[NON_DISCOVERABLE_ADV_SIZE_BYTES] = {1};
#if NEARBY_FP_ENABLE_SPOT
constexpr uint8_t kSpotAdvertisement[NEARBY_SPOT_ADVERTISEMENT_SIZE] = {2};
#endif /* NEARBY_FP_ENABLE_SPOT */
class AdvertTest : public ::testing::Test {
protected:
void SetUp() override;
};
void AdvertTest::SetUp() {
nearby_platform_OsInit(NULL);
nearby_platform_BleInit(NULL);
nearby_AdvertInit();
}
TEST_F(AdvertTest, SetFpAdvertisement_AdvertisesTimerOff) {
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(kFpAdvertisement,
sizeof(kFpAdvertisement),
kNoLargerThan100ms));
ASSERT_THAT(kFpAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, ChangeFpAdvertisement_AdvertisesTimerOff) {
constexpr uint8_t kAdvertisement[NON_DISCOVERABLE_ADV_SIZE_BYTES] = {3};
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(kFpAdvertisement,
sizeof(kFpAdvertisement),
kNoLargerThan100ms));
ASSERT_EQ(kNearbyStatusOK,
nearby_SetFpAdvertisement(kAdvertisement, sizeof(kAdvertisement),
kNoLargerThan100ms));
ASSERT_THAT(kAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, RemoveFpAdvertisement_NoAdvertisementTimerOff) {
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(kFpAdvertisement,
sizeof(kFpAdvertisement),
kNoLargerThan100ms));
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(NULL, 0, kDisabled));
ASSERT_EQ(0, nearby_test_fakes_GetAdvertisement().size());
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
#if NEARBY_FP_ENABLE_SPOT
TEST_F(AdvertTest, SetSpotAdvertisement_AdvertisesTimerOff) {
ASSERT_EQ(kNearbyStatusOK,
nearby_SetSpotAdvertisement(0, kSpotAdvertisement,
sizeof(kSpotAdvertisement)));
ASSERT_THAT(kSpotAdvertisement,
ElementsAreArray(nearby_test_fakes_GetSpotAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, ChangeSpotAdvertisement_AdvertisesTimerOff) {
constexpr uint8_t kAdvertisement[NON_DISCOVERABLE_ADV_SIZE_BYTES] = {3};
ASSERT_EQ(kNearbyStatusOK,
nearby_SetSpotAdvertisement(0, kSpotAdvertisement,
sizeof(kSpotAdvertisement)));
ASSERT_EQ(kNearbyStatusOK, nearby_SetSpotAdvertisement(0,
kAdvertisement, sizeof(kAdvertisement)));
ASSERT_THAT(kAdvertisement,
ElementsAreArray(nearby_test_fakes_GetSpotAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, RemoveSpotAdvertisement_NoAdvertisementTimerOff) {
ASSERT_EQ(kNearbyStatusOK,
nearby_SetSpotAdvertisement(0, kSpotAdvertisement,
sizeof(kSpotAdvertisement)));
ASSERT_EQ(kNearbyStatusOK, nearby_SetSpotAdvertisement(0, NULL, 0));
ASSERT_EQ(0, nearby_test_fakes_GetSpotAdvertisement().size());
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
#ifndef NEARBY_FP_HAVE_MULTIPLE_ADVERTISEMENTS
TEST_F(AdvertTest, SetFpThenSpot_RotatesOnTimer) {
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(kFpAdvertisement,
sizeof(kFpAdvertisement),
kNoLargerThan100ms));
ASSERT_EQ(kNearbyStatusOK,
nearby_SetSpotAdvertisement(kSpotAdvertisement,
sizeof(kSpotAdvertisement)));
ASSERT_THAT(kSpotAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
nearby_test_fakes_SetCurrentTimeMs(nearby_test_fakes_GetNextTimerMs());
ASSERT_THAT(kFpAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
nearby_test_fakes_SetCurrentTimeMs(nearby_test_fakes_GetNextTimerMs());
ASSERT_THAT(kSpotAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
}
TEST_F(AdvertTest, SetSpotThenFp_RotatesOnTimer) {
ASSERT_EQ(kNearbyStatusOK,
nearby_SetSpotAdvertisement(kSpotAdvertisement,
sizeof(kSpotAdvertisement)));
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(kFpAdvertisement,
sizeof(kFpAdvertisement),
kNoLargerThan100ms));
ASSERT_THAT(kFpAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
nearby_test_fakes_SetCurrentTimeMs(nearby_test_fakes_GetNextTimerMs());
ASSERT_THAT(kSpotAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
nearby_test_fakes_SetCurrentTimeMs(nearby_test_fakes_GetNextTimerMs());
ASSERT_THAT(kFpAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
}
TEST_F(AdvertTest, RemoveFp_SpotActive_RemovesTimer) {
nearby_SetFpAdvertisement(kFpAdvertisement, sizeof(kFpAdvertisement),
kNoLargerThan100ms);
nearby_SetSpotAdvertisement(kSpotAdvertisement, sizeof(kSpotAdvertisement));
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(NULL, 0, kDisabled));
ASSERT_THAT(kSpotAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, RemoveFp_FpActive_SwitchesToSpotRemovesTimer) {
nearby_SetSpotAdvertisement(kSpotAdvertisement, sizeof(kSpotAdvertisement));
nearby_SetFpAdvertisement(kFpAdvertisement, sizeof(kFpAdvertisement),
kNoLargerThan100ms);
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(NULL, 0, kDisabled));
ASSERT_THAT(kSpotAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, RemoveSpot_FpActive_RemovesTimer) {
nearby_SetSpotAdvertisement(kSpotAdvertisement, sizeof(kSpotAdvertisement));
nearby_SetFpAdvertisement(kFpAdvertisement, sizeof(kFpAdvertisement),
kNoLargerThan100ms);
ASSERT_EQ(kNearbyStatusOK, nearby_SetSpotAdvertisement(NULL, 0));
ASSERT_THAT(kFpAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, RemoveSpot_SpotActive_SwitchesToFpRemovesTimer) {
nearby_SetFpAdvertisement(kFpAdvertisement, sizeof(kFpAdvertisement),
kNoLargerThan100ms);
nearby_SetSpotAdvertisement(kSpotAdvertisement, sizeof(kSpotAdvertisement));
ASSERT_EQ(kNearbyStatusOK, nearby_SetSpotAdvertisement(NULL, 0));
ASSERT_THAT(kFpAdvertisement,
ElementsAreArray(nearby_test_fakes_GetAdvertisement()));
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
TEST_F(AdvertTest, RemoveBoth_DisablesAdvertisementRemovesTimer) {
nearby_SetSpotAdvertisement(kSpotAdvertisement, sizeof(kSpotAdvertisement));
nearby_SetFpAdvertisement(kFpAdvertisement, sizeof(kFpAdvertisement),
kNoLargerThan100ms);
ASSERT_EQ(kNearbyStatusOK, nearby_SetSpotAdvertisement(NULL, 0));
ASSERT_EQ(kNearbyStatusOK, nearby_SetFpAdvertisement(NULL, 0, kDisabled));
ASSERT_EQ(0, nearby_test_fakes_GetAdvertisement().size());
ASSERT_EQ(0, nearby_test_fakes_GetNextTimerMs());
}
#endif /* NEARBY_FP_HAVE_MULTIPLE_ADVERTISEMENTS */
#endif /* NEARBY_FP_ENABLE_SPOT */
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
+1
View File
@@ -126,6 +126,7 @@ uint64_t nearby_platform_GetActiveAudioSource() { return active_peer_address; }
void nearby_test_fakes_SetActiveAudioSource(uint64_t peer_address) {
active_peer_address = peer_address;
callbacks->on_state_change();
}
// Initializes Audio module
+59 -12
View File
@@ -16,12 +16,25 @@
#include "nearby.h"
#include "nearby_platform_battery.h"
#ifdef NEARBY_SPOT_BATTERY_LEVEL_INDICATION
#include "nearby_spot.h"
#endif /* NEARBY_SPOT_BATTERY_LEVEL_INDICATION */
constexpr nearby_platform_BatteryInfo kDefaultBatteryInfo = {
.is_charging = true,
.right_bud_battery_level = 80,
.left_bud_battery_level = 85,
.charging_case_battery_level = 90,
.remaining_time_minutes = 100};
#if NEARBY_BATTERY_LEVELS_SIZE == 1
{NEARBY_PLATFORM_SET_BATTERY_LEVEL(85, true)}
#elif NEARBY_BATTERY_LEVELS_SIZE == 2
{NEARBY_PLATFORM_SET_BATTERY_LEVEL(85, true), \
NEARBY_PLATFORM_SET_BATTERY_LEVEL(80, true)}
#elif NEARBY_BATTERY_LEVELS_SIZE == 3
{NEARBY_PLATFORM_SET_BATTERY_LEVEL(85, true), \
NEARBY_PLATFORM_SET_BATTERY_LEVEL(80, true), \
NEARBY_PLATFORM_SET_BATTERY_LEVEL(90, true)}
#endif /* NEARBY_BATTERY_LEVELS_SIZE */
#if NEARBY_BATTERY_REMAINING_TIME
,100
#endif /* NEARBY_BATTERY_REMAINING_TIME */
};
static nearby_platform_BatteryInfo test_battery_info = kDefaultBatteryInfo;
static nearby_platform_status get_battery_info_result = kNearbyStatusOK;
@@ -33,24 +46,58 @@ nearby_platform_status nearby_platform_GetBatteryInfo(
}
void nearby_test_fakes_SetIsCharging(bool charging) {
test_battery_info.is_charging = charging;
uint8_t level = 0;
uint8_t charging_bit = NEARBY_PLATFORM_BATTERY_INFO_NOT_CHARGING;
// If charging is true set the charging bit to 1
if (charging) {
charging_bit = NEARBY_PLATFORM_BATTERY_INFO_CHARGING;
}
level = NEARBY_PLATFORM_GET_BATTERY_LEVEL(test_battery_info.battery_level[kNearbyLeftBudBatteryLevel]);
test_battery_info.battery_level[kNearbyLeftBudBatteryLevel] = charging_bit | level;
#if NEARBY_BATTERY_LEVELS_SIZE >= 2
level = NEARBY_PLATFORM_GET_BATTERY_LEVEL(test_battery_info.battery_level[kNearbyRightBudBatteryLevel]);
test_battery_info.battery_level[kNearbyRightBudBatteryLevel] = charging_bit | level;
#endif
#if NEARBY_BATTERY_LEVELS_SIZE >= 3
level = NEARBY_PLATFORM_GET_BATTERY_LEVEL(test_battery_info.battery_level[kNearbyChargingCaseBatteryLevel]);
test_battery_info.battery_level[kNearbyChargingCaseBatteryLevel] = charging_bit | level;
#endif
}
void nearby_test_fakes_SetRightBudBatteryLevel(unsigned battery_level) {
test_battery_info.right_bud_battery_level = battery_level;
#if NEARBY_BATTERY_LEVELS_SIZE >= 2
void nearby_test_fakes_SetRightBudBatteryLevel(unsigned battery_level, bool charging) {
test_battery_info.battery_level[kNearbyRightBudBatteryLevel] =
NEARBY_PLATFORM_SET_BATTERY_LEVEL(battery_level, charging);
}
#endif
void nearby_test_fakes_SetLeftBudBatteryLevel(unsigned battery_level, bool charging) {
test_battery_info.battery_level[kNearbyLeftBudBatteryLevel] =
NEARBY_PLATFORM_SET_BATTERY_LEVEL(battery_level, charging);
}
void nearby_test_fakes_SetLeftBudBatteryLevel(unsigned battery_level) {
test_battery_info.left_bud_battery_level = battery_level;
#if NEARBY_BATTERY_LEVELS_SIZE >= 3
void nearby_test_fakes_SetChargingCaseBatteryLevel(unsigned battery_level, bool charging) {
test_battery_info.battery_level[kNearbyChargingCaseBatteryLevel] =
NEARBY_PLATFORM_SET_BATTERY_LEVEL(battery_level, charging);
}
#endif
void nearby_test_fakes_SetChargingCaseBatteryLevel(unsigned battery_level) {
test_battery_info.charging_case_battery_level = battery_level;
#ifdef NEARBY_SPOT_BATTERY_LEVEL_INDICATION
void nearby_test_fakes_SetMainBatteryLevel(unsigned battery_level) {
test_battery_info.battery_level[0] =
NEARBY_PLATFORM_SET_BATTERY_LEVEL(battery_level, false);
}
#endif /* NEARBY_SPOT_BATTERY_LEVEL_INDICATION */
#if NEARBY_BATTERY_REMAINING_TIME
void nearby_test_fakes_BatteryTime(uint16_t battery_time) {
test_battery_info.remaining_time_minutes = battery_time;
}
#endif /* NEARBY_BATTERY_REMAINING_TIME */
void nearby_test_fakes_SetGetBatteryInfoResult(nearby_platform_status status) {
get_battery_info_result = status;
+48
View File
@@ -25,11 +25,13 @@
constexpr uint64_t kDefaultBleAddress = 0xbabababa;
static uint64_t ble_address = kDefaultBleAddress;
static uint64_t spot_address = kDefaultBleAddress;
static uint64_t peer_address = 0x345678ab;
static const nearby_platform_BleInterface* ble_interface;
static std::map<nearby_fp_Characteristic, std::vector<uint8_t>> notifications;
static std::vector<uint8_t> advertisement;
static nearby_fp_AvertisementInterval interval;
static std::vector<uint8_t> spot_advertisement;
constexpr int32_t kDefaultPsm = -1;
static int32_t psm = kDefaultPsm;
@@ -73,6 +75,14 @@ nearby_platform_status nearby_platform_GattNotify(
return kNearbyStatusOK;
}
nearby_platform_status nearby_platform_GattNotifyAll(
nearby_fp_Characteristic characteristic, const uint8_t* message,
size_t length) {
notifications.emplace(characteristic,
std::vector<uint8_t>(message, message + length));
return kNearbyStatusOK;
}
nearby_platform_status nearby_platform_SetAdvertisement(
const uint8_t* payload, size_t length,
nearby_fp_AvertisementInterval interval) {
@@ -85,6 +95,17 @@ nearby_platform_status nearby_platform_SetAdvertisement(
return kNearbyStatusOK;
}
nearby_platform_status nearby_platform_SetSpotAdvertisement(uint64_t address,
const uint8_t* payload, size_t length) {
if (payload == NULL || length == 0) {
spot_advertisement.clear();
} else {
spot_advertisement.assign(payload, payload + length);
}
spot_address = address;
return kNearbyStatusOK;
}
int32_t nearby_platform_GetMessageStreamPsm() { return psm; }
// Initializes BLE
@@ -93,9 +114,11 @@ nearby_platform_status nearby_platform_BleInit(
ble_interface = callbacks;
notifications.clear();
advertisement.clear();
spot_advertisement.clear();
interval = kDisabled;
ble_address = kDefaultBleAddress;
psm = kDefaultPsm;
spot_address = kDefaultBleAddress;
return kNearbyStatusOK;
}
@@ -105,6 +128,31 @@ std::vector<uint8_t>& nearby_test_fakes_GetAdvertisement() {
return advertisement;
}
std::vector<uint8_t>& nearby_test_fakes_GetSpotAdvertisement() {
#ifdef NEARBY_FP_HAVE_MULTIPLE_ADVERTISEMENTS
return spot_advertisement;
#else
return nearby_test_fakes_GetAdvertisement();
#endif /* NEARBY_FP_HAVE_MULTIPLE_ADVERTISEMENTS */
}
uint64_t nearby_test_fakes_GetSpotAddress() {
return spot_address;
}
nearby_platform_status nearby_test_fakes_GattRead(
nearby_fp_Characteristic characteristic, uint8_t* output, size_t* length) {
return ble_interface->on_gatt_read(peer_address, characteristic, output,
length);
}
nearby_platform_status nearby_test_fakes_GattWrite(
nearby_fp_Characteristic characteristic, const uint8_t* request,
size_t length) {
return ble_interface->on_gatt_write(peer_address, characteristic, request,
length);
}
nearby_platform_status nearby_test_fakes_GattReadModelId(uint8_t* output,
size_t* length) {
return ble_interface->on_gatt_read(peer_address, kModelId, output, length);
+35 -6
View File
@@ -21,12 +21,22 @@
#include "nearby.h"
#include "nearby_platform_ble.h"
#include "nearby_platform_os.h"
#include "nearby_platform_battery.h"
void nearby_test_fakes_SetRandomNumber(unsigned int value);
void nearby_test_fakes_SetRandomNumberSequence(std::vector<uint8_t>& value);
std::vector<uint8_t> nearby_test_fakes_GetRawAccountKeys();
nearby_platform_status nearby_test_fakes_GattRead(
nearby_fp_Characteristic characteristic, uint8_t* output, size_t* length);
nearby_platform_status nearby_test_fakes_GattWrite(
nearby_fp_Characteristic characteristic, const uint8_t* request,
size_t length);
nearby_platform_status nearby_test_fakes_GattReadModelId(uint8_t* output,
size_t* length);
@@ -52,6 +62,8 @@ nearby_platform_status nearby_test_fakes_Aes128Encrypt(
const uint8_t key[AES_MESSAGE_SIZE_BYTES]);
std::vector<uint8_t>& nearby_test_fakes_GetAdvertisement();
std::vector<uint8_t>& nearby_test_fakes_GetSpotAdvertisement();
uint64_t nearby_test_fakes_GetSpotAddress();
std::map<nearby_fp_Characteristic, std::vector<uint8_t>>&
nearby_test_fakes_GetGattNotifications();
@@ -98,7 +110,7 @@ class AccountKeyList {
for (size_t i = 0; i < list->num_keys; i++) {
uint64_t address = 0;
#ifdef NEARBY_FP_ENABLE_SASS
#if NEARBY_FP_ENABLE_SASS
address = list->key[i].peer_address;
#endif /* NEARBY_FP_ENABLE_SASS */
key_pairs_.emplace_back(
@@ -120,7 +132,7 @@ class AccountKeyList {
RawAccountKeyList account_keys;
account_keys.num_keys = size();
for (size_t i = 0; i < size(); i++) {
#ifdef NEARBY_FP_ENABLE_SASS
#if NEARBY_FP_ENABLE_SASS
account_keys.key[i].peer_address = key_pairs_[i].address_;
#endif /* NEARBY_FP_ENABLE_SASS */
for (size_t j = 0; j < key_pairs_[i].account_key_.size(); j++) {
@@ -135,6 +147,9 @@ class AccountKeyList {
std::vector<AccountKeyPair> key_pairs_;
};
void nearby_test_fakes_SetRingingInfo(const nearby_platform_RingingInfo* info);
void nearby_test_fakes_NotifyRingStateChanged();
void nearby_test_fakes_SetAccountKeys(const uint8_t* input, size_t length);
void nearby_test_fakes_SetAccountKeys(
std::vector<AccountKeyPair>& account_key_pairs);
@@ -144,13 +159,23 @@ AccountKeyList nearby_test_fakes_GetAccountKeys();
void nearby_test_fakes_SetIsCharging(bool charging);
void nearby_test_fakes_SetRightBudBatteryLevel(unsigned battery_level);
#if NEARBY_BATTERY_LEVELS_SIZE >= 2
void nearby_test_fakes_SetRightBudBatteryLevel(unsigned battery_level, bool charging);
#endif
void nearby_test_fakes_SetLeftBudBatteryLevel(unsigned battery_level);
void nearby_test_fakes_SetLeftBudBatteryLevel(unsigned battery_level, bool charging);
void nearby_test_fakes_SetChargingCaseBatteryLevel(unsigned battery_level);
#if NEARBY_BATTERY_LEVELS_SIZE >= 3
void nearby_test_fakes_SetChargingCaseBatteryLevel(unsigned battery_level, bool charging);
#endif
#ifdef NEARBY_SPOT_BATTERY_LEVEL_INDICATION
void nearby_test_fakes_SetMainBatteryLevel(unsigned battery_level);
#endif /* NEARBY_SPOT_BATTERY_LEVEL_INDICATION */
#if NEARBY_BATTERY_REMAINING_TIME
void nearby_test_fakes_BatteryTime(uint16_t battery_time);
#endif /* NEARBY_BATTERY_REMAINING_TIME */
void nearby_test_fakes_SetGetBatteryInfoResult(nearby_platform_status status);
@@ -171,6 +196,10 @@ void nearby_test_fakes_SetCurrentTimeMs(uint32_t ms);
void nearby_test_fakes_SetInPairingMode(bool in_pairing_mode);
#if NEARBY_FP_ENABLE_SPOT
void nearby_test_fakes_SetHasUserContentForReadingEik(bool has_user_consent);
#endif /* NEARBY_FP_ENABLE_SPOT */
uint8_t nearby_test_fakes_GetRingCommand(void);
uint16_t nearby_test_fakes_GetRingTimeout(void);
@@ -187,7 +216,7 @@ void nearby_test_fakes_SassMultipointSwitch(uint8_t reason,
uint64_t peer_address,
const char* name);
#ifdef NEARBY_FP_ENABLE_SASS
#if NEARBY_FP_ENABLE_SASS
void nearby_test_fakes_SetActiveAudioSource(uint64_t peer_address);
#endif /* NEARBY_FP_ENABLE_SASS */
+109 -24
View File
@@ -12,21 +12,40 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include <cstddef>
#include <limits>
#include <map>
#include "nearby.h"
#include "nearby_assert.h"
#include "nearby_fp_library.h"
#include "nearby_platform_os.h"
const char firmware_revision[] = {'1', '.', '0', '.', '0', '\0'};
bool has_user_consent_for_reading_eik = false;
typedef void (*timer)();
static timer timer_callback;
static uint32_t timer_trigger_time;
static bool timer_has_run;
struct TimerInfo {
TimerInfo(timer callback, uint32_t trigger_time)
: callback_(callback), trigger_time_(trigger_time) {}
timer callback_;
uint32_t trigger_time_;
bool has_run_ = false;
};
static std::map<long, TimerInfo> timers;
static long timer_key = 0;
static uint32_t current_time;
static const nearby_platform_OsInterface* callbacks;
static uint32_t seconds = 50000;
static constexpr nearby_platform_RingingInfo kDefaultRingingInfo = {
.ring_state = kRingStateStoppedReasonTimeout,
.num_components = 3,
.components = 0,
.timeout = 0,
#if NEARBY_FP_ENABLE_SPOT
.volume = kRingingVolumeDefault,
#endif /* NEARBY_FP_ENABLE_SPOT */
};
static nearby_platform_RingingInfo ringing_info = kDefaultRingingInfo;
@@ -34,37 +53,92 @@ static nearby_platform_RingingInfo ringing_info = kDefaultRingingInfo;
// Gets current time in ms.
unsigned int nearby_platform_GetCurrentTimeMs() { return current_time; }
uint32_t nearby_platform_GetPersistentTime() { return seconds; }
nearby_platform_status nearby_platform_GetRingingInfo(
nearby_platform_RingingInfo* ringing_info) {
*ringing_info = ::ringing_info;
return kNearbyStatusOK;
}
nearby_platform_status nearby_platform_Ring(
uint8_t command, uint16_t timeout, nearby_platform_RingingVolume volume) {
ringing_info.components = command;
ringing_info.timeout = timeout;
#if NEARBY_FP_ENABLE_SPOT
ringing_info.volume = volume;
#endif /* NEARBY_FP_ENABLE_SPOT */
ringing_info.ring_state = kRingStateStarted;
return kNearbyStatusOK;
}
#if NEARBY_FP_ENABLE_SPOT
/* returns user consent as true if reading of EIK is permitted (when in
EIK recovery mode) by a button press or other user action */
bool nearby_platform_HasUserConsentForReadingEik(void)
{
return has_user_consent_for_reading_eik;
}
void nearby_test_fakes_SetHasUserContentForReadingEik(bool has_user_consent) {
has_user_consent_for_reading_eik = has_user_consent;
}
/* Perform a factory reset and clear all stored Account Keys */
nearby_platform_status nearby_platform_FactoryReset() {
// Clear all the account keys
return nearby_fp_ClearAccountKeys();
}
#endif /* NEARBY_FP_ENABLE_SPOT */
// Starts a timer. Returns an opaque timer handle or null on error.
void* nearby_platform_StartTimer(void (*callback)(), unsigned int delay_ms) {
timer_callback = callback;
timer_trigger_time = nearby_platform_GetCurrentTimeMs() + delay_ms;
timer_has_run = false;
return (void*)timer_callback;
long key = timer_key++;
timers.emplace(std::make_pair(
key, TimerInfo(callback, nearby_platform_GetCurrentTimeMs() + delay_ms)));
return reinterpret_cast<void*>(key);
}
// Cancels a timer
nearby_platform_status nearby_platform_CancelTimer(void* timer) {
NEARBY_ASSERT(timer == timer_callback);
timer_callback = NULL;
timer_trigger_time = 0;
long key = reinterpret_cast<long>(timer);
auto timer_info = timers.find(key);
NEARBY_ASSERT(timer_info != timers.end());
timers.erase(timer_info);
return kNearbyStatusOK;
}
uint32_t nearby_test_fakes_GetNextTimerMs() { return timer_trigger_time; }
uint32_t nearby_test_fakes_GetNextTimerMs() {
uint32_t timer_trigger_time = std::numeric_limits<uint32_t>::max();
std::for_each(
timers.begin(), timers.end(),
[&timer_trigger_time](std::pair<long, TimerInfo> elem) {
auto timer = elem.second;
if (!timer.has_run_ && timer.trigger_time_ < timer_trigger_time) {
timer_trigger_time = timer.trigger_time_;
}
});
// Returns 0 if there are no timers set
return timer_trigger_time != std::numeric_limits<uint32_t>::max()
? timer_trigger_time
: 0;
}
void nearby_test_fakes_SetCurrentTimeMs(uint32_t ms) {
current_time = ms;
if (!timer_has_run && timer_callback != NULL && timer_trigger_time <= ms) {
timer_has_run = true;
timer_callback();
}
std::for_each(timers.begin(), timers.end(),
[ms](std::pair<long, TimerInfo> elem) {
auto timer = elem.second;
if (!timer.has_run_ && timer.callback_ != NULL &&
timer.trigger_time_ <= ms) {
timer.has_run_ = true;
timer.callback_();
}
});
}
nearby_platform_status nearby_platform_Ring(uint8_t command, uint16_t timeout) {
ringing_info.components = command;
ringing_info.timeout = timeout;
ringing_info.ring_state = kRingStateStarted;
return kNearbyStatusOK;
void nearby_test_fakes_SetRingingInfo(const nearby_platform_RingingInfo* info) {
ringing_info = *info;
}
uint8_t nearby_test_fakes_GetRingCommand(void) {
@@ -73,10 +147,21 @@ uint8_t nearby_test_fakes_GetRingCommand(void) {
uint16_t nearby_test_fakes_GetRingTimeout(void) { return ringing_info.timeout; }
nearby_platform_status nearby_platform_OsInit() {
void nearby_test_fakes_NotifyRingStateChanged() {
callbacks->on_ring_state_change();
}
nearby_platform_status nearby_platform_OsInit(
const nearby_platform_OsInterface* os_interface) {
callbacks = os_interface;
current_time = 0;
timer_callback = NULL;
timer_trigger_time = 0;
timer_has_run = false;
timers.clear();
ringing_info = kDefaultRingingInfo;
has_user_consent_for_reading_eik = false;
return kNearbyStatusOK;
}
// Gets the firmware string
const char* nearby_platform_GetFirmwareRevision(void) {
return firmware_revision;
}
@@ -17,6 +17,7 @@
#include <vector>
#include "fakes.h"
#include "nearby.h"
#include "nearby_platform_persistence.h"
static std::map<nearby_fp_StoredKey, std::vector<uint8_t>> storage;
@@ -46,6 +47,16 @@ nearby_platform_status nearby_platform_SaveValue(nearby_fp_StoredKey key,
return kNearbyStatusOK;
}
nearby_platform_status nearby_platform_ClearValue(nearby_fp_StoredKey key) {
storage.erase(key);
return kNearbyStatusOK;
}
nearby_platform_status nearby_platform_ClearAllValue() {
storage.clear();
return kNearbyStatusOK;
}
nearby_platform_status nearby_platform_PersistenceInit() {
storage.clear();
return kNearbyStatusOK;
+26 -1
View File
@@ -26,7 +26,11 @@
#include <string>
#include "fakes.h"
#include "nearby.h"
#include "nearby_platform_se.h"
#include "nearby_trace.h"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-function"
@@ -196,6 +200,27 @@ nearby_platform_status nearby_platform_GenSec256r1Secret(
}
#endif /* NEARBY_PLATFORM_HAS_SE */
#ifndef NEARBY_PLATFORM_USE_MBEDTLS
nearby_platform_status nearby_platform_Aes256Encrypt(const uint8_t input[16],
uint8_t output[16],
const uint8_t key[32]) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int input_length = 16;
int output_length = 16;
uint8_t buffer[16];
EVP_EncryptInit(ctx, EVP_aes_256_ecb(), key, NULL);
if (1 !=
EVP_EncryptUpdate(ctx, buffer, &output_length, input, input_length)) {
return kNearbyStatusError;
}
memcpy(output, buffer, 16);
EVP_CIPHER_CTX_free(ctx);
return kNearbyStatusOK;
}
#endif
void nearby_test_fakes_SetRandomNumber(unsigned int value) {
random_value = value;
}
@@ -259,4 +284,4 @@ nearby_platform_status nearby_platform_SecureElementInit() {
random_value = 0;
random_sequence = std::queue<uint8_t>();
return kNearbyStatusOK;
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff