add no-std

This commit is contained in:
2025-12-06 22:30:42 +01:00
parent f3b79bb9aa
commit c2b9eaa8d3
5 changed files with 47 additions and 12 deletions

View File

@@ -12,6 +12,10 @@ categories = ["encoding", "decoding"]
# [features]
# json = ["serde_json"]
[features]
default = ["std"]
std = []
[dependencies]
serde = { version = "1.0"}
# serde_json = { version = "1.0", optional = true }
@@ -19,7 +23,7 @@ thiserror = "2.0.17"
chrono = { version = "0.4", features = ["serde"] }
enum_stringify = "0.6.4"
num_enum = "0.7.5"
log = "0.4.28"
log = { version = "0.4" }
[lints.clippy]

View File

@@ -1,10 +1,9 @@
use enum_stringify::EnumStringify;
use num_enum::{IntoPrimitive, TryFromPrimitive};
/// Message Identifier used in the communication protocol. This is specified by Viessmann
/// and UNACKED is rarely to never used.
#[repr(u8)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, EnumStringify)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq)]
pub enum MessageIdentifier {
Request = 0x00,
Response = 0x01,
@@ -12,11 +11,12 @@ pub enum MessageIdentifier {
CommError = 0x03,
}
/// Function Code used in the communication protocol. This is specified by Viessmann
/// and Read and Write are usually used for simpler values, RPC can maybe be used for
/// more complex things, but this is currently unclear
/// Function Code used in the communication protocol.
///
/// This is specified by Viessmann and Read and Write are usually used for simpler values,
/// RPC can maybe be used for more complex things, but this is currently unclear
#[repr(u8)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, EnumStringify)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq)]
pub enum FunctionCode {
VirtualREAD = 0x01,
VirtualWRITE = 0x02,

View File

@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
mod message;
mod enums;
mod utils;

View File

@@ -1,6 +1,19 @@
use log::warn;
use thiserror::Error;
use log::{warn, error};
// Use std::vec when std exists, otherwise alloc::vec
#[cfg(feature = "std")]
use std::vec;
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use thiserror::Error;
use crate::{enums::{FunctionCode, MessageIdentifier}, utils::calculate_checksum};
#[derive(PartialEq, Eq, Debug)]
@@ -167,7 +180,7 @@ impl Protocol300Message {
pub enum MessageCreationError {
/// The data length is longer than u8
#[error("Data too long")]
DataTooLong(#[from] std::num::TryFromIntError),
DataTooLong(#[from] core::num::TryFromIntError),
}
@@ -190,7 +203,7 @@ pub enum MessageDecodingError {
InvalidTelegramStart(u8),
/// Error getiting the length of the bytes slice, should never happen
#[error("Error getting the length of the bytes slice")]
NoLength(#[from] std::num::TryFromIntError),
NoLength(#[from] core::num::TryFromIntError),
/// The advertiesed telegram length and actual length do not match
#[error("The advertiesed telegram length and actual length do not match")]
LengthMismatch(),
@@ -204,7 +217,15 @@ pub enum MessageDecodingError {
#[cfg(test)]
mod tests {
use crate::{enums::{FunctionCode, MessageIdentifier}, message::{MessageCreationError, Protocol300Message}, utils::calculate_checksum};
use crate::{enums::{FunctionCode, MessageIdentifier}, message::{MessageCreationError, Protocol300Message}};
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[test]

View File

@@ -18,6 +18,14 @@ pub fn calculate_checksum(length: u8, message_identifier: MessageIdentifier, fun
#[cfg(test)]
mod tests {
use crate::{enums::{FunctionCode, MessageIdentifier}, message::Protocol300Message, utils::calculate_checksum};
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[test]
fn test_check_checksum_1() {