Skip to content

Commit

Permalink
Octet-string as byte iterator (#162)
Browse files Browse the repository at this point in the history
* Documentation update + CI

* use iterator over u8 in octet string handler

* back to c++ feature branch

* fix java example

Co-authored-by: Émile Grégoire <eg@emilegregoire.ca>
  • Loading branch information
jadamcrain and emgre committed Jan 20, 2022
1 parent 25f7d0c commit 0b1cbef
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 34 deletions.
8 changes: 4 additions & 4 deletions ffi/bindings/c/master_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ void handle_octet_strings(dnp3_header_info_t info, dnp3_octet_string_iterator_t
dnp3_octet_string_t *value = NULL;
while (value = dnp3_octet_string_iterator_next(it)) {
printf("Octet String: %u: Value=", value->index);
dnp3_byte_value_t *single_byte = dnp3_byte_iterator_next(value->value);
while (single_byte != NULL) {
printf("%02X", single_byte->value);
single_byte = dnp3_byte_iterator_next(value->value);
uint8_t *byte = dnp3_byte_iterator_next(value->value);
while (byte != NULL) {
printf("%02X", *byte);
byte = dnp3_byte_iterator_next(value->value);
}

printf("\n");
Expand Down
2 changes: 1 addition & 1 deletion ffi/bindings/c/master_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ReadHandler : public dnp3::ReadHandler {
if (!first) {
std::cout << ",";
}
write_hex_byte(std::cout, byte.value);
write_hex_byte(std::cout, byte);
first = false;
}
std::cout << "]" << std::endl;
Expand Down
8 changes: 4 additions & 4 deletions ffi/bindings/c/master_tls_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ void handle_octet_strings(dnp3_header_info_t info, dnp3_octet_string_iterator_t*
dnp3_octet_string_t* value = NULL;
while (value = dnp3_octet_string_iterator_next(it)) {
printf("Octet String: %u: Value=", value->index);
dnp3_byte_value_t* single_byte = dnp3_byte_iterator_next(value->value);
while (single_byte != NULL) {
printf("%02X", single_byte->value);
single_byte = dnp3_byte_iterator_next(value->value);
uint8_t* byte = dnp3_byte_iterator_next(value->value);
while (byte != NULL) {
printf("%02X", *byte);
byte = dnp3_byte_iterator_next(value->value);
}

printf("\n");
Expand Down
2 changes: 1 addition & 1 deletion ffi/bindings/dotnet/examples/master/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void HandleOctetString(HeaderInfo info, ICollection<OctetString> values)
Console.Write($"Octet String {val.Index}: Value=");
foreach (var b in val.Value)
{
Console.Write($"{b.Value:X2} ");
Console.Write($"{b:X2} ");
}
Console.WriteLine();
}
Expand Down
2 changes: 1 addition & 1 deletion ffi/bindings/dotnet/examples/master_tls/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void HandleOctetString(HeaderInfo info, ICollection<OctetString> values)
Console.Write($"Octet String {val.Index}: Value=");
foreach (var b in val.Value)
{
Console.Write($"{b.Value:X2} ");
Console.Write($"{b:X2} ");
}
Console.WriteLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void handleOctetString(HeaderInfo info, List<OctetString> it) {
val -> {
System.out.print("Octet String " + val.index + ": Value=");
val.value.forEach(
b -> System.out.print(String.format("%02X", b.value.byteValue()) + " "));
b -> System.out.print(String.format("%02X", b.byteValue()) + " "));
System.out.println();
});
}
Expand Down
24 changes: 12 additions & 12 deletions ffi/dnp3-ffi/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ impl<'a> ffi::OctetString<'a> {

pub struct ByteIterator<'a> {
inner: std::slice::Iter<'a, u8>,
next: Option<ffi::ByteValue>,
next: Option<u8>,
}

impl<'a> ByteIterator<'a> {
Expand All @@ -385,21 +385,21 @@ impl<'a> ByteIterator<'a> {
}

fn next(&mut self) {
self.next = self.inner.next().map(|value| ffi::ByteValue::new(*value))
self.next = self.inner.next().copied()
}
}

pub unsafe fn byte_iterator_next(it: *mut ByteIterator) -> Option<&ffi::ByteValue> {
pub unsafe fn byte_iterator_next(it: *mut ByteIterator) -> *const u8 {
let it = it.as_mut();
it.and_then(|it| {
it.next();
it.next.as_ref()
})
}

impl ffi::ByteValue {
fn new(value: u8) -> Self {
Self { value }
match it {
None => std::ptr::null(),
Some(x) => {
x.next();
match &x.next {
None => std::ptr::null(),
Some(x) => x,
}
}
}
}

Expand Down
11 changes: 1 addition & 10 deletions ffi/dnp3-schema/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,7 @@ fn build_iterator<T: Into<UniversalStructField>>(
fn build_octet_string(
lib: &mut LibraryBuilder,
) -> Result<(FunctionReturnStructHandle, AbstractIteratorHandle), BindingError> {
// Octet string stuff
let byte_struct_decl = lib.declare_function_return_struct("byte_value")?;
let byte_struct = lib
.define_function_return_struct(byte_struct_decl)?
.add("value", Primitive::U8, "Byte value")?
.doc("Single byte struct")?
.end_fields()?
.build()?;

let byte_it = lib.define_iterator_with_lifetime("byte_iterator", byte_struct)?;
let byte_it = lib.define_iterator_with_lifetime("byte_iterator", Primitive::U8)?;

let octet_string_struct_decl = lib.declare_function_return_struct("octet_string")?;
let octet_string_struct = lib
Expand Down

0 comments on commit 0b1cbef

Please sign in to comment.