Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows fixes #35

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
common --enable_platform_specific_config
build --strip=never
build --incompatible_enable_cc_toolchain_resolution

Expand All @@ -20,6 +21,5 @@ test:ppc --run_under="qemu-ppc -L /usr/powerpc-linux-gnu/ "
test:ppc64 --run_under="qemu-ppc64 -L /usr/powerpc64-linux-gnu/ "
test:ppc64le --run_under="qemu-ppc64le -L /usr/powerpc64le-linux-gnu/ "


# parallel tests break on a single vcan interface
test: --local_test_jobs=1 --test_output=all
test: --local_test_jobs=1 --test_output=all
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:

jobs:
build:

runs-on: ubuntu-latest

steps:
Expand All @@ -22,4 +21,13 @@ jobs:
- name: run unit tests
run: bazel test //test:all --test_tag_filters=-vcan

build-windows:
runs-on: windows-latest

steps:
- name: checkout
uses: actions/checkout@v3

- name: run unit tests
run: bazel test //test:all --verbose_failures --test_output=all

18 changes: 10 additions & 8 deletions amalgamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def strip_includes(src):
return re.sub(r'#include ".*', "", src, flags=re.MULTILINE)

isotp_c_wrapped_c = "#if defined(UDS_ISOTP_C)\n" + \
strip_includes(open("src/tp/isotp-c/isotp.c").read()) + \
strip_includes(open("src/tp/isotp-c/isotp.c", encoding="utf-8").read()) + \
"#endif\n"

isotp_c_wrapped_h = "#if defined(UDS_ISOTP_C)\n" + \
Expand All @@ -30,7 +30,7 @@ def strip_includes(src):
]]) + \
"#endif\n"

with open(args.out_c, "w") as f:
with open(args.out_c, "w", encoding="utf-8") as f:
f.write("#include \"iso14229.h\"\n")
for src in [
"src/client.c",
Expand All @@ -46,15 +46,16 @@ def strip_includes(src):
f.write("#ifdef UDS_LINES\n")
f.write(f'#line 1 "{src}"' + "\n")
f.write("#endif\n")
with open(src) as src_file:
f.write(strip_includes(src_file.read()))
with open(src, "r", encoding="utf-8") as src_file:
stripped = strip_includes(src_file.read())
f.write(stripped)
f.write("\n")

f.write(isotp_c_wrapped_c)
f.write("\n")


with open(args.out_h, "w") as f:
with open(args.out_h, "w", encoding="utf-8") as f:
f.write("#ifndef ISO14229_H\n")
f.write("#define ISO14229_H\n")
f.write("\n")
Expand All @@ -77,8 +78,9 @@ def strip_includes(src):
"src/server.h",
]:
f.write("\n")
with open(src) as src_file:
f.write(strip_includes(src_file.read()))
with open(src, "r", encoding="utf-8") as src_file:
stripped = strip_includes(src_file.read())
f.write(stripped)
f.write("\n")

f.write(isotp_c_wrapped_h)
Expand Down Expand Up @@ -106,4 +108,4 @@ def strip_includes(src):
# os.chmod(iso14229_c, 0o444)

if __name__ == "__main__":
print(f"amalgamated source files written to {args.out_c} and {args.out_h}")
print(f"amalgamated source files written to {args.out_c} and {args.out_h}")
2 changes: 1 addition & 1 deletion run_clang_format.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/bash
#!/bin/bash

files=`find src -type f \( -name '*.c' -o -name '*.h' \) -not -path "src/tp/isotp-c/*"`

Expand Down
10 changes: 5 additions & 5 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define UDS_CLIENT_DEFAULT_P2_STAR_MS (1500U)
#endif

_Static_assert(UDS_CLIENT_DEFAULT_P2_STAR_MS > UDS_CLIENT_DEFAULT_P2_MS, "");
static_assert(UDS_CLIENT_DEFAULT_P2_STAR_MS > UDS_CLIENT_DEFAULT_P2_MS, "");

#ifndef UDS_SERVER_DEFAULT_POWER_DOWN_TIME_MS
#define UDS_SERVER_DEFAULT_POWER_DOWN_TIME_MS (10)
Expand All @@ -41,10 +41,10 @@ _Static_assert(UDS_CLIENT_DEFAULT_P2_STAR_MS > UDS_CLIENT_DEFAULT_P2_MS, "");
#define UDS_SERVER_DEFAULT_S3_MS (3000)
#endif

_Static_assert(0 < UDS_SERVER_DEFAULT_P2_MS &&
UDS_SERVER_DEFAULT_P2_MS < UDS_SERVER_DEFAULT_P2_STAR_MS &&
UDS_SERVER_DEFAULT_P2_STAR_MS < UDS_SERVER_DEFAULT_S3_MS,
"");
static_assert(0 < UDS_SERVER_DEFAULT_P2_MS &&
UDS_SERVER_DEFAULT_P2_MS < UDS_SERVER_DEFAULT_P2_STAR_MS &&
UDS_SERVER_DEFAULT_P2_STAR_MS < UDS_SERVER_DEFAULT_S3_MS,
"");

// Amount of time to wait after boot before accepting 0x27 requests.
#ifndef UDS_SERVER_0x27_BRUTE_FORCE_MITIGATION_BOOT_DELAY_MS
Expand Down
1 change: 1 addition & 0 deletions src/sys_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#if UDS_SYS == UDS_SYS_UNIX

#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
Expand Down
14 changes: 13 additions & 1 deletion src/sys_win32.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#pragma once

#if UDS_SYS == UDS_SYS_WIN32
#if UDS_SYS == UDS_SYS_WINDOWS

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;

#ifdef _MSC_VER
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif

#endif
6 changes: 3 additions & 3 deletions src/tp/isotp_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,18 @@ static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid, bool
perror("setsockopt");
return -1;
}

struct can_isotp_options opts;
memset(&opts, 0, sizeof(opts));
// configure socket to wait for tx completion to catch FC frame timeouts
opts.flags |= CAN_ISOTP_WAIT_TX_DONE;

if (functional) {
printf("configuring fd: %d as functional\n", fd);
// configure the socket as listen-only to avoid sending FC frames
opts.flags |= CAN_ISOTP_LISTEN_MODE;
}

if (setsockopt(fd, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts)) < 0) {
perror("setsockopt (isotp_options):");
return -1;
Expand Down
31 changes: 24 additions & 7 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ cc_library(
],
deps = [ "@cmocka" ],
defines = [
"UDS_TP_ISOTP_C_SOCKETCAN",
"UDS_TP_ISOTP_SOCK",
"UDS_TP_MOCK",
"UDS_CUSTOM_MILLIS",
"UDS_ENABLE_DBG_PRINT",
"UDS_ENABLE_ASSERT",
# "UDS_LINES",
],
copts = [ "-g", ],
] + select({
"//conditions:default": [
"UDS_TP_ISOTP_C_SOCKETCAN",
"UDS_TP_ISOTP_SOCK",
],
"@bazel_tools//src/conditions:windows": [
],
}),
copts = select({
"//conditions:default": ["-g"],
"@bazel_tools//src/conditions:windows": [
],
}),
)

TEST_SRCS = [
Expand Down Expand Up @@ -53,7 +62,11 @@ TEST_NAMES = [ src.split(".c")[0] for src in TEST_SRCS ]
deps=[":env"],
size = "small",
env = { "UDS_TP_TYPE": "0", },
copts = ["-g"],
copts = select({
"//conditions:default": ["-g"],
"@bazel_tools//src/conditions:windows": [
],
}),
tags = [ "exclusive", ],
) for name, src in zip(TEST_NAMES, TEST_SRCS)
]
Expand All @@ -67,6 +80,7 @@ TEST_NAMES = [ src.split(".c")[0] for src in TEST_SRCS ]
env = { "UDS_TP_TYPE": "1"},
copts = ["-g"],
tags = [ "exclusive", "vcan", "isotp_sock"],
target_compatible_with = ["@platforms//os:linux"],
) for name, src in zip(TEST_NAMES, TEST_SRCS)
]

Expand All @@ -79,6 +93,7 @@ TEST_NAMES = [ src.split(".c")[0] for src in TEST_SRCS ]
env = { "UDS_TP_TYPE": "2"},
copts = ["-g"],
tags = [ "exclusive", "vcan"],
target_compatible_with = ["@platforms//os:linux"],
) for name, src in zip(TEST_NAMES, TEST_SRCS)
]

Expand Down Expand Up @@ -115,8 +130,10 @@ sh_test(
args = ["$(locations //:iso14229)"],
size = "small",

# Not exactly right. It's to prevent this test from being run under qemu
target_compatible_with = ["@platforms//cpu:x86_64"],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)

cc_library(
Expand Down
8 changes: 7 additions & 1 deletion test/cmocka.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ cc_library(
],
copts = [
"-DHAVE_SIGNAL_H",
],
] + select({
"@bazel_tools//src/conditions:windows": [
"-DHAVE__SNPRINTF_S",
"-DHAVE__VSNPRINTF_S",
],
"//conditions:default": [],
}),
includes = [
"include",
],
Expand Down
29 changes: 27 additions & 2 deletions test/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static UDSServer_t *registeredServer = NULL;
static UDSClient_t *registeredClient = NULL;
Expand Down Expand Up @@ -72,7 +71,13 @@ void ENV_RegisterClient(UDSClient_t *client) { registeredClient = client; }
uint32_t UDSMillis() { return TimeNowMillis; }

// actually sleep for milliseconds
void msleep(int ms) { usleep(ms * 1000); }
void msleep(int ms) {
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}

static bool IsNetworkedTransport(int tp_type) {
return tp_type == ENV_TP_TYPE_ISOTP_SOCK || tp_type == ENV_TP_TYPE_ISOTPC;
Expand Down Expand Up @@ -113,22 +118,30 @@ UDSTpHandle_t *ENV_TpNew(const char *name) {
tp = TPMockNew("server", TPMOCK_DEFAULT_SERVER_ARGS);
break;
case ENV_TP_TYPE_ISOTP_SOCK: {
#ifdef UDS_TP_ISOTP_SOCK
UDSTpIsoTpSock_t *isotp = malloc(sizeof(UDSTpIsoTpSock_t));
strcpy(isotp->tag, "server");
assert(UDS_OK == UDSTpIsoTpSockInitServer(isotp, opts.ifname, opts.srv_src_addr,
opts.srv_target_addr,
opts.srv_src_addr_func));
tp = (UDSTpHandle_t *)isotp;
break;
#endif
printf("isotp_sock not supported\n");
return NULL;
}
case ENV_TP_TYPE_ISOTPC: {
#ifdef UDS_TP_ISOTP_C_SOCKETCAN
UDSTpISOTpC_t *isotp = malloc(sizeof(UDSTpISOTpC_t));
strcpy(isotp->tag, "server");

assert(UDS_OK == UDSTpISOTpCInit(isotp, opts.ifname, opts.srv_src_addr,
opts.srv_target_addr, opts.srv_src_addr_func, 0));
tp = (UDSTpHandle_t *)isotp;
break;
#endif
printf("isotp_c_socketcan not supported\n");
return NULL;
}
default:
printf("unknown TP type: %d\n", opts.tp_type);
Expand All @@ -140,21 +153,29 @@ UDSTpHandle_t *ENV_TpNew(const char *name) {
tp = TPMockNew("client", TPMOCK_DEFAULT_CLIENT_ARGS);
break;
case ENV_TP_TYPE_ISOTP_SOCK: {
#ifdef UDS_TP_ISOTP_SOCK
UDSTpIsoTpSock_t *isotp = malloc(sizeof(UDSTpIsoTpSock_t));
strcpy(isotp->tag, "client");
assert(UDS_OK == UDSTpIsoTpSockInitClient(isotp, opts.ifname, opts.cli_src_addr,
opts.cli_target_addr,
opts.cli_tgt_addr_func));
tp = (UDSTpHandle_t *)isotp;
break;
#endif
printf("isotp_sock not supported\n");
return NULL;
}
case ENV_TP_TYPE_ISOTPC: {
#ifdef UDS_TP_ISOTP_C_SOCKETCAN
UDSTpISOTpC_t *isotp = malloc(sizeof(UDSTpISOTpC_t));
strcpy(isotp->tag, "client");
assert(UDS_OK == UDSTpISOTpCInit(isotp, opts.ifname, opts.cli_src_addr,
opts.cli_target_addr, 0, opts.cli_tgt_addr_func));
tp = (UDSTpHandle_t *)isotp;
break;
#endif
printf("isotp_c_socketcan not supported\n");
return NULL;
}
default:
printf("unknown TP type: %d\n", opts.tp_type);
Expand All @@ -174,11 +195,15 @@ void ENV_TpFree(UDSTpHandle_t *tp) {
TPMockFree(tp);
break;
case ENV_TP_TYPE_ISOTP_SOCK:
#ifdef UDS_TP_ISOTP_SOCK
UDSTpIsoTpSockDeinit((UDSTpIsoTpSock_t *)tp);
#endif
break;
case ENV_TP_TYPE_ISOTPC:
#ifdef UDS_TP_ISOTP_C_SOCKETCAN
UDSTpISOTpCDeinit((UDSTpISOTpC_t *)tp);
free(tp);
#endif
break;
}
}
Expand Down
Loading