Skip to content

Commit

Permalink
Right sizing MACAddress buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Oct 23, 2023
1 parent 9ddca5f commit c34e076
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ private boolean isPassingDeviceTypeFilter(DiscoveredNodeData discoveredNodeData)

private void addCommissioningInfo(DiscoveredNodeData discoveredNodeData) {
if (preCommissionedVideoPlayers != null) {
long currentUnixTimeMS = System.currentTimeMillis();
for (VideoPlayer videoPlayer : preCommissionedVideoPlayers) {
if (videoPlayer.isSameAs(discoveredNodeData)) {
Log.d(
TAG,
"Matching Video Player with the following information found for DiscoveredNodeData"
+ videoPlayer);
long currentUnixTimeMS = System.currentTimeMillis();
Log.d(TAG, "Updating discovery timestamp for VideoPlayer to " + currentUnixTimeMS);
videoPlayer.setLastDiscoveredMs(currentUnixTimeMS);
discoveredNodeData.setConnectableVideoPlayer(videoPlayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe

if (MACAddress != nullptr)
{
chip::CharSpan MACAddressSpan(MACAddress, strlen(MACAddress) - 1);
chip::CharSpan MACAddressSpan(MACAddress, 2 * 2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength);
outTargetVideoPlayerInfo.SetMACAddress(MACAddressSpan);
}

Expand Down Expand Up @@ -257,9 +257,9 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
jstring MACAddress = nullptr;
if (targetVideoPlayerInfo->GetMACAddress() != nullptr && targetVideoPlayerInfo->GetMACAddress()->data() != nullptr)
{
char MACAddressWithNil[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength + 1];
strncpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(),
targetVideoPlayerInfo->GetMACAddress()->size());
char MACAddressWithNil[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength + 1];
memcpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(),
targetVideoPlayerInfo->GetMACAddress()->size());
MACAddressWithNil[targetVideoPlayerInfo->GetMACAddress()->size()] = '\0';
MACAddress = env->NewStringUTF(MACAddressWithNil);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class TargetVideoPlayerInfo
bool IsAsleep() { return mIsAsleep; }
void SetMACAddress(chip::CharSpan MACAddress)
{
memcpy(mMACAddressBuf, MACAddress.data(), MACAddress.size());
mMACAddress = chip::CharSpan(mMACAddressBuf, MACAddress.size());
memcpy(mMACAddressBuf, MACAddress.data(), sizeof(mMACAddressBuf));
mMACAddress = chip::CharSpan(mMACAddressBuf, sizeof(mMACAddressBuf));
}
chip::System::Clock::Timestamp GetLastDiscovered() { return mLastDiscovered; }
void SetLastDiscovered(chip::System::Clock::Timestamp lastDiscovered) { mLastDiscovered = lastDiscovered; }
Expand All @@ -106,7 +106,7 @@ class TargetVideoPlayerInfo
// it was recently discoverable if its mLastDiscovered.count is within
// CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_HOURS of current time
chip::System::Clock::Timestamp currentUnixTimeMS = chip::System::Clock::kZero;
chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS);
VerifyOrReturnValue(chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS) == CHIP_NO_ERROR, true);
ChipLogProgress(AppServer, "WasRecentlyDiscoverable currentUnixTimeMS: %lu mLastDiscovered: %lu",
static_cast<unsigned long>(currentUnixTimeMS.count()), static_cast<unsigned long>(mLastDiscovered.count()));
return mLastDiscovered.count() >
Expand Down Expand Up @@ -211,7 +211,7 @@ class TargetVideoPlayerInfo
char mInstanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1];
uint16_t mPort;
chip::CharSpan mMACAddress;
char mMACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength];
char mMACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
chip::System::Clock::Timestamp mLastDiscovered;
bool mIsAsleep = false;
bool mInitialized = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
char hostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
size_t numIPs = 0;
Inet::IPAddress ipAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];
uint64_t lastDiscoveredMs = 0;
char MACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength] = {};
uint32_t MACAddressLength = 0;
char instanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1] = {};
uint16_t port = 0;
uint64_t lastDiscoveredMs = 0;
char MACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength] = {};
uint32_t MACAddressLength = 0;
char instanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1] = {};
uint16_t port = 0;

CHIP_ERROR err;
while ((err = reader.Next()) == CHIP_NO_ERROR)
Expand Down Expand Up @@ -349,7 +349,7 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
{
MACAddressLength = reader.GetLength();
ReturnErrorOnFailure(reader.GetBytes(reinterpret_cast<uint8_t *>(MACAddressBuf),
2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength));
2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength));
continue;
}

Expand Down

0 comments on commit c34e076

Please sign in to comment.