Bläddra i källkod

Add utility library libgluonutil

Matthias Schiffer 8 år sedan
förälder
incheckning
840d07dd48

+ 35 - 0
package/libgluonutil/Makefile

@@ -0,0 +1,35 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=libgluonutil
+PKG_VERSION:=1
+CMAKE_INSTALL:=1
+
+PKG_LICENSE:=BSD-2-Clause
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+include $(INCLUDE_DIR)/cmake.mk
+
+define Package/libgluonutil
+  SECTION:=libs
+  CATEGORY:=Libraries
+  TITLE:=Gluon utility library
+  DEPENDS:=+libjson-c
+endef
+
+CMAKE_OPTIONS += \
+	-DCMAKE_BUILD_TYPE:String="MINSIZEREL"
+
+
+define Build/Prepare
+	mkdir -p $(PKG_BUILD_DIR)
+	$(CP) ./src/* $(PKG_BUILD_DIR)/
+endef
+
+define Package/libgluonutil/install
+	$(INSTALL_DIR) $(1)/usr/lib
+	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/libgluonutil.so $(1)/usr/lib/
+endef
+
+$(eval $(call BuildPackage,libgluonutil))

+ 24 - 0
package/libgluonutil/src/CMakeLists.txt

@@ -0,0 +1,24 @@
+cmake_minimum_required(VERSION 2.6)
+
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
+
+project(libgluonutil C)
+
+set(LIBDIR "lib${LIB_SUFFIX}")
+
+find_package(JSON_C REQUIRED)
+
+set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS _GNU_SOURCE)
+
+add_library(gluonutil SHARED libgluonutil.c)
+set_property(TARGET gluonutil PROPERTY COMPILE_FLAGS "-Wall -std=c99 ${JSON_C_CFLAGS_OTHER}")
+set_property(TARGET gluonutil PROPERTY LINK_FLAGS "${JSON_C_LDFLAGS_OTHER}")
+set_property(TARGET gluonutil APPEND PROPERTY INCLUDE_DIRECTORIES ${JSON_C_INCLUDE_DIR})
+target_link_libraries(gluonutil ${JSON_C_LIBRARIES})
+install(TARGETS gluonutil
+  ARCHIVE DESTINATION ${LIBDIR}
+  LIBRARY DESTINATION ${LIBDIR}
+)
+
+
+install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/libgluonutil.h DESTINATION include)

+ 20 - 0
package/libgluonutil/src/FindJSON_C.cmake

@@ -0,0 +1,20 @@
+# Defines the following variables:
+#  JSON_C_FOUND
+#  JSON_C_INCLUDE_DIR
+#  JSON_C_LIBRARIES
+#  JSON_C_CFLAGS_OTHER
+#  JSON_C_LDFLAGS_OTHER
+
+
+find_package(PkgConfig REQUIRED QUIET)
+
+pkg_check_modules(_JSON_C json-c)
+
+find_path(JSON_C_INCLUDE_DIR NAMES json-c/json.h HINTS ${_JSON_C_INCLUDE_DIRS})
+find_library(JSON_C_LIBRARIES NAMES json-c HINTS ${_JSON_C_LIBRARY_DIRS})
+
+set(JSON_C_CFLAGS_OTHER "${_JSON_C_CFLAGS_OTHER}" CACHE STRING "Additional compiler flags for json-c")
+set(JSON_C_LDFLAGS_OTHER "${_JSON_C_LDFLAGS_OTHER}" CACHE STRING "Additional linker flags for json-c")
+
+find_package_handle_standard_args(JSON_C REQUIRED_VARS JSON_C_LIBRARIES JSON_C_INCLUDE_DIR)
+mark_as_advanced(JSON_C_INCLUDE_DIR JSON_C_LIBRARIES JSON_C_CFLAGS_OTHER JSON_C_LDFLAGS_OTHER)

+ 114 - 0
package/libgluonutil/src/libgluonutil.c

@@ -0,0 +1,114 @@
+/*
+  Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
+  All rights reserved.
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions are met:
+
+    1. Redistributions of source code must retain the above copyright notice,
+       this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright notice,
+       this list of conditions and the following disclaimer in the documentation
+       and/or other materials provided with the distribution.
+
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#include "libgluonutil.h"
+
+#include <json-c/json.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+char * gluonutil_read_line(const char *filename) {
+	FILE *f = fopen(filename, "r");
+	if (!f)
+		return NULL;
+
+	char *line = NULL;
+	size_t len = 0;
+
+	ssize_t r = getline(&line, &len, f);
+
+	fclose(f);
+
+	if (r >= 0) {
+		len = strlen(line);
+
+		if (len && line[len-1] == '\n')
+			line[len-1] = 0;
+	}
+	else {
+		free(line);
+		line = NULL;
+	}
+
+	return line;
+}
+
+char * gluonutil_get_sysconfig(const char *key) {
+	if (strchr(key, '/'))
+		return NULL;
+
+	const char prefix[] = "/lib/gluon/core/sysconfig/";
+	char path[strlen(prefix) + strlen(key) + 1];
+	snprintf(path, sizeof(path), "%s%s", prefix, key);
+
+	return gluonutil_read_line(path);
+}
+
+char * gluonutil_get_node_id(void) {
+	char *node_id = gluonutil_get_sysconfig("primary_mac");
+	if (!node_id)
+		return NULL;
+
+	char *in = node_id, *out = node_id;
+
+	do {
+		if (*in != ':')
+			*out++ = *in;
+	} while (*in++);
+
+	return node_id;
+}
+
+char * gluonutil_get_interface_address(const char *ifname) {
+	const char *format = "/sys/class/net/%s/address";
+	char path[strlen(format) + strlen(ifname) - 1];
+
+	snprintf(path, sizeof(path), format, ifname);
+
+	return gluonutil_read_line(path);
+}
+
+
+
+struct json_object * gluonutil_wrap_string(const char *str) {
+	if (!str)
+		return NULL;
+
+	return json_object_new_string(str);
+}
+
+struct json_object * gluonutil_wrap_and_free_string(char *str) {
+	struct json_object *ret = gluonutil_wrap_string(str);
+	free(str);
+	return ret;
+}
+
+struct json_object * gluonutil_load_site_config(void) {
+	return json_object_from_file("/lib/gluon/site.json");
+}

+ 40 - 0
package/libgluonutil/src/libgluonutil.h

@@ -0,0 +1,40 @@
+/*
+  Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
+  All rights reserved.
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions are met:
+
+    1. Redistributions of source code must retain the above copyright notice,
+       this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright notice,
+       this list of conditions and the following disclaimer in the documentation
+       and/or other materials provided with the distribution.
+
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#ifndef _LIBGLUON_LIBGLUON_H_
+#define _LIBGLUON_LIBGLUON_H_
+
+char * gluonutil_read_line(const char *filename);
+char * gluonutil_get_sysconfig(const char *key);
+char * gluonutil_get_node_id(void);
+char * gluonutil_get_interface_address(const char *ifname);
+
+struct json_object * gluonutil_wrap_string(const char *str);
+struct json_object * gluonutil_wrap_and_free_string(char *str);
+
+struct json_object * gluonutil_load_site_config(void);
+
+#endif /* _LIBGLUON_LIBGLUON_H_ */