IVI पर SDV Gateway का इस्तेमाल करना

गाड़ी में सूचना और मनोरंजन की सुविधाएं (आईवीआई) देने वाले सिस्टम पर मौजूद सॉफ़्टवेयर डिफाइंड व्हीकल (एसडीवी) गेटवे, आईवीआई सिस्टम और रिमोट एसडीवी सेवाओं के बीच कम्यूनिकेशन को आसान बनाता है. गेटवे की मदद से, ओईएम के Java ऐप्लिकेशन और नेटिव सेवाएं, जैसे कि VHAL, एसडीवी सेवाओं के साथ इंटरैक्ट कर सकती हैं. गेटवे, कम्यूनिकेशन के लिए पहले से तय किए गए तरीकों का इस्तेमाल करता है. इनमें सेवा का रजिस्ट्रेशन, खोज, और आरपीसी शामिल हैं.

यह गेटवे, AAOS SDV प्रोजेक्ट की कुछ खास ज़रूरी शर्तों को पूरा करता है. जैसे, प्रॉपर्टी की जानकारी के लिए SDV डेटा टनल का इस्तेमाल करके, वीएचएएल रेफ़रंस लागू करने की सुविधा चालू करना. इससे IVI पर मौजूद Java और Kotlin Android ऐप्लिकेशन, SDV Comms स्टैक का इस्तेमाल कर सकते हैं. साथ ही, सेवाओं के तौर पर रजिस्टर कर सकते हैं, SDV की अन्य सेवाएं ढूंढ सकते हैं, और उनसे कम्यूनिकेट कर सकते हैं.

SDV Gateway के कोड की जगहों और SDV Gateway के क्लाइंट के उदाहरणों के लिए, कोड की जगहें देखें.

SDV Gateway इंटिग्रेशन मॉडल

IVI आर्किटेक्चर पर SDV गेटवे के ज़रिए SDV-IVI Comms का इस्तेमाल करना

SDV Gateway, Java ऐप्लिकेशन, नेटिव सेवाओं, SDV Comms स्टैक, और वाहन नेटवर्क के साथ इंटरैक्ट करता है. पहली इमेज में इन इंटरैक्शन के बारे में बताया गया है:

SDV Gateway के साथ इंटरैक्शन

पहली इमेज. SDV Gateway के साथ इंटरैक्शन.

इस सिस्टम डायग्राम में:

  • ऐप्लिकेशन, क्लाइंट सेवाओं के ज़रिए SDV Gateway से इंटरैक्ट करते हैं.
  • AAOS SDV SDK टूल से आपको ये सुविधाएं मिलती हैं:
    • यह इंटरप्रोसेस कम्यूनिकेशन के लिए, ISdvGateway AIDL API है.
    • नेटवर्क इंटरैक्शन के लिए कम्यूनिकेशन लाइब्रेरी.
    • नेटिव सेवा इंटिग्रेशन के लिए, C API का इस्तेमाल करना आसान है.
  • ISdvGateway AIDL API को SDV Gateway सेवा और सबसिस्टम लागू करते हैं.
  • SDV Gateway सेवा, इन चीज़ों को मैनेज करती है:
    • दोनों दिशाओं में डिवाइस और सेवाओं की अपने-आप पहचान करना.
    • रिमोट एसडीवी सेवाओं के साथ कम्यूनिकेशन.
    • कारोबार के मुख्य नियम.
  • SDV Gateway सबसिस्टम, वाहन के नेटवर्क से कनेक्ट होता है.
  • नेटिव सेवाएं, ISdvGateway AIDL API का इस्तेमाल सीधे तौर पर या SDK के C API के ज़रिए कर सकती हैं. इनमें VHAL लागू करने की सुविधा भी शामिल है.
  • VHAL प्रॉक्सी, VHAL को लागू करने के लिए एक रेफ़रंस के तौर पर काम करता है. इसमें VSIDL मैपिंग इंटिग्रेशन शामिल होता है.

IVI की नेटिव सेवा पर SDV Gateway के लिए इंटिग्रेशन मॉडल

इंटीग्रेशन मॉडल को दूसरी इमेज में दिखाया गया है:

SDV Gateway इंटिग्रेशन मॉडल

दूसरी इमेज. SDV Gateway इंटिग्रेशन मॉडल.

IVI की नेटिव सेवा पर SDV Gateway का इस्तेमाल करना

तीसरी इमेज में, IVI पर SDV Gateway के इस्तेमाल के बारे में बताया गया है:

IVI पर SDV Gateway

तीसरी इमेज. IVI पर SDV Gateway.

पहले से तय शर्तें

Binder थ्रेड पूल शुरू करें:

  • SDV Gateway क्लाइंट लाइब्रेरी को Binder सेवाओं से एसिंक्रोनस कॉलबैक पाने के लिए, शुरू किए गए Binder थ्रेड पूल की ज़रूरत होती है.

  • जब Binder थ्रेड पूल शुरू नहीं होता है, तब SDV Gateway क्लाइंट बनाने के लिए ज़रूरी एपीआई काम नहीं करता.

नेटिव गेटवे क्लाइंट लाइब्रेरी शामिल करें

नेटिव गेटवे क्लाइंट लाइब्रेरी, C API को ऐक्सेस करने की सुविधा देती है. C API का इस्तेमाल करने के लिए, libsdvgatewayclient का एक इंस्टेंस डिपेंडेंसी के तौर पर जोड़ें:

cc_binary {
    name: "your_binary_name",
    srcs: ["main.cpp"],
    shared_libs: [
        "libsdvgatewayclient",
    ],
}

नेटिव गेटवे क्लाइंट लोड करना

#include "libsdvgatewayclient.h"

नेटिव क्लाइंट इंस्टेंस बनाना

ASDVGateway_Client* client;
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_new(
     &client, /*outStatus*/nullptr);

क्लाइंट बनाने के बाद, क्लाइंट:

  • यह कुकी, Gateway सेवा के साथ होने वाले सभी इंटरैक्शन के लिए स्थिति को सेव करती है.

  • यह SDV की सुविधा वाली अन्य सेवाओं के साथ सभी इंटरैक्शन के कॉन्टेक्स्ट के तौर पर काम करता है. साथ ही, इसे C API फ़ंक्शन में पहले पैरामीटर के तौर पर पास किया जाता है.

स्टेटस कोड और गड़बड़ी के मैसेज

ज़्यादातर C API फ़ंक्शन की परिभाषा यह है:

ASDVGateway_StatusCode_t ApiFunctionName(..., ASDVGateway_Status_t* outStatus);

सफलता का आकलन करने के लिए, दिखाए गए स्टेटस कोड की जांच की जा सकती है. यह ASDVGateway_StatusCode_t टाइप का होता है. इसके अलावा, किसी स्ट्रक्चर का पॉइंटर भी पास किया जा सकता है. इससे फ़ंक्शन, स्टेटस कोड और गड़बड़ी का मैसेज भर सकता है. पॉइंटर को आखिरी पैरामीटर के तौर पर पास किया जाता है. इसका नाम outStatus है. शून्य वैल्यू का मतलब है कि आउटपुट स्ट्रक्चर का इस्तेमाल नहीं किया गया है.

कॉलर को गड़बड़ी के मैसेज के लिए, मेमोरी स्टेटस स्ट्रक्चर असाइन करना होगा. स्टेटस स्ट्रक्चर में, स्टेटस कोड और गड़बड़ी का मैसेज, दोनों शामिल हो सकते हैं. नया क्लाइंट बनाते समय गड़बड़ी का मैसेज पाने का उदाहरण उपलब्ध है.

सफलता का आकलन करने के लिए:

  1. जवाब में मिले स्टेटस कोड की जांच करें. यह ASDVGateway_StatusCode_t टाइप का होता है.

  2. उस स्ट्रक्चर का पॉइंटर पास करें जहां फ़ंक्शन, स्टेटस कोड और गड़बड़ी का मैसेज भर सकता है.

    • पॉइंटर को आखिरी पैरामीटर के तौर पर पास किया जाता है. इसका नाम outStatus है.
    • शून्य वैल्यू का मतलब है कि आउटपुट स्ट्रक्चर का इस्तेमाल नहीं किया जाता.
    • कॉल करने वाले को, गड़बड़ी के मैसेज के लिए मेमोरी स्टेटस स्ट्रक्चर असाइन करना होगा.
    • स्टेटस स्ट्रक्चर में स्टेटस कोड और गड़बड़ी का मैसेज शामिल हो सकता है.

यहां एक सैंपल दिया गया है, जिसमें नए क्लाइंट के लिए गड़बड़ी का मैसेज वापस पाने का तरीका बताया गया है:

#include <iostream>
#include <array>

struct StatusWithErrorMsg : ASDVGateway_Status_t {
    StatusWithErrorMsg() {
        // Ensure the base struct pointers point to our internal buffer
        errorMessage = errorMessageBuffer.data();
        maxErrorMessageSize = errorMessageBuffer.size();

        // Good practice: Zero-initialize the buffer
        errorMessageBuffer.fill(0);
    }

    std::array<char, 256> errorMessageBuffer;
};

// --- Execution ---

ASDVGateway_Client* client = nullptr;
StatusWithErrorMsg status;

// Initialize the client
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_new(&client, &status);

// Log Results
std::cout << "Returned statusCode:    " << statusCode << std::endl;
std::cout << "Status Struct Code:     " << status.statusCode << std::endl;
std::cout << "Status Error Message:   " << status.errorMessage << std::endl;

इस उदाहरण में:

  • स्टेटस स्ट्रक्चर में मौजूद स्टेटस कोड की वैल्यू, दिखाए गए स्टेटस कोड की वैल्यू के बराबर है.

  • गड़बड़ी का मैसेज, सिर्फ़ maxErrorMessageSize वर्णों की सीमा तक लिखा जाता है. इसमें नल टर्मिनेटिंग वर्ण \0 भी शामिल है. अगर कोई गड़बड़ी नहीं होती है (स्टेटस कोड OK होता है), तो गड़बड़ी का मैसेज एक खाली स्ट्रिंग होता है.

Init comms

Init comms, SDV Comm Stack और SDV Gateway का इस्तेमाल करके, कॉलर ऐप्लिकेशन और अन्य ऐप्लिकेशन के बीच बातचीत शुरू करता है. इन दोनों कॉन्टेक्स्ट में, Init comms को कॉल किया जा सकता है::

  • क्लाइंट खाता बनने के बाद.

  • डेटा टनल, आरपीसी या सेवा की खोज से जुड़े किसी भी इंटरैक्शन से पहले.

यहां एक उदाहरण दिया गया है:

ASDVGateway_InitCommsParams_t params{
    .packageName         = "android.sdv.samples.gateway.client",
    .serviceBundleName   = "NativeTestApp",
    .serviceInstanceName = "default",
};

// Initialize communications and capture the status code
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_initComms(client, &params, &status);

// Recommended check
if (statusCode != ASDVGateway_StatusCode_OK) {
    std::cerr << "Failed to init comms: " << status.errorMessage << std::endl;
}

डिवाइस और सेवाओं की अपने-आप पहचान करना

किसी खास तरह की सेवा इकाइयों या नाम के रजिस्टर होने या रजिस्टर न होने पर, आपको सूचनाएं मिल सकती हैं. कॉलबैक फ़ंक्शन को उस थ्रेड में सूचना दी जाती है जिसका मालिकाना हक गेटवे क्लाइंट के पास होता है. यह कॉलबैक, C API कॉल के तुरंत बाद रजिस्टर की गई सभी सेवा इकाइयों के साथ ट्रिगर होता है. शुरुआती ट्रिगर के बाद, सूचनाएं अपडेट होती रहती हैं. ऐसा तब तक होता है, जब तक कि सुनने वाला व्यक्ति साफ़ तौर पर सदस्यता रद्द नहीं कर देता.

class NativeSdvGatewayTestApp {
public:
    static void ServiceUnitChangeListenerCallback(
        const ASDVGateway_ServiceUnitChangeEventType eventType,
        const ASDVGateway_ServiceUnitDefinition* serviceUnitDefinition,
        void* userData
    ) {
        auto* testApp = reinterpret_cast<NativeSdvGatewayTestApp*>(userData);

        if (testApp) {
            // Logic to react when services are registered or unregistered
            // e.g., testApp->handleServiceChange(eventType, serviceUnitDefinition);
        }
    }
};

// --- Listener Registration ---

// Ensure thisApp remains in scope as long as the listener is active
NativeSdvGatewayTestApp* thisApp = get_current_app_context();

ASDVGateway_UnitType_t unitType{
    .sdvPackageName     = "android.sdv.samples.sdv_gateway",
    .serviceBundleName  = "DtPublisher",
    .unitTypeName       = "TirePressure",
};

// Register the listener
ASDVGateway_Client_registerListenerForServiceUnitChangeByType(
    client,
    &unitType,
    NativeSdvGatewayTestApp::ServiceUnitChangeListenerCallback,
    static_cast<void*>(thisApp), // userData
    &listenerHandle,
    &status
);

// --- Cleanup ---

// Unregistering stops all notification to the callback function
ASDVGateway_Client_unregisterListenerForServiceUnitChangeByType(
    client,
    listenerHandle,
    &status
);

बिना किसी सूचना के, रजिस्टर की गई सेवा इकाइयों को वापस पाने के लिए, ASDVGateway_Client_fetchServiceUnitsByType और ASDVGateway_Client_fetchServiceUnitsByName एपीआई का इस्तेमाल करें.

class NativeSdvGatewayTestApp {
public:
    /**
     * Callback triggered for each service unit found that matches the requested type.
     */
    static void FetchServiceUnitsCallback(
        const ASDVGateway_ServiceUnitDefinition* serviceUnitDefinition,
        void* userData
    ) {
        auto* app = reinterpret_cast<NativeSdvGatewayTestApp*>(userData);

        if (serviceUnitDefinition) {
            // The service unit is registered with service discovery.
            // Example: processServiceUnit(serviceUnitDefinition);
        }
    }
};

// --- Execution ---

ASDVGateway_UnitType_t unitType{
    .sdvPackageName     = "android.sdv.samples.sdv_gateway",
    .serviceBundleName  = "DtPublisher",
    .unitTypeName       = "TirePressure",
};

// Context used to differentiate between various fetchServiceUnits calls
void* userData = static_cast<void*>(thisApp);

ASDVGateway_Client_fetchServiceUnitsByType(
    client,
    &unitType,
    NativeSdvGatewayTestApp::FetchServiceUnitsCallback,
    userData,
    &status
);

ASDVGateway_Client_fetchServiceUnitsByType एपीआई कॉल के दौरान, कॉलर के थ्रेड में कॉलबैक को सिंक्रोनस तरीके से ट्रिगर किया जाता है. यूनिट टाइप के बजाय नाम के हिसाब से रजिस्टर की गई सेवा यूनिट पाने के लिए, ASDVGateway_Client_fetchServiceUnitsByName का इस्तेमाल करें:

ASDVGateway_StatusCode_t ASDVGateway_Client_fetchServiceUnitsByName(
    // [in]  Opaque pointer to a client object.
    const ASDVGateway_Client* client,

    // [in]  Pointer to a structure containing the package name,
    //       service bundle name, and service unit name.
    const ASDVGateway_UnitNameDiscoveryArgs_t* unitName,

    // [in]  Callback function to be called for each service unit
    //       definition registered. Called synchronously in the
    //       caller's thread before the fetch completes.
    ASDVGateway_FetchServiceUnitsCallback callback,

    // [in]  Optional value passed back as a parameter of the callback.
    void* userData,

    // [out] Optional pointer to status structure for result
    //       codes and error messages.
    ASDVGateway_Status_t* outStatus
);

आरपीसी फ़्लो

गेटवे क्लाइंट लाइब्रेरी, SDV की सुविधा वाले अन्य ऐप्लिकेशन के साथ कम्यूनिकेशन के लिए, ट्रांसपोर्ट लेयर सिक्योरिटी (टीएलएस) सेटिंग को मैनेज करती है. खास तौर पर, यह कम्यूनिकेशन के लिए ज़रूरी TLS सेटिंग को वापस लाता है. टीएलएस के इस्तेमाल का पता इस तरह लगाया जाता है:

  • एसडीवी बूट मोड LOCKED होने पर, TLS का इस्तेमाल किया जाता है.

  • एसडीवी बूट मोड UNLOCKED पर सेट होने पर, असुरक्षित कम्यूनिकेशन का इस्तेमाल किया जाता है. TLS का इस्तेमाल करने पर, Gateway क्लाइंट लाइब्रेरी एक पासकोड पेयर जनरेट करती है. यह पासकोड पेयर सिर्फ़ ऐप्लिकेशन प्रोसेस को पता होता है. क्लाइंट, आरपीसी सर्वर या आरपीसी क्लाइंट चैनल बनाने के लिए, आरपीसी क्रेडेंशियल फिर से हासिल करता है. आरपीसी, एसडीवी-आरपीसी वीएलएएन का इस्तेमाल करता है. गेटवे क्लाइंट लाइब्रेरी, प्रोसेस के डिफ़ॉल्ट नेटवर्क को SDV-RPC वीएलएएन पर स्विच करने के लिए android_setprocnetwork को कॉल करती है. ऐसा ASDVGateway_Client_initComms कॉल के दौरान या उसके बाद किया जाता है.

RPC की उपलब्धता

जल्दी बूट होने के लिए कॉन्फ़िगर किया गया क्लाइंट, SDV-RPC वीएलएएन के उपलब्ध होने से पहले शुरू हो सकता है. कोई आरपीसी सर्वर या आरपीसी क्लाइंट सॉकेट बनाने की कोशिश करने से पहले, यह देख लें कि आरपीसी उपलब्ध है या नहीं:

// Check if the RPC (Remote Procedure Call) service is available
bool isRpcAvailable = ASDVGateway_Client_isRpcAvailable(client);

if (isRpcAvailable) {
    // Proceed with RPC calls
} else {
    // Handle the case where RPC is not yet ready or available
}

या

// Check if the process is correctly bound to the SDV RPC Network Interface/VLAN
bool isRpcNetworkBound = ASDVGateway_Client_isProcessBoundToSdvRpcNetworkInterface(client);

if (!isRpcNetworkBound) {
    // Usually implies the process isn't running on the correct network interface
    // or the VLAN configuration is missing.
    std::cerr << "Warning: Process is not bound to the SDV RPC VLAN." << std::endl;
}

आरपीसी की उपलब्धता का स्टेटस बदलने पर सूचना पाने के लिए, ASDVGateway_Client_setClientNotificationCallback का इस्तेमाल करके क्लाइंट इवेंट के लिए लिसनर सेट करें. ASDVGateway_Client_isProcessBoundToSdvRpcNetworkInterface को प्राथमिकता दी जाती है, क्योंकि यह जांच करता है कि आरपीसी उपलब्ध है या नहीं. साथ ही, यह भी जांच करता है कि प्रोसेस, SDV-RPC वीएलएएन से जुड़ी है या नहीं. ऐसा तब होता है, जब ऐप्लिकेशन प्रोसेस के नेटवर्क को स्विच करता है.

प्रोसेस के डिफ़ॉल्ट नेटवर्क को स्विच करना

इंटरनेट से जुड़े कनेक्शन के लिए सॉकेट खोलने के लिए, आपको प्रोसेस के डिफ़ॉल्ट नेटवर्क के तौर पर SDV-RPC VLAN से स्विच करना पड़ सकता है. SDV-RPC वीएलएएन से प्रोसेस को अनबाइंड और रीबाइंड करने के लिए, ASDVGateway_Client_unbindProcessFromSdvRpcNetworkInterface और ASDVGateway_Client_bindProcessToSdvRpcNetworkInterface को कॉल करें. ये दोनों कॉल, ग्लोबल स्विच की तरह काम करते हैं. ये प्रोसेस के सभी थ्रेड के लिए, सॉकेट से जुड़े नेटवर्क इंटरफ़ेस को स्विच करते हैं.

// 1. Unbind: Sockets return to the "default" network interface (e.g., wlan0, eth0)
ASDVGateway_StatusCode_t unbindStatus =
    ASDVGateway_Client_unbindProcessFromSdvRpcNetworkInterface(client, &status);

// 2. Bind: Sockets are now bound to the dedicated SDV-RPC VLAN
ASDVGateway_StatusCode_t bindStatus =
    ASDVGateway_Client_bindProcessToSdvRpcNetworkInterface(client, &status);

// Validation
if (bindStatus == ASDVGateway_StatusCode_OK) {
    // Sockets are successfully bound to the SDV-RPC VLAN
}

RPC सूचनाएं

RPC की उपलब्धता में हुए बदलावों और रूट सर्टिफ़िकेट के अपडेट के बारे में सूचनाएं पाने के लिए, क्लाइंट लिसनर सेट करें. इन अपडेट को RPC सर्वर को स्वीकार करना होगा:

class NativeSdvGatewayTestApp {
public:
    static void ClientNotificationCallback(
        ASDVGateway_ClientNotificationType_t notificationType,
        void* userData
    ) {
        auto* testApp = reinterpret_cast<NativeSdvGatewayTestApp*>(userData);
        if (!testApp) return;

        switch (notificationType) {
            case ASDVGateway_ClientNotificationType_RootCertsChanged:
                std::cout << "onClientNotification: Root Certs Changed" << std::endl;
                // Handle certificate rotation logic here
                break;

            case ASDVGateway_ClientNotificationType_RpcAvailabilityChanged:
                std::cout << "onClientNotification: RPC Availability Changed" << std::endl;
                // Handle reconnection or UI updates here
                break;

            default:
                std::cout << "onClientNotification: Received Unknown Notification ("
                          << notificationType << ")" << std::endl;
                break;
        }
    }
};

// --- Registration ---

NativeSdvGatewayTestApp* thisApp = get_current_app_context();

// Register the notification callback to monitor system-level changes
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_setClientNotificationCallback(
    client,
    NativeSdvGatewayTestApp::ClientNotificationCallback,
    static_cast<void*>(thisApp), // userData
    &status
);

RPC सर्वर फ़्लो

आपको आरपीसी सर्वर बनाने के लिए क्रेडेंशियल का इस्तेमाल करना होगा:

ASDVGateway_RpcCredentials_t* rpcCredentials = nullptr;

// Retrieve the credentials from the client
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_rpcCredentials(client, &rpcCredentials, &status);

// Validation: Differentiating between an error and a deliberate "insecure mode"
if (status.statusCode != ASDVGateway_StatusCode_OK) {
    std::cerr << "Error retrieving credentials: " << status.errorMessage << std::endl;
    return;
}

// Logic for choosing Security Credentials
if (rpcCredentials == nullptr) {
    // If the call succeeded but credentials are null, the system expects insecure communication
    std::cout << "Configuring for Insecure Mode" << std::endl;
} else {
    std::cout << "Configuring for Secure Mode:" << std::endl;
    std::cout << "  Private Key: " << (rpcCredentials->privateKeyPem ? "Present" : "Missing") << std::endl;
    std::cout << "  RootCerts:   " << rpcCredentials->rootCertsPem << std::endl;
    std::cout << "  CertChain:   " << rpcCredentials->certChainPem << std::endl;
    std::cout << "  SAN:         " << rpcCredentials->subjectAlternativeName << std::endl;
}

// --- Cleanup ---

// Release the memory once the RPC server/client is initialized
if (rpcCredentials != nullptr) {
    ASDVGateway_Client_deleteRpcCredentials(client, rpcCredentials);
}

जब आपने RPC सर्वर बना लिया हो और उसका लिसनिंग पोर्ट पता हो, तो RPC सर्वर को रजिस्टर करें, ताकि वह अन्य ऐप्लिकेशन ढूंढ सके:

ASDVGateway_RegisterRpcServerParams_t params{
    .serviceUnitName = "android-sdv-samples-sunroof-sunroof",
    .unitType = ASDVGateway_UnitType_t{
        .sdvPackageName     = "android.sdv.samples.sunroof",
        .serviceBundleName  = "SunroofServer",
        .unitTypeName       = "Sunroof",
    },
    .listeningPort = listeningPort,
    .serverUnitMetadata = ASDVGateway_ServerUnitMetadata_t{
        .version = 1,
    },
};

// Register the RPC server with the SDV Gateway
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_registerRpcServer(
    client,
    &params,
    &status
);

// Basic error handling
if (statusCode != ASDVGateway_StatusCode_OK) {
    std::cerr << "Failed to register RPC server: " << status.errorMessage << std::endl;
}

जब सिस्टम, रन टाइम पर रूट सर्टिफ़िकेट अपडेट करता है (उदाहरण के लिए, वीएम की स्थिति में बदलाव के दौरान), तो आरपीसी सर्वर को स्वीकार किए गए सर्टिफ़िकेट की सूची को रीफ़्रेश करना होगा:

  • रूट सर्टिफ़िकेट अपडेट होने पर सूचना पाने के लिए, ASDVGateway_Client_setClientNotificationCallback का इस्तेमाल करके क्लाइंट इवेंट के लिए लिसनर सेट करें.

  • अपडेट किए गए रूट सर्टिफ़िकेट पाने के लिए, ASDVGateway_Client_rpcCredentials पर कॉल करें.

RPC क्लाइंट फ़्लो

यहां आरपीसी क्लाइंट का फ़्लो दिया गया है:

ASDVGateway_FindRpcServerByNameParams_t params{
    .packageName        = "android.sdv.samples.cluster",
    .serviceBundleName  = "ClusterServer",
    .serviceUnitName    = "android-sdv-samples-cluster-cluster",
};

ASDVGateway_SocketAddress_t socketAddress;
ASDVGateway_RpcCredentials_t* rpcCredentials = nullptr;

// Perform the lookup to find the server's location and security requirements
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_findRpcServerByName(
    mClient,
    &params,
    &socketAddress,
    &rpcCredentials,
    &status
);

// Mandatory check: Distinguish between system errors and intentional "Insecure Mode"
if (status.statusCode != ASDVGateway_StatusCode_OK) {
    std::cerr << "Failed to find RPC server: " << status.errorMessage << std::endl;
    return;
}

// Logic for establishing the RPC channel
if (rpcCredentials == nullptr) {
    std::cout << "Connecting via Insecure Mode to "
              << socketAddress.address << ":" << socketAddress.port << std::endl;
} else {
    std::cout << "Connecting via Secure Mode to "
              << socketAddress.address << ":" << socketAddress.port << std::endl;

    std::cout << "  RootCerts: " << rpcCredentials->rootCertsPem << std::endl;
    std::cout << "  SAN:       " << rpcCredentials->subjectAlternativeName << std::endl;
    // Note: privateKeyPem and certChainPem are typically used if the client
    // needs to perform mutual TLS (mTLS).
}

// Cleanup: Release the credential memory once the RPC channel is established
if (rpcCredentials != nullptr) {
    ASDVGateway_Client_deleteRpcCredentials(client, rpcCredentials);
}

पब्लिशर बनाना और मैसेज पब्लिश करना

ASDVGateway_Client_createPublication API का इस्तेमाल, पब्लिशर की सेवा यूनिट को SDV Gateway के साथ रजिस्टर करने के लिए किया जाता है. इसके लिए, AIDL इंटरफ़ेस का इस्तेमाल किया जाता है. साथ ही, ऐप्लिकेशन प्रोसेस में Fast Message Queue (FMQ) बनाया जाता है. Binder का इस्तेमाल सिर्फ़ FMQ सेट अप करने के लिए किया जाता है. हालांकि, मैसेज लिखते समय इसका इस्तेमाल नहीं किया जाता. इसके बाद, बनाए गए पब्लिकेशन पर मैसेज पब्लिश करने के लिए, ASDVGateway_Client_publishMessages एपीआई का इस्तेमाल किया जाता है. इसके लिए, पब्लिकेशन के एफएमक्यू को लिखना होता है और यह सूचना देनी होती है कि मैसेज लिखे जा चुके हैं.

ASDVGateway_CreatePublicationParams_t params{
    .serviceUnitName = "mirror-position-adjust-impl-1",
    .unitType = ASDVGateway_UnitType_t{
        .sdvPackageName     = "android.sdv.samples.sdv_gateway",
        .serviceBundleName  = "DtPublisher",
        .unitTypeName       = "MirrorPositionAdjust",
    },
    .publisherUnitMetadata = ASDVGateway_PublisherUnitMetadata_t{
        .version          = 1,
        .messageSizeBytes = 64,
        .messageCount     = 16,
    },
};

ASDVGateway_PublicationMetadata_t metadata;

// 1. Create the Publication (allocates resources on the gateway)
ASDVGateway_StatusCode_t createStatus = ASDVGateway_Client_createPublication(
    client,
    &params,
    &metadata,
    &status
);

if (status.statusCode != ASDVGateway_StatusCode_OK) {
    std::cerr << "Failed to create publication: " << status.errorMessage << std::endl;
    return;
}

// 2. Publish Messages
// Note: serializedMessage should contain your encoded protobuf data
std::vector<uint8_t> serializedMessage;

ASDVGateway_Client_publishMessages(
    client,
    serializedMessage.data(),
    serializedMessage.size(),
    metadata.publicationId,
    &status
);

सूचना सुनने की सुविधा के साथ सदस्य बनाना

किसी पब्लिकेशन की सदस्यता लेने और डेटा टनल की सूचनाएं (जैसे कि मैसेज की उपलब्धता) पाने के लिए, ASDVGateway_Client_subscribeToPublicationByName एपीआई का इस्तेमाल करें. यह एपीआई, पब्लिश किए गए मैसेज को पढ़ने के लिए FMQ भी सेट अप करता है. सदस्यता लेने की प्रोसेस के दौरान या बाद में, सूचना के लिए कॉलबैक कॉन्फ़िगर किया जा सकता है.

#include <map>
#include <memory>
#include <iostream>

class NativeSdvGatewayTestApp {
public:
    struct SubscriptionContext {
        int32_t subscriptionId;
        std::string topicName;
        // Add other context-specific data here (e.g., counters, buffers)
    };

    /**
     * Callback triggered when new data is published to a subscribed topic.
     */
    static void SubscriptionNotificationCallback(
        const ASDVGateway_SubscriptionNotificationData_t* notification,
        void* userData
    ) {
        // The SDV Gateway client passes back the userData pointer.
        // We ensure validity by managing the lifecycle of SubscriptionContext
        // within the mSubscriptions map.
        auto* ctx = reinterpret_cast<SubscriptionContext*>(userData);

        if (ctx && notification) {
            // React to data being available for the subscription.
            // Example: handleIncomingData(notification->data, notification->size);
            std::cout << "Notification received for sub ID: " << ctx->subscriptionId << std::endl;
        }
    }

private:
    // Maps subscription handles/IDs to their respective contexts
    std::map<int32_t, std::unique_ptr<SubscriptionContext>> mSubscriptions;
};

// --- Usage ---
NativeSdvGatewayTestApp app;

सदस्यता लेते समय, पब्लिशर की सेवा इकाई के नाम के साथ-साथ अन्य विकल्प भी चुने जा सकते हैं. इन विकल्पों की मदद से, सदस्यता लेने से पहले सबसे हाल ही में पब्लिश किया गया मैसेज वापस पाया जा सकता है. साथ ही, सूचनाओं के बीच कम से कम समय का अंतर तय किया जा सकता है.

ASDVGateway_SubscribeToPublicationByNameParams_t params{
    .sdvVmName           = "vm1",
    .packageName         = "test.package.impl.name",
    .serviceBundleName   = "TestBundleImpl",
    .serviceUnitName     = "GrpcServerImpl",
};

ASDVGateway_Subscriber_Options_t options{
    // Ensure we receive the most recent message immediately upon subscribing
    .flags         = ASDVGATEWAY_SUBSCRIBER_OPTIONS_FLAG_FETCHLASTMESSAGE,
    .minIntervalMs = 0, // No rate limiting; receive updates as they happen
};

// 1. Prepare the context for the callback
auto subCtx = std::make_unique<SubscriptionContext>();
ASDVGateway_PublicationMetadata_t metadata{};

// 2. Perform the subscription
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_subscribeToPublicationByName(
    client,
    &params,
    &options,
    NativeSdvGatewayTestApp::SubscriptionNotificationCallback,
    static_cast<void*>(subCtx.get()), // Pass the raw pointer as userData
    &metadata,
    &status
);

// 3. Validation and Lifecycle Management
if (status.statusCode != ASDVGateway_StatusCode_OK) {
    // Error handling: subCtx will be automatically deleted here
    std::cerr << "Subscription failed: " << status.errorMessage << std::endl;
    return;
}

// Store the context using the publicationId as the key.
// Once moved, the map owns the lifetime of subCtx.
app.mSubscriptions.emplace(metadata.publicationId, std::move(subCtx));

पब्लिकेशन से मैसेज पढ़ने के लिए, ASDVGateway_Client_readAvailableMessages एपीआई का इस्तेमाल करें:

uint32_t messagesAvailable = 0;

// 1. Check how many messages are waiting in the queue
ASDVGateway_StatusCode_t availStatus = ASDVGateway_Client_availableToRead(
    client,
    metadata.publicationId,
    &messagesAvailable,
    &status
);

if (status.statusCode != ASDVGateway_StatusCode_OK) {
    // Error handling for availability check
    return;
}

if (messagesAvailable == 0) {
    // No messages available for this publication at this time
    return;
}

// 2. Prepare the buffer
// metadata.messageSizeBytes was provided during the publication/subscription setup
std::vector<uint8_t> bytesForMessages;
bytesForMessages.resize(metadata.messageSizeBytes * messagesAvailable);

// 3. Read the messages from the Gateway into your local buffer
uint32_t actualMessageCount = 0; // Filled by the SDK with the number of messages read
ASDVGateway_StatusCode_t readStatus = ASDVGateway_Client_readAvailableMessages(
    client,
    metadata.publicationId,
    bytesForMessages.data(),
    bytesForMessages.size(),
    &actualMessageCount,
    &status
);

if (status.statusCode == ASDVGateway_StatusCode_OK) {
    // Successfully read 'actualMessageCount' messages
    // Process bytesForMessages...
}

सदस्यता लेने के बाद कॉलबैक सेट करने के लिए, ASDVGateway_Client_setNotificationCallbackForPublicationId एपीआई का इस्तेमाल करें:

ASDVGateway_StatusCode_t ASDVGateway_Client_setNotificationCallbackForPublicationId(
    // [in]  Opaque pointer to the client object.
    const ASDVGateway_Client* client,

    // [in]  Identifies the specific publication to monitor.
    const int32_t publicationId,

    // [in]  Function pointer triggered when new data is available
    //       for the specified publication.
    ASDVGateway_SubscriptionNotificationCallback notificationCallback,

    // [in]  Optional user-defined context passed back to the callback.
    void* notificationCallbackUserData,

    // [out] Optional pointer to a status structure for result codes
    //       and error messages.
    ASDVGateway_Status_t* outStatus
);

init सेवा सेट अप करना

सबसे पहले, नेटिव सेवाओं के लिए पहचान में दिया गया तरीका अपनाकर, सेवा को Android आईडी (एआईडी) के ज़रिए सेवा की पहचान असाइन करें.

मान लें कि आपकी सेवा का नाम native_sdv_gateway_client_service है, एक्ज़ीक्यूटेबल /vendor पार्टीशन पर /vendor/bin/native_sdv_gateway_client_service पर मौजूद है, और सेवा को चलाने के लिए vendor_gateway_client का इस्तेमाल Android आईडी (एआईडी) के तौर पर किया जाता है. इस सेटअप की मदद से, यहां दी गई init service को तय किया जा सकता है:

service native_sdv_gateway_client_service /vendor/bin/native_sdv_gateway_client_service
    class core
    user vendor_gateway_client
    group inet
    disabled
    oneshot

इस कॉन्फ़िगरेशन में इन पैरामीटर का इस्तेमाल किया जाता है:

  • vendor_gateway_client, सेवा के लिए बनाया गया या चुना गया एआईडी है.
  • एसडीवी गेटवे क्लाइंट को group inet की ज़रूरत होती है, ताकि वे एसडीवी वीएम के बीच कम्यूनिकेट करने के लिए नेटवर्क इंटरफ़ेस का इस्तेमाल कर सकें. ज़रूरत पड़ने पर, अन्य ग्रुप जोड़े जा सकते हैं.
  • इस उदाहरण में disabled और oneshot का इस्तेमाल किया गया है. आपको अपनी सेवा के लिए, सेवा के विकल्पों में बदलाव करना पड़ सकता है. sdv_gateway के बाद सेवा शुरू करें.

सेवा के लिए SELinux के नियम बनाना

SDV Gateway API का इस्तेमाल करने के लिए, आपको सेवा के लिए SELinux के इन नियमों की ज़रूरत होगी:

# Define the domain for the service
type native_sdv_gateway_client_service, domain;

# Define the executable file type on the vendor partition
type native_sdv_gateway_client_service_exec, exec_type, file_type, vendor_file_type;

# Macro to transition from 'init' to this service's domain upon execution
init_daemon_domain(native_sdv_gateway_client_service)

# Macro to grant the necessary permissions to communicate with the SDV Gateway
sdv_gateway_client_domain(native_sdv_gateway_client_service)

SELinux के नियमों में:

  • init_daemon_domain, init से सेवा शुरू करने की अनुमति देता है.

  • sdv_gateway_client_domain SDV Gateway के साथ इंटरैक्ट करने के लिए, सभी ज़रूरी SELinux अनुमतियां देता है. नीचे दी गई लाइन, इन नियमों को एक्ज़ीक्यूटेबल के लिए लागू करती है:

    /vendor/bin/native_sdv_gateway_client_service    u:object_r:native_sdv_gateway_client_service_exec:s0
    

कोड सैंपल

सीएपीआई के बारे में बताने वाले नेटिव कोड के सैंपल को चलाने के बारे में ज़्यादा जानने के लिए, system/software_defined_vehicle/samples/sdv_gateway/NativeSdvGatewayTestApp/README.md देखें.

IVI Java ऐप्लिकेशन पर SDV गेटवे

इस डायग्राम में, IVI पर SDV गेटवे के इंटरैक्शन मॉडल के बारे में बताया गया है:

IVI Java ऐप्लिकेशन पर SDV गेटवे

चौथी इमेज. IVI Java ऐप्लिकेशन पर SDV गेटवे.

खास तौर पर, मॉडल:

  • यह सभी कॉल को JNI लेयर पर भेजता है.
  • यह Java और C API को जोड़ता है.
  • ISdvGateway के साथ एआईडीएल इंटरैक्शन तय करता है:
    • Init comms
    • आरपीसी सर्वर ढूंढना या बनाना
    • Pub/Sub बनाएं
    • डिवाइस और सेवाओं की अपने-आप पहचान करने के लिए डेटा टनल इंटरैक्शन करना
    • सूचनाएं पाना (उदाहरण के लिए, डेटा उपलब्ध है)
    • यह कुकी, मैसेज पढ़ती और लिखती है. इसका इस्तेमाल, Pub/Sub के लिए FMQ के तौर पर किया जाता है. इसका इस्तेमाल, मुख्य जोड़ी बनाने और टीएलएस के लिए सर्टिफ़िकेट बनाने के लिए किया जाता है

गेटवे की क्लाइंट लाइब्रेरी शामिल करना

libsdvgatewayclient C API के लिए Java लाइब्रेरी और JNI रैपर, IVI टारगेट पर APEX में इंस्टॉल किए जाते हैं. Java लाइब्रेरी स्टब, रन टाइम में इस्तेमाल की जाने वाली Java लाइब्रेरी, और Java लाइब्रेरी वाले ज़रूरी APEX में कंपाइल टाइम डिपेंडेंसी जोड़ें.

android_app {
    name: "YourAppName",
    // ...
    static_libs: [
        "libsdvgatewayclient-java",
    ],
    libs: [
        "libsdvgatewayclient-java-sdk.stubs",
    ],
    uses_libs: [
        "libsdvgatewayclient-java-sdk",
    ],
    required: [
        "com.sdv.google.gateway.client",
    ],
    // ...
}

एसडीवी ट्री के बाहर बनाए गए अनबंडल्ड ऐप्लिकेशन के मामले में, प्रोसेस एक जैसी होती है:

  1. जनरेट किए गए Java लाइब्रेरी स्टब JAR और RPC के लिए काम करने वाले JAR को ऐप्लिकेशन की लाइब्रेरी फ़ोल्डर में कॉपी करें. ज़्यादा जानकारी के लिए, system/software_defined_vehicle/sdv_gateway/libsdvgatewayclient_apex/README.md देखें.

  2. स्टब JAR को सिर्फ़ कंपाइल करने के लिए डिपेंडेंसी के तौर पर जोड़ें. उदाहरण के लिए, स्टब पर निर्भर रहने के लिए Gradle कॉन्फ़िगरेशन अपडेट करें. इसके लिए, dependencies सेक्शन में compileOnly एंट्री जोड़ें:

    dependencies {
        // The library supporting functions for SDK RPC.
        // Statically linked into the app APK.
        implementation(files("libs/libsdvgatewayclient-java.jar"))
    
        // Stub of the SDV-Gateway client library.
        // Used only for compilation; the real implementation is provided 
        // by the com.sdv.google.gateway.client APEX at runtime.
        compileOnly(files("libs/libsdvgatewayclient-java-sdk.jar"))
    }
    
  3. Java लाइब्रेरी को AndroidManifest.xml फ़ाइल के ऐप्लिकेशन सेक्शन में जोड़ें.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
        <application>
    
            <!--
              Declares that this app requires the SDV Gateway client library.
              The 'android:required="true"' attribute ensures the app won't
              install/run if the library is missing from the system.
            -->
            <uses-library
                android:name="libsdvgatewayclient-java-sdk"
                android:required="true" />
    
        </application>
    
    </manifest>
    

लाइब्रेरी लोड करना

क्लाइंट लाइब्रेरी से मिला SdvGatewayClient ऑब्जेक्ट बनाएं:

import google.sdv.gateway.client.SdvGatewayClient;

// --- Inside your Activity, Service, or ViewModel ---

// Initialize the SDV Gateway Client
SdvGatewayClient gatewayClient = new SdvGatewayClient();

Init comms

सेवा के बंडल के नाम के तौर पर ऐप्लिकेशन का नाम इस्तेमाल करके, initComms() को कॉल करें:

// Define a unique name for your service bundle (usually constant)
private static final String SERVICE_BUNDLE_NAME = "MySdvServiceBundle";

// ... inside an Activity or Service context ...

try {
    // Initialize communications with the SDV Gateway
    // context.getPackageName() provides "android.sdv.samples.gateway.client" or similar
    gatewayClient.initComms(context.getPackageName(), SERVICE_BUNDLE_NAME);

    Log.i("SDV_GATEWAY", "Communications initialized successfully");
} catch (Exception e) {
    // Unlike the C API which uses status codes,
    // the Java SDK often throws exceptions for initialization failures.
    Log.e("SDV_GATEWAY", "Failed to initialize communications", e);
}

डिवाइस और सेवाओं की अपने-आप पहचान करना

Java API में ये तरीके शामिल हैं:

  • जब किसी खास तरह की यूनिट वाली सेवा इकाइयों को रजिस्टर या अनरजिस्टर किया जा रहा हो, तब सूचना पाएं. इसके अलावा, किसी खास नाम से मेल खाने वाली सेवा इकाइयों के लिए भी सूचनाएं पाएं.

  • किसी खास यूनिट टाइप वाली मौजूदा सेवा इकाइयों की सूची बनाएं. इसके अलावा, किसी खास सेवा इकाई टाइप के नाम से भी मिलान किया जा सकता है. सेवा यूनिट में हुए बदलावों की सूचना पाने के लिए, एक लिसनर बनाएं:

import google.sdv.gateway.client.ServiceUnitChangeListener;
import google.sdv.gateway.client.ServiceUnitChangeEventType;
import google.sdv.gateway.client.ServiceUnitDefinition;

// --- Implementation ---

ServiceUnitChangeListener listener = new ServiceUnitChangeListener() {
    @Override
    public void onServiceUnitChanged(
        ServiceUnitChangeEventType eventType,
        ServiceUnitDefinition serviceUnitDefinition
    ) {
        // This is triggered when services matching your criteria
        // are registered or unregistered on the vehicle network.
        if (eventType == ServiceUnitChangeEventType.REGISTERED) {
            // Handle new service discovery
        } else if (eventType == ServiceUnitChangeEventType.UNREGISTERED) {
            // Handle service removal
        }
    }
};

addListenerForServiceUnitChangeByName Java मेथड, लिसनर को सूचना देता है:

// 1. Register the listener for a specific service unit by name
AutoCloseable handle = gatewayClient.addListenerForServiceUnitChangeByName(
    new UnitNameDiscoveryArgs(
        "",                                   // sdvVmName (empty for local/auto-discovery)
        "android.sdv.samples.cluster",        // sdvPackageName
        "ClusterServer",                      // serviceBundleName
        "android-sdv-samples-cluster-cluster" // serviceUnitName
    ),
    listener
);

// --- Later in the application lifecycle ---

// 2. To stop receiving notifications and clean up resources, close the handle.
try {
    if (handle != null) {
        handle.close();
    }
} catch (Exception e) {
    Log.e("SDV_GATEWAY", "Error while closing the listener handle", e);
}

इसके अलावा, addListenerForServiceUnitChangeByType Java तरीके का इस्तेमाल करके, उस लिसनर को सूचना दें जब तय यूनिट टाइप वाली सेवाएं रजिस्टर या अनरजिस्टर की गई हों:

// 1. Register a listener based on the Service Unit Type
AutoCloseable handle = gatewayClient.addListenerForServiceUnitChangeByType(
    new UnitType(
        "com.android.testapp.sdvcarmonitor", // sdvPackageName
        "SunroofRpcServer",                  // serviceBundleName
        "Sunroof"                            // unitTypeName
    ),
    listener
);

// --- Execution Loop / Lifecycle ---

// 2. To stop receiving notifications and clean up memory, close the handle.
// This effectively unregisters the listener from the SDV Gateway.
try {
    if (handle != null) {
        handle.close();
    }
} catch (Exception e) {
    Log.e("SDV_GATEWAY", "Failed to close the service unit listener handle", e);
}

addListenerForServiceUnitChangeByName और addListenerForServiceUnitChangeByType के लिए, जोड़े जाने के बाद श्रोता को रजिस्टर की गई सभी सेवा इकाइयों के बारे में सूचना दी जाती है. नाम या टाइप के हिसाब से, सिर्फ़ रजिस्टर की गई सेवा यूनिट पाने के लिए, listServiceUnitsByName और listServiceUnitsByType Java API का इस्तेमाल करें:

import google.sdv.gateway.client.ServiceUnitDefinition;
import google.sdv.gateway.client.UnitNameDiscoveryArgs;
import google.sdv.gateway.client.UnitType;

// --- 1. Synchronous Lookup by Specific Name ---
ServiceUnitDefinition[] definitionsByName = gatewayClient.listServiceUnitsByName(
    new UnitNameDiscoveryArgs(
        "",                                   // sdvVmName
        "android.sdv.samples.cluster",        // sdvPackageName
        "ClusterServer",                      // serviceBundleName
        "android-sdv-samples-cluster-cluster" // serviceUnitName
    )
);

// --- 2. Synchronous Lookup by Service Type ---
ServiceUnitDefinition[] definitionsByType = gatewayClient.listServiceUnitsByType(
    new UnitType(
        "com.android.testapp.sdvcarmonitor", // sdvPackageName
        "SunroofRpcServer",                  // serviceBundleName
        "Sunroof"                            // unitTypeName
    )
);

// Example processing
if (definitionsByType.length > 0) {
    ServiceUnitDefinition firstSunroof = definitionsByType[0];
    // Proceed to connect...
}

RPC सर्वर फ़्लो

IVI सिस्टम में SDV Gateway क्लाइंट, SDV सेवाओं के साथ कम्यूनिकेट करने के लिए Google रिमोट प्रोसीजर कॉल (gRPC) का इस्तेमाल करते हैं. ये इंटरैक्शन, VSIDL कैटलॉग से प्रोटो डेफिनिशन पर निर्भर करते हैं. ये प्रोटो डेफिनिशन, SDV Core पर इस्तेमाल किए गए प्रोटो डेफिनिशन के जैसे या उनसे मिलते-जुलते होते हैं. Java ऐप्लिकेशन के लिए, gRPC-Java को लागू करने का विकल्प चुना गया है. ऐप्लिकेशन सर्वर के लिए, सर्वर प्रोटो की परिभाषा का एक सैंपल, sunroof.proto दिया गया है.

service Sunroof {
    /**
     * Retrieves the current state of the sunroof (e.g., position, tilt, status).
     *
     * @param .google.protobuf.Empty - No input parameters required.
     * @return SunroofStateResponse - The current telemetry data for the sunroof.
     */
    rpc GetSunroofState(.google.protobuf.Empty) returns (SunroofStateResponse) {}
}

मिलती-जुलती प्रोटो लाइब्रेरी से लिंक करें और सेवा को तय करें:

import com.android.sdv.sdvgrpclibrary.SunroofGrpc;
import com.android.sdv.sdvgrpclibrary.SunroofStateResponse; // Assuming this is the generated class
import com.google.protobuf.Empty;
import io.grpc.stub.StreamObserver;

/**
 * Implementation of the Sunroof gRPC service.
 * This class handles the logic for the RPCs defined in your .proto file.
 */
static class SunroofGrpcImpl extends SunroofGrpc.SunroofImplBase {

    @Override
    public void getSunroofState(Empty request, StreamObserver<SunroofStateResponse> responseObserver) {
        // 1. Fetch current sunroof data (e.g., from a Hardware Abstraction Layer)
        int currentPosition = 50; // Example value: 50% open

        // 2. Build the Protobuf response message
        SunroofStateResponse response = SunroofStateResponse.newBuilder()
                .setPercentageOpen(currentPosition)
                .build();

        // 3. Send the response to the client using the observer
        responseObserver.onNext(response);

        // 4. Close the stream to signal that the RPC is finished
        responseObserver.onCompleted();
    }
}

gRPC सर्वर को सुरक्षित और असुरक्षित चैनल क्रेडेंशियल के साथ रजिस्टर करें:

// 1. Define the type signature for the service
UnitType unitType = new UnitType(
    "com.android.testapp.sdvcarmonitor", // sdvPackageName
    "SunroofRpcServer",                  // serviceBundleName
    "Sunroof"                            // typeName (Unit Type)
);

// 2. Register the RPC server with the Gateway
// The gateway creates a mapping between the ServiceUnitName and your implementation.
server = gatewayClient.registerRpcServer(
    "SunroofRpcServerImpl-1",                       // serviceUnitName (Unique instance name)
    unitType,                                       // unitType defined earlier
    "SUNROOF_GRPC_SERVER_VALUE_HOLDER".getBytes(),  // appMetadataValueHolder (Static discovery data)
    1,                                              // appMetadataVersion
    new SunroofGrpcImpl()                           // The actual gRPC service implementation
);

क्लाइंट लाइब्रेरी, अंदरूनी तौर पर gRPC सर्वर ऑब्जेक्ट, SdvGatewayClient.java बनाती है. साथ ही, रूट सर्टिफ़िकेट के अपडेट भी मैनेज करती है:

// 1. Initialize credentials (Insecure for dev, TLS for production)
ServerCredentials serverCredentials = InsecureServerCredentials.create();

// 2. Build and start the OkHttp-based gRPC server
final int bindAnyPort = 0;
final Server server = OkHttpServerBuilder
    .forPort(bindAnyPort, serverCredentials)
    .addService(gRpcServerImplementation) // Your SunroofGrpcImpl
    .build()
    .start();

// The assigned port can now be retrieved using server.getPort()
int actualPort = server.getPort();

// 3. Prepare the JNI data structure
// This object mirrors the ASDVGateway_ServiceUnitDefinition_t C struct
JniServiceUnitDefinition definition = new JniServiceUnitDefinition();

// Fill the RPC service definition params (Port, Name, Type, etc.)
definition.setPort(actualPort);
definition.setServiceUnitName("SunroofRpcServerImpl-1");

// 4. Perform the cross-language call
// This jumps from Java -> JNI -> ASDVGateway_Client_registerRpcServer (C API)
mJniClient.nativeRegisterRpcServer(definition);

RPC क्लाइंट फ़्लो

इस कोड सैंपल में, उस सर्वर के लिए सर्वर प्रोटो डेफ़िनिशन (tpms.proto) दी गई है जिससे ऐप्लिकेशन, क्लाइंट के तौर पर कनेक्ट होता है:

/**
 * The TPMS service provides real-time pressure and temperature
 * data for all tires on the vehicle.
 */
service Tpms {
    /**
     * Returns the full state of all monitored tires.
     */
    rpc GetTpmsState(.google.protobuf.Empty) returns (TpmsStateResponse) {}

    /**
     * A filtered query that returns only the tires
     * below the recommended pressure threshold.
     */
    rpc GetLowTires(.google.protobuf.Empty) returns (LowTiresResponse) {}
}

संबंधित प्रोटो लाइब्रेरी के ख़िलाफ़ लिंक करें:

import com.android.sdv.sdvgrpclibrary.TpmsGrpc;
import io.grpc.ManagedChannel;

// --- Inside your Client Application ---

// 1. Request a ManagedChannel from the Gateway for a specific service unit
ManagedChannel managedChannel = gatewayClient.connectToRpcServerByName(
    "",                                     // sdvVmName (empty for local/auto-lookup)
    "android.sdv.samples.cluster",          // packageName
    "ClusterServer",                        // serviceBundleName
    "android-sdv-samples-cluster-cluster"  // serviceUnitName
);

// 2. Use the channel to create a gRPC stub (e.g., for the TPMS service)
TpmsGrpc.TpmsBlockingStub tpmsStub = TpmsGrpc.newBlockingStub(managedChannel);

// 3. Now you can call RPC methods directly
// TpmsStateResponse response = tpmsStub.getTpmsState(Empty.getDefaultInstance());

RPC सर्वर ढूंढने के लिए, इंटरनल तौर पर ASDVGateway_Client_findRpcServerByName एपीआई को कॉल किया जाता है. अगर आरपीसी सर्वर मिल जाता है, तो मैनेज किया गया चैनल, असुरक्षित मोड में बनाया जाता है या टीएलएस कॉन्फ़िगरेशन का इस्तेमाल करने के लिए तय किया जाता है. यह आरपीसी सर्वर फ़्लो की तरह ही होता है. यह सर्विस डिस्कवरी कॉन्फ़िगरेशन पर निर्भर करता है. ऐप्लिकेशन, ManagedChannel ऑब्जेक्ट का इस्तेमाल करके स्टब बनाता है और सर्वर के इन तरीकों को कॉल करता है:

import com.android.sdv.sdvgrpclibrary.TpmsGrpc;
import com.android.sdv.sdvgrpclibrary.TpmsStateResponse;
import com.google.protobuf.Empty;
import io.grpc.stub.MetadataUtils;

// 1. Create a "Blocking Stub" from the existing ManagedChannel.
// We apply an interceptor to attach mandatory metadata (headers)
// required by the SDV Gateway for authorization.
TpmsGrpc.TpmsBlockingStub tpmsStub = TpmsGrpc.newBlockingStub(managedChannel)
    .withInterceptors(MetadataUtils.newAttachHeadersInterceptor(mMetadata));

// 2. Execute the RPC call.
// Because this is a "BlockingStub," the thread will wait here until
// the vehicle service responds or times out.
TpmsStateResponse tpmsStateResponse = tpmsStub.getTpmsState(Empty.getDefaultInstance());

// 3. Extract the domain-specific state object from the Protobuf response.
// 'newState' can now be used to update your UI or application logic.
newState = tpmsStateResponse.getTpmsState();

प्रोसेस के डिफ़ॉल्ट नेटवर्क को स्विच करना

इंटरनेट से जुड़े कनेक्शन के लिए सॉकेट खोलने के लिए, आपको प्रोसेस के डिफ़ॉल्ट नेटवर्क के तौर पर SDV-RPC VLAN से स्विच करना पड़ सकता है. प्रोसेस को SDV-RPC VLAN से अनबाइंड और रीबाइंड करने के लिए, unbindProcessFromSdvRpcNetworkInterface और bindProcessToSdvRpcNetworkInterface को कॉल करें. ये दोनों कॉल, ग्लोबल स्विच की तरह काम करते हैं. ये प्रोसेस के सभी थ्रेड के लिए, सॉकेट से जुड़े नेटवर्क इंटरफ़ेस को स्विच करते हैं.

// 1. Redirect all socket traffic from this process to the SDV-RPC VLAN.
// This call is required for making RPC calls or hosting RPC services for SDV.
gatewayClient.bindProcessToSdvRpcNetworkInterface();

// --- Process is now communicating over the SDV-RPC interface ---

// 2. Revert the process network binding back to the "default" interface.
// This allows the app to access internet resources again.
gatewayClient.unbindProcessFromSdvRpcNetworkInterface();

पब्लिशर बनाना और मैसेज पब्लिश करना

// 1. Define the interface and type for the publication
UnitType unitType = new UnitType(
    "android.sdv.samples.tires.interface", // sdvPackageName
    "TirePressurePublisherInterface",      // serviceBundleName
    "TirePressure"                         // typeName
);

// 2. Configure the Publisher's buffer and message constraints
PublisherUnitMetadata publisherUnitMetadata = new PublisherUnitMetadata(
    1,   // version
    64,  // message size in bytes (fixed size for performance)
    128  // max message count (buffer depth)
);

// 3. Create the Publication instance through the Gateway
Publisher tirePublisher = gatewayClient.createPublication(
    "tire-pressure-service-unit-name",
    unitType,
    publisherUnitMetadata
);

// 4. Prepare and publish a message
// In a real app, you would encode your sensor data into this byte array
byte[] msg = new byte[publisherUnitMetadata.messageSizeBytes];

// ... fill msg with data ...

tirePublisher.publish(msg);

आंतरिक तौर पर, Java createPublication और publish API, नेटिव ASDVGateway_Client_createPublication और ASDVGateway_Client_readAvailableMessages API पर निर्भर करते हैं. सी एपीआई के बारे में ज़्यादा जानकारी के लिए, IVI की नेटिव सेवा पर SDV Gateway का इस्तेमाल करना लेख पढ़ें. Publisher ऑब्जेक्ट, मैसेज लिखने और पब्लिकेशन के लाइफ़साइकल को मैनेज करने के लिए कॉन्टेक्स्ट उपलब्ध कराता है.

सूचना सुनने की सुविधा के साथ सदस्य बनाना

Java API, लिसनर को पब्लिकेशन की सदस्यता लेने के तरीके के पैरामीटर के तौर पर पास करने की अनुमति देता है. साथ ही, यह Subscription ऑब्जेक्ट दिखाता है.

  • Listener को सूचना मिलती है, जब सदस्यता लिए गए पब्लिकेशन के लिए डेटा उपलब्ध होता है.

  • Subscription एक कॉन्टेक्स्ट ऑब्जेक्ट के तौर पर काम करता है. इसका इस्तेमाल मैसेज पढ़ने और सदस्यता बंद करने के लिए किया जा सकता है.

// 1. Define the Listener to handle incoming data notifications
SubscriptionNotificationListener listener = new SubscriptionNotificationListener() {
    @Override
    public void onSubscriptionNotification(
        Subscription subscription,
        SubscriptionNotificationType notificationType
    ) {
        // Only process if the notification indicates new data is ready
        if (notificationType != SubscriptionNotificationType.DataAvailable) {
            return;
        }

        // Read the message from the subscription buffer
        byte[] content = subscription.readMessage();

        // Note: This callback often runs on a background thread provided by the SDK.
        // If you need to update the UI, use a Handler or View.post().
        processTireData(content);
    }
};

// 2. Subscribe to the publication by its unique name
Subscription tireSubscription = gatewayClient.subscribeToPublicationByName(
    "",                                  // sdvVmName (empty for auto-lookup)
    "android.sdv.samples.dt_publisher",  // packageName
    "SdvGatewayDtPublisher",             // serviceBundleName
    "tire",                              // serviceUnitName
    listener
);

// 3. Read Messages
// You can poll or register callbacks for new messages.
// When polling the message, call this method in a loop.
// When registering callbacks, call this method to get the message payload.
byte[] manualContent = tireSubscription.readMessage();

कोड सैंपल

लिसनर कॉलबैक, क्लाइंट की ओर से मैनेज की जाने वाली एक थ्रेड पर शुरू किए जाते हैं. इससे लिसनर के अंदर प्रोसेसिंग को कम किया जा सकता है, ताकि अन्य सदस्यताओं के लिए सूचनाएं मिलने में देरी न हो. Java लेयर, सदस्यता मैनेज करने, सूचनाएं हैंडल करने, और मैसेज वापस पाने के लिए C API का इस्तेमाल करती है. एपीआई के इस्तेमाल का डेमो देखने के लिए, system/software_defined_vehicle/samples/sdv_gateway/README.md पर बाइनरी फ़ाइल में दिया गया Java ऐप्लिकेशन का सैंपल देखें.

ज़रूरी अनुमतियां

SDV Gateway client API को कॉल करने के लिए, किसी खास अनुमति की ज़रूरत नहीं होती. हालांकि, आपको अपने ऐप्लिकेशन के लिए सही SELinux के नियम लागू करने होंगे.

  • डेटा टनल की सदस्यता और पब्लिकेशन के लिए, आपको किसी अनुमति की ज़रूरत नहीं होती.
  • एसडीवी आरपीसी के लिए, आपके पास ये अनुमतियां होनी चाहिए:
    • android.permission.INTERNET
    • android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS

android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS के लिए, आपको अपने ऐप्लिकेशन के साथ-साथ उसी पार्टीशन में etc/permissions के तहत अनुमति वाली फ़ाइल की भी ज़रूरत होती है. उदाहरण के लिए, SdvCarMonitorTestApp (पैकेज का नाम com.android.testapp.sdvcarmonitor) के लिए, फ़ाइल इस तरह दिखती है:

<?xml version="1.0" encoding="utf-8"?>

<permissions>
    <privapp-permissions package="com.android.testapp.sdvcarmonitor">
        <permission name="android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS"/>
    </privapp-permissions>
</permissions>

SELinux के नियम

एसडीवी गेटवे एपीआई का इस्तेमाल करने के लिए, Java ऐप्लिकेशन को वही अनुमतियां चाहिए जो नेटिव सेवाओं के लिए ज़रूरी होती हैं. sdv_gateway_client_domain() SELinux मैक्रो का इस्तेमाल करके, ये अनुमतियां दें:

sdv_gateway_client_domain(my_oem_sdv_gateway_client_app)

OEM, SDV Gateway का इस्तेमाल करने की अनुमति वाले Java ऐप्लिकेशन के लिए my_oem_sdv_gateway_client_app डोमेन तय करता है. SDV Gateway का इस्तेमाल सिर्फ़ सिस्टम और खास अधिकारों वाले ऐप्लिकेशन से करें.

कोड की जगहें

SDV Gateway का सोर्स कोड system/software_defined_vehicle/sdv_gateway/ पर पाएं. SDV Gateway के सैंपल इन प्लैटफ़ॉर्म के लिए उपलब्ध हैं:

  • क्लाइंट C का एपीआई: system/software_defined_vehicle/samples/sdv_gateway/NativeSdvGatewayTestApp/
  • क्लाइंट Java API: system/software_defined_vehicle/samples/sdv_gateway/SdvCarMonitorTestApp/