Files
allwpilib/ntcore/src/main/native/cpp/WireDecoder.h

159 lines
4.4 KiB
C
Raw Normal View History

/*----------------------------------------------------------------------------*/
2018-01-01 17:29:13 -08:00
/* Copyright (c) FIRST 2015-2018. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
2017-08-19 23:08:27 -07:00
#ifndef NTCORE_WIREDECODER_H_
#define NTCORE_WIREDECODER_H_
#include <stdint.h>
#include <cstddef>
2017-08-19 23:08:27 -07:00
#include <memory>
#include <string>
#include <support/leb128.h>
#include <support/raw_istream.h>
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
#include "Log.h"
2017-08-19 23:08:27 -07:00
#include "networktables/NetworkTableValue.h"
namespace nt {
/* Decodes network data into native representation.
* This class is designed to read from a raw_istream, which provides a blocking
* read interface. There are no provisions in this class for resuming a read
* that was interrupted partway. Read functions return false if
* raw_istream.read() returned false (indicating the end of the input data
* stream).
*/
class WireDecoder {
public:
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
WireDecoder(wpi::raw_istream& is, unsigned int proto_rev,
wpi::Logger& logger);
~WireDecoder();
void set_proto_rev(unsigned int proto_rev) { m_proto_rev = proto_rev; }
/* Get the active protocol revision. */
unsigned int proto_rev() const { return m_proto_rev; }
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
/* Get the logger. */
wpi::Logger& logger() const { return m_logger; }
/* Clears error indicator. */
void Reset() { m_error = nullptr; }
/* Returns error indicator (a string describing the error). Returns nullptr
* if no error has occurred.
*/
const char* error() const { return m_error; }
void set_error(const char* error) { m_error = error; }
/* Reads the specified number of bytes.
* @param buf pointer to read data (output parameter)
* @param len number of bytes to read
* Caution: the buffer is only temporarily valid.
*/
2017-08-19 23:08:27 -07:00
bool Read(const char** buf, size_t len) {
if (len > m_allocated) Realloc(len);
*buf = m_buf;
m_is.read(m_buf, len);
#if 0
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
if (m_logger.min_level() <= NT_LOG_DEBUG4 && m_logger.HasLogger()) {
std::ostringstream oss;
oss << "read " << len << " bytes:" << std::hex;
2017-08-19 23:08:27 -07:00
if (!rv) {
oss << "error";
2017-08-19 23:08:27 -07:00
} else {
for (size_t i = 0; i < len; ++i)
oss << ' ' << static_cast<unsigned int>((*buf)[i]);
}
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
m_logger.Log(NT_LOG_DEBUG4, __FILE__, __LINE__, oss.str().c_str());
}
#endif
return !m_is.has_error();
}
/* Reads a single byte. */
bool Read8(unsigned int* val) {
const char* buf;
if (!Read(&buf, 1)) return false;
*val = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
return true;
}
/* Reads a 16-bit word. */
bool Read16(unsigned int* val) {
const char* buf;
if (!Read(&buf, 2)) return false;
unsigned int v = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
++buf;
v <<= 8;
v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
*val = v;
return true;
}
/* Reads a 32-bit word. */
2017-08-19 23:08:27 -07:00
bool Read32(uint32_t* val) {
const char* buf;
if (!Read(&buf, 4)) return false;
unsigned int v = (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
++buf;
v <<= 8;
v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
++buf;
v <<= 8;
v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
++buf;
v <<= 8;
v |= (*reinterpret_cast<const unsigned char*>(buf)) & 0xff;
*val = v;
return true;
}
/* Reads a double. */
bool ReadDouble(double* val);
/* Reads an ULEB128-encoded unsigned integer. */
2017-08-19 23:08:27 -07:00
bool ReadUleb128(uint64_t* val) { return wpi::ReadUleb128(m_is, val); }
bool ReadType(NT_Type* type);
bool ReadString(std::string* str);
std::shared_ptr<Value> ReadValue(NT_Type type);
WireDecoder(const WireDecoder&) = delete;
WireDecoder& operator=(const WireDecoder&) = delete;
protected:
/* The protocol revision. E.g. 0x0200 for version 2.0. */
unsigned int m_proto_rev;
/* Error indicator. */
const char* m_error;
private:
/* Reallocate temporary buffer to specified length. */
2017-08-19 23:08:27 -07:00
void Realloc(size_t len);
/* input stream */
wpi::raw_istream& m_is;
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
/* logger */
wpi::Logger& m_logger;
/* temporary buffer */
char* m_buf;
/* allocated size of temporary buffer */
2017-08-19 23:08:27 -07:00
size_t m_allocated;
};
} // namespace nt
2017-08-19 23:08:27 -07:00
#endif // NTCORE_WIREDECODER_H_