Skip to content

Commit

Permalink
Provide serial_port_get_description() function
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Apr 5, 2024
1 parent 342fdf8 commit bf4f9c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions native/src/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ cJSON *serial_list_ports_json() {

const char *name = sp_get_port_name(port);
cJSON_AddStringToObject(item, "name", name);
const char *description = sp_get_port_description(port);
cJSON_AddStringToObject(item, "description", description);
enum sp_transport transport = sp_get_port_transport(port);
cJSON_AddStringToObject(item, "transport", SP_TRANSPORT_STR[transport]);

if (serial_port_get_description != NULL) {
char *description = serial_port_get_description(port);
cJSON_AddStringToObject(item, "description", description);
free(description);
} else {
const char *description = sp_get_port_description(port);
cJSON_AddStringToObject(item, "description", description);
}

switch (transport) {
case SP_TRANSPORT_NATIVE:
break;
Expand Down
1 change: 1 addition & 0 deletions native/src/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const char *serial_auth_grant(const char *port_name);
void serial_auth_revoke(const char *port_name);

char *serial_port_get_id(struct sp_port *port);
__attribute__((weak)) char *serial_port_get_description(struct sp_port *port);
__attribute__((weak)) void serial_port_fix_details(struct sp_port *port, const char *id);

serial_port_t *serial_get_by_auth(const char *auth_key);
Expand Down
20 changes: 20 additions & 0 deletions native/src/serial_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,24 @@ char *serial_port_get_id(struct sp_port *port) {
}
}

char *serial_port_get_description(struct sp_port *port) {
const char *name = sp_get_port_name(port);
const char *description = sp_get_port_description(port);

if (strstr(name, "/dev/") == name)
name += sizeof("/dev/") - 1;

char *full_description = malloc(strlen(name) + sizeof(" - ") + strlen(description));
if (full_description == NULL)
return NULL;

full_description[0] = '\0';
if (strstr(description, name) == NULL) {
strcat(full_description, name);
strcat(full_description, " - ");
}
strcat(full_description, description);
return full_description;
}

#endif

0 comments on commit bf4f9c0

Please sign in to comment.