Skip to content

Commit

Permalink
libj1939: add libj1939_parse_canaddr and reuse it by testj1939
Browse files Browse the repository at this point in the history
this function will be needed jcat

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
  • Loading branch information
olerem committed Jan 4, 2019
1 parent c45a17e commit e649b6e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
21 changes: 21 additions & 0 deletions libj1939.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ static int libj1939_ifindex(const char *str)
return 0;
}

void libj1939_parse_canaddr(char *spec, struct sockaddr_can *paddr)
{
char *str;

str = strsep(&spec, ":");
if (strlen(str))
paddr->can_ifindex = if_nametoindex(str);

str = strsep(&spec, ",");
if (str && strlen(str))
paddr->can_addr.j1939.addr = strtoul(str, NULL, 0);

str = strsep(&spec, ",");
if (str && strlen(str))
paddr->can_addr.j1939.pgn = strtoul(str, NULL, 0);

str = strsep(&spec, ",");
if (str && strlen(str))
paddr->can_addr.j1939.name = strtoul(str, NULL, 0);
}

int libj1939_str2addr(const char *str, char **endp, struct sockaddr_can *can)
{
char *p;
Expand Down
1 change: 1 addition & 0 deletions libj1939.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
extern "C" {
#endif

void libj1939_parse_canaddr(char *spec, struct sockaddr_can *paddr);
extern int libj1939_str2addr(const char *str, char **endp, struct sockaddr_can *can);
extern const char *libj1939_addr2str(const struct sockaddr_can *can);

Expand Down
25 changes: 2 additions & 23 deletions testj1939.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,6 @@ static const char help_msg[] =

static const char optstring[] = "?vbos::rep:cnw::";

static void parse_canaddr(char *spec, struct sockaddr_can *paddr)
{
char *str;

str = strsep(&spec, ":");
if (strlen(str))
paddr->can_ifindex = if_nametoindex(str);

str = strsep(&spec, ",");
if (str && strlen(str))
paddr->can_addr.j1939.addr = strtoul(str, NULL, 0);

str = strsep(&spec, ",");
if (str && strlen(str))
paddr->can_addr.j1939.pgn = strtoul(str, NULL, 0);

str = strsep(&spec, ",");
if (str && strlen(str))
paddr->can_addr.j1939.name = strtoul(str, NULL, 0);
}

static void onsigalrm(int sig)
{
error(0, 0, "exit as requested");
Expand Down Expand Up @@ -157,7 +136,7 @@ int main(int argc, char *argv[])

if (argv[optind]) {
if (strcmp("-", argv[optind]))
parse_canaddr(argv[optind], &sockname);
libj1939_parse_canaddr(argv[optind], &sockname);
++optind;
}

Expand All @@ -166,7 +145,7 @@ int main(int argc, char *argv[])

if (argv[optind]) {
if (strcmp("-", argv[optind])) {
parse_canaddr(argv[optind], &peername);
libj1939_parse_canaddr(argv[optind], &peername);
valid_peername = 1;
}
++optind;
Expand Down

0 comments on commit e649b6e

Please sign in to comment.