Skip to content

Commit c5d368b

Browse files
committed
macOS: Fix logging; don't register for custom pairing
1 parent 5501787 commit c5d368b

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

src/platform/psmove_port_osx.mm

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
return 1;
107107
}
108108

109-
OSXPAIR_DEBUG("Switching Bluetooth %s...\n", powered?"on":"off");
109+
OSXPAIR_DEBUG("Switching Bluetooth %s...", powered?"on":"off");
110110
IOBluetoothPreferenceSetControllerPowerState(powered);
111111

112112
// Wait a bit for Bluetooth to be (de-)activated
@@ -207,47 +207,57 @@ explicit MacOSVersion(int major=-1, int minor=-1) : major(major), minor(minor) {
207207
bool
208208
psmove_port_register_psmove(char *addr, char *host, enum PSMove_Model_Type model)
209209
{
210-
// TODO: Host is ignored for now
210+
char *tmp = psmove_port_get_host_bluetooth_address();
211+
std::string mac_host_addr = tmp;
212+
free(tmp);
213+
214+
// We can have this check here, since macOS doesn't support multiple BT adapters
215+
// at the same time (connecting an external one might disable the internal one)
216+
if (!_psmove_btaddrs_equal(host, mac_host_addr.c_str())) {
217+
OSXPAIR_DEBUG("Not registering controller: Host address %s is not this machine (%s)",
218+
host, mac_host_addr.c_str());
219+
return false;
220+
}
211221

212222
// TODO: FIXME: If necessary, handle different controller models differently.
213223

214224
ScopedNSAutoreleasePool pool;
215225
std::string btaddr = _psmove_normalize_btaddr_inplace(addr, true, '-');
216226

217227
if (btaddr.length() == 0) {
218-
OSXPAIR_DEBUG("Not a valid Bluetooth address: %s\n", addr);
228+
OSXPAIR_DEBUG("Not a valid Bluetooth address: %s", addr);
219229
return false;
220230
}
221231

222232
auto macos_version = MacOSVersion::running();
223233
if (!macos_version.valid()) {
224-
OSXPAIR_DEBUG("Cannot detect macOS version.\n");
234+
OSXPAIR_DEBUG("Cannot detect macOS version.");
225235
return false;
226236
} else if (macos_version >= MacOSVersion(13, 0)) {
227237
PSMOVE_WARNING("Pairing not yet supported on macOS Ventura, see https://github.com/thp/psmoveapi/issues/457");
228238
return false;
229239
} else if (macos_version < MacOSVersion(10, 7)) {
230-
OSXPAIR_DEBUG("No need to add entry for macOS before 10.7.\n");
240+
OSXPAIR_DEBUG("No need to add entry for macOS before 10.7.");
231241
return false;
232242
}
233243

234-
OSXPAIR_DEBUG("Detected: macOS %d.%d\n", macos_version.major, macos_version.minor);
244+
OSXPAIR_DEBUG("Detected: macOS %d.%d", macos_version.major, macos_version.minor);
235245

236246
if (macosx_blued_is_paired(btaddr)) {
237-
OSXPAIR_DEBUG("Entry for %s already present.\n", btaddr.c_str());
247+
OSXPAIR_DEBUG("Entry for %s already present.", btaddr.c_str());
238248
return true;
239249
}
240250

241251
if (macos_version < MacOSVersion(10, 10)) {
242252
if (!macosx_bluetooth_set_powered(0)) {
243-
OSXPAIR_DEBUG("Cannot shutdown Bluetooth (shut it down manually).\n");
253+
OSXPAIR_DEBUG("Cannot shutdown Bluetooth (shut it down manually).");
244254
}
245255

246-
OSXPAIR_DEBUG("Waiting for blued shutdown (takes ca. 42s) ...\n");
256+
OSXPAIR_DEBUG("Waiting for blued shutdown (takes ca. 42s) ...");
247257
while (macosx_blued_running()) {
248258
psmove_port_sleep_ms(1000);
249259
}
250-
OSXPAIR_DEBUG("blued successfully shutdown.\n");
260+
OSXPAIR_DEBUG("blued successfully shutdown.");
251261
}
252262

253263
std::string command = format("/usr/bin/defaults write %s HIDDevices -array-add %s",
@@ -258,7 +268,7 @@ explicit MacOSVersion(int major=-1, int minor=-1) : major(major), minor(minor) {
258268
command.c_str());
259269
}
260270

261-
OSXPAIR_DEBUG("Running: '%s'\n", command.c_str());
271+
OSXPAIR_DEBUG("Running: '%s'", command.c_str());
262272
bool result = true;
263273
if (system(command.c_str()) != 0) {
264274
OSXPAIR_DEBUG("Could not run the command.");

src/psmove.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,3 +2600,19 @@ psmove_log(const char *filename, int lineno, enum PSMove_LogLevel level, const c
26002600
psmove_vlog(filename, lineno, level, fmt, args);
26012601
va_end(args);
26022602
}
2603+
2604+
bool
2605+
_psmove_btaddrs_equal(const char *addr1, const char *addr2)
2606+
{
2607+
PSMove_Data_BTAddr a1;
2608+
if (_psmove_btaddr_from_string(addr1, &a1) == 0) {
2609+
return false;
2610+
}
2611+
2612+
PSMove_Data_BTAddr a2;
2613+
if (_psmove_btaddr_from_string(addr2, &a2) == 0) {
2614+
return false;
2615+
}
2616+
2617+
return memcmp(a1, a2, sizeof(PSMove_Data_BTAddr)) == 0;
2618+
}

src/psmove_private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ typedef unsigned char PSMove_Data_BTAddr[6];
166166
ADDAPI int
167167
ADDCALL _psmove_btaddr_from_string(const char *string, PSMove_Data_BTAddr *dest);
168168

169+
/* Return true if the two addresses are equal */
170+
ADDAPI bool
171+
ADDCALL _psmove_btaddrs_equal(const char *addr1, const char *addr2);
169172

170173
/**
171174
* Formats the contents of addr to a newly-allocated string and

0 commit comments

Comments
 (0)