Commit Graph

167 Commits

Author SHA1 Message Date
Gold856
3bbbf86632 [wpiutil, wpilib] Add FileLogger and log console output (#6977) 2024-09-12 22:13:06 -07:00
Peter Johnson
4c7fe73f69 [wpiutil] DataLog: Add last value and change detection (#6674)
Update() checks/updates the last value and appends only if changed.
GetLastValue() gets the last value.

Also add OutputStream support to Java DataLogWriter.
2024-07-21 13:08:15 -07:00
Gold856
5ce72d43e4 [build] Fix CMake protobuf dependency handling (#6772)
Reverts #6609 since that fix didn't Just Work(tm) on Windows. (edit: or Ubuntu. Seems to have broken everything except macOS.) This PR configures CMake to try and find protobuf-config.cmake first, which allows protobuf to pull in abseil for us. If protobuf-config.cmake is not available (coprocessors which don't have a new enough protobuf installed are a common case), it will fallback to CMake's built-in FindProtobuf module, which is what we were using before.

Add wpi::CreateMessage, a wrapper with an ifdef to switch between Arena::CreateMessage and Arena::Create, since the former is deprecated in newer versions of protobuf. This allows forward compatibility with newer versions of protobuf.
2024-06-28 06:28:39 -07:00
Gold856
3d6b710293 [wpiutil] DataLog: Don't constantly retry log creation when low on space (#6730) 2024-06-10 20:22:50 -06:00
Thad House
4ce8f3f935 Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.

For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.

Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.

The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.

WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
Peter Johnson
178fe99f12 [wpiutil] Split DataLog background writer into different class (#6590)
DataLog is now a base class, with DataLogBackgroundWriter being the
background thread version and DataLogWriter being a non-threaded version.

Also split the C header into a separate file to make it more wpiformat friendly.
2024-05-12 14:09:43 -07:00
Tyler Veness
d88c71ffdc [wpiutil] Upgrade to fmt 10.2.1, add wpi::print (#6161)
We now use a wrapper (wpi::print) to catch exceptions since we can't patch
std::print() to not throw when we ultimately migrate to it.

fmtlib and std format/print throw the same exceptions and always have. We previously patched fmt::print() to not throw a write failure exception, but we can't do that for std::print(); wpi::print() is the migration plan.
2024-05-12 06:25:42 -07:00
Gold856
3e5187ff32 [wpilibj] DataLogManager: Fix behavior when low on space (#6486)
Uses getUsableSpace in Java, matching how C++ determines available space (C++ calls it available, but they mean the same thing.) This fixes a bug where logs wouldn't get deleted due to incorrect available space detection.

The DataLog thread now also checks if the state was marked as stopped after a call to StartLogFile.
2024-04-21 20:34:05 -07:00
Peter Johnson
c88be31ec2 Merge branch 'development' 2024-04-21 20:15:51 -07:00
Gold856
c32e7db8e3 [wpiutil] DataLog: Don't constantly retry creating logs when low on space (#6468)
When low on space, a log file won't be created. This is detected as a "deletion", and the DataLog thread will continously try to create a log, fail to do so because of low space, detect it as a "deletion", and do so in a loop.

If there's not enough space, the DataLog will be marked as stopped, preventing this infinite loop. Calls to start() will hit this code path and mark it as stopped again.
2024-03-24 23:50:18 -07:00
Tyler Veness
b4674bacb9 [wpiutil] Upgrade to LLVM 18.1.1 (#6405) 2024-03-17 18:39:03 -07:00
Thad House
c19ee8b0fe [wpiutil, hal] Crash on failure for SetupNowRio() and wpi::Now() when not configured (#6417)
This is an unrecoverable condition, so always terminate.
2024-03-08 00:26:53 -08:00
Peter Johnson
f1a1ffd7fc [wpiutil] Rate-limit FPGA error from Now() (#6394) 2024-02-25 11:25:46 -08:00
Thad House
ba15844c28 [hal,wpiutil] Error out of HAL_Initialize if SetupRioNow fails (#6374) 2024-02-15 22:57:06 -08:00
Thad House
0e5eb3f35c [wpiutil] Fix DynamicStruct string handling (#6253)
Dynamic structs had a few major issues.

In C++, if the string was the last definition in the schema, attempting to set a string would trigger an assertion. This has been fixed

Setting a string value could truncate the string actually stored in the struct, if the definition was shorter than the string to set.
There was no way to detect if this case occurred. The set string function now returns a bool if the string was fully written or not.

Reading a string that had a value shorter than the schema definition would result in embedded trailing nulls in the string. This would make comparing string equality basically impossible, as those embedded nulls count for the length of the string.

The above truncating didn't take into account UTF8 code points. This means a truncation could happen in the middle of a unicode character. Depending on the language this had different behavior, but unpaired code points are problematic to detect in any case. On the decoding side, detect if a split UTF8 code point has occurred by the writer, and if so just ignore it and treat it as not part of the string. Doing this on the receive side means a newer receive side is all that is needed to fix this, which is generally a better option then requiring all senders to update.

Actual DynamicStruct instances have 0 units tests for them. Added a bunch of unit tests around strings to ensure things work properly.
2024-01-19 22:24:54 -08:00
Thad House
7f9389f101 [wpiutil] DataLog: Remove extra entry parameter from C AddSchema functions (#6246) 2024-01-19 20:35:44 -08:00
Thad House
a2d45dbca4 [wpiutil] DataLog: Add AddSchema functions to C API (#6232) 2024-01-15 23:34:18 -08:00
Peter Johnson
5659038443 [wpiutil,cscore,apriltag] Fix RawFrame (#6098) 2023-12-26 22:05:02 -06:00
Peter Johnson
21d1972d7a [wpiutil] DataLog: Ensure file is written on shutdown (#6087)
Previously the thread could end without the file being written.
2023-12-23 15:40:51 -06:00
Peter Johnson
c29e8c66cf [wpiutil] DataLog: Fix UB in AppendImpl (#6088) 2023-12-23 15:39:39 -06:00
Thad House
7c8b7a97ad [wpiutil] Zero out roborio system timestamp (#6042)
There was also an issue where the clock zero offset would be incorrect due to the system time updating. This solves that issue for the Rio.
2023-12-13 15:20:24 -08:00
Thad House
54a55b8b53 [wpiutil,hal] Update image; init Rio Now() HMB with a FPGA session (#6016) 2023-12-08 23:22:59 -08:00
Peter Johnson
b0719942f0 [wpiutil] Timestamp: Report errors on Rio HMB init failure (#5974) 2023-11-29 10:10:07 -08:00
Drew Williams
ca81ced409 [wpiutil] Move RawFrame to wpiutil; add generation of RawFrame for AprilTags (#5923) 2023-11-23 10:55:10 -08:00
Peter Johnson
cfbff32185 [wpiutil] timestamp: Fix startup race on Rio (#5930)
Note this doesn't use a full mutex, so there is still a shutdown race.
2023-11-15 21:58:51 -08:00
Peter Johnson
f1a82828fe [wpiutil] Add DataLog and DataLogManager Stop() (#5860)
Restarting a stopped log results in creating a new log file with fresh copies of the same start records and schema data records.

Also check to see if the file has been deleted or if the log file exceeds 1.8 GB, and start a new one.
2023-11-03 20:34:43 -07:00
Peter Johnson
1713386869 [wpiutil] ProtobufMessageDatabase: Fix out-of-order Add() rebuild (#5845) 2023-10-29 16:50:01 -07:00
Peter Johnson
2ab4fcbc24 [wpiutil] ProtobufMessageDatabase: Clear messages first (#5827)
The message destructor appears to call something on the descriptor.
2023-10-26 19:17:29 -07:00
Peter Johnson
23ea188e60 [glass] Add protobuf decode error log message (#5812) 2023-10-23 23:36:23 -07:00
Peter Johnson
cf54d9ccb7 [wpiutil, ntcore] Add structured data support (#5391)
This adds support for two serialization formats for complex data types:

- Protobuf for complex objects with variable length internals that need forward and backward wire compatibility (lower speed, more flexible)
- Raw struct (ByteBuffer-style) for fixed-length objects (higher speed, less flexible)

Deserialization can be done either by creating a new object (for immutable objects) or overwriting the contents of an existing object (for mutable objects).

Implementing classes should provide inner classes that implement the Protobuf or Struct interface (in Java) or specialize the wpi::Protobuf or wpi::Struct struct (in C++). It is possible for classes to implement both. If the class itself does not implement serialization, it's possible for third parties/users to provide an implementation instead.

Uses the Google protobuf implementation for C++ and the QuickBuffers alternative protobuf implementation for Java.
2023-10-19 21:41:47 -07:00
Peter Johnson
cb1bd0a3be [wpiutil] Get more precise system time on Windows (#5722)
Windows 8 added GetSystemTimePreciseAsFileTime().
2023-10-03 20:38:48 -07:00
Peter Johnson
8d2cbfce16 [wpiutil] DataLog: Stop logging if insufficient free space (#5699)
The threshold is set to 5 MB.
2023-10-01 14:01:49 -07:00
Peter Johnson
5e295dfbda [wpiutil] DataLog: Limit total buffer allocation (#5700)
Pause logging if more than 1 MB of outgoing buffers are allocated.
Limit free buffer list to 256 KB.
2023-09-30 20:19:28 -07:00
Tyler Veness
1b6ec5a95d [wpiutil] Upgrade to LLVM 17.0.1 (#5482) 2023-09-21 19:54:33 -07:00
Peter Johnson
a6157f184d [wpiutil] timestamp: Add ShutdownNowRio (#5610) 2023-09-07 09:59:39 -07:00
Tyler Veness
165ebe4c79 Upgrade to fmt 10.1.0 (#5326) 2023-08-28 15:15:14 -07:00
Tyler Veness
8e2a7fd306 Include thirdparty libraries with angle brackets (#5578) 2023-08-28 15:13:34 -07:00
Joseph Eng
2e4ad35e36 [wpiutil] jni_util: Add JSpan and CriticalJSpan (#5554)
These replace JArrayRef et al and support statically sized arrays similar to std::span.
2023-08-24 00:02:56 -07:00
Peter Johnson
c01814b80e [wpiutil] Add C API for DataLog (#5509) 2023-08-06 20:18:50 -07:00
Thad House
3ad5d2e42d [hal,wpiutil] Use HMB for FPGA Timestamps (#5499)
Current timestamp read code uses FPGA register reads. Through testing,
this read was slower then clock_gettime by about 4-5x. However, another
method of reading the FPGA time is available, using HMB. HMB
is memory mapped IO from RAM to the FPGA. So to code side,
reading the value is just a memory barrier and a memory read.

There is some latency on the write side, so a very small artifical delay
(5us) is added to avoid register reads such as interrupts being ahead
of current timestamps, which could cause issues.

Below is read times for 1000 calls to clock_gettime, register reads and
hmb reads.
```
Clock: Rise 1.72939400 s Fall 1.72990700 s Delta 0.00051300 s
FPGA : Rise 1.72999000 s Fall 1.73429300 s Delta 0.00430300 s
HMB  : Rise 1.73466800 s Fall 1.73481900 s Delta 0.00015100 s
```

Also add full HMB struct to HAL for future usage.
2023-08-03 23:46:55 -07:00
Peter Johnson
a95994fff6 [wpiutil] timestamp: Call FPGA functions directly (#5235)
This works around an exit race with wpi::Now() on Rio; it was overridden
to call HAL_GetFPGATime(), which calls chipobject, but on exit, because
there was not a library dependency, the chipobject could be destroyed
prior to wpiutil/wpinet being shut down.
2023-07-24 23:03:28 -07:00
Peter Johnson
3a6e40a44b [wpiutil] Enhance DataLog Java raw value support
Add support for start, length, and ByteBuffers
2023-07-22 17:17:52 -07:00
Ryan Blue
1fca519fb4 [wpiutil] Remove remnants of ghc fs and tcb_span libraries (#5411) 2023-06-22 19:42:44 -07:00
Peter Johnson
d56314f866 [wpiutil] Disable mock time on the Rio (#5092) 2023-02-12 22:38:34 -08:00
Dustin Spicuzza
42fc4cb6bc [wpiutil] SafeThread: Provide start/stop hooks (#4880)
Co-authored-by: David Vo <auscompgeek@users.noreply.github.com>
2022-12-31 15:40:45 -08:00
Tyler Veness
1fc098e696 Enable log macros to work with no args (#4475)
This is enabled by the C++20 __VA_OPT__ feature.
Uses of "{}" format string were updated.
Some warning suppressions were required for older clang versions.
Also improve codegen of wpi::Logger::Log(), frc::ReportError(), and frc::MakeError();
these generate better and less redundant code if they use fmt::string_view for the
format string instead of templating on it.
2022-10-19 10:49:27 -07:00
Tyler Veness
fbdc810887 Upgrade to C++20 (#4239)
* Use explicit this capture required by C++20
* Use C++20 span
* Replace wpi::numbers with std::numbers
* Fix C++20 clang-tidy warning false positive in fmt
* Remove ciso646 include since C++20 removed that header
* Fix global-buffer-overflow asan warnings in ntcore tests
* Add DIOSetProxy constructor to HAL

* Upgrade MSVC compiler to 2022
* Bump native-utils to 2023.2.7 (changes to std=c++20)

Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
2022-10-15 16:33:14 -07:00
Peter Johnson
c7b7624c1c [wpiutil] Add MessagePack utility functions (#4448)
Also add mpack to srcDirs.
2022-10-02 08:11:42 -05:00
Peter Johnson
8767e4a941 [wpiutil] DataLog: Fix SetMetadata output (#4430) 2022-09-22 21:54:55 -07:00
Peter Johnson
8c4af073f4 [wpiutil] Synchronization: shutdown race protection (#4429) 2022-09-22 21:53:51 -07:00