Skip to content

Commit bf28e37

Browse files
joevtdingusdev
authored andcommitted
adbdevice: Remove set_register_3 duplicate code.
1 parent 8413e92 commit bf28e37

5 files changed

Lines changed: 25 additions & 31 deletions

File tree

‎devices/common/adb/adbapplejack.cpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ bool AdbAppleJack::get_register_0() {
7777
}
7878

7979
void AdbAppleJack::set_register_3() {
80-
if (this->host_obj->get_input_count() < 2) // ensure we got enough data
81-
return;
82-
8380
const uint8_t* in_data = this->host_obj->get_input_buf();
8481

8582
switch (in_data[1]) {

‎devices/common/adb/adbdevice.cpp‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2121

2222
/** @file Base class for Apple Desktop Bus devices. */
2323

24+
#include <loguru.hpp>
2425
#include <core/timermanager.h>
2526
#include <devices/common/adb/adbdevice.h>
2627
#include <devices/common/adb/adbbus.h>
@@ -92,7 +93,8 @@ void AdbDevice::listen(const uint8_t dev_addr, const uint8_t reg_num) {
9293
this->set_register_2();
9394
break;
9495
case 3:
95-
this->set_register_3();
96+
if (this->host_obj->get_input_count() >= 2) // ensure we got enough data
97+
this->set_register_3();
9698
break;
9799
}
98100
}
@@ -107,6 +109,25 @@ bool AdbDevice::get_register_3() {
107109
return true;
108110
}
109111

112+
void AdbDevice::set_register_3() {
113+
const uint8_t* in_data = this->host_obj->get_input_buf();
114+
uint8_t new_addr = in_data[0] & 0xF;
115+
116+
switch (in_data[1]) {
117+
case 0:
118+
this->my_addr = new_addr;
119+
this->srq_flag = !!(in_data[0] & 0x20);
120+
break;
121+
case 0xFE: // move to a new address if there was no collision
122+
if (!this->got_collision) {
123+
this->my_addr = new_addr;
124+
}
125+
break;
126+
default:
127+
LOG_F(WARNING, "ADB Device: unknown handler ID = 0x%X", in_data[1]);
128+
}
129+
}
130+
110131
uint8_t AdbDevice::gen_random_address() {
111132
return TimerManager::get_instance()->current_time_ns() & 0xF;
112133
}

‎devices/common/adb/adbdevice.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class AdbDevice : public HWComponent {
6464
virtual void set_register_0() {}
6565
virtual void set_register_1() {}
6666
virtual void set_register_2() {}
67-
virtual void set_register_3() {}
67+
virtual void set_register_3();
6868

6969
uint8_t exc_event_flag = 0;
7070
uint8_t srq_flag = 0;

‎devices/common/adb/adbkeyboard.cpp‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,29 +147,17 @@ bool AdbKeyboard::get_register_3() {
147147
}
148148

149149
void AdbKeyboard::set_register_3() {
150-
if (this->host_obj->get_input_count() < 2) // ensure we got enough data
151-
return;
152-
153150
const uint8_t* in_data = this->host_obj->get_input_buf();
154151

155152
switch (in_data[1]) {
156-
case 0:
157-
this->my_addr = in_data[0] & 0xF;
158-
this->srq_flag = !!(in_data[0] & 0x20);
159-
break;
160153
case 1:
161154
case 2:
162155
this->dev_handler_id = in_data[1];
163156
break;
164157
case 3: // extended keyboard protocol isn't supported yet
165158
break;
166-
case 0xFE: // move to a new address if there was no collision
167-
if (!this->got_collision) {
168-
this->my_addr = in_data[0] & 0xF;
169-
}
170-
break;
171159
default:
172-
LOG_F(WARNING, "%s: unknown handler ID = 0x%X", this->name.c_str(), in_data[1]);
160+
this->AdbDevice::set_register_3();
173161
}
174162
}
175163

‎devices/common/adb/adbmouse.cpp‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,16 @@ bool AdbMouse::get_register_1() {
142142
}
143143

144144
void AdbMouse::set_register_3() {
145-
if (this->host_obj->get_input_count() < 2) // ensure we got enough data
146-
return;
147-
148145
const uint8_t* in_data = this->host_obj->get_input_buf();
149146

150147
switch (in_data[1]) {
151-
case 0:
152-
this->my_addr = in_data[0] & 0xF;
153-
this->srq_flag = !!(in_data[0] & 0x20);
154-
break;
155148
case 1:
156149
case 2:
157150
case 4: // switch over to extended mouse protocol
158151
this->dev_handler_id = in_data[1];
159152
break;
160-
case 0xFE: // move to a new address if there was no collision
161-
if (!this->got_collision) {
162-
this->my_addr = in_data[0] & 0xF;
163-
}
164-
break;
165153
default:
166-
LOG_F(WARNING, "%s: unknown handler ID = 0x%X", this->name.c_str(), in_data[1]);
154+
this->AdbDevice::set_register_3();
167155
}
168156
}
169157

0 commit comments

Comments
 (0)