1use ocpp_types::v16::{AuthorizeRequest, HeartbeatResponse, IdTag, RpcErrorCode};
6use ocpp_types::{Call, CallError, CallResult, EmptyPayload, MessageId};
7
8fn main() {
9 let call = Call {
13 message_id: MessageId::try_from("19223201").unwrap(),
14 payload: AuthorizeRequest {
15 id_tag: IdTag::try_from("ABC123").unwrap(),
16 },
17 };
18
19 let mut buf = [0u8; 256];
20 let len = serde_json_core::to_slice(&call, &mut buf).unwrap();
21 let json = core::str::from_utf8(&buf[..len]).unwrap();
22 println!("CALL: {json}");
23
24 let (parsed, _): (Call<AuthorizeRequest>, usize) =
25 serde_json_core::from_slice(&buf[..len]).unwrap();
26 assert_eq!(parsed, call);
27
28 let current_time =
35 ocpp_types::OcppTimestamp::parse_rfc3339("2024-01-01T00:00:00Z").unwrap();
36
37 let result: CallResult<HeartbeatResponse> = CallResult {
38 message_id: MessageId::try_from("19223201").unwrap(),
39 payload: HeartbeatResponse { current_time },
40 };
41
42 let mut buf = [0u8; 256];
43 let len = serde_json_core::to_slice(&result, &mut buf).unwrap();
44 println!("CALLRESULT: {}", core::str::from_utf8(&buf[..len]).unwrap());
45
46 let error: CallError<RpcErrorCode> = CallError {
50 message_id: MessageId::try_from("19223201").unwrap(),
51 error_code: RpcErrorCode::NotImplemented,
52 error_description: heapless::String::try_from("unrecognized action").unwrap(),
53 error_details: EmptyPayload,
54 };
55
56 let mut buf = [0u8; 256];
57 let len = serde_json_core::to_slice(&error, &mut buf).unwrap();
58 println!("CALLERROR: {}", core::str::from_utf8(&buf[..len]).unwrap());
59
60 let wrong_type: Result<(Call<AuthorizeRequest>, usize), _> =
63 serde_json_core::from_str(r#"[3,"1","Authorize",{"idTag":"ABC123"}]"#);
64 println!(
65 "a CALLRESULT-shaped frame parsed as Call<T> is rejected: {}",
66 wrong_type.is_err()
67 );
68}