目录

Running a Data App to Process Data

Overview

This section describes how to run a data app in the confidential space to execute data processing tasks.

The confidential space is an isolated computing environment that focuses on the computing of sensitive personal data, and provides dedicated APIs for loading and running data apps.

A data app (DA) is an independent app running in the confidential space. It can use the data provided by an external app and the user data provided by the data management service in the confidential space to complete specific computing tasks, and then send the final computing result back to the external app. Data apps are loaded to the confidential space as dynamic libraries (.so files) through the confidential space APIs.

Constraints

  1. An app can call the runApp API for up to 20 times a day.

  2. A data app can send a maximum of 4 bits of data during a single run, that is, an array with a length of 1. The value range of this element is [0, 15].

  3. The running duration of a data app cannot exceed 5 seconds each time. Instances that time out will be forcibly terminated.

Service Process

To help you understand the complete running process of the confidential space, this section describes the internal running principles of the confidential space.

System service processes related to the confidential space include the confidential space management service and data management service.

The service process for an app to run a DA in the confidential space is as follows:

  1. An app calls runApp to asynchronously initiate a request to run a DA.

    1. The app connects to the confidential space management service in the system, which then starts the confidential space as required.
    2. The service in the space starts a new process in the space and loads the DA dynamic library file to be started.
    3. Call the initialization function of the DA.
    4. After the DA is started, a socket is established between the app and the data management service, and another socket is established between the data management service and the DA. The socket between the app and the data management service is encapsulated into a DataAppHandle object.
  2. The app calls onReceiveData to register a callback for receiving messages.

  3. The app calls sendData to asynchronously send data.

    1. The app sends a packet to the data management service to transfer the data to be sent. The data management service reads the packet and forwards the data to the DA.
    2. The DA receives the message and enters the message processing logic.
    3. After processing the message, the DA sends the data to the app.
    4. The data management service verifies the data sent by the DA. If the verification is successful, the data management service generates a data signature and sends it back to the caller app.
    5. The caller app receives the data and triggers the message receiving callback.
  4. The app calls stop to stop the DA. The confidential space service then terminates the DA process.

API Description

For details about the APIs and their usage, see API Reference.

API Name Description
runApp(appPath: string, argv: string[]): Promise<DataAppHandle> Starts a data app with a given path.
sendData(data: Uint8Array): Promise<void> Sends data to a data app.
onReceiveData(callback: Callback<Uint8Array>): void Registers a callback function for receiving data from a data app.
offReceiveData(callback?: Callback<Uint8Array>): void Unregisters the callback function for receiving data from a data app.
onReceiveDataError(callback: ErrorCallback<BusinessError<DataAppErrorInfo>>): void Registers a callback function for handling errors that occur when receiving data.
offReceiveDataError(callback?: ErrorCallback<BusinessError<DataAppErrorInfo>>): void Unregisters a callback function for handling errors that occur when receiving data.
stop(): void Ends a data app.

Development Procedure

  1. Import the confidential space module.

    import { confidentialSpace } from '@kit.ConfidentialSpaceKit';
    // Import the log module and text-to-binary conversion capability required by the sample code.
    import { buffer } from '@kit.ArkTS';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Call runApp to start a data app in the confidential space.

    const TAG = 'ConfidentialSpace';
    
    let handle: confidentialSpace.DataAppHandle;
    try {
      // Run a data app in the confidential space.
      handle = await confidentialSpace.runApp(`/data/storage/el1/bundle/libs/arm64/libdemo_da.so`, []);
    } catch (e) {
      hilog.error(0x0000, TAG, `Failed to run app. code=${e.code} message=${e.message}`);
      // ...
    }
  3. Call the onReceiveData and onReceiveDataError methods to register the callback function when data is received (or an error occurs during data receiving).

    // Register the message receiving callback and error callback.
    handle.onReceiveData((data: Uint8Array) => {
      hilog.info(0x0000, TAG, `Received data, length=${data.length}`);
      handle.stop();
      handle.offReceiveData();
      handle.offReceiveDataError();
      // ...
    });
    handle.onReceiveDataError((e: BusinessError<confidentialSpace.DataAppErrorInfo>) => {
      hilog.error(0x0000, TAG, `Failed to receive data. code=${e.code} message=${e.message}`);
      if (e.code === 1028700014) { // An error is generated by the data app.
        hilog.error(0x0000, TAG, `Error is from DA; raw code=${e.data?.dataAppErrorCode}`);
      }
      handle.stop();
      handle.offReceiveData();
      handle.offReceiveDataError();
      // ...
    });
  4. Call sendData to send data to the data app.

    // Define the message format as "ECHO:${msg}" and convert it into a character array.
    let payload = new Uint8Array(buffer.from(`ECHO:${msg}`, 'utf-8').buffer);
    // Send data.
    try {
      await handle.sendData(payload);
    } catch (e) {
      hilog.error(0x0000, TAG, `Failed to send data. code=${e.code} message=${e.message}`);
      handle.stop();
      // ...
    }