Skip to content

Commit

Permalink
restructure source directory
Browse files Browse the repository at this point in the history
  • Loading branch information
driftregion committed Dec 10, 2023
1 parent 12db911 commit abb4dbf
Show file tree
Hide file tree
Showing 58 changed files with 3,445 additions and 3,907 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ corpus
.gdb_history
fuzzer
*.o
.vscode
.vscode
**/*.cache
**/build
**/sdkconfig*
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "tp/isotp-c"]
path = tp/isotp-c
path = src/tp/isotp-c
url = https://github.com/driftregion/isotp-c.git
65 changes: 41 additions & 24 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
package(default_visibility = ["//visibility:public"])
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")

genrule(
name = "isotp_c_wrapped_c",
srcs = glob(["src/tp/isotp-c/*.c"]),
outs = ["isotp_c_wrapped.c"],
cmd = "echo '#if defined(UDS_TP) && UDS_TP==UDS_TP_ISOTP_C' >> $(OUTS) ; for f in $(SRCS); do cat $$f >> $(OUTS); done ; echo '#endif' >> $(OUTS)",
)

genrule(
name = "isotp_c_wrapped_h",
srcs = glob(["src/tp/isotp-c/*.h"]),
outs = ["isotp_c_wrapped.h"],
cmd = "echo '#if defined(UDS_TP) && UDS_TP==UDS_TP_ISOTP_C' >> $(OUTS) ; for f in $(SRCS); do cat $$f >> $(OUTS); done ; echo '#endif' >> $(OUTS)",
)

genrule(
name = "c_src",
srcs = glob(["src/*.c", "src/tp/*.c"]) + [
":isotp_c_wrapped_c",
],
outs = ["iso14229.c"],
cmd = "(echo '#include \"iso14229.h\"'; (for f in $(SRCS); do echo; echo '#ifdef UDS_LINES'; echo \"#line 1 \\\"$$f\"\\\"; echo '#endif'; cat $$f | sed -e 's,#include \".*,,'; done)) > $(OUTS)",
)

genrule(
name = "h_src",
srcs = glob(["src/*.h", "src/tp/*.h"]) + [
":isotp_c_wrapped_h",
],
outs = ["iso14229.h"],
cmd = "echo $(SRCS); (cat ; echo '#ifndef ISO14229_H'; echo '#define ISO14229_H'; echo; echo '#ifdef __cplusplus'; echo 'extern \"C\" {'; echo '#endif'; cat src/sys.h src/sys_arduino.h src/sys_unix.h src/sys_win32.h src/sys_esp32.h src/config.h src/util.h src/tp.h src/uds.h src/client.h src/server.h $(location //:isotp_c_wrapped_h) src/tp/*.h |sed -e 's,#include \".*,,' -e 's,^#pragma once,,' ; echo '#endif'; echo '#ifdef __cplusplus'; echo '}'; echo '#endif';) > $(OUTS)",
)

filegroup(
name = "iso14229_srcs",
name = "srcs",
srcs = [
"iso14229.c",
"iso14229.h",
"iso14229serverbufferedwriter.h",
],
)

cc_test(
name="test_all",
srcs=[
":iso14229_srcs",
"test_iso14229.c",
],
deps = [
"//tp:mock",
],
copts=[
"-Wall",
"-Wextra",
"-Wno-missing-field-initializers",
"-Werror",
"-Wno-unused-parameter",
],
defines=[
"UDS_TP=UDS_TP_CUSTOM",
"UDS_CUSTOM_MILLIS",
cc_library(
name="iso14229",
srcs = [
"iso14229.c",
"iso14229.h",
],
size = "small",
)

cc_test(
Expand All @@ -43,9 +58,11 @@ cc_test(
size = "small",
)


cc_library(
name="iso14229",
srcs=[":iso14229_srcs"],
name="iso14229_2",
srcs=glob(["src/**/*.c", "src/**/*.h"]),
copts=['-Isrc'],
)


Expand Down
58 changes: 19 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,37 @@
<a href="README_zh.md">简体中文</a>
</p>

iso14229 is a server and client session-layer implementation of (ISO14229-1:2013) targeting embedded systems. It is tested with [`isotp-c`](https://github.com/lishen2/isotp-c) as well as [linux kernel](https://github.com/linux-can/can-utils/blob/master/include/linux/can/isotp.h) ISO15765-2 (ISO-TP) transport layer implementations.
iso14229 is an implementation of UDS (ISO14229-1:2013) targeting embedded systems. It is tested with [`isotp-c`](https://github.com/lishen2/isotp-c) as well as [linux kernel](https://github.com/linux-can/can-utils/blob/master/include/linux/can/isotp.h) ISO15765-2 (ISO-TP) transport layer implementations.

API status: **stabilizing**
API status: **not yet stable**.

## quickstart: server
## Features

```c
#include "iso14229.h"

static uint8_t fn(UDSServer_t *srv, UDSServerEvent_t ev, const void *arg) {
switch (ev) {
case UDS_SRV_EVT_EcuReset: { // 0x10
UDSECUResetArgs_t *r = (UDSECUResetArgs_t *)arg;
printf("got ECUReset request of type %x\n", r->type);
return kPositiveResponse;
default:
return kServiceNotSupported;
}
}
}
- static memory allocation. does not use `malloc`, `calloc`
- highly portable. tested on arm, x86-64, ppc, ppc64
- easy to integrate. Download `iso14229.c` and `iso14229.h` from the releases page and copy into your source tree.
- supports:
- linux
- Windows
- esp32
- Arduino
- NXP s32k
- cares about security
- server has fuzz test, see [test/README.md](test/README.md)
-

int main() {
UDSServer_t server;
UDSServerConfig_t cfg = {
.fn = &fn,
};
UDSServerInit(&server, &cfg);
for (;;) {
UDSServerPoll(&server);
}
}
```
## Quick Start

## quickstart: client
```c
// see examples/client.c
```
See [examples](./examples).

## Preprocessor Defines

| Define | Description | Valid values |
| - | - | - |
| `UDS_ARCH` | Select a porting target | `UDS_ARCH_CUSTOM`, `UDS_ARCH_UNIX` |
| `UDS_SYS` | Select a porting target | `UDS_SYS_CUSTOM`, `UDS_SYS_UNIX` |
| `UDS_TP` | Select a transport layer | `UDS_TP_ISOTP_C`, `UDS_TP_ISOTP_SOCKET` |
| `UDS_CUSTOM_MILLIS` | Use your own `millis()` implementation | defined or not defined |

Features:
- all memory allocation is static
- architecture-independent. tested on arm, x86-64, ppc, ppc64. see [test_qemu.py](./test_qemu.py)
- has many existing unit-tests and tests are easy to extend

## supported functions (server and client )

Expand Down Expand Up @@ -362,9 +342,9 @@ MIT
## 0.7.0
- test refactoring. theme: test invariance across different transports and processor architectures
- breaking API changes:
- rename `phys_send_id`, `phys_recv_id`, `func_send_id`, and `func_recv_id` to be consistent with the standard. Now mandatory for all UDSServerConfig_t and UDSClientConfig_t regardless of transport layer implementation
- overhauled transport layer implementation
- simplified client and server init
- `UDS_ARCH_` renamed to `UDS_SYS_`

## 0.6.0
- breaking API changes:
Expand Down
146 changes: 0 additions & 146 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,149 +407,3 @@ MIT
- 初次发布

---


# 开发者文档

## 客户端请求状态机

```plantuml
@startuml
title 客户端请求状态机
note as N1
enum {
kNoError=0,
kErrBadRequest,
kErrP2Timeout,
} ClientErr;
static inline bool isRequestComplete() {return state==Idle;}
while (Idle != client->state) {
receiveCAN(client);
UDSClientPoll(client);
}
end note
state Idle
state Sending
state Sent
state SentAwaitResponse
state ProcessResponse
Idle: if (ISOTP_RET_OK == isotp_receive(...)) // Error
ProcessResponse: isotp_receive()
ProcessResponse: _ClientValidateResponse(...)
ProcessResponse: _ClientHandleResponse(...)
Sending --> Sent: 传输层完成传输
Sent --> Idle : suppressPositiveResponse
Sending --> SentAwaitResponse: !suppressPositiveResponse
SentAwaitResponse -> Idle: 响应收到了 ||\np2 超时
SentAwaitResponse --> ProcessResponse : ISOTP_RECEIVE_STATUS_FULL == link->receive_status
ProcessResponse --> Idle
[*] -> Idle
Idle -> Sending : _SendRequest()
@enduml
```

```plantuml
@startuml
title Request Lifecycle
alt normal
alt positive response
client --> client: Sending
client -> server : *Any* Service
client --> client: SentAwaitResponse: set p2
alt 0x78 requestCorrectlyReceived-ResponsePending
server -> client : 0x3F 0x78
client -->server : txLink idle
client --> client: SentAwaitResponse: set p2star
end
server -> client : Positive Service Response
client --> client: Idle
else negative response
server -> client !! : Negative Service Response
client --> client: Idle: RequestErrorNegativeResponse
else SID mismatch
server -> client !! : Mismatched Service Response
client --> client: Idle: RequestErrorResponseSIDMismatch
end
else unexpected response
server -> client !! : Unexpected Response
client --> client: Idle: RequestErrorUnsolicitedResponse
end
@enduml
```


```plantuml
@startuml
' !pragma useVerticalIf on
title 客户端请求流程
start
:clientSendRequest();
if (验证参数) then (对)
:ok;
else (不对)
:foo;
detach
endif
:clearRequestContext();
if (等待UDS访问) then (访问接收了,进入UDS会话)
else (时间超过<b>20ms)
@enduml
```

## 服务器 0x78 requestCorrectlyReceived-ResponsePending


```plantuml
@startuml
client -> server : *Any* Service
server -> userServiceHandler: handler(args)
note right: Doing this will take a long time\nso I return 0x78
userServiceHandler -> server: 0x78
server -> client : 0x3F 0x78
client -->server : txLink idle
server -> userServiceHandler: handler(args)
note right: actually call the long-running service
... p2* > t > p2 ...
userServiceHandler -> server : Service Response
server -> client : Service Response
@enduml
```

```plantuml
@startuml
' !pragma useVerticalIf on
title 0x78流程(写flash)
start
:BufferedWriterWrite(BufferedWriter *self, const uint8_t *ibuf, uint32_t size, bool RCRRP);
if (RCRRP) then (true)
:write to flash;
else (false)
endif
if (iBufIdx == size) then (true)
:write to pageBuffer;
:iBufIdx = 0;
:return kBufferedWriterWritePending;
:0x78 RCRRP;
detach;
else (false)
:memmove(pageBuffer + pageBufIdx, iBuf + iBufIdx, size - iBufIdx);
:write to pageBuffer;
:iBufIdx += size;
:0x01 PositiveResponse;
:0x78 RCRRP;
detach
endif
@enduml
```
Empty file added examples/BUILD
Empty file.
7 changes: 7 additions & 0 deletions examples/arduino_server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Required Hardware

- [Arduino MKR-WIFI 1010](https://store-usa.arduino.cc/products/arduino-mkr-wifi-1010)
- [MKR CAN Shield](https://store.arduino.cc/products/arduino-mkr-can-shield)

Note that the labeling of CAN-H and CAN-L on silkscreen on the MKR CAN Shield is contradictory on the top and bottom of the board. The top is correct.

1 change: 1 addition & 0 deletions examples/arduino_server/main/iso14229.c
1 change: 1 addition & 0 deletions examples/arduino_server/main/iso14229.h
Loading

0 comments on commit abb4dbf

Please sign in to comment.