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 //! Implementation of device status service. 17 18 #![allow(dead_code)] 19 #![allow(unused_variables)] 20 21 use std::ffi::{ c_char, CString }; 22 use std::os::fd::RawFd; 23 use std::sync::{ Mutex, Once }; 24 use hilog_rust::{ error, hilog, HiLogLabel, LogType }; 25 use fusion_data_rust::{ FusionResult }; 26 use fusion_utils_rust::{ call_debug_enter }; 27 use crate::binding::FusionNativeService; 28 29 const LOG_LABEL: HiLogLabel = HiLogLabel { 30 log_type: LogType::LogCore, 31 domain: 0xD002220, 32 tag: "FusionService" 33 }; 34 35 #[derive(Default)] 36 struct FusionServiceImpl { 37 native_service: FusionNativeService, 38 } 39 40 impl FusionServiceImpl { on_start(&self)41 fn on_start(&self) 42 { 43 call_debug_enter!("FusionServiceImpl:on_start"); 44 self.native_service.on_start(); 45 } 46 on_stop(&self)47 fn on_stop(&self) 48 { 49 call_debug_enter!("FusionServiceImpl:on_stop"); 50 self.native_service.on_stop(); 51 } 52 alloc_socket_fd(&self, program_name: &str, module_type: i32, client_fd: &mut RawFd, token_type: &mut i32) -> FusionResult<i32>53 fn alloc_socket_fd(&self, program_name: &str, module_type: i32, 54 client_fd: &mut RawFd, token_type: &mut i32) -> FusionResult<i32> 55 { 56 call_debug_enter!("FusionServiceImpl:alloc_socket_fd"); 57 self.native_service.alloc_socket_fd(program_name, module_type, client_fd, token_type) 58 } 59 } 60 61 /// Proxy for device status service. 62 #[derive(Default)] 63 pub struct FusionService { 64 service_impl: Mutex<FusionServiceImpl>, 65 } 66 67 impl FusionService { 68 /// TODO: add documentation. get_instance() -> Option<&'static Self>69 pub fn get_instance() -> Option<&'static Self> { 70 static mut FUSION_SERVICE_PROXY: Option<FusionService> = None; 71 static INIT_ONCE: Once = Once::new(); 72 unsafe { 73 INIT_ONCE.call_once(|| { 74 FUSION_SERVICE_PROXY = Some(Self::default()); 75 }); 76 FUSION_SERVICE_PROXY.as_ref() 77 } 78 } 79 80 /// Called when service is starting. on_start(&self)81 pub fn on_start(&self) 82 { 83 match self.service_impl.lock() { 84 Ok(guard) => { 85 guard.on_start(); 86 } 87 Err(err) => { 88 error!(LOG_LABEL, "lock error: {}", err); 89 } 90 } 91 } 92 93 /// Called when service is stopping. on_stop(&self)94 pub fn on_stop(&self) 95 { 96 match self.service_impl.lock() { 97 Ok(guard) => { 98 guard.on_stop(); 99 } 100 Err(err) => { 101 error!(LOG_LABEL, "lock error: {}", err); 102 } 103 } 104 } 105 106 /// Call to allocate socket pair for client/server communication. alloc_socket_fd(&self, program_name: &str, module_type: i32, client_fd: &mut RawFd, token_type: &mut i32) -> FusionResult<i32>107 pub fn alloc_socket_fd(&self, program_name: &str, module_type: i32, 108 client_fd: &mut RawFd, token_type: &mut i32) -> FusionResult<i32> 109 { 110 match self.service_impl.lock() { 111 Ok(guard) => { 112 guard.alloc_socket_fd(program_name, module_type, client_fd, token_type) 113 } 114 Err(err) => { 115 error!(LOG_LABEL, "lock error: {}", err); 116 Err(-1) 117 } 118 } 119 } 120 } 121