L1 Hi3516

 该文档基于OpenHarmony 3.0开发,已在2022年9月弃用。

根据openharmony开源社区的samgr_lite的说明文档,写一个SA的demo
https://gitee.com/openharmony/distributedschedule_samgr_lite

目录

1.demo代码

1.1目录树

1.2 module_define.h

1.3 moduleserver.c

1.4 modulefeature.c

1.5 moduleclient.c

1.6 moduleservice.c

1.7 small目录下的BUILD.gn

1.8 small上一级目录的BUILD.gn

2 添加mymodule.json

3 修改config.json

4 新增启动进程

5 新增SA的IPC权限

6 生成文件


1.demo代码

1.1目录树

mymodule
└─lite
 │ BUILD.gn
 │
 ├─mini
 └─small
   │ BUILD.gn
   │
   ├─include
   │ module_define.h
   │
   └─src
     moduleclient.c
     modulefeature.c
      moduleserver.c
     moduleservice.c

1.2 module_define.h

#ifndef MODULE_DEFINE_H
#define MODULE_DEFINE_H

#define MAX_DATA_LEN 1024
#define MODULE_STACK_SIZE 0x1000
#define MODULE_QUEUE_SIZE 20
#define EXAMPLE_SERVICE "example_service"
#define EXAMPLE_FEATURE "example_feature"

#endif

1.3 moduleserver.c

#include <ohos_init.h>
#include <unistd.h>
#include <stdio.h>
#include <iunknown.h>
#include "samgr_lite.h"
#include "module_define.h"

typedef struct ExampleService {
    INHERIT_SERVICE;
    Identity identity;
} ExampleService;

typedef enum {
    /* add new msg */
    EXAMPLE_SRV_MSG_TEST,
    EXAMPLE_SRV_MSG_ID_MAX
} ExampleServiceMsgID;

static const char *GetName(Service *service);
static BOOL Initialize(Service *service, Identity identity);
static BOOL MessageHandle(Service *service, Request *msg);
static TaskConfig GetTaskConfig(Service *service);
// 创建服务对象
static ExampleService g_example = {
    .GetName = GetName,
    .Initialize = Initialize,
    .MessageHandle = MessageHandle,
    .GetTaskConfig = GetTaskConfig,
    {-1, -1, NULL}
};
//实现服务的生命周期函数
static const char *GetName(Service *service)
{
    (void)service;
    return EXAMPLE_SERVICE;
}

static BOOL Initialize(Service *service, Identity identity)
{
    ExampleService *example = (ExampleService *)service;
    example->identity = identity;
    return TRUE;
}

static BOOL MessageHandle(Service *service, Request *msg)
{
    ExampleService *example = (ExampleService *)service;
    (void)example;
    switch (msg->msgId) {
    case EXAMPLE_SRV_MSG_TEST:
        //业务处理
        break;
    default: break;
    }
    return FALSE;
}

static TaskConfig GetTaskConfig(Service *service)
{
    (void)service;
    TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, MODULE_STACK_SIZE, MODULE_QUEUE_SIZE, SINGLE_TASK};
    return config;
}
//向SAMGR注册服务及接口
static void Init(void)
{
    SAMGR_GetInstance()->RegisterService((Service *)&g_example);
}
//定义服务的初始化入口
SYSEX_SERVICE_INIT(Init);

1.4 modulefeature.c

#include <ohos_init.h>
#include <unistd.h>
#include <securec.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <iunknown.h>
#include "samgr_lite.h"
#include "iproxy_client.h"
#include "iproxy_server.h"
#include "module_define.h"

typedef struct {
    INHERIT_SERVER_IPROXY;
} DemoApi;

typedef struct DemoFeature {
    INHERIT_FEATURE;
    INHERIT_IUNKNOWNENTRY(DemoApi);
    Identity identity;
    Service *parent;
} DemoFeature;

typedef enum {
    MODULE_MSG_PROC,
    MODULE_MSG_TIME_PROC,
    MODULE_MSG_HELLO,
    MODULE_MSG_MAX
} ModuleFuncID;

static const char *FEATURE_GetName(Feature *feature);
static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity);
static void FEATURE_OnStop(Feature *feature, Identity identity);
static BOOL FEATURE_OnMessage(Feature *feature, Request *request);
static int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply);
//创建功能对象
static DemoFeature g_example = {
    .GetName = FEATURE_GetName,
    .OnInitialize = FEATURE_OnInitialize,
    .OnStop = FEATURE_OnStop,
    .OnMessage = FEATURE_OnMessage,
    DEFAULT_IUNKNOWN_ENTRY_BEGIN,
        .Invoke = Invoke,
    DEFAULT_IUNKNOWN_ENTRY_END,
    .identity = {-1, -1, NULL},
};
//实现功能的生命周期函数
static const char *FEATURE_GetName(Feature *feature)
{
    (void)feature;
    return EXAMPLE_FEATURE;
}

static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
{
    DemoFeature *demoFeature = (DemoFeature *)feature;
    demoFeature->identity = identity;
    demoFeature->parent = parent;
}

static void FEATURE_OnStop(Feature *feature, Identity identity)
{
    (void)feature;
    (void)identity;
    g_example.identity.queueId = NULL;
    g_example.identity.featureId = -1;
    g_example.identity.serviceId = -1;
}

static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
{
    (void)feature;
    if (request->msgId == MODULE_MSG_PROC) {
        Response response = {.data = "Yes, you did!", .len = 0};
        SAMGR_SendResponse(request, &response);
        return TRUE;
    }
    return FALSE;
}

static int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
{
    if ((iProxy == NULL) || (origin == NULL) || (req == NULL) || (reply == NULL)) {
        return -1;
    }
    if ((!IpcIoAvailable(req)) || (!IpcIoAvailable(reply))) {
        return -1;
    }
    switch (funcId) {
        case MODULE_MSG_HELLO:
            IpcIoPushString(req, "TRUE");
            break;
        default:
            IpcIoPushBool(reply, FALSE);
            break;
    }
    return 0;
}
//向SAMGR注册功能及接口
static void Init(void){
    SAMGR_GetInstance()->RegisterFeature(EXAMPLE_SERVICE, (Feature *)&g_example);
    SAMGR_GetInstance()->RegisterFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE, GET_IUNKNOWN(g_example));
}
//定义功能的初始化入口:
SYSEX_FEATURE_INIT(Init);

1.5 moduleclient.c

#include <ohos_errno.h>
#include <ohos_types.h>
#include <pthread.h>
#include <registry.h>
#include "samgr_lite.h"
#include <securec.h>
#include <stdint.h>
#include <unistd.h>
#include "iproxy_server.h"
#include "iproxy_client.h"
#include <iunknown.h>
#include <stdio.h>
#include "module_define.h"

typedef struct {
    INHERIT_CLIENT_IPROXY;
    int32_t(*HandleModuleMsg)(IUnknown *iUnknown, const char *buff);
} mymoduleClientProxy;

typedef struct DemoClientEntry {
    INHERIT_IUNKNOWNENTRY(mymoduleClientProxy);
} DemoClientEntry;

typedef enum {
    ID_HANDLE_MODULE_MSG
}ModuleFuncIdCall;

static mymoduleClientProxy *g_clientProxy;
static int32_t HandleModuleMsg(IUnknown *iUnknown, const char *buff);
static void *CreateClient(const char *service, const char *feature, uint32 size);
static void DestroyClient(const char *service, const char *feature, void *iproxy);

static int32_t ModuleSamgrInitialize(void)
{
    int32_t ret = (int32_t)SAMGR_RegisterFactory(EXAMPLE_SERVICE, EXAMPLE_FEATURE, CreateClient, DestroyClient);
    if (ret != 0) {
        return -1;
    }
    return 0;
}

static void *CreateClient(const char *service, const char *feature, uint32 size)
{
    (void)service;
    (void)feature;
    uint32 len = size + sizeof(DemoClientEntry);
    uint8 *client = malloc(len);
    if (client == NULL) {
        return NULL;
    }
    (void)memset_s(client, len, 0, len);
    DemoClientEntry *entry = (DemoClientEntry *)&client[size];
    entry->ver = ((uint16)CLIENT_PROXY_VER | (uint16)DEFAULT_VERSION);
    entry->ref = 1;
    entry->iUnknown.QueryInterface = IUNKNOWN_QueryInterface;
    entry->iUnknown.AddRef = IUNKNOWN_AddRef;
    entry->iUnknown.Release = IUNKNOWN_Release;
    entry->iUnknown.Invoke = NULL;
    entry->iUnknown.HandleModuleMsg = HandleModuleMsg;
    return client;
}

void DestroyClient(const char *service, const char *feature, void *iproxy)
{
    (void)service;
    (void)feature;
    if (iproxy != NULL) {
        free(iproxy);
        iproxy = NULL;
    }
}

static int32_t GetModuleClientApi()
{
    IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE);
    if (iUnknown == NULL) {
        return -1;
    }
    int32_t ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_clientProxy);
    if (ret != 0 || g_clientProxy == NULL) {
        return -1;
    }
    return 0;
}

static int32_t HandleModuleMsg(IUnknown *iUnknown, const char *buff)
{
    mymoduleClientProxy *proxy = (mymoduleClientProxy *)iUnknown;
    IpcIo request;
    char data[MAX_DATA_LEN];
    IpcIoInit(&request, data, MAX_DATA_LEN, 0);
    IpcIoPushString(&request, buff);
    int ret = proxy->Invoke((IClientProxy *)proxy, ID_HANDLE_MODULE_MSG, &request, NULL, NULL);
    printf("[test]%s\n\r", buff);
    return ret == EC_SUCCESS;
}

int HandleModule(void)
{
    int32_t ret = ModuleSamgrInitialize();
    if (ret != 0) {
        return -1;
    }
    ret = GetModuleClientApi();
    if ((ret != 0) || (g_clientProxy == NULL)) {
        return -1;
    }
    if (g_clientProxy->HandleModuleMsg == NULL) {
        return -1;
    }
    g_clientProxy->HandleModuleMsg((IUnknown *)g_clientProxy, "hello");
    (void)g_clientProxy->Release((IUnknown *)g_clientProxy);
    return 0;
}

1.6 moduleservice.c

#include <ohos_init.h>
#include <unistd.h>
#include <securec.h>
#include <iunknown.h>
#include <samgr_lite.h>
#include <signal.h>

int main(void)
{
    printf("my_module-start:SAMGR_Bootstrap\r\n");
    SAMGR_Bootstrap();
    while(1) {
        pause();
    return 0;
}
}

1.7 small目录下的BUILD.gn

import("//build/lite/config/component/lite_component.gni")
cflags_common = [ 
    "-O2",
    "-ftrapv",
    "-Wall",
    "-Werror",
    "-Wextra",
    "-Wshadow",
    "-fstack-protector-all",
    "-D_FORTIFY_SOURCE=2",
    "-Wformat=2", "-Wfloat-equal", "-Wdate-time", ]
#L1 server
lite_library("mymoduleserver") {
target_type = "shared_library"
sources = [
"src/modulefeature.c",
"src/moduleserver.c",
]
cflags = [ "-fPIC", ]
cflags += cflags_common
cflags_cc = cflags
ldflags = [ "-pthread", ]
if (board_name != "omap_se") {
ldflags += [ "-Wl,-z,relro,-z,now", "-s" ]
}
include_dirs = [
"include",
"//utils/native/lite/include",
"//utils/native/base/include/",
"//base/startup/syspara_lite/interfaces/kits",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/registry",
"//foundation/communication/ipc_lite/interfaces/kits",
]
deps = [
"//foundation/distributedschedule/samgr_lite:samgr",
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
]
}

#L1 client
lite_library("mymoduleclient") {
target_type = "shared_library"
sources = [
"src/moduleclient.c",
]
cflags = [ "-fPIC", ]
cflags += cflags_common
cflags_cc = cflags
ldflags = [ "-pthread", ]
if (board_name != "omap_se") {
ldflags += [ "-Wl,-z,relro,-z,now", "-s" ]
}
include_dirs = [
"include",
"//utils/native/lite/include",
"//utils/native/base/include/",
"//base/startup/syspara_lite/interfaces/kits",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/registry",
"//foundation/communication/ipc_lite/interfaces/kits",
]
deps = [
"//foundation/distributedschedule/samgr_lite:samgr",
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
]
}

#L1 service.bin
executable("mymodule_service") {
sources = [
"src/moduleservice.c",
]
cflags = [ "-fPIC", ]
cflags += cflags_common
cflags_cc = cflags
ldflags = [ "-pthread", ]
if (board_name != "omap_se") {
ldflags += [ "-Wl,-z,relro,-z,now", "-s" ]
}
include_dirs = [
"include",
"//utils/native/lite/include",
"//utils/native/base/include/",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/registry",
"//foundation/communication/ipc_lite/interfaces/kits",
]
deps = [
":mymoduleserver",
"//foundation/distributedschedule/samgr_lite:samgr",
]
}

1.8 small上一级目录的BUILD.gn

import("//build/lite/config/component/lite_component.gni")
lite_component("mymodule_lite") {
if (ohos_kernel_type == "liteos_m") {
features = []
} else {
features = [ 
    "small:mymoduleserver",
    "small:mymodule_service",
    "small:mymoduleclient", ]
}
}

2 添加mymodule.json

在build/lite/components下添加mymodule.json

{
    "components" : [
    {
        "component" : "mymodule_lite",
        "optional" : "true",
        "dirs" : [
            "//base/my_module/lite"
        ],
        "targets" : [
            "//base/my_module/lite:mymodule_lite"
        ],
        "rom" : "2.6M",
        "ram" : "~1.8M",
        "output" : [
            "libmymoduleserver.so",
            "libmymoduleclient.so"
        ],
        "adapted_kernel" : [ "liteos_a", "linux" ],
        "feature" : [],
        "deps" : {
        "third_party" : [
            "bounds_checking_function",
            "cjson"
        ],
        "components" : [
            "hilog_lite",
            "samgr_lite",
            "kal_timer",
            "ipc_lite"
        ]
        }
    }
    ]
}

3 修改config.json

在vendor/hisilicon/hispark_taurus/config.json文件里的"subsystems"添加代码

        {
            "subsystem" : "mymodule",
            "components" : [
            {
            "component" : "mymodule_lite",
            "features" : []
            }
            ]
        },

4 新增启动进程

在目录vendor\hisilicon\hispark_taurus\init_configs下,对文件
init_liteos_a_3516dv300.cfg 和

init_liteos_a_3516dv300_mksh.cfg 进行修改

“jobs” -> {“name” : “init” …} -> "cmds"下,增加

"start mymodule_service"

以及在"services"下增加

 { "name":"mymodule_service", "path":["/bin/mymodule_service"], "uid":0', "gid":0, "once":1, "importance":0, "caps":[] }

5 新增SA的IPC权限

修改文件base\security\permission\services\permission_lite\ipc_auth\include\policy_preset.h
增加内容:

FeaturePolicy mymoduleFeature[]={
    {
        NULL,
        {
            {
                .type = RAANGE,
                .uidMin = 0,
                .uidMax = 999,
            },
        },
    },
};

以及在g_presetPolicies[]里面增加

{"mymodule_svc", mymoduleFeature, 1}

6 生成文件

编译生成的bin文件在
out/hispark_taurus/ipcamera_hispark_taurus/bin

编译生成的so文件在
out/hispark_taurus/ipcamera_hispark_taurus/usr/lib

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐