From: David Plotek Date: Tue, 15 Jul 2008 12:05:41 +0000 (+0200) Subject: Initial commit X-Git-Url: https://rtime.felk.cvut.cz/gitweb/tiny-bt.git/commitdiff_plain/98aebfe5eea84921ee638922085f6569142a2ccc Initial commit --- 98aebfe5eea84921ee638922085f6569142a2ccc diff --git a/Doxyfile b/Doxyfile new file mode 100644 index 0000000..46d1906 --- /dev/null +++ b/Doxyfile @@ -0,0 +1,283 @@ +# Doxyfile 1.5.1-KDevelop + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +PROJECT_NAME = bluetproj +PROJECT_NUMBER = 1 +OUTPUT_DIRECTORY = +CREATE_SUBDIRS = NO +OUTPUT_LANGUAGE = English +USE_WINDOWS_ENCODING = NO +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = /root/ +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = NO +MULTILINE_CPP_IS_BRIEF = NO +DETAILS_AT_TOP = NO +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 8 +ALIASES = +OPTIMIZE_OUTPUT_FOR_C = NO +OPTIMIZE_OUTPUT_JAVA = NO +BUILTIN_STL_SUPPORT = NO +DISTRIBUTE_GROUP_DOC = NO +SUBGROUPING = YES +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = NO +EXTRACT_PRIVATE = NO +EXTRACT_STATIC = NO +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = YES +HIDE_SCOPE_NAMES = NO +SHOW_INCLUDE_FILES = YES +INLINE_INFO = YES +SORT_MEMBER_DOCS = YES +SORT_BRIEF_DOCS = NO +SORT_BY_SCOPE_NAME = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_DIRECTORIES = NO +FILE_VERSION_FILTER = +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = NO +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = NO +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = /home/pes/bluetproj +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.d \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.idl \ + *.odl \ + *.cs \ + *.php \ + *.php3 \ + *.inc \ + *.m \ + *.mm \ + *.dox \ + *.py \ + *.C \ + *.CC \ + *.C++ \ + *.II \ + *.I++ \ + *.H \ + *.HH \ + *.H++ \ + *.CS \ + *.PHP \ + *.PHP3 \ + *.M \ + *.MM \ + *.PY \ + *.C \ + *.H \ + *.tlh \ + *.diff \ + *.patch \ + *.moc \ + *.xpm \ + *.dox +RECURSIVE = yes +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = +EXAMPLE_PATH = +EXAMPLE_PATTERNS = * +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = NO +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = YES +REFERENCES_RELATION = YES +REFERENCES_LINK_SOURCE = YES +USE_HTAGS = NO +VERBATIM_HEADERS = YES +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = NO +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = html +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_ALIGN_MEMBERS = YES +GENERATE_HTMLHELP = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +BINARY_TOC = NO +TOC_EXPAND = NO +DISABLE_INDEX = NO +ENUM_VALUES_PER_LINE = 4 +GENERATE_TREEVIEW = NO +TREEVIEW_WIDTH = 250 +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = YES +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4wide +EXTRA_PACKAGES = +LATEX_HEADER = +PDF_HYPERLINKS = NO +USE_PDFLATEX = NO +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_LINKS = NO +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = yes +XML_OUTPUT = xml +XML_SCHEMA = +XML_DTD = +XML_PROGRAMLISTING = YES +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = bluetproj.tag +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +PERL_PATH = /usr/bin/perl +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +CLASS_DIAGRAMS = YES +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = NO +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = NO +CALLER_GRAPH = NO +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DOT_IMAGE_FORMAT = png +DOT_PATH = +DOTFILE_DIRS = +MAX_DOT_GRAPH_WIDTH = 1024 +MAX_DOT_GRAPH_HEIGHT = 1024 +MAX_DOT_GRAPH_DEPTH = 1000 +DOT_TRANSPARENT = NO +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- +SEARCHENGINE = NO diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b22a357 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/Makefile.omk b/Makefile.omk new file mode 100644 index 0000000..1bfdcf4 --- /dev/null +++ b/Makefile.omk @@ -0,0 +1 @@ +SUBDIRS=src diff --git a/Makefile.rules b/Makefile.rules new file mode 100644 index 0000000..6f9e6eb --- /dev/null +++ b/Makefile.rules @@ -0,0 +1,1217 @@ +# Version for Linux/RTLinux builds. #OMK@linux +# +# Makefile.rules - OCERA make framework common project rules -*- makefile -*- #OMK@base +# +# (C) Copyright 2003 by Pavel Pisa - OCERA team member +# (C) Copyright 2006 by Michal Sojka - Czech Technical University, FEE, DCE +# +# Homepage: http://rtime.felk.cvut.cz/omk/ +# +# The OMK build system is distributed under the GNU General Public +# License. See file COPYING for details. +# +# input variables +# V .. if set to 1, full command text is shown else short form is used +# W .. whole tree - if set to 1, make is always called from the top-level directory +# SUBDIRS .. list of subdirectories intended for make from actual directory +# default_CONFIG .. list of default config assignments CONFIG_XXX=y/n ... +# LN_HEADERS .. if "y", header files are symbolicaly linked instead of copied. #OMK@include +# #OMK@linux +# input variables +# lib_LIBRARIES .. list of the user-space libraries +# shared_LIBRARIES .. list of the user-space shared libraries +# kernel_LIBRARIES .. list of the kernel-space libraries +# rtlinux_LIBRARIES.. list of the RT-Linux kernel-space libraries +# include_HEADERS .. list of the user-space public header files +# nobase_include_HEADERS .. public headers copied even with directory part +# renamed_include_HEADERS .. public headers copied to the different target name +# kernel_HEADERS .. list of the kernel-space public header files +# rtlinux_HEADERS .. list of the RT-Linux kernel-space public header files +# bin_PROGRAMS .. list of the require binary programs +# utils_PROGRAMS .. list of the development utility programs +# test_PROGRAMS .. list of the testing programs +# kernel_MODULES .. list of the kernel side modules/applications +# rtlinux_MODULES .. list of RT-Linux the kernel side modules/applications +# xxx_SOURCES .. list of specific target sources +# INCLUDES .. additional include directories and defines for user-space +# kernel_INCLUDES .. additional include directories and defines for kernel-space +# rtlinux_INCLUDES .. additional include directories and defines for RT-Linux +# OMIT_KERNEL_PASSES if defined, all kernel passes are omited +# +# LINUX_DIR .. location of Linux kernel sources +# RTL_DIR .. location of RT-Linux sources +# CFLAGS .. C compiler flags +# CXXFLAGS .. C++ compiler flags +# LOCAL_CONFIG_H .. name of local config.h file generated from values #OMK@config_h +# of options defined in the current directory +# config_include_HEADERS .. names of global config files (possibly +# with subdirectories) +# xxx_DEFINES .. list of config directives to be included in +# config header file of the name /xxx.h +# DOXYGEN .. if non-empty, generated headers includes Doxygen's @file +# command, so it is possible to document config +# variables. +# QT_SUBDIRS .. subdirectories where to build QT applications using qmake #OMK@qt +# QTDIR .. where QT resides + #OMK@base +# We need to ensure definition of sources directory first +ifndef SOURCES_DIR +# Only shell built-in pwd understands -L +SOURCES_DIR := $(shell ( pwd -L ) ) +endif + +# If we are not called by OMK leaf Makefile... +ifndef MAKERULES_DIR +MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST)))) +endif + +.PHONY: all default check-make-ver omkize + +ifdef W + ifeq ("$(origin W)", "command line") + OMK_WHOLE_TREE:=$(W) + endif +endif +ifndef OMK_WHOLE_TREE + OMK_WHOLE_TREE:=0 +endif + +ifneq ($(OMK_WHOLE_TREE),1) +all: check-make-ver default + @echo "Compilation finished" +else +# Run make in the top-level directory +all: + @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0 +endif + +ifdef OMK_TESTSROOT +# Usage: $(call canttest,) +define canttest + ( echo "$(1)" > $(MAKERULES_DIR)/_canttest; echo "$(1)"; exit 1 ) +endef +else +define canttest + echo "$(1)" +endef +endif + +#========================= +# Include the config file + +ifneq ($(CONFIG_FILE_OK),y) +ifndef CONFIG_FILE +CONFIG_FILE := $(MAKERULES_DIR)/config.omk +endif +ifneq ($(wildcard $(CONFIG_FILE)-default),) +-include $(CONFIG_FILE)-default +else +ifneq ($(MAKECMDGOALS),default-config) +$(warning Please, run "make default-config" first) +endif +endif + +-include $(MAKERULES_DIR)/config.target + +ifneq ($(wildcard $(CONFIG_FILE)),) +include $(CONFIG_FILE) +CONFIG_FILE_OK = y +endif +endif #$(CONFIG_FILE_OK) + +export SOURCES_DIR MAKERULES_DIR RELATIVE_DIR +export CONFIG_FILE OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT +# OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes +# must to be serialized only in the toplevel make. + +ifndef RELATIVE_DIR +RELATIVE_DIR := $(SOURCES_DIR:$(MAKERULES_DIR)%=%) +endif +#$(warning === RELATIVE_DIR = "$(RELATIVE_DIR)" ===) +override RELATIVE_DIR := $(RELATIVE_DIR:/%=%) +override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%) +#$(warning RELATIVE_DIR = "$(RELATIVE_DIR)") +override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | sed -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__') +#$(warning BACK2TOP_DIR = "$(BACK2TOP_DIR)") + +#$(warning SOURCES_DIR = "$(SOURCES_DIR)") +#$(warning MAKERULES_DIR = "$(MAKERULES_DIR)") +#$(warning RELATIVE_DIR = "$(RELATIVE_DIR)") + +# We have to use RELATIVE_PREFIX because of mingw +override RELATIVE_PREFIX := $(RELATIVE_DIR)/ +override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%) + +#vpath %.c $(SOURCES_DIR) +#vpath %.cc $(SOURCES_DIR) +#vpath %.cxx $(SOURCES_DIR) + +# Define srcdir for Automake compatibility +srcdir = $(SOURCES_DIR) + +# Defines for quiet compilation +ifdef V + ifeq ("$(origin V)", "command line") + OMK_VERBOSE = $(V) + endif +endif +ifndef OMK_VERBOSE + OMK_VERBOSE = 0 +endif +ifneq ($(OMK_VERBOSE),0) + Q = +else + Q = @ +endif +ifneq ($(findstring s,$(MAKEFLAGS)),) + QUIET_CMD_ECHO = true + OMK_SILENT = 1 +else + QUIET_CMD_ECHO = echo +endif + +MAKEFILE_OMK=Makefile.omk +# All subdirectories (even linked ones) containing Makefile.omk +# Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS) +ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK)))) + +# =================================================================== +# We have set up all important variables, so we can check and include +# real OCERA style Makefile.omk now +ifndef OMK_INCLUDED +include $(SOURCES_DIR)/$(MAKEFILE_OMK) +OMK_INCLUDED := 1 +endif + +check-make-ver: + @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | sed -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \ + if [ x$$GOOD_MAKE_VERSION != xy ] ; then \ + echo "Your make program version is too old and does not support OMK system." ; \ + echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \ + fi + +distclean dist-clean: + @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)" + @rm -fr $(MAKERULES_DIR)/$(COMPILED_DIR_NAME) $(MAKERULES_DIR)/$(BUILD_DIR_NAME) + +# Common OMK templates +# ==================== + +# Syntax: $(call mkdir,) +define mkdir_def + [ -d $(1) ] || mkdir -p $(1) || exit 1 +endef + +ifneq ($(V),2) +NO_PRINT_DIRECTORY := --no-print-directory +endif + +ifeq ($(USE_LEAF_MAKEFILES),n) +export USE_LEAF_MAKEFILES +SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules +SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules +else +SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(dir)/Makefile +SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile +endif + +ifdef OMK_TESTSROOT +check-target = $$(@:%=%-check) +endif + +# Syntax: $(call omk_pass_template,,,[],[]) +define omk_pass_template +.PHNOY: $(1) $(addsuffix -local,$(1)) $(addsuffix -check,$(1)) +$(1): + +@$(foreach dir,$(SUBDIRS),$(call mkdir_def,$(2)/$(dir)); \ + $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) $(NO_PRINT_DIRECTORY) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir) -C $(2)/$(dir) \ + -f $(SUBDIR_MAKEFILE) $$@ || exit 1 ;) true +ifneq ($(4),) + @echo "make[omk]: $$@ in $(RELATIVE_DIR)"; \ + $(call mkdir_def,$(2)); \ + $(MAKE) $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \ + -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $$(@:%=%-local) +endif +endef + +# ======================= +# DEFAULT CONFIG PASS + +default-config: + @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default" + @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default" + @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default" + @echo >> "$(CONFIG_FILE)-default" + @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(MAKERULES_DIR) \ + RELATIVE_DIR="" SOURCES_DIR=$(MAKERULES_DIR) \ + -f $(MAKERULES_DIR)/Makefile default-config-pass + +$(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always)) + +default-config-pass-local: +# @echo Default config for $(RELATIVE_DIR) + @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default" + @$(foreach x, $(default_CONFIG), echo '$(x)' | \ + sed -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; ) + + +omkize: + $(Q)if ! grep -q MAKERULES_DIR Makefile; then \ + echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \ + fi + $(Q)for i in `find -L . -name Makefile.omk` ; do \ + i=`dirname $${i}`; \ + if [ x"$$i" != x"." ]; then \ + rm -f $${i}/Makefile; \ + cp Makefile $${i}/Makefile; \ + fi \ + done +ifeq ($(OMK_VERBOSE),1) #OMK@include +CPHEADER_FLAGS += -v +LNHEADER_FLAGS += -v +endif + +ifneq ($(LN_HEADERS),y) +define cp_cmd +( echo " CP $(1:$(MAKERULES_DIR)/%=%) -> $(2:$(MAKERULES_DIR)/%=%)"; cp $(CPHEADER_FLAGS) $(1) $(2) ) +endef +else +define cp_cmd +( echo " LN $(1:$(MAKERULES_DIR)/%=%) -> $(2:$(MAKERULES_DIR)/%=%)"; [ -f $(1) ] && ln -sf $(LNHEADER_FLAGS) $(1) $(2) ) +endef +endif + +# TODO: Check modification date of changed header files. If it is +# newer that in source dir, show a warning. + +# Syntax: $(call include-pass-template,,) +define include-pass-template + @$(foreach f, $($(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(notdir $(f)) \ + || $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(notdir $(f))) || exit 1 ; ) + @$(foreach f, $(nobase_$(2)_HEADERS), cmp --quiet $(SOURCES_DIR)/$(f) $(1)/$(f) \ + || ( mkdir -p $(1)/$(dir $(f)) && $(call cp_cmd,$(SOURCES_DIR)/$(f),$(1)/$(f)) ) || exit 1 ; ) + @$(foreach f, $(renamed_$(2)_HEADERS), \ + srcfname=`echo '$(f)' | sed -e 's/^\(.*\)->.*$$/\1/'` ; destfname=`echo '$(f)' | sed -e 's/^.*->\(.*\)$$/\1/'` ; \ + cmp --quiet $(SOURCES_DIR)/$${srcfname} $(1)/$${destfname} \ + || ( mkdir -p `dirname $(1)/$${destfname}` && $(call cp_cmd,$(SOURCES_DIR)/$${srcfname},$(1)/$${destfname}) ) || exit 1 ; ) +endef + +# Local Variables: +# mode:makefile +# End: + #OMK@linux +# Hack to check RT-Linux rules +#LINUX_DIR := /home/cvs/ocera/ocera-build/kernel/linux +#RTL_DIR := /home/cvs/ocera/ocera-build/kernel/rtlinux +#CONFIG_RTLINUX = y +#OCERA_DIR := $(shell ( cd -L $(MAKERULES_DIR)/../../.. ; pwd -L ) ) + +-include $(MAKERULES_DIR)/OCERA_TOP_DIR + +BUILD_DIR_NAME = _build +COMPILED_DIR_NAME = _compiled +ifndef GROUP_DIR_NAME +GROUP_DIR_NAME = nogroup +endif + +ifdef OCERA_DIR +ifeq ($(wildcard $(OCERA_DIR)/ocera.mk),) +$(warning "ocera.mk" file does not exist. Adapt Makefile.rules for standalone compilation) +$(warning (comment out definition of OCERA_DIR line and optionally select RTL_DIR) ) +$(error or go to the ocera/ directory and do 'make' to generate the "ocera.mk" file first, please) +endif +include $(OCERA_DIR)/ocera.mk +KERN_INCLUDE_DIR := $(OCERA_KERNEL_INCLUDES_DIR) +KERN_LIB_DIR := $(OCERA_KERNEL_LIBRARIES_DIR) +KERN_MODULES_DIR := $(OCERA_MODULES_DIR) +KERN_BUILD_DIR := $(BUILD_DIR)/kern/$(GROUP_DIR_NAME) +KERN_MODPOST_DIR := $(BUILD_DIR)/kern-modpost +USER_INCLUDE_DIR := $(OCERA_USER_INCLUDES_DIR) +USER_LIB_DIR := $(OCERA_USER_LIBRARIES_DIR) +USER_UTILS_DIR := $(TARGET_DIR)/usr/bin +USER_TESTS_DIR := $(TARGET_DIR)/usr/bin +USER_BIN_DIR := $(TARGET_DIR)/usr/bin +USER_BUILD_DIR := $(BUILD_DIR)/user/$(GROUP_DIR_NAME) +#LINUX_DIR := $(OCERA_DIR)/kernel/linux +#RTL_DIR := $(OCERA_DIR)/kernel/rtlinux +#CONFIG_FILE := $(OCERA_DIR)/emdebsys/.config +ifneq ($(wildcard $(CONFIG_FILE)),) +CONFIG_FILE_OK = y +endif +else # OCERA_DIR +KERN_INCLUDE_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/include-kern +KERN_LIB_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/lib-kern +KERN_MODULES_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/modules +KERN_BUILD_DIR := $(MAKERULES_DIR)/$(BUILD_DIR_NAME)/kern +KERN_MODPOST_DIR := $(MAKERULES_DIR)/$(BUILD_DIR_NAME)/kern-modpost +USER_INCLUDE_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/include +USER_LIB_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/lib +USER_UTILS_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/bin-utils +USER_TESTS_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/bin-tests +USER_BIN_DIR := $(MAKERULES_DIR)/$(COMPILED_DIR_NAME)/bin +USER_BUILD_DIR := $(MAKERULES_DIR)/$(BUILD_DIR_NAME)/user + +ifndef LINUX_VERSION +LINUX_VERSION=$(shell uname -r) +endif +ifndef LINUX_DIR +LINUX_DIR=/lib/modules/$(LINUX_VERSION)/build +endif +endif # OCERA_DIR + +ifeq ($(BUILD_OS),) + # Check for target + ifeq ($(OS),Windows_NT) + BUILD_OS := win32 + else + BUILD_OS := $(shell uname | tr '[A-Z]' '[a-z]' ) + #$(warning BUILD_OS=$(BUILD_OS)) + endif +endif + +ifeq ($(TARGET_OS),) + TARGET_OS := $(BUILD_OS) +endif + +LOCAL_BUILD_DIR = $(USER_OBJS_DIR) + +# Assign default values to CFLAGS variable. If the variable is defined +# earlier (i.g. in config.omk), it is not overriden here. +CFLAGS ?= -O2 -Wall +CXXFLAGS ?= -O2 -Wall + + +CPPFLAGS += -I $(USER_INCLUDE_DIR) + +LOADLIBES += -L$(USER_LIB_DIR) + +LOADLIBES += $(lib_LOADLIBES:%=-l%) + +LIB_CPPFLAGS += $(CPPFLAGS) +LIB_CFLAGS += $(CFLAGS) + +ifeq ($(TARGET_OS),win32) + SOLIB_EXT = dll +else + SOLIB_EXT = so + SOLIB_PICFLAGS += -fpic +endif + +#vpath %.c $(SOURCES_DIR) +#vpath %.cc $(SOURCES_DIR) +#vpath %.cxx $(SOURCES_DIR) + +USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR) +KERN_OBJS_DIR = $(KERN_BUILD_DIR)/$(RELATIVE_DIR) + +.PHONY: dep subdirs clean clean-custom cleandepend check-dir + +# Some support to serialize some targets for parallel make +ifneq ($(OMK_SERIALIZE_INCLUDED),y) +include-pass: check-dir +library-pass: include-pass +binary-pass: library-pass +kernel-lib-pass: include-pass +kernel-mod-pass: kernel-lib-pass +kernel-modpost-pass: kernel-mod-pass +kernel-pass: kernel-mod-pass kernel-modpost-pass + +override OMK_SERIALIZE_INCLUDED = y +MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES)) +endif + +# Checks for OMK tester +ifdef OMK_TESTSROOT +default-config-pass-check include-pass-check: +library-pass-check binary-pass-check: + @[ -x "$(shell which $(CC))" ] || $(call canttest,Cannot file compiler: $(CC)) +endif + +#===================================================================== +# User-space rules and templates to compile programs, libraries etc. + +ifdef USER_RULE_TEMPLATES + +USER_SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o + +USER_SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .o/.S .lo/.lo + +#%.lo: %.c +# $(CC) -o $@ $(LCFLAGS) -c $< + +c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -DOMK_FOR_USER + +cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -DOMK_FOR_USER + +idl_COMPILE = $(IDL_COMPILER) + +# Check GCC version for user build +ifndef CC_MAJOR_VERSION +CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/') +endif +# Prepare suitable define for dependency building +ifeq ($(CC_MAJOR_VERSION),2) +CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp" +else +CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp" +endif + + +# Syntax: $(call COMPILE_c_o_template,,,) +define COMPILE_c_o_template +$(2): $(1) $$(GEN_HEADERS) + @$(QUIET_CMD_ECHO) " CC $$@" + $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + +# Syntax: $(call COMPILE_cc_o_template,,,) +define COMPILE_cc_o_template +$(2): $(1) $$(GEN_HEADERS) + @$(QUIET_CMD_ECHO) " CXX $$@" + $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + +# Syntax: $(call COMPILE_S_o_template,,,) +define COMPILE_S_o_template +$(2): $(1) $$(GEN_HEADERS) + @$(QUIET_CMD_ECHO) " AS $$@" + $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ + then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + +# Syntax: $(call CMETRIC_o_h_template,,) +define CMETRIC_o_h_template +$(2): $(1) + @$(QUIET_CMD_ECHO) " CMETRIC $$@" + $(Q)if [ -n `dirname $$@` ] ; then \ + if [ ! -e `dirname $$@` ] ; then \ + mkdir -p `dirname $$@` ; fi ; fi + $(Q)echo >$$@ '/* Automatically generated from $$< */' + $(Q)echo >>$$@ '/* Conditionals to control compilation */' + $(Q)set -o pipefail ; $(NM) $$< \ + | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \ + | sort >>$$@ + $(Q)echo >>$$@ '/* Defines from the values defined to symbols */' + $(Q)set -o pipefail ; $(NM) $$< \ + | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \ + | sort >>$$@ +endef + + + +define COMPILE_idl_template +$(2).c $(2)-stubs.c $(2)-skels.c $(2)-common.c $(2).h: $(1) + @$(QUIET_CMD_ECHO) " IDL $$@" + $(Q) $$(idl_COMPILE) $(1) +endef + + +# Syntax: $(call PROGRAM_template,,,,) +# FIXME: ???????? asi je tu blbej komentar +define PROGRAM_template + +USER_IDLS += $$($(1)_SERVER_IDL) $$($(1)_CLIENT_IDL) $$($(1)_IDL) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-skels.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c)) +USER_GEN_SOURCES += $$($(1)_GEN_SOURCES) + +$(foreach x, $(USER_SOURCES2OBJS), +$(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ + $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) +) +$(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%)) + +USER_OBJS += $$($(1)_OBJS) +USER_SOURCES += $$($(1)_SOURCES) + +$(2)/$(1)$(3): $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " LINK $$@" + $(Q) $$(if $$(filter %.cc,$$($(1)_SOURCES:%.cxx=%.cc)),$$(CXX),$$(CC)) \ + $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(LDFLAGS) -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@ + @echo "$(2)/$(1)$(3): \\" >$(USER_OBJS_DIR)/$(1).exe.d + @sed -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(USER_OBJS_DIR)/$(1).exe.map|tr '&' '\134' >>$(USER_OBJS_DIR)/$(1).exe.d + @echo >>$(USER_OBJS_DIR)/$(1).exe.d +endef + + +# Syntax: $(call LIBRARY_template,) +define LIBRARY_template + +USER_IDLS += $$($(1)_SERVER_IDL) $$($(1)_CLIENT_IDL) $$($(1)_IDL) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-skels.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c)) +USER_GEN_SOURCES += $$($(1)_GEN_SOURCES) + +$(foreach x, $(USER_SOURCES2OBJS), +$(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ + $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) +) +$(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%)) + +USER_OBJS += $$($(1)_OBJS) +USER_SOURCES += $$($(1)_SOURCES) + +$(USER_LIB_DIR)/lib$(1).a: $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " AR $$@" + $(Q) $(AR) rcs $$@ $$^ +endef + + +# Syntax: $(call SOLIB_template,) +define SOLIB_template + +USER_IDLS += $$($(1)_SERVER_IDL) $$($(1)_CLIENT_IDL) $$($(1)_IDL) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-skels.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c)) +SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES) + +$(foreach x, $(USER_SOURCES2OBJSLO), +$(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ + $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) +) +$(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%)) + +SOLIB_OBJS += $$($(1)_OBJSLO) +SOLIB_SOURCES += $$($(1)_SOURCES) + +$(USER_LIB_DIR)/lib$(1).$(SOLIB_EXT): $$($(1)_OBJSLO) + @$(QUIET_CMD_ECHO) " LINK $$@" + $(Q) $(LD) --shared --soname=lib$(1).$(SOLIB_EXT) -o $$@ $$^ $$(LOADLIBES) $$($(1)_LIBS:%=-l%) +endef + + + +library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS)) \ + $(lib_LIBRARIES:%=$(USER_LIB_DIR)/lib%.a) $(shared_LIBRARIES:%=$(USER_LIB_DIR)/lib%.$(SOLIB_EXT)) + +binary-pass-local: $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%$(EXE_SUFFIX)) $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%$(EXE_SUFFIX)) $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%$(EXE_SUFFIX)) + +# Special rules for CMETRIC generated headers + +$(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call COMPILE_c_o_template,\ + $(SOURCES_DIR)/$($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES),\ + $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),))) +$(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\ + $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),\ + $(addprefix $(USER_INCLUDE_DIR)/,$(cmetrh))))) + +GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%) + +GEN_HEADERS+=$(filter %.h,$(USER_IDLS:%.idl=%.h)) + +# Generate rules for compilation of programs and libraries + +$(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX)))) + +$(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX)))) + +$(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX)))) + +$(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib)))) + +$(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib)))) + +-include $(USER_OBJS_DIR)/*.d + +endif + +#===================================================================== +# Kernel-space rules and templates to compile modules, libraries etc. + +ifdef KERN_RULE_TEMPLATES + +$(KERN_LIB_DIR)/kernel.mk: $(LINUX_DIR)/.config $(MAKERULES_DIR)/kernelcfg2mk + @$(QUIET_CMD_ECHO) " KCFG2MK $$@" + $(Q) $(MAKERULES_DIR)/kernelcfg2mk $(LINUX_DIR) $(KERN_LIB_DIR) + +ifeq ($(CONFIG_RTLINUX),y) +include $(RTL_DIR)/rtl.mk + +KERN_CC = $(CC) +kern_GCCLIB_DIR=$(shell LANG=C LC_ALL=C LC_MESSAGES=C $(CC) -print-search-dirs | sed -n -e 's/^install: \(.*\)$$/\1/p' ) +INCLUDES := -I $(KERN_INCLUDE_DIR) $(INCLUDE) $(rtlinux_INCLUDES) $(kernel_INCLUDES) +#-DEXPORT_NO_SYMBOLS +c_o_kern_COMPILE = $(KERN_CC) -idirafter $(kern_GCCLIB_DIR)/include $(INCLUDES) $(CFLAGS) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc +cc_o_kern_COMPILE = $(CXX) $(INCLUDES) $(CXXFLAGS) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB +KERN_EXE_SUFFIX := .o +KERN_ARCH = $(ARCH) +KERN_LD = $(LD) +KERN_AR = $(AR) + +else # CONFIG_RTLINUX + +include $(KERN_LIB_DIR)/kernel.mk + +ifeq ($(LINUX_SRC),) +LINUX_SRC = $(LINUX_DIR) +endif +kernel_INCLUDES += -I $(LINUX_DIR) -idirafter $(LINUX_SRC)/include/linux + +ifdef LINUX_CC +KERN_CC = $(LINUX_CC) +kern_GCCLIB_DIR=$(shell LANG=C LC_ALL=C LC_MESSAGES=C $(LINUX_CC) -print-search-dirs | sed -n -e 's/^install: \(.*\)$$/\1/p' ) +else +KERN_CC = echo KERN_CC not defined - compilation skipped +endif +c_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_CFLAGS) $(LINUX_CFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc +cc_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_CFLAGS) $(LINUX_CFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB +KERN_EXE_SUFFIX := $(LINUX_MODULE_EXT) +KERN_LDFLAGS = $(LINUX_LDFLAGS) +ifdef LINUX_ARCH +KERN_ARCH = $(LINUX_ARCH) +else +KERN_ARCH = echo KERN_ARCH not defined - skipped +endif +ifdef LINUX_LD +KERN_LD = $(LINUX_LD) +else +KERN_LD = echo KERN_LD not defined - skipped +endif +ifneq ($(LINUX_AR),) +KERN_AR = $(LINUX_AR) +else +KERN_AR = $(AR) +endif +ifeq ($(LINUX_QUOTE_MODNAME),y) +KERN_MQ=\" +KERN_KBUILD_MODNAME=-D"KBUILD_MODNAME=((THIS_MODULE)?(THIS_MODULE)->name:NULL)" +endif +endif # CONFIG_RTLINUX + +KERN_LOADLIBES += -L$(KERN_LIB_DIR) + +KERN_LOADLIBES += $(rtlinux_LOADLIBES:%=-l%) +KERN_LOADLIBES += $(kernel_LOADLIBES:%=-l%) + + + +# Check GCC version for kernel part of build +ifndef kern_CC_MAJOR_VERSION +kern_CC_MAJOR_VERSION := $(shell $(KERN_CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/') +endif +# Prepare suitable define for dependency building +ifeq ($(kern_CC_MAJOR_VERSION),2) +kern_CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp" +else +kern_CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp" +endif + +ifeq ($(KERN_EXE_SUFFIX),.ko) +ifeq ($(wildcard $(LINUX_DIR)/scripts/mod/modpost),) +KERN_MODPOST = $(LINUX_DIR)/scripts/modpost +else +KERN_MODPOST = $(LINUX_DIR)/scripts/mod/modpost +endif +KERN_MODULES_LINK_DIR = $(KERN_MODPOST_DIR) +KERN_LINK_SUFFIX = .o +else +KERN_MODULES_LINK_DIR = $(KERN_MODULES_DIR) +KERN_LINK_SUFFIX = $(KERN_EXE_SUFFIX) +endif + +define COMPILE_c_o_kern_template + +$(2): $(1) + @$(QUIET_CMD_ECHO) " CC [K] $$@" + $(Q) if $$(c_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \ + -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \ + -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + + +define COMPILE_cc_o_kern_template + +$(2): $(1) + @$(QUIET_CMD_ECHO) " CXX [K] $$@" + $(Q) if $$(cc_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \ + -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \ + -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + + +define MODULE_kern_template +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o)) +$(1)_OBJS := $$(sort $$($(1)_OBJS)) + +KERN_OBJS += $$($(1)_OBJS) +KERN_SOURCES += $$($(1)_SOURCES) + +# this is hack to build "__this_module" structure for 2.6.x kernels +# modpost is used for that purpose now + +#$(1).mod.c: +# echo "\ +# #include @\ +# #include @\ +# #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,5))@\ +# #include @\ +# #include @\ +# @\ +# MODULE_INFO(vermagic, VERMAGIC_STRING);@\ +# @\ +# #undef unix@\ +# struct module __this_module@\ +# __attribute__((section(\".gnu.linkonce.this_module\"))) = {@\ +# .name = __stringify(KBUILD_MODNAME),@\ +# .init = init_module,@\ +# #ifdef CONFIG_MODULE_UNLOAD@\ +# .exit = cleanup_module,@\ +# #endif@\ +# };@\ +# #endif@\ +# " | tr @ \\n >$$@ + + +#$(eval $(call COMPILE_c_o_kern_template,$(1).mod.c,$(1).mod.o,-DKBUILD_MODNAME=$(1))) + +$(2)/$(1)$(KERN_LINK_SUFFIX): $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " LD [K] $$@" + $(Q) $$(KERN_LD) $$(KERN_LDFLAGS) -r $$($(1)_OBJS) -L$$(kern_GCCLIB_DIR) $$($(1)_LIBS:%=-l%) $$(KERN_LOADLIBES) -Map $(KERN_OBJS_DIR)/$(1).mod.map -o $$@ + @echo "$(2)/$(1)$(KERN_LINK_SUFFIX): \\" >$(KERN_OBJS_DIR)/$(1).mod.d + @sed -n -e 's/^LOAD \(.*\)$$$$/ \1 \\/p' $(KERN_OBJS_DIR)/$(1).mod.map >>$(KERN_OBJS_DIR)/$(1).mod.d + @echo >>$(KERN_OBJS_DIR)/$(1).mod.d + @if [ "$(KERN_EXE_SUFFIX)" = ".ko" ] ; then \ + echo $(1) >>$(KERN_MODPOST_DIR)/module-changes ; \ + echo $(1) >>$(KERN_MODPOST_DIR)/$(1).mod.stamp ; \ + fi + +endef + + + +define LIBRARY_kern_template +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o)) +$(1)_OBJS := $$(sort $$($(1)_OBJS)) + +KERN_OBJS += $$($(1)_OBJS) +KERN_SOURCES += $$($(1)_SOURCES) + +$(KERN_LIB_DIR)/lib$(1).a: $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " AR [K] $$@" + $(Q) $(KERN_AR) rcs $$@ $$^ +endef + + +ifdef LINUX_DIR + +kernel-lib-pass-local: $(kernel_LIBRARIES:%=$(KERN_LIB_DIR)/lib%.a) + +kernel-mod-pass-local: $(kernel_MODULES:%=$(KERN_MODULES_LINK_DIR)/%$(KERN_LINK_SUFFIX)) + +$(foreach module,$(kernel_MODULES),$(eval $(call MODULE_kern_template,$(module),$(KERN_MODULES_LINK_DIR)))) + +$(foreach lib,$(kernel_LIBRARIES),$(eval $(call LIBRARY_kern_template,$(lib)))) + +endif + + +ifeq ($(CONFIG_RTLINUX),y) + +kernel-mod-pass-local: $(rtlinux_MODULES:%=$(KERN_MODULES_LINK_DIR)/%$(KERN_LINK_SUFFIX)) + +kernel-lib-pass-local: $(rtlinux_LIBRARIES:%=$(KERN_LIB_DIR)/lib%.a) + +$(foreach module,$(rtlinux_MODULES),$(eval $(call MODULE_kern_template,$(module),$(KERN_MODULES_LINK_DIR)))) + +$(foreach lib,$(rtlinux_LIBRARIES),$(eval $(call LIBRARY_kern_template,$(lib)))) + +endif + +ifeq ($(KERN_MODPOST_PASS),y) + +MODULES_LIST := $(wildcard *.mod.stamp) +MODULES_LIST := $(MODULES_LIST:%.mod.stamp=%) + +define MODPOST_kern_template +$(2) : $(1)$(KERN_LINK_SUFFIX) $(1).mod.c + @$(QUIET_CMD_ECHO) " LD [M] $$@" + $(Q) $$(cc_o_kern_COMPILE) -D"KBUILD_BASENAME=$(KERN_MQ)$(1)$(KERN_MQ)" \ + -D"KBUILD_MODNAME=$(KERN_MQ)$(1)$(KERN_MQ)" \ + -o $(1).mod.o -c $(1).mod.c + $(Q) $$(KERN_LD) $$(KERN_LDFLAGS) $(1)$(KERN_LINK_SUFFIX) $(1).mod.o -r -o $$@ +endef + +kernel-modpost-versions: $(wildcard $(LINUX_DIR)/Module.symvers) + @$(QUIET_CMD_ECHO) " MODPOST $(KERN_MODPOST_DIR)" + @echo >$(KERN_MODPOST_DIR)/modpost-running + @rm -f $(KERN_MODPOST_DIR)/module-changes + $(Q) $(KERN_MODPOST) $(MODULES_LIST:%=%$(KERN_LINK_SUFFIX)) $(^:%=-i %) + +$(MODULES_LIST:%=%.mod.c) : kernel-modpost-versions + +kernel-modpost-pass-local: $(MODULES_LIST:%=$(KERN_MODULES_DIR)/%$(KERN_EXE_SUFFIX)) + @rm -f $(KERN_MODPOST_DIR)/modpost-running + +$(foreach module,$(MODULES_LIST),$(eval $(call MODPOST_kern_template,$(module),$(module:%=$(KERN_MODULES_DIR)/%$(KERN_EXE_SUFFIX))))) + +endif + +-include $(KERN_OBJS_DIR)/*.d + +endif + +#===================================================================== + +# Kernel requires its own set of configuration header-files +ifneq ($(kernel_LIBRARIES)$(rtlinux_LIBRARIES)$(kernel_MODULES)$(rtlinux_MODULES)$(kernel_HEADERS)$(rtlinux_HEADERS)$(kernel_HEADERS)$(rtlinux_HEADERS)$(nobase_kernel_HEADERS)$(nobase_rtlinux_HEADERS)$(renamed_kernel_HEADERS)$(renamed_rtlinux_HEADERS),) +KERN_CONFIG_HEADERS_REQUIRED = y +endif + +$(eval $(call omk_pass_template, kernel-lib-pass,$(KERN_OBJS_DIR),KERN_RULE_TEMPLATES=y,$(kernel_LIBRARIES)$(rtlinux_LIBRARIES))) +$(eval $(call omk_pass_template, kernel-mod-pass,$(KERN_OBJS_DIR),KERN_RULE_TEMPLATES=y,$(kernel_MODULES)$(rtlinux_MODULES))) + +kernel-modpost-pass: + +@if [ -e "$(KERN_MODPOST_DIR)/module-changes" -o -e "$(KERN_MODPOST_DIR)/modpost-running" ] ; \ + then \ + $(MAKE) --no-print-directory -C $(KERN_MODPOST_DIR) \ + -f $(SOURCES_DIR)/Makefile KERN_RULE_TEMPLATES=y KERN_MODPOST_PASS=y $(@:%=%-local) ; \ + fi + +$(eval $(call omk_pass_template, library-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES))) +$(eval $(call omk_pass_template, binary-pass, $(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(bin_PROGRAMS)$(utils_PROGRAMS)$(test_PROGRAMS))) + +OTHER_PASSES = clean install include-pass +$(eval $(call omk_pass_template,$(OTHER_PASSES),$(USER_OBJS_DIR),,always)) + +check-dir: + @$(call mkdir_def,$(USER_BUILD_DIR)) + @$(call mkdir_def,$(KERN_BUILD_DIR)) + @$(call mkdir_def,$(USER_INCLUDE_DIR)) + @$(call mkdir_def,$(KERN_INCLUDE_DIR)) + @$(call mkdir_def,$(USER_LIB_DIR)) + @$(call mkdir_def,$(KERN_LIB_DIR)) + @$(call mkdir_def,$(USER_BIN_DIR)) + @$(call mkdir_def,$(USER_UTILS_DIR)) + @$(call mkdir_def,$(USER_TESTS_DIR)) + @$(call mkdir_def,$(KERN_MODULES_DIR)) + @$(call mkdir_def,$(KERN_MODPOST_DIR)) + +install-local: # TODO + +include-pass-local: + $(call include-pass-template,$(USER_INCLUDE_DIR),include) + $(call include-pass-template,$(KERN_INCLUDE_DIR),kernel) +ifeq ($(CONFIG_RTLINUX),y) + $(call include-pass-template,$(KERN_INCLUDE_DIR),rtlinux) +endif + +ifdef USER_RULE_TEMPLATES + +# User-space static libraries and applications object files + +USER_SOURCES := $(sort $(USER_SOURCES)) + +USER_GEN_SOURCES := $(sort $(USER_GEN_SOURCES)) + +#$(warning USER_SOURCES = $(USER_SOURCES)) + +$(foreach src,$(filter %.c,$(USER_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),))) + +$(foreach src,$(filter %.cc,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),))) + +$(foreach src,$(filter %.cxx,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),))) + +$(foreach src,$(filter %.c,$(USER_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.o),))) + +# User-space shared libraries object files + +SOLIB_SOURCES := $(sort $(SOLIB_SOURCES)) + +SOLIB_GEN_SOURCES := $(sort $(SOLIB_GEN_SOURCES)) + +#$(warning SOLIB_SOURCES = $(SOLIB_SOURCES)) +#$(warning SOLIB_GEN_SOURCES = $(SOLIB_GEN_SOURCES)) + +$(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS)))) + +$(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS)))) + +$(foreach src,$(filter %.cxx,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.lo),$(SOLIB_PICFLAGS)))) + +$(foreach src,$(filter %.c,$(SOLIB_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS)))) + +# IDL compilation + +USER_IDLS := $(sort $(USER_IDLS)) + +$(foreach src,$(filter %.idl,$(USER_IDLS)),$(eval $(call COMPILE_idl_template,$(SOURCES_DIR)/$(src),$(src:%.idl=%)))) + +endif + +ifdef KERN_RULE_TEMPLATES + +KERN_SOURCES := $(sort $(KERN_SOURCES)) + +#$(warning KERN_SOURCES = $(KERN_SOURCES)) + +$(foreach src,$(filter %.c,$(KERN_SOURCES)),$(eval $(call COMPILE_c_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),))) + +$(foreach src,$(filter %.cc,$(KERN_SOURCES)),$(eval $(call COMPILE_cc_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),))) + +$(foreach src,$(filter %.cxx,$(KERN_SOURCES)),$(eval $(call COMPILE_cc_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),))) + +endif + +clean-local: clean-custom + @echo Cleaning in $(KERN_OBJS_DIR) and $(USER_OBJS_DIR) + @rm -f $(KERN_OBJS_DIR)/*.o $(USER_OBJS_DIR)/*.o $(USER_OBJS_DIR)/*.lo \ + $(KERN_OBJS_DIR)/*.d $(USER_OBJS_DIR)/*.d \ + $(KERN_OBJS_DIR)/*.map $(USER_OBJS_DIR)/*.map \ + $(KERN_OBJS_DIR)/*.mod.c \ + $(kernel_MODULES:%=$(KERN_MODPOST_DIR)/%.*) \ + $(LOCAL_CONFIG_H:%=$(KERN_OBJS_DIR)/%) \ + $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%) + @if [ -e $(KERN_LIB_DIR)/kernel.mk ] ; then \ + touch -t 200001010101 $(KERN_LIB_DIR)/kernel.mk ; \ + fi + +default: include-pass library-pass binary-pass +ifndef OMIT_KERNEL_PASSES +# Also make kernel passes if not disabled +default: kernel-lib-pass kernel-pass +endif + +# Local Variables: +# mode:makefile +# End: + #OMK@config_h +# Syntax: $(call BUILD_CONFIG_H_template,,,,) +define BUILD_CONFIG_H_template + +$(addprefix $(1)/,$(notdir $(addsuffix .stamp,$(2)))) : $(wildcard $(CONFIG_FILE)) $(wildcard $(CONFIG_FILE)-default) + @$(QUIET_CMD_ECHO) " CONFGEN $$(@:%.stamp=%)" + @if [ ! -d `dirname $(2).tmp` ] ; then \ + mkdir -p `dirname $(2).tmp` ; fi + @echo "/* Automatically generated from */" > "$(2).tmp" + @echo "/* config file: $$< */" >> "$(2).tmp" + $(if $(DOXYGEN),@echo "/** @file */" >> "$(2).tmp") + @echo "#ifndef $(4)" >> "$(2).tmp" + @echo "#define $(4)" >> "$(2).tmp" + @( $(foreach x, $(shell echo '$($(3))' | tr 'x\t ' 'x\n\n' | sed -e 's/^\([^ =]*\)\(=[^ ]\+\|\)$$/\1/' ), \ + echo '$(x).$($(x))' ; ) echo ; ) | \ + sed -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \ + sed -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \ + >> "$(2).tmp" + @echo "#endif /*$(4)*/" >> "$(2).tmp" + @touch "$$@" + @if cmp --quiet "$(2).tmp" "$(2)" ; then rm "$(2).tmp"; \ + else mv "$(2).tmp" "$(2)" ; \ + echo "Updated configuration $(2)" ; fi + +endef + +ifdef LOCAL_CONFIG_H + +# This must be declared after the default cflags are assigned! +# Override is used to override command line assignemnt. +override CFLAGS += -I. +$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(USER_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) ) + +endif + +# Special rules for configuration exported headers + +#FIXME: The directory for headers should not be specified here. +$(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\ +_$(basename $(notdir $(confh)))_H \ +))) + +config_h_stamp_files = $(addprefix $(USER_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H)))) + +# Add some hooks to standard passes +include-pass-local: $(config_h_stamp_files) + +ifneq ($(KERN_CONFIG_HEADERS_REQUIRED),) + +ifdef LOCAL_CONFIG_H +$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(KERN_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) ) +endif + +$(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(addprefix $(KERN_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\ +_$(basename $(notdir $(confh)))_H \ +))) + +kern_config_h_stamp_files = $(addprefix $(KERN_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H)))) + +# Add some hooks to standard passes +include-pass-local: $(kern_config_h_stamp_files) + +endif + +clean-local: clean-local-config-h + +clean-local-config-h: + @$(foreach confh,$(config_h_stamp_files) $(kern_config_h_stamp_files),\ + if [ -e $(confh) ] ; then rm $(confh) ; fi ; \ + ) + + +# Local Variables: +# mode:makefile +# End: + #OMK@qt +ifneq ($(QT_SUBDIRS),) + +.PHONY: qt-subpass clean-qt distclean-qt + +# Usage: $(call qt_makefile_template,) +define qt_makefile_template +$(SOURCES_DIR)/$(1)/Makefile: $(wildcard $(SOURCES_DIR)/$(1)/*.pro) + cd $(SOURCES_DIR)/$(1); $(QTDIR:%=%/bin/)qmake TOP_DIR=$(MAKERULES_DIR) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) $(QTDIR:%=QTDIR=%) CC=$(CC) \ + CXX=$(CXX) LIBS+="-L$(USER_LIB_DIR)" INCLUDEPATH+="$(USER_INCLUDE_DIR)" +endef +$(foreach dir,$(QT_SUBDIRS), $(eval $(call qt_makefile_template,$(dir)))) + +qt-subpass: $(foreach dir,$(QT_SUBDIRS), $(SOURCES_DIR)/$(dir)/Makefile) + $(foreach dir,$(QT_SUBDIRS),\ + $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir) -C $(SOURCES_DIR)/$(dir) \ + -f $(SOURCES_DIR)/$(dir)/Makefile || exit 1 ;) + +# Hook to binary pass +binary-pass: qt-subpass + +# Hook to clean pass +clean-local: clean-qt +clean-qt: + +@$(foreach dir, $(QT_SUBDIRS), \ + $(if $(wildcard $(SOURCES_DIR)/$(dir)/Makefile), \ + @$(QUIET_CMD_ECHO) " CLEAN $(dir)"; \ + $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir) -C $(SOURCES_DIR)/$(dir) \ + -f $(SOURCES_DIR)/$(dir)/Makefile clean|| exit 1 ;)) + + +# Hook to distclean +distclean: distclean-qt + +# TODO: Add distclean-qt-pass to handle QT_SUBDIRS in the whole +# tree. This way we only distclean toplevel subdirs. +distclean-qt: + +@$(foreach dir, $(QT_SUBDIRS), \ + $(if $(wildcard $(SOURCES_DIR)/$(dir)/Makefile), \ + @$(QUIET_CMD_ECHO) " DISTCLEAN $(dir)"; \ + $(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir) -C $(SOURCES_DIR)/$(dir) \ + -f $(SOURCES_DIR)/$(dir)/Makefile distclean|| exit 1 ;)) +endif + +# Local Variables: +# mode:makefile +# End: + #OMK@sources-list +# Rules that creates the list of files which are used during +# compilation. The list reflects conditional compilation depending on +# config.omk and other variables. + +SOURCES_LIST_FN=sources.txt +ifndef SOURCES_LIST +SOURCES_LIST_DIR:=$(RELATIVE_DIR) +SOURCES_LIST:=$(MAKERULES_DIR)/$(SOURCES_LIST_DIR)/$(SOURCES_LIST_FN) +export SOURCES_LIST SOURCES_LIST_DIR +endif + +ifeq ($(MAKECMDGOALS),sources-list) +NEED_SOURCES_LIST=y +endif +ifeq ($(MAKECMDGOALS),TAGS) +NEED_SOURCES_LIST=y +endif +ifeq ($(MAKECMDGOALS),tags) +NEED_SOURCES_LIST=y +endif + +ifeq ($(NEED_SOURCES_LIST),y) # avoid execution of find command bellow if it is not useful +.PHONY: sources-list +sources-list: $(SOURCES_LIST) + +$(SOURCES_LIST): $(CONFIG_FILE) $(CONFIG_FILE)-default $(shell find -name $(MAKEFILE_OMK)) + @echo -n "" > "$(SOURCES_LIST).tmp" + @$(MAKE) --no-print-directory sources-list-pass + @echo "# Automatically generated list of files in '$(RELATIVE_DIR)' that are used during OMK compilation" > "$(SOURCES_LIST).tmp2" + @cat "$(SOURCES_LIST).tmp"|sort|uniq >> "$(SOURCES_LIST).tmp2" + @rm "$(SOURCES_LIST).tmp" + @mv "$(SOURCES_LIST).tmp2" "$(SOURCES_LIST)" +endif + +$(eval $(call omk_pass_template,sources-list-pass,$$(LOCAL_BUILD_DIR),,always)) + +sources-list-pass-local: + @$(foreach h,$(include_HEADERS) $(nobase_include_HEADERS) $(kernel_HEADERS),\ + echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(h)" >> "$(SOURCES_LIST).tmp";) + @$(foreach ch,$(config_include_HEADERS), \ + echo "$(USER_INCLUDE_DIR:$(MAKERULES_DIR)/$(addsuffix /,$(SOURCES_LIST_DIR))%=%)/$(ch)" >> "$(SOURCES_LIST).tmp";) + @$(foreach h,$(renamed_include_HEADERS),echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(h)"|sed -e 's/\(.*\)->.*/\1/' >> "$(SOURCES_LIST).tmp";) + @$(foreach bin,$(lib_LIBRARIES) $(shared_LIBRARIES) $(bin_PROGRAMS) $(test_PROGRAMS) $(utils_PROGRAMS) \ + $(kernel_LIBRARIES) $(rtlinux_LIBRARIES) $(kernel_MODULES),\ + $(foreach src,$(filter-out %.o,$($(bin)_SOURCES)),echo "$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%)/$(src)" >> "$(SOURCES_LIST).tmp";)) + +############ TAGS ########### + +ifeq ($(MAKECMDGOALS),TAGS) +ETAGS=etags +TAGS_CMD = $(ETAGS) +TAGS: $(SOURCES_LIST) + @$(MAKE) --no-print-directory do-tags +endif +ifeq ($(MAKECMDGOALS),tags) +CTAGS=ctags -N +TAGS_CMD = $(CTAGS) +tags: $(SOURCES_LIST) + @$(MAKE) --no-print-directory do-tags +endif +export TAGS_CMD + +ifeq ($(MAKECMDGOALS),do-tags) +.PHONY: do-tags +do-tags: $(shell sed -e '/^\#/d' $(SOURCES_LIST)) + @$(QUIET_CMD_ECHO) " TAGS $(SOURCES_LIST_FN)" + $(Q)$(TAGS_CMD) $^ +endif + +############ CSCOPE ########### + +cscope: $(SOURCES_LIST) + @$(QUIET_CMD_ECHO) " CSCOPE < $(SOURCES_LIST_FN)" + $(Q)sed -e '/^#/d' $(SOURCES_LIST)|cscope -b -i- +#FIXME: see doc to -i in cscope(1) + +# Local Variables: +# mode:makefile +# End: diff --git a/config.omk b/config.omk new file mode 100644 index 0000000..ad1825d --- /dev/null +++ b/config.omk @@ -0,0 +1 @@ +CFLAGS = -Wall -g -O0 diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..b22a357 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/src/Makefile.omk b/src/Makefile.omk new file mode 100644 index 0000000..b386f43 --- /dev/null +++ b/src/Makefile.omk @@ -0,0 +1,3 @@ +bin_PROGRAMS = hil2cap +hil2cap_SOURCES = hil2cap.c hcidriver.c +hil2cap_LIBS = hil2cap.h hcidriver.h \ No newline at end of file diff --git a/src/hcidriver.c b/src/hcidriver.c new file mode 100644 index 0000000..24819b2 --- /dev/null +++ b/src/hcidriver.c @@ -0,0 +1,271 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "hcidriver.h" + + + +/************************** hci command functions**************************/ +int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout){ + inquiry_cp cmdp,*p_cmdp=&cmdp; + hci_request req,*p_req=&req; + + //hci_inquiry_complete_ev com_ev,*p_com_ev=&com_ev; + __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF; + + p_cmdp->lap[0]=0x33; + p_cmdp->lap[1]=0x8b; + p_cmdp->lap[2]=0x9e; + p_cmdp->length=5; + p_cmdp->num_rsp=5; + memset(p_req,0,sizeof(req)); + assemble_ocf_ogf(0x01,0x01,p_OCF_OGF); + p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_retcmdp=p_addressarray; + p_req->p_cmdp=p_cmdp; + p_req->retcmdp_len=INQUIRY_INFO_SIZE; + p_req->cmdp_len=INQUIRY_CP_SIZE; + p_req->event=EVT_INQUIRY_RESULT; + + if(hci_send_request(dd,p_req,timeout)<0) + return -1; +// if(p_com_ev->status) +// return -1; + + + return 0; +} +// fill up the input parameter address pointer by one address +int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address, int timeout){ + read_bd_addr_rp cmdp,*p_cmdp; //command parameters return/comand + hci_request req,*p_req; + __u16 OCF_OGF,*p_OCF_OGF; + p_OCF_OGF=&OCF_OGF; + p_cmdp=&cmdp; + p_req=&req; + + + memset(p_req,0,sizeof(req)); + assemble_ocf_ogf(0x09,0x04,p_OCF_OGF); + p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_retcmdp=p_cmdp; + p_req->retcmdp_len=READ_BD_ADDR_RP_SIZE; + p_req->cmdp_len=0; + p_req->event=EVT_CMD_COMPLETE; + + if(hci_send_request(dd,p_req,timeout)<0) + return -1; + if(p_cmdp->status) + return -1; + + bacpy(p_address, &cmdp.bdaddr); + return 0; +} + +int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout){ + create_conn_cp cmdp,*p_cmdp=&cmdp; + hci_request req,*p_req=&req; + __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF; + + p_cmdp->bdaddr=*p_address; + p_cmdp->pkt_type=0x0010; + p_cmdp->pscan_rep_mode=0x01; + p_cmdp->pscan_mode=0x00; + p_cmdp->clock_offset=0xf000; + p_cmdp->role_switch=0x00; + memset(p_req,0,sizeof(req)); + assemble_ocf_ogf(0x05,0x01,p_OCF_OGF); + p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_cmdp=p_cmdp; + p_req->cmdp_len=CREATE_CONN_CP_SIZE; + p_req->event=EVT_CONN_COMPLETE; + + if(hci_send_request(dd,p_req,timeout)<0) + return -1; + //if(p_com_ev->status) + // return -1; + + return 0; +} + + +/*******************************HCI main functions ********************************/ + +//create and bind the socket to one device, return -1 when error, device descriptor when OK +int hci_open_device(int dev_id){ + struct sockaddr_hci address; + int dd; + + if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){ + perror("socket no created"); + return dd; //it return -1 when socket isn't created + } + + memset(&address,0,sizeof(address)); + address.hci_family=AF_BLUETOOTH; + address.hci_dev=dev_id; + if(bind(dd,(struct sockaddr *) &address, sizeof(address))<0){ + perror("Socket not binded to hci device"); + close(dd); + return -1; + } + + return dd; +} + +int hci_close_dev(int dd){ + return close(dd); +} + +int hci_send_command(int dd, hci_request *p_req){ + __u8 array[p_req->cmdp_len+4]; //type + OCF+OGF+plen + int ii; + array[0]=0x01; + memcpy(&array[1],p_req->p_OCF_OGF,2); + array[3]= p_req->cmdp_len; + if(p_req->cmdp_len > 0){ + memcpy(&array[4],p_req->p_cmdp,p_req->cmdp_len); // !!!!!!!!! segmentation fault + } + + for(ii=0;iicmdp_len+4))<0){ + perror("write interrupted"); + if(errno == EAGAIN || errno == EINTR) + continue; + return -1; + } + return 0; +} + +int hci_send_request(int dd, hci_request *p_req,int timeout){ + return 0; +} + +/*void print_device_list(int ctl){ + bt_device_req_list dlist,*p_dlist; + bt_device_req *p_dreq; + int i; + bt_device devi; + + p_dlist=&dlist; + p_dlist->dev_num=HCI_MAX_DEV; + p_dreq=p_dlist->dev_req; + + if(ioctl(ctl, HCIGETDEVLIST, (void *) p_dlist)<0){ + perror("Can't get device list"); + exit(1); + } + + for(i=0;idev_num;i++){ + devi.dev_id=(p_dreq+i)->dev_id; + if(ioctl(ctl, HCIGETDEVINFO, (void *) &devi)<0) + printf("No info"); + continue; + + } + +} + + + +void assemble_hci_data(void *p_con_handle,void *p_data_size,void *p_data){ + __u16 data_size=0; + data_size= *(__u16 *)p_data_size; + __u8 array[data_size+5]; //type + handle + flags + size + array[0]=0x02; + memcpy(&array[1],p_con_handle,2); + memcpy(&array[3],p_data_size,2); + memcpy(&array[6],p_data,data_size); + //call send to socket/uart + +} */ + + + +/*****************************HCI support functions*****************************/ + + +void assemble_ocf_ogf(__u8 ocf,__u8 ogf,__u16 *p_ocf_ogf){ + __u16 var1; + __u16 result; + //result=ocf; + //result=(result<<8); + //var1=(ogf<<2); + //*p_ocf_ogf=(result|var1); + var1=(ogf<<10); + result=ocf; + *p_ocf_ogf=(result|var1); +} + +void printba(bt_address *ba){ + printf("address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X: \n",ba->byte[0],ba->byte[1],ba->byte[2],ba->byte[3],ba->byte[4],ba->byte[5]); +} + +int compare_bda(bt_address *p_first, bt_address *p_second){ + __u8 i,k=0; + for(i=0;ibyte[i]==p_second->byte[i]) + k++; + } + if(k==sizeof(bt_address)) return 1; //all bytes are similar + + return 0; //addreses are different in one byte at least +} + +void fill_zero(bt_address *p_addr){ + __u8 i; + for(i=0;ibyte[i]=0x00; + } +} +void swap_addrbytes(bt_address *p_addr){ + bt_address help,*p_help=&help; + __u8 i; + for(i=0;ibyte[i]=p_addr->byte[5-i]; + } + *p_addr=*p_help; +} + +__u8 swap8(__u8 byte1){ + __u8 i,mask=0,hvar1=0,resvar=0; + for(i=0;i<8;i++){ + mask=1<0) + resvar=resvar + (128>>i); + } + return resvar; +} + +__u16 swap16(__u16 byte2){ + __u8 i; + __u16 mask=0,hvar1=0,resvar=0; + for(i=0;i<16;i++){ + mask=1<0) + resvar=resvar + (32768>>i); + } + return resvar; +} + +void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth){ + addr->byte[0]=first; + addr->byte[1]=sec; + addr->byte[2]=third; + addr->byte[3]=forth; + addr->byte[4]=fifth; + addr->byte[5]=sixth; +} + diff --git a/src/hcidriver.h b/src/hcidriver.h new file mode 100644 index 0000000..28ec3e9 --- /dev/null +++ b/src/hcidriver.h @@ -0,0 +1,176 @@ +// +// C++ Interface: hcidriver +// +// Description: my idea , embeded system will be in waiting state. For this role it will be slave. Master it is a main computer or phone. +// +// +// Author: root , (C) 2008 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#ifndef __HCIDRIVER +#define __HCIDRIVER + +#include +#include +#include +#include + + + + + +/* BT protocols(from bluetooth.h)*/ +#define BTPROTO_L2CAP 0 +#define BTPROTO_HCI 1 +#define BTPROTO_SCO 2 +#define BTPROTO_RFCOMM 3 +#define BTPROTO_BNEP 4 +#define BTPROTO_CMTP 5 +#define BTPROTO_HIDP 6 +#define BTPROTO_AVDTP 7 + +#define SOL_HCI 0 +#define SOL_L2CAP 6 +#define SOL_SCO 17 +#define SOL_RFCOMM 18 + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + + +/* BD_ADDR (bluetooth.h)*/ +typedef struct{ + __u8 byte[6]; +} __attribute__((packed)) bt_address; + +typedef struct{ + uint8_t b[6]; +} __attribute__((packed)) bdaddr_t; + +typedef struct{ + __u8 byte[3]; +} __attribute__((packed)) dev_lap; + + + +/* Class of device*/ + +typedef struct{ + __u8 byte[3]; +}cl_device; + + +typedef struct{ + __u16 *p_OCF_OGF; + void *p_cmdp; + void *p_retcmdp; + __u16 cmdp_len; + __u16 retcmdp_len; + int event; //for setting event filter , some hci commands have own events +}hci_request; + + + +/* next function structures*/ + + +extern int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout); +extern int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address, int timeout); +extern int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout); +extern int hci_open_device(int dev_id); +extern int hci_send_command(int dd, hci_request *p_req); +extern int hci_send_request(int dd, hci_request *p_req,int timeout); +extern void print_device_list(int ctl); +extern int hci_close_dev(int dd); + + +extern void assemble_hci_data(void *p_con_handle,void *p_data_size,void *p_data); +extern void assemble_ocf_ogf(__u8 ogf,__u8 ocf,__u16 *p_ocf_ogf); +extern __u8 swap8(__u8 byte1); +extern __u16 swap16(__u16 byte2); +//extern void bacpy(bt_address p_dest, bt_address p_source); +extern void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth); +extern void printba(bt_address *ba); +extern int compare_bda(bt_address *p_first, bt_address *p_second); +extern void fill_zero(bt_address *p_addr); +extern void swap_addrbytes(bt_address *p_addr); + + +static inline void hci_set_bit(int nr, void *addr) +{ + *((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31)); +} + +static inline void hci_clear_bit(int nr, void *addr) +{ + *((uint32_t *) addr + (nr >> 5)) &= ~(1 << (nr & 31)); +} + +static inline int hci_test_bit(int nr, void *addr) +{ + return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31)); +} + +/* HCI filter tools */ +static inline void hci_filter_clear(struct hci_filter *f) +{ + memset(f, 0, sizeof(*f)); +} +static inline void hci_filter_set_ptype(int t, struct hci_filter *f) +{ + hci_set_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline void hci_filter_clear_ptype(int t, struct hci_filter *f) +{ + hci_clear_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline int hci_filter_test_ptype(int t, struct hci_filter *f) +{ + return hci_test_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline void hci_filter_all_ptypes(struct hci_filter *f) +{ + memset((void *) &f->type_mask, 0xff, sizeof(f->type_mask)); +} +static inline void hci_filter_set_event(int e, struct hci_filter *f) +{ + hci_set_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline void hci_filter_clear_event(int e, struct hci_filter *f) +{ + hci_clear_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline int hci_filter_test_event(int e, struct hci_filter *f) +{ + return hci_test_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline void hci_filter_all_events(struct hci_filter *f) +{ + memset((void *) f->event_mask, 0xff, sizeof(f->event_mask)); +} +static inline void hci_filter_set_opcode(int opcode, struct hci_filter *f) +{ + f->opcode = opcode; +} +static inline void hci_filter_clear_opcode(struct hci_filter *f) +{ + f->opcode = 0; +} +static inline int hci_filter_test_opcode(int opcode, struct hci_filter *f) +{ + return (f->opcode == opcode); +} + +static inline int bacmp(const bt_address *ba1, const bt_address *ba2) +{ + return memcmp(ba1, ba2, sizeof(bt_address)); +} +static inline void bacpy(bt_address *dst, const bt_address *src) +{ + memcpy(dst, src, sizeof(bt_address)); +} + + +#endif /* __HCIDRIVER */ diff --git a/src/hil2cap.c b/src/hil2cap.c new file mode 100644 index 0000000..87885b5 --- /dev/null +++ b/src/hil2cap.c @@ -0,0 +1,120 @@ +/* +* C Implementation: testovaci +* +* Description: +* +* +* Author: root , (C) 2008 +* +* Copyright: See COPYING file that comes with this distribution +* +*/ +#include +#include +#include +#include +#include +#include "hil2cap.h" +#include "hcidriver.h" + + + +extern int errno; + +int main(void) +{ dev_lap lap; + lap.byte[0]=0x00; + lap.byte[1]=0x55; + lap.byte[2]=0x12; + //inicialization process, obtain adress and name from local controller + struct hci_dev_info device,*p_device; + p_device=&device; + connection_hci connection,*p_connection; + p_connection=&connection; + p_connection->con_state=DISCONNECTED; + //show_all_local_dev(p_connection,p_device); + + +return 0; +} + + + +int create_master_connection(void){//establish connection and send a coutn of bytes every x second + cmd_state_request array_req[MAX_CMD_STATE_REQUEST]; //array of open request which are in waiting state for appropriate event or answer + connection_hci connection,*p_connection=&connection; + p_connection->con_id=0; + p_connection->con_state=DISCONNECTED; + + memset(array_req,0,(sizeof(cmd_state_request)*MAX_CMD_STATE_REQUEST)); + while(1){ + if(!p_connection->con_state){ //there is no connection, create it + + } + + + + } + + +} + + +//void call_hci_send_data(unsigned short con_handle, __u8 pb_flag, __u8 b_flag, unsigned short data_lenght) + +//void call_hci_create_connection(bt_address *dev_adress,unsigned short pkt_type,__u8 sc_mod){// + +//} + +//Socket functions +/*void show_all_local_dev(connection_hci *connection, struct hci_dev_info *p_device){ + int errr=0; + int sock,i; + int timeout=1000; + + bt_device_req_list dlist,*p_dlist; + bt_device_req *p_dreq; + + p_dlist=&dlist; + p_dlist->dev_num=HCI_MAX_DEV; + p_dreq=p_dlist->dev_req; + + if((sock=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){ + perror("socket"); + errr=1; + } + + if((ioctl(sock, HCIGETDEVLIST, (void *) p_dlist))<0){ + perror("Can't get device list"); + errr=2; + } + if(p_dlist->dev_num >1 && errr == 0){ + bt_device dev_array[p_dlist->dev_num]; + for(i=0;idev_num;i++){ + dev_array[i].dev_id=(p_dreq+i)->dev_id; + if(ioctl(sock, HCIGETDEVINFO, (void *) &dev_array[i])<0){ + printf("No info"); + continue; + } + int dd=hci_open_device(dev_array[i].dev_id); + if(call_hci_read_bd_addr_cmd(dd,&dev_array[i].bdaddr, timeout)< 0){ + printf("Error in call hci bd addr cnd"); + exit(0); + } + printba(&dev_array[i].bdaddr); + } + } + else{ + switch(errr){ + case 1: printf("Error: %d,%d",errr,errno);break; + case 2: printf("Error: %d,%d",errr,errno);break; + //case 3: printf("Error in call hci bd addr cnd");break; + } + printf("There is no BT host controler device connected!! \n"); + exit(0); + } + +} */ + + + diff --git a/src/hil2cap.h b/src/hil2cap.h new file mode 100644 index 0000000..62871d4 --- /dev/null +++ b/src/hil2cap.h @@ -0,0 +1,53 @@ +// +// C++ Interface: hil2cap +// +// Description: +// +// +// Author: root , (C) 2008 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#ifndef __HIL2CAP +#define __HIL2CAP + +#include"hcidriver.h" +#include +#include +#include + +/* Connection states */ +#define DISCONNECTED 0 +#define CONNECTED 1 +#define PENDING 2 + + +#define MAX_CMD_STATE_REQUEST 0x0F +typedef struct{ + __u8 actual_status; + __u8 id; + __u16 request_opcode; + __u16 answer_opcode; + +} __attribute__((packed)) cmd_state_request; + +typedef struct{ + __u16 con_id; + __u8 con_state; + int socket_fd; +}connection_hci; + + + + +//extern void show_all_local_dev(connection_hci *connection, bt_device *p_device); +//extern void call_hci_inquiry_cmd(dev_lap lap,__u8 inq_length,__u8 resp_count); +//extern void call_hci_create_connection(bt_address *dev_adress,unsigned short pkt_type,__u8 sc_mod); +//extern void call_hci_read_bd_addr_cmd(bt_device *p_device); +//extern void call_hci_read_local_name_cmd(bt_device *p_device); +//extern void create_BTsocket_connection(connection_hci *connection, bt_device *p_device); + + + +#endif diff --git a/testf/Makefile b/testf/Makefile new file mode 100644 index 0000000..bbc3ce2 --- /dev/null +++ b/testf/Makefile @@ -0,0 +1,2 @@ +sockfree: sockfree.c + gcc -Wall -o sockfree sockfree.c \ No newline at end of file diff --git a/testf/hcidriver.h b/testf/hcidriver.h new file mode 100644 index 0000000..6d27daf --- /dev/null +++ b/testf/hcidriver.h @@ -0,0 +1,454 @@ +// +// C++ Interface: hcidriver +// +// Description: +// +// +// Author: root , (C) 2008 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#ifndef __HCIDRIVER +#define __HCIDRIVER + +#include +#include +#include +#include + +/* HCI ioctl defines */ +#define HCIDEVUP _IOW('H', 201, int) +#define HCIDEVDOWN _IOW('H', 202, int) +#define HCIDEVRESET _IOW('H', 203, int) +#define HCIDEVRESTAT _IOW('H', 204, int) + +#define HCIGETDEVLIST _IOR('H', 210, int) +#define HCIGETDEVINFO _IOR('H', 211, int) +#define HCIGETCONNLIST _IOR('H', 212, int) +#define HCIGETCONNINFO _IOR('H', 213, int) + +#define HCISETRAW _IOW('H', 220, int) +#define HCISETSCAN _IOW('H', 221, int) +#define HCISETAUTH _IOW('H', 222, int) +#define HCISETENCRYPT _IOW('H', 223, int) +#define HCISETPTYPE _IOW('H', 224, int) +#define HCISETLINKPOL _IOW('H', 225, int) +#define HCISETLINKMODE _IOW('H', 226, int) +#define HCISETACLMTU _IOW('H', 227, int) +#define HCISETSCOMTU _IOW('H', 228, int) + +#define HCISETSECMGR _IOW('H', 230, int) + +#define HCIINQUIRY _IOR('H', 240, int) + +/* HCI socket options*/ +#define HCI_FILTER 2 +#define SOL_HCI 0 + +#define HCI_FLT_TYPE_BITS 31 +#define HCI_FLT_EVENT_BITS 63 +#define HCI_FLT_OGF_BITS 63 +#define HCI_FLT_OCF_BITS 127 + +/* HCI data types */ +#define HCI_COMMAND_PKT 0x01 +#define HCI_ACLDATA_PKT 0x02 +#define HCI_EVENT_PKT 0x04 +#define HCI_VENDOR_PKT 0xFF + +/* HCI packet types */ +#define HCI_DM1 0x0008 +#define HCI_DM3 0x0400 +#define HCI_DM5 0x4000 +#define HCI_DH1 0x0010 +#define HCI_DH3 0x0800 +#define HCI_DH5 0x8000 + +#define HCI_HV1 0x0020 +#define HCI_HV2 0x0040 +#define HCI_HV3 0x0080 + +/* ACL flags */ +#define ACL_CONT 0x01 +#define ACL_START 0x02 +#define ACL_ACTIVE_BCAST 0x04 +#define ACL_PICO_BCAST 0x08 + +/* Connection states */ +#define DISCONNECTED 0 +#define CONNECTED 1 +#define PENDING 2 + +/* BT protocols*/ +#define BTPROTO_L2CAP 0 +#define BTPROTO_HCI 1 +#define BTPROTO_SCO 2 +#define BTPROTO_RFCOMM 3 +#define BTPROTO_BNEP 4 +#define BTPROTO_CMTP 5 +#define BTPROTO_HIDP 6 +#define BTPROTO_AVDTP 7 + +#define SOL_HCI 0 +#define SOL_L2CAP 6 +#define SOL_SCO 17 +#define SOL_RFCOMM 18 + +#define HCI_MAX_DEV 4 +#define HCI_MAX_EVENT_SIZE 260 + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + + +/* BD_ADDR */ +typedef struct{ + __u8 byte[6]; +} __attribute__((packed)) bt_address; + +typedef struct{ + __u8 byte[3]; +} __attribute__((packed)) dev_lap; + + + +/* Class of device*/ + +typedef struct{ + __u8 byte[3]; +}cl_device; + +struct hci_dev_stats { + __u32 err_rx; + __u32 err_tx; + __u32 cmd_tx; + __u32 evt_rx; + __u32 acl_tx; + __u32 acl_rx; + __u32 sco_tx; + __u32 sco_rx; + __u32 byte_rx; + __u32 byte_tx; +}; + +typedef struct{ + __u16 dev_id; + char name[8]; + + bt_address bdaddr; + + __u32 flags; + __u8 type; + + __u8 features[8]; + + __u32 pkt_type; + __u32 link_policy; + __u32 link_mode; + + __u16 acl_mtu; + __u16 acl_pkts; + __u16 sco_mtu; + __u16 sco_pkts; + + struct hci_dev_stats stat; +}bt_device; + + +typedef struct{ + __u16 dev_id; + __u32 dev_opt; +}bt_device_req; + +typedef struct{ + __u16 dev_num; + bt_device_req dev_req[HCI_MAX_DEV]; +}bt_device_req_list; + +typedef struct{ + __u16 *p_OCF_OGF; + void *p_cmdp; + void *p_retcmdp; + __u16 cmdp_len; + __u16 retcmdp_len; + int event; //for setting event filter , some hci commands have own events +}hci_request; + +struct hci_filter{ + __u32 type_mask; + __u32 event_mask[2]; + __u16 opcode; +}; + +/* Socket address*/ +typedef struct{ + sa_family_t hci_family; + __u8 dev_id; +}sockaddr_hci; + +typedef struct{ + __u16 con_id; + __u8 con_state; + int socket_fd; +}connection_hci; + + +/*HCI packet headers*/ + +typedef struct { + uint16_t opcode; /* OCF & OGF */ + uint8_t plen; +} __attribute__ ((packed)) hci_command_hdr; +#define HCI_COMMAND_HDR_SIZE 3 + +typedef struct { + uint8_t evt; + uint8_t plen; +} __attribute__ ((packed)) hci_event_hdr; +#define HCI_EVENT_HDR_SIZE 2 + +typedef struct { + uint16_t handle; /* Handle & Flags(PB, BC) */ + uint16_t dlen; +} __attribute__ ((packed)) hci_acl_hdr; +#define HCI_ACL_HDR_SIZE 4 + + + +/*HCI Commands */ +/*Link control commands*/ +#define OCF_INQUIRY_CMD 0x0001 +struct hci_inquiry_cmd { + __u8 lap[3]; + __u8 inquiry_length; + __u8 num_responces; +} __attribute__ ((packed)); + +#define OCF_INQUIRY_CANCEL_CMD 0x0002 + +#define OCF_CREATE_CONNECTION_CMD 0x0005 +struct hci_create_connection_cmd{ + bt_address address; + __u16 packet_type; + __u8 rep_mode; + __u8 reserved; + __u16 clock_offset; + __u8 role_switch; +} __attribute__ ((packed)); + +#define OCF_DISCONNECT_CMD 0x0006 +struct hci_disconnect_cmd{ + __u8 reason; +} __attribute__ ((packed)); + +#define OCF_CREATE_CONNECTION_CANCEL_CMD 0x0008 +struct hci_create_connection_cancel_cmd{ + bt_address address; +} __attribute__ ((packed)); + +#define OCF_ACCEPT_CONNECTION_REQ_CMD 0x0009 +struct hci_accept_connection_req_cmd{ + bt_address address; + __u8 role; +} __attribute__ ((packed)); + +#define OCF_REJECT_CONNECTION_REQ_CMD 0x000A +struct hci_reject_connection_req_cmd{ + bt_address address; + __u8 reason; +} __attribute__ ((packed)); + +#define OCF_REMOTE_NAME_REQ_CMD 0x0019 +typedef struct { + bt_address address; + __u8 rep_mode; + __u8 reserved; + __u16 clock_offset; +} __attribute__ ((packed)) hci_remote_name_req_cmd; + +/*Link policy commands*/ +#define OCF_RESET_CMD 0x0003 + + +/*Informational parameters commands */ +#define OGF_INFO_CMD 0x04 + +#define OCF_READ_BD_ADDR_CMD 0x0009 +#define READ_BD_ADDR_CMD_PARL 7 +typedef struct{ + + //return parameters + __u8 status; + bt_address address; +} __attribute__ ((packed)) read_bd_addr_cmd; + +/*Controllers and baseband commands */ +#define OGF_CON_AND_BSB_CMD 0x03 + +#define OCF_READ_LOCAL_NAME_CMD 0x0014 + + + +/* HCI events */ + +#define INQUIRY_COMPLETE_EV 0x01 +struct hci_inquiry_complete_ev{ + __u8 status; +} __attribute__ ((packed)); + +#define INQUIRY_RESULT_EV 0x02 +struct hci_inquiry_result_ev{ + __u8 num_responces; + bt_address ba_array; + __u8 rep_mode_array; + __u8 per_mode_array; + __u8 reserved_array; + cl_device cod_array; + __u16 clock_offset_array; +} __attribute__ ((packed)); + +#define CONNECTION_COMPLETE_EV 0x03 +struct hci_connection_complete_ev{ + __u8 status; // error code 00 status ok + __u16 con_handle; + bt_address adress; + __u8 link_type; + __u8 encrypt_mode; +} __attribute__ ((packed)); + +#define CONNECTION_REQUEST_EV 0x04 +struct hci_connection_request_ev{ + bt_address adress; + cl_device cod; //clas of device + __u8 link_type; +} __attribute__ ((packed)); + +#define DISCONNECTION_COMPLETE_EV 0x05 +struct hci_disconnection_complete_ev{ + __u8 status; // error code 00 status ok + __u16 con_handle; + __u8 reason; +} __attribute__ ((packed)); + +#define REMOTE_NAME_REQUEST_EV 0x07 +typedef struct { + __u8 status; + bt_address address; + __u8 remote_name[247]; // array for 248 Octets +} __attribute__ ((packed)) hci_remote_name_request_complete_ev; + +#define CMD_COMPLETE_EV 0x0E +#define CMD_COMPLETE_EV_SIZE 3 +// !!!!!! +typedef struct { + __u8 cmd_packets_number; + __u16 cmd_opcode; + __u8 return_param[31]; +} __attribute__ ((packed)) hci_cmd_complete_ev; + +#define CMD_STATUS_EV 0x0F +typedef struct { + __u8 status; + __u8 cmd_packets_number; + __u16 cmd_opcode; +} __attribute__ ((packed)) hci_cmd_status_ev; + +#define HARDWARE_ERROR_EV 0x10 +struct hci_hardware_error_ev{ + __u8 hw_code; +} __attribute__ ((packed)); + +extern void call_hci_inquiry_cmd(dev_lap lap, __u8 inq_length, __u8 resp_count); +extern int call_hci_read_bd_addr_cmd(int dd, bt_address *p_bdaddr, int timeout); +extern int hci_open_device(int dev_id); +extern int hci_send_command(int dd, hci_request *p_req); +extern int hci_send_request(int dd, hci_request *p_req,int timeout); +extern void print_device_list(int ctl); +extern int hci_close_dev(int dd); + + +extern void assemble_hci_data(void *p_con_handle,void *p_data_size,void *p_data); +extern void assemble_ocf_ogf(__u8 ogf,__u8 ocf,__u16 *p_ocf_ogf); +extern __u8 swap8(__u8 byte1); +extern __u16 swap16(__u16 byte2); +//extern void bacpy(bt_address p_dest, bt_address p_source); +extern void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth); +extern void printba(bt_address *ba); + +static inline void hci_set_bit(int nr, void *addr) +{ + *((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31)); +} + +static inline void hci_clear_bit(int nr, void *addr) +{ + *((uint32_t *) addr + (nr >> 5)) &= ~(1 << (nr & 31)); +} + +static inline int hci_test_bit(int nr, void *addr) +{ + return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31)); +} + +/* HCI filter tools */ +static inline void hci_filter_clear(struct hci_filter *f) +{ + memset(f, 0, sizeof(*f)); +} +static inline void hci_filter_set_ptype(int t, struct hci_filter *f) +{ + hci_set_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline void hci_filter_clear_ptype(int t, struct hci_filter *f) +{ + hci_clear_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline int hci_filter_test_ptype(int t, struct hci_filter *f) +{ + return hci_test_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline void hci_filter_all_ptypes(struct hci_filter *f) +{ + memset((void *) &f->type_mask, 0xff, sizeof(f->type_mask)); +} +static inline void hci_filter_set_event(int e, struct hci_filter *f) +{ + hci_set_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline void hci_filter_clear_event(int e, struct hci_filter *f) +{ + hci_clear_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline int hci_filter_test_event(int e, struct hci_filter *f) +{ + return hci_test_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline void hci_filter_all_events(struct hci_filter *f) +{ + memset((void *) f->event_mask, 0xff, sizeof(f->event_mask)); +} +static inline void hci_filter_set_opcode(int opcode, struct hci_filter *f) +{ + f->opcode = opcode; +} +static inline void hci_filter_clear_opcode(struct hci_filter *f) +{ + f->opcode = 0; +} +static inline int hci_filter_test_opcode(int opcode, struct hci_filter *f) +{ + return (f->opcode == opcode); +} + +static inline int bacmp(const bt_address *ba1, const bt_address *ba2) +{ + return memcmp(ba1, ba2, sizeof(bt_address)); +} +static inline void bacpy(bt_address *dst, const bt_address *src) +{ + memcpy(dst, src, sizeof(bt_address)); +} + + +#endif /* __HCIDRIVER */ diff --git a/testf/sockfree.c b/testf/sockfree.c new file mode 100644 index 0000000..14c64da --- /dev/null +++ b/testf/sockfree.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include "hcidriver.h" + +#define BTPROTO_HCI 1 + + + +int main(void){ + int sock,sock2; + sockaddr_hci address,address2; + + if((sock=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){ + printf("first socket: %d wasn't created\n",sock); + + } + + if((sock2=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){ + printf("second socket: %d wasn't created\n",sock2); + } + + memset(&address,0,sizeof(address)); + address.hci_family=AF_BLUETOOTH; + address.dev_id=0; + + memset(&address2,0,sizeof(address2)); + address2.hci_family=AF_BLUETOOTH; + address2.dev_id=1; + + if(bind(sock,(struct sockaddr *) &address, sizeof(address))<0){ + printf("Socket: %d wasn't binded\n",sock); + close(sock); + return -1; + } + printf("Socket: %d was binded\n",sock); + + if(bind(sock2,(struct sockaddr *) &address2, sizeof(address2))<0){ + printf("Socket: %d wasn't binded\n",sock2); + close(sock2); + return -1; + } + printf("Socket: %d was binded\n",sock2); + + + printf("Value of sock is now %d \n",sock); + printf("Value of sock2 is now %d \n",sock2); + close(sock); + printf("Socket: %d was closed\n",sock); + return 0; +} + diff --git a/testf/t1/Makefile b/testf/t1/Makefile new file mode 100644 index 0000000..b1f42bc --- /dev/null +++ b/testf/t1/Makefile @@ -0,0 +1,2 @@ +rectest: rectest.c + gcc -Wall -o rectest rectest.c \ No newline at end of file diff --git a/testf/t1/hcidriver.h b/testf/t1/hcidriver.h new file mode 100644 index 0000000..13e369e --- /dev/null +++ b/testf/t1/hcidriver.h @@ -0,0 +1,478 @@ +// +// C++ Interface: hcidriver +// +// Description: +// +// +// Author: root , (C) 2008 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#ifndef __HCIDRIVER +#define __HCIDRIVER + +#include +#include +#include +#include + +/* HCI ioctl defines */ +#define HCIDEVUP _IOW('H', 201, int) +#define HCIDEVDOWN _IOW('H', 202, int) +#define HCIDEVRESET _IOW('H', 203, int) +#define HCIDEVRESTAT _IOW('H', 204, int) + +#define HCIGETDEVLIST _IOR('H', 210, int) +#define HCIGETDEVINFO _IOR('H', 211, int) +#define HCIGETCONNLIST _IOR('H', 212, int) +#define HCIGETCONNINFO _IOR('H', 213, int) + +#define HCISETRAW _IOW('H', 220, int) +#define HCISETSCAN _IOW('H', 221, int) +#define HCISETAUTH _IOW('H', 222, int) +#define HCISETENCRYPT _IOW('H', 223, int) +#define HCISETPTYPE _IOW('H', 224, int) +#define HCISETLINKPOL _IOW('H', 225, int) +#define HCISETLINKMODE _IOW('H', 226, int) +#define HCISETACLMTU _IOW('H', 227, int) +#define HCISETSCOMTU _IOW('H', 228, int) + +#define HCISETSECMGR _IOW('H', 230, int) + +#define HCIINQUIRY _IOR('H', 240, int) + +/* HCI socket options*/ +#define HCI_FILTER 2 +#define SOL_HCI 0 + +#define HCI_FLT_TYPE_BITS 31 +#define HCI_FLT_EVENT_BITS 63 +#define HCI_FLT_OGF_BITS 63 +#define HCI_FLT_OCF_BITS 127 + +/* HCI data types */ +#define HCI_COMMAND_PKT 0x01 +#define HCI_ACLDATA_PKT 0x02 +#define HCI_EVENT_PKT 0x04 +#define HCI_VENDOR_PKT 0xFF + +/* HCI packet types */ +#define HCI_DM1 0x0008 +#define HCI_DM3 0x0400 +#define HCI_DM5 0x4000 +#define HCI_DH1 0x0010 +#define HCI_DH3 0x0800 +#define HCI_DH5 0x8000 + +#define HCI_HV1 0x0020 +#define HCI_HV2 0x0040 +#define HCI_HV3 0x0080 + +/* ACL flags */ +#define ACL_CONT 0x01 +#define ACL_START 0x02 +#define ACL_ACTIVE_BCAST 0x04 +#define ACL_PICO_BCAST 0x08 + +/* Connection states */ +#define DISCONNECTED 0 +#define CONNECTED 1 +#define PENDING 2 + +/* BT protocols*/ +#define BTPROTO_L2CAP 0 +#define BTPROTO_HCI 1 +#define BTPROTO_SCO 2 +#define BTPROTO_RFCOMM 3 +#define BTPROTO_BNEP 4 +#define BTPROTO_CMTP 5 +#define BTPROTO_HIDP 6 +#define BTPROTO_AVDTP 7 + +#define SOL_HCI 0 +#define SOL_L2CAP 6 +#define SOL_SCO 17 +#define SOL_RFCOMM 18 + +#define HCI_MAX_DEV 4 +#define HCI_MAX_EVENT_SIZE 260 + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + + +/* BD_ADDR */ +typedef struct{ + __u8 byte[6]; +} __attribute__((packed)) bt_address; + +typedef struct{ + __u8 byte[3]; +} __attribute__((packed)) dev_lap; + + + +/* Class of device*/ + +typedef struct{ + __u8 byte[3]; +}cl_device; + +struct hci_dev_stats { + __u32 err_rx; + __u32 err_tx; + __u32 cmd_tx; + __u32 evt_rx; + __u32 acl_tx; + __u32 acl_rx; + __u32 sco_tx; + __u32 sco_rx; + __u32 byte_rx; + __u32 byte_tx; +}; + +typedef struct{ + __u16 dev_id; + char name[8]; + + bt_address bdaddr; + + __u32 flags; + __u8 type; + + __u8 features[8]; + + __u32 pkt_type; + __u32 link_policy; + __u32 link_mode; + + __u16 acl_mtu; + __u16 acl_pkts; + __u16 sco_mtu; + __u16 sco_pkts; + + struct hci_dev_stats stat; +}bt_device; + + +typedef struct{ + __u16 dev_id; + __u32 dev_opt; +}bt_device_req; + +typedef struct{ + __u16 dev_num; + bt_device_req dev_req[HCI_MAX_DEV]; +}bt_device_req_list; + +typedef struct{ + __u16 *p_OCF_OGF; + void *p_cmdp; + void *p_retcmdp; + __u16 cmdp_len; + __u16 retcmdp_len; + int event; //for setting event filter , some hci commands have own events +} hci_request; + +struct hci_filter{ + __u32 type_mask; + __u32 event_mask[2]; + __u16 opcode; +}; + +/* Socket address*/ +typedef struct{ + sa_family_t hci_family; + __u8 dev_id; +}sockaddr_hci; + +typedef struct{ + __u16 con_id; + __u8 con_state; + int socket_fd; +}connection_hci; + + +/*HCI packet headers*/ + +typedef struct { + uint16_t opcode; /* OCF & OGF */ + uint8_t plen; +} __attribute__ ((packed)) hci_command_hdr; +#define HCI_COMMAND_HDR_SIZE 3 + +typedef struct { + uint8_t evt; + uint8_t plen; +} __attribute__ ((packed)) hci_event_hdr; +#define HCI_EVENT_HDR_SIZE 2 + +typedef struct { + uint16_t handle; /* Handle & Flags(PB, BC) */ + uint16_t dlen; +} __attribute__ ((packed)) hci_acl_hdr; +#define HCI_ACL_HDR_SIZE 4 + + + +/*HCI Commands */ +/*Link control commands*/ +#define OCF_INQUIRY_CMD 0x0001 +#define HCI_INQUIRY_CMD_PARL 0x05 +typedef struct { + __u8 lap[3]; + __u8 inquiry_length; + __u8 num_responces; +} __attribute__ ((packed)) hci_inquiry_cmd; + +#define OCF_INQUIRY_CANCEL_CMD 0x0002 + +#define OCF_CREATE_CONNECTION_CMD 0x0005 +#define HCI_CREATE_CONNECTION_CMD_PARL 0x0D +typedef struct { + bt_address address; + __u16 packet_type; + __u8 rep_mode; + __u8 reserved; + __u16 clock_offset; + __u8 role_switch; +} __attribute__ ((packed)) hci_create_connection_cmd; + +#define OCF_DISCONNECT_CMD 0x0006 +struct hci_disconnect_cmd{ + __u8 reason; +} __attribute__ ((packed)); + +#define OCF_CREATE_CONNECTION_CANCEL_CMD 0x0008 +struct hci_create_connection_cancel_cmd{ + bt_address address; +} __attribute__ ((packed)); + +#define OCF_ACCEPT_CONNECTION_REQ_CMD 0x0009 +struct hci_accept_connection_req_cmd{ + bt_address address; + __u8 role; +} __attribute__ ((packed)); + +#define OCF_REJECT_CONNECTION_REQ_CMD 0x000A +struct hci_reject_connection_req_cmd{ + bt_address address; + __u8 reason; +} __attribute__ ((packed)); + +#define OCF_REMOTE_NAME_REQ_CMD 0x0019 +typedef struct { + bt_address address; + __u8 rep_mode; + __u8 reserved; + __u16 clock_offset; +} __attribute__ ((packed)) hci_remote_name_req_cmd; + +/*Link policy commands*/ +#define OCF_RESET_CMD 0x0003 + + +/*Informational parameters commands */ +#define OGF_INFO_CMD 0x04 + +#define OCF_READ_BD_ADDR_CMD 0x0009 +#define READ_BD_ADDR_CMD_PARL 7 +typedef struct{ + + //return parameters + __u8 status; + bt_address address; +} __attribute__ ((packed)) read_bd_addr_cmd; + +/*Controllers and baseband commands */ +#define OGF_CON_AND_BSB_CMD 0x03 + +#define OCF_READ_LOCAL_NAME_CMD 0x0014 + + + +/* HCI events */ + +#define INQUIRY_COMPLETE_EV 0x01 + +typedef struct { + __u8 status; +} __attribute__ ((packed)) hci_inquiry_complete_ev; + +#define INQUIRY_RESULT_EV 0x02 +#define INQUIRY_RESULT_EV_PARL 0x0F + //lenght for one responsing device +typedef struct { + __u8 num_responces; + bt_address ba_array; + __u8 rep_mode_array; + __u8 per_mode_array; + __u8 reserved_array; + cl_device cod_array; + __u16 clock_offset_array; +} __attribute__ ((packed)) hci_inquiry_result_ev; + +#define INQUIRY_RESULT_RSSI_EV 0x22 +#define INQUIRY_RESULT_RSSI_EV_PARL 0x0F + //lenght for one responsing device +typedef struct { + __u8 num_responces; + bt_address ba_array; + __u8 rep_mode_array; + __u8 per_mode_array; + cl_device dev_class_array; + __u16 clock_offset_array; + __u8 RSSI_array; +} __attribute__ ((packed)) hci_inquiry_result_RSSI_ev; + +#define CONNECTION_COMPLETE_EV 0x03 +#define CONNECTION_COMPLETE_EV_PARL 0x0B +struct hci_connection_complete_ev{ + __u8 status; // error code 00 status ok + __u16 con_handle; + bt_address adress; + __u8 link_type; + __u8 encrypt_mode; +} __attribute__ ((packed)); + +#define CONNECTION_REQUEST_EV 0x04 +struct hci_connection_request_ev{ + bt_address adress; + cl_device cod; //clas of device + __u8 link_type; +} __attribute__ ((packed)); + +#define DISCONNECTION_COMPLETE_EV 0x05 +struct hci_disconnection_complete_ev{ + __u8 status; // error code 00 status ok + __u16 con_handle; + __u8 reason; +} __attribute__ ((packed)); + +#define REMOTE_NAME_REQUEST_EV 0x07 +typedef struct { + __u8 status; + bt_address address; + __u8 remote_name[247]; // array for 248 Octets +} __attribute__ ((packed)) hci_remote_name_request_complete_ev; + +#define CMD_COMPLETE_EV 0x0E +#define CMD_COMPLETE_EV_SIZE 3 +// !!!!!! +typedef struct { + __u8 cmd_packets_number; + __u16 cmd_opcode; + __u8 return_param[31]; +} __attribute__ ((packed)) hci_cmd_complete_ev; + +#define CMD_STATUS_EV 0x0F +typedef struct { + __u8 status; + __u8 cmd_packets_number; + __u16 cmd_opcode; +} __attribute__ ((packed)) hci_cmd_status_ev; + +#define HARDWARE_ERROR_EV 0x10 +struct hci_hardware_error_ev{ + __u8 hw_code; +} __attribute__ ((packed)); + +extern int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout); +extern int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address, int timeout); +extern int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout); +extern int hci_open_device(int dev_id); +extern int hci_send_command(int dd, hci_request *p_req); +extern int hci_send_request(int dd, hci_request *p_req,int timeout); +extern void print_device_list(int ctl); +extern int hci_close_dev(int dd); + + +extern void assemble_hci_data(void *p_con_handle,void *p_data_size,void *p_data); +extern void assemble_ocf_ogf(__u8 ogf,__u8 ocf,__u16 *p_ocf_ogf); +extern __u8 swap8(__u8 byte1); +extern __u16 swap16(__u16 byte2); +//extern void bacpy(bt_address p_dest, bt_address p_source); +extern void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth); +extern void printba(bt_address *ba); +extern int compare_bda(bt_address *p_first, bt_address *p_second); +extern void fill_zero(bt_address *p_addr); +extern void swap_addrbytes(bt_address *p_addr); + + +static inline void hci_set_bit(int nr, void *addr) +{ + *((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31)); +} + +static inline void hci_clear_bit(int nr, void *addr) +{ + *((uint32_t *) addr + (nr >> 5)) &= ~(1 << (nr & 31)); +} + +static inline int hci_test_bit(int nr, void *addr) +{ + return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31)); +} + +/* HCI filter tools */ +static inline void hci_filter_clear(struct hci_filter *f) +{ + memset(f, 0, sizeof(*f)); +} +static inline void hci_filter_set_ptype(int t, struct hci_filter *f) +{ + hci_set_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline void hci_filter_clear_ptype(int t, struct hci_filter *f) +{ + hci_clear_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline int hci_filter_test_ptype(int t, struct hci_filter *f) +{ + return hci_test_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); +} +static inline void hci_filter_all_ptypes(struct hci_filter *f) +{ + memset((void *) &f->type_mask, 0xff, sizeof(f->type_mask)); +} +static inline void hci_filter_set_event(int e, struct hci_filter *f) +{ + hci_set_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline void hci_filter_clear_event(int e, struct hci_filter *f) +{ + hci_clear_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline int hci_filter_test_event(int e, struct hci_filter *f) +{ + return hci_test_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); +} +static inline void hci_filter_all_events(struct hci_filter *f) +{ + memset((void *) f->event_mask, 0xff, sizeof(f->event_mask)); +} +static inline void hci_filter_set_opcode(int opcode, struct hci_filter *f) +{ + f->opcode = opcode; +} +static inline void hci_filter_clear_opcode(struct hci_filter *f) +{ + f->opcode = 0; +} +static inline int hci_filter_test_opcode(int opcode, struct hci_filter *f) +{ + return (f->opcode == opcode); +} + +static inline int bacmp(const bt_address *ba1, const bt_address *ba2) +{ + return memcmp(ba1, ba2, sizeof(bt_address)); +} +static inline void bacpy(bt_address *dst, const bt_address *src) +{ + memcpy(dst, src, sizeof(bt_address)); +} + + +#endif /* __HCIDRIVER */ diff --git a/testf/t1/rectest b/testf/t1/rectest new file mode 100755 index 0000000..49e0e35 Binary files /dev/null and b/testf/t1/rectest differ diff --git a/testf/t1/rectest.c b/testf/t1/rectest.c new file mode 100644 index 0000000..b169f8a --- /dev/null +++ b/testf/t1/rectest.c @@ -0,0 +1,419 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "hcidriver.h" + +int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address, int timeout){ + read_bd_addr_cmd cmdp,*p_cmdp; //command parameters return/comand + hci_request req,*p_req; + __u16 OCF_OGF,*p_OCF_OGF; + p_OCF_OGF=&OCF_OGF; + p_cmdp=&cmdp; + p_req=&req; + + + memset(p_req,0,sizeof(req)); + assemble_ocf_ogf(0x09,0x04,p_OCF_OGF); + p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_retcmdp=p_cmdp; + p_req->retcmdp_len=READ_BD_ADDR_CMD_PARL; + p_req->cmdp_len=0; + p_req->event=CMD_COMPLETE_EV; + + if(hci_send_request(dd,p_req,timeout)<0) + return -1; + if(p_cmdp->status) + return -1; + + bacpy(p_address, &cmdp.address); + return 0; +} + +int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout){ + hci_inquiry_cmd cmdp,*p_cmdp=&cmdp; + hci_request req,*p_req=&req; + + hci_inquiry_complete_ev com_ev,*p_com_ev=&com_ev; + __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF; + + p_cmdp->lap[0]=0x33; + p_cmdp->lap[1]=0x8b; + p_cmdp->lap[2]=0x9e; + p_cmdp->inquiry_length=5; + p_cmdp->num_responces=5; + memset(p_req,0,sizeof(req)); + assemble_ocf_ogf(0x01,0x01,p_OCF_OGF); + p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_retcmdp=p_addressarray; + p_req->p_cmdp=p_cmdp; + p_req->retcmdp_len=INQUIRY_RESULT_EV_PARL; + p_req->cmdp_len=HCI_INQUIRY_CMD_PARL; + p_req->event=INQUIRY_RESULT_EV; + + if(hci_send_request(dd,p_req,timeout)<0) + return -1; + if(p_com_ev->status) + return -1; + + + return 0; +} + +int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout){ + hci_create_connection_cmd cmdp,*p_cmdp=&cmdp; + hci_request req,*p_req=&req; + __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF; + + p_cmdp->address=*p_address; + p_cmdp->packet_type=0x0010; + p_cmdp->rep_mode=0x01; + p_cmdp->reserved=0x00; + p_cmdp->clock_offset=0xf000; + p_cmdp->role_switch=0x00; + memset(p_req,0,sizeof(req)); + assemble_ocf_ogf(0x05,0x01,p_OCF_OGF); + p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_cmdp=p_cmdp; + p_req->cmdp_len=HCI_CREATE_CONNECTION_CMD_PARL; + p_req->event=CONNECTION_COMPLETE_EV; + + if(hci_send_request(dd,p_req,timeout)<0) + return -1; + //if(p_com_ev->status) + // return -1; + + + return 0; +} + + +int hci_open_device(int dev_id){ + sockaddr_hci address; + int dd; + + if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){ + perror("socket no created"); + return dd; //it return -1 when socket isn't created + } + + memset(&address,0,sizeof(address)); + address.hci_family=AF_BLUETOOTH; + address.dev_id=dev_id; + if(bind(dd,(struct sockaddr *) &address, sizeof(address))<0){ + perror("Socket not binded to hci device"); + close(dd); + return -1; + } + + return dd; +} + +int hci_close_dev(int dd){ + return close(dd); +} + +int hci_send_command(int dd, hci_request *p_req){ + __u8 array[p_req->cmdp_len+4]; //type + OCF+OGF+plen + int ii; + array[0]=0x01; + memcpy(&array[1],p_req->p_OCF_OGF,2); + array[3]= p_req->cmdp_len; + if(p_req->cmdp_len > 0){ + memcpy(&array[4],p_req->p_cmdp,p_req->cmdp_len); // !!!!!!!!! segmentation fault + } + + for(ii=0;iicmdp_len+4))<0){ + printf("write was interupted: %d",dd); + if(errno == EAGAIN || errno == EINTR) + continue; + return -1; + } + return 0; +} + +int hci_send_request(int dd, hci_request *p_req,int timeout){ + __u8 recbuf[HCI_MAX_EVENT_SIZE],*p_recbuf; + int j,count=0; + socklen_t len; + hci_event_hdr *p_hdr; + struct hci_filter nf, of; + int try_count, sign=0;; + bt_address *p_actual; + + + + len = sizeof(of); + if(getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &len)<0){ + printf("some problem with getsockopt: %d",dd); + return -1; + } + + hci_filter_clear(&nf); + hci_filter_set_ptype(HCI_EVENT_PKT, &nf); + hci_filter_set_event(CMD_STATUS_EV, &nf); + hci_filter_set_event(INQUIRY_COMPLETE_EV, &nf); + hci_filter_set_event(INQUIRY_RESULT_RSSI_EV, &nf); + hci_filter_set_event(p_req->event, &nf); + hci_filter_set_opcode(*(p_req->p_OCF_OGF), &nf); + + if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf))<0){ + printf("some problem with setsockopt: %d",dd); + return -1; + } + + if(hci_send_command(dd, p_req)<0){ + printf("some problems with sending command: %d",dd); + goto fail; + } + + try_count= 10; + + while(try_count--){ + printf("try %d \n",try_count); + hci_cmd_complete_ev *cc; + hci_cmd_status_ev *cs; + hci_remote_name_request_complete_ev *rnrc; + hci_remote_name_req_cmd *cpar; + hci_inquiry_result_RSSI_ev *p_res_ev; + + + + if(timeout){ + struct pollfd p; + int n; + + p.fd = dd; p.events = POLLIN; + while((n = poll(&p, 1, timeout)) < 0){ //pool return 1 when there are some incoming data + if(errno == EAGAIN || errno == EINTR) + continue; + perror("Problem with poll"); + goto fail; + } + + if (!n) { //time out pool return 0 + errno = ETIMEDOUT; + perror("Poll return 0 timeout"); + printf("exited pool timeout \n "); + goto fail; + } + + timeout -= 10; + if(timeout < 0) timeout = 0; + + } + printf("Timeout after round is: %d \n",timeout); + + while((len=read(dd, recbuf, sizeof(recbuf)))<0){ + if(errno == EAGAIN || errno == EINTR) + continue; + perror("Problem with cmd sending"); + goto fail; + } + printf("Count of received bytes %d \n ",len); + for(j=0;jevt){ + case CMD_STATUS_EV: + cs = (void *) p_recbuf; + if(cs->cmd_opcode != *(p_req->p_OCF_OGF)) + continue; + if(p_req->event != CMD_STATUS_EV){ + if(cs->status){ // if there is something except 0 it is error + errno = EIO; + perror("Some error state has occured on receive"); + goto fail; + } + break; + } + p_req->retcmdp_len = min(len, p_req->retcmdp_len); + memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); + goto succes; + + case CMD_COMPLETE_EV: + cc = (void *) p_recbuf; + if(cc->cmd_opcode != *(p_req->p_OCF_OGF)) + continue; + p_recbuf += CMD_COMPLETE_EV_SIZE; + len -= CMD_COMPLETE_EV_SIZE; + + p_req->retcmdp_len = min(len, p_req->retcmdp_len); + memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); + printf("retcmdp lenght: %d \n",p_req->retcmdp_len); + goto succes; + // __u8 first,*p_first; + // p_first=&first; + // for(j=0;jretcmdp_len;j++){ + // memcpy(p_first,p_req->p_retcmdp+j,1); + // printf("%d. byte is: %X \n",j,first); + // printf(" %X",*((unsigned int *)p_req->p_retcmdp+j)); + //printf("address of p_req: %d \n",p_req); + //printf("address of p_req: %d \n",p_req->p_retcmdp); + // } + // printf("Ok\n"); + + + case REMOTE_NAME_REQUEST_EV: + if(p_hdr->evt != p_req->event) + break; + rnrc = (void *) p_recbuf; + cpar = p_req->p_cmdp; + + if(bacmp(&rnrc->address, &cpar->address)) + continue; + + p_req->retcmdp_len = min(len, p_req->retcmdp_len); + memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); + goto succes; + + case INQUIRY_RESULT_EV: + printf("bingo inquiry result event \n"); + break; + //goto succes; + + case INQUIRY_RESULT_RSSI_EV: + printf("bingo inquiry result RSSI event \n"); + p_res_ev = (void *) p_recbuf; + p_actual = &(p_res_ev->ba_array); + for(j=0;jp_retcmdp+(j*sizeof(bt_address)))) == 1){ + sign=1; + } //im looking for all array members an compare with actual address + } + if(!sign){ // if in array address doesnt exist i will add it to array + *(bt_address*)(p_req->p_retcmdp+(count*sizeof(bt_address)))= *p_actual; + count++; + } + if(count==0){ + *(bt_address*)(p_req->p_retcmdp)= *p_actual; + count++; + } + + //printf("type %2.2X \n",((hci_inquiry_result_RSSI_ev *)p_req->p_retcmdp)->RSSI_array); + break; + //goto succes; + + case INQUIRY_COMPLETE_EV: + printf("bingo inquiry complete event \n"); + goto succes; + + + default: if(p_hdr->evt != p_req->event) + break; + p_req->retcmdp_len = min(len, p_req->retcmdp_len); + memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); + goto succes; + + } + + + } + errno = ETIMEDOUT; + return -1; + + +fail: + setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of)); + hci_close_dev(dd); + return -1; +succes: + setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of)); + return 0; + +} + +void assemble_ocf_ogf(__u8 ocf,__u8 ogf,__u16 *p_ocf_ogf){ + __u16 var1; + __u16 result; + //result=ocf; + //result=(result<<8); + //var1=(ogf<<2); + //*p_ocf_ogf=(result|var1); + var1=(ogf<<10); + result=ocf; + *p_ocf_ogf=(result|var1); +} + +void printba(bt_address *ba){ + printf("address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X: \n",ba->byte[0],ba->byte[1],ba->byte[2],ba->byte[3],ba->byte[4],ba->byte[5]); +} + +int compare_bda(bt_address *p_first, bt_address *p_second){ + __u8 i,k=0; + for(i=0;ibyte[i]==p_second->byte[i]) + k++; + } + if(k==sizeof(bt_address)) return 1; //all bytes are similar + + return 0; //addreses are different in one byte at least +} + +void fill_zero(bt_address *p_addr){ + __u8 i; + for(i=0;ibyte[i]=0x00; + } +} +void swap_addrbytes(bt_address *p_addr){ + bt_address help,*p_help=&help; + __u8 i; + for(i=0;ibyte[i]=p_addr->byte[5-i]; + } + *p_addr=*p_help; +} + + + +int main(void){ + bt_address addressarray[HCI_MAX_DEV]; + bt_address address,*p_address; + p_address=&address; + int dd,i; + + memset(addressarray,0,sizeof(bt_address)*HCI_MAX_DEV); + + if((dd=hci_open_device(0))<0){ + printf("some problem with socket creating or binding: %d \n",dd); + return -1; + } + if(call_hci_read_bd_addr_cmd(dd,p_address,1000)<0){ + printf("some problem with call_hci_read_bd_addr: %d \n",dd); + return -1; + + } + printba(p_address); + + if(call_hci_inquiry_cmd(dd,addressarray,3000)<0){ + printf("some problem with call_hci_inquiry_cmd: %d \n",dd); + return -1; + } + for(i=0;i