+
+ if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL ||
+ (f = fopen("/proc/device-tree/model", "r")) != NULL)
+ {
+ if (fgets(line, sizeof(line), f))
+ {
+ val = strtok(line, "\t\n");
+
+ if (val)
+ blobmsg_add_string(&b, "model", val);
+ }
+
+ fclose(f);
+ }
+ else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
+ {
+ while(fgets(line, sizeof(line), f))
+ {
+ key = strtok(line, "\t:");
+ val = strtok(NULL, "\t\n");
+
+ if (!key || !val)
+ continue;
+
+ if (!strcasecmp(key, "machine") ||
+ !strcasecmp(key, "hardware"))
+ {
+ blobmsg_add_string(&b, "model", val + 2);
+ break;
+ }
+ }
+
+ fclose(f);
+ }
+
+ if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
+ {
+ c = blobmsg_open_table(&b, "release");
+
+ while (fgets(line, sizeof(line), f))
+ {
+ char *dest;
+ char ch;
+
+ key = line;
+ val = strchr(line, '=');
+ if (!val)
+ continue;
+
+ *(val++) = 0;
+
+ if (!strcasecmp(key, "DISTRIB_ID"))
+ key = "distribution";
+ else if (!strcasecmp(key, "DISTRIB_RELEASE"))
+ key = "version";
+ else if (!strcasecmp(key, "DISTRIB_REVISION"))
+ key = "revision";
+ else if (!strcasecmp(key, "DISTRIB_CODENAME"))
+ key = "codename";
+ else if (!strcasecmp(key, "DISTRIB_TARGET"))
+ key = "target";
+ else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
+ key = "description";
+ else
+ continue;
+
+ dest = blobmsg_alloc_string_buffer(&b, key, strlen(val));
+ if (!dest) {
+ ERROR("Failed to allocate blob.\n");
+ continue;
+ }
+
+ while (val && (ch = *(val++)) != 0) {
+ switch (ch) {
+ case '\'':
+ case '"':
+ next = strchr(val, ch);
+ if (next)
+ *next = 0;
+
+ strcpy(dest, val);
+
+ if (next)
+ val = next + 1;
+
+ dest += strlen(dest);
+ break;
+ case '\\':
+ *(dest++) = *(val++);
+ break;
+ }
+ }
+ blobmsg_add_string_buffer(&b);
+ }
+
+ blobmsg_close_array(&b, c);
+
+ fclose(f);
+ }
+
+ ubus_send_reply(ctx, req, b.head);
+
+ return UBUS_STATUS_OK;
+}
+
+static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ time_t now;
+ struct tm *tm;
+#ifdef linux
+ struct sysinfo info;
+ void *c;
+
+ if (sysinfo(&info))
+ return UBUS_STATUS_UNKNOWN_ERROR;
+#endif
+
+ now = time(NULL);
+
+ if (!(tm = localtime(&now)))
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ blob_buf_init(&b, 0);
+
+ blobmsg_add_u32(&b, "localtime", now + tm->tm_gmtoff);
+
+#ifdef linux
+ blobmsg_add_u32(&b, "uptime", info.uptime);
+
+ c = blobmsg_open_array(&b, "load");
+ blobmsg_add_u32(&b, NULL, info.loads[0]);
+ blobmsg_add_u32(&b, NULL, info.loads[1]);
+ blobmsg_add_u32(&b, NULL, info.loads[2]);
+ blobmsg_close_array(&b, c);
+
+ c = blobmsg_open_table(&b, "memory");
+ blobmsg_add_u64(&b, "total", info.mem_unit * info.totalram);
+ blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram);
+ blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram);
+ blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram);
+ blobmsg_close_table(&b, c);
+
+ c = blobmsg_open_table(&b, "swap");
+ blobmsg_add_u64(&b, "total", info.mem_unit * info.totalswap);
+ blobmsg_add_u64(&b, "free", info.mem_unit * info.freeswap);
+ blobmsg_close_table(&b, c);
+#endif
+