• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 //! packet_assemble
16 #![allow(missing_docs)]
17 
18 use crate::config::TaskMessage;
19 use crate::config::*;
20 use crate::serializer::native_struct;
21 use crate::serializer::serialize::Serialization;
22 #[allow(unused)]
23 use crate::utils::hdc_log::*;
24 
25 use std::io::{self, Error, ErrorKind};
26 
calc_check_sum(data: &[u8]) -> u827 fn calc_check_sum(data: &[u8]) -> u8 {
28     data.iter().sum()
29 }
30 
unpack_payload_head(data: Vec<u8>) -> io::Result<native_struct::PayloadHead>31 pub fn unpack_payload_head(data: Vec<u8>) -> io::Result<native_struct::PayloadHead> {
32     if data[..PACKET_FLAG.len()] != PACKET_FLAG[..] {
33         return Err(Error::new(
34             ErrorKind::Other,
35             format!(
36                 "PACKET_FLAG incorrect, content: {:?}",
37                 data.iter()
38                     .map(|&c| format!("{c:02X}"))
39                     .collect::<Vec<_>>()
40                     .join(" ")
41             ),
42         ));
43     }
44 
45     let mut payload_head = native_struct::PayloadHead::default();
46     payload_head.parse(data)?;
47     Ok(payload_head)
48 }
49 
unpack_payload_protect(data: Vec<u8>) -> io::Result<native_struct::PayloadProtect>50 pub fn unpack_payload_protect(data: Vec<u8>) -> io::Result<native_struct::PayloadProtect> {
51     let mut payload_protect = native_struct::PayloadProtect::default();
52     payload_protect.parse(data)?;
53     if payload_protect.v_code != PAYLOAD_VCODE {
54         return Err(Error::new(
55             ErrorKind::Other,
56             "Session recv static vcode failed",
57         ));
58     }
59     Ok(payload_protect)
60 }
61 
concat_pack(task_message: TaskMessage) -> Vec<u8>62 pub fn concat_pack(task_message: TaskMessage) -> Vec<u8> {
63     // let data = obj.serialize();
64     let check_sum: u8 = if ENABLE_IO_CHECK {
65         calc_check_sum(&task_message.payload)
66     } else {
67         0
68     };
69     let payload_protect = native_struct::PayloadProtect {
70         channel_id: task_message.channel_id,
71         command_flag: task_message.command as u32,
72         check_sum,
73         v_code: PAYLOAD_VCODE,
74     };
75 
76     let protect_buf = payload_protect.serialize();
77 
78     let payload_head = native_struct::PayloadHead {
79         flag: [PACKET_FLAG[0], PACKET_FLAG[1]],
80         protocol_ver: VER_PROTOCOL as u8,
81         head_size: (protect_buf.len() as u16).to_be(),
82         data_size: (task_message.payload.len() as u32).to_be(),
83         reserve: [0, 0],
84     };
85 
86     let head_buf = payload_head.serialize();
87     [
88         head_buf.as_slice(),
89         protect_buf.as_slice(),
90         task_message.payload.as_slice(),
91     ]
92     .concat()
93 }
94