summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/input_common/helpers/joycon_protocol/common_protocol.cpp18
-rw-r--r--src/input_common/helpers/joycon_protocol/common_protocol.h20
-rw-r--r--src/input_common/helpers/joycon_protocol/generic_functions.cpp8
3 files changed, 26 insertions, 20 deletions
diff --git a/src/input_common/helpers/joycon_protocol/common_protocol.cpp b/src/input_common/helpers/joycon_protocol/common_protocol.cpp
index 2f0ab6b2a..0ef240344 100644
--- a/src/input_common/helpers/joycon_protocol/common_protocol.cpp
+++ b/src/input_common/helpers/joycon_protocol/common_protocol.cpp
@@ -22,8 +22,8 @@ void JoyconCommonProtocol::SetNonBlocking() {
22} 22}
23 23
24DriverResult JoyconCommonProtocol::GetDeviceType(ControllerType& controller_type) { 24DriverResult JoyconCommonProtocol::GetDeviceType(ControllerType& controller_type) {
25 std::vector<u8> buffer; 25 std::array<u8, 1> buffer{};
26 const auto result = ReadSPI(SpiAddress::DEVICE_TYPE, 1, buffer); 26 const auto result = ReadRawSPI(SpiAddress::DEVICE_TYPE, buffer);
27 controller_type = ControllerType::None; 27 controller_type = ControllerType::None;
28 28
29 if (result == DriverResult::Success) { 29 if (result == DriverResult::Success) {
@@ -148,11 +148,13 @@ DriverResult JoyconCommonProtocol::SendVibrationReport(std::span<const u8> buffe
148 return SendData(local_buffer); 148 return SendData(local_buffer);
149} 149}
150 150
151DriverResult JoyconCommonProtocol::ReadSPI(SpiAddress addr, u8 size, std::vector<u8>& output) { 151DriverResult JoyconCommonProtocol::ReadRawSPI(SpiAddress addr, std::span<u8> output) {
152 constexpr std::size_t HeaderSize = 20;
152 constexpr std::size_t MaxTries = 10; 153 constexpr std::size_t MaxTries = 10;
154 const auto size = output.size();
153 std::size_t tries = 0; 155 std::size_t tries = 0;
154 std::array<u8, 5> buffer = {0x00, 0x00, 0x00, 0x00, size}; 156 std::array<u8, 5> buffer = {0x00, 0x00, 0x00, 0x00, static_cast<u8>(size)};
155 std::vector<u8> local_buffer(size + 20); 157 std::vector<u8> local_buffer{};
156 158
157 buffer[0] = static_cast<u8>(static_cast<u16>(addr) & 0x00FF); 159 buffer[0] = static_cast<u8>(static_cast<u16>(addr) & 0x00FF);
158 buffer[1] = static_cast<u8>((static_cast<u16>(addr) & 0xFF00) >> 8); 160 buffer[1] = static_cast<u8>((static_cast<u16>(addr) & 0xFF00) >> 8);
@@ -167,8 +169,12 @@ DriverResult JoyconCommonProtocol::ReadSPI(SpiAddress addr, u8 size, std::vector
167 } 169 }
168 } while (local_buffer[15] != buffer[0] || local_buffer[16] != buffer[1]); 170 } while (local_buffer[15] != buffer[0] || local_buffer[16] != buffer[1]);
169 171
172 if (local_buffer.size() < size + HeaderSize) {
173 return DriverResult::WrongReply;
174 }
175
170 // Remove header from output 176 // Remove header from output
171 output = std::vector<u8>(local_buffer.begin() + 20, local_buffer.begin() + 20 + size); 177 memcpy(output.data(), local_buffer.data() + HeaderSize, size);
172 return DriverResult::Success; 178 return DriverResult::Success;
173} 179}
174 180
diff --git a/src/input_common/helpers/joycon_protocol/common_protocol.h b/src/input_common/helpers/joycon_protocol/common_protocol.h
index 675eacf68..75d3f20a4 100644
--- a/src/input_common/helpers/joycon_protocol/common_protocol.h
+++ b/src/input_common/helpers/joycon_protocol/common_protocol.h
@@ -97,26 +97,26 @@ public:
97 /** 97 /**
98 * Reads the SPI memory stored on the joycon 98 * Reads the SPI memory stored on the joycon
99 * @param Initial address location 99 * @param Initial address location
100 * @param size in bytes to be read
101 * @returns output buffer containing the responce 100 * @returns output buffer containing the responce
102 */ 101 */
103 DriverResult ReadSPI(SpiAddress addr, u8 size, std::vector<u8>& output); 102 DriverResult ReadRawSPI(SpiAddress addr, std::span<u8> output);
104 103
104 /**
105 * Reads the SPI memory stored on the joycon
106 * @param Initial address location
107 * @returns output object containing the responce
108 */
105 template <typename Output> 109 template <typename Output>
106 requires(std::is_trivially_copyable_v<Output>) 110 requires std::is_trivially_copyable_v<Output> DriverResult ReadSPI(SpiAddress addr,
107 DriverResult ReadSPI(SpiAddress addr, Output& output) { 111 Output& output) {
108 std::vector<u8> buffer; 112 std::array<u8, sizeof(Output)> buffer;
109 output = {}; 113 output = {};
110 114
111 const auto result = ReadSPI(addr, sizeof(Output), buffer); 115 const auto result = ReadRawSPI(addr, buffer);
112 if (result != DriverResult::Success) { 116 if (result != DriverResult::Success) {
113 return result; 117 return result;
114 } 118 }
115 119
116 if (buffer.size() != sizeof(Output)) {
117 return DriverResult::WrongReply;
118 }
119
120 std::memcpy(&output, buffer.data(), sizeof(Output)); 120 std::memcpy(&output, buffer.data(), sizeof(Output));
121 return DriverResult::Success; 121 return DriverResult::Success;
122 } 122 }
diff --git a/src/input_common/helpers/joycon_protocol/generic_functions.cpp b/src/input_common/helpers/joycon_protocol/generic_functions.cpp
index 4763ba3e6..484c208e6 100644
--- a/src/input_common/helpers/joycon_protocol/generic_functions.cpp
+++ b/src/input_common/helpers/joycon_protocol/generic_functions.cpp
@@ -71,8 +71,8 @@ DriverResult GenericProtocol::GetBattery(u32& battery_level) {
71 71
72DriverResult GenericProtocol::GetColor(Color& color) { 72DriverResult GenericProtocol::GetColor(Color& color) {
73 ScopedSetBlocking sb(this); 73 ScopedSetBlocking sb(this);
74 std::vector<u8> buffer; 74 std::array<u8, 12> buffer{};
75 const auto result = ReadSPI(SpiAddress::COLOR_DATA, 12, buffer); 75 const auto result = ReadRawSPI(SpiAddress::COLOR_DATA, buffer);
76 76
77 color = {}; 77 color = {};
78 if (result == DriverResult::Success) { 78 if (result == DriverResult::Success) {
@@ -87,8 +87,8 @@ DriverResult GenericProtocol::GetColor(Color& color) {
87 87
88DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) { 88DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) {
89 ScopedSetBlocking sb(this); 89 ScopedSetBlocking sb(this);
90 std::vector<u8> buffer; 90 std::array<u8, 16> buffer{};
91 const auto result = ReadSPI(SpiAddress::SERIAL_NUMBER, 16, buffer); 91 const auto result = ReadRawSPI(SpiAddress::SERIAL_NUMBER, buffer);
92 92
93 serial_number = {}; 93 serial_number = {};
94 if (result == DriverResult::Success) { 94 if (result == DriverResult::Success) {