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

Runtime shutdown with optional timeout #110

Merged
merged 11 commits into from
Jan 21, 2023
Merged
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
40 changes: 16 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dep_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
"scursor": {
"url": "https://github.com/stepfunc/scursor"
},
"sfio-promise": {
"url": "https://github.com/stepfunc/promise"
},
"sfio-tokio-ffi": {
"url": "https://github.com/stepfunc/tokio-ffi"
},
Expand Down
44 changes: 21 additions & 23 deletions ffi/bindings/c/client_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ void on_port_state_change(rodbus_port_state_t state, void *ctx)

rodbus_client_state_listener_t get_client_listener()
{
return rodbus_client_state_listener_init(on_client_state_change, NULL, NULL);
return (rodbus_client_state_listener_t) {
.on_change = &on_client_state_change,
};
}

rodbus_port_state_listener_t get_port_listener()
{
return rodbus_port_state_listener_init(on_port_state_change, NULL, NULL);
rodbus_port_state_listener_t get_port_listener(){
return (rodbus_port_state_listener_t) {
.on_change = &on_port_state_change,
};
}


run_channel(rodbus_client_channel_t* channel)
{
// ANCHOR: enable_channel
Expand All @@ -93,26 +97,20 @@ run_channel(rodbus_client_channel_t* channel)
// ANCHOR_END: address_range

// ANCHOR: bit_read_callback_init
rodbus_bit_read_callback_t bit_callback = rodbus_bit_read_callback_init(
on_read_bits_complete, // Success callback
on_read_bits_failure, // Failure callback
NULL, // Destroy callback
NULL // Callback context
);
rodbus_bit_read_callback_t bit_callback = {
.on_complete = &on_read_bits_complete,
.on_failure = &on_read_bits_failure,
};
// ANCHOR_END: bit_read_callback_init
rodbus_register_read_callback_t register_callback = rodbus_register_read_callback_init(
on_read_registers_complete, // Success callback
on_read_registers_failure, // Failure callback
NULL, // Destroy callback
NULL // Callback context
);
rodbus_register_read_callback_t register_callback = {
.on_complete = on_read_registers_complete, // Success callback
.on_failure = on_read_registers_failure, // Failure callback
};
// ANCHOR: write_callback_init
rodbus_write_callback_t write_callback = rodbus_write_callback_init(
on_write_complete, // Success callback
on_write_failure, // Failure callback
NULL, // Destroy callback
NULL // Callback context
);
rodbus_write_callback_t write_callback = {
.on_complete = on_write_complete,
.on_failure = on_write_failure,
};
/// ANCHOR_END: write_callback_init

char cbuf[10];
Expand Down Expand Up @@ -304,7 +302,7 @@ int main(int argc, char* argv[])
{
// ANCHOR: logging_init
// initialize logging with the default configuration
rodbus_logger_t logger = rodbus_logger_init(&on_log_message, NULL, NULL);
rodbus_logger_t logger = {.on_message = on_log_message};
rodbus_configure_logging(rodbus_logging_config_init(), logger);
// ANCHOR_END: logging_init

Expand Down
39 changes: 25 additions & 14 deletions ffi/bindings/c/server_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,20 @@ int run_server(rodbus_server_t* server)
}
else if (strcmp(cbuf, "uc\n") == 0) {
// ANCHOR: update_coil
rodbus_server_update_database(server, 1, rodbus_database_callback_init(update_coil, NULL, &state));
rodbus_server_update_database(server, 1, (rodbus_database_callback_t) {
.callback = update_coil,
.ctx = &state,
});
// ANCHOR_END: update_coil
}
else if (strcmp(cbuf, "udi\n") == 0) {
rodbus_server_update_database(server, 1, rodbus_database_callback_init(update_discrete_input, NULL, &state));
rodbus_server_update_database(server, 1, (rodbus_database_callback_t){.callback = update_discrete_input, .ctx = &state});
}
else if (strcmp(cbuf, "uhr\n") == 0) {
rodbus_server_update_database(server, 1, rodbus_database_callback_init(update_holding_register, NULL, &state));
rodbus_server_update_database(server, 1, (rodbus_database_callback_t){.callback = update_holding_register, .ctx = &state});
}
else if (strcmp(cbuf, "uir\n") == 0) {
rodbus_server_update_database(server, 1, rodbus_database_callback_init(update_input_register, NULL, &state));
rodbus_server_update_database(server, 1, (rodbus_database_callback_t){.callback = update_input_register, .ctx = &state});
}
else {
printf("Unknown command\n");
Expand All @@ -179,14 +182,17 @@ int run_server(rodbus_server_t* server)
rodbus_device_map_t* build_device_map()
{
// ANCHOR: device_map_init
rodbus_write_handler_t write_handler =
rodbus_write_handler_init(&on_write_single_coil, &on_write_single_register, &on_write_multiple_coils, &on_write_multiple_registers, NULL, NULL);

rodbus_write_handler_t write_handler = {
.write_single_coil = on_write_single_coil,
.write_single_register = on_write_single_register,
.write_multiple_coils = on_write_multiple_coils,
.write_multiple_registers = on_write_multiple_registers,
};
rodbus_device_map_t* map = rodbus_device_map_create();
rodbus_device_map_add_endpoint(map,
1, // Unit ID
write_handler, // Handler for write requests
rodbus_database_callback_init(configure_db, NULL, NULL) // Callback for the initial state of the database
(rodbus_database_callback_t){.callback = configure_db } // Callback for the initial state of the database
);
// ANCHOR_END: device_map_init

Expand Down Expand Up @@ -234,11 +240,16 @@ int run_rtu_channel(rodbus_runtime_t* runtime)
rodbus_authorization_handler_t get_auth_handler()
{
// ANCHOR: auth_handler_init
rodbus_authorization_handler_t auth_handler = rodbus_authorization_handler_init(
&auth_read, &auth_read, &auth_read, &auth_read,
&auth_single_write, &auth_single_write, &auth_multiple_writes, &auth_multiple_writes,
NULL, NULL
);
rodbus_authorization_handler_t auth_handler = {
.read_coils = auth_read,
.read_discrete_inputs = auth_read,
.read_holding_registers = auth_read,
.read_input_registers = auth_read,
.write_single_coil = auth_single_write,
.write_single_register = auth_single_write,
.write_multiple_coils = auth_multiple_writes,
.write_multiple_registers = auth_multiple_writes,
};
// ANCHOR_END: auth_handler_init

return auth_handler;
Expand Down Expand Up @@ -324,7 +335,7 @@ int create_and_run_channel(int argc, char *argv[], rodbus_runtime_t *runtime)
int main(int argc, char* argv[])
{
// initialize logging with the default configuration
rodbus_logger_t logger = rodbus_logger_init(&on_log_message, NULL, NULL);
rodbus_logger_t logger = {.on_message = on_log_message};
rodbus_configure_logging(rodbus_logging_config_init(), logger);

// Create runtime
Expand Down
2 changes: 1 addition & 1 deletion ffi/rodbus-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/stepfunc/rodbus"
readme = "../README.md"

[dependencies]
oo-bindgen = "0.6"
oo-bindgen = "0.8"
rodbus-schema = { path = "../rodbus-schema" }
tracing = "^0.1"
tracing-subscriber = "^0.3"
Expand Down
2 changes: 1 addition & 1 deletion ffi/rodbus-ffi-java/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ tls = ["rodbus-ffi/tls"]

[build-dependencies]
rodbus-schema = { path = "../rodbus-schema" }
oo-bindgen = "0.6"
oo-bindgen = "0.8"
2 changes: 1 addition & 1 deletion ffi/rodbus-ffi-java/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {

match rodbus_schema::build_lib() {
Err(err) => {
eprintln!("{}", err);
eprintln!("{err}");
std::process::exit(-1);
}
Ok(lib) => {
Expand Down
1 change: 1 addition & 0 deletions ffi/rodbus-ffi-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
clippy::let_unit_value,
clippy::needless_return,
clippy::not_unsafe_ptr_arg_deref,
clippy::uninlined_format_args,
unused_variables,
dead_code
)]
Expand Down
7 changes: 4 additions & 3 deletions ffi/rodbus-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ tracing-subscriber = "0.2"
rodbus = { path = "../../rodbus", default-features = false }
tokio = { version = "1.5", features = ["rt-multi-thread"]}
num_cpus = "1"
sfio-promise = "0.2"

[build-dependencies]
rodbus-schema = { path = "../rodbus-schema" }
oo-bindgen = "0.6"
sfio-tracing-ffi = "0.5"
sfio-tokio-ffi = "0.5"
oo-bindgen = "0.8"
sfio-tracing-ffi = "0.8"
sfio-tokio-ffi = "0.8"


[features]
Expand Down
2 changes: 1 addition & 1 deletion ffi/rodbus-ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
oo_bindgen::backend::rust::generate_ffi(&lib).unwrap();
}
Err(err) => {
eprintln!("rodbus model error: {}", err);
eprintln!("rodbus model error: {err}");
std::process::exit(-1);
}
};
Expand Down
Loading