From 1100687894659da9008af00d4d0c205677074a9b Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Tue, 21 Mar 2023 20:56:17 +0100 Subject: [PATCH] [Tizen] Do not log BLE binary data using "%s" (#25768) * Do not print binary data using "%s" * Improve readability of app preference logging format * Use Uint8:: helpers instead of reinterpret_cast --- src/platform/Tizen/AppPreference.cpp | 6 +++--- src/platform/Tizen/BLEManagerImpl.cpp | 26 ++++++++++++++------------ src/platform/Tizen/BLEManagerImpl.h | 2 ++ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/platform/Tizen/AppPreference.cpp b/src/platform/Tizen/AppPreference.cpp index 01c4a2b982eae1..ffb36ee6777186 100644 --- a/src/platform/Tizen/AppPreference.cpp +++ b/src/platform/Tizen/AppPreference.cpp @@ -86,7 +86,7 @@ CHIP_ERROR GetData(const char * key, void * data, size_t dataSize, size_t * getD } ::memcpy(data, decodedData.Get() + offset, copySize); - ChipLogDetail(DeviceLayer, "Get data [%s]: %u", key, static_cast(copySize)); + ChipLogDetail(DeviceLayer, "Get preference data: key=%s len=%u", key, static_cast(copySize)); ChipLogByteSpan(DeviceLayer, ByteSpan(reinterpret_cast(data), copySize)); return CHIP_NO_ERROR; @@ -114,7 +114,7 @@ CHIP_ERROR SaveData(const char * key, const void * data, size_t dataSize) return CHIP_ERROR_INCORRECT_STATE; } - ChipLogDetail(DeviceLayer, "Save data [%s]: %u", key, static_cast(dataSize)); + ChipLogDetail(DeviceLayer, "Save preference data: key=%s len=%u", key, static_cast(dataSize)); ChipLogByteSpan(DeviceLayer, ByteSpan(reinterpret_cast(data), dataSize)); return CHIP_NO_ERROR; @@ -133,7 +133,7 @@ CHIP_ERROR RemoveData(const char * key) return CHIP_ERROR_INCORRECT_STATE; } - ChipLogProgress(DeviceLayer, "Remove data [%s]", key); + ChipLogProgress(DeviceLayer, "Remove preference data: key=%s", key); return CHIP_NO_ERROR; } diff --git a/src/platform/Tizen/BLEManagerImpl.cpp b/src/platform/Tizen/BLEManagerImpl.cpp index 1ce4d49829ea2d..ef9c73aed1436b 100644 --- a/src/platform/Tizen/BLEManagerImpl.cpp +++ b/src/platform/Tizen/BLEManagerImpl.cpp @@ -49,10 +49,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -173,8 +175,8 @@ static constexpr const char * __ConvertAttTypeToStr(bt_gatt_type_e type) } } -static void __ReadValueRequestedCb(const char * remoteAddress, int requestId, bt_gatt_server_h server, bt_gatt_h gattHandle, - int offset, void * userData) +void BLEManagerImpl::ReadValueRequestedCb(const char * remoteAddress, int requestId, bt_gatt_server_h server, bt_gatt_h gattHandle, + int offset, void * userData) { int ret, len = 0; bt_gatt_type_e type; @@ -183,13 +185,13 @@ static void __ReadValueRequestedCb(const char * remoteAddress, int requestId, bt VerifyOrReturn(__GetAttInfo(gattHandle, &uuid, &type) == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from GATT handle")); - ChipLogProgress(DeviceLayer, "Read Requested on %s: %s", __ConvertAttTypeToStr(type), StringOrNullMarker(uuid)); + ChipLogProgress(DeviceLayer, "Gatt read requested on %s: uuid=%s", __ConvertAttTypeToStr(type), StringOrNullMarker(uuid)); g_free(uuid); ret = bt_gatt_get_value(gattHandle, &value, &len); VerifyOrReturn(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_get_value() failed. ret: %d", ret)); - ChipLogProgress(DeviceLayer, "Read Value (len: %d): %.*s", len, len, value); + ChipLogByteSpan(DeviceLayer, ByteSpan(Uint8::from_const_char(value), len)); ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_READ, offset, 0x00, value, len); g_free(value); @@ -209,9 +211,9 @@ void BLEManagerImpl::WriteValueRequestedCb(const char * remoteAddress, int reque VerifyOrReturn(__GetAttInfo(gattHandle, &uuid, &type) == BT_ERROR_NONE, ChipLogError(DeviceLayer, "Failed to fetch GATT Attribute from GATT handle")); - - ChipLogProgress(DeviceLayer, "Write Requested on %s: %s", __ConvertAttTypeToStr(type), StringOrNullMarker(uuid)); - ChipLogProgress(DeviceLayer, "Write Value (len: %d): %.*s ", len, len, value); + ChipLogProgress(DeviceLayer, "Gatt write requested on %s: uuid=%s len=%d", __ConvertAttTypeToStr(type), + StringOrNullMarker(uuid), len); + ChipLogByteSpan(DeviceLayer, ByteSpan(Uint8::from_const_char(value), len)); g_free(uuid); ret = bt_gatt_set_value(gattHandle, value, len); @@ -220,7 +222,7 @@ void BLEManagerImpl::WriteValueRequestedCb(const char * remoteAddress, int reque ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_WRITE, offset, 0x00, nullptr, 0); VerifyOrReturn(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_server_send_response() failed. ret: %d", ret)); - sInstance.HandleC1CharWriteEvent(conn, reinterpret_cast(value), len); + sInstance.HandleC1CharWriteEvent(conn, Uint8::from_const_char(value), len); } void BLEManagerImpl::NotificationStateChangedCb(bool notify, bt_gatt_server_h server, bt_gatt_h gattHandle, void * userData) @@ -271,7 +273,7 @@ void BLEManagerImpl::CharacteristicNotificationCb(bt_gatt_h characteristic, char VerifyOrReturn(conn->gattCharC2Handle == characteristic, ChipLogError(DeviceLayer, "Gatt characteristic handle did not match")); ChipLogProgress(DeviceLayer, "Notification Received from CHIP peripheral [%s]", conn->peerAddr); - sInstance.HandleRXCharChanged(conn, reinterpret_cast(value), len); + sInstance.HandleRXCharChanged(conn, Uint8::from_const_char(value), len); } void BLEManagerImpl::IndicationConfirmationCb(int result, const char * remoteAddress, bt_gatt_server_h server, @@ -581,7 +583,7 @@ int BLEManagerImpl::RegisterGATTServer() &char2); VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_characteristic_create() failed. ret: %d", ret)); - ret = bt_gatt_server_set_read_value_requested_cb(char2, __ReadValueRequestedCb, nullptr); + ret = bt_gatt_server_set_read_value_requested_cb(char2, ReadValueRequestedCb, nullptr); VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_server_set_read_value_requested_cb() failed. ret: %d", ret)); ret = bt_gatt_server_set_characteristic_notification_state_change_cb(char2, NotificationStateChangedCb, nullptr); @@ -1284,7 +1286,7 @@ bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const Ble::Chip conn = static_cast(g_hash_table_lookup(sInstance.mConnectionMap, conn->peerAddr)); VerifyOrExit(conn != nullptr, ChipLogError(DeviceLayer, "Failed to find connection info")); - ret = bt_gatt_set_value(mGattCharC2Handle, reinterpret_cast(pBuf->Start()), pBuf->DataLength()); + ret = bt_gatt_set_value(mGattCharC2Handle, Uint8::to_const_char(pBuf->Start()), pBuf->DataLength()); VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_set_value() failed. ret: %d", ret)); ChipLogProgress(DeviceLayer, "Sending indication for CHIPoBLE RX characteristic (con %s, len %u)", conn->peerAddr, @@ -1319,7 +1321,7 @@ bool BLEManagerImpl::SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::Ch ChipLogError(DeviceLayer, "SendWriteRequest() called with invalid characteristic ID")); VerifyOrExit(conn->gattCharC1Handle != nullptr, ChipLogError(DeviceLayer, "Char C1 is null")); - ret = bt_gatt_set_value(conn->gattCharC1Handle, reinterpret_cast(pBuf->Start()), pBuf->DataLength()); + ret = bt_gatt_set_value(conn->gattCharC1Handle, Uint8::to_const_char(pBuf->Start()), pBuf->DataLength()); VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_set_value() failed. ret: %d", ret)); ChipLogProgress(DeviceLayer, "Sending Write Request for CHIPoBLE TX characteristic (con %s, len %u)", conn->peerAddr, diff --git a/src/platform/Tizen/BLEManagerImpl.h b/src/platform/Tizen/BLEManagerImpl.h index 9610d387ee39c8..629d7208a88c26 100644 --- a/src/platform/Tizen/BLEManagerImpl.h +++ b/src/platform/Tizen/BLEManagerImpl.h @@ -174,6 +174,8 @@ class BLEManagerImpl final : public BLEManager, static void AdvertisingStateChangedCb(int result, bt_advertiser_h advertiser, bt_adapter_le_advertising_state_e advState, void * userData); static void NotificationStateChangedCb(bool notify, bt_gatt_server_h server, bt_gatt_h gattHandle, void * userData); + static void ReadValueRequestedCb(const char * remoteAddress, int requestId, bt_gatt_server_h server, bt_gatt_h gattHandle, + int offset, void * userData); static void WriteValueRequestedCb(const char * remoteAddress, int requestId, bt_gatt_server_h server, bt_gatt_h gattHandle, bool responseNeeded, int offset, const char * value, int len, void * userData); static void IndicationConfirmationCb(int result, const char * remoteAddress, bt_gatt_server_h server, bt_gatt_h characteristic,