当前位置: 首页 > 工具软件 > DBus > 使用案例 >

DBUS学习-DBUS实例

何骞尧
2023-12-01
  • 由 b178903294创建, 最后修改于8月 20, 2019

一、原生DBUS实例

原生dbus客户端代码和服务端代码:

client.c 折叠源码

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <dbus/dbus.h>

  

void printf_Buddha()

{

    printf("                                                     \n ");

    printf("   **                    _ooOoo_                     \n ");

    printf("   **                   o8888888o                    \n ");

    printf("   **                   88\" . \"88                    \n ");

    printf("   **                   (| -_- |)                    \n ");

    printf("   **                    O\\ = /O                     \n ");

    printf("   **                ____/`---'\\____                 \n ");

    printf("   **              .   ' \\\\| |// `.                  \n ");

    printf("   **               / \\\\||| : |||//\\                \n ");

    printf("   **             / _||||| -:- |||||- \\              \n ");

    printf("   **               | | \\\\\\ - /// | |                \n ");

    printf("   **             | \\_| ''\\---/'' | |                \n ");

    printf("   **              \\ .-\\__ `-` ___/-. /              \n ");

    printf("   **           ___`. .' /--.--\\ `. . __             \n ");

    printf("   **        ."" '< `.___\\_<|>_/___.' >'"".          \n ");

    printf("   **       | | : `- \\`.;`\\ _ /`;.`/ - ` : | |       \n ");

    printf("   **         \\ \\ `-. \\_ __\\ /__ _/ .-` / /          \n ");

    printf("   ** ======`-.____`-.___\\_____/___.-`____.-'======  \n ");

    printf("   **                    `=---='                     \n ");

    printf("   **                                                \n ");

    printf("   ** .............................................  \n ");

    printf("   **          佛祖保佑             永无BUG          \n ");

    printf("   **/                                               \n ");

}

  

DBusConnection* init_bus()

{

    DBusConnection *connection;

    DBusError err;

    int ret;

  

    dbus_error_init(&err);

    connection = dbus_bus_get(DBUS_BUS_SESSION, &err);

    if(dbus_error_is_set(&err))

    {

        printf("connection error: :%s -- %s\n", err.name, err.message);

        dbus_error_free(&err);

        return NULL;

    }

    ret = dbus_bus_request_name(connection, "hello.world.client", DBUS_NAME_FLAG_REPLACE_EXISTING, &err);

    if(dbus_error_is_set(&err))

    {

        printf("Name error: %s -- %s\n", err.name, err.message);

        dbus_error_free(&err);

        return NULL;

    }

    if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)

        return NULL;

  

    return connection;

}

  

  

void send_signal(DBusConnection *connection)

{

    DBusMessage *msg;

    DBusMessageIter arg;

    char *str = "hello world!";

  

  

    //创建一个signal对象

    //param1: path (这个逻辑来说,可以是任何字符串,只要符合规则即可)

    //param2: interface (一样)

    //param3: 信号方法名(必须与服务端名匹配)

    if((msg = dbus_message_new_signal("/hello""aa.bb.cc""alarm_test")) == NULL)

    {

        printf("message is NULL\n");

        return;

    }

#if 0

     //这个看需求添加,一般来说,信号是一种单向广播,加上这一句变单向单播

     //param2: bus_name

    if(!dbus_message_set_destination(msg, "hello.world.service"))

        {

                printf("memory error\n");

        }

#endif

  

    //添加参数的一些接口

    dbus_message_iter_init_append(msg, &arg);

    dbus_message_iter_append_basic(&arg, DBUS_TYPE_STRING, &str);

    //入队

    dbus_connection_send(connection, msg, NULL);

    //发送

    dbus_connection_flush(connection);

    //释放内存

    dbus_message_unref(msg);

  

    return;

}

  

void send_method_call(DBusConnection *connection)

{

    DBusMessage *msg;

    DBusMessageIter arg;

    DBusPendingCall *pending;

    int a = 100;

    int b = 99;

    int sum;

    //输入a和b两个整型数据

    printf("please input value of a and b \n");

    scanf("%d %d", &a, &b);

    printf("a= %d b=%d \n", a,b);

    msg = dbus_message_new_method_call("hello.world.service""/hello/world","hello.world""add");

    if(msg == NULL)

    {

        printf("no memory\n");

        return;

    }

  

    dbus_message_iter_init_append(msg, &arg);

        if(!dbus_message_iter_append_basic (&arg, DBUS_TYPE_INT32,&a)){

            printf("no memory!");

            dbus_message_unref(msg);

            return;

        }

    if(!dbus_message_iter_append_basic (&arg, DBUS_TYPE_INT32,&b)){

            printf("no memory!");

            dbus_message_unref(msg);

            return;

        }

  

    //入队message,等待回复

    //param1: 连接描述符

    //param2: message

    //param3: 相当于一个回调的一个描述符,为了获了返回的消息

    //param4: 超时间. -1代表无限

    if(!dbus_connection_send_with_reply (connection, msg, &pending, -1)){

        printf("no memeory!");

        dbus_message_unref(msg);

        return;

    }

  

    if(pending == NULL){

        printf("Pending is NULL, may be disconnect...\n");

        dbus_message_unref(msg);

        return;

    }

    //send

    dbus_connection_flush(connection);

    dbus_message_unref(msg);

     

    //阻塞,直到接收到一个响应.

    dbus_pending_call_block (pending);

    msg = dbus_pending_call_steal_reply (pending);

    if (msg == NULL) {

        printf("reply is null. error\n");

        return;

    }

    //释放pending内存

    dbus_pending_call_unref(pending);

    //解析参数

    if (!dbus_message_iter_init(msg, &arg))

        printf("no argument, error\n");

    if(dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_INT32)

    {

        printf("paramter type error\n");

    }

  

    dbus_message_iter_get_basic(&arg, &sum);

  

    printf(" a(%d) + b(%d) = %d\n",a, b, sum);

    dbus_message_unref(msg);

     

    return;

}

  

  

int main(int argc, char **argv)

{

    DBusConnection *connection;

  

    printf_Buddha();

    connection = init_bus();

    if(connection == NULL)

    {

        printf("connect to bus failed...\n");

        return -1;

    }

    send_signal(connection);

    send_method_call(connection);

     

    return 0;

}

service.c 折叠源码

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <dbus/dbus.h>

#include <unistd.h>

  

DBusConnection* init_bus()

{

    DBusConnection *connection;

    DBusError err;

    int ret = 0;

  

    dbus_error_init(&err);

  

    //与session dbus 建立连接

    //param1:bus type = {DBUS_BUS_SESSION, DBUS_BUS_SYSTEM} 一个系统dbus, 一个普通用户dbus

    //param2:错误信息,包括错误名与错误信息.

    connection = dbus_bus_get(DBUS_BUS_SESSION, &err);

    if(dbus_error_is_set(&err))

    {

        printf("Connection Error: %s--%s\n", err.name, err.message);

        dbus_error_free(&err);

        return NULL;

    }

  

    //为连接设置一个bus name: bus_name;

    //param 1: 连接描述符

    //param 2: 请求bus要分配的bus name(逻辑上讲,bus name可以是任何字符串,只要符合命名规则)

    //param 3: flags ={DBUS_NAME_FLAG_REPLACE_EXISTING,

    //                  DBUS_NAME_FLAG_ALLOW_REPLACEMENT,

    //                  DBUS_NAME_FLAG_DO_NOT_QUEUE

    //                   }

    //param 4: err info

    ret = dbus_bus_request_name(connection, "hello.world.service", DBUS_NAME_FLAG_REPLACE_EXISTING, &err);

    if(dbus_error_is_set(&err))

    printf("Name Error: %s--%s\n", err.name, err.message);

        dbus_error_free(&err);

        return NULL;

    }

  

    if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)

        return NULL;

  

    //注册感兴趣的signal: 来自接口dbus.test.signal.sender

    //param1: 连接描述符

    //param2: match rule (常用的类型: sender=

    //                                  interface=

    //                                  type=

    //                                  member= )

    //param3: err info

    //只设置一个type = signal,表示所有信号都接受.也可以加上接口,发送者bus_name

    dbus_bus_add_match(connection, "type='signal'", &err);

    //阻塞,直到消息发送成功.

    dbus_connection_flush(connection);

    if(dbus_error_is_set(&err))

    {

        printf("add Match Error %s--%s\n", err.name, err.message);

        dbus_error_free(&err);

        return connection;

    }

    return connection;

}

  

void handle_message(DBusConnection *connection)

{

    DBusMessage *msg;

    DBusMessageIter arg;

    char *str;

  

    while(1)

    {

        //param1: 连接描述符

        //param2: 超时时间, -1无限超时时间

        dbus_connection_read_write(connection, 0);

        //从队列中取出一条消息

        msg = dbus_connection_pop_message(connection);

        if(msg == NULL)

        {

            sleep(1);

            continue;

        }

        //这里应该过滤path,暂且不做

        //打印出消息对象路径

        printf("path: %s\n", dbus_message_get_path (msg));

        //param1: message

        //param2: interface 这个名字必须与发送那个接口一样.才能处理

        //param3: singal name 方法名也必须一样.

        if(dbus_message_is_signal(msg, "aa.bb.cc""alarm_test"))

        {

            //解析message 参数,0为无参数.

            if(!dbus_message_iter_init(msg, &arg))

            {

                printf("no argument\n");

            }

            //获取第一个参数类型

            if(dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_INVALID)

            {

                //获取参数的值

                dbus_message_iter_get_basic(&arg,&str);

                printf("recv param --: %s\n", str);

            }

             

        }

        else if(dbus_message_is_method_call(msg, "hello.world""add"))

        {/处理 add 远程调用.

            DBusMessage *rp;

            DBusMessageIter r_arg;

            int a = 0;

            int b = 0;

            int sum = 0;

            printf("service: add  function\n");

  

            if(!dbus_message_iter_init(msg, &arg))

            {

                printf("no argument!\n");

                goto out;

            }

            if(dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_INT32)

            {

                printf("argument error\n");

                goto out;

            }

            dbus_message_iter_get_basic(&arg, &a);

  

            if(!dbus_message_iter_next(&arg))

            {

                printf("too few argument!\n");

                goto out;

            }

            //check argument type....

            dbus_message_iter_get_basic(&arg, &b);

            sum = a + b;

out:

            //new 一个回应对象

            rp = dbus_message_new_method_return(msg);

            dbus_message_iter_init_append(rp, &r_arg);

            if(!dbus_message_iter_append_basic(&r_arg, DBUS_TYPE_INT32, &sum))

            {

                printf("no memory!!\n");

                return;

            }

  

            //param3: 这个跟消息序列有关

            if(!dbus_connection_send(connection, rp, NULL))

            {

                printf("no memory!!\n");

                return;

            }

            dbus_connection_flush(connection);

            dbus_message_unref(rp);

        }

        //释放空间

        dbus_message_unref(msg);

    }

    //dbus_bus_remove_match();

     

}

  

int main(int argc, char **argv)

{

    int ret = 0;

    DBusConnection *connection;

  

    connection = init_bus();

    if(connection == NULL)

    {

        printf("connect the dbus failed...\n");

        return -1;

    }

  

    handle_message(connection);

  

    return 0;

}

chroot环境下:

编译: gcc client.c -ldbus-1 -I/usr/include/dbus-1 -o client

            gcc service.c -ldbus-1 -I/usr/include/dbus-1 -o service

            有可能会提示找不dbus-arch-deps.h头文件,在系统中搜一下,然后拷贝到/usr/include/dbus-1.0/dbus目录
1. 先运行: dbus-launch

(cr) (c/c/dev-71) jq.bi@1fd8ac2f6306 ~/trunk/src/scripts dbus-launch

DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-xcnXn0MkyV,guid=cf2c2d2299c50fb44bcf8d195d59f539

DBUS_SESSION_BUS_PID=10306

2.导出这个变量 

export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-xcnXn0MkyV,guid=cf2c2d2299c50fb44bcf8d195d59f539

3. 运行 ./service &                        //&放入后台运行,需要在同一终端环境下,因为connection = dbus_bus_get(DBUS_BUS_SESSION, &err);  这句将获得上一步声明的DBUS_SESSION_BUS_ADDRESS环境变量。          ps:也可以在seewobook环境下一个终端下使用screen命令如同tmux一样多个pane,一个pane运行service另一个运行client。

4. 运行 ./client   之后观察结果

localhost ~ # /usr/sbin/service&

[1] 4612

localhost ~ # path: /org/freedesktop/DBus

path: /org/freedesktop/DBus

localhost ~ # client01

    **                    _ooOoo_

    **                   o8888888o

    **                   88" . "88

    **                   (| -_- |)

    **                    O\ = /O

    **                ____/`---'\____

    **              .   ' \\| |// `.

    **               / \\||| : |||//\

    **             / _||||| -:- |||||- \

    **               | | \\\ - /// | |

    **             | \_| ''\---/'' | |

    **              \ .-\__ `-` ___/-. /

    **           ___`. .' /--.--\ `. . __

    **        . '< `.___\_<|>_/___.' >'.

    **       | | : `- \`.;`\ _ /`;.`/ - ` : | |

    **         \ \ `-. \_ __\ /__ _/ .-` / /

    ** ======`-.____`-.___\_____/___.-`____.-'======

    **                    `=---='

    **

    ** .............................................

    **          佛祖保佑             永无BUG

    **/

 please input value of a and b

path: /org/freedesktop/DBus

path: /org/freedesktop/DBus

path: /hello

recv param --: hello world!

123 22

a= 123 b=22

path: /hello/world

service: add  function

 a(123) + b(22) = 145

localhost ~ # path: /org/freedesktop/DBus

path: /org/freedesktop/DBus

经过观察,代码确实没有可见bug。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

由于chromeos平台上使用了chromeos封装过后的dbus,但也同时兼容了原生的dbus,所以此实例仅供了解原生dbus原理和使用流程。封装过后的dbus实例参照 ,有完整的端口声明、头文件生成、相关接口的绑定与使用、gyp编译、添加upstart启动服务、由ebuild包管理等步骤流程。与dbus接口相关的函数可参照Chrome中的Chrome OS D-Bus使用情况、chromeos-dbus-bindings和Chrome OS D-Bus最佳实践中的解释来对应。

二、chromeos封装dbus实例

下面我们仿写大佬的代码,实现一个与上述代码功能相同的服务端代码。

首先先编写XML文件,后续使用其来生成org.chromium.CrosIit.h服务端基类头文件。

org.chromium.CrosIit.xml

<?xml version="1.0" encoding="UTF-8" ?>

<node name="/org/chromium/CrosIit"

      xmlns:tp="DbusSpec">

  <interface name="org.chromium.CrosIit">

    <method name="StartIitaddService">

      <arg name="argA" type="i" direction="in" />

      <arg name="argB" type="i" direction="in" />

    </method>

    <signal name="ReceiveRemoteIitData">

      <arg name="data" type="i" direction="out" />

    </signal>

  </interface>

</node>

method name为方法调用接口的名字,arg name是传入的两个参数。signal name是传出信号的名字,用于广播给对此感兴趣的进程。

此基类是由上述XML文件经过gyp编译后生成的。里面有我们需要的一个方法调用和一个信号对应的函数声明。

org.chromium.CrosIit.h 展开源码

在系统中为我们的服务起个名字,JSON服务配置文件如下:

dbus-service-config.json

{

  "service_name": "org.chromium.CrosIit"

}

policy文件主要用于用户权限的管理。

org.chromium.CrosIit.conf

<!DOCTYPE busconfig PUBLIC

 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"

 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">

<!--

  Copyright 2017 The Chromium Authors. All rights reserved.

  Use of this source code is governed by a BSD-style license that can be

  found in the LICENSE file.

-->

<busconfig>

  <policy user="iit">

    <allow own="org.chromium.CrosIit" />

    <allow send_destination="org.chromium.CrosIit" />

  </policy>

  <policy user="chronos">

    <allow send_destination="org.chromium.CrosIit" />

  </policy>

  <limit name="max_replies_per_connection">512</limit>

</busconfig>

注意里面的policy user对应的用户名字要与最后的ebuild用户组名字一致,也要与系统中新增的用户组名字一致。

代码实现:

main.cc 

#include <base/command_line.h>

#include <base/logging.h>

#include <brillo/syslog_logging.h>

#include "iit_daemon.h"

int __attribute__((visibility("default"))) main(int argc, char *argv[])

{

  base::CommandLine::Init(argc, argv);

  brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);

  iit::IitDaemon::GetInstance()->Run();

//  iit::IitDaemon().Run();

  return 0;

}

iit_dbus_adaptor.h 

#ifndef IIT_SDK_SRC_IIT_DBUS_ADAPTOR_H_

#define IIT_SDK_SRC_IIT_DBUS_ADAPTOR_H_

#include <string>

#include <memory>

#include <base/macros.h>

#include <base/memory/weak_ptr.h>

#include <brillo/dbus/dbus_object.h>

#include <brillo/dbus/exported_object_manager.h>

#include <brillo/secure_blob.h>

#include <dbus/file_descriptor.h>

#include "iit-sdk/dbus_adaptors/org.chromium.CrosIit.h"

namespace iit {

extern const char kIitServiceName[];

extern const char kIitServicePath[];

class IitDbusAdaptor : public org::chromium::CrosIitInterface,

                      public org::chromium::CrosIitAdaptor {

public:

  explicit IitDbusAdaptor(scoped_refptr<dbus::Bus> bus);

  ~IitDbusAdaptor() override = default;

  // Register the D-Bus object and interfaces.

  void RegisterAsync(

    const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);

  bool StartIitaddService(

       brillo::ErrorPtr *error,  

       int32_t in_argA,

       int32_t in_argB) override; //此处实现对基类的重载

  void ReceiveRemoteIitData( int32_t data);

private:

  brillo::dbus_utils::DBusObject dbus_object_;

  //IitClientImpl iot_client_impl_;

  DISALLOW_COPY_AND_ASSIGN(IitDbusAdaptor);

};

// namespace

#endif  // IIT_SDK_SRC_IOT_DBUS_ADAPTOR_H_

iit_dbus_adaptor.cc 

#include "iit_dbus_adaptor.h"

#include "iit_daemon.h"

namespace iit

{

const char kIitServiceName[] = "org.chromium.CrosIit";

const char kIitServicePath[] = "/org/chromium/CrosIit";

IitDbusAdaptor::IitDbusAdaptor(scoped_refptr<dbus::Bus> bus)

    : org::chromium::CrosIitAdaptor(this),

      dbus_object_(nullptr, bus, dbus::ObjectPath(kIitServicePath))

{

}

void IitDbusAdaptor::RegisterAsync(

    const brillo::dbus_utils::AsyncEventSequencer::CompletionAction &cb)

{

  RegisterWithDBusObject(&dbus_object_);

  dbus_object_.RegisterAsync(cb);

}

bool IitDbusAdaptor::StartIitaddService(             //此处是对基类方法调用的具体实现

     brillo::ErrorPtr *error,

     int32_t in_argA,

     int32_t in_argB)

{

  int32_t sum = 0;

  sum  = in_argA + in_argB;

  IitDaemon::GetInstance()->SendDataToClient(sum);

  return true;

}

//fire signal

void IitDbusAdaptor::ReceiveRemoteIitData( int32_t data)

{

  SendReceiveRemoteIitDataSignal(data);

}

// namespace

iit_daemon.h 

#ifndef IIT_SDK_SRC_IIT_DAEMON_H_

#define IIT_SDK_SRC_IIT_DAEMON_H_

#include <base/logging.h>

#include <brillo/daemons/dbus_daemon.h>

#include <brillo/syslog_logging.h>

#include <chromeos/libminijail.h>

#include "iit_dbus_adaptor.h"

namespace iit

{

class IitDaemon : public brillo::DBusServiceDaemon

{

public:

  static std::shared_ptr<IitDaemon> GetInstance();

  void SendDataToClient( int32_t data);

protected:

  void RegisterDBusObjectsAsync(

      brillo::dbus_utils::AsyncEventSequencer *sequencer) override;

private:

  IitDaemon();

  std::unique_ptr<iit::IitDbusAdaptor> adaptor_;

  static std::shared_ptr<IitDaemon> instance_;

  DISALLOW_COPY_AND_ASSIGN(IitDaemon);

};

//namespace

#endif // IOT_SDK_SRC_IOT_DAEMON_H_

iit_daemon.cc 

#include "iit_daemon.h"

namespace iit

{

std::shared_ptr<IitDaemon> IitDaemon::instance_ = nullptr;

IitDaemon::IitDaemon() : DBusServiceDaemon(iit::kIitServiceName) {}

std::shared_ptr<IitDaemon> IitDaemon::GetInstance()

{

  if (instance_ == nullptr)

  {

    instance_.reset(new IitDaemon());

  }

  return instance_;

}

void IitDaemon::SendDataToClient( int32_t data) {

  adaptor_->ReceiveRemoteIitData(data);

}

void IitDaemon::RegisterDBusObjectsAsync(

    brillo::dbus_utils::AsyncEventSequencer *sequencer)  //override

{

  adaptor_.reset(new iit::IitDbusAdaptor(bus_));

  adaptor_->RegisterAsync(sequencer->GetHandler(

      "RegisterAsync() failed."true));

}

}

我们仿写大佬的gyp,感谢前人们用躯体给菜鸟新人们铺成的道路。

iit-sdk.gyp 

{

  'variables': {

    'USE_target_arch_arm%': 0,

    'USE_target_arch_x86_64%': 0,

    'enable_werror': 0,

  },

  'target_defaults': {

    'variables': {

      'deps': [

        'gthread-2.0',

        'libbrillo-<(libbase_ver)',

        'libchrome-<(libbase_ver)',

      ],

      'enable_exceptions': 1,

    },

  },

  'targets': [

    {

      'target_name': 'iit-adaptors',

      'type': 'none',

      'variables': {

        'dbus_service_config': 'src/dbus_bindings/dbus-service-config.json',

        'dbus_adaptors_out_dir': 'include/iit-sdk/dbus_adaptors',

      },

      'sources': [

        'src/dbus_bindings/org.chromium.CrosIit.xml',

      ],

      'includes': ['../../platform2/common-mk/generate-dbus-adaptors.gypi'],

    },

    {

      'target_name': 'iit',

      'type': 'executable',

      'dependencies': [

        'iit-adaptors',

      ],

      'include_dirs': [

        'public/include/',

        'public/include/boost'

      ],

      'sources': [

        'src/main.cc',

        'src/iit_dbus_adaptor.cc',

        'src/iit_dbus_adaptor.h',

        'src/iit_daemon.cc',

        'src/iit_daemon.h',

      ],

      'conditions': [

        ['USE_target_arch_arm == 1', {

            'libraries': [

              '-L<!(pwd)/public/so/arm/',

              '-L<!(pwd)/public/a/arm/',

              '<!(pwd)/public/a/arm/libcrypto.a',

              '-lz',

              '-ldl',

              '-fopenmp',

              '-fpermissive',

            ],

          }

        ],

        ['USE_target_arch_x86_64 == 1', {

            'libraries': [

              '-L<!(pwd)/public/so/x86_64/',

              '-L<!(pwd)/public/a/x86_64/',

              '<!(pwd)/public/a/x86_64/libcrypto.a',

              '-lz',

              '-ldl',

              '-fopenmp',

              '-fpermissive',

            ],

          }

        ]

      ]

    }

  ]

}

上述gyp代码中实现了对XML的编译生成我们之前说到的org.chromium.CrosIit.h。

下面再次踩着前人的躯体进行ebuild仿写:

iit-sdk-9999.ebuild 

# Copyright 1999-2018 Gentoo Foundation

# Distributed under the terms of the GNU General Public License v2

  

EAPI=4

  

CROS_WORKON_LOCALNAME=("platform2" "third_party/iit-sdk")

CROS_WORKON_PROJECT=("chromiumos/platform2" "chromiumos/third_party/iit-sdk")

CROS_WORKON_DESTDIR=("${S}/platform2" "${S}/third_party/iit-sdk")

  

CROS_WORKON_INCREMENTAL_BUILD=1

  

PLATFORM_SUBDIR="iit-sdk"

  

inherit cros-workon platform user

  

DESCRIPTION="Seewo iot sdk and service in chromiumos"

SLOT="0"

LICENSE="BSD-Google"

KEYWORDS="*"

IUSE="static-libs"

SRC_URI=""

HOMEPAGE="https://gitlab.gz.cctv.cn/CrOS/system/chromiumos/third_party/iit-sdk"

  

RDEPEND="

    chromeos-base/libbrillo

    dev-libs/dbus-c++

    dev-libs/glib

"

  

DEPEND="${RDEPEND}

    sys-apps/dbus

    chromeos-base/system_api"

  

pkg_preinst() {

    enewuser "iit"

    enewgroup "iit"

}

  

src_unpack() {

    local s="${S}"

    platform_src_unpack

    S="${s}/third_party/iit-sdk"

}

  

src_install(){

    into /

    dosbin "${OUT}"/iit

      

    insinto /usr/lib

    doins public/so/libgomp.so.1

  

    # Install D-Bus config file.

    insinto /etc/dbus-1/system.d

    doins src/share/org.chromium.CrosIit.conf

     #通过添加upstart来开机启动此服务

#    insinto /etc/init

#    doins src/share/iit.conf

}

搞定上述文件后我们就可以emerge-coral_cctv 进行编译安装了。之后我们使用cros deploy向设备部署:

(cr) (c/c/dev-71) jq.bi@1fd8ac2f6306 ~/trunk/src/scripts $ cros deploy 172.18.88.28 iit-sdk

17:03:06: NOTICE: Cleaning outdated binary packages from /build/coral_cctv

17:03:08: NOTICE: These are the packages to emerge:

17:03:08: NOTICE: * 1) chromeos-base/iit-sdk-9999

 [####################################################################################] 100%

17:03:24: WARNING: Please restart any updated services on the device, or just reboot it.

部署到设备完成后我们重启seewobook后输入

localhost ~ # minijail0 -u iit -g iit -i -G -l -n /sbin/iit

 来运行此服务。之后再通过ps查看:

localhost ~ # ps -aux | grep iit

iit       2868  0.0  0.1  21724  5380 ?        Ss   17:04   0:00 /sbin/iit

root      2970  0.0  0.0   4376   884 pts/0    S+   17:09   0:00 grep --colour=auto iit

发现我们的dbus服务端是正常运行的,说明踩的前人的路是通畅的。

 类似资料: