# codetics-ocpp-core
Version-independent building blocks of OCPP-J, the JSON-over-WebSocket transport of OCPP 1.6, 2.0.1 and 2.1:
- RPC framing: `CALL`, `CALLRESULT`, `CALLERROR`, and the OCPP 2.1 `CALLRESULTERROR` and `SEND` message types;
- RPC error codes, including the historic OCPP 1.6 spellings (`FormationViolation`, `OccurenceConstraintViolation`);
- version negotiation through the `Sec-WebSocket-Protocol` subprotocol (`ocpp1.6`, `ocpp2.0.1`, `ocpp2.1`);
- an RFC 3339 `DateTime`, schema-constraint validation (`Validate`) and unique message ids.
```rust
use codetics_ocpp_core::rpc::{self, Frame};
use codetics_ocpp_core::version::negotiate;
use codetics_ocpp_core::{ErrorCode, ErrorDialect, OcppVersion, RpcError};
// The client lists versions by preference; the server picks the first acceptable one.
let (version, token) = negotiate(["ocpp2.1, ocpp1.6"], &[OcppVersion::V16]).unwrap();
assert_eq!((version, token), (OcppVersion::V16, "ocpp1.6"));
match Frame::parse(r#"[2,"19223201","Heartbeat",{}]"#).unwrap() {
Frame::Call { id, action, .. } => {
assert_eq!(action, "Heartbeat");
let reply = rpc::encode_result(&id, &serde_json::json!({ "currentTime": "2025-01-01T00:00:00Z" })).unwrap();
assert!(reply.starts_with(r#"[3,"19223201""#));
}
_ => unreachable!(),
}
let err = RpcError::new(ErrorCode::FormatViolation, "bad payload");
assert_eq!(rpc::encode_error("1", &err, ErrorDialect::Ocpp16, false), r#"[4,"1","FormationViolation","bad payload",{}]"#);
```
## Part of Codetics OCPP
This crate belongs to [Codetics OCPP](https://codetics.fr), a complete OCPP 1.6J / 2.0.1 / 2.1
implementation in Rust written by **Kevin Vavelin** ([Codetics](https://codetics.fr)).
It can be used as is; contributions are welcome.
## License and credit
Licensed under the [Apache License, Version 2.0](LICENSE).
Copyright 2026 Kevin Vavelin, Codetics. Any redistribution of this crate or of a work derived
from it (including forks and products embedding it) must keep the copyright notice and include
the [NOTICE](NOTICE) file crediting Kevin Vavelin / Codetics as the origin of the work.