将ubus call network.interface status 用C的API接口?
- 通俗点说,ubus就是一个用于进程间通讯的通用框架。ubus具备很强的可移植性,能够很方便的移植到其余Linux平台上使用。
- ubus模块被设计用于提供守护进程(daemons)和应用程序(applications)间的通信,包含了守护进程ubusd、库以及一些例子。ubusd能够认为是一个消息管理服务器(Server),须要通讯的进程能够经过提供的libubus使用ubus,而ubus又依赖于ubox。
- 主要思路:可以参考网上的相关的例程,尝试读懂个大概,模仿下即可,因为读懂了过几天可能会忘记,获取到数据后,后面繁琐的就是需要解析下JSON的数据,总体来说不是很难。
调用一个对象的子模块举例子
ubus call network.device status '{"name":"lo"}' 截取了lo设备的信息
ubus call network.interface status '{"interface":"lan"}' 等价于 ubus call network.interface.lan status
#include <libubox/blobmsg_json.h>
#include "uloop.h"
#include <string.h>
#include <stdlib.h>
#include <libubox/ustream.h>
#include <libubox/utils.h>
#include <libubus.h>
#include <libubox/blobmsg_json.h>
#include "cJSON.h"
#include "json.h"
static struct ubus_context * ctx = NULL;
static struct blob_buf b;
static const char * cli_path;
char *jsonstr = NULL;
enum
{
SCAN_CHNID,
SCAN_POLICY_MAX,
};
static const struct blobmsg_policy scan_policy[SCAN_POLICY_MAX] = {
[SCAN_CHNID] = {.name = "name", .type = BLOBMSG_TYPE_STRING},
};
static int timeout = 30;
static bool simple_output = false;
static void scanreq_prog_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
if (