2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <sys/utsname.h>
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
23 #include <libubox/uloop.h>
28 #define HOSTNAME_PATH "/proc/sys/kernel/hostname"
30 static struct blob_buf b
;
31 static char *board_name
;
32 static char *board_model
;
34 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
35 struct ubus_request_data
*req
, const char *method
,
36 struct blob_attr
*msg
)
42 if (board_name
&& board_model
) {
43 blobmsg_add_string(&b
, "boardname", board_name
);
44 blobmsg_add_string(&b
, "boardmodel", board_model
);
47 blobmsg_add_string(&b
, "hostname", uts
.nodename
);
48 blobmsg_add_string(&b
, "machine", uts
.machine
);
49 blobmsg_add_string(&b
, "kernel", uts
.release
);
51 if (!clock_gettime(CLOCK_MONOTONIC
, &ts
))
52 blobmsg_add_u32(&b
, "uptime", ts
.tv_sec
);
53 ubus_send_reply(ctx
, req
, b
.head
);
64 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
65 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
66 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
69 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
70 struct ubus_request_data
*req
, const char *method
,
71 struct blob_attr
*msg
)
73 struct blob_attr
*tb
[__WDT_MAX
];
76 return UBUS_STATUS_INVALID_ARGUMENT
;
78 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
79 if (tb
[WDT_FREQUENCY
]) {
80 unsigned int timeout
= watchdog_timeout(0);
81 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
84 if (freq
> timeout
/ 2)
86 watchdog_frequency(freq
);
90 if (tb
[WDT_TIMEOUT
]) {
91 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
92 unsigned int frequency
= watchdog_frequency(0);
94 if (timeout
<= frequency
)
95 timeout
= frequency
* 2;
96 watchdog_timeout(timeout
);
100 blobmsg_add_string(&b
, "status", (watchdog_fd() >= 0) ? ("running") : ("offline"));
101 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
102 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
103 ubus_send_reply(ctx
, req
, b
.head
);
108 static const struct ubus_method system_methods
[] = {
109 UBUS_METHOD_NOARG("info", system_info
),
110 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
113 static struct ubus_object_type system_object_type
=
114 UBUS_OBJECT_TYPE("system", system_methods
);
116 static struct ubus_object system_object
= {
118 .type
= &system_object_type
,
119 .methods
= system_methods
,
120 .n_methods
= ARRAY_SIZE(system_methods
),
123 static char* load_file_content(const char *file
)
128 fd
= open(file
, O_RDONLY
);
131 r
= read(fd
, buf
, sizeof(buf
) - 1);
135 if (buf
[r
- 1] == '\n')
143 void ubus_init_system(struct ubus_context
*ctx
)
148 board_model
= load_file_content("/tmp/sysinfo/model");
150 board_name
= load_file_content("/tmp/sysinfo/board_name");
151 ret
= ubus_add_object(ctx
, &system_object
);
153 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));