I have developed a code using the libusb library to send USB packets to a Samsung device. Initially, I tested it with my Samsung Note 10. After running the code, the device transitioned to accessory mode, as indicated by the output of the lsusb command:
Before running the code:
Bus 001 Device 079: ID 04e8:6860 Samsung Electronics Co., Ltd Galaxy series, misc. (MTP mode)
After running the code:
Bus 001 Device 080: ID 18d1:2d01 Google Inc. Android Open Accessory device (accessory + ADB)
However, when attempting to use the same code with my Samsung Fold 4, the device did not transition to accessory mode. I'm uncertain whether the Fold 4 supports accessory mode. Could someone confirm this? Additionally, I would appreciate any insights into what might be wrong with my code.
#include <iostream>
#include <cstring>
#include <libusb-1.0/libusb.h>
// Define vendor and product IDs for Samsung devices
#define SAMSUNG_ACCESSORY_VID 0x04e8 // Samsung's vendor ID
#define SAMSUNG_ACCESSORY_PID 0x6860 // PID for accessory mode
// Define Android accessory protocol requests
#define ACCESSORY_GET_PROTOCOL 51
#define ACCESSORY_SEND_STRING 52
#define ACCESSORY_START 53
// Define accessory identification strings
#define MANUFACTURER "YourManufacturer"
#define MODEL "YourModel"
#define DESCRIPTION "YourDescription"
#define VERSION "1.0"
#define URI "http://your.uri"
#define SERIAL_NUMBER "YourSerialNumber"
// Function to send accessory command to the device
void send_accessory_command(libusb_device_handle* handle, int request, const char* data) {
libusb_control_transfer(handle,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
request, 0, 0, (unsigned char*)data, data ? strlen(data) + 1 : 0, 0);
}
int main() {
libusb_device_handle* handle = nullptr;
libusb_context* ctx = nullptr;
// Initialize libusb
int r = libusb_init(&ctx);
if (r < 0) {
std::cerr << "Error initializing libusb: " << libusb_error_name(r) << std::endl;
return 1;
}
// Try to open the Samsung Fold 4 device in accessory mode
handle = libusb_open_device_with_vid_pid(ctx, SAMSUNG_ACCESSORY_VID, SAMSUNG_ACCESSORY_PID);
if (!handle) {
std::cerr << "Could not find Samsung Fold 4 device in accessory mode." << std::endl;
libusb_exit(ctx);
return 1;
}
// Check accessory protocol version
unsigned char protocol[2];
int protocolVersion = 0;
r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
ACCESSORY_GET_PROTOCOL, 0, 0, protocol, sizeof(protocol), 0);
if (r >= 0) {
protocolVersion = protocol[1] << 8 | protocol[0];
}
std::cerr << "Accessory protocol: " << protocolVersion << std::endl;
// Check if accessory protocol is supported
if (protocolVersion < 1) {
std::cerr << "Accessory protocol not supported." << std::endl;
libusb_release_interface(handle, 0);
libusb_close(handle);
libusb_exit(ctx);
return 1;
}
// Send accessory identification strings
send_accessory_command(handle, ACCESSORY_SEND_STRING, MANUFACTURER);
send_accessory_command(handle, ACCESSORY_SEND_STRING, MODEL);
send_accessory_command(handle, ACCESSORY_SEND_STRING, DESCRIPTION);
send_accessory_command(handle, ACCESSORY_SEND_STRING, VERSION);
send_accessory_command(handle, ACCESSORY_SEND_STRING, URI);
send_accessory_command(handle, ACCESSORY_SEND_STRING, SERIAL_NUMBER);
// Switch the device to accessory mode
send_accessory_command(handle, ACCESSORY_START, NULL);
// Output message indicating successful switch to accessory mode
std::cout << "Switched to accessory mode." << std::endl;
// Clean up libusb resources
libusb_release_interface(handle, 0);
libusb_close(handle);
libusb_exit(ctx);
return 0;
}