frameworks/base/core/java/android/hardware/location/ContextHubManager.java
java层对外的接口是 ContextHubManager.java, 通过它创建 ContextHubClient和NanoApp通信。
HandlerThread handlerThread = new HandlerThread("MainLoopThread");
handlerThread.start();
mMainLooper = handlerThread.getLooper();
mContext = this;
try {
mContextHubManager = new ContextHubManager(mContext, mMainLooper);
} catch (Exception e) {
e.printStackTrace();
}
获得ContextHubInfo
private List<ContextHubInfo> mListContextHubInfo;
try {
mListContextHubInfo = mContextHubManager.getContextHubs();
} catch (Exception e) {
e.printStackTrace();
}
load nanoApp
byte[] nanoappByte = FiletoByteArray("/data/data/com.xiaomi.aiasst.android_learning/files/java_nanoapp");
Log.i(TAG, "nanoApp length: " + nanoappByte.length);
mNanoApp = new NanoAppBinary(nanoappByte);
Log.i(TAG, "LoadNanoAppInfo: " + "Magic: " + mNanoApp.hasValidHeader());
Log.i(TAG, "LoadNanoAppInfo: " + "NanoAppId: " + mNanoApp.getNanoAppId());
Log.i(TAG, "LoadNanoAppInfo: " + "NanoAppVersion: " + mNanoApp.getNanoAppVersion());
ContextHubTransaction<Void> loadNanoAppTrans = mContextHubManager.loadNanoApp(mListContextHubInfo.get(0), mNanoApp);
ContextHubTransaction.Response<Void> responseLoad = loadNanoAppTrans.waitForResponse(5000, TimeUnit.MILLISECONDS);
Log.i(TAG, "LoadNanoAppInfo: " + "Result: " + responseLoad.getResult());
从java层加载nanoApp 和native加载nanoApp是不同的,native层加载的是elf格式的so文件,而Java层加载的是nano_app_binary_t
// The binary format below is in little endian format
struct nano_app_binary_t {
uint32_t header_version; // 0x1 for this version
uint32_t magic; // "NANO"
struct hub_app_name_t app_id; // App Id contains vendor id
uint32_t app_version; // Version of the app
uint32_t flags; // Signed, encrypted
uint64_t hw_hub_type; // which hub type is this compiled for
// The version of the CHRE API that this nanoapp was compiled against.
// If these values are both set to 0, then they must be interpreted the same
// as if major version were set to 1, and minor 0 (the first valid CHRE API
// version).
uint8_t target_chre_api_major_version;//1
uint8_t target_chre_api_minor_version;//2
uint8_t reserved[6]; // Should be all zeroes
uint8_t custom_binary[0]; // start of custom binary data
} __attribute__((packed));
就是说java层使用的是nano_app_binary 真正的so文件前面还有个header.
怎么生成header文件
使用python 的 struct 库
ss = struct.pack("<2IQ2IQ8B", 1, 1330528590, 2328716490200580116, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0)
binfile = open("/home/nanoapp_head", "wb")
binfile.write(ss)
binfile.close()
怎么合并文件
cat nanoapp_head chre_app_oem.so > java_nanoapp
unload NanoApp
当前enable/disable NanoApp没有实现,load后就enable如果不让nanoApp工作可以使用unload.
ContextHubTransaction<Void> disableNanoAppTrans = mContextHubManager.unloadNanoApp(mListContextHubInfo.get(0), mListNanoAppState.getContents().get(0).getNanoAppId());
ContextHubTransaction.Response<Void> responseLoad = disableNanoAppTrans.waitForResponse(5000, TimeUnit.MILLISECONDS);
Log.i(TAG, "unloadNanoAppTrans: " + "Result: " + responseLoad.getResult());
获得NanoApp Info
ContextHubTransaction<List<NanoAppState>> queryNanoAppsTrans = mContextHubManager.queryNanoApps(mListContextHubInfo.get(0));
mListNanoAppState = queryNanoAppsTrans.waitForResponse(5000, TimeUnit.MILLISECONDS);
for (int i = 0; i < mListNanoAppState.getContents().size(); i++) {
Log.i(TAG, "NanoAppState: " + mListNanoAppState.getContents().get(i).getNanoAppId());
Log.i(TAG, "NanoAppState: " + mListNanoAppState.getContents().get(i).isEnabled());
}
和slpi上下行通信
实现的通信的前提是创建ContextHubClient对象
create ContextHubClient
public class impl_ContextHubClientCallback extends ContextHubClientCallback {
public void onMessageFromNanoApp(ContextHubClient client, NanoAppMessage message) {
Log.i(TAG, "onMessageFromNanoApp");
String ss = new String(message.getMessageBody());
Log.i(TAG, "message body: " + ss);
}
public void onHubReset(ContextHubClient client) {
Log.i(TAG, "onHubReset");
}
public void onNanoAppAborted(ContextHubClient client, long nanoAppId, int abortCode) {
Log.i(TAG, "onNanoAppAborted");
}
public void onNanoAppLoaded(ContextHubClient client, long nanoAppId) {
Log.i(TAG, "onNanoAppLoaded");
}
public void onNanoAppUnloaded(ContextHubClient client, long nanoAppId) {
Log.i(TAG, "onNanoAppUnloaded");
}
public void onNanoAppEnabled(ContextHubClient client, long nanoAppId) {
Log.i(TAG, "onNanoAppEnabled");
}
public void onNanoAppDisabled(ContextHubClient client, long nanoAppId) {
Log.i(TAG, "onNanoAppDisabled");
}
}
mContextHubClientCallback = new impl_ContextHubClientCallback();
mContextHubClient = mContextHubManager.createClient(mListContextHubInfo.get(0), mContextHubClientCallback);
Log.i(TAG, "ContextHubClient: OK");
create NanoAppMessage
String str = "welcome to xxx park";
int echoType= 0x00401;
NanoAppMessage echoMessage = NanoAppMessage.createMessageToNanoApp(mListNanoAppState.getContents().get(0).getNanoAppId(), echoType, str.getBytes());
mContextHubClient.sendMessageToNanoApp(echoMessage);
message return from slpi
public void onMessageFromNanoApp(ContextHubClient client, NanoAppMessage message) {
Log.i(TAG, "onMessageFromNanoApp");
String ss = new String(message.getMessageBody());
Log.i(TAG, "message body: " + ss);
}