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//! config 16#![allow(missing_docs)] 17 18use std::convert::TryFrom; 19 20use log::LevelFilter; 21 22pub enum CompressType { 23 None = 0, 24 Lz4, 25 Lz77, 26 Lzma, 27 Brotli, 28} 29 30impl TryFrom<u8> for CompressType { 31 type Error = (); 32 fn try_from(cmd: u8) -> Result<Self, ()> { 33 match cmd { 34 0 => Ok(Self::None), 35 1 => Ok(Self::Lz4), 36 2 => Ok(Self::Lz77), 37 3 => Ok(Self::Lzma), 38 4 => Ok(Self::Brotli), 39 _ => Err(()), 40 } 41 } 42} 43 44#[allow(unused)] 45#[derive(Clone, Default, Debug)] 46pub enum ConnectType { 47 Usb(String), 48 #[default] 49 Tcp, 50 Uart, 51 Bt, 52 Bridge, 53 HostUsb(String), 54} 55 56pub enum ErrCode { 57 Success = 0, 58 ModuleJdwpFailed = -18000, 59} 60 61pub enum NodeType { 62 Server, 63 Daemon, 64} 65 66#[derive(Debug, Clone)] 67pub struct TaskMessage { 68 pub channel_id: u32, 69 pub command: HdcCommand, 70 pub payload: Vec<u8>, 71} 72 73#[derive(PartialEq, Debug, Clone, Copy)] 74#[repr(u32)] 75pub enum HdcCommand { 76 KernelHelp = 0, 77 KernelHandshake, 78 KernelChannelClose, 79 KernelServerKill, 80 KernelTargetDiscover, 81 KernelTargetList, 82 KernelTargetAny, 83 KernelTargetConnect, 84 KernelTargetDisconnect, 85 KernelEcho, 86 KernelEchoRaw, 87 KernelEnableKeepalive, 88 KernelWakeupSlavetask, 89 KernelCheckServer, 90 KernelCheckDevice, 91 KernelWaitFor, 92 93 // New in refactor 94 KernelServerStart, 95 ClientVersion, 96 ClientKeyGenerate, 97 98 // One-pass simple commands 99 UnityCommandHead = 1000, // not use 100 UnityExecute, 101 UnityRemount, 102 UnityReboot, 103 UnityRunmode, 104 UnityHilog, 105 UnityTerminate, 106 UnityRootrun, 107 JdwpList, 108 JdwpTrack, 109 UnityCommandTail, // not use 110 // It will be separated from unity in the near future 111 UnityBugreportInit, 112 UnityBugreportData, 113 // Shell commands types 114 ShellInit = 2000, 115 ShellData, 116 // Forward commands types 117 ForwardInit = 2500, 118 ForwardCheck, 119 ForwardCheckResult, 120 ForwardActiveSlave, 121 ForwardActiveMaster, 122 ForwardData, 123 ForwardFreeContext, 124 ForwardList, 125 ForwardRemove, 126 ForwardSuccess, 127 ForwardRportInit, 128 ForwardRportList, 129 ForwardRportRemove, 130 // File commands 131 FileInit = 3000, 132 FileCheck, 133 FileBegin, 134 FileData, 135 FileFinish, 136 AppSideload, 137 FileMode, 138 DirMode, 139 FileRecvInit, 140 // App commands 141 AppInit = 3500, 142 AppCheck, 143 AppBegin, 144 AppData, 145 AppFinish, 146 AppUninstall, 147 // Flashd commands 148 FlashdUpdateInit = 4000, 149 FlashdFlashInit, 150 FlashdCheck, 151 FlashdBegin, 152 FlashdData, 153 FlashdFinish, 154 FlashdErase, 155 FlashdFormat, 156 FlashdProgress, 157 158 UartFinish, 159} 160 161impl TryFrom<u32> for HdcCommand { 162 type Error = (); 163 fn try_from(cmd: u32) -> Result<Self, ()> { 164 match cmd { 165 0 => Ok(Self::KernelHelp), 166 1 => Ok(Self::KernelHandshake), 167 2 => Ok(Self::KernelChannelClose), 168 3 => Ok(Self::KernelServerKill), 169 4 => Ok(Self::KernelTargetDiscover), 170 5 => Ok(Self::KernelTargetList), 171 6 => Ok(Self::KernelTargetAny), 172 7 => Ok(Self::KernelTargetConnect), 173 8 => Ok(Self::KernelTargetDisconnect), 174 9 => Ok(Self::KernelEcho), 175 10 => Ok(Self::KernelEchoRaw), 176 11 => Ok(Self::KernelEnableKeepalive), 177 12 => Ok(Self::KernelWakeupSlavetask), 178 13 => Ok(Self::KernelCheckServer), 179 14 => Ok(Self::KernelCheckDevice), 180 15 => Ok(Self::KernelWaitFor), 181 182 1000 => Ok(Self::UnityCommandHead), 183 1001 => Ok(Self::UnityExecute), 184 1002 => Ok(Self::UnityRemount), 185 1003 => Ok(Self::UnityReboot), 186 1004 => Ok(Self::UnityRunmode), 187 1005 => Ok(Self::UnityHilog), 188 1006 => Ok(Self::UnityTerminate), 189 1007 => Ok(Self::UnityRootrun), 190 1008 => Ok(Self::JdwpList), 191 1009 => Ok(Self::JdwpTrack), 192 1010 => Ok(Self::UnityCommandTail), 193 1011 => Ok(Self::UnityBugreportInit), 194 1012 => Ok(Self::UnityBugreportData), 195 196 2000 => Ok(Self::ShellInit), 197 2001 => Ok(Self::ShellData), 198 199 2500 => Ok(Self::ForwardInit), 200 2501 => Ok(Self::ForwardCheck), 201 2502 => Ok(Self::ForwardCheckResult), 202 2503 => Ok(Self::ForwardActiveSlave), 203 2504 => Ok(Self::ForwardActiveMaster), 204 2505 => Ok(Self::ForwardData), 205 2506 => Ok(Self::ForwardFreeContext), 206 2507 => Ok(Self::ForwardList), 207 2508 => Ok(Self::ForwardRemove), 208 2509 => Ok(Self::ForwardSuccess), 209 210 3000 => Ok(Self::FileInit), 211 3001 => Ok(Self::FileCheck), 212 3002 => Ok(Self::FileBegin), 213 3003 => Ok(Self::FileData), 214 3004 => Ok(Self::FileFinish), 215 3005 => Ok(Self::AppSideload), 216 3006 => Ok(Self::FileMode), 217 3007 => Ok(Self::DirMode), 218 219 3500 => Ok(Self::AppInit), 220 3501 => Ok(Self::AppCheck), 221 3502 => Ok(Self::AppBegin), 222 3503 => Ok(Self::AppData), 223 3504 => Ok(Self::AppFinish), 224 3505 => Ok(Self::AppUninstall), 225 226 4000 => Ok(Self::FlashdUpdateInit), 227 4001 => Ok(Self::FlashdFlashInit), 228 4002 => Ok(Self::FlashdCheck), 229 4003 => Ok(Self::FlashdBegin), 230 4004 => Ok(Self::FlashdData), 231 4005 => Ok(Self::FlashdFinish), 232 4006 => Ok(Self::FlashdErase), 233 4007 => Ok(Self::FlashdFormat), 234 4008 => Ok(Self::FlashdProgress), 235 236 _ => Err(()), 237 } 238 } 239} 240 241#[allow(unused)] 242pub enum AuthType { 243 None, 244 Token, 245 Signature, 246 Publickey, 247 OK, 248 Fail, 249} 250 251pub enum AppModeType { 252 Install = 1, 253 UnInstall, 254} 255 256impl TryFrom<u8> for AppModeType { 257 type Error = (); 258 fn try_from(cmd: u8) -> Result<Self, ()> { 259 match cmd { 260 1 => Ok(Self::Install), 261 2 => Ok(Self::UnInstall), 262 _ => Err(()), 263 } 264 } 265} 266 267#[repr(u8)] 268pub enum MessageLevel { 269 Fail, 270 Info, 271 Ok, 272} 273 274pub const PACKET_FLAG: &[u8] = "HW".as_bytes(); 275pub const VER_PROTOCOL: u16 = 1; 276pub const ENABLE_IO_CHECK: bool = false; 277pub const PAYLOAD_VCODE: u8 = 0x09; 278pub const HDC_BUF_MAX_SIZE: usize = 0x7fffffff; 279pub const HANDSHAKE_MESSAGE: &str = "OHOS HDC"; 280pub const BANNER_SIZE: usize = 12; 281pub const KEY_MAX_SIZE: usize = 32; 282pub const FILE_PACKAGE_HEAD: usize = 64; 283pub const FILE_PACKAGE_PAYLOAD_SIZE: usize = 49152; 284pub const KERNEL_FILE_NODE_SIZE: u16 = 1024 * 4; // kernel file node 4K buffer 285pub const MAX_PACKET_SIZE_HISPEED: i32 = 512; 286pub const MAX_SIZE_IOBUF: usize = 61440; 287pub const MAX_DIED_SESSION_NUM: usize = 10; 288 289pub const WIN_CMD_PROG: &str = "cmd.exe"; 290pub const SHELL_PROG: &str = "sh"; 291pub const SHELL_TEMP: &str = "/data/local/tmp/hdc-pty"; 292 293pub const LOG_FILE_NAME: &str = "hdc"; 294pub const LOG_BAK_NAME: &str = "hdclast"; 295pub const LOG_TAIL_NAME: &str = ".log"; 296pub const LOG_CACHE_NAME: &str = ".hdc.cache"; 297pub const LOG_FILE_SIZE: usize = 1024 * 1024 * 100; // 100MB 298 299pub const DAEMON_PORT: u16 = 0; 300pub const SERVER_DEFAULT_PORT: u16 = 9710; 301pub const MAX_PORT_NUM: u32 = 65535; 302pub const MAX_PORT_LEN: usize = 5; 303pub const IPV4_MAPPING_PREFIX: &str = "::ffff:"; 304pub const LOCAL_HOST: &str = "127.0.0.1"; 305 306pub const UART_NODE: &str = "/dev/ttyS4"; 307pub const UART_DEFAULT_BAUD_RATE: i32 = 1500000; 308pub const UART_DEFAULT_BITS: i32 = 8; 309pub const UART_EVENT: u8 = 78; 310pub const MAX_UART_SIZE_IOBUF: u32 = 4096; 311 312pub const USB_FFS_BASE: &str = "/dev/usb-ffs/"; 313pub const USB_PACKET_FLAG: &[u8] = "UB".as_bytes(); 314pub const USB_QUEUE_LEN: usize = 64; 315 316pub const TRANSFER_FUNC_NAME: &str = "install"; 317pub const INSTALL_TMP_DIR: &str = "/data/local/tmp/"; 318pub const INSTALL_TAR_MAX_CNT: usize = 512; 319 320pub const ENV_HDC_MODE: &str = "persist.hdc.mode"; 321pub const ENV_HOST_PORT: &str = "persist.hdc.port"; 322pub const MODE_USB: &str = "usb"; 323pub const MODE_TCP: &str = "tcp"; 324pub const PREFIX_PORT: &str = "port "; 325pub const ENV_ROOT_RUN_MODE: &str = "persist.hdc.root"; 326pub const ENV_STARTUP: &str = "ohos.startup.powerctrl"; 327pub const ENV_DEBUGGABLE: &str = "const.debuggable"; 328pub const ENV_SHELL_CONTROL: &str = "persist.hdc.control.shell"; 329pub const ENV_FILE_CONTROL: &str = "persist.hdc.control.file"; 330pub const ENV_FPORT_CONTROL: &str = "persist.hdc.control.fport"; 331 332pub const RSA_BIT_NUM: usize = 3072; 333pub const RSA_PUBKEY_PATH: &str = "/data/service/el1/public/hdc"; 334pub const RSA_PUBKEY_NAME: &str = "hdc_keys"; 335pub const RSA_PRIKEY_PATH: &str = ".harmony"; 336pub const RSA_PRIKEY_NAME: &str = "hdckey"; 337// "\f" asicc is 0x0C 338pub const HDC_HOST_DAEMON_BUF_SEPARATOR: char = '\x0C'; 339// the API WaitParameter can only accept max 96 bytes value 340pub const HDC_PARAMETER_VALUE_MAX_LEN: usize = 96; 341pub const HDC_HOSTNAME_MAX_LEN: usize = HDC_PARAMETER_VALUE_MAX_LEN; 342pub const HDC_WAIT_PARAMETER_FOREVER: i32 = 0; 343pub const HDC_HANDSHAKE_TOKEN_LEN: usize = 32; 344 345pub const DAEOMN_AUTH_SUCCESS: &str = "SUCCESS"; 346pub const DAEOMN_UNAUTHORIZED: &str = "DAEMON_UNAUTH"; 347pub const TLV_TAG_LEN: usize = 16; 348pub const TLV_VAL_LEN: usize = 16; 349pub const TLV_VAL_MAXLEN: usize = 1024; 350pub const TLV_VAL_INVALID_LEN: usize = TLV_VAL_MAXLEN + 1; 351pub const TLV_MIN_LEN: usize = TLV_TAG_LEN + TLV_VAL_LEN; 352pub const TAG_DEVNAME: &str = "devname"; 353pub const TAG_HOSTNAME: &str = "hostname"; 354pub const TAG_PUBKEY: &str = "pubkey"; 355pub const TAG_EMGMSG: &str = "emgmsg"; 356pub const TAG_TOKEN: &str = "token"; 357pub const TAG_DAEOMN_AUTHSTATUS: &str = "daemonauthstatus"; 358 359pub const LOG_LEVEL_ORDER: [LevelFilter; 7] = [ 360 LevelFilter::Off, 361 LevelFilter::Error, 362 LevelFilter::Warn, 363 LevelFilter::Info, 364 LevelFilter::Debug, 365 LevelFilter::Trace, 366 LevelFilter::Trace, 367]; 368 369// |----------------------------------------------------------------| 370// | 31-28 | 27-24 | 23-20 | 19-16 | 15-12 | 11-08 | 07-00 | 371// |----------------------------------------------------------------| 372// | major |reserve| minor |reserve|version| fix | reserve | 373// |----------------------------------------------------------------| 374// 0x30000400 is 3.0.0e 375const HDC_VERSION_NUMBER: u32 = 0x30000400; 376pub const AUTH_BASE_VERSDION: &str = "Ver: 3.0.0b"; 377pub fn get_version() -> String { 378 let major = (HDC_VERSION_NUMBER >> 28) & 0xff; 379 let minor = (HDC_VERSION_NUMBER >> 20) & 0xff; 380 let version = (HDC_VERSION_NUMBER >> 12) & 0xff; 381 let fix = std::char::from_u32(((HDC_VERSION_NUMBER >> 8) & 0xff) + 0x61).unwrap(); 382 format!("Ver: {major}.{minor}.{version}{}", fix) 383} 384