From 8e2eca20042c9b2b41c6152c7de01c39ec419622 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 18 Apr 2023 18:36:25 -0400 Subject: [PATCH] haze: abstract firmware version and serial number fetch --- troposphere/haze/include/haze.hpp | 1 + .../haze/include/haze/device_properties.hpp | 26 ++++++++++ troposphere/haze/source/device_properties.cpp | 50 +++++++++++++++++++ troposphere/haze/source/main.cpp | 3 ++ troposphere/haze/source/ptp_responder.cpp | 14 +----- troposphere/haze/source/usb_session.cpp | 10 +--- 6 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 troposphere/haze/include/haze/device_properties.hpp create mode 100644 troposphere/haze/source/device_properties.cpp diff --git a/troposphere/haze/include/haze.hpp b/troposphere/haze/include/haze.hpp index 1d72415f1..be2dca8c7 100644 --- a/troposphere/haze/include/haze.hpp +++ b/troposphere/haze/include/haze.hpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include diff --git a/troposphere/haze/include/haze/device_properties.hpp b/troposphere/haze/include/haze/device_properties.hpp new file mode 100644 index 000000000..fa66ffe06 --- /dev/null +++ b/troposphere/haze/include/haze/device_properties.hpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +namespace haze { + + Result LoadDeviceProperties(); + + const char *GetSerialNumber(); + + const char *GetFirmwareVersion(); + +} diff --git a/troposphere/haze/source/device_properties.cpp b/troposphere/haze/source/device_properties.cpp new file mode 100644 index 000000000..8021fd721 --- /dev/null +++ b/troposphere/haze/source/device_properties.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +namespace haze { + + namespace { + + constinit SetSysSerialNumber g_serial_number = {}; + constinit SetSysFirmwareVersion g_firmware_version = {}; + + } + + Result LoadDeviceProperties() { + /* Initialize set:sys. */ + R_TRY(setsysInitialize()); + + /* Ensure we maintain a clean state on exit. */ + ON_SCOPE_EXIT { setsysExit(); }; + + /* Get the serial number and firmware version. */ + R_TRY(setsysGetSerialNumber(std::addressof(g_serial_number))); + R_TRY(setsysGetFirmwareVersion(std::addressof(g_firmware_version))); + + /* We succeeded. */ + R_SUCCEED(); + } + + const char *GetSerialNumber() { + return g_serial_number.number; + } + + const char *GetFirmwareVersion() { + return g_firmware_version.display_version; + } + +} diff --git a/troposphere/haze/source/main.cpp b/troposphere/haze/source/main.cpp index f2eaf732c..1cab75b9b 100644 --- a/troposphere/haze/source/main.cpp +++ b/troposphere/haze/source/main.cpp @@ -17,6 +17,9 @@ #include int main(int argc, char **argv) { + /* Load device firmware version and serial number. */ + HAZE_R_ABORT_UNLESS(haze::LoadDeviceProperties()); + /* Run the application. */ haze::ConsoleMainLoop::RunApplication(); diff --git a/troposphere/haze/source/ptp_responder.cpp b/troposphere/haze/source/ptp_responder.cpp index 4064242fa..22d732cc0 100644 --- a/troposphere/haze/source/ptp_responder.cpp +++ b/troposphere/haze/source/ptp_responder.cpp @@ -304,16 +304,6 @@ namespace haze { Result PtpResponder::GetDeviceInfo(PtpDataParser &dp) { PtpDataBuilder db(g_bulk_write_buffer, std::addressof(m_usb_server)); - /* Initialize set:sys, ensuring we clean up on exit. */ - R_TRY(setsysInitialize()); - ON_SCOPE_EXIT { setsysExit(); }; - - /* Get the device version and serial number. */ - SetSysFirmwareVersion version; - SetSysSerialNumber serial; - R_TRY(setsysGetFirmwareVersion(std::addressof(version))); - R_TRY(setsysGetSerialNumber(std::addressof(serial))); - /* Write the device info data. */ R_TRY(db.WriteVariableLengthData(m_request_header, [&] () { R_TRY(db.Add(MtpStandardVersion)); @@ -328,8 +318,8 @@ namespace haze { R_TRY(db.AddArray(SupportedPlaybackFormats, util::size(SupportedPlaybackFormats))); R_TRY(db.AddString(MtpDeviceManufacturer)); R_TRY(db.AddString(MtpDeviceModel)); - R_TRY(db.AddString(version.display_version)); - R_TRY(db.AddString(serial.number)); + R_TRY(db.AddString(GetFirmwareVersion())); + R_TRY(db.AddString(GetSerialNumber())); R_SUCCEED(); })); diff --git a/troposphere/haze/source/usb_session.cpp b/troposphere/haze/source/usb_session.cpp index 18368ed32..3c0244f5f 100644 --- a/troposphere/haze/source/usb_session.cpp +++ b/troposphere/haze/source/usb_session.cpp @@ -165,19 +165,11 @@ namespace haze { static const u16 supported_langs[1] = { 0x0409 }; R_TRY(usbDsAddUsbLanguageStringDescriptor(nullptr, supported_langs, util::size(supported_langs))); - /* Initialize set:sys, ensuring we clean up on exit. */ - R_TRY(setsysInitialize()); - ON_SCOPE_EXIT { setsysExit(); }; - - /* Get the device serial number. */ - SetSysSerialNumber serial; - R_TRY(setsysGetSerialNumber(std::addressof(serial))); - /* Report strings. */ u8 iManufacturer, iProduct, iSerialNumber; R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iManufacturer), "Nintendo")); R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iProduct), "Nintendo Switch")); - R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iSerialNumber), serial.number)); + R_TRY(usbDsAddUsbStringDescriptor(std::addressof(iSerialNumber), GetSerialNumber())); /* Send device descriptors */ struct usb_device_descriptor device_descriptor = {