小熊派添加自定義JS API接口流程(以點亮LED為例)
原創(chuàng)??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??
??https://harmonyos.51cto.com??
一、前言
之前我們開發(fā)了小熊派的LED燈驅(qū)動,并且編寫了相應的代碼調(diào)用自己的開發(fā)驅(qū)動,但是自己寫的代碼只能在終端以命令的形式調(diào)用,顯然這樣十分的不太友好。假設(shè)小熊派是我們的一個智能設(shè)備,這個LED是我們的手電筒,通過命令行的形式打開手電筒肯定不是我們所期待的,我們更希望能夠在圖像界面上點一個按鈕從而打開我們的手電筒,而目前OpenHarmony大多數(shù)采用JS開發(fā)應用,如果我們想要在圖形界面上打開我們的手電筒,就需要自己定義一個JS的API接口來調(diào)用我們的LED。本文便介紹了如何自定義JS API從而調(diào)用自己開發(fā)的驅(qū)動。
二、流程總覽
添加JS API接口–>編寫接口代碼–>配置HDF頭文件路徑–>添加編譯依賴
三、添加JS API接口
打開文件foundation\ace\ace_engine_lite\frameworks\src\core\modules\app_module.h,在32行處添加如下代碼
static JSIValue ToggleLed(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum);
如圖所示
在65行處添加如下代碼
JSI::SetModuleAPI(exports, "ledcontrol", AppModule::ToggleLed);
四、編寫對應的驅(qū)動代碼
打開foundation\ace\ace_engine_lite\frameworks\src\core\modules\app_module.cpp文件,需要在兩個位置添加我們自己API的代碼
4.1添加頭文件
在頭文件導入處添加如下頭文件
#include "hdf_sbuf.h"
#include "hdf_io_service_if.h"
#define LED_WRITE_READ 1
#define LED_SERVICE "hdf_led"
如圖所示
4.2添加API的代碼
在適當位置加入以下代碼,建議在JSIValue AppModule::GetInfo函數(shù)之前
static int OnDevEventReceived(void *priv, uint32_t id, struct HdfSBuf *data)
{
uint32_t value;
HdfSbufReadUint32(data, &value);
HILOG_ERROR(HILOG_MODULE_ACE,"%s: dev event received: %u %u\n", (char *)priv, id, value);
return HDF_SUCCESS;
}
static int GpioWriteRead(struct HdfIoService *serv, int32_t eventData, int32_t *val)
{
int ret = HDF_FAILURE;
struct HdfSBuf *data = HdfSBufObtainDefaultSize();
struct HdfSBuf *reply = HdfSBufObtainDefaultSize();
if (data == NULL || reply == NULL) {
HILOG_ERROR(HILOG_MODULE_ACE,"fail to obtain sbuf data\n");
return ret;
}
if (!HdfSbufWriteUint8(data, (uint8_t)eventData))
{
HILOG_ERROR(HILOG_MODULE_ACE,"fail to write sbuf\n");
HdfSBufRecycle(data);
HdfSBufRecycle(reply);
return ret;
}
ret = serv->dispatcher->Dispatch(&serv->object, LED_WRITE_READ, data, reply);
if (ret != HDF_SUCCESS)
{
HILOG_ERROR(HILOG_MODULE_ACE,"fail to send service call\n");
HdfSBufRecycle(data);
HdfSBufRecycle(reply);
return ret;
}
if (!HdfSbufReadInt32(reply, val))
{
HILOG_ERROR(HILOG_MODULE_ACE,"fail to get service call reply\n");
ret = HDF_ERR_INVALID_OBJECT;
HdfSBufRecycle(data);
HdfSBufRecycle(reply);
return ret;
}
HILOG_ERROR(HILOG_MODULE_ACE,"Get reply is: %d\n", val);
HdfSBufRecycle(data);
HdfSBufRecycle(reply);
return ret;
}
JSIValue AppModule::ToggleLed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
{
HILOG_ERROR(HILOG_MODULE_ACE, "led button pressed.");
struct HdfIoService *serv = HdfIoServiceBind(LED_SERVICE);
if (serv == NULL)
{
HILOG_ERROR(HILOG_MODULE_ACE,"fail to get service2 %s\n", LED_SERVICE);
return JSI::CreateUndefined();
}
if ((args == nullptr) || (argsNum == 0) || (JSI::ValueIsUndefined(args[0]))) {
return JSI::CreateUndefined();
}
JSIValue success = JSI::GetNamedProperty(args[0], CB_SUCCESS);
JSIValue fail = JSI::GetNamedProperty(args[0], CB_FAIL);
JSIValue complete = JSI::GetNamedProperty(args[0], CB_COMPLETE);
int32_t num = (int32_t)JSI::GetNumberProperty(args[0], "code");
int32_t replyData = 0;
if (GpioWriteRead(serv, num, &replyData))
{
HILOG_ERROR(HILOG_MODULE_ACE,"fail to send event\n");
JSI::CallFunction(fail, thisVal, nullptr, 0);
JSI::CallFunction(complete, thisVal, nullptr, 0);
JSI::ReleaseValueList(success, fail, complete);
return JSI::CreateUndefined();
}
JSIValue result = JSI::CreateObject();
JSI::SetNumberProperty(result, "led_status", replyData);
JSIValue argv[ARGC_ONE] = {result};
JSI::CallFunction(success, thisVal, argv, ARGC_ONE);
JSI::CallFunction(complete, thisVal, nullptr, 0);
JSI::ReleaseValueList(success, fail, complete, result);
HdfIoServiceRecycle(serv);
return JSI::CreateUndefined();
}
代碼推薦添加位置如圖所示
五、配置HDF頭文件路徑
打開foundation\ace\ace_engine_lite\ace_lite.gni文件,在大約80行處添加如下配置
ace_lite_include_dirs += [
"http://drivers/framework/ability/sbuf/include",
"http://drivers/framework/include/core",
"http://drivers/framework/include/utils",
"http://drivers/adapter/uhdf/posix/include",
]
JS API\HDF配置.png)
六、添加編譯依賴
打開foundation\ace\ace_engine_lite\frameworks\BUILD.gn,在public_deps中添加以下代碼
"http://drivers/adapter/uhdf/manager:hdf_core",
打開foundation\ace\ace_engine_lite\test\ace_test_config.gni,在extra_deps中添加以下代碼
"http://drivers/adapter/uhdf/manager:hdf_core",
??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??
??https://harmonyos.51cto.com??