Skip to content

Commit

Permalink
Implement serial_port_get_id() on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Apr 5, 2024
1 parent 4463ddb commit 342fdf8
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion native/src/serial_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,61 @@
#include "serial.h"

char *serial_port_get_id(struct sp_port *port) {
return port->name;
const char *name = sp_get_port_name(port);
enum sp_transport transport = sp_get_port_transport(port);

int id_length = strlen(name);

switch (transport) {
case SP_TRANSPORT_USB: {
int vid = 0, pid = 0;
const char *serial = sp_get_port_usb_serial(port);
if (sp_get_port_usb_vid_pid(port, &vid, &pid) == SP_OK)
id_length += sizeof("#VID=xxxx#PID=xxxx") - 1;
if (serial != NULL)
id_length += sizeof("#SN=") - 1 + strlen(serial);

char *id = malloc(id_length + 1);
if (id == NULL)
return NULL;

if (vid != 0 && pid != 0) {
if (serial != NULL)
sprintf(id, "%s#VID=%04X#PID=%04X#SN=%s", name, vid, pid, serial);
else
sprintf(id, "%s#VID=%04X#PID=%04X", name, vid, pid);
} else {
if (serial != NULL)
sprintf(id, "%s#SN=%s", name, serial);
else
strcpy(id, name);
}

return id;
}

case SP_TRANSPORT_BLUETOOTH: {
const char *address = sp_get_port_bluetooth_address(port);
if (address != NULL)
id_length += sizeof("#ADDR=") - 1 + strlen(address);

char *id = malloc(id_length + 1);
if (id == NULL)
return NULL;

if (address != NULL)
sprintf(id, "%s#ADDR=%s", name, address);
else
strcpy(id, name);

return id;
}

case SP_TRANSPORT_NATIVE:
default: {
return strdup(port->name);
}
}
}

#endif

0 comments on commit 342fdf8

Please sign in to comment.