«دروازه خودرو نرمافزارمحور» (SDV) در سیستمهای «سرگرمی-اطلاعاتی درونخودرو» (IVI) ارتباط بین سیستمهای IVI و سرویسهای SDV ازراهدور را تسهیل میکند. این درگاه به برنامههای Java و سرویسهای بومی سازنده تجهیزات اصلی (OEM)، مانند لایه انتزاعی سختافزار خودرو (VHAL)، اجازه میدهد با سرویسهای «خودرو نرمافزارمحور» (SDV) تعامل داشته باشند. دروازه از روشهای ارتباطی تثبیتشده، ازجمله ثبت سرویس، کشف، و فراخوانیهای رویه ازراهدور (RPC) استفاده میکند.
این درگاه الزامات خاص پروژه «خودروی نرمافزارمحور» (SDV) در «سیستمعامل Android Automotive» (AAOS) را برآورده میکند، مانند فعال کردن پیادهسازی مرجع VHAL بااستفاده از «تونل داده خودروی نرمافزارمحور» برای اطلاعات ویژگی. همچنین به برنامههای Android مبتنی بر Java و Kotlin در سیستم سرگرمی-اطلاعاتی درونخودرو اجازه میدهد از پشته ارتباطات خودروی نرمافزارمحور استفاده کنند، بهعنوان سرویس ثبت شوند، سرویسهای دیگر خودروی نرمافزارمحور را پیدا کنند، و با آنها ارتباط برقرار کنند.
برای مکانهای کد «دروازه خودروی نرمافزارمحور» و نمونههای مشتریان «دروازه خودروی نرمافزارمحور»، به مکانهای کد مراجعه کنید.
مدلهای ادغام درگاه SDV
این بخش معماری «دروازه خودروی نرمافزارمحور» را در «سیستم سرگرمی-اطلاعاتی درونخودرو» شرح میدهد و نشان میدهد که چگونه سرویسهای بومی C یا C++ با کتابخانه کلاینت دروازه ادغام میشوند.
استفاده از SDV-IVI Comms ازطریق SDV Gateway در معماری IVI
«درگاه خودروی نرمافزارمحور» با برنامههای Java، سرویسهای بومی، پشته ارتباطات خودروی نرمافزارمحور، و شبکه خودرو تعامل دارد. شکل ۱ این تعاملها را نشان میدهد:
شکل ۱. تعاملات درگاه SDV.
در این نمودار سیستم:
- برنامهها ازطریق سرویسهای کارخواه با «دروازه خودروی نرمافزارمحور» تعامل برقرار میکنند.
- کیت توسعه نرمافزار خودروی نرمافزارمحور سیستم عامل Android Automotive موارد زیر را دراختیارتان قرار میدهد:
- ISdvGateway AIDL API for interprocess communication.
- کتابخانههای ارتباطی برای تعامل شبکه.
- میانای برنامهسازی کاربردی Convenience C برای ادغام سرویس بومی.
- میانای برنامهسازی کاربردی ISdvGateway AIDL توسط سرویس و زیرسیستم SDV Gateway پیادهسازی میشود.
- سرویس «دروازه خودروی نرمافزارمحور» موارد زیر را مدیریت میکند:
- کشف سرویس دوطرفه.
- ارتباط با سرویسهای SDV از راه دور.
- منطق کسبوکار اصلی.
- زیرسیستم «دروازه خودروی نرمافزارمحور» به شبکه خودرو متصل میشود.
- سرویسهای بومی، ازجمله پیادهسازی VHAL، میتوانند از ISdvGateway AIDL API مستقیماً یا ازطریق C API کیت توسعه نرمافزار استفاده کنند.
- پراکسی VHAL بهعنوان یک پیادهسازی مرجع VHAL عمل میکند و ادغام نگاشت VSIDL را دربرمیگیرد.
مدل ادغام برای «دروازه خودروی نرمافزارمحور» در سرویس بومی «سیستم سرگرمی-اطلاعاتی درونخودرو»
مدل ادغام در شکل ۲ نشان داده شده است:
شکل ۲. مدل ادغام درگاه SDV.
استفاده از «دروازه خودروی نرمافزارمحور» در سرویس بومی «سیستم سرگرمی-اطلاعاتی درونخودرو»
شکل ۳ استفاده از «دروازه خودروی نرمافزارمحور» را در «سیستم سرگرمی-اطلاعاتی درونخودرو» نشان میدهد:
شکل ۳. SDV Gateway در IVI.
پیششرطها
منبع رشته Binder را شروع کنید:
کتابخانه کارخواه «دروازه SDV» برای دریافت فراخوانهای برگشتی ناهمگام از سرویسهای Binder به استخر رشته Binder شروعشده نیاز دارد.
اگر استخر رشته Binder شروع نشده باشد، میانای برنامهسازی کاربردی موردنیاز برای ایجاد کارخواه «دروازه خودروی نرمافزارمحور» ناموفق خواهد بود.
گنجاندن کتابخانه کارخواه Gateway بومی
کتابخانه Native Gateway Client یک میانای برنامهسازی کاربردی C را نمایش میدهد. برای استفاده از C API، نمونهای از
libsdvgatewayclient را بهعنوان وابستگی اضافه کنید:
cc_binary {
name: "your_binary_name",
srcs: ["main.cpp"],
shared_libs: [
"libsdvgatewayclient",
],
}
بار کردن کارخواه Gateway بومی
#include "libsdvgatewayclient.h"
ایجاد نمونه مشتری بومی
ASDVGateway_Client* client;
ASDVGateway_StatusCode_t statusCode = ASDVGateway_Client_new(
&client, /*outStatus*/nullptr);
پساز ایجاد کارخواه، کارخواه:
وضعیت را برای همه تعاملات بعدی با سرویس «دروازه» نگه میدارد.
بهعنوان بافت همه تعاملها با دیگر سرویسهای فعالشده با SDV عمل میکند و بهعنوان اولین پارامترها به توابع C API ارسال میشود.
کدهای وضعیت و پیامهای خطا
اکثر توابع C API این تعریف را دارند:
ASDVGateway_StatusCode_t ApiFunctionName(..., ASDVGateway_Status_t* outStatus);
برای ارزیابی موفقیت، میتوانید رمز وضعیت برگرداندهشده را که از نوع ASDVGateway_StatusCode_t است بررسی کنید. همچنین میتوانید اشارهگری را به ساختاری منتقل کنید
که در آن تابع میتواند کد وضعیت و پیام خطا را پر کند. اشارهگر بهعنوان آخرین پارامتر با نام outStatus ارسال میشود. مقدار تهی یعنی از ساختار خروجی استفاده نمیشود.
فراخواننده باید ساختار وضعیت حافظه را برای پیام خطا اختصاص دهد. ساختار وضعیت میتواند هم کد وضعیت و هم پیام خطا را دربرگیرد. نمونهای از بازیابی پیام خطا هنگام ایجاد مشتری جدید دردسترس است.
برای ارزیابی موفقیت:
کد وضعیت برگرداندهشده را که از نوع
ASDVGateway_StatusCode_tاست بررسی کنید.اشارهگری را به ساختاری ارسال کنید که تابع بتواند کد وضعیت و پیام خطا را در آن پر کند.
- اشارهگر بهعنوان آخرین پارامتر با نام
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 ارتباط بین برنامه تماسگیرنده و برنامههای دیگر را بااستفاده از SDV Comm Stack و SDV Gateway مقداردهی اولیه میکند. Init comms can be called in both of these contexts::
پساز ایجاد کارخواه.
قبلاز هرگونه تعامل با Data Tunnel، RPC، یا Service Discovery.
مثالی در اینجا ارائه شده است:
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, ¶ms, &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
);
جریان RPC
کتابخانه کلاینت Gateway تنظیمات «امنیت لایه انتقال» (TLS) را برای ارتباط با دیگر برنامههای سازگار با «خودرو نرمافزارمحور» مدیریت میکند. بهطور خاص، تنظیمات TLS موردنیاز برای ارتباط را بازیابی میکند. نحوه تعیین استفاده از TLS در اینجا آمده است:
وقتی «حالت راهاندازی خودروی نرمافزارمحور»
LOCKEDباشد، از TLS استفاده میشود.وقتی «حالت راهاندازی خودروی نرمافزارمحور»
UNLOCKEDباشد، از ارتباط ناامن استفاده میشود. وقتی از TLS استفاده میشود، کتابخانه کارخواه Gateway جفت کلیدی تولید میکند که فقط برای فرایند برنامه شناختهشده است. کارخواه اعتبارنامه RPC را برای ایجاد سرور RPC یا کانال کارخواه RPC بازیابی میکند. RPC از VLAN SDV-RPC اختصاصی استفاده میکند. کتابخانه مشتری Gateway برای تغییر شبکه پیشفرض فرایند به SDV-RPC VLAN، درطول یا پساز تماسASDVGateway_Client_initComms، android_setprocnetwork را فراخوانی میکند.
دردسترس بودن RPC
کارخواهی که برای شروع در راهاندازی اولیه پیکربندی شده است ممکن است قبلاز دردسترس قرار گرفتن SDV-RPC VLAN شروع شود. قبلاز تلاش برای ایجاد هرگونه سوکت سرور RPC یا سوکت مشتری RPC، بررسی کنید که 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، شنوندهای برای رویدادهای کارخواه تنظیم کنید تا وقتی وضعیت دردسترس بودن RPC تغییر میکند مطلع شوید.
اگر برنامه شبکه فرایند را عوض کند، ASDVGateway_Client_isProcessBoundToSdvRpcNetworkInterface ترجیح داده میشود زیرا هم دردسترس بودن RPC را بررسی میکند و هم اینکه فرایند به VLAN خودروی نرمافزارمحور-RPC محدود شده باشد.
عوض کردن شبکه پیشفرض فرایند
ممکن است لازم باشد از SDV-RPC VLAN بهعنوان شبکه پیشفرض فرایند
به سوکتهای باز برای اتصالات محدود به اینترنت تغییر دهید. برای لغو اتصال و اتصال مجدد فرایند به SDV-RPC VLAN،
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
برای ایجاد سرور 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,
¶ms,
&status
);
// Basic error handling
if (statusCode != ASDVGateway_StatusCode_OK) {
std::cerr << "Failed to register RPC server: " << status.errorMessage << std::endl;
}
وقتی سیستم گواهینامههای ریشه را در زمان اجرا بهروز میکند (برای مثال، درطول تغییرات وضعیت VM)، سرورهای RPC باید فهرست گواهینامههای پذیرفتهشده خود را بازآوری کنند:
بااستفاده از
ASDVGateway_Client_setClientNotificationCallback، شنوندهای برای رویدادهای کارخواه تنظیم کنید تا وقتی گواهینامههای ریشه بهروزرسانی شدند مطلع شوید.برای دریافت گواهینامههای ریشه بهروزشده، با
ASDVGateway_Client_rpcCredentialsتماس بگیرید.
جریان کارخواه RPC
جریان برای مشتری 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,
¶ms,
&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 برای ثبت واحد سرویس ناشر در «دروازه خودروی نرمافزارمحور» ازطریق میانای AIDL آن و ایجاد «صف پیام سریع» (FMQ) در فرایند برنامه استفاده میشود. Binder فقط برای راهاندازی FMQ استفاده میشود، نه هنگام نوشتن پیام. سپس از ASDVGateway_Client_publishMessages API
برای انتشار پیامها در انتشارات ایجادشده استفاده میشود. این شامل
نوشتن در FMQ انتشارات و اطلاعرسانی درباره نوشته شدن پیامها است.
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,
¶ms,
&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
);
ایجاد مشترک با شنونده اعلان
برای مشترک شدن در یک سایت خبری و دریافت اعلانهای Data Tunnel (مانند
دردسترس بودن پیام)، از ASDVGateway_Client_subscribeToPublicationByName
API استفاده کنید. این میانای برنامهسازی کاربردی همچنین 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,
¶ms,
&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 API استفاده کنید:
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 (AID) به سرویس اختصاص دهید.
فرض کنید سرویس شما native_sdv_gateway_client_service نام دارد،
فایل اجرایی در پارتیشن /vendor در
/vendor/bin/native_sdv_gateway_client_service قرار دارد، و از
vendor_gateway_client بهعنوان شناسه Android (AID) برای اجرای سرویس استفاده میکنید.
با این راهاندازی، میتوانید سرویس init زیر را تعریف کنید:
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برای کلاینتهای دروازه SDV برای استفاده از میانای شبکه جهت برقراری ارتباط بین ماشینهای مجازی SDV لازم است. گروههای دیگر را میتوان درصورت نیاز اضافه کرد. - این مثال از
disabledوoneshotاستفاده میکند. ممکن است لازم باشد گزینههای خدمات را برای خدماتتان اصلاح کنید. سرویس را بعدازsdv_gatewayشروع کنید.
ایجاد قوانین SELinux برای سرویس
برای استفاده از میاناهای برنامهسازی کاربردی SDV Gateway، به قوانین 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همه اجازههای SELinux لازم را برای تعامل با «دروازه SDV» ارائه میدهد. خط زیر این قوانین را به فایل اجرایی اعطا میکند:/vendor/bin/native_sdv_gateway_client_service u:object_r:native_sdv_gateway_client_service_exec:s0
نمونه کد
برای کسب اطلاعات بیشتر درباره اجرای نمونه کد بومی که نحوه مستندسازی میاناهای برنامهسازی کاربردی C را نشان میدهد، به
system/software_defined_vehicle/samples/sdv_gateway/NativeSdvGatewayTestApp/README.md مراجعه کنید.
SDV Gateway در برنامه IVI Java
مدل تعامل برای «دروازه خودروی نرمافزارمحور» در «سیستم سرگرمی-اطلاعاتی درونخودرو» در این نمودار نشان داده شده است:
شکل ۴. SDV Gateway on IVI Java app.
بهطور خاص، این مدل:
- همه تماسها را به لایه JNI ارسال میکند.
- میانای برنامهسازی کاربردی Java و C را بههم متصل میکند.
- تعاملات AIDL را با
ISdvGatewayتعریف میکند:- ارتباطات اولیه
- سرور RPC را پیدا یا ایجاد کنید
- ایجاد pub/sub
- انجام تعاملات تونل داده کشف سرویس
- دریافت اعلان (برای مثال، دادههای دردسترس)
- خواندن و نوشتن پیامها (FMQ برای pub/sub) برای ایجاد جفت کلید و گواهیهای TLS
گنجاندن کتابخانههای کارخواه Gateway
کتابخانه Java و پوشاننده JNI برای libsdvgatewayclient C API در APEX در مقصد IVI نصب شدهاند. یک وابستگی زمان ترجمه به
کدبند کتابخانه Java، کتابخانه Java که باید در زمان اجرا استفاده شود، و
APEX لازم حاوی کتابخانه Java اضافه کنید.
android_app {
name: "YourAppName",
// ...
static_libs: [
"libsdvgatewayclient-java",
],
libs: [
"libsdvgatewayclient-java-sdk.stubs",
],
uses_libs: [
"libsdvgatewayclient-java-sdk",
],
required: [
"com.sdv.google.gateway.client",
],
// ...
}
در مورد برنامه جداسازیشدهای که خارج از درخت SDV ساخته شده است، فرایند مشابه است:
JAR خرد کتابخانه Java تولیدشده و JAR پشتیبانی برای RPC را در پوشه libs برنامه کپی کنید. برای جزئیات، به
system/software_defined_vehicle/sdv_gateway/libsdvgatewayclient_apex/README.mdمراجعه کنید.JAR تهبرگها را بهعنوان وابستگی فقط کامپایل اضافه کنید. برای مثال، پیکربندیهای Gradle را بهروز کنید تا با افزودن ورودی
compileOnlyبه بخشdependenciesبه کدهای ناقص وابسته شوند: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")) }کتابخانه 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();
ارتباطات اولیه
بااستفاده از نام برنامه بهعنوان نام بسته سرویس، با 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 شامل روشهایی برای موارد زیر است:
وقتی واحدهای سرویس با نوع واحد خاصی (یا بهطور جایگزین با نام خاصی مطابقت داشته باشند) ثبت یا لغو ثبت میشوند، اعلان دریافت کنید.
واحدهای خدمات فعلی را با نوع واحد خاصی فهرست کنید (یا بهطور جایگزین با نام نوع واحد خدمات خاصی مطابقت دهید). برای دریافت اعلان درباره تغییرات واحد سرویس، شنوندهای ایجاد کنید:
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 برای مطلع کردن شنونده هنگام ثبت یا لغو ثبت سرویسها با نوع واحد مشخصشده استفاده کنید:
// 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 استفاده کنید:
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
کلاینتهای «دروازه خودروی نرمافزارمحور» در سیستمهای «سرگرمی-اطلاعاتی درونخودرو» از فراخوانی رویه از راه دور Google (gRPC) برای ارتباط با سرویسهای «خودروی نرمافزارمحور» استفاده میکنند. این تعاملات به تعاریف proto از کاتالوگ 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) {}
}
به کتابخانه proto مربوطه پیوند دهید و سرویس را تعریف کنید:
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) {}
}
پیوند به کتابخانه proto مربوطه:
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());
در داخل، میانای برنامهسازی کاربردی ASDVGateway_Client_findRpcServerByName برای پیدا کردن
سرور RPC فراخوانی میشود. اگر سرور RPC پیدا شود، کانال مدیریتشده در حالت ناامن ایجاد میشود یا برای استفاده از پیکربندی TLS تعیین میشود، مشابه جریان سرور RPC، بسته به پیکربندی Service Discovery. برنامه
با شیء 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 بهعنوان شبکه پیشفرض فرایند
به سوکتهای باز برای اتصالات محدود به اینترنت تغییر دهید. unbindProcessFromSdvRpcNetworkInterface و
bindProcessToSdvRpcNetworkInterface را فراخوانی کنید تا فرایند را از VLAN خودروی نرمافزارمحور-RPC جدا کنید و دوباره به آن متصل کنید. این دو فراخوانی بهعنوان سوئیچهای سراسری عمل میکنند و رابط شبکه را که سوکتها برای همه رشتههای فرایند به آن متصل هستند تغییر میدهند.
// 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 و انتشار به میاناهای برنامهسازی کاربردی
ASDVGateway_Client_createPublication و
ASDVGateway_Client_readAvailableMessages بومی متکی هستند. برای اطلاعات دقیق درباره
C API، به استفاده از SDV Gateway در سرویس بومی IVI مراجعه کنید. شیء
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 برای مدیریت اشتراک، رسیدگی به اعلان، و بازیابی پیام استفاده میکند. برای
نمایش استفاده از API، نمونه برنامه Java ارائهشده در فایل دودویی
در system/software_defined_vehicle/samples/sdv_gateway/README.md را ببینید.
مجوزهای لازم
فراخوانی میانای برنامهسازی کاربردی مشتری «دروازه خودروی نرمافزارمحور» به اجازههای خاصی نیاز ندارد، اما باید قوانین SELinux مناسب را برای برنامههایتان اعمال کنید.
- برای اشتراکها و انتشارات تونل داده، به هیچ اجازهای نیاز ندارید.
- برای SDV RPC، به اجازههای زیر نیاز دارید:
android.permission.INTERNETandroid.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
برای استفاده از میاناهای برنامهسازی کاربردی SDV Gateway، برنامههای Java به همان اجازههای سرویسهای بومی نیاز دارند. این اجازهها را بااستفاده از
ماکرو SELinux sdv_gateway_client_domain() اعطا کنید:
sdv_gateway_client_domain(my_oem_sdv_gateway_client_app)
سازنده اصلی تجهیزات دامنه my_oem_sdv_gateway_client_app را برای برنامههای Java
که مجاز به استفاده از «دروازه خودروی نرمافزارمحور» هستند تعریف میکند. از «دروازه خودروی نرمافزارمحور» فقط از سیستم و
برنامههای ممتاز استفاده کنید.
مکانهای کد
کد منبع «دروازه خودروی نرمافزارمحور» را در
system/software_defined_vehicle/sdv_gateway/ دریافت کنید. میتوانید نمونههای «دروازه خودروی نرمافزارمحور» را برای موارد زیر دریافت کنید:
- میانای برنامهسازی کاربردی کارخواه C:
system/software_defined_vehicle/samples/sdv_gateway/NativeSdvGatewayTestApp/ - میانای برنامهسازی کاربردی Java کارخواه:
system/software_defined_vehicle/samples/sdv_gateway/SdvCarMonitorTestApp/