ubus [2] - libubus

libubus

數據結構

struct ubus_event_handler {
    struct ubus_object obj;

    ubus_event_handler_t cb;
};

struct ubus_context {
    struct list_head requests;
    struct avl_tree objects;    /** client端object鏈表頭 */
    struct list_head pending;

    struct uloop_fd sock;       /** client端sock對象 */

    uint32_t local_id;          /** ubusd分配的client id */
    uint16_t request_seq;
    int stack_depth;

/** 斷開鏈接回調函數 */
    void (*connection_lost)(struct ubus_context *ctx);

    struct {
        struct ubus_msghdr hdr;
        char data[UBUS_MAX_MSGLEN];
    } msgbuf;                   /** 通訊報文 */
};

struct ubus_object_data {
    uint32_t id;
    uint32_t type_id;
    const char *path;
    struct blob_attr *signature;
};

struct ubus_request_data {
    uint32_t object;
    uint32_t peer;
    uint16_t seq;

    /* internal use */
    bool deferred;
    int fd;
};

struct ubus_request {
    struct list_head list;

    struct list_head pending;
    int status_code;
    bool status_msg;
    bool blocked;
    bool cancelled;
    bool notify;

    uint32_t peer;
    uint16_t seq;

    ubus_data_handler_t raw_data_cb;
    ubus_data_handler_t data_cb;
    ubus_fd_handler_t fd_cb;
    ubus_complete_handler_t complete_cb;

    struct ubus_context *ctx;
    void *priv;
};

struct ubus_notify_request {
    struct ubus_request req;

    ubus_notify_complete_handler_t status_cb;
    ubus_notify_complete_handler_t complete_cb;

    uint32_t pending;
    uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
};

struct ubus_auto_conn {
    struct ubus_context ctx;
    struct uloop_timeout timer;
    const char *path;
    ubus_connect_handler_t cb;
};

接口說明

/**
 * 初始化client端context結構,並鏈接ubusd
 */
struct ubus_context *ubus_connect(const char *path)

/**
 * 與ubus_connect()函數基本功能相同,但此函數在鏈接斷開後會自動進行重連
 */
void ubus_auto_connect(struct ubus_auto_conn *conn)

/**
 * 註冊新事件
 */
int ubus_register_event_handler(struct ubus_context *ctx,
                struct ubus_event_handler *ev,
                const char *pattern)

/**
 * 發出事件消息
 */
int ubus_send_event(struct ubus_context *ctx, const char *id,
            struct blob_attr *data)

/**
 * 向ubusd查詢指定UBUS_ATTR_OBJPATH對應對象信息內容
 * 內容經過輸入回調函數ubus_lookup_handler_t由調用者自行處理
 */
int ubus_lookup(struct ubus_context *ctx, const char *path,
        ubus_lookup_handler_t cb, void *priv)

/**
 * 向ubusd查詢指定UBUS_ATTR_OBJPATH對應的ID號
 */
int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)

libubus-io

接口說明

/**
 * libubus關注的報文中數據屬性列表
 */
static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
    [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
    [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
    [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
    [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
    [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
    [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
    [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
};

/**
 * 把libubus關注的無序報文轉化爲有序的blob_attr數組
 */
__hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)

/**
 * 發送報文
 * 
 * @param ctx    - client上下文對象
 * @param seq    - 報文順序號 hdr.seq
 * @param msg    - 報文內容 
 * @param cmd    - 報文類型 hdr.type
 * @param perr   -
 * @param fd     - 需傳遞給對端的描述符,等於-1表示不需傳遞
 */
int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
               struct blob_attr *msg, int cmd, uint32_t peer, int fd)

/**
 * client端fd收包處理函數
 */
void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)

/**
 * client端輪詢fd收包處理函數
 */
void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)

/**
 * client鏈接ubusd server
 */
int ubus_reconnect(struct ubus_context *ctx, const char *path)

libubus-obj

數據結構

struct ubus_object {
    struct avl_node avl;  /** 關係到struct ubus_context的objects */

    const char *name;     /** UBUS_ATTR_OBJPATH */
    uint32_t id;          /** 由ubusd server分配的obj id */

    const char *path;
    struct ubus_object_type *type;

/** 第1次被訂閱或最後1次補退訂時被調用 */
    ubus_state_handler_t subscribe_cb;
    bool has_subscribers;    /** 此對象是否被訂閱 */

    const struct ubus_method *methods;  /** 方法數組 */
    int n_methods;                      /** 方法數組個數 */
};

struct ubus_object_type {
    const char *name;
    uint32_t id;            /** 由ubusd server分配的obj type id */

    const struct ubus_method *methods;  /** 方法數組 */
    int n_methods;                      /** 方法數組個數 */
};

struct ubus_method {
    const char *name;         /** 方法名稱 */
    ubus_handler_t handler;   /** 方法處理回調函數 */

    unsigned long mask;                   /** 參數過濾掩碼 */
    const struct blobmsg_policy *policy;  /** 參數過濾列表 */
    int n_policy;                         /** 參數過濾列表個數 */
};

接口說明

/**
 * client端向ubusd server請求增長一個新object
 */
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)

/**
 * client端向ubusd server請求刪除一個object
 */
int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)

/**
 * 處理收到與object相關報文
 */
void __hidden ubus_process_obj_msg(struct ubus_context *ctx, 
                                   struct ubus_msghdr *hdr)

/**
 * 處理UBUS_MSG_INVOKE報文
 */
static void
ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
            struct ubus_object *obj, struct blob_attr **attrbuf)

/**
 * 處理UBUS_MSG_UNSUBSCRIBE報文
 */
static void
ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
             struct ubus_object *obj, struct blob_attr **attrbuf)

/**
 * 處理UBUS_MSG_NOTIFY報文
 */
static void
ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
            struct ubus_object *obj, struct blob_attr **attrbuf)

libubus-sub

數據結構

struct ubus_subscriber {
    struct ubus_object obj;

    ubus_handler_t cb;
    ubus_remove_handler_t remove_cb;
};

接口說明

/**
 * 
 */
int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *s)

/**
 * 
 */
int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, 
                   uint32_t id)

/**
 *
 */
int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj,
                     uint32_t id)

libubus-req

數據結構

struct ubus_request_data {
    uint32_t object;
    uint32_t peer;
    uint16_t seq;

    /* internal use */
    bool deferred;
    int fd;
};

struct ubus_request {
    struct list_head list;

    struct list_head pending;
    int status_code;
    bool status_msg;
    bool blocked;
    bool cancelled;
    bool notify;

    uint32_t peer;
    uint16_t seq;

    ubus_data_handler_t raw_data_cb;
    ubus_data_handler_t data_cb;
    ubus_fd_handler_t fd_cb;
    ubus_complete_handler_t complete_cb;

    struct ubus_context *ctx;
    void *priv;
};

struct ubus_notify_request {
    struct ubus_request req;

    ubus_notify_complete_handler_t status_cb;
    ubus_notify_complete_handler_t complete_cb;

    uint32_t pending;
    uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
};

接口說明

/**
 * 發送迴應信息,消息類型UBUS_MSG_DATA
 */
int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
            struct blob_attr *msg)

/**
 * 異步通知指定object執行其方法
 */
int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
                       struct blob_attr *msg, struct ubus_request *req)

/**
 * 同步通知指定object執行其方法
 */

int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
                struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
        int timeout)

/**
 * 異步發出通知消息
 */
int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
              const char *type, struct blob_attr *msg,
              struct ubus_notify_request *req)

/**
 * 同步發出通知消息
 */
int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
        const char *type, struct blob_attr *msg, int timeout)

例子

向ubusd註冊新object

定義object方法:node

enum {
    OBJ_SET_ARG1,
    OBJ_SET_ARG2,
    __OBJ_SET_ATTR_MAX
};

/** 定義set方法參數列表 */
static const struct blobmsg_policy obj_set_attrs[__OBJ_SET_ATTR_MAX] = {
    [OBJ_SET_ARG1] = { .name = "arg1", .type = BLOBMSG_TYPE_STRING },
    [OBJ_SET_ARG2 ] = { .name = "arg2", .type = BLOBMSG_TYPE_STRING },
};

static struct ubus_method obj_methods[] = {
    { .name = "enable", .handler = obj_enable },
    UBUS_METHOD("set", obj_set, obj_set_attrs),
    { .name = "dump", .handler = obj_dump },
};

定義object類型:數組

static struct ubus_object_type obj_type = 
    UBUS_OBJECT_TYPE("my_obj", obj_methods);

定義object:數據結構

static struct ubus_object obj = {
    .name = "myobj",
    .type = &obj_type,
    .methods = obj_methods,
    .n_methods = ARRAR_SIZE(obj_methods),
};

註冊新object:異步

uloop_init();

    struct ubus_context *ubus_ctx = ubus_connect(NULL);
    ubus_add_uloop(ubus_ctx);
    ubus_add_object(ubus_ctx, &obj);

    uloop_run();

向ubusd註冊事件監聽

定義事件觸發回調方法:async

static void
event_receive_cb(struct ubus_context *ctx, struct ubus_event_handler *ev, 
                const char *type, struct blob_attr *msg)
{
    enum {
        EV_ACTION,
        EV_IFNAME,
        __EV_MAX
    };
    static const struct blobmsg_policy ev_policy[__EV_MAX] = {
        [EV_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
        [EV_IFNAME] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
    };
    struct blob_attr *tb[__EV_MAX];

    blobmsg_parse(ev_policy, __EV_MAX, tb, blob_data(msg), blob_len(msg));

    /* do something */
}

註冊監聽事件函數

static void
event_listen(void)
{
    static struct ubus_event_handler listener;
    memset(&listener, 0, sizeof(listener));

    /** 監聽netwrok.interface事件 */
    ubus_register_event_handler(ubus_ctx, &listener, "network.interface");
}

向ubusd發送命令

定義命令返回回調方法:oop

static void
command_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
    if (!msg)
        return;

    enum {
        ADDR_IPV4,
        __ADDR_MAX,
    };
    static const struct blobmsg_policy policy[__ADDR_MAX] = {
        [ADDR_IPV4] = { .name = "ipv4-address", .type = BLOBMSG_TYPE_ARRAY },
    };
    struct blob_attr *tb[__ADDR_MAX];

    blobmsg_parse(policy, __ADDR_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));

    /** do something */
}

發送命令:ui

static void
invoke_command(char *net)
{
    uint32_t id;
    char path[64] = {0};

    sprintf(path, "network.interface.%s", net);

    /** lookup `network.interface.%s` object id */
    ubus_lookup_id(ubus_ctx, path, &id);

    /** invoke command `status` */
    ubus_invoke(ubus_ctx, id, "status", NULL, command_cb, NULL, 500);
}
相關文章
相關標籤/搜索