• 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 
16 use std::ffi::{CString, c_char};
17 use hilog_rust::{hilog, error, HiLogLabel, LogType};
18 use std::mem::size_of;
19 use crate::stream_buffer::StreamBuffer;
20 const STREAM_BUF_WRITE_FAIL: i32 = 2;
21 const LOG_LABEL: HiLogLabel = HiLogLabel {
22     log_type: LogType::LogCore,
23     domain: 0xD002700,
24     tag: "NetPacket"
25 };
26 
27 #[repr(packed(1))]
28 #[repr(C)]
29 pub(crate) struct PackHead {
30     pub(crate) id_msg: MessageId,
31     pub(crate) size: usize,
32 }
33 
34 #[derive(Copy, Clone)]
35 #[repr(C)]
36 pub(crate) enum MessageId {
37     Invalid = 0,
38     Device,
39     DeviceIds,
40     DeviceSupportKeys,
41     AddDeviceListener,
42     DeviceKeyboardType,
43     DisplayInfo,
44     NoticeAnr,
45     MarkProcess,
46     OnSubscribeKey,
47     OnKeyEvent,
48     OnPointerEvent,
49     ReportKeyEvent,
50     ReportPointerEvent,
51     OnDeviceAdded,
52     OnDeviceRemoved,
53     CoordinationAddListener,
54     CoordinationMessage,
55     CoordinationGetState,
56     DragNotifyResult,
57     DragStateListener,
58 }
59 
60 #[derive(Copy, Clone)]
61 #[repr(C)]
62 pub(crate) struct NetPacket {
63     pub(crate) msg_id: MessageId,
64     pub(crate) stream_buffer: StreamBuffer,
65 }
66 
67 #[derive(Copy, Clone)]
68 #[repr(C)]
69 pub struct CNetPacket {
70     pub(super) msg_id: MessageId,
71     pub(super) stream_buffer_ptr: *const StreamBuffer,
72 }
73 
74 impl Default for NetPacket {
default() -> Self75     fn default() -> Self {
76         Self {
77             msg_id: MessageId::Invalid,
78             stream_buffer: Default::default(),
79         }
80     }
81 }
82 
83 impl NetPacket {
as_ref<'a>(object: *const Self) -> Option<&'a Self>84     fn as_ref<'a>(object: *const Self) -> Option<&'a Self> {
85         // SAFETY: as_ref has already done no-null verification inside
86         unsafe {
87             object.as_ref()
88         }
89     }
as_mut<'a>(object: *mut Self) -> Option<&'a mut Self>90     fn as_mut<'a>(object: *mut Self) -> Option<&'a mut Self> {
91         // SAFETY: as_mut has already done no-null verification inside
92         unsafe {
93             object.as_mut()
94         }
95     }
size(&self) -> usize96     fn size(&self) -> usize {
97         self.stream_buffer.size()
98     }
get_packet_length(&self) -> usize99     pub(super) fn get_packet_length(&self) -> usize {
100         size_of::<PackHead>() + self.stream_buffer.w_pos
101     }
make_data(&self, buf: &mut StreamBuffer)102     pub(crate) fn make_data(&self, buf: &mut StreamBuffer) {
103         let head = PackHead {
104             id_msg: self.msg_id,
105             size: self.stream_buffer.w_pos,
106         };
107         buf.write(head);
108         if self.stream_buffer.w_pos > 0 && !buf.write_char_usize(&self.stream_buffer.sz_buff[0] as *const c_char,
109             self.stream_buffer.w_pos) {
110             error!(LOG_LABEL, "Write data to stream failed, errCode:{}", STREAM_BUF_WRITE_FAIL);
111         }
112     }
113 }
114 
115