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 //! Binding of native service implementation.
17 
18 #![allow(dead_code)]
19 #![allow(unused_variables)]
20 
21 use std::ffi::{ c_char, c_int, CString };
22 use std::os::fd::RawFd;
23 use hilog_rust::{ error, hilog, HiLogLabel, LogType };
24 use fusion_utils_rust::{ call_debug_enter, FusionResult, FusionErrorCode };
25 
26 const LOG_LABEL: HiLogLabel = HiLogLabel {
27     log_type: LogType::LogCore,
28     domain: 0xD002220,
29     tag: "FusionNativeService"
30 };
31 
32 /// Represent service instance.
33 #[repr(C)]
34 struct NativeService {
35     _private: [u8; 0],
36 }
37 
38 extern "C" {
39     /// Call to create an instance of service.
NativeServiceNew() -> *mut NativeService40     fn NativeServiceNew() -> *mut NativeService;
41     /// Call to increase reference count of the service instance.
NativeServiceRef(service: *mut NativeService) -> *mut NativeService42     fn NativeServiceRef(service: *mut NativeService) -> *mut NativeService;
43     /// Call to decrease reference count of the service instance.
NativeServiceUnref(service: *mut NativeService) -> *mut NativeService44     fn NativeServiceUnref(service: *mut NativeService) -> *mut NativeService;
45     /// Called on dump operation.
NativeServiceOnDump(service: *mut NativeService)46     fn NativeServiceOnDump(service: *mut NativeService);
47     /// Called when service is starting.
NativeServiceOnStart(service: *mut NativeService)48     fn NativeServiceOnStart(service: *mut NativeService);
49     /// Called when service is stopping.
NativeServiceOnStop(service: *mut NativeService)50     fn NativeServiceOnStop(service: *mut NativeService);
51     /// Call to allocate socket pair for client/server communication.
NativeServiceAllocSocketFd(service: *mut NativeService, program_name: *const c_char, module_type: i32, client_fd: *mut i32, token_type: *mut i32) -> i3252     fn NativeServiceAllocSocketFd(service: *mut NativeService,
53         program_name: *const c_char, module_type: i32,
54         client_fd: *mut i32, token_type: *mut i32) -> i32;
55 }
56 
57 pub struct FusionNativeService {
58     service: *mut NativeService,
59 }
60 
61 impl FusionNativeService {
is_valid(&self) -> bool62     pub fn is_valid(&self) -> bool
63     {
64         !self.service.is_null()
65     }
66 
on_start(&self)67     pub fn on_start(&self)
68     {
69         call_debug_enter!("FusionNativeService:on_start");
70         unsafe { NativeServiceOnStart(self.service) };
71     }
72 
on_stop(&self)73     pub fn on_stop(&self)
74     {
75         call_debug_enter!("FusionNativeService:on_stop");
76         unsafe { NativeServiceOnStop(self.service) };
77     }
78 
alloc_socket_fd(&self, program_name: &str, module_type: i32, client_fd: &mut RawFd, token_type: &mut i32) -> FusionResult<()>79     pub fn alloc_socket_fd(&self, program_name: &str, module_type: i32,
80         client_fd: &mut RawFd, token_type: &mut i32) -> FusionResult<()>
81     {
82         call_debug_enter!("FusionNativeService:alloc_socket_fd");
83         let mut fd: c_int = 0;
84 
85         let ret = unsafe {
86             NativeServiceAllocSocketFd(self.service, program_name.as_ptr() as *const c_char,
87                 module_type, &mut fd, token_type)
88         };
89         if ret == 0 {
90             *client_fd = fd as RawFd;
91             Ok(())
92         } else {
93             error!(LOG_LABEL, "Failed to allocate socket pair");
94             Err(FusionErrorCode::Fail)
95         }
96     }
97 }
98 
99 impl Default for FusionNativeService {
default() -> Self100     fn default() -> Self
101     {
102         Self {
103             service: unsafe {
104                 NativeServiceNew()
105             },
106         }
107     }
108 }
109 
110 impl Drop for FusionNativeService {
drop(&mut self)111     fn drop(&mut self)
112     {
113         unsafe {
114             NativeServiceUnref(self.service);
115         }
116     }
117 }
118 
119 impl Clone for FusionNativeService {
clone(&self) -> Self120     fn clone(&self) -> Self
121     {
122         Self {
123             service: unsafe {
124                 NativeServiceRef(self.service)
125             },
126         }
127     }
128 }
129