ohos_rust_binding 0.1.0

Rust binding for OHOS
Documentation
  • Coverage
  • 0%
    0 out of 1 items documented0 out of 0 items with examples
  • Size
  • Source code size: 37.47 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 997.79 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 55s Average build duration of successful builds.
  • all releases: 55s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • lwecei

ohos_rust_binding

This is a Rust language binding library for the OpenHarmony operating system, allowing developers to interact with OpenHarmony native APIs using Rust.

Features

  • Provides Rust language bindings for OpenHarmony native APIs
  • Supports various OpenHarmony feature modules, including:
    • Logging system (log)
    • ArkTS callback handling (arkts_cb)
    • XComponent access (xcomponent)
    • Account management (account)
    • Resource management (asset)
    • Bundle management (bundle)
    • Drawing functionality (drawing)
    • EGL graphics interface (egl)
    • HiAppEvent events (hiappevent)
    • HiDebug debugging tools (hidebug)
    • HiTrace tracing (hitrace)
    • Input event handling (input)
    • Relational database (rdb)
    • Task scheduler (scheduler)
    • Web functionality access (web)
    • Window management (window)
  • Cross-platform development support
  • Type-safe API calls

Environment Requirements

  • Rust 1.60+ (latest stable version recommended)
  • Cargo package manager
  • OpenHarmony Native SDK

Installation and Configuration

1. Set Environment Variables

Before using this library, you need to set the following environment variables:

# Set OpenHarmony Native SDK path
export OHOS_NATIVE_HOME=/path/to/ohos/native/sdk

2. Add Dependency

Add the ohos_rust_binding dependency to your Cargo.toml file:

[dependencies]
ohos_rust_binding = {
    path = "../ohos_rust_binding",
    features = ["log", "arkts_cb", "xcomponent"] # Select modules as needed
}

Build Instructions

Supported Target Platforms

The library currently supports the following target platform:

  • aarch64-unknown-linux-ohos

Build Commands

# Execute in the ohos_rust_binding directory
cargo build

# Build for a specific target platform
cargo build --target aarch64-unknown-linux-ohos

Usage Examples

Log Module Example

use ohos_rust_binding::log::*;

fn main() {
    // Output logs of different levels
    error!("This is an error log");
    warn!("This is a warning log");
    info!("This is an info log");
    debug!("This is a debug log");
    
    // Log with formatting parameters
    let app_name = "MyApp";
    info!("Application {} has started", app_name);
}

XComponent Example

use ohos_rust_binding::xcomponent::*;
use napi::{CallContext, JsObject, Result};

fn get_xcomponent_id(ctx: CallContext) -> Result<JsObject> {
    // Get XComponent object from callback information
    let xcomponent = get_x_component_from_callback(ctx.env, ctx.this_unchecked())?;
    
    // Get XComponent ID
    let id = get_x_component_id(&xcomponent)?;
    
    // Process XComponent ID...
    Ok(ctx.env.create_string_from_std(id)?.into_object()?)
}

ArkTS Callback Example

use ohos_rust_binding::arkts_cb::*;
use napi::{CallContext, Error, Result};

fn call_arkts_function(ctx: CallContext) -> Result<()> {
    // Assuming this is a callback function passed from JavaScript/ArkTS layer
    let callback = ctx.get::<JsFunction>(0)?;
    
    // Prepare parameters
    let params = vec![ctx.env.create_string("Hello from Rust!")?.into()];
    
    // Call ArkTS function
    match thread_safe_func_call_ark_ts(ctx.env, &callback, params) {
        Ok(_) => Ok(()),
        Err(err) => Err(Error::from_reason(format!("Failed to call ArkTS function: {}", err))),
    }
}

Development Notes

  1. Platform Compatibility:

    • On unsupported target platforms, the build process will automatically skip bindgen code generation but still allows development work in the development environment.
    • When running on unsupported platforms, relevant API calls may return errors or perform no operations.
  2. Memory Management:

    • Pay attention to memory management differences between Rust and C when using this library
    • Avoid creating memory leaks, especially when handling C strings and pointers
  3. Asynchronous Operations:

    • For asynchronous operations, it is recommended to use Rust's Future and async/await syntax
    • Communicate across threads through napi's thread-safe functions

Contribution Guidelines

Contributions to this project are welcome! If you want to participate in development, please follow these steps:

  1. Fork this repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

If you have any questions or suggestions, please raise them on the project's Issue page.