shared_library("serial_service_api") { sources = [ "service/serial_service.cpp" ] include_dirs = ["include","service", ]
void InitRouterModule(JSIValue exports)
{
JSI::SetModuleAPI(exports, "replace", RouterModule::Replace);
JSI::SetModuleAPI(exports, "init", RouterModule::Init);
JSI::SetModuleAPI(exports, "format", RouterModule::Format);
JSI::SetModuleAPI(exports, "write", RouterModule::Write);
JSI::SetModuleAPI(exports, "read", RouterModule::ReadFormat);
JSI::SetModuleAPI(exports, "disable", RouterModule::Disable);
JSI::SetModuleAPI(exports, "on", RouterModule::OnRead);
COMMUNICATION::GetInstance()->SerialRead(RouterModule::ReadingCallback);
}
串口初始化及进制转换相关代码可直接调用库函数。串口读回调的相关代码如下:
JSIValue ReadCallback = JSI::CreateUndefined();
void RouterModule::ReadingCallback(const char* buffer, int length)
{
if(!JSI::ValueIsUndefined(ReadCallback) && gSerialFlag){
if(length < 0){
SERIAL_LOGI("JS ReadingCallback == 0");
JSIValue evt = JSI::CreateString("error");
JSI::CallFunction(ReadCallback, JSI::CreateUndefined(), &evt, 1);
return;
} else if(length >= 0){
SERIAL_LOGI("JS ReadingCallback == 1");
JSIValue evt = JSI::CreateString(buffer);
JSI::CallFunction(ReadCallback, JSI::CreateUndefined(), &evt, 1);
}
}
}
JSIValue RouterModule::OnRead(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize)
{
if(!JSI::ValueIsUndefined(ReadCallback)){
JSI::ReleaseValue(ReadCallback);
return JSI::CreateBoolean(false);
}
if (JSI::ValueIsUndefined(args[0])) {
return JSI::CreateBoolean(false);
}
ReadCallback = JSI::GetNamedProperty(args[0], "ReadingCallback");
if(JSI::ValueIsUndefined(ReadCallback)){
SERIAL_LOGI("Read Callbk is not got it");
}
}
在route模块增加相关依赖foundationaceace_engine_liteframeworksBUILD.gn
deps = [
"//device/hals/communication/serial_port:serial_service_api",
]
extern void InitSerialPortModule(JSIValue exports);const Module OHOS_MODULES[] = {{"serialport", InitSerialPortModule},}
配置服务自启动:vendor/ingenic/halley5/rootfs-overlay/etc/init.d/S99WmsStart:sleep 1 && /bin/wifi_server &
#! /bin/sh
sleep 1 && /bin/wms_server &
sleep 1 && /bin/wifi_server &
sleep 1 && /bin/util_server &
sleep 1 && /bin/serial_port_service &
配置服务:basesecuritypermissionservicespermission_liteipc_authincludepolicy_preset.h
FeaturePolicy serialServiceFeature[] = {
{
NULL,
{
{
.type = RANGE,
.uidMin=0,
.uidMax=__INT_MAX__,
}
},
},
};
static PolicySetting g_presetPolicies[] = {
{"serialportservice", serialServiceFeature, 1},
};
代码实现服务初始化,具体实现可参考wifi_lite相关代码。在Invoke中去调用串口相关功能**serial_portserial_port_liteservicesamgr_serial_port_service.cpp
//继承并重新定义服务:
struct SamgrSerialPortService {
INHERIT_SERVICE;
INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
Identity identity;
};
static const char *GetName(Service *service)
{
//(void)service;
return SERIAL_PORT_SERVICE_NAME;
}
static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
{
SerialPortService::RequestHandle(funcId, origin, req, reply);
return EC_SUCCESS;
}
//创建服务对象:
static SamgrSerialPortService SerialPortSvc = {
.GetName = GetName,
.Initialize = Initialize,
.MessageHandle = MessageHandle,
.GetTaskConfig = GetTaskConfig,
SERVER_IPROXY_IMPL_BEGIN,
.Invoke = Invoke,
IPROXY_END,
};
//向SAMGR注册服务及接口:
static void Init()
{
SERIAL_LOGI("serial RegisterService Init, ver = %u", SerialPortSvc.ver);
bool ret = SAMGR_GetInstance()->RegisterService((Service *)&SerialPortSvc);
if (!ret) {
SERIAL_LOGI("RegisterService error");
return;
}
ret = SAMGR_GetInstance()->RegisterDefaultFeatureApi(SERIAL_PORT_SERVICE_NAME, GET_IUNKNOWN(SerialPortSvc));
if (!ret) {
SERIAL_LOGI("RegisterDefaultFeatureApi error");
return;
}
SERIAL_LOGI("serial RegisterService out, ver = %u", SerialPortSvc.ver);
}
SYSEX_SERVICE_INIT(Init);
更多热点文章阅读
提示:本文由电子发烧友论坛发布,转载请注明来源。如需社区合作及入群交流,请添加微信EEFans0806,或者发邮箱liuyong@huaqiu.com。
原文标题:OpenHarmony L1(3.0)串口功能开发
文章出处:【微信公众号:电子发烧友开源社区】欢迎添加关注!文章转载请注明出处。
全部0条评论
快来发表一下你的评论吧 !