• 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!("PACKET_FLAG incorrect, content: {:#?}", data),
36         ));
37     }
38 
39     let mut payload_head = native_struct::PayloadHead::default();
40     payload_head.parse(data)?;
41     Ok(payload_head)
42 }
43 
unpack_payload_protect(data: Vec<u8>) -> io::Result<native_struct::PayloadProtect>44 pub fn unpack_payload_protect(data: Vec<u8>) -> io::Result<native_struct::PayloadProtect> {
45     let mut payload_protect = native_struct::PayloadProtect::default();
46     payload_protect.parse(data)?;
47     if payload_protect.v_code != PAYLOAD_VCODE {
48         return Err(Error::new(
49             ErrorKind::Other,
50             "Session recv static vcode failed",
51         ));
52     }
53     Ok(payload_protect)
54 }
55 
concat_pack(task_message: TaskMessage) -> Vec<u8>56 pub fn concat_pack(task_message: TaskMessage) -> Vec<u8> {
57     // let data = obj.serialize();
58     let check_sum: u8 = if ENABLE_IO_CHECK {
59         calc_check_sum(&task_message.payload)
60     } else {
61         0
62     };
63     let payload_protect = native_struct::PayloadProtect {
64         channel_id: task_message.channel_id,
65         command_flag: task_message.command as u32,
66         check_sum,
67         v_code: PAYLOAD_VCODE,
68     };
69 
70     let protect_buf = payload_protect.serialize();
71 
72     let payload_head = native_struct::PayloadHead {
73         flag: [PACKET_FLAG[0], PACKET_FLAG[1]],
74         protocol_ver: VER_PROTOCOL as u8,
75         head_size: (protect_buf.len() as u16).to_be(),
76         data_size: (task_message.payload.len() as u32).to_be(),
77         reserve: [0, 0],
78     };
79 
80     crate::trace!(
81         "concat pack, datasize: {}, slicelen: {}, headsize: {}",
82         task_message.payload.len(),
83         task_message.payload.as_slice().len(),
84         protect_buf.len()
85     );
86 
87     let head_buf = payload_head.serialize();
88     [
89         head_buf.as_slice(),
90         protect_buf.as_slice(),
91         task_message.payload.as_slice(),
92     ]
93     .concat()
94 }
95