├── Makefile ├── README.md ├── build ├── hi3518ev200.make ├── jz2440.make └── ubuntu.make ├── component ├── Makefile ├── module │ ├── Makefile │ └── cbus.h └── protocol │ ├── Makefile │ ├── coap │ └── Makefile │ ├── http │ └── Makefile │ ├── mqtt │ └── Makefile │ ├── protocol.c │ └── protocol.h ├── docs ├── README.md └── UML │ ├── LinuxMultiTask.vpp │ ├── LinuxMultiTask.vpp.bak_031f │ ├── LinuxMultiTask.vpp.bak_032d │ ├── LinuxMultiTask.vpp.bak_033d │ ├── LinuxMultiTask.vpp.bak_034d │ ├── LinuxMultiTask.vpp.bak_035d │ ├── LinuxMultiTask.vpp.bak_036d │ ├── LinuxMultiTask.vpp.bak_037d │ ├── LinuxMultiTask.vpp.bak_038d │ ├── LinuxMultiTask.vpp.bak_039d │ ├── LinuxMultiTask.vpp.bak_040d │ ├── LinuxMultiTask.vpp.bak_041d │ ├── LinuxMultiTask.vpp.bak_042d │ ├── LinuxMultiTask.vpp.bak_043d │ ├── LinuxMultiTask.vpp.bak_044d │ ├── LinuxMultiTask.vpp.bak_045d │ ├── LinuxMultiTask.vpp.bak_046d │ ├── LinuxMultiTask.vpp.bak_047d │ ├── LinuxMultiTask.vpp.bak_048d │ ├── LinuxMultiTask.vpp.bak_049d │ ├── LinuxMultiTask.vpp.bak_050d │ ├── LinuxMultiTask.vpp.bak_051d │ ├── LinuxMultiTask.vpp.bak_052d │ ├── LinuxMultiTask.vpp.bak_053d │ ├── LinuxMultiTask.vpp.bak_054d │ ├── LinuxMultiTask.vpp.bak_055d │ ├── LinuxMultiTask.vpp.bak_056d │ ├── LinuxMultiTask.vpp.bak_057d │ ├── LinuxMultiTask.vpp.bak_058d │ ├── LinuxMultiTask.vpp.bak_059d │ ├── LinuxMultiTask.vpp.bak_060d │ ├── LinuxMultiTask.vpp.bak_061d │ └── LinuxMultiTask.vpp.bak_062f ├── include ├── inits.h ├── klist.h ├── multitask.h └── version.h ├── init ├── Makefile └── main.c ├── platform ├── Makefile ├── bsp │ └── Makefile └── os │ ├── Makefile │ ├── freertos │ └── Makefile │ ├── include │ ├── os_async_queue.h │ ├── os_broadcast.h │ ├── os_bus.h │ ├── os_evdriver.h │ ├── os_file.h │ ├── os_io.h │ ├── os_log.h │ ├── os_misc.h │ ├── os_mm.h │ ├── os_msg.h │ ├── os_multicast.h │ ├── os_queue.h │ ├── os_stdlibc.h │ ├── os_sys.h │ ├── os_task.h │ ├── os_tcp_client.h │ ├── os_tcp_server.h │ ├── os_thread.h │ ├── os_threadpool.h │ ├── os_timer.h │ └── os_type.h │ ├── linux │ ├── Makefile │ ├── base │ │ ├── Makefile │ │ ├── os_async_queue.c │ │ ├── os_broadcast.c │ │ ├── os_bus.c │ │ ├── os_evdriver.c │ │ ├── os_file.c │ │ ├── os_io.c │ │ ├── os_log.c │ │ ├── os_misc.c │ │ ├── os_mm.c │ │ ├── os_msg.c │ │ ├── os_multicast.c │ │ ├── os_queue.c │ │ ├── os_stdlibc.c │ │ ├── os_sys.c │ │ ├── os_task.c │ │ ├── os_tcp_client.c │ │ ├── os_tcp_server.c │ │ ├── os_thread.c │ │ ├── os_threadpool.c │ │ └── os_timer.c │ └── ubus │ │ ├── Makefile │ │ ├── json-c.tar.gz │ │ ├── libubox.tar.gz │ │ └── ubus.tar.gz │ ├── linux_glib │ ├── Makefile │ ├── base │ │ ├── Makefile │ │ ├── os_async_queue.c │ │ ├── os_broadcast.c │ │ ├── os_bus.c │ │ ├── os_evdriver.c │ │ ├── os_log.c │ │ ├── os_misc.c │ │ ├── os_mm.c │ │ ├── os_msg.c │ │ ├── os_multicast.c │ │ ├── os_queue.c │ │ ├── os_stdlibc.c │ │ ├── os_task.c │ │ ├── os_tcp_client.c │ │ ├── os_tcp_server.c │ │ ├── os_thread.c │ │ └── os_threadpool.c │ └── glib │ │ ├── Makefile │ │ ├── glib-2.40.2.tar.gz │ │ ├── libffi-3.2.1.tar.gz │ │ └── zlib-1.2.11.tar.gz │ └── ros │ └── Makefile ├── sample ├── Makefile ├── mm_broadcast.c ├── sample.c ├── task.c ├── task_broadcast.c ├── task_json.c ├── task_multicast.c ├── task_show.c ├── tcp_client.c ├── tcp_server.c ├── udp_tcp_client.c └── udp_tcp_server.c ├── scripts ├── Makefile.build ├── Makefile.function ├── Makefile.test.build ├── common.sh ├── create_3thlibs.sh ├── create_shared_lib.sh └── create_so.sh ├── service └── Makefile └── util ├── Makefile ├── parson.c └── parson.h /Makefile: -------------------------------------------------------------------------------- 1 | # export LD_LIBRARY_PATH= 2 | 3 | PHONY : all 4 | 5 | TARGET_NAME ?= target/bin/multitask 6 | LIBCOMM_D_NAME ?= target/lib/libmultitask.so 7 | LIBCOMM_S_NAME ?= target/lib/libmultitask.a 8 | LD_COMM_NAME ?= -lmultitask 9 | 10 | export LD_COMM_NAME 11 | ############################# 12 | 13 | # Shell command ########### 14 | TAR=tar 15 | CP=/bin/cp 16 | RM=/bin/rm -f 17 | GREP=grep 18 | SED=sed 19 | MKDIR=mkdir -p 20 | CHMOD=chmod 21 | MV=mv 22 | CD=cd 23 | LN=ln 24 | MAKE=make 25 | MKNOD=mknod 26 | PUSHD=pushd 27 | POPD=popd 28 | RMDIR=rmdir 29 | DEPMOD=/sbin/depmod 30 | RMDIR=rmdir 31 | MKIMG=mkimage 32 | PATCH=patch 33 | DIFF=diff 34 | TOUCH=touch 35 | CAT=cat 36 | PWD=`pwd` 37 | HOME=$(shell cd ~ && pwd) 38 | 39 | # How to install. If your install program does not support "-p", then 40 | # you may have to run ranlib on the installed liblua.a. 41 | INSTALL= install -p 42 | INSTALL_EXEC= $(INSTALL) -m 0755 43 | INSTALL_DATA= $(INSTALL) -m 0644 44 | 45 | export MKDIR MAKE PWD HOME INSTALL INSTALL_EXEC INSTALL_DATA 46 | #**************************************************************************** 47 | # functions defined: LOG 48 | #**************************************************************************** 49 | # for debug 50 | DEBUG_LOG_FILE='&2' 51 | DEBUG_LOG_LEVEL=0 52 | 53 | # ANSI COLORS for shell scrpts 54 | COLOR_CRE="\033[39m" 55 | COLOR_NORMAL="\033[0m" 56 | COLOR_RED="\033[31m" 57 | COLOR_GREEN="\033[32m" 58 | COLOR_YELLOW="\033[33m" 59 | COLOR_BLUE="\033[34m" 60 | COLOR_MAGENTA="\033[36m" 61 | COLOR_CYAN="\033[35m" 62 | COLOR_WHITE="\033[37m" 63 | 64 | #$1 string 65 | log-err = $(shell echo -e ${COLOR_RED} ERR: ${1}${COLOR_NORMAL} >&2 ) 66 | 67 | #$1 string 68 | log-warn = $(shell echo -e ${COLOR_YELLOW} WARN: ${1}${COLOR_NORMAL} >&2 ) 69 | 70 | #$1 string 71 | log-cmd = $(shell echo -e ${COLOR_GREEN} CMD: ${1}${COLOR_NORMAL} >&2 ) 72 | 73 | # $1: string 74 | log-debug = $(shell echo -e ${COLOR_CYAN} LOG: ${1}${COLOR_NORMAL} >&2 ) 75 | 76 | # $1: string 77 | log-echo = $(shell echo -e ${COLOR_MAGENTA}${1}${COLOR_NORMAL} >&2 ) 78 | ############################################################################# 79 | LINK_STATIC ?= -Wl,-Bstatic 80 | LINK_SHARED ?= -Wl,-Bdynamic 81 | LINK_FALGS ?= 82 | LINK_SLIBS ?= 83 | LINK_DLIBS ?= 84 | INSTALL_LIB ?= target/lib 85 | 86 | export LINK_STATIC LINK_SHARED LINK_FALGS LINK_SLIBS LINK_DLIBS INSTALL_LIB 87 | 88 | #**************************************************************************** 89 | # GCC 90 | #**************************************************************************** 91 | HOST_NAME ?= 92 | CROSS_COMPILE ?= 93 | CFLAGS ?= 94 | LDFLAGS ?= 95 | TEST_CFLAGS ?= 96 | MERGE_LDFLAGS ?= 97 | BUILD_FILE ?= build/ubuntu.make 98 | 99 | LINK_PATH := -L target/lib 100 | LD_LIBS := -lpthread -lm -lrt -ldl -lresolv 101 | PLATFORM_LIBS := 102 | 103 | ###################################### 104 | export BUILD_FILE 105 | -include $(BUILD_FILE) 106 | ###################################### 107 | AS = $(CROSS_COMPILE)as 108 | LD = $(CROSS_COMPILE)ld 109 | CC = $(CROSS_COMPILE)gcc 110 | CPP = $(CC) -E 111 | AR = $(CROSS_COMPILE)ar 112 | NM = $(CROSS_COMPILE)nm 113 | STRIP = $(CROSS_COMPILE)strip 114 | OBJCOPY = $(CROSS_COMPILE)objcopy 115 | OBJDUMP = $(CROSS_COMPILE)objdump 116 | RANLIB = $(CROSS_COMPILE)ranlib 117 | 118 | 119 | CFLAGS += -fPIC -rdynamic -pipe -O2 -Wall 120 | CFLAGS += -I ${CURDIR}/include 121 | CFLAGS += -I ${CURDIR}/target/include 122 | CFLAGS += -I ${CURDIR}/include/util 123 | CFLAGS += -I ${CURDIR}/platform/bsp/include 124 | CFLAGS += -I ${CURDIR}/platform/os/include 125 | CFLAGS += -I ${CURDIR}/util 126 | 127 | 128 | #LDFLAGS += -rdynamic -shared 129 | #LDFLAGS += -L ${INSTALL_LIB} 130 | 131 | # merge share lib flags 132 | MERGE_LDFLAGS += -z defs -z muldefs -undefined -Bsymbolic -shared 133 | #MERGE_LDFLAGS := -t -z defs -z muldefs -undefined -Bsymbolic -shared 134 | 135 | 136 | 137 | TEST_CFLAGS += ${CFLAGS} 138 | 139 | 140 | ifeq ($(strip $(CONFIG_SELINUX)),y) 141 | LD_LIBS += -lselinux 142 | endif 143 | 144 | export AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP RANLIB CFLAGS LDFLAGS MERGE_LDFLAGS HOST_NAME 145 | export TEST_CFLAGS LINK_PATH LD_LIBS 146 | 147 | MAKEFILE_BUILD := scripts/Makefile.build 148 | MAKEFILE_TEST_BUILD := scripts/Makefile.test.build 149 | export MAKEFILE_BUILD MAKEFILE_TEST_BUILD 150 | 151 | dirs := util/ platform/ component/ 152 | dirs := ${patsubst %/,%,$(filter %/, $(dirs))} 153 | PHONY += $(dirs) 154 | $(dirs): FORCE 155 | @make -f ${MAKEFILE_BUILD} obj=$@ 156 | 157 | build_comm_dym_lib: FORCE 158 | @$(call log-echo, "make build all common lib over !!! ") 159 | @$(call log-cmd, "Start building a shared library now...") 160 | @$(shell rm -f ${LIBCOMM_D_NAME}) 161 | $(CC) ${CFLAGS} ${MERGE_LDFLAGS} -o ${LIBCOMM_D_NAME} ${LINK_PATH} \ 162 | ${LINK_STATIC} -Wl,--whole-archive ${shell ls ${INSTALL_LIB}/*.a} ${PLATFORM_LIBS} -Wl,--no-whole-archive \ 163 | ${LINK_SHARED} ${LINK_COMMON_DEP_DLIBS} 164 | @$(call log-cmd, "make libcore.so SUCC...") 165 | 166 | build_comms_static_lib: FORCE 167 | @$(call log-echo, "make build all common library over !!! ") 168 | @$(call log-cmd, "Start building a static library now...") 169 | @$(shell rm -f ${LIBCOMM_S_NAME}) 170 | ${LD} -r -o ${LIBCOMM_S_NAME} ${LINK_PATH} --whole-archive ${shell ls ${INSTALL_LIB}/*.a} ${PLATFORM_LIBS} --no-whole-archive 171 | @$(call log-cmd, "make static library SUCC...") 172 | 173 | 174 | build_3th: FORCE 175 | @${shell for i in `find 3th/ -name *.tar.*`;do tar -xvf $$i -C `dirname $$i` ;done} 176 | 177 | objs := init/main.o 178 | 179 | all: ${dirs} ${objs} build_comms_static_lib FORCE 180 | mkdir -p target/bin target/lib target/include 181 | $(CC) ${CFLAGS} ${LINK_PATH} -o ${TARGET_NAME} ${objs} ${LD_COMM_NAME} ${LD_LIBS} 182 | 183 | test_dirs := sample/ 184 | test_dirs := ${patsubst %/,%,$(filter %/, $(test_dirs))} 185 | $(test_dirs): FORCE 186 | @make -f ${MAKEFILE_TEST_BUILD} obj=$@ 187 | 188 | test: $(test_dirs) FORCE 189 | 190 | opensouce_clean: FORCE 191 | @rm -fr platform/os/linux_glib/glib/zlib-1.2.11 192 | @rm -fr platform/os/linux_glib/glib/glib-2.40.2 193 | @rm -fr platform/os/linux_glib/glib/libffi-3.2.1 194 | @rm -fr platform/os/linux/ubus/json-c 195 | @rm -fr platform/os/linux/ubus/libubox 196 | @rm -fr platform/os/linux/ubus/ubus 197 | @exit 0 198 | 199 | clean: FORCE 200 | @echo ">>> clean target" 201 | @rm -f *.bak *.so *.a 202 | @rm -f ${TARGET_NAME} ${LIBCOMM_NAME} 203 | @${shell for dir in `find -maxdepth 4 -type d | grep -v git| grep -v include | grep -v target |grep -v \.si4project`;\ 204 | do rm -f $${dir}/*.o $${dir}/*.bak $${dir}/*.so $${dir}/*.a $${dir}/*.dep;done} 205 | @${shell cd sample && for i in `find *.c`;do rm -f `echo $$i|sed 's/\.c//g' `;done } 206 | @exit 0 207 | 208 | distclean: opensouce_clean clean 209 | @echo ">>> distclean target" 210 | @rm -fr target 211 | @exit 0 212 | 213 | help: 214 | @echo 'Cleaning targets:' 215 | @echo ' clean - Remove most generated files but keep the config and' 216 | @echo ' enough build support to build external modules' 217 | @echo ' mrproper - Remove all generated files + config + various backup files' 218 | @echo ' distclean - mrproper + remove editor backup and patch files' 219 | @echo '' 220 | @exit 0 221 | 222 | 223 | PHONY += FORCE 224 | FORCE: 225 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 多任务通讯系统. 2 | 3 | 架构设计核心思想: 统一、抽象、下沉、上升、量化、闭环。 4 | 统一:做到通用,标准化,如posix标准,高可移植性。 5 | 抽象:将具体功能业务抽象化,减少依赖 6 | 下沉:下沉组件,组件化。工具化、自动化。 7 | 上升:上升业务逻辑,打薄业务层,专注于业务。 8 | 量化:对系统性能、算法等能够量化分析。 9 | 闭环:软件闭环,逻辑闭环。 10 | 11 | 12 | 13 | > 嵌入式系统职能划分 14 | ![image](https://note.youdao.com/yws/public/resource/53020d3e2dd5db8dc377d086688552e5/4BBF58B17FF04ABFADD990E7F0B74BB2?ynotemdtimestamp=1637413162344) 15 | 16 | > 嵌入式系统架构 17 | ![image](https://note.youdao.com/yws/public/resource/53020d3e2dd5db8dc377d086688552e5/9390BA86588D4B6AAE386BBB79C1BDD3?ynotemdtimestamp=1636789562673) 18 | 19 | > 进程间通讯框架 20 | ![image](https://note.youdao.com/yws/public/resource/53020d3e2dd5db8dc377d086688552e5/020D72D6CAD443FA84B916764DC84018?ynotemdtimestamp=1636902903987) 21 | 22 | # 跨平台编译指令. 23 | make distclean 24 | ##################################################################### 25 | # auto make x86 26 | ##################################################################### 27 | make BUILD_FILE=build/ubuntu.make 28 | 29 | ##################################################################### 30 | # auto make hi3518ev200 (for glibc) 31 | ##################################################################### 32 | make BUILD_FILE=build/hi3518ev200.make 33 | 34 | ##################################################################### 35 | # auto make hi3518ev200 (for glibc) 36 | ##################################################################### 37 | make BUILD_FILE=build/jz2440.make 38 | 39 | 40 | -------------------------------------------------------------------------------- /build/hi3518ev200.make: -------------------------------------------------------------------------------- 1 | 2 | #arch 3 | HOST_NAME=arm-hisiv400-linux 4 | CROSS_COMPILE=arm-hisiv400-linux- 5 | 6 | #config 7 | CONFIG_OS=linux_glib 8 | CONFIG_UBUS=y 9 | 10 | -------------------------------------------------------------------------------- /build/jz2440.make: -------------------------------------------------------------------------------- 1 | 2 | #arch 3 | HOST_NAME=arm-linux 4 | CROSS_COMPILE=arm-linux- 5 | 6 | #config 7 | CONFIG_OS=linux 8 | CONFIG_SOC=jz2440 9 | 10 | CONFIG_UBUS=n 11 | CONFIG_EVDRIVER=n 12 | -------------------------------------------------------------------------------- /build/ubuntu.make: -------------------------------------------------------------------------------- 1 | 2 | #arch 3 | HOST_NAME= 4 | CROSS_COMPILE= 5 | 6 | #config 7 | CONFIG_OS=linux 8 | CONFIG_UBUS=n 9 | CONFIG_PARSON=y 10 | 11 | -------------------------------------------------------------------------------- /component/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | obj-y += module/ 4 | obj-y += protocol/ 5 | 6 | -------------------------------------------------------------------------------- /component/module/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | 4 | 5 | -------------------------------------------------------------------------------- /component/module/cbus.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/component/module/cbus.h -------------------------------------------------------------------------------- /component/protocol/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | obj-y += 4 | -------------------------------------------------------------------------------- /component/protocol/coap/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/component/protocol/coap/Makefile -------------------------------------------------------------------------------- /component/protocol/http/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/component/protocol/http/Makefile -------------------------------------------------------------------------------- /component/protocol/mqtt/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/component/protocol/mqtt/Makefile -------------------------------------------------------------------------------- /component/protocol/protocol.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/component/protocol/protocol.c -------------------------------------------------------------------------------- /component/protocol/protocol.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/component/protocol/protocol.h -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/README.md -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_031f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_031f -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_032d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_032d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_033d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_033d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_034d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_034d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_035d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_035d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_036d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_036d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_037d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_037d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_038d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_038d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_039d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_039d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_040d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_040d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_041d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_041d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_042d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_042d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_043d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_043d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_044d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_044d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_045d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_045d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_046d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_046d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_047d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_047d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_048d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_048d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_049d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_049d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_050d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_050d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_051d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_051d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_052d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_052d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_053d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_053d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_054d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_054d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_055d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_055d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_056d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_056d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_057d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_057d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_058d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_058d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_059d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_059d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_060d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_060d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_061d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_061d -------------------------------------------------------------------------------- /docs/UML/LinuxMultiTask.vpp.bak_062f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/docs/UML/LinuxMultiTask.vpp.bak_062f -------------------------------------------------------------------------------- /include/inits.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __INITS_H 3 | #define __INITS_H 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | 10 | typedef int (*initcall_t)(void); 11 | typedef void (*exitcall_t)(void); 12 | 13 | #define __define_pre_initcall(fn) \ 14 | static initcall_t __pre_initcall_##fn __attribute__((__used__)) \ 15 | __attribute__((__section__(".preinit_array"))) = fn 16 | 17 | #define __define_initcall(level,fn,id) \ 18 | static initcall_t __initcall_##fn##id __attribute__((__used__)) \ 19 | __attribute__((__section__(".init_array." level ".init"))) = fn 20 | 21 | #define __define_fini_initcall(fn) \ 22 | static exitcall_t __fini_initcall_##fn __attribute__((__used__)) \ 23 | __attribute__((__section__(".fini_array"))) = fn 24 | 25 | 26 | #define earliest_init(x) __define_pre_initcall(x) 27 | #define latest_init(x) __define_fini_initcall(x) 28 | 29 | /************************************************************ 30 | * pure->os->osa->core->hal->hala->module->service->app 31 | ************************************************************/ 32 | #define pure_initcall(fn) __define_initcall("0",fn,0) 33 | #define osa_initcall(fn) __define_initcall("1",fn,1) 34 | #define os_initcall(fn) __define_initcall("2",fn,2) 35 | #define core_initcall(fn) __define_initcall("3",fn,3) 36 | #define hala_initcall(fn) __define_initcall("4",fn,4) 37 | #define hal_initcall(fn) __define_initcall("5",fn,5) 38 | #define modulea_initcall(fn) __define_initcall("6",fn,6) 39 | #define module_initcall(fn) __define_initcall("7",fn,7) 40 | #define service_initcall(fn) __define_initcall("8",fn,8) 41 | #define app_initcall(fn) __define_initcall("9",fn,9) 42 | 43 | /***********************************************************/ 44 | #define pure_init(x) pure_initcall(x) 45 | #define core_init(x) core_initcall(x) 46 | #define osla_init(x) osa_initcall(x) 47 | #define core_init(x) core_initcall(x) 48 | #define hal_init(x) hal_initcall(x) 49 | #define module_init(x) module_initcall(x) 50 | #define modulea_init(x) modulea_initcall(x) 51 | #define service_init(x) service_initcall(x) 52 | #define app_init(x) app_initcall(x) 53 | 54 | #define module_exit(fn) latest_init(fn) 55 | 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* __INITS_H */ 62 | 63 | 64 | -------------------------------------------------------------------------------- /include/multitask.h: -------------------------------------------------------------------------------- 1 | #ifndef __MULTITASK__H 2 | #define __MULTITASK__H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "inits.h" 9 | #include "klist.h" 10 | #include "version.h" 11 | 12 | #include "os_msg.h" 13 | #include "os_async_queue.h" 14 | #include "os_log.h" 15 | #include "os_misc.h" 16 | #include "os_queue.h" 17 | #include "os_mm.h" 18 | #include "os_task.h" 19 | #include "os_broadcast.h" 20 | #include "os_multicast.h" 21 | #include "os_stdlibc.h" 22 | #include "os_evdriver.h" 23 | #include "os_io.h" 24 | #include "os_tcp_client.h" 25 | #include "os_tcp_server.h" 26 | #include "os_threadpool.h" 27 | #include "os_sys.h" 28 | #include "os_file.h" 29 | 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | 36 | 37 | #endif 38 | 39 | 40 | -------------------------------------------------------------------------------- /include/version.h: -------------------------------------------------------------------------------- 1 | #ifndef __VERSION__H 2 | #define __VERSION__H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #define MULTI_TASK_VERSION "1.4.0" 9 | 10 | 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | 17 | #endif 18 | 19 | -------------------------------------------------------------------------------- /init/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := main.o 3 | 4 | -------------------------------------------------------------------------------- /init/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "multitask.h" 4 | 5 | #define TASK_PRODUCER "producer" 6 | #define TASK_CONSUMER1 "consumer1" 7 | #define TASK_CONSUMER2 "consumer2" 8 | 9 | static void *task_routine_no1(void *arg) 10 | { 11 | int cnt = 0; 12 | void *p = NULL; 13 | while (1) 14 | { 15 | p = MALLOC((cnt+1)*16); 16 | ASSERT(p); 17 | os_sleep(1); 18 | } 19 | return NULL; 20 | } 21 | 22 | static void *task_routine_no2(void *arg) 23 | { 24 | int cnt = 0; 25 | void *p = NULL; 26 | while (1) 27 | { 28 | p = MALLOC((cnt+1)*512); 29 | ASSERT(p); 30 | os_sleep(1); 31 | } 32 | return NULL; 33 | } 34 | 35 | 36 | static void *task_routine_normal(void *arg) 37 | { 38 | int cnt = 0; 39 | void *p = NULL; 40 | while (1) 41 | { 42 | p = MALLOC((cnt+1)*2048); 43 | cnt++; 44 | FREE(p); 45 | ASSERT(p); 46 | os_sleep(1); 47 | 48 | } 49 | return NULL; 50 | } 51 | 52 | 53 | static void *task_routine_producer(void *arg) 54 | { 55 | os_msg_t *msg; 56 | while (1) 57 | { 58 | msg = MALLOC(sizeof(*msg)+32); 59 | ASSERT(msg); 60 | MLOGM("msg1=%p\n",msg); 61 | os_memset(msg,0,(sizeof(*msg)+32)); 62 | os_strcpy((char *)msg->data,"hello"); 63 | msg->src = TASK_PRODUCER; 64 | msg->dst = TASK_CONSUMER1; 65 | msg->priority = 0; 66 | msg->size = 32; 67 | if(os_msg_send(msg)) 68 | { 69 | FREE(msg); 70 | msg = NULL; 71 | } 72 | os_sleep(1); 73 | 74 | msg = MALLOC(sizeof(*msg)+32); 75 | ASSERT(msg); 76 | MLOGM("msg2=%p\n",msg); 77 | os_memset(msg,0,(sizeof(*msg)+32)); 78 | os_strcpy((char *)msg->data,"world"); 79 | msg->src = TASK_PRODUCER; 80 | msg->dst = TASK_CONSUMER2; 81 | msg->priority = 0; 82 | msg->size = 32; 83 | if(os_msg_send(msg)) 84 | { 85 | FREE(msg); 86 | msg = NULL; 87 | } 88 | os_sleep(1); 89 | } 90 | 91 | return NULL; 92 | } 93 | 94 | static void *task_routine_consumer1(void *arg) 95 | { 96 | const char *str = NULL; 97 | os_msg_t *msg; 98 | while (1) 99 | { 100 | msg = os_msg_recv(); 101 | str = (const char *)msg->data; 102 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 103 | FREE(msg); 104 | } 105 | 106 | return NULL; 107 | } 108 | 109 | static void *task_routine_consumer2(void *arg) 110 | { 111 | const char *str = NULL; 112 | os_msg_t *msg; 113 | while (1) 114 | { 115 | msg = os_msg_recv(); 116 | str = (const char *)msg->data; 117 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 118 | FREE(msg); 119 | } 120 | 121 | return NULL; 122 | } 123 | 124 | 125 | static void *task_routine_loop(void *arg) 126 | { 127 | while (1) 128 | { 129 | } 130 | return NULL; 131 | } 132 | 133 | 134 | pthread_mutex_t dieMutex = PTHREAD_MUTEX_INITIALIZER; 135 | //死锁 136 | static void *task_routine_die_lock(void *arg) 137 | { 138 | os_sleep(3); 139 | pthread_mutex_lock(&dieMutex); 140 | os_sleep(10); 141 | return NULL; 142 | } 143 | 144 | static void *task_routine_lock(void *arg) 145 | { 146 | pthread_mutex_lock(&dieMutex); 147 | while (1) 148 | { 149 | os_sleep(10); 150 | } 151 | return NULL; 152 | } 153 | 154 | int main(void) 155 | { 156 | MLOGD("Task start ...."); 157 | 158 | os_task_create("no1",0,0, task_routine_no1, (void *)NULL); 159 | os_task_create("no2",0,0, task_routine_no2, (void *)NULL); 160 | os_task_create("normal",0,0, task_routine_normal, (void *)NULL); 161 | os_task_create(TASK_PRODUCER,0,0, task_routine_producer, (void *)NULL); 162 | os_task_create(TASK_CONSUMER1,0,0, task_routine_consumer1, (void *)NULL); 163 | os_task_create(TASK_CONSUMER2,0,0, task_routine_consumer2, (void *)NULL); 164 | os_task_create("loop",0,0, task_routine_loop, (void *)NULL); 165 | os_task_create("lock",0,0, task_routine_lock, (void *)NULL); 166 | os_task_create("die_lock",0,0, task_routine_die_lock, (void *)NULL); 167 | 168 | while(1) 169 | { 170 | //system("clear"); 171 | os_task_mm_show(); 172 | os_sleep(1); 173 | } 174 | 175 | os_mm_show(); 176 | return 0; 177 | } 178 | 179 | -------------------------------------------------------------------------------- /platform/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | obj-y += os/ 4 | 5 | -------------------------------------------------------------------------------- /platform/bsp/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/bsp/Makefile -------------------------------------------------------------------------------- /platform/os/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | 4 | ifeq ($(strip $(CONFIG_OS)),linux) 5 | obj-y += linux/ 6 | else ifeq ($(strip $(CONFIG_OS)),freertos) 7 | obj-y += freertos/ 8 | else ifeq ($(strip $(CONFIG_OS)),linux_glib) 9 | obj-y += linux_glib/ 10 | else ifeq ($(strip $(CONFIG_OS)),ros) 11 | obj-y += ros/ 12 | else 13 | obj-y += linux/ 14 | endif 15 | 16 | -------------------------------------------------------------------------------- /platform/os/freertos/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/freertos/Makefile -------------------------------------------------------------------------------- /platform/os/include/os_async_queue.h: -------------------------------------------------------------------------------- 1 | #ifndef __MT_ASYNCQUEUE_H 2 | #define __MT_ASYNCQUEUE_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | 9 | typedef struct os_async_queue_s os_async_queue_t; 10 | 11 | os_async_queue_t *os_async_queue_new(void); 12 | void os_async_queue_free(os_async_queue_t *q); 13 | unsigned int os_async_queue_length(os_async_queue_t *q); 14 | void os_async_queue_push(os_async_queue_t *q,void *data); 15 | void *os_async_queue_pop(os_async_queue_t *q); 16 | 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /platform/os/include/os_broadcast.h: -------------------------------------------------------------------------------- 1 | #ifndef __BROADCAST_H 2 | #define __BROADCAST_H 3 | 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | typedef struct os_broadcast_s os_broadcast_t; 10 | 11 | typedef enum { 12 | BROADCAST_TYPE_SERVER = 0, 13 | BROADCAST_TYPE_CLIENT, 14 | BROADCAST_TYPE_BUTT 15 | }os_broadcast_type_e; 16 | 17 | // 18 | os_broadcast_t *os_broadcast_create(os_broadcast_type_e type,unsigned short port); 19 | void os_broadcast_destroy(os_broadcast_t *b); 20 | int os_broadcast_send(os_broadcast_t *b,unsigned char *data,unsigned int size); 21 | int os_broadcast_recv(os_broadcast_t *b,unsigned char *data,unsigned int size); 22 | 23 | 24 | #ifdef __cplusplus 25 | } 26 | #endif 27 | 28 | 29 | 30 | #endif //__BROADCAST_H 31 | -------------------------------------------------------------------------------- /platform/os/include/os_bus.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_BUS__H 2 | #define __OS_BUS__H 3 | 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | 10 | #ifdef __cplusplus 11 | } 12 | #endif 13 | 14 | 15 | #endif //__OS_BUS__H 16 | 17 | -------------------------------------------------------------------------------- /platform/os/include/os_evdriver.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_EVDRIVER__H 2 | #define __OS_EVDRIVER__H 3 | 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | typedef enum { 10 | OS_EVDRIVER_IO, 11 | OS_EVDRIVER_TIMER, 12 | OS_EVDRIVER_SIGNAL, 13 | OS_EVDRIVER_BUTT 14 | }os_evdriver_type_e; 15 | 16 | /* Run flags */ 17 | #define EVDIRVER_ONCE 1 /**< run loop once */ 18 | #define EVDIRVER_NONBLOCK 2 /**< exit if no event */ 19 | 20 | 21 | typedef struct os_event_s { 22 | os_evdriver_type_e event; 23 | union { 24 | struct __evdriver_io_s { 25 | int fd; 26 | }io; 27 | 28 | struct __evdriver_signal_s { 29 | int signo; 30 | }signal; 31 | 32 | struct __evdriver_timer_s { 33 | unsigned long when; 34 | unsigned long interval; 35 | }timer; 36 | }; 37 | }os_event_t; 38 | 39 | 40 | typedef struct os_evdriver_s os_evdriver_t; 41 | typedef struct os_evdriver_node_s os_evdriver_node_t; 42 | 43 | typedef void (*os_evdriver_callback_t)(os_evdriver_node_t *evdriver,void *user_data); 44 | 45 | ////////////////////////////////////////////////////////////////////////////// 46 | os_evdriver_t *os_evdriver_create(void); 47 | os_evdriver_t *os_evdriver_create2(const char *name); 48 | void os_evdriver_destroy(os_evdriver_t *evdriver); 49 | int os_evdriver_run(os_evdriver_t *evdriver); 50 | int os_evdriver_run2(os_evdriver_t *evdriver,int flag); 51 | 52 | os_evdriver_node_t *os_evdriver_add(os_evdriver_t *evdriver,os_event_t *ev,os_evdriver_callback_t cb,void *user_data); 53 | void os_evdriver_del(os_evdriver_node_t *evnode); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | 59 | 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /platform/os/include/os_file.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_FILE_H 2 | #define __OS_FILE_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | unsigned int os_file_write(int fd, const void* buf, unsigned int count); 9 | int os_file_append(const char* path, unsigned char* data,unsigned int size); 10 | int os_file_write_str(const char* path, const char* str, int mode); 11 | int os_file_rewrite(const char *path, const void *data, int len); 12 | unsigned int os_file_read(int fd, void *buf, unsigned int count); 13 | int os_file_read2(const char *file,void *data, int len); 14 | void os_trace2file(const char* filename, const char* fmt, ...); 15 | int os_file_exist(const char *filename); 16 | int os_file_size(const char* filename); 17 | int os_file_key_write(const char *filename,const char *key,const char *val); 18 | 19 | 20 | 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | #endif /* __OS_FILE_H */ 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /platform/os/include/os_io.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_IO_H 2 | #define __OS_IO_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef void (*os_io_callback_t)(const char *name,void *data,unsigned int size,void *user_data); 9 | 10 | typedef struct { 11 | const char *name; 12 | int fd; 13 | int (*send)(const char *name,void *data,unsigned int size,long long timeout); 14 | }os_io_node; 15 | 16 | 17 | int os_io_register(const char *name,os_io_callback_t cb,void *user_data); 18 | void os_io_unregister(const char *name); 19 | 20 | int os_io_node_add(os_io_node *node); 21 | void os_io_node_del(const char *name); 22 | void os_io_node_del2(int fd); 23 | 24 | 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | #endif /* __OS_IO_H */ 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /platform/os/include/os_log.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MT_LOG_H 3 | #define __MT_LOG_H 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | //输出颜色 10 | #define COLOUR_NONE "\e[0m" 11 | #define COLOUR_BLACK "\e[0;30m" 12 | #define COLOUR_L_BLACK "\e[1;30m" 13 | #define COLOUR_RED "\e[0;31m" 14 | #define COLOUR_L_RED "\e[1;31m" 15 | #define COLOUR_GREEN "\e[0;32m" 16 | #define COLOUR_L_GREEN "\e[1;32m" 17 | #define COLOUR_BROWN "\e[0;33m" 18 | #define COLOUR_YELLOW "\e[1;33m" 19 | #define COLOUR_BLUE "\e[0;34m" 20 | #define COLOUR_L_BLUE "\e[1;34m" 21 | #define COLOUR_PURPLE "\e[0;35m" 22 | #define COLOUR_L_PURPLE "\e[1;35m" 23 | #define COLOUR_CYAN "\e[0;36m" 24 | #define COLOUR_L_CYAN "\e[1;36m" 25 | #define COLOUR_GRAY "\e[0;37m" 26 | #define COLOUR_WHITE "\e[1;37m" 27 | 28 | 29 | typedef enum { 30 | MLOG_VERBOSE = 0, 31 | MLOG_DEBUG, 32 | MLOG_INFO, 33 | MLOG_MESSAGE, 34 | MLOG_WARN, 35 | MLOG_ERROR, 36 | MLOG_FATAL, 37 | MLOG_BUTT 38 | }MMP_LOG_ENUM; 39 | 40 | #define VERBOSE_INT(filename, linenr, funcname, args...) \ 41 | os_log_message(MLOG_VERBOSE, filename, linenr, funcname,args) 42 | #define DEBUG_INT(filename, linenr, funcname, args...) \ 43 | os_log_message(MLOG_DEBUG, filename, linenr, funcname,args) 44 | #define INFO_INT(filename, linenr, funcname, args...) \ 45 | os_log_message(MLOG_INFO, filename, linenr, funcname,args) 46 | #define MESSAGE_INT(filename, linenr, funcname, args...) \ 47 | os_log_message(MLOG_MESSAGE, filename, linenr, funcname,args) 48 | #define WARN_INT(filename, linenr, funcname, args...) \ 49 | os_log_message(MLOG_WARN, filename, linenr, funcname,args) 50 | #define ERROR_INT(filename, linenr, funcname, args...) \ 51 | os_log_message(MLOG_ERROR, filename, linenr, funcname,args) 52 | #define FATAL_INT(filename, linenr, funcname, args...) \ 53 | os_log_message(MLOG_FATAL, filename, linenr, funcname,args) 54 | 55 | #define MLOGV(args...) VERBOSE_INT(__FILE__, __LINE__, __func__,args) 56 | #define MLOGD(args...) DEBUG_INT(__FILE__, __LINE__, __func__,args) 57 | #define MLOGI(args...) INFO_INT(__FILE__, __LINE__, __func__,args) 58 | #define MLOGM(args...) MESSAGE_INT(__FILE__, __LINE__, __func__,args) 59 | #define MLOGW(args...) WARN_INT(__FILE__, __LINE__, __func__,args) 60 | #define MLOGE(args...) ERROR_INT(__FILE__, __LINE__, __func__,args) 61 | #define MLOGF(args...) FATAL_INT(__FILE__, __LINE__, __func__,args) 62 | 63 | #define MLOG_BUFF_SIZE 1024*4 64 | 65 | 66 | typedef void (*os_log_callback_t)(int level,const char *message); 67 | 68 | void os_log_message(int level 69 | ,const char *file_name 70 | ,int line 71 | ,const char *func_name 72 | ,const char *fmt, ...); 73 | void os_log_assert(const char *source 74 | ,const char *expr 75 | ,const char *filename 76 | ,int linenr 77 | ,const char *fmt, ...); 78 | 79 | 80 | #undef ASSERT 81 | #define ASSERT(expr) if(!(expr)) os_log_assert(NULL,#expr,__FILE__, __LINE__,NULL) 82 | 83 | #undef ASSERT_ARGS 84 | #define ASSERT_ARGS(expr,args...) if(!(expr)) os_log_assert(NULL,#expr,__FILE__, __LINE__,args) 85 | 86 | 87 | 88 | #undef RETURN_IF_FAIL 89 | #define RETURN_IF_FAIL(expr) ({\ 90 | if (!(expr))\ 91 | {\ 92 | os_log_assert(NULL,#expr,__FILE__, __LINE__,NULL);\ 93 | return;\ 94 | }\ 95 | }) 96 | 97 | #undef RETURN_IF_FAIL2 98 | #define RETURN_IF_FAIL2(expr,_actions) ({\ 99 | if (!(expr))\ 100 | {\ 101 | {_actions};\ 102 | os_log_assert(NULL,#expr,__FILE__, __LINE__,NULL);\ 103 | return;\ 104 | }\ 105 | }) 106 | 107 | #undef RETURN_IF_FAIL_ARGS 108 | #define RETURN_IF_FAIL_ARGS(expr,args...) ({\ 109 | if (!(expr))\ 110 | {\ 111 | os_log_assert(NULL,#expr,__FILE__, __LINE__,args);\ 112 | return;\ 113 | }\ 114 | }) 115 | 116 | 117 | #undef RETURN_VAL_IF_FAIL 118 | #define RETURN_VAL_IF_FAIL(expr,return_val) ({\ 119 | if (!(expr))\ 120 | {\ 121 | os_log_assert(NULL,#expr,__FILE__, __LINE__,NULL);\ 122 | return return_val;\ 123 | }\ 124 | }) 125 | 126 | #undef RETURN_VAL_IF_FAIL2 127 | #define RETURN_VAL_IF_FAIL2(expr,_actions,return_val) ({\ 128 | if (!(expr))\ 129 | {\ 130 | {_actions};\ 131 | os_log_assert(NULL,#expr,__FILE__, __LINE__,NULL);\ 132 | return return_val;\ 133 | }\ 134 | }) 135 | 136 | 137 | #undef RETURN_VAL_IF_FAIL_ARGS 138 | #define RETURN_VAL_IF_FAIL_ARGS(expr,return_val,args...) ({\ 139 | if (!(expr))\ 140 | {\ 141 | os_log_assert(NULL,#expr,__FILE__, __LINE__,args);\ 142 | return return_val;\ 143 | }\ 144 | }) 145 | 146 | 147 | #undef GOTO_LABEL_IF_FAIL 148 | #define GOTO_LABEL_IF_FAIL(expr,label) ({\ 149 | if (!(expr))\ 150 | {\ 151 | os_log_assert(NULL,#expr,__FILE__, __LINE__,NULL);\ 152 | goto label;\ 153 | }\ 154 | }) 155 | 156 | 157 | #undef GOTO_LABEL_IF_FAIL_ARGS 158 | #define GOTO_LABEL_IF_FAIL_ARGS(expr,label,args...) ({\ 159 | if (!(expr))\ 160 | {\ 161 | os_log_assert(NULL,#expr,__FILE__, __LINE__,args);\ 162 | goto label;\ 163 | }\ 164 | }) 165 | 166 | #undef CONTINUE_IF_FAIL 167 | #define CONTINUE_IF_FAIL(expr) ({\ 168 | if (!(expr))\ 169 | {\ 170 | os_log_assert(NULL,#expr,__FILE__, __LINE__,NULL);\ 171 | continue;\ 172 | }\ 173 | }) 174 | 175 | #undef CONTINUE_IF_FAIL_ARGS 176 | #define CONTINUE_IF_FAIL_ARGS(expr,args...) ({\ 177 | if (!(expr))\ 178 | {\ 179 | os_log_assert(NULL,#expr,__FILE__, __LINE__,args);\ 180 | continue;\ 181 | }\ 182 | }) 183 | 184 | 185 | #ifdef __cplusplus 186 | } 187 | #endif 188 | 189 | #endif /* __MT_LOG_H */ 190 | 191 | 192 | -------------------------------------------------------------------------------- /platform/os/include/os_misc.h: -------------------------------------------------------------------------------- 1 | #ifndef __MT_MISC_H 2 | #define __MT_MISC_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | void time2str(char *buf,int size); 9 | void thread_name_get(char *task_name); 10 | void thread_name_set(const char *task_name); 11 | int child_pid_serialize2string(char *strs,unsigned int size); 12 | int child_pid_get(unsigned long **pp_arr,unsigned int *p_size); 13 | void child_pid_free(unsigned long *p_arr); 14 | 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | 20 | #endif /* __MT_MISC_H */ 21 | 22 | -------------------------------------------------------------------------------- /platform/os/include/os_mm.h: -------------------------------------------------------------------------------- 1 | #ifndef __MM__H 2 | #define __MM__H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #define CONFIG_OS_MM_TRACE 1 9 | 10 | void *os_mm_malloc(const char *func,unsigned long line,unsigned long size); 11 | void os_mm_free(void *addr); 12 | void os_mm_show(void); 13 | void os_mm_show2(void (*show)(const char *)); 14 | 15 | #define MALLOC(size) os_mm_malloc(__func__,__LINE__,size) 16 | #define FREE(addr) os_mm_free(addr) 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /platform/os/include/os_msg.h: -------------------------------------------------------------------------------- 1 | #ifndef __MT_MSG_H 2 | #define __MT_MSG_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef struct os_msg_s { 9 | const char *src; 10 | const char *dst; 11 | int priority; 12 | unsigned int size; 13 | unsigned char data[0]; 14 | }os_msg_t; 15 | 16 | 17 | int os_msg_send(os_msg_t *msg); 18 | os_msg_t *os_msg_recv(void); 19 | int os_msg_send2(const char *dst,int priority,void *data,unsigned int size); 20 | void *os_msg_recv2(unsigned int *p_size); 21 | 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /platform/os/include/os_multicast.h: -------------------------------------------------------------------------------- 1 | #ifndef __MULTICASR_H 2 | #define __MULTICASR_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef struct os_multicast_s os_multicast_t; 9 | 10 | 11 | typedef enum { 12 | MULTICAST_TYPE_SERVER = 0, 13 | MULTICAST_TYPE_CLIENT, 14 | MULTICAST_TYPE_BUTT 15 | }os_multicast_type_e; 16 | 17 | // 18 | os_multicast_t *os_multicast_create(os_multicast_type_e type,const char *addr,unsigned short port); 19 | void os_multicast_destroy(os_multicast_t *m); 20 | int os_multicast_send(os_multicast_t *m,unsigned char *data,unsigned int size); 21 | int os_multicast_recv(os_multicast_t *m,unsigned char *data,unsigned int size); 22 | 23 | 24 | #ifdef __cplusplus 25 | } 26 | #endif 27 | 28 | 29 | #endif //__MULTICASR_H 30 | 31 | -------------------------------------------------------------------------------- /platform/os/include/os_queue.h: -------------------------------------------------------------------------------- 1 | #ifndef __MT_QUEUE_H 2 | #define __MT_QUEUE_H 3 | #include "klist.h" 4 | 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | typedef struct os_queue_s os_queue_t; 11 | 12 | os_queue_t *os_queue_new(void); 13 | void os_queue_free(os_queue_t *q); 14 | unsigned int os_queue_length(os_queue_t *q); 15 | unsigned int os_queue_empty(os_queue_t *q); 16 | void os_queue_push_tail(os_queue_t *q,void *data); 17 | void os_queue_push_head(os_queue_t *q,void *data); 18 | void *os_queue_pop_tail(os_queue_t *q); 19 | void *os_queue_pop_head(os_queue_t *q); 20 | 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /platform/os/include/os_stdlibc.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __OS_STDLIBC__H 3 | #define __OS_STDLIBC__H 4 | #include "os_type.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | 11 | void *os_malloc(os_size_t size); 12 | void os_free(void *ptr); 13 | void *os_calloc(os_size_t nmemb, os_size_t size); 14 | void *os_realloc(void *ptr, os_size_t size); 15 | 16 | int os_system(const char *command); 17 | unsigned int os_sleep(unsigned int seconds); 18 | void *os_memset(void *s, int c, os_size_t n); 19 | char *os_strcpy(char *dest, const char *src); 20 | char *os_strncpy(char *dest, const char *src, os_size_t n); 21 | 22 | int os_scanf(const char *format, ...); 23 | int os_sscanf(const char *str, const char *format, ...); 24 | os_size_t os_strlen(const char *s); 25 | int os_strcmp(const char *s1, const char *s2); 26 | int os_strncmp(const char *s1, const char *s2, unsigned int n); 27 | 28 | 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /platform/os/include/os_sys.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __OS_SYS__H 3 | #define __OS_SYS__H 4 | #include "os_type.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | int os_open(const char *pathname, int flags); 11 | int os_open2(const char *pathname, int flags, int mode); 12 | int os_close(int fd); 13 | int os_read(int fd, void *buf, unsigned int count); 14 | int os_write(int fd, const void *buf, unsigned int count); 15 | 16 | 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /platform/os/include/os_task.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_TASK__H 2 | #define __OS_TASK__H 3 | #include "klist.h" 4 | #include "os_async_queue.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | #define CONFIG_OS_TASK_MM_TRACE 1 11 | 12 | typedef struct os_task_mm_node_s{ 13 | struct list_head list; 14 | ///////////////////// 15 | char os_task_name[32]; 16 | unsigned long pid; //process ID 17 | unsigned long tid; //thread ID 18 | const char *func; 19 | unsigned long line; 20 | unsigned long size; 21 | void *addr; 22 | }os_task_mm_node_t; 23 | 24 | typedef void *(*os_task_func_t)(void *user_data); 25 | typedef struct os_task_s os_task_t; 26 | 27 | 28 | os_task_t *os_task_create(const char *name,unsigned long stack_size,int priority,os_task_func_t func,void *arg); 29 | os_task_t *os_task_create2(const char *name,unsigned long stack_size,int priority,int async,os_task_func_t func,void *arg); 30 | 31 | void os_task_mm_show(void); 32 | void os_task_mm_add(unsigned long tid,os_task_mm_node_t *mnode); 33 | void os_task_mm_del(unsigned long tid,void *addr); 34 | os_async_queue_t *os_task_aq_get(const char *name); 35 | os_async_queue_t *os_task_aq_self(void); 36 | const char *os_task_name_get_from_pid(unsigned long pid); 37 | const char *os_task_name_get_from_tid(unsigned long tid); 38 | int os_task_mm_json_get(char **ppjson); 39 | 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /platform/os/include/os_tcp_client.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_TCP_CLIENT_H 2 | #define __OS_TCP_CLIENT_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef void (*os_tcp_client_callback_t)(int fd,void *user_data); 9 | 10 | typedef struct os_tcp_client_s os_tcp_client_t; 11 | 12 | os_tcp_client_t *os_tcp_client_create(const char *name,const char *ip,unsigned short port); 13 | void os_tcp_client_destroy(os_tcp_client_t *c); 14 | int os_tcp_client_write(os_tcp_client_t *c,void *data,unsigned int size); 15 | int os_tcp_client_read(os_tcp_client_t *c,void *data,unsigned int size); 16 | 17 | 18 | 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | #endif /* __OS_TCP_CLIENT_H */ 25 | 26 | 27 | -------------------------------------------------------------------------------- /platform/os/include/os_tcp_server.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_TCP_SERVER_H 2 | #define __OS_TCP_SERVER_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #define TASK_TCP_SERVER "tcp_server_node" 9 | 10 | #define TCP_SERVER_QUQUE_MAX 10 11 | 12 | typedef void (*os_tcp_server_callback_t)(int fd,void *user_data); 13 | 14 | typedef struct os_tcp_server_s os_tcp_server_t; 15 | 16 | os_tcp_server_t *os_tcp_server_create(int threads,unsigned int listen_fds,int port,os_tcp_server_callback_t cb,void *user_data); 17 | void os_tcp_server_destroy(os_tcp_server_t *s); 18 | 19 | 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | #endif /* __OS_TCP_SERVER_H */ 26 | 27 | 28 | -------------------------------------------------------------------------------- /platform/os/include/os_thread.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __OS_THREAD__H 3 | #define __OS_THREAD__H 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | /////////////////////////////////////////////////////////////////// 10 | typedef void* os_pthread_t; 11 | typedef struct os_pthread_attr_s { 12 | int res; 13 | }os_pthread_attr_t; 14 | /////////// 15 | int os_pthread_create(os_pthread_t *tid, const os_pthread_attr_t *attr, void *(*func) (void *), void *arg); 16 | int os_pthread_join (os_pthread_t tid, void ** status); 17 | os_pthread_t os_pthread_self (void); 18 | int os_pthread_detach (os_pthread_t tid); 19 | void os_pthread_exit (void *status); 20 | 21 | /////////////////////////////////////////////////////////////////// 22 | typedef void* os_pthread_mutex_t; 23 | typedef struct os_pthread_mutexattr_s { 24 | int res; 25 | }os_pthread_mutexattr_t; 26 | /////////// 27 | int os_pthread_mutex_init(os_pthread_mutex_t * mutex,const os_pthread_mutexattr_t *attr); 28 | int os_pthread_mutex_destroy(os_pthread_mutex_t *mutex); 29 | int os_pthread_mutex_lock(os_pthread_mutex_t *mutex); 30 | int os_pthread_mutex_trylock(os_pthread_mutex_t *mutex); 31 | int os_pthread_mutex_unlock(os_pthread_mutex_t *mutex); 32 | 33 | /////////////////////////////////////////////////////////////////// 34 | typedef void* os_pthread_cond_t; 35 | typedef struct os_pthread_condattr_s { 36 | int res; 37 | }os_pthread_condattr_t; 38 | 39 | typedef struct os_timespec_s { 40 | int res; 41 | }os_timespec_t; 42 | 43 | /////////// 44 | int os_pthread_cond_destroy(os_pthread_cond_t *cond); 45 | int os_pthread_cond_init(os_pthread_cond_t * cond, 46 | const os_pthread_condattr_t * attr); 47 | int os_pthread_cond_timedwait(os_pthread_cond_t * cond, 48 | os_pthread_mutex_t * mutex, 49 | const os_timespec_t * abstime); 50 | int os_pthread_cond_wait(os_pthread_cond_t * cond, 51 | os_pthread_mutex_t * mutex); 52 | int os_pthread_cond_broadcast(os_pthread_cond_t *cond); 53 | int os_pthread_cond_signal(os_pthread_cond_t *cond); 54 | 55 | 56 | /////////////////////////////////////////////////////////////////// 57 | typedef void* os_sem_t; 58 | /////////// 59 | int os_sem_init(os_sem_t *sem, int pshared, unsigned int value); 60 | int os_sem_wait(os_sem_t *sem); 61 | int os_sem_trywait(os_sem_t *sem); 62 | int os_sem_post(os_sem_t * sem); 63 | int os_sem_destroy(os_sem_t * sem); 64 | 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /platform/os/include/os_threadpool.h: -------------------------------------------------------------------------------- 1 | #ifndef __MT_THREADPOOL_H 2 | #define __MT_THREADPOOL_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #define MAX_THREADS 64 9 | #define MAX_QUEUE 65536 10 | 11 | 12 | typedef struct os_threadpool_s os_threadpool_t; 13 | 14 | os_threadpool_t *os_threadpool_create(int thread_count, int queue_size, int flags); 15 | void os_threadpool_destroy(os_threadpool_t *pool, int flags); 16 | int os_threadpool_add(os_threadpool_t *pool, void (*routine)(void *),void *arg, int flags); 17 | 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | 24 | #endif 25 | 26 | 27 | -------------------------------------------------------------------------------- /platform/os/include/os_timer.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef __OS_TIMER__H 5 | #define __OS_TIMER__H 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | typedef struct os_timer_s 12 | { 13 | unsigned long long start; 14 | unsigned long long end; 15 | }os_timer_t; 16 | 17 | 18 | void os_timer_start(os_timer_t *timer); 19 | unsigned int os_timer_stop(os_timer_t *timer); 20 | 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | #endif 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /platform/os/include/os_type.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __OS_TYPE__H 3 | #define __OS_TYPE__H 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | typedef unsigned long os_size_t; 10 | 11 | 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | 17 | #endif 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /platform/os/linux/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | obj-y += base/ 4 | obj-$(CONFIG_UBUS) += ubus/ 5 | 6 | -------------------------------------------------------------------------------- /platform/os/linux/base/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | obj-y += os_async_queue.o 4 | obj-y += os_broadcast.o 5 | obj-$(CONFIG_EVDRIVER) += os_evdriver.o 6 | obj-y += os_log.o 7 | obj-y += os_misc.o 8 | obj-y += os_mm.o 9 | obj-y += os_msg.o 10 | obj-y += os_multicast.o 11 | obj-y += os_queue.o 12 | obj-y += os_stdlibc.o 13 | obj-y += os_task.o 14 | obj-y += os_thread.o 15 | obj-y += os_threadpool.o 16 | obj-y += os_tcp_server.o 17 | obj-y += os_tcp_client.o 18 | obj-y += os_sys.o 19 | obj-y += os_timer.o 20 | obj-y += os_file.o 21 | 22 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_async_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "os_queue.h" 9 | #include "os_async_queue.h" 10 | #include "os_log.h" 11 | #include "multitask.h" 12 | 13 | 14 | struct os_async_queue_s { 15 | pthread_cond_t cond; 16 | pthread_mutex_t mutex; 17 | os_queue_t *queue; 18 | void (*destroy)(os_async_queue_t *q,void *data); 19 | }; 20 | 21 | 22 | os_async_queue_t *os_async_queue_new(void) 23 | { 24 | os_async_queue_t *q = NULL; 25 | q = MALLOC(sizeof(*q)); 26 | RETURN_VAL_IF_FAIL(q, NULL); 27 | memset(q,0,sizeof(*q)); 28 | q->queue = os_queue_new(); 29 | GOTO_LABEL_IF_FAIL(q->queue, fail); 30 | pthread_mutex_init(&q->mutex, NULL); 31 | pthread_cond_init(&q->cond, NULL); 32 | 33 | return q; 34 | fail: 35 | FREE(q); 36 | q = NULL; 37 | return NULL; 38 | } 39 | 40 | void os_async_queue_free(os_async_queue_t *q) 41 | { 42 | if(q) 43 | { 44 | pthread_cond_broadcast(&(q->cond)); 45 | pthread_cond_destroy(&q->cond); 46 | pthread_mutex_destroy(&q->mutex); 47 | os_queue_free(q->queue); 48 | if(q->destroy) q->destroy(q,NULL); 49 | FREE(q); 50 | q = NULL; 51 | } 52 | } 53 | 54 | unsigned int os_async_queue_length(os_async_queue_t *q) 55 | { 56 | return os_queue_length(q->queue); 57 | } 58 | 59 | void os_async_queue_push(os_async_queue_t *q,void *data) 60 | { 61 | RETURN_IF_FAIL(q && data); 62 | pthread_mutex_lock(&q->mutex); 63 | os_queue_push_tail(q->queue,data); 64 | pthread_cond_signal(&q->cond); 65 | pthread_mutex_unlock(&q->mutex); 66 | } 67 | 68 | 69 | void *os_async_queue_pop(os_async_queue_t *q) 70 | { 71 | void *data = NULL; 72 | RETURN_VAL_IF_FAIL(q,NULL); 73 | pthread_mutex_lock(&q->mutex); 74 | pthread_cond_wait(&q->cond, &q->mutex); 75 | data = os_queue_pop_head(q->queue); 76 | pthread_mutex_unlock(&q->mutex); 77 | return data; 78 | } 79 | 80 | 81 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_broadcast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "os_broadcast.h" 18 | 19 | 20 | struct os_broadcast_s { 21 | /*< private >*/ 22 | int socket_fd; 23 | struct sockaddr_in addr; 24 | }; 25 | 26 | 27 | #ifndef DEBUG 28 | #define DEBUG(args...) printf(args) 29 | #endif 30 | 31 | #ifndef ERROR 32 | #define ERROR(args...) printf(args) 33 | #endif 34 | 35 | os_broadcast_t *os_broadcast_create(os_broadcast_type_e type,unsigned short port) 36 | { 37 | int ret = -1; 38 | const int opt=-1; 39 | os_broadcast_t *b = NULL; 40 | b = malloc(sizeof(os_broadcast_t)); 41 | assert(b); 42 | b->socket_fd = socket(AF_INET,SOCK_DGRAM,0); 43 | if(b->socket_fd < 0) 44 | { 45 | ERROR("socket fail...\n"); 46 | goto fail; 47 | } 48 | ret = setsockopt(b->socket_fd,SOL_SOCKET,SO_BROADCAST,(char*)&opt,sizeof(opt));//设置套接字类型 49 | if(ret < 0) 50 | { 51 | ERROR("setsockopt fail...\n"); 52 | goto set_fail; 53 | } 54 | bzero(&b->addr,sizeof(struct sockaddr_in)); 55 | b->addr.sin_family = AF_INET; 56 | b->addr.sin_addr.s_addr = (BROADCAST_TYPE_CLIENT == type)? htonl(INADDR_ANY) : htonl(INADDR_BROADCAST); 57 | b->addr.sin_port = htons(port); 58 | 59 | if(BROADCAST_TYPE_CLIENT == type) 60 | { 61 | if(-1 == bind(b->socket_fd,(struct sockaddr*)&(b->addr),sizeof(b->addr))) 62 | { 63 | ERROR("bind error...\n"); 64 | goto bind_fail; 65 | } 66 | } 67 | return b; 68 | 69 | bind_fail: 70 | set_fail: 71 | close(b->socket_fd); 72 | fail: 73 | free(b); 74 | return NULL; 75 | } 76 | 77 | void os_broadcast_destroy(os_broadcast_t *b) 78 | { 79 | if(b) 80 | { 81 | if(b->socket_fd > 0) close(b->socket_fd); 82 | free(b); 83 | } 84 | } 85 | 86 | int os_broadcast_send(os_broadcast_t *b,unsigned char *data,unsigned int size) 87 | { 88 | assert(b); 89 | socklen_t len = sizeof(b->addr); 90 | return sendto(b->socket_fd,data,size,0,(struct sockaddr*)&b->addr,len);//向广播地址发布消息 91 | } 92 | 93 | 94 | int os_broadcast_recv(os_broadcast_t *b,unsigned char *data,unsigned int size) 95 | { 96 | assert(b); 97 | socklen_t len = sizeof(b->addr); 98 | return recvfrom(b->socket_fd,data,size,0,(struct sockaddr*)&b->addr,&len); 99 | } 100 | 101 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_bus.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_evdriver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include /* O_CLOEXEC */ 11 | #include /* memset() */ 12 | #include 13 | #include 14 | #include /* for select() workaround */ 15 | #include /* struct signalfd_siginfo */ 16 | #include 17 | #include 18 | 19 | #include "os_evdriver.h" 20 | #include "multitask.h" 21 | 22 | #define MAX_EVENTS 10 23 | 24 | 25 | struct os_evdriver_s { 26 | struct list_head list; 27 | pthread_mutex_t mutex; 28 | /* INFO */ 29 | const char *owner; 30 | int loop; 31 | int running; 32 | }; 33 | 34 | struct os_evdriver_node_s { 35 | struct list_head list; 36 | os_evdriver_t *evdriver; 37 | int event_fd; 38 | os_evdriver_type_e event_type; 39 | os_evdriver_callback_t cb; 40 | void *user_data; 41 | }; 42 | 43 | static void msec2tspec(int msec, struct timespec *ts) 44 | { 45 | if (msec) { 46 | ts->tv_sec = msec / 1000; 47 | ts->tv_nsec = (msec % 1000) * 1000000; 48 | } else { 49 | ts->tv_sec = 0; 50 | ts->tv_nsec = 0; 51 | } 52 | } 53 | 54 | static int __evdriver_epoll_add(os_evdriver_node_t *node,unsigned int events) 55 | { 56 | struct epoll_event ev; 57 | RETURN_VAL_IF_FAIL(node, -1); 58 | ev.events = events | EPOLLRDHUP; 59 | ev.data.ptr = node; 60 | if (epoll_ctl(node->evdriver->loop, EPOLL_CTL_ADD, node->event_fd, &ev) < 0) { 61 | if (errno != EPERM) 62 | return -1; 63 | } 64 | return 0; 65 | } 66 | 67 | 68 | static int __evdriver_io_add(os_evdriver_node_t *node, int fd) 69 | { 70 | node->event_fd = fd; 71 | return __evdriver_epoll_add(node,EPOLLIN); 72 | } 73 | 74 | static int __evdriver_signal_add(os_evdriver_node_t *node, int signo) 75 | { 76 | int fd = -1; 77 | sigset_t mask; 78 | sigemptyset(&mask); 79 | sigaddset(&mask, signo); 80 | 81 | fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC); 82 | if (fd < 0) 83 | return -1; 84 | if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) 85 | return -1; 86 | if (signalfd(fd, &mask, SFD_NONBLOCK) < 0) 87 | return -1; 88 | node->event_fd = fd; 89 | return 0; 90 | } 91 | 92 | static int __evdriver_timer_add(os_evdriver_node_t *node, unsigned long when,unsigned long interval) 93 | { 94 | int fd = -1; 95 | struct itimerspec time; 96 | 97 | msec2tspec(when, &time.it_value); 98 | msec2tspec(interval, &time.it_interval); 99 | fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 100 | RETURN_VAL_IF_FAIL(fd > 0,-1); 101 | timerfd_settime(fd, 0, &time, NULL); 102 | node->event_fd = fd; 103 | return __evdriver_epoll_add(node,EPOLLIN); 104 | } 105 | 106 | 107 | 108 | os_evdriver_t *os_evdriver_create2(const char *name) 109 | { 110 | os_evdriver_t *evdriver = NULL; 111 | evdriver = MALLOC(sizeof(*evdriver)); 112 | RETURN_VAL_IF_FAIL(evdriver, NULL); 113 | memset(evdriver,0,sizeof(*evdriver)); 114 | evdriver->loop = -1; 115 | evdriver->owner = name; 116 | evdriver->running = 0; 117 | pthread_mutex_init(&evdriver->mutex, NULL); 118 | INIT_LIST_HEAD(&evdriver->list); 119 | return evdriver; 120 | } 121 | 122 | os_evdriver_t *os_evdriver_create(void) 123 | { 124 | return os_evdriver_create2("evdriver"); 125 | } 126 | 127 | 128 | void os_evdriver_destroy(os_evdriver_t *evdriver) 129 | { 130 | os_evdriver_node_t *node = NULL,*tmp = NULL; 131 | if(evdriver) 132 | { 133 | evdriver->running = 0; 134 | pthread_mutex_lock(&evdriver->mutex); 135 | list_for_each_entry_safe(node, tmp,&evdriver->list, list) { 136 | list_del(&node->list); 137 | epoll_ctl(node->evdriver->loop, EPOLL_CTL_DEL, node->event_fd, NULL); 138 | close(node->event_fd); 139 | node->event_fd = -1; 140 | FREE(node); 141 | node = NULL; 142 | } 143 | pthread_mutex_unlock(&evdriver->mutex); 144 | pthread_mutex_destroy(&evdriver->mutex); 145 | if(evdriver->loop > -1) close(evdriver->loop); 146 | evdriver->loop = -1; 147 | FREE(evdriver); 148 | evdriver = NULL; 149 | } 150 | } 151 | 152 | int os_evdriver_run2(os_evdriver_t *evdriver,int flags) 153 | { 154 | struct epoll_event ee[MAX_EVENTS]; 155 | int i, nfds; 156 | os_evdriver_node_t *ev_node = NULL; 157 | int timeout = -1; 158 | 159 | if (flags & EVDIRVER_NONBLOCK) 160 | timeout = 0; 161 | 162 | RETURN_VAL_IF_FAIL(evdriver && evdriver->loop >= 0,-1); 163 | 164 | evdriver->running = 1; 165 | while(evdriver->running) 166 | { 167 | unsigned int events; 168 | nfds = epoll_wait(evdriver->loop, ee, MAX_EVENTS,timeout); 169 | //printf("nfds=%d\n",nfds); 170 | for (i = 0; i < nfds; i++) 171 | { 172 | struct signalfd_siginfo fdsi; 173 | uint64_t exp; 174 | 175 | ev_node = (os_evdriver_node_t *)ee[i].data.ptr; 176 | CONTINUE_IF_FAIL(ev_node); 177 | events = ee[i].events; 178 | switch (ev_node->event_type) { 179 | case OS_EVDRIVER_IO: 180 | if (events & (EPOLLHUP | EPOLLERR)) 181 | { 182 | ASSERT(0); 183 | } 184 | break; 185 | 186 | case OS_EVDRIVER_SIGNAL: 187 | { 188 | if (read(ev_node->event_fd, &fdsi, sizeof(fdsi)) != sizeof(fdsi)) 189 | { 190 | ASSERT(0); 191 | } 192 | } 193 | break; 194 | 195 | case OS_EVDRIVER_TIMER: 196 | { 197 | if (read(ev_node->event_fd, &exp, sizeof(exp)) != sizeof(exp)) { 198 | ASSERT(0); 199 | } 200 | } 201 | break; 202 | default: 203 | ASSERT(0); 204 | break; 205 | } 206 | if(ev_node->cb) ev_node->cb(ev_node,ev_node->user_data); 207 | 208 | } 209 | if (flags & EVDIRVER_ONCE) 210 | break; 211 | } 212 | return 0; 213 | } 214 | 215 | int os_evdriver_run(os_evdriver_t *evdriver) 216 | { 217 | return os_evdriver_run2(evdriver,0); 218 | } 219 | 220 | os_evdriver_node_t *os_evdriver_add(os_evdriver_t *evdriver,os_event_t *ev,os_evdriver_callback_t cb,void *user_data) 221 | { 222 | os_evdriver_node_t *node = NULL; 223 | int ret = -1; 224 | RETURN_VAL_IF_FAIL(evdriver && ev && cb, NULL); 225 | RETURN_VAL_IF_FAIL(ev->event < OS_EVDRIVER_BUTT, NULL); 226 | node = MALLOC(sizeof(*evdriver)); 227 | RETURN_VAL_IF_FAIL(node, NULL); 228 | node->cb = cb; 229 | node->user_data = user_data; 230 | node->event_fd = -1; 231 | node->evdriver = evdriver; 232 | node->event_type = ev->event; 233 | switch (ev->event) 234 | { 235 | case OS_EVDRIVER_IO: 236 | { 237 | ret = __evdriver_io_add(node,ev->io.fd); 238 | GOTO_LABEL_IF_FAIL(0 == ret,fail); 239 | }break; 240 | case OS_EVDRIVER_TIMER: 241 | { 242 | ret = __evdriver_timer_add(node,ev->timer.when,ev->timer.interval); 243 | GOTO_LABEL_IF_FAIL(0 == ret,fail); 244 | }break; 245 | case OS_EVDRIVER_SIGNAL: 246 | { 247 | ret = __evdriver_signal_add(node,ev->signal.signo); 248 | GOTO_LABEL_IF_FAIL(0 == ret,fail); 249 | }break; 250 | default: 251 | { 252 | GOTO_LABEL_IF_FAIL(0,fail); 253 | }break; 254 | } 255 | pthread_mutex_lock(&evdriver->mutex); 256 | list_add_tail(&node->list, &evdriver->list); 257 | pthread_mutex_unlock(&evdriver->mutex); 258 | return node; 259 | fail: 260 | FREE(node); 261 | return NULL; 262 | } 263 | 264 | void os_evdriver_del(os_evdriver_node_t *evnode) 265 | { 266 | os_evdriver_node_t *node = NULL,*tmp = NULL; 267 | RETURN_IF_FAIL(evnode); 268 | os_evdriver_t *evdriver = evnode->evdriver; 269 | if(evdriver) 270 | { 271 | pthread_mutex_lock(&evdriver->mutex); 272 | list_for_each_entry_safe(node, tmp,&evdriver->list, list) { 273 | if(evnode == node) 274 | { 275 | list_del(&node->list); 276 | /* Remove from kernel */ 277 | epoll_ctl(evdriver->loop, EPOLL_CTL_DEL, evnode->event_fd, NULL); 278 | close(evnode->event_fd); 279 | evnode->event_fd = -1; 280 | break; 281 | } 282 | } 283 | pthread_mutex_unlock(&evdriver->mutex); 284 | } 285 | } 286 | 287 | 288 | 289 | 290 | 291 | 292 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_file.c: -------------------------------------------------------------------------------- 1 | 2 | #include "multitask.h" 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | unsigned int os_file_write(int fd, const void* buf, unsigned int count) 20 | { 21 | unsigned int nwritten = 0; 22 | while (count > 0) 23 | { 24 | ssize_t r = write(fd, buf, count); 25 | 26 | if (r < 0 && errno == EINTR) continue; 27 | if (r < 0) return r; 28 | if (r == 0) return nwritten; 29 | buf = (const char*)buf + r; 30 | count -= r; 31 | nwritten += r; 32 | } 33 | return nwritten; 34 | } 35 | 36 | int os_file_append(const char* path, unsigned char* data,unsigned int size) 37 | { 38 | int fd; 39 | 40 | fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0777); 41 | if (fd == -1) 42 | return -1; 43 | if (os_file_write(fd, data, size) < 0) 44 | { 45 | close(fd); 46 | return -1; 47 | } 48 | /* Use errno from failed close only if there was no write error. */ 49 | if (close(fd) != 0) return -1; 50 | 51 | return 0; 52 | } 53 | 54 | 55 | int os_file_write_str(const char* path, const char* str, int mode) 56 | { 57 | int fd; 58 | 59 | if (mode) 60 | fd = open(path, O_WRONLY | O_APPEND | O_CREAT, mode); 61 | else 62 | fd = open(path, O_WRONLY | O_APPEND); 63 | if (fd == -1) 64 | return -1; 65 | 66 | if (os_file_write(fd, str, strlen(str)) < 0) 67 | { 68 | close(fd); 69 | return -1; 70 | } 71 | 72 | /* Use errno from failed close only if there was no write error. */ 73 | if (close(fd) != 0) 74 | return -1; 75 | 76 | return 0; 77 | } 78 | 79 | 80 | 81 | int os_file_rewrite(const char *path, const void *data, int len) 82 | { 83 | int fd; 84 | 85 | fd = open(path, O_WRONLY|O_CREAT, 0777); 86 | if (fd == -1) 87 | return -1; 88 | 89 | if (os_file_write(fd, data, len) < 0) { 90 | close(fd); 91 | return -1; 92 | } 93 | 94 | /* Use errno from failed close only if there was no write error. */ 95 | if (close(fd) != 0) 96 | return -1; 97 | 98 | return 0; 99 | } 100 | 101 | unsigned int os_file_read(int fd, void *buf, unsigned int count) 102 | { 103 | unsigned int nread = 0; 104 | while (count > 0) { 105 | ssize_t r = read(fd, buf, count); 106 | if (r < 0 && errno == EINTR) 107 | continue; 108 | if (r < 0) 109 | return r; 110 | if (r == 0) 111 | return nread; 112 | buf = (char *)buf + r; 113 | count -= r; 114 | nread += r; 115 | } 116 | return nread; 117 | } 118 | 119 | int os_file_read2(const char *file,void *data, int len) 120 | { 121 | int fd; 122 | ssize_t sz; 123 | 124 | fd = open(file, O_RDONLY); 125 | if (fd < 0) 126 | return -errno; 127 | 128 | sz = os_file_read(fd, data, len); 129 | close(fd); 130 | if (sz < 0) 131 | return -errno; 132 | 133 | return sz; 134 | } 135 | 136 | 137 | void os_trace2file(const char* filename, const char* fmt, ...) 138 | { 139 | char buffer[1024 * 4] = {0}; 140 | int size = 0; 141 | if (filename) 142 | { 143 | va_list args; 144 | va_start(args, fmt); 145 | size = vsnprintf(buffer, sizeof(buffer), fmt, args); 146 | va_end(args); 147 | buffer[size] = '\n'; 148 | os_file_write_str(filename, buffer, 0777); 149 | } 150 | } 151 | 152 | int os_file_exist(const char *filename) 153 | { 154 | return (access(filename,F_OK) != -1); 155 | } 156 | 157 | int os_file_size(const char* filename) 158 | { 159 | struct stat statbuf; 160 | int size = 0; 161 | stat(filename,&statbuf); 162 | size = statbuf.st_size; 163 | return size; 164 | } 165 | 166 | int os_file_key_write(const char *filename,const char *key,const char *val) 167 | { 168 | char buf[512] = {0}; 169 | 170 | snprintf(buf,sizeof(buf),"%s=%s\n",key,val); 171 | return os_file_write_str(filename, buf, 0777); 172 | } 173 | 174 | 175 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_io.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | 9 | 10 | 11 | int os_io_register(const char *name,os_io_callback_t cb,void *user_data) 12 | { 13 | return 0; 14 | } 15 | 16 | void os_io_unregister(const char *name) 17 | { 18 | 19 | } 20 | 21 | int os_io_node_add(os_io_node *node) 22 | { 23 | return 0; 24 | } 25 | 26 | void os_io_node_del(const char *name) 27 | { 28 | 29 | } 30 | 31 | void os_io_node_del2(int fd) 32 | { 33 | 34 | } 35 | 36 | 37 | 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_log.c: -------------------------------------------------------------------------------- 1 | /* Includes ------------------------------------------------------------------*/ 2 | #include 3 | #include 4 | 5 | #include "os_log.h" 6 | #include "os_misc.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* Private typedef -----------------------------------------------------------*/ 13 | /* Private macro -------------------------------------------------------------*/ 14 | /* Private variables ---------------------------------------------------------*/ 15 | static os_log_callback_t g_os_log_callback = NULL; 16 | /* Private function prototypes -----------------------------------------------*/ 17 | /* Private functions ---------------------------------------------------------*/ 18 | 19 | const char *os_log_enum2str(MMP_LOG_ENUM level) 20 | { 21 | const char *ret = "[U]"; 22 | switch(level) 23 | { 24 | case MLOG_VERBOSE: ret = "[V]";break; 25 | case MLOG_DEBUG: ret = "[D]";break; 26 | case MLOG_INFO: ret = "[I]";break; 27 | case MLOG_MESSAGE: ret = "[M]";break; 28 | case MLOG_WARN: ret = "[W]";break; 29 | case MLOG_ERROR: ret = "[E]";break; 30 | case MLOG_FATAL: ret = "[F]";break; 31 | default: break; 32 | } 33 | return ret; 34 | } 35 | 36 | 37 | const char *os_log_colour_get(MMP_LOG_ENUM level) 38 | { 39 | const char *ret = COLOUR_NONE; 40 | switch(level) 41 | { 42 | case MLOG_VERBOSE: ret = COLOUR_NONE;break; 43 | case MLOG_DEBUG: ret = COLOUR_WHITE;break; 44 | case MLOG_INFO: ret = COLOUR_WHITE;break; 45 | case MLOG_MESSAGE: ret = COLOUR_L_GREEN;break; 46 | case MLOG_WARN: ret = COLOUR_YELLOW;break; 47 | case MLOG_ERROR: ret = COLOUR_L_RED;break; 48 | case MLOG_FATAL: ret = COLOUR_RED;break; 49 | default: break; 50 | } 51 | return ret; 52 | } 53 | 54 | void os_log_message(int level 55 | ,const char *file_name 56 | ,int line 57 | ,const char *func_name 58 | ,const char *fmt, ...) 59 | { 60 | int ret = 0; 61 | /* 头-实际内容 */ 62 | /* 日志头 */ 63 | char task_name[32] = {0}; 64 | char time_buf[64] = {0}; 65 | char log_buffer[MLOG_BUFF_SIZE] = {0}; 66 | thread_name_get(task_name); 67 | time2str(time_buf,sizeof(time_buf)); 68 | 69 | ret = snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"%s%s ",os_log_colour_get(level),time_buf); 70 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"(%s,%d)",file_name,line); 71 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"[%s]",task_name); 72 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"%s",os_log_enum2str(level)); 73 | /* 组包 */ 74 | va_list args; 75 | va_start(args, fmt); 76 | vsnprintf(log_buffer+ret,sizeof(log_buffer)-ret,fmt,args); 77 | va_end(args); 78 | /* 输出 */ 79 | if(g_os_log_callback) 80 | g_os_log_callback(level,log_buffer); 81 | else 82 | printf("%s\n""\e[0m",log_buffer); 83 | } 84 | 85 | 86 | void os_log_assert(const char *source 87 | ,const char *expr 88 | ,const char *file_name 89 | ,int line 90 | ,const char *fmt, ...) 91 | { 92 | int ret = 0; 93 | /* 头-实际内容 */ 94 | /* 日志头 */ 95 | char task_name[32] = {0}; 96 | char time_buf[64] = {0}; 97 | char log_buffer[MLOG_BUFF_SIZE] = {0}; 98 | const char *colour = COLOUR_L_RED; 99 | thread_name_get(task_name); 100 | time2str(time_buf,sizeof(time_buf)); 101 | 102 | ret = snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"%s%s ",colour?colour:"",time_buf); 103 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"(%s,%d)",file_name,line); 104 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"[%s]",task_name); 105 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"assertion failed: # %s #",expr?expr:""); 106 | /* 组包 */ 107 | va_list args; 108 | va_start(args, fmt); 109 | vsnprintf(log_buffer+ret,sizeof(log_buffer)-ret,fmt,args); 110 | va_end(args); 111 | /* 输出 */ 112 | if(g_os_log_callback) 113 | g_os_log_callback(MLOG_FATAL,log_buffer); 114 | else 115 | printf("%s\n""\e[0m",log_buffer); 116 | } 117 | 118 | 119 | #ifdef __cplusplus 120 | } 121 | #endif 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_misc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "os_log.h" 14 | #include "multitask.h" 15 | 16 | extern pid_t gettid(void); 17 | 18 | void time2str(char *buf,int size) 19 | { 20 | assert(buf && size > 0); 21 | struct timeval tv; 22 | struct tm tm; 23 | int pos = 0; 24 | long millisecond = 0; 25 | bzero(buf,size); 26 | gettimeofday(&tv,NULL); 27 | localtime_r(&tv.tv_sec,&tm); 28 | //格式化化为字符串输出. 29 | strftime(buf,size,"%F %T",&tm); 30 | pos = strlen(buf); 31 | millisecond = tv.tv_usec / 1000; 32 | snprintf(buf+pos,size-pos,".%lu",millisecond); 33 | } 34 | 35 | void thread_name_get(char *task_name) 36 | { 37 | prctl(PR_GET_NAME,task_name); 38 | } 39 | 40 | void thread_name_set(const char *task_name) 41 | { 42 | prctl(PR_SET_NAME,task_name); 43 | } 44 | 45 | 46 | int child_pid_serialize2string(char *strs,unsigned int size) 47 | { 48 | #define CHILD_PID_CMD "ls /proc/%d/task/ | xargs" 49 | char cmd[1024] = {0,}; 50 | FILE *fp = NULL; 51 | char *pResult = NULL; 52 | RETURN_VAL_IF_FAIL(strs && size, -1); 53 | pid_t pid = getpid(); 54 | memset(cmd,0,sizeof(cmd)); 55 | snprintf(cmd,sizeof(cmd),CHILD_PID_CMD,pid); 56 | //printf("cmd: %s\n",cmd); 57 | fp = popen (cmd, "r"); 58 | assert(fp); 59 | memset(strs,0,size); 60 | pResult = fgets (strs, size, fp); 61 | assert(pResult); 62 | fclose(fp); 63 | fp = NULL; 64 | //printf("strs: %s\n",strs); 65 | return 0; 66 | } 67 | 68 | 69 | int child_pid_get(unsigned long **pp_arr,unsigned int *p_size) 70 | { 71 | RETURN_VAL_IF_FAIL(pp_arr && p_size, -1); 72 | char buf[1024] = {0}; 73 | int ret = -1; 74 | int i; 75 | char *sub = NULL,*str = NULL; 76 | unsigned int cnt = 0; 77 | unsigned long *p_arr = NULL; 78 | ret = child_pid_serialize2string(buf,sizeof(buf)); 79 | RETURN_VAL_IF_FAIL(0 == ret, -1); 80 | 81 | for(i = 0;i < sizeof(buf) && 0 != buf[i];i++) 82 | { 83 | if(' ' == buf[i]) 84 | cnt++; 85 | } 86 | cnt += 1; 87 | p_arr = MALLOC(cnt*sizeof(unsigned long)); 88 | RETURN_VAL_IF_FAIL(p_arr, -1); 89 | 90 | i = 0; 91 | str = buf; 92 | do { 93 | sub = strtok(str," "); 94 | if(NULL == sub) 95 | break; 96 | sscanf(str,"%lu",&p_arr[i]); 97 | str += (strlen(sub)+1); 98 | i++; 99 | } while(1); 100 | 101 | *p_size = cnt; 102 | *pp_arr = p_arr; 103 | return 0; 104 | } 105 | 106 | 107 | void child_pid_free(unsigned long *p_arr) 108 | { 109 | if(p_arr) FREE(p_arr); 110 | } 111 | 112 | 113 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_mm.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE /* See feature_test_macros(7) */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "multitask.h" 14 | 15 | #if defined(CONFIG_OS_MM_TRACE) 16 | pthread_mutex_t os_mm_mutex = PTHREAD_MUTEX_INITIALIZER; 17 | static LIST_HEAD(os_mm_list); 18 | #endif 19 | 20 | extern pid_t gettid(void); 21 | 22 | void *os_mm_malloc(const char *func,unsigned long line,unsigned long size) 23 | { 24 | void *addr = NULL; 25 | addr = malloc(size); 26 | assert(addr); 27 | unsigned long pid = (unsigned long)gettid(); //process ID 28 | unsigned long tid = (unsigned long)pthread_self(); //thread ID 29 | 30 | #if defined(CONFIG_OS_TASK_MM_TRACE) 31 | os_task_mm_node_t *node = NULL; 32 | node = malloc(sizeof(*node)); 33 | assert(node); 34 | node->tid = tid; 35 | node->pid = pid; 36 | node->func = func; 37 | node->line = line; 38 | node->size = size; 39 | node->addr = addr; 40 | prctl(PR_GET_NAME,node->os_task_name); 41 | os_task_mm_add(node->tid,node); 42 | #endif 43 | 44 | #if defined(CONFIG_OS_MM_TRACE) 45 | os_task_mm_node_t *mm_node = NULL; 46 | mm_node = malloc(sizeof(*mm_node)); 47 | assert(mm_node); 48 | mm_node->tid = tid; 49 | mm_node->pid = pid; 50 | mm_node->func = func; 51 | mm_node->line = line; 52 | mm_node->size = size; 53 | mm_node->addr = addr; 54 | prctl(PR_GET_NAME,node->os_task_name); 55 | pthread_mutex_lock(&os_mm_mutex); 56 | list_add_tail(&mm_node->list, &os_mm_list); 57 | pthread_mutex_unlock(&os_mm_mutex); 58 | #endif 59 | 60 | return addr; 61 | } 62 | 63 | 64 | void os_mm_free(void *addr) 65 | { 66 | if(addr) 67 | { 68 | #if defined(CONFIG_TASK_MM_TRACE) 69 | unsigned long tid; //thread ID 70 | tid = (unsigned long)pthread_self(); 71 | os_task_mm_del(tid,addr); 72 | #endif 73 | 74 | #if defined(CONFIG_MM_TRACE) 75 | os_task_mm_node_t *mm_node = NULL,*mm_tmp = NULL; 76 | pthread_mutex_lock(&os_mm_mutex); 77 | list_for_each_entry_safe(mm_node, mm_tmp,&os_mm_list, list) { 78 | if(mm_node->addr == addr) 79 | { 80 | list_del(&mm_node->list); 81 | free(mm_node); 82 | mm_node = NULL; 83 | break; 84 | } 85 | } 86 | pthread_mutex_unlock(&os_mm_mutex); 87 | #endif 88 | 89 | free(addr); 90 | addr = NULL; 91 | } 92 | } 93 | 94 | 95 | void os_mm_show(void) 96 | { 97 | os_task_mm_node_t *node = NULL,*tmp = NULL; 98 | printf("\n\n=========================================== mm_show ===========================================\n"); 99 | printf("%-15s %-15s %-15s %-32s %-15s %-15s %-15s\n", 100 | "[task]","[tid]","[pid]","[function]","[line]","[addr]","[size]"); 101 | list_for_each_entry_safe(node, tmp,&os_mm_list, list) { 102 | printf("%-15s %-15lu %-15lu %-32s %-15lu %-15p %-15lu\n", 103 | node->os_task_name,node->tid,node->pid,node->func,node->line,node->addr,node->size); 104 | } 105 | } 106 | 107 | 108 | void os_mm_show2(void (*show)(const char *)) 109 | { 110 | os_task_mm_node_t *node = NULL,*tmp = NULL; 111 | char buf[1024] = {0,}; 112 | printf("\n\n=========================================== mm_show ===========================================\n"); 113 | printf("%-15s %-15s %-15s %-32s %-15s %-15s %-15s\n", 114 | "[task]","[tid]","[pid]","[function]","[line]","[addr]","[size]"); 115 | list_for_each_entry_safe(node, tmp,&os_mm_list, list) { 116 | memset(buf,0,sizeof(buf)); 117 | snprintf(buf,sizeof(buf),"%-15s %-15lu %-15lu %-32s %-15lu %-15p %-15lu\n", 118 | node->os_task_name,node->tid,node->pid,node->func,node->line,node->addr,node->size); 119 | printf("%s",buf); 120 | if(show) show((const char *)buf); 121 | } 122 | } 123 | 124 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_msg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "os_msg.h" 10 | #include "os_log.h" 11 | #include "os_async_queue.h" 12 | #include "multitask.h" 13 | 14 | 15 | 16 | int os_msg_send(os_msg_t *msg) 17 | { 18 | RETURN_VAL_IF_FAIL(msg, -1); 19 | os_async_queue_t *q = NULL; 20 | q = (os_async_queue_t *)os_task_aq_get(msg->dst); 21 | RETURN_VAL_IF_FAIL(q, -1); 22 | os_async_queue_push(q, msg); 23 | return 0; 24 | } 25 | 26 | 27 | os_msg_t *os_msg_recv(void) 28 | { 29 | os_msg_t *msg = NULL; 30 | os_async_queue_t *q; 31 | q = os_task_aq_self(); 32 | RETURN_VAL_IF_FAIL(q, NULL); 33 | msg = os_async_queue_pop(q); 34 | RETURN_VAL_IF_FAIL(msg, NULL); 35 | return msg; 36 | } 37 | 38 | 39 | int os_msg_send2(const char *dst,int priority,void *data,unsigned int size) 40 | { 41 | os_msg_t *msg = NULL; 42 | RETURN_VAL_IF_FAIL(dst , -1); 43 | msg = MALLOC(sizeof(*msg)+size); 44 | RETURN_VAL_IF_FAIL(msg, -1); 45 | msg->src = os_task_name_get_from_tid((unsigned long)pthread_self()); 46 | msg->dst = dst; 47 | msg->priority = priority; 48 | msg->size = size; 49 | memcpy(msg->data,data,size); 50 | ///////////////////////////// 51 | os_async_queue_t *q = NULL; 52 | q = (os_async_queue_t *)os_task_aq_get(dst); 53 | GOTO_LABEL_IF_FAIL(q, fail); 54 | os_async_queue_push(q, msg); 55 | return 0; 56 | 57 | fail: 58 | FREE(msg); 59 | msg = NULL; 60 | return -1; 61 | } 62 | 63 | 64 | void *os_msg_recv2(unsigned int *p_size) 65 | { 66 | os_msg_t *msg = NULL; 67 | void *data = NULL; 68 | os_async_queue_t *q; 69 | q = os_task_aq_self(); 70 | RETURN_VAL_IF_FAIL(q, NULL); 71 | msg = os_async_queue_pop(q); 72 | RETURN_VAL_IF_FAIL(msg, NULL); 73 | data = MALLOC(msg->size); 74 | RETURN_VAL_IF_FAIL2(data,{FREE(msg);msg=NULL;},NULL); 75 | if(p_size) *p_size = msg->size; 76 | memcpy(data,msg->data,msg->size); 77 | FREE(msg); 78 | msg = NULL; 79 | return data; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_multicast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "os_multicast.h" 18 | 19 | 20 | 21 | struct os_multicast_s { 22 | /*< private >*/ 23 | int socket_fd; 24 | struct sockaddr_in addr; 25 | }; 26 | 27 | 28 | #ifndef DEBUG 29 | #define DEBUG(args...) printf(args) 30 | #endif 31 | 32 | #ifndef ERROR 33 | #define ERROR(args...) printf(args) 34 | #endif 35 | 36 | os_multicast_t *os_multicast_create(os_multicast_type_e type,const char *addr,unsigned short port) 37 | { 38 | os_multicast_t *m = NULL; 39 | m = malloc(sizeof(os_multicast_t)); 40 | int err; 41 | assert(m); 42 | m->socket_fd = socket(AF_INET,SOCK_DGRAM,0); 43 | if(m->socket_fd < 0) 44 | { 45 | printf("socket create fail.\n"); 46 | return NULL; 47 | } 48 | memset(&m->addr,0,sizeof(m->addr)); 49 | m->addr.sin_family = AF_INET; 50 | m->addr.sin_addr.s_addr = (MULTICAST_TYPE_CLIENT == type)?htonl(INADDR_ANY) :inet_addr(addr); 51 | m->addr.sin_port = htons(port); 52 | 53 | if(MULTICAST_TYPE_CLIENT == type) 54 | { 55 | /*绑定socket*/ 56 | err = bind(m->socket_fd,(struct sockaddr*)&m->addr,sizeof(m->addr)); 57 | if(err<0) 58 | { 59 | printf("bind fail.\n"); 60 | return NULL; 61 | } 62 | /*设置回环许可*/ 63 | int loop=1; 64 | err=setsockopt(m->socket_fd,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop)); 65 | if(err<0) 66 | { 67 | printf("set sock error\n"); 68 | return NULL; 69 | } 70 | struct ip_mreq mreq;/*加入广播组*/ 71 | mreq.imr_multiaddr.s_addr=inet_addr(addr);//广播地址 72 | mreq.imr_interface.s_addr=htonl(INADDR_ANY); //网络接口为默认 73 | /*将本机加入广播组*/ 74 | err=setsockopt(m->socket_fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)); 75 | if(err<0) 76 | { 77 | printf("set sock error\n"); 78 | return NULL; 79 | } 80 | } 81 | return m; 82 | 83 | } 84 | 85 | void os_multicast_destroy(os_multicast_t *m) 86 | { 87 | if(m) 88 | { 89 | if(m->socket_fd > 0) close(m->socket_fd); 90 | free(m); 91 | } 92 | } 93 | 94 | int os_multicast_send(os_multicast_t *m,unsigned char *data,unsigned int size) 95 | { 96 | assert(m); 97 | //向局部多播地址发送多播内容 98 | return sendto(m->socket_fd,data,size,0,(struct sockaddr*)&m->addr,sizeof(m->addr)); 99 | } 100 | 101 | int os_multicast_recv(os_multicast_t *m,unsigned char *data,unsigned int size) 102 | { 103 | assert(m); 104 | socklen_t len = sizeof(m->addr); 105 | return recvfrom(m->socket_fd,data,size,0,(struct sockaddr*)&m->addr,&len); 106 | } 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "os_queue.h" 8 | #include "os_log.h" 9 | #include "multitask.h" 10 | 11 | struct os_queue_s { 12 | struct list_head list; 13 | void *data; 14 | }; 15 | 16 | 17 | os_queue_t *os_queue_new(void) 18 | { 19 | os_queue_t *q = NULL; 20 | q = MALLOC(sizeof(*q)); 21 | RETURN_VAL_IF_FAIL(q, NULL); 22 | memset(q,0,sizeof(*q)); 23 | INIT_LIST_HEAD(&q->list); 24 | return q; 25 | } 26 | 27 | void os_queue_free(os_queue_t *q) 28 | { 29 | os_queue_t *queue = NULL,*queue_next = NULL; 30 | if(q) 31 | { 32 | list_for_each_entry_safe(queue, queue_next, &q->list,list) { 33 | list_del(&queue->list); 34 | if(queue->data) 35 | { 36 | FREE(queue->data); 37 | queue->data = NULL; 38 | } 39 | } 40 | FREE(q); 41 | q = NULL; 42 | } 43 | } 44 | 45 | unsigned int os_queue_length(os_queue_t *q) 46 | { 47 | return list_length(&q->list); 48 | } 49 | 50 | unsigned int os_queue_empty(os_queue_t *q) 51 | { 52 | RETURN_VAL_IF_FAIL(q, 0); 53 | return list_empty(&q->list); 54 | } 55 | 56 | 57 | void os_queue_push_tail(os_queue_t *q,void *data) 58 | { 59 | os_queue_t *node = NULL; 60 | RETURN_IF_FAIL(q); 61 | node = MALLOC(sizeof(*node)); 62 | RETURN_IF_FAIL(node); 63 | node->data = data; 64 | list_add_tail(&node->list, &q->list); 65 | } 66 | 67 | void os_queue_push_head(os_queue_t *q,void *data) 68 | { 69 | os_queue_t *node = NULL; 70 | RETURN_IF_FAIL(q); 71 | node = MALLOC(sizeof(*node)); 72 | RETURN_IF_FAIL(node); 73 | node->data = data; 74 | list_add(&node->list, &q->list); 75 | } 76 | 77 | void *os_queue_pop_tail(os_queue_t *q) 78 | { 79 | void *data = NULL; 80 | os_queue_t *queue = NULL,*queue_next = NULL; 81 | RETURN_VAL_IF_FAIL(q, NULL); 82 | list_for_each_entry_safe_reverse(queue, queue_next, &q->list,list) 83 | { 84 | list_del(&queue->list); 85 | data = queue->data; 86 | FREE(queue); 87 | queue = NULL; 88 | break; 89 | } 90 | return data; 91 | } 92 | 93 | void *os_queue_pop_head(os_queue_t *q) 94 | { 95 | void *data = NULL; 96 | os_queue_t *queue = NULL,*queue_next = NULL; 97 | RETURN_VAL_IF_FAIL(q, NULL); 98 | list_for_each_entry_safe(queue, queue_next, &q->list,list) 99 | { 100 | list_del(&queue->list); 101 | data = queue->data; 102 | FREE(queue); 103 | queue = NULL; 104 | break; 105 | } 106 | return data; 107 | } 108 | 109 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_stdlibc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include "os_stdlibc.h" 9 | 10 | 11 | void *os_malloc(os_size_t size) 12 | { 13 | return malloc((size_t)size); 14 | } 15 | 16 | void os_free(void *ptr) 17 | { 18 | free(ptr); 19 | } 20 | 21 | void *os_calloc(os_size_t nmemb, os_size_t size) 22 | { 23 | return calloc((size_t)nmemb,(size_t)size); 24 | } 25 | 26 | void *os_realloc(void *ptr, os_size_t size) 27 | { 28 | return realloc(ptr,(size_t)size); 29 | } 30 | 31 | int os_system(const char *command) 32 | { 33 | return system(command); 34 | } 35 | 36 | unsigned int os_sleep(unsigned int seconds) 37 | { 38 | return sleep(seconds); 39 | } 40 | 41 | 42 | void *os_memset(void *s, int c, os_size_t n) 43 | { 44 | return memset(s, c, (size_t)n); 45 | } 46 | 47 | 48 | char *os_strcpy(char *dest, const char *src) 49 | { 50 | return strcpy(dest,src); 51 | } 52 | 53 | char *os_strncpy(char *dest, const char *src, os_size_t n) 54 | { 55 | return strncpy(dest,src,n); 56 | } 57 | 58 | int os_scanf(const char *format, ...) 59 | { 60 | int ret = -1; 61 | va_list args; 62 | va_start(args, format); 63 | ret = scanf(format,args); 64 | va_end(args); 65 | return ret; 66 | } 67 | 68 | int os_sscanf(const char *str, const char *format, ...) 69 | { 70 | int ret = -1; 71 | va_list args; 72 | va_start(args, format); 73 | ret = sscanf(str,format,args); 74 | va_end(args); 75 | return ret; 76 | } 77 | 78 | os_size_t os_strlen(const char *s) 79 | { 80 | return strlen(s); 81 | } 82 | 83 | 84 | int os_strcmp(const char *s1, const char *s2) 85 | { 86 | return strcmp(s1,s2); 87 | } 88 | 89 | 90 | int os_strncmp(const char *s1, const char *s2, unsigned int n) 91 | { 92 | return strncmp(s1,s2,n); 93 | } 94 | 95 | 96 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_sys.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "multitask.h" 12 | 13 | 14 | int os_open(const char *pathname, int flags) 15 | { 16 | return open(pathname,flags); 17 | } 18 | 19 | int os_open2(const char *pathname, int flags, int mode) 20 | { 21 | return open(pathname,flags,mode); 22 | } 23 | 24 | int os_close(int fd) 25 | { 26 | return close(fd); 27 | } 28 | 29 | int os_read(int fd, void *buf, unsigned int count) 30 | { 31 | return read(fd,buf,count); 32 | } 33 | 34 | int os_write(int fd, const void *buf, unsigned int count) 35 | { 36 | return write(fd,buf,count); 37 | } 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_task.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "multitask.h" 14 | 15 | #include 16 | 17 | __attribute__((weak)) pid_t gettid(void) 18 | { 19 | return syscall(SYS_gettid); 20 | } 21 | 22 | 23 | typedef struct os_task_info_s { 24 | const char *name; 25 | unsigned long stack_size; 26 | int priority; 27 | os_task_func_t func; 28 | void *arg; 29 | /* other */ 30 | unsigned long tid; 31 | unsigned long pid; 32 | }os_task_info_t; 33 | 34 | struct os_task_s { 35 | struct list_head list; 36 | /////////////////////// 37 | struct list_head head; 38 | pthread_mutex_t mutex; 39 | os_task_info_t info; 40 | os_async_queue_t *q; 41 | }; 42 | 43 | 44 | pthread_mutex_t os_task_mutex = PTHREAD_MUTEX_INITIALIZER; 45 | static LIST_HEAD(os_task_list); 46 | 47 | 48 | static void os_task_exit(os_task_t *task) 49 | { 50 | os_task_t *node = NULL,*tmp = NULL; 51 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 52 | 53 | if(task) 54 | { 55 | MLOGW("os_task_exit: %s(%lu : %lu)\n",task->info.name,task->info.pid,task->info.tid); 56 | if(task->q) os_async_queue_free(task->q); 57 | task->q = NULL; 58 | pthread_mutex_lock(&os_task_mutex); 59 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 60 | if(node == task) 61 | { 62 | list_del(&node->list); 63 | break; 64 | } 65 | } 66 | pthread_mutex_unlock(&os_task_mutex); 67 | printf("%-15s %-15s %-15s %-32s %-15s %-15s %-15s\n", 68 | "[task]","[tid]","[pid]","[function]","[line]","[addr]","[size]"); 69 | list_for_each_entry_safe(mnode, tmnode,&task->head, list) { 70 | printf("%-15s %-15lu %-15lu %-32s %-15lu %-15p %-15lu\n",mnode->os_task_name, 71 | mnode->tid,mnode->pid,mnode->func,mnode->line,mnode->addr,mnode->size); 72 | list_del(&mnode->list); 73 | free(mnode); 74 | } 75 | pthread_mutex_destroy(&task->mutex); 76 | free(task); 77 | task = NULL; 78 | } 79 | } 80 | 81 | static void *__os_task_routine(void *arg) 82 | { 83 | pthread_detach(pthread_self()); 84 | os_task_t *task = (os_task_t *)arg; 85 | RETURN_VAL_IF_FAIL(task,NULL); 86 | task->info.tid = (unsigned long)pthread_self(); 87 | task->info.pid = (unsigned long)gettid(); 88 | prctl(PR_SET_NAME,task->info.name); 89 | if(task->info.func) 90 | task->info.func(task->info.arg); 91 | os_task_exit(task); 92 | return NULL; 93 | } 94 | 95 | os_task_t *os_task_create(const char *name,unsigned long stack_size,int priority,os_task_func_t func,void *arg) 96 | { 97 | os_task_t *task = NULL; 98 | pthread_t thread_id; 99 | task = malloc(sizeof(*task)); 100 | RETURN_VAL_IF_FAIL(task,NULL); 101 | memset(task,0,sizeof(*task)); 102 | task->info.name = name; 103 | task->info.stack_size = stack_size; 104 | task->info.priority = priority; 105 | task->info.func = func; 106 | task->info.arg = arg; 107 | task->q = os_async_queue_new(); 108 | 109 | INIT_LIST_HEAD(&task->head); 110 | pthread_mutex_init(&task->mutex, NULL); 111 | pthread_mutex_lock(&os_task_mutex); 112 | list_add_tail(&task->list, &os_task_list); 113 | pthread_mutex_unlock(&os_task_mutex); 114 | 115 | if(pthread_create(&thread_id, NULL, __os_task_routine, (void *)task) != 0) 116 | { 117 | os_async_queue_free(task->q); 118 | task->q = NULL; 119 | list_del_init(&task->list); 120 | pthread_mutex_destroy(&task->mutex); 121 | free(task); 122 | task=NULL; 123 | return NULL; 124 | } 125 | return task; 126 | } 127 | 128 | os_task_t *os_task_create2(const char *name,unsigned long stack_size,int priority,int async,os_task_func_t func,void *arg) 129 | { 130 | os_task_t *task = NULL; 131 | pthread_t thread_id; 132 | task = malloc(sizeof(*task)); 133 | assert(task); 134 | memset(task,0,sizeof(*task)); 135 | task->info.name = name; 136 | task->info.stack_size = stack_size; 137 | task->info.priority = priority; 138 | task->info.func = func; 139 | task->info.arg = arg; 140 | if(async > 0) task->q = os_async_queue_new(); 141 | 142 | INIT_LIST_HEAD(&task->head); 143 | pthread_mutex_init(&task->mutex, NULL); 144 | pthread_mutex_lock(&os_task_mutex); 145 | list_add_tail(&task->list, &os_task_list); 146 | pthread_mutex_unlock(&os_task_mutex); 147 | 148 | if(pthread_create(&thread_id, NULL, __os_task_routine, (void *)task) != 0) 149 | { 150 | os_async_queue_free(task->q); 151 | task->q = NULL; 152 | list_del_init(&task->list); 153 | pthread_mutex_destroy(&task->mutex); 154 | free(task); 155 | task=NULL; 156 | return NULL; 157 | return NULL; 158 | } 159 | return task; 160 | } 161 | 162 | 163 | os_async_queue_t *os_task_aq_get(const char *name) 164 | { 165 | os_async_queue_t *qq = NULL; 166 | os_task_t *node = NULL,*tmp = NULL; 167 | pthread_mutex_lock(&os_task_mutex); 168 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 169 | if(0 == strcmp(name,node->info.name)) 170 | { 171 | qq = node->q; 172 | break; 173 | } 174 | } 175 | pthread_mutex_unlock(&os_task_mutex); 176 | return (os_async_queue_t *)qq; 177 | } 178 | 179 | const char *os_task_name_get_from_pid(unsigned long pid) 180 | { 181 | os_task_t *node = NULL,*tmp = NULL; 182 | const char *name = NULL; 183 | pthread_mutex_lock(&os_task_mutex); 184 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 185 | if(pid == node->info.pid) 186 | { 187 | name = node->info.name; 188 | break; 189 | } 190 | } 191 | pthread_mutex_unlock(&os_task_mutex); 192 | return name; 193 | } 194 | 195 | const char *os_task_name_get_from_tid(unsigned long tid) 196 | { 197 | os_task_t *node = NULL,*tmp = NULL; 198 | const char *name = NULL; 199 | pthread_mutex_lock(&os_task_mutex); 200 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 201 | if(tid == node->info.tid) 202 | { 203 | name = node->info.name; 204 | break; 205 | } 206 | } 207 | pthread_mutex_unlock(&os_task_mutex); 208 | return name; 209 | } 210 | 211 | 212 | os_async_queue_t *os_task_aq_self(void) 213 | { 214 | os_async_queue_t *qq = NULL; 215 | os_task_t *node = NULL,*tmp = NULL; 216 | unsigned long tid = (unsigned long)pthread_self(); 217 | pthread_mutex_lock(&os_task_mutex); 218 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 219 | if(tid == node->info.tid) 220 | { 221 | qq = node->q; 222 | break; 223 | } 224 | } 225 | pthread_mutex_unlock(&os_task_mutex); 226 | return qq; 227 | } 228 | 229 | 230 | 231 | void os_task_mm_add(unsigned long tid,os_task_mm_node_t *mnode) 232 | { 233 | os_task_t *node = NULL,*tmp = NULL; 234 | pthread_mutex_lock(&os_task_mutex); 235 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 236 | if(tid == node->info.tid) 237 | { 238 | list_add_tail(&mnode->list, &node->head); 239 | break; 240 | } 241 | } 242 | pthread_mutex_unlock(&os_task_mutex); 243 | } 244 | 245 | 246 | void os_task_mm_del(unsigned long tid,void *addr) 247 | { 248 | os_task_t *node = NULL,*tmp = NULL; 249 | pthread_mutex_lock(&os_task_mutex); 250 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 251 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 252 | list_for_each_entry_safe(mnode, tmnode,&node->head, list) { 253 | if(addr == mnode->addr) 254 | { 255 | list_del(&mnode->list); 256 | free(mnode); 257 | goto succ; 258 | } 259 | } 260 | } 261 | succ: 262 | pthread_mutex_unlock(&os_task_mutex); 263 | } 264 | 265 | 266 | void os_task_mm_show(void) 267 | { 268 | os_task_t *node = NULL,*tmp = NULL; 269 | char time[64] = {0,}; 270 | unsigned long os_task_pid = 0,os_task_tid = 0; 271 | unsigned long mem_size = 0; 272 | const char *os_task_name = NULL; 273 | unsigned long *p_arr = NULL; 274 | unsigned int arr_size = 0; 275 | int ret = 0; 276 | int i; 277 | ////////////////////////////// 278 | time2str(time,sizeof(time)); 279 | printf("\n\n=========================================== os_task_mm_show [%s] ===========================================\n",time); 280 | printf("%-20s %-20s %-20s %-20s\n","[name]","[pid]","[tid]","[size]"); 281 | 282 | ret = child_pid_get(&p_arr,&arr_size); 283 | RETURN_IF_FAIL(0 == ret); 284 | for(i = 0;i < arr_size;i++) 285 | { 286 | os_task_pid = p_arr[i]; 287 | /////////////////////////////// 288 | mem_size = 0; 289 | os_task_name = NULL; 290 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 291 | if(os_task_pid == node->info.pid) 292 | { 293 | os_task_name = node->info.name; 294 | os_task_tid = node->info.tid; 295 | /////////////////////////////// 296 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 297 | list_for_each_entry_safe(mnode, tmnode,&node->head, list) { 298 | mem_size += mnode->size; 299 | } 300 | } 301 | } 302 | printf("%-20s %-20lu %-20lu %-20lu\n",os_task_name,os_task_pid,os_task_tid,mem_size); 303 | } 304 | child_pid_free(p_arr); 305 | } 306 | 307 | int os_task_mm_json_get(char **ppjson) 308 | { 309 | #if 0 310 | char *pjson = NULL; 311 | RETURN_VAL_IF_FAIL(ppjson,-1); 312 | 313 | os_task_t *node = NULL,*tmp = NULL; 314 | 315 | char cmd[1024] = {0,}; 316 | FILE *fp = NULL; 317 | char buf[1024] = {0,}; 318 | char *pResult = NULL; 319 | unsigned long os_task_pid = 0,os_task_tid = 0; 320 | unsigned long mem_size = 0; 321 | const char *os_task_name = NULL; 322 | pid_t pid = getpid(); 323 | memset(cmd,0,sizeof(cmd)); 324 | snprintf(cmd,sizeof(cmd),"ls /proc/%d/task/ | xargs",pid); 325 | fp = popen (cmd, "r"); 326 | assert(fp); 327 | memset(buf,0,sizeof(buf)); 328 | pResult = fgets (buf, sizeof (buf), fp); 329 | assert(pResult); 330 | fclose(fp); 331 | fp = NULL; 332 | ////////////////////// 333 | 334 | char *sub = NULL,*str = NULL; 335 | str = buf; 336 | JSON_Value *jValRoot = json_value_init_array(); 337 | assert(jValRoot); 338 | JSON_Array *jArrRoot = json_array(jValRoot); 339 | assert(jArrRoot); 340 | do { 341 | sub = strtok(str," "); 342 | if(NULL == sub) 343 | break; 344 | sscanf(str,"%lu",&os_task_pid); 345 | //printf("pid=%d, os_task_pid: %d\n",pid,os_task_pid); 346 | str += (strlen(sub)+1); 347 | 348 | /////////////////////////////// 349 | mem_size = 0; 350 | os_task_name = NULL; 351 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 352 | if(os_task_pid == node->info.pid) 353 | { 354 | os_task_name = node->info.name; 355 | os_task_tid = node->info.tid; 356 | /////////////////////////////// 357 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 358 | list_for_each_entry_safe(mnode, tmnode,&node->head, list) { 359 | mem_size += mnode->size; 360 | } 361 | } 362 | } 363 | ///////////////////////////// 364 | JSON_Value *jVal = NULL; 365 | JSON_Object *jObj = NULL; 366 | jVal = json_value_init_object(); 367 | assert(jVal); 368 | jObj = json_value_get_object(jVal); 369 | assert(jObj); 370 | json_object_dotset_string(jObj, "name", os_task_name); 371 | json_object_dotset_number(jObj, "pid", os_task_pid); 372 | json_object_dotset_number(jObj, "tid", os_task_tid); 373 | json_object_dotset_number(jObj, "size", mem_size); 374 | json_array_append_value(jArrRoot,jVal); 375 | } while(1); 376 | pjson = json_serialize_to_string(jValRoot); 377 | json_value_free(jValRoot); 378 | /////////////////////////////////////////// 379 | *ppjson = pjson; 380 | return 0; 381 | #else 382 | return -1; 383 | #endif 384 | 385 | } 386 | 387 | 388 | 389 | static os_task_t _task; 390 | static int __os_task_init(void) 391 | { 392 | //printf("enter __os_task_init()\n"); 393 | static char _name[32] = {0,}; 394 | memset(&_task,0,sizeof(_task)); 395 | prctl(PR_GET_NAME,_name); 396 | _task.info.name = _name; 397 | _task.info.stack_size = 0; 398 | _task.info.priority = 0; 399 | _task.info.func = NULL; 400 | _task.info.arg = NULL; 401 | 402 | _task.info.tid = (unsigned long)pthread_self(); 403 | _task.info.pid = (unsigned long)gettid(); 404 | 405 | INIT_LIST_HEAD(&_task.head); 406 | pthread_mutex_init(&_task.mutex, NULL); 407 | pthread_mutex_lock(&os_task_mutex); 408 | list_add_tail(&_task.list, &os_task_list); 409 | pthread_mutex_unlock(&os_task_mutex); 410 | return 0; 411 | } 412 | 413 | static void __os_task_exit(void) 414 | { 415 | pthread_mutex_destroy(&_task.mutex); 416 | } 417 | 418 | pure_init(__os_task_init); 419 | module_exit(__os_task_exit); 420 | 421 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_tcp_client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | #include "os_tcp_client.h" 13 | 14 | #include "multitask.h" 15 | 16 | struct os_tcp_client_s { 17 | const char *name; 18 | int fd; 19 | }; 20 | 21 | os_tcp_client_t *os_tcp_client_create(const char *name,const char *ip,unsigned short port) 22 | { 23 | os_tcp_client_t *c = NULL; 24 | int sockfd; 25 | struct sockaddr_in servaddr; 26 | int ret = -1; 27 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 28 | RETURN_VAL_IF_FAIL(sockfd > 0, NULL); 29 | memset(&servaddr, 0, sizeof(servaddr)); 30 | servaddr.sin_family = AF_INET; 31 | servaddr.sin_port = htons(port); 32 | if( inet_pton(AF_INET, ip, &servaddr.sin_addr) <= 0){ 33 | MLOGE("inet_pton Fail..."); 34 | close(sockfd); 35 | return NULL; 36 | } 37 | ret = connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)); 38 | RETURN_VAL_IF_FAIL2(0 == ret, close(sockfd); , NULL); 39 | c = MALLOC(sizeof(*c)); 40 | RETURN_VAL_IF_FAIL2(c, close(sockfd); , NULL); 41 | c->fd = sockfd; 42 | c->name = name; 43 | return c; 44 | } 45 | 46 | void os_tcp_client_destroy(os_tcp_client_t *c) 47 | { 48 | if(c) 49 | { 50 | close(c->fd); 51 | FREE(c); 52 | c = NULL; 53 | } 54 | } 55 | 56 | int os_tcp_client_write(os_tcp_client_t *c,void *data,unsigned int size) 57 | { 58 | return os_write(c->fd, data, size); 59 | } 60 | 61 | int os_tcp_client_read(os_tcp_client_t *c,void *data,unsigned int size) 62 | { 63 | return os_read(c->fd, data, size); 64 | } 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_tcp_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #include "multitask.h" 16 | #include "os_tcp_server.h" 17 | #include "os_threadpool.h" 18 | 19 | struct os_tcp_server_s { 20 | int listen_fd; 21 | int threads; 22 | unsigned int listen_fds; 23 | int port; 24 | os_task_t *task; 25 | os_threadpool_t *pool; 26 | sem_t sem; 27 | os_tcp_server_callback_t cb; 28 | void *user_data; 29 | }; 30 | 31 | typedef struct os_tcp_server_node_s { 32 | int conn_fd; 33 | os_tcp_server_callback_t cb; 34 | void *user_data; 35 | }os_tcp_server_node_t; 36 | 37 | 38 | static void thread_pool_tcp_server_routine(void *arg) 39 | { 40 | os_tcp_server_node_t *node = (os_tcp_server_node_t *)arg; 41 | RETURN_IF_FAIL(node); 42 | if(node->cb) 43 | node->cb(node->conn_fd,node->user_data); 44 | FREE(node); 45 | node = NULL; 46 | } 47 | 48 | static void *task_routine_tcp_server(void *arg) 49 | { 50 | MLOGM("task start..."); 51 | int conn_fd; 52 | int ret; 53 | os_threadpool_t *pool; 54 | os_tcp_server_node_t *node = NULL; 55 | os_tcp_server_t *s = (os_tcp_server_t *)arg; 56 | pool = os_threadpool_create(s->threads,TCP_SERVER_QUQUE_MAX,0); 57 | GOTO_LABEL_IF_FAIL(pool, exit); 58 | s->pool = pool; 59 | while(1) 60 | { 61 | conn_fd = accept(s->listen_fd, (struct sockaddr*)NULL, NULL); 62 | CONTINUE_IF_FAIL(conn_fd > 0); 63 | node = MALLOC(sizeof(*node)); 64 | CONTINUE_IF_FAIL(node); 65 | node->conn_fd = conn_fd; 66 | node->cb = s->cb; 67 | node->user_data = s->user_data; 68 | ret = os_threadpool_add(pool,thread_pool_tcp_server_routine,node,0); 69 | CONTINUE_IF_FAIL(0 == ret); 70 | } 71 | 72 | exit: 73 | os_threadpool_destroy(s->pool,0); 74 | sem_post(&s->sem); 75 | return NULL; 76 | } 77 | 78 | 79 | os_tcp_server_t *os_tcp_server_create(int threads,unsigned int listen_fds,int port,os_tcp_server_callback_t cb,void *user_data) 80 | { 81 | os_tcp_server_t *s = NULL; 82 | int listen_fd; 83 | struct sockaddr_in serv_addr; 84 | int ret = -1; 85 | os_task_t *task = NULL; 86 | 87 | RETURN_VAL_IF_FAIL(threads > 0 && listen_fds > 0 && port > 0, NULL); 88 | listen_fd = socket(AF_INET, SOCK_STREAM, 0); 89 | RETURN_VAL_IF_FAIL(listen_fd > 0, NULL); 90 | memset(&serv_addr, 0, sizeof(serv_addr)); 91 | serv_addr.sin_family = AF_INET; 92 | serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 93 | serv_addr.sin_port = htons(port); 94 | ret = bind(listen_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 95 | RETURN_VAL_IF_FAIL(ret >= 0, NULL); 96 | ret = listen(listen_fd, threads); 97 | RETURN_VAL_IF_FAIL(ret >= 0, NULL); 98 | s = MALLOC(sizeof(*s)); 99 | RETURN_VAL_IF_FAIL(s, NULL); 100 | sem_init(&s->sem, 0, 0); 101 | task = os_task_create(TASK_TCP_SERVER,0,0, task_routine_tcp_server, (void *)s); 102 | RETURN_VAL_IF_FAIL2(task, FREE(task);,NULL); 103 | s->listen_fd = listen_fd; 104 | s->threads = threads; 105 | s->listen_fds = listen_fds; 106 | s->port = port; 107 | s->task = task; 108 | s->cb = cb; 109 | s->user_data = user_data; 110 | return s; 111 | } 112 | 113 | void os_tcp_server_destroy(os_tcp_server_t *s) 114 | { 115 | if(s) 116 | { 117 | sem_wait(&s->sem); 118 | sem_destroy(&s->sem); 119 | close(s->listen_fd); 120 | FREE(s); 121 | s = NULL; 122 | } 123 | } 124 | 125 | 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | #include "os_log.h" 9 | 10 | #include "os_thread.h" 11 | 12 | 13 | int os_pthread_create(os_pthread_t *tid, const os_pthread_attr_t *attr, void *(*func) (void *), void *arg) 14 | { 15 | pthread_t id = 0; 16 | int ret = -1; 17 | ret = pthread_create(&id,NULL,func,arg); 18 | RETURN_VAL_IF_FAIL(0 == ret, -1); 19 | *tid = (os_pthread_t)id; 20 | return 0; 21 | } 22 | 23 | int os_pthread_join (os_pthread_t tid, void ** status) 24 | { 25 | return pthread_join((pthread_t)tid,status); 26 | } 27 | 28 | os_pthread_t os_pthread_self (void) 29 | { 30 | return (os_pthread_t)pthread_self(); 31 | } 32 | 33 | int os_pthread_detach (os_pthread_t tid) 34 | { 35 | return pthread_detach((pthread_t)tid); 36 | } 37 | 38 | void os_pthread_exit (void *status) 39 | { 40 | pthread_exit(status); 41 | } 42 | 43 | /////////////////////////////////////////////////////////////////// 44 | int os_pthread_mutex_init(os_pthread_mutex_t *mutex,const os_pthread_mutexattr_t *attr) 45 | { 46 | int ret = -1; 47 | pthread_mutex_t *p = malloc(sizeof(pthread_mutex_t)); 48 | RETURN_VAL_IF_FAIL(p, -1); 49 | ret = pthread_mutex_init(p,NULL); 50 | GOTO_LABEL_IF_FAIL(0 == ret, fail); 51 | *mutex = (os_pthread_mutex_t)p; 52 | return ret; 53 | fail: 54 | if(p) free(p); 55 | return -1; 56 | } 57 | int os_pthread_mutex_destroy(os_pthread_mutex_t *mutex){ 58 | return pthread_mutex_destroy((pthread_mutex_t *)(*mutex)); 59 | } 60 | int os_pthread_mutex_lock(os_pthread_mutex_t *mutex){ 61 | pthread_mutex_t *p = (pthread_mutex_t *)(*mutex); 62 | return pthread_mutex_lock(p); 63 | } 64 | int os_pthread_mutex_trylock(os_pthread_mutex_t *mutex){ 65 | pthread_mutex_t *p = (pthread_mutex_t *)(*mutex); 66 | return pthread_mutex_trylock(p); 67 | } 68 | int os_pthread_mutex_unlock(os_pthread_mutex_t *mutex){ 69 | pthread_mutex_t *p = (pthread_mutex_t *)(*mutex); 70 | return pthread_mutex_unlock(p); 71 | } 72 | 73 | /////////////////////////////////////////////////////////////////// 74 | int os_pthread_cond_init(os_pthread_cond_t * cond, 75 | const os_pthread_condattr_t * attr){ 76 | int ret = -1; 77 | pthread_cond_t *p = malloc(sizeof(pthread_cond_t)); 78 | RETURN_VAL_IF_FAIL(p, -1); 79 | ret = pthread_cond_init(p,NULL); 80 | GOTO_LABEL_IF_FAIL(0 == ret, fail); 81 | *cond = (os_pthread_cond_t)p; 82 | return ret; 83 | fail: 84 | if(p) free(p); 85 | return -1; 86 | } 87 | 88 | int os_pthread_cond_destroy(os_pthread_cond_t *cond) { 89 | return pthread_cond_destroy((pthread_cond_t *)(*cond)); 90 | } 91 | int os_pthread_cond_timedwait(os_pthread_cond_t * cond, 92 | os_pthread_mutex_t * mutex, 93 | const os_timespec_t * abstime) { 94 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 95 | pthread_mutex_t *m = (pthread_mutex_t *)(*mutex); 96 | struct timespec time; 97 | memset(&time,0,sizeof(time)); 98 | //TODO: timer 99 | return pthread_cond_timedwait(c,m,&time); 100 | } 101 | int os_pthread_cond_wait(os_pthread_cond_t * cond, 102 | os_pthread_mutex_t *mutex) { 103 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 104 | pthread_mutex_t *m = (pthread_mutex_t *)(*mutex); 105 | return pthread_cond_wait(c,m); 106 | } 107 | int os_pthread_cond_broadcast(os_pthread_cond_t *cond) { 108 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 109 | return pthread_cond_broadcast(c); 110 | } 111 | int os_pthread_cond_signal(os_pthread_cond_t *cond) { 112 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 113 | return pthread_cond_signal(c); 114 | } 115 | 116 | 117 | /////////////////////////////////////////////////////////////////// 118 | int os_sem_init(os_sem_t *sem, int pshared, unsigned int value){ 119 | int ret = -1; 120 | sem_t *p = malloc(sizeof(sem_t)); 121 | RETURN_VAL_IF_FAIL(p, -1); 122 | ret = sem_init(p,pshared,value); 123 | GOTO_LABEL_IF_FAIL(0 == ret, fail); 124 | *sem = (os_sem_t)p; 125 | return ret; 126 | fail: 127 | if(p) free(p); 128 | return -1; 129 | } 130 | 131 | int os_sem_wait(os_sem_t *sem){ 132 | sem_t *s = (sem_t *)(*sem); 133 | return sem_wait(s); 134 | } 135 | 136 | int os_sem_trywait(os_sem_t *sem){ 137 | sem_t *s = (sem_t *)(*sem); 138 | return sem_trywait(s); 139 | } 140 | 141 | int os_sem_post(os_sem_t * sem){ 142 | sem_t *s = (sem_t *)(*sem); 143 | return sem_post(s); 144 | } 145 | 146 | int os_sem_destroy(os_sem_t * sem){ 147 | sem_t *s = (sem_t *)(*sem); 148 | return sem_destroy(s); 149 | } 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_threadpool.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "os_async_queue.h" 10 | #include "os_threadpool.h" 11 | #include "os_log.h" 12 | #include "os_misc.h" 13 | 14 | 15 | typedef struct { 16 | void (*function)(void *); 17 | void *argument; 18 | } os_threadpool_task_t; 19 | 20 | struct os_threadpool_s { 21 | int thread_count; 22 | int queue_size; 23 | int shutdown; 24 | int started; 25 | pthread_t *threads; 26 | os_async_queue_t *queue; 27 | }; 28 | 29 | static void *os_threadpool_thread(void *threadpool) 30 | { 31 | os_threadpool_t *pool = (os_threadpool_t *)threadpool; 32 | RETURN_VAL_IF_FAIL(pool, NULL); 33 | os_threadpool_task_t *task = NULL; 34 | thread_name_set("threadpool"); 35 | while(1) 36 | { 37 | task = (os_threadpool_task_t *)os_async_queue_pop(pool->queue); 38 | if(task && task->function) 39 | { 40 | task->function(task->argument); 41 | free(task); 42 | task = NULL; 43 | } 44 | } 45 | 46 | return NULL; 47 | } 48 | 49 | os_threadpool_t *os_threadpool_create(int thread_count, int queue_size, int flags) 50 | { 51 | os_threadpool_t *pool = NULL; 52 | int i; 53 | RETURN_VAL_IF_FAIL(thread_count > 0 && thread_count < MAX_THREADS 54 | && queue_size > 0 && queue_size < MAX_QUEUE, NULL); 55 | pool = (os_threadpool_t *)malloc(sizeof(os_threadpool_t)); 56 | RETURN_VAL_IF_FAIL(pool, NULL); 57 | /* Initialize */ 58 | pool->thread_count = 0; 59 | pool->queue_size = queue_size; 60 | pool->shutdown = pool->started = 0; 61 | /* Allocate thread and task queue */ 62 | pool->queue = os_async_queue_new(); 63 | GOTO_LABEL_IF_FAIL(pool->queue, label1); 64 | pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count); 65 | GOTO_LABEL_IF_FAIL(pool->threads, label2); 66 | /* Start worker threads */ 67 | for(i = 0; i < thread_count; i++) { 68 | if(pthread_create(&(pool->threads[i]), NULL, 69 | os_threadpool_thread, (void*)pool) != 0) { 70 | os_threadpool_destroy(pool, 0); 71 | return NULL; 72 | } 73 | pool->thread_count++; 74 | pool->started++; 75 | } 76 | return pool; 77 | 78 | label2: 79 | if(pool && pool->queue) os_async_queue_free(pool->queue); 80 | pool->queue = NULL; 81 | 82 | label1: 83 | if(pool) free(pool); 84 | pool = NULL; 85 | 86 | return NULL; 87 | } 88 | 89 | void os_threadpool_destroy(os_threadpool_t *pool, int flags) 90 | { 91 | int i; 92 | if(pool) 93 | { 94 | /* Join all worker thread */ 95 | for(i = 0; i < pool->thread_count; i++) { 96 | if(pthread_join(pool->threads[i], NULL) != 0) { 97 | ASSERT(0); 98 | } 99 | } 100 | if(pool && pool->threads) free(pool->threads); 101 | pool->threads = NULL; 102 | if(pool && pool->queue) os_async_queue_free(pool->queue); 103 | pool->queue = NULL; 104 | } 105 | } 106 | 107 | int os_threadpool_add(os_threadpool_t *pool, void (*routine)(void *),void *arg, int flags) 108 | { 109 | os_threadpool_task_t *task = NULL; 110 | RETURN_VAL_IF_FAIL(pool, -1); 111 | RETURN_VAL_IF_FAIL(pool->queue_size < MAX_QUEUE, -1); 112 | task = malloc(sizeof(*task)); 113 | RETURN_VAL_IF_FAIL(task, -1); 114 | task->function = routine; 115 | task->argument = arg; 116 | os_async_queue_push(pool->queue, task); 117 | return 0; 118 | } 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /platform/os/linux/base/os_timer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | #include "os_timer.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | 13 | long long get_monotonic_time (void) 14 | { 15 | struct timespec ts; 16 | clock_gettime (CLOCK_MONOTONIC, &ts); 17 | return (((long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); 18 | } 19 | 20 | 21 | void os_timer_start(os_timer_t *timer) 22 | { 23 | if(timer) 24 | { 25 | bzero(timer,sizeof(*timer)); 26 | timer->start = get_monotonic_time(); 27 | } 28 | } 29 | 30 | unsigned int os_timer_stop(os_timer_t *timer) 31 | { 32 | unsigned int time = 0; 33 | long long elapsed = 0; 34 | if(timer) 35 | { 36 | timer->end = get_monotonic_time(); 37 | elapsed = timer->end - timer->start; 38 | time = elapsed % 1000000; 39 | } 40 | return time; 41 | } 42 | 43 | 44 | 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | 51 | -------------------------------------------------------------------------------- /platform/os/linux/ubus/Makefile: -------------------------------------------------------------------------------- 1 | 2 | current_dir := ${CURDIR}/platform/os/linux/ubus 3 | 4 | configure_jsonc_args := --enable-static --enable-shared --host=${HOST_NAME} --prefix=${CURDIR}/target 5 | configure_jsonc_args += CFLAGS="${CFLAGS} -Drpl_realloc=realloc -Drpl_malloc=malloc" 6 | 7 | configure_ubox_args := -DBUILD_LUA=OFF -DBUILD_EXAMPLES=OFF -DCMAKE_INSTALL_PREFIX=${CURDIR}/target 8 | configure_ubox_args += -DPKG_CONFIG_USE_CMAKE_PREFIX_PATH=${CURDIR}/target 9 | configure_ubox_args += -DCMAKE_C_FLAGS="${CFLAGS} -DJSONC -I ${CURDIR}/target/include/json-c" 10 | 11 | configure_ubus_args := -DBUILD_LUA=OFF -DBUILD_EXAMPLES=OFF -DBUILD_STATIC=ON -DCMAKE_INSTALL_PREFIX=${CURDIR}/target 12 | configure_ubus_args += -DPKG_CONFIG_USE_CMAKE_PREFIX_PATH=${CURDIR}/target -DCMAKE_EXE_LINKER_FLAGS="${LD_LIBS}" 13 | 14 | lib_name_jsonc := json-c 15 | lib_name_ubox := libubox 16 | lib_name_ubus := ubus 17 | 18 | lib_name_suffix :=.tar.gz 19 | 20 | sub_target: 21 | $(call auto_make_install_compressed_lib_once,${current_dir},${lib_name_jsonc},$(lib_name_suffix),$(configure_jsonc_args)) 22 | $(call auto_cmake_install_compressed_lib_once,${current_dir},${lib_name_ubox},$(lib_name_suffix),$(configure_ubox_args)) 23 | $(call auto_cmake_install_compressed_lib_once,${current_dir},${lib_name_ubus},$(lib_name_suffix),$(configure_ubus_args)) 24 | 25 | -------------------------------------------------------------------------------- /platform/os/linux/ubus/json-c.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/linux/ubus/json-c.tar.gz -------------------------------------------------------------------------------- /platform/os/linux/ubus/libubox.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/linux/ubus/libubox.tar.gz -------------------------------------------------------------------------------- /platform/os/linux/ubus/ubus.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/linux/ubus/ubus.tar.gz -------------------------------------------------------------------------------- /platform/os/linux_glib/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | 4 | obj-y += glib/ 5 | obj-y += base/ 6 | 7 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | obj-y += os_async_queue.o 4 | obj-y += os_broadcast.o 5 | obj-y += os_evdriver.o 6 | obj-y += os_log.o 7 | obj-y += os_misc.o 8 | obj-y += os_mm.o 9 | obj-y += os_msg.o 10 | obj-y += os_multicast.o 11 | obj-y += os_queue.o 12 | obj-y += os_stdlibc.o 13 | obj-y += os_task.o 14 | obj-y += os_thread.o 15 | obj-y += os_threadpool.o 16 | obj-y += os_tcp_server.o 17 | obj-y += os_tcp_client.o 18 | 19 | 20 | CFLAGS += -I ${CURDIR}/target/include/glib-2.0 21 | CFLAGS += -I ${CURDIR}/target/lib/glib-2.0/include 22 | 23 | 24 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_async_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "multitask.h" 9 | #include "glib.h" 10 | 11 | 12 | os_async_queue_t *os_async_queue_new(void) 13 | { 14 | os_async_queue_t *q = (os_async_queue_t *)g_async_queue_new(); 15 | return q; 16 | } 17 | 18 | void os_async_queue_free(os_async_queue_t *q) 19 | { 20 | if(q) 21 | { 22 | g_async_queue_unref((GAsyncQueue *)q); 23 | } 24 | } 25 | 26 | unsigned int os_async_queue_length(os_async_queue_t *q) 27 | { 28 | return g_async_queue_length((GAsyncQueue *)q); 29 | } 30 | 31 | void os_async_queue_push(os_async_queue_t *q,void *data) 32 | { 33 | g_async_queue_push((GAsyncQueue *)q,data); 34 | } 35 | 36 | void *os_async_queue_pop(os_async_queue_t *q) 37 | { 38 | void *data = NULL; 39 | data = g_async_queue_pop((GAsyncQueue *)q); 40 | return data; 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_broadcast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "os_broadcast.h" 18 | 19 | 20 | struct os_broadcast_s { 21 | /*< private >*/ 22 | int socket_fd; 23 | struct sockaddr_in addr; 24 | }; 25 | 26 | 27 | #ifndef DEBUG 28 | #define DEBUG(args...) printf(args) 29 | #endif 30 | 31 | #ifndef ERROR 32 | #define ERROR(args...) printf(args) 33 | #endif 34 | 35 | os_broadcast_t *os_broadcast_create(os_broadcast_type_e type,unsigned short port) 36 | { 37 | int ret = -1; 38 | const int opt=-1; 39 | os_broadcast_t *b = NULL; 40 | b = malloc(sizeof(os_broadcast_t)); 41 | assert(b); 42 | b->socket_fd = socket(AF_INET,SOCK_DGRAM,0); 43 | if(b->socket_fd < 0) 44 | { 45 | ERROR("socket fail...\n"); 46 | goto fail; 47 | } 48 | ret = setsockopt(b->socket_fd,SOL_SOCKET,SO_BROADCAST,(char*)&opt,sizeof(opt));//设置套接字类型 49 | if(ret < 0) 50 | { 51 | ERROR("setsockopt fail...\n"); 52 | goto set_fail; 53 | } 54 | bzero(&b->addr,sizeof(struct sockaddr_in)); 55 | b->addr.sin_family = AF_INET; 56 | b->addr.sin_addr.s_addr = (BROADCAST_TYPE_CLIENT == type)? htonl(INADDR_ANY) : htonl(INADDR_BROADCAST); 57 | b->addr.sin_port = htons(port); 58 | 59 | if(BROADCAST_TYPE_CLIENT == type) 60 | { 61 | if(-1 == bind(b->socket_fd,(struct sockaddr*)&(b->addr),sizeof(b->addr))) 62 | { 63 | ERROR("bind error...\n"); 64 | goto bind_fail; 65 | } 66 | } 67 | return b; 68 | 69 | bind_fail: 70 | set_fail: 71 | close(b->socket_fd); 72 | fail: 73 | free(b); 74 | return NULL; 75 | } 76 | 77 | void os_broadcast_destroy(os_broadcast_t *b) 78 | { 79 | if(b) 80 | { 81 | if(b->socket_fd > 0) close(b->socket_fd); 82 | free(b); 83 | } 84 | } 85 | 86 | int os_broadcast_send(os_broadcast_t *b,unsigned char *data,unsigned int size) 87 | { 88 | assert(b); 89 | socklen_t len = sizeof(b->addr); 90 | return sendto(b->socket_fd,data,size,0,(struct sockaddr*)&b->addr,len);//向广播地址发布消息 91 | } 92 | 93 | 94 | int os_broadcast_recv(os_broadcast_t *b,unsigned char *data,unsigned int size) 95 | { 96 | assert(b); 97 | socklen_t len = sizeof(b->addr); 98 | return recvfrom(b->socket_fd,data,size,0,(struct sockaddr*)&b->addr,&len); 99 | } 100 | 101 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_bus.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_evdriver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include /* O_CLOEXEC */ 11 | #include /* memset() */ 12 | #include 13 | #include 14 | #include /* for select() workaround */ 15 | #include /* struct signalfd_siginfo */ 16 | #include 17 | #include 18 | 19 | #include "multitask.h" 20 | #include 21 | #include 22 | 23 | 24 | typedef struct glib_evdriver_timer_s { 25 | os_evdriver_node_t *evnode; 26 | gboolean loop; 27 | os_evdriver_callback_t cb; 28 | void *userdata; 29 | }glib_evdriver_timer_t; 30 | 31 | typedef struct glib_evdriver_signal_s { 32 | os_evdriver_node_t *evnode; 33 | os_evdriver_callback_t cb; 34 | void *userdata; 35 | }glib_evdriver_signal_t; 36 | 37 | typedef struct glib_evdriver_io_s { 38 | os_evdriver_node_t *evnode; 39 | os_evdriver_callback_t cb; 40 | void *userdata; 41 | }glib_evdriver_io_t; 42 | 43 | 44 | static gboolean 45 | __evdriver_timeout_callback (gpointer data) 46 | { 47 | gboolean loop = G_SOURCE_REMOVE; 48 | glib_evdriver_timer_t *timer = (glib_evdriver_timer_t *)data; 49 | loop = timer->loop; 50 | if(timer->cb) timer->cb(timer->evnode,timer->userdata); 51 | FREE(timer); 52 | timer = NULL; 53 | 54 | return loop; 55 | } 56 | 57 | static gboolean 58 | __evdriver_on_sig_callback (gpointer data) 59 | { 60 | glib_evdriver_signal_t *signal = (glib_evdriver_signal_t *)data; 61 | if(signal->cb) signal->cb(signal->evnode,signal->userdata); 62 | FREE(signal); 63 | signal = NULL; 64 | return G_SOURCE_REMOVE; 65 | } 66 | 67 | static gboolean 68 | __evdriver_on_io_callback (gpointer data) 69 | { 70 | glib_evdriver_io_t *io = (glib_evdriver_io_t *)data; 71 | if(io->cb) io->cb(io->evnode,io->userdata); 72 | FREE(io); 73 | io = NULL; 74 | return G_SOURCE_CONTINUE; 75 | } 76 | 77 | 78 | 79 | os_evdriver_t *os_evdriver_create2(const char *name) 80 | { 81 | os_evdriver_t *evdriver = NULL; 82 | evdriver = (os_evdriver_t *)g_main_new(TRUE); 83 | return evdriver; 84 | } 85 | 86 | os_evdriver_t *os_evdriver_create(void) 87 | { 88 | return os_evdriver_create2("evdriver"); 89 | } 90 | 91 | 92 | void os_evdriver_destroy(os_evdriver_t *evdriver) 93 | { 94 | g_main_loop_unref((GMainLoop *)evdriver); 95 | } 96 | 97 | int os_evdriver_run2(os_evdriver_t *evdriver,int flags) 98 | { 99 | g_main_loop_run((GMainLoop *)evdriver); 100 | return 0; 101 | } 102 | 103 | int os_evdriver_run(os_evdriver_t *evdriver) 104 | { 105 | return os_evdriver_run2(evdriver,0); 106 | } 107 | 108 | os_evdriver_node_t *os_evdriver_add(os_evdriver_t *evdriver,os_event_t *ev,os_evdriver_callback_t cb,void *user_data) 109 | { 110 | GMainContext* context = NULL; 111 | GSource *source = NULL; 112 | RETURN_VAL_IF_FAIL(evdriver && ev && cb, NULL); 113 | RETURN_VAL_IF_FAIL(ev->event < OS_EVDRIVER_BUTT, NULL); 114 | 115 | context = g_main_loop_get_context((GMainLoop *)evdriver); 116 | RETURN_VAL_IF_FAIL(context, NULL); 117 | 118 | switch (ev->event) 119 | { 120 | case OS_EVDRIVER_IO: 121 | { 122 | glib_evdriver_timer_t *io = NULL; 123 | io = MALLOC(sizeof(*io)); 124 | RETURN_VAL_IF_FAIL(io, NULL); 125 | io->evnode = (os_evdriver_node_t *)context; 126 | io->cb = cb; 127 | io->userdata = user_data; 128 | source = g_unix_fd_source_new (ev->io.fd, G_IO_IN); 129 | g_source_set_priority (source, G_PRIORITY_DEFAULT); 130 | g_source_set_callback (source, (GSourceFunc) __evdriver_on_io_callback,io , NULL); 131 | g_source_attach (source, NULL); 132 | g_source_unref (source); 133 | }break; 134 | case OS_EVDRIVER_TIMER: 135 | { 136 | glib_evdriver_timer_t *timer = NULL; 137 | unsigned long timeout = 0; 138 | timer = MALLOC(sizeof(*timer)); 139 | RETURN_VAL_IF_FAIL(timer, NULL); 140 | timer->evnode = (os_evdriver_node_t *)context; 141 | timer->loop = G_SOURCE_REMOVE; 142 | timer->cb = cb; 143 | timer->userdata = user_data; 144 | if(ev->timer.interval > 0) 145 | { 146 | timeout = ev->timer.interval; 147 | timer->loop = G_SOURCE_CONTINUE; 148 | } 149 | else 150 | { 151 | timeout = ev->timer.when; 152 | } 153 | source = g_timeout_source_new(timeout); 154 | g_source_set_callback (source, (GSourceFunc)__evdriver_timeout_callback, timer, NULL); 155 | g_source_set_priority (source, G_PRIORITY_DEFAULT); 156 | g_source_attach (source, context); 157 | g_source_unref (source); 158 | }break; 159 | case OS_EVDRIVER_SIGNAL: 160 | { 161 | glib_evdriver_signal_t *signal = NULL; 162 | signal = MALLOC(sizeof(*signal)); 163 | RETURN_VAL_IF_FAIL(signal, NULL); 164 | signal->evnode = (os_evdriver_node_t *)context; 165 | signal->cb = cb; 166 | signal->userdata = user_data; 167 | source = g_unix_signal_source_new (ev->signal.signo); 168 | g_source_set_priority (source, G_PRIORITY_DEFAULT); 169 | g_source_set_callback (source, (GSourceFunc)__evdriver_on_sig_callback, user_data, NULL); 170 | g_source_attach (source, context); 171 | g_source_unref (source); 172 | }break; 173 | default: 174 | { 175 | GOTO_LABEL_IF_FAIL(0,fail); 176 | }break; 177 | } 178 | 179 | return (os_evdriver_node_t *)context; 180 | 181 | fail: 182 | 183 | return NULL; 184 | } 185 | 186 | 187 | void os_evdriver_del(os_evdriver_node_t *evnode) 188 | { 189 | 190 | } 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_log.c: -------------------------------------------------------------------------------- 1 | /* Includes ------------------------------------------------------------------*/ 2 | #include 3 | #include 4 | 5 | #include "os_log.h" 6 | #include "os_misc.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* Private typedef -----------------------------------------------------------*/ 13 | /* Private macro -------------------------------------------------------------*/ 14 | /* Private variables ---------------------------------------------------------*/ 15 | static os_log_callback_t g_os_log_callback = NULL; 16 | /* Private function prototypes -----------------------------------------------*/ 17 | /* Private functions ---------------------------------------------------------*/ 18 | 19 | const char *os_log_enum2str(MMP_LOG_ENUM level) 20 | { 21 | const char *ret = "[U]"; 22 | switch(level) 23 | { 24 | case MLOG_VERBOSE: ret = "[V]";break; 25 | case MLOG_DEBUG: ret = "[D]";break; 26 | case MLOG_INFO: ret = "[I]";break; 27 | case MLOG_MESSAGE: ret = "[M]";break; 28 | case MLOG_WARN: ret = "[W]";break; 29 | case MLOG_ERROR: ret = "[E]";break; 30 | case MLOG_FATAL: ret = "[F]";break; 31 | default: break; 32 | } 33 | return ret; 34 | } 35 | 36 | 37 | const char *os_log_colour_get(MMP_LOG_ENUM level) 38 | { 39 | const char *ret = COLOUR_NONE; 40 | switch(level) 41 | { 42 | case MLOG_VERBOSE: ret = COLOUR_NONE;break; 43 | case MLOG_DEBUG: ret = COLOUR_WHITE;break; 44 | case MLOG_INFO: ret = COLOUR_WHITE;break; 45 | case MLOG_MESSAGE: ret = COLOUR_L_GREEN;break; 46 | case MLOG_WARN: ret = COLOUR_YELLOW;break; 47 | case MLOG_ERROR: ret = COLOUR_L_RED;break; 48 | case MLOG_FATAL: ret = COLOUR_RED;break; 49 | default: break; 50 | } 51 | return ret; 52 | } 53 | 54 | void os_log_message(int level 55 | ,const char *file_name 56 | ,int line 57 | ,const char *func_name 58 | ,const char *fmt, ...) 59 | { 60 | int ret = 0; 61 | /* 头-实际内容 */ 62 | /* 日志头 */ 63 | char task_name[32] = {0}; 64 | char time_buf[64] = {0}; 65 | char log_buffer[MLOG_BUFF_SIZE] = {0}; 66 | thread_name_get(task_name); 67 | time2str(time_buf,sizeof(time_buf)); 68 | 69 | ret = snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"%s%s ",os_log_colour_get(level),time_buf); 70 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"(%s,%d)",file_name,line); 71 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"[%s]",task_name); 72 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"%s",os_log_enum2str(level)); 73 | /* 组包 */ 74 | va_list args; 75 | va_start(args, fmt); 76 | vsnprintf(log_buffer+ret,sizeof(log_buffer)-ret,fmt,args); 77 | va_end(args); 78 | /* 输出 */ 79 | if(g_os_log_callback) 80 | g_os_log_callback(level,log_buffer); 81 | else 82 | printf("%s\n""\e[0m",log_buffer); 83 | } 84 | 85 | 86 | void os_log_assert(const char *source 87 | ,const char *expr 88 | ,const char *file_name 89 | ,int line 90 | ,const char *fmt, ...) 91 | { 92 | int ret = 0; 93 | /* 头-实际内容 */ 94 | /* 日志头 */ 95 | char task_name[32] = {0}; 96 | char time_buf[64] = {0}; 97 | char log_buffer[MLOG_BUFF_SIZE] = {0}; 98 | const char *colour = COLOUR_L_RED; 99 | thread_name_get(task_name); 100 | time2str(time_buf,sizeof(time_buf)); 101 | 102 | ret = snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"%s%s ",colour?colour:"",time_buf); 103 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"(%s,%d)",file_name,line); 104 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"[%s]",task_name); 105 | ret += snprintf(log_buffer+ret,sizeof(log_buffer)-ret,"assertion failed: # %s #",expr?expr:""); 106 | /* 组包 */ 107 | va_list args; 108 | va_start(args, fmt); 109 | vsnprintf(log_buffer+ret,sizeof(log_buffer)-ret,fmt,args); 110 | va_end(args); 111 | /* 输出 */ 112 | if(g_os_log_callback) 113 | g_os_log_callback(MLOG_FATAL,log_buffer); 114 | else 115 | printf("%s\n""\e[0m",log_buffer); 116 | } 117 | 118 | 119 | #ifdef __cplusplus 120 | } 121 | #endif 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_misc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "os_log.h" 14 | #include "multitask.h" 15 | 16 | extern pid_t gettid(void); 17 | 18 | void time2str(char *buf,int size) 19 | { 20 | assert(buf && size > 0); 21 | struct timeval tv; 22 | struct tm tm; 23 | int pos = 0; 24 | long millisecond = 0; 25 | bzero(buf,size); 26 | gettimeofday(&tv,NULL); 27 | localtime_r(&tv.tv_sec,&tm); 28 | //格式化化为字符串输出. 29 | strftime(buf,size,"%F %T",&tm); 30 | pos = strlen(buf); 31 | millisecond = tv.tv_usec / 1000; 32 | snprintf(buf+pos,size-pos,".%lu",millisecond); 33 | } 34 | 35 | void thread_name_get(char *task_name) 36 | { 37 | prctl(PR_GET_NAME,task_name); 38 | } 39 | 40 | void thread_name_set(const char *task_name) 41 | { 42 | prctl(PR_SET_NAME,task_name); 43 | } 44 | 45 | 46 | int child_pid_serialize2string(char *strs,unsigned int size) 47 | { 48 | #define CHILD_PID_CMD "ls /proc/%d/task/ | xargs" 49 | char cmd[1024] = {0,}; 50 | FILE *fp = NULL; 51 | char *pResult = NULL; 52 | RETURN_VAL_IF_FAIL(strs && size, -1); 53 | pid_t pid = getpid(); 54 | memset(cmd,0,sizeof(cmd)); 55 | snprintf(cmd,sizeof(cmd),CHILD_PID_CMD,pid); 56 | //printf("cmd: %s\n",cmd); 57 | fp = popen (cmd, "r"); 58 | assert(fp); 59 | memset(strs,0,size); 60 | pResult = fgets (strs, size, fp); 61 | assert(pResult); 62 | fclose(fp); 63 | fp = NULL; 64 | //printf("strs: %s\n",strs); 65 | return 0; 66 | } 67 | 68 | 69 | int child_pid_get(unsigned long **pp_arr,unsigned int *p_size) 70 | { 71 | RETURN_VAL_IF_FAIL(pp_arr && p_size, -1); 72 | char buf[1024] = {0}; 73 | int ret = -1; 74 | int i; 75 | char *sub = NULL,*str = NULL; 76 | unsigned int cnt = 0; 77 | unsigned long *p_arr = NULL; 78 | ret = child_pid_serialize2string(buf,sizeof(buf)); 79 | RETURN_VAL_IF_FAIL(0 == ret, -1); 80 | 81 | for(i = 0;i < sizeof(buf) && 0 != buf[i];i++) 82 | { 83 | if(' ' == buf[i]) 84 | cnt++; 85 | } 86 | cnt += 1; 87 | p_arr = MALLOC(cnt*sizeof(unsigned long)); 88 | RETURN_VAL_IF_FAIL(p_arr, -1); 89 | 90 | i = 0; 91 | str = buf; 92 | do { 93 | sub = strtok(str," "); 94 | if(NULL == sub) 95 | break; 96 | sscanf(str,"%lu",&p_arr[i]); 97 | str += (strlen(sub)+1); 98 | i++; 99 | } while(1); 100 | 101 | *p_size = cnt; 102 | *pp_arr = p_arr; 103 | return 0; 104 | } 105 | 106 | 107 | void child_pid_free(unsigned long *p_arr) 108 | { 109 | if(p_arr) FREE(p_arr); 110 | } 111 | 112 | 113 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_mm.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE /* See feature_test_macros(7) */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "multitask.h" 14 | 15 | #if defined(CONFIG_OS_MM_TRACE) 16 | pthread_mutex_t os_mm_mutex = PTHREAD_MUTEX_INITIALIZER; 17 | static LIST_HEAD(os_mm_list); 18 | #endif 19 | 20 | extern pid_t gettid(void); 21 | 22 | void *os_mm_malloc(const char *func,unsigned long line,unsigned long size) 23 | { 24 | void *addr = NULL; 25 | addr = malloc(size); 26 | assert(addr); 27 | unsigned long pid = (unsigned long)gettid(); //process ID 28 | unsigned long tid = (unsigned long)pthread_self(); //thread ID 29 | 30 | #if defined(CONFIG_OS_TASK_MM_TRACE) 31 | os_task_mm_node_t *node = NULL; 32 | node = malloc(sizeof(*node)); 33 | assert(node); 34 | node->tid = tid; 35 | node->pid = pid; 36 | node->func = func; 37 | node->line = line; 38 | node->size = size; 39 | node->addr = addr; 40 | prctl(PR_GET_NAME,node->os_task_name); 41 | os_task_mm_add(node->tid,node); 42 | #endif 43 | 44 | #if defined(CONFIG_OS_MM_TRACE) 45 | os_task_mm_node_t *mm_node = NULL; 46 | mm_node = malloc(sizeof(*mm_node)); 47 | assert(mm_node); 48 | mm_node->tid = tid; 49 | mm_node->pid = pid; 50 | mm_node->func = func; 51 | mm_node->line = line; 52 | mm_node->size = size; 53 | mm_node->addr = addr; 54 | prctl(PR_GET_NAME,node->os_task_name); 55 | pthread_mutex_lock(&os_mm_mutex); 56 | list_add_tail(&mm_node->list, &os_mm_list); 57 | pthread_mutex_unlock(&os_mm_mutex); 58 | #endif 59 | 60 | return addr; 61 | } 62 | 63 | 64 | void os_mm_free(void *addr) 65 | { 66 | if(addr) 67 | { 68 | #if defined(CONFIG_TASK_MM_TRACE) 69 | unsigned long tid; //thread ID 70 | tid = (unsigned long)pthread_self(); 71 | os_task_mm_del(tid,addr); 72 | #endif 73 | 74 | #if defined(CONFIG_MM_TRACE) 75 | os_task_mm_node_t *mm_node = NULL,*mm_tmp = NULL; 76 | pthread_mutex_lock(&os_mm_mutex); 77 | list_for_each_entry_safe(mm_node, mm_tmp,&os_mm_list, list) { 78 | if(mm_node->addr == addr) 79 | { 80 | list_del(&mm_node->list); 81 | free(mm_node); 82 | mm_node = NULL; 83 | break; 84 | } 85 | } 86 | pthread_mutex_unlock(&os_mm_mutex); 87 | #endif 88 | 89 | free(addr); 90 | addr = NULL; 91 | } 92 | } 93 | 94 | 95 | void os_mm_show(void) 96 | { 97 | os_task_mm_node_t *node = NULL,*tmp = NULL; 98 | printf("\n\n=========================================== mm_show ===========================================\n"); 99 | printf("%-15s %-15s %-15s %-32s %-15s %-15s %-15s\n", 100 | "[task]","[tid]","[pid]","[function]","[line]","[addr]","[size]"); 101 | list_for_each_entry_safe(node, tmp,&os_mm_list, list) { 102 | printf("%-15s %-15lu %-15lu %-32s %-15lu %-15p %-15lu\n", 103 | node->os_task_name,node->tid,node->pid,node->func,node->line,node->addr,node->size); 104 | } 105 | } 106 | 107 | 108 | void os_mm_show2(void (*show)(const char *)) 109 | { 110 | os_task_mm_node_t *node = NULL,*tmp = NULL; 111 | char buf[1024] = {0,}; 112 | printf("\n\n=========================================== mm_show ===========================================\n"); 113 | printf("%-15s %-15s %-15s %-32s %-15s %-15s %-15s\n", 114 | "[task]","[tid]","[pid]","[function]","[line]","[addr]","[size]"); 115 | list_for_each_entry_safe(node, tmp,&os_mm_list, list) { 116 | memset(buf,0,sizeof(buf)); 117 | snprintf(buf,sizeof(buf),"%-15s %-15lu %-15lu %-32s %-15lu %-15p %-15lu\n", 118 | node->os_task_name,node->tid,node->pid,node->func,node->line,node->addr,node->size); 119 | printf("%s",buf); 120 | if(show) show((const char *)buf); 121 | } 122 | } 123 | 124 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_msg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "os_msg.h" 10 | #include "os_log.h" 11 | #include "os_async_queue.h" 12 | #include "multitask.h" 13 | 14 | 15 | 16 | int os_msg_send(os_msg_t *msg) 17 | { 18 | RETURN_VAL_IF_FAIL(msg, -1); 19 | os_async_queue_t *q = NULL; 20 | q = (os_async_queue_t *)os_task_aq_get(msg->dst); 21 | RETURN_VAL_IF_FAIL(q, -1); 22 | os_async_queue_push(q, msg); 23 | return 0; 24 | } 25 | 26 | 27 | os_msg_t *os_msg_recv(void) 28 | { 29 | os_msg_t *msg = NULL; 30 | os_async_queue_t *q; 31 | q = os_task_aq_self(); 32 | RETURN_VAL_IF_FAIL(q, NULL); 33 | msg = os_async_queue_pop(q); 34 | RETURN_VAL_IF_FAIL(msg, NULL); 35 | return msg; 36 | } 37 | 38 | 39 | int os_msg_send2(const char *dst,int priority,void *data,unsigned int size) 40 | { 41 | os_msg_t *msg = NULL; 42 | RETURN_VAL_IF_FAIL(dst , -1); 43 | msg = MALLOC(sizeof(*msg)+size); 44 | RETURN_VAL_IF_FAIL(msg, -1); 45 | msg->src = os_task_name_get_from_tid((unsigned long)pthread_self()); 46 | msg->dst = dst; 47 | msg->priority = priority; 48 | msg->size = size; 49 | memcpy(msg->data,data,size); 50 | ///////////////////////////// 51 | os_async_queue_t *q = NULL; 52 | q = (os_async_queue_t *)os_task_aq_get(dst); 53 | GOTO_LABEL_IF_FAIL(q, fail); 54 | os_async_queue_push(q, msg); 55 | return 0; 56 | 57 | fail: 58 | FREE(msg); 59 | msg = NULL; 60 | return -1; 61 | } 62 | 63 | 64 | void *os_msg_recv2(unsigned int *p_size) 65 | { 66 | os_msg_t *msg = NULL; 67 | void *data = NULL; 68 | os_async_queue_t *q; 69 | q = os_task_aq_self(); 70 | RETURN_VAL_IF_FAIL(q, NULL); 71 | msg = os_async_queue_pop(q); 72 | RETURN_VAL_IF_FAIL(msg, NULL); 73 | data = MALLOC(msg->size); 74 | RETURN_VAL_IF_FAIL2(data,{FREE(msg);msg=NULL;},NULL); 75 | if(p_size) *p_size = msg->size; 76 | memcpy(data,msg->data,msg->size); 77 | FREE(msg); 78 | msg = NULL; 79 | return data; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_multicast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "os_multicast.h" 18 | 19 | 20 | 21 | struct os_multicast_s { 22 | /*< private >*/ 23 | int socket_fd; 24 | struct sockaddr_in addr; 25 | }; 26 | 27 | 28 | #ifndef DEBUG 29 | #define DEBUG(args...) printf(args) 30 | #endif 31 | 32 | #ifndef ERROR 33 | #define ERROR(args...) printf(args) 34 | #endif 35 | 36 | os_multicast_t *os_multicast_create(os_multicast_type_e type,const char *addr,unsigned short port) 37 | { 38 | os_multicast_t *m = NULL; 39 | m = malloc(sizeof(os_multicast_t)); 40 | int err; 41 | assert(m); 42 | m->socket_fd = socket(AF_INET,SOCK_DGRAM,0); 43 | if(m->socket_fd < 0) 44 | { 45 | printf("socket create fail.\n"); 46 | return NULL; 47 | } 48 | memset(&m->addr,0,sizeof(m->addr)); 49 | m->addr.sin_family = AF_INET; 50 | m->addr.sin_addr.s_addr = (MULTICAST_TYPE_CLIENT == type)?htonl(INADDR_ANY) :inet_addr(addr); 51 | m->addr.sin_port = htons(port); 52 | 53 | if(MULTICAST_TYPE_CLIENT == type) 54 | { 55 | /*绑定socket*/ 56 | err = bind(m->socket_fd,(struct sockaddr*)&m->addr,sizeof(m->addr)); 57 | if(err<0) 58 | { 59 | printf("bind fail.\n"); 60 | return NULL; 61 | } 62 | /*设置回环许可*/ 63 | int loop=1; 64 | err=setsockopt(m->socket_fd,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop)); 65 | if(err<0) 66 | { 67 | printf("set sock error\n"); 68 | return NULL; 69 | } 70 | struct ip_mreq mreq;/*加入广播组*/ 71 | mreq.imr_multiaddr.s_addr=inet_addr(addr);//广播地址 72 | mreq.imr_interface.s_addr=htonl(INADDR_ANY); //网络接口为默认 73 | /*将本机加入广播组*/ 74 | err=setsockopt(m->socket_fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)); 75 | if(err<0) 76 | { 77 | printf("set sock error\n"); 78 | return NULL; 79 | } 80 | } 81 | return m; 82 | 83 | } 84 | 85 | void os_multicast_destroy(os_multicast_t *m) 86 | { 87 | if(m) 88 | { 89 | if(m->socket_fd > 0) close(m->socket_fd); 90 | free(m); 91 | } 92 | } 93 | 94 | int os_multicast_send(os_multicast_t *m,unsigned char *data,unsigned int size) 95 | { 96 | assert(m); 97 | //向局部多播地址发送多播内容 98 | return sendto(m->socket_fd,data,size,0,(struct sockaddr*)&m->addr,sizeof(m->addr)); 99 | } 100 | 101 | int os_multicast_recv(os_multicast_t *m,unsigned char *data,unsigned int size) 102 | { 103 | assert(m); 104 | socklen_t len = sizeof(m->addr); 105 | return recvfrom(m->socket_fd,data,size,0,(struct sockaddr*)&m->addr,&len); 106 | } 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "os_queue.h" 8 | #include "os_log.h" 9 | #include "multitask.h" 10 | 11 | struct os_queue_s { 12 | int dummy; 13 | }; 14 | 15 | 16 | os_queue_t *os_queue_new(void) 17 | { 18 | os_queue_t *q = NULL; 19 | return q; 20 | } 21 | 22 | void os_queue_free(os_queue_t *q) 23 | { 24 | } 25 | 26 | unsigned int os_queue_length(os_queue_t *q) 27 | { 28 | return 0; 29 | } 30 | 31 | unsigned int os_queue_empty(os_queue_t *q) 32 | { 33 | return 0; 34 | } 35 | 36 | 37 | void os_queue_push_tail(os_queue_t *q,void *data) 38 | { 39 | } 40 | 41 | void os_queue_push_head(os_queue_t *q,void *data) 42 | { 43 | } 44 | 45 | void *os_queue_pop_tail(os_queue_t *q) 46 | { 47 | void *data = NULL; 48 | 49 | return data; 50 | } 51 | 52 | void *os_queue_pop_head(os_queue_t *q) 53 | { 54 | void *data = NULL; 55 | 56 | return data; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_stdlibc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include "os_stdlibc.h" 9 | 10 | 11 | void *os_malloc(os_size_t size) 12 | { 13 | return malloc((size_t)size); 14 | } 15 | 16 | void os_free(void *ptr) 17 | { 18 | free(ptr); 19 | } 20 | 21 | void *os_calloc(os_size_t nmemb, os_size_t size) 22 | { 23 | return calloc((size_t)nmemb,(size_t)size); 24 | } 25 | 26 | void *os_realloc(void *ptr, os_size_t size) 27 | { 28 | return realloc(ptr,(size_t)size); 29 | } 30 | 31 | int os_system(const char *command) 32 | { 33 | return system(command); 34 | } 35 | 36 | unsigned int os_sleep(unsigned int seconds) 37 | { 38 | return sleep(seconds); 39 | } 40 | 41 | 42 | void *os_memset(void *s, int c, os_size_t n) 43 | { 44 | return memset(s, c, (size_t)n); 45 | } 46 | 47 | 48 | char *os_strcpy(char *dest, const char *src) 49 | { 50 | return strcpy(dest,src); 51 | } 52 | 53 | char *os_strncpy(char *dest, const char *src, os_size_t n) 54 | { 55 | return strncpy(dest,src,n); 56 | } 57 | 58 | int os_scanf(const char *format, ...) 59 | { 60 | int ret = -1; 61 | va_list args; 62 | va_start(args, format); 63 | ret = scanf(format,args); 64 | va_end(args); 65 | return ret; 66 | } 67 | 68 | int os_sscanf(const char *str, const char *format, ...) 69 | { 70 | int ret = -1; 71 | va_list args; 72 | va_start(args, format); 73 | ret = sscanf(str,format,args); 74 | va_end(args); 75 | return ret; 76 | } 77 | 78 | os_size_t os_strlen(const char *s) 79 | { 80 | return strlen(s); 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_task.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "multitask.h" 14 | 15 | #include 16 | #include 17 | 18 | __attribute__((weak)) pid_t gettid(void) 19 | { 20 | return syscall(SYS_gettid); 21 | } 22 | 23 | 24 | typedef struct os_task_info_s { 25 | const char *name; 26 | unsigned long stack_size; 27 | int priority; 28 | os_task_func_t func; 29 | void *arg; 30 | /* other */ 31 | unsigned long tid; 32 | unsigned long pid; 33 | }os_task_info_t; 34 | 35 | struct os_task_s { 36 | struct list_head list; 37 | /////////////////////// 38 | struct list_head head; 39 | pthread_mutex_t mutex; 40 | os_task_info_t info; 41 | os_async_queue_t *q; 42 | }; 43 | 44 | 45 | pthread_mutex_t os_task_mutex = PTHREAD_MUTEX_INITIALIZER; 46 | static LIST_HEAD(os_task_list); 47 | 48 | 49 | static void os_task_exit(os_task_t *task) 50 | { 51 | os_task_t *node = NULL,*tmp = NULL; 52 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 53 | 54 | if(task) 55 | { 56 | MLOGW("os_task_exit: %s(%lu : %lu)\n",task->info.name,task->info.pid,task->info.tid); 57 | if(task->q) os_async_queue_free(task->q); 58 | task->q = NULL; 59 | pthread_mutex_lock(&os_task_mutex); 60 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 61 | if(node == task) 62 | { 63 | list_del(&node->list); 64 | break; 65 | } 66 | } 67 | pthread_mutex_unlock(&os_task_mutex); 68 | printf("%-15s %-15s %-15s %-32s %-15s %-15s %-15s\n", 69 | "[task]","[tid]","[pid]","[function]","[line]","[addr]","[size]"); 70 | list_for_each_entry_safe(mnode, tmnode,&task->head, list) { 71 | printf("%-15s %-15lu %-15lu %-32s %-15lu %-15p %-15lu\n",mnode->os_task_name, 72 | mnode->tid,mnode->pid,mnode->func,mnode->line,mnode->addr,mnode->size); 73 | list_del(&mnode->list); 74 | free(mnode); 75 | } 76 | pthread_mutex_destroy(&task->mutex); 77 | free(task); 78 | task = NULL; 79 | } 80 | } 81 | 82 | static void *__os_task_routine(void *arg) 83 | { 84 | pthread_detach(pthread_self()); 85 | os_task_t *task = (os_task_t *)arg; 86 | RETURN_VAL_IF_FAIL(task,NULL); 87 | task->info.tid = (unsigned long)pthread_self(); 88 | task->info.pid = (unsigned long)gettid(); 89 | prctl(PR_SET_NAME,task->info.name); 90 | if(task->info.func) 91 | task->info.func(task->info.arg); 92 | os_task_exit(task); 93 | return NULL; 94 | } 95 | 96 | os_task_t *os_task_create(const char *name,unsigned long stack_size,int priority,os_task_func_t func,void *arg) 97 | { 98 | os_task_t *task = NULL; 99 | task = malloc(sizeof(*task)); 100 | RETURN_VAL_IF_FAIL(task,NULL); 101 | memset(task,0,sizeof(*task)); 102 | task->info.name = name; 103 | task->info.stack_size = stack_size; 104 | task->info.priority = priority; 105 | task->info.func = func; 106 | task->info.arg = arg; 107 | task->q = os_async_queue_new(); 108 | 109 | INIT_LIST_HEAD(&task->head); 110 | pthread_mutex_init(&task->mutex, NULL); 111 | pthread_mutex_lock(&os_task_mutex); 112 | list_add_tail(&task->list, &os_task_list); 113 | pthread_mutex_unlock(&os_task_mutex); 114 | 115 | if(NULL == g_thread_new (name, __os_task_routine, (void *)task)) 116 | { 117 | os_async_queue_free(task->q); 118 | task->q = NULL; 119 | list_del_init(&task->list); 120 | pthread_mutex_destroy(&task->mutex); 121 | free(task); 122 | task=NULL; 123 | return NULL; 124 | } 125 | return task; 126 | } 127 | 128 | os_task_t *os_task_create2(const char *name,unsigned long stack_size,int priority,int async,os_task_func_t func,void *arg) 129 | { 130 | os_task_t *task = NULL; 131 | task = malloc(sizeof(*task)); 132 | assert(task); 133 | memset(task,0,sizeof(*task)); 134 | task->info.name = name; 135 | task->info.stack_size = stack_size; 136 | task->info.priority = priority; 137 | task->info.func = func; 138 | task->info.arg = arg; 139 | if(async > 0) task->q = os_async_queue_new(); 140 | 141 | INIT_LIST_HEAD(&task->head); 142 | pthread_mutex_init(&task->mutex, NULL); 143 | pthread_mutex_lock(&os_task_mutex); 144 | list_add_tail(&task->list, &os_task_list); 145 | pthread_mutex_unlock(&os_task_mutex); 146 | 147 | if(NULL == g_thread_new (name, __os_task_routine, (void *)task)) 148 | { 149 | os_async_queue_free(task->q); 150 | task->q = NULL; 151 | list_del_init(&task->list); 152 | pthread_mutex_destroy(&task->mutex); 153 | free(task); 154 | task=NULL; 155 | return NULL; 156 | } 157 | return task; 158 | } 159 | 160 | 161 | os_async_queue_t *os_task_aq_get(const char *name) 162 | { 163 | os_async_queue_t *qq = NULL; 164 | os_task_t *node = NULL,*tmp = NULL; 165 | pthread_mutex_lock(&os_task_mutex); 166 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 167 | if(0 == strcmp(name,node->info.name)) 168 | { 169 | qq = node->q; 170 | break; 171 | } 172 | } 173 | pthread_mutex_unlock(&os_task_mutex); 174 | return (os_async_queue_t *)qq; 175 | } 176 | 177 | const char *os_task_name_get_from_pid(unsigned long pid) 178 | { 179 | os_task_t *node = NULL,*tmp = NULL; 180 | const char *name = NULL; 181 | pthread_mutex_lock(&os_task_mutex); 182 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 183 | if(pid == node->info.pid) 184 | { 185 | name = node->info.name; 186 | break; 187 | } 188 | } 189 | pthread_mutex_unlock(&os_task_mutex); 190 | return name; 191 | } 192 | 193 | const char *os_task_name_get_from_tid(unsigned long tid) 194 | { 195 | os_task_t *node = NULL,*tmp = NULL; 196 | const char *name = NULL; 197 | pthread_mutex_lock(&os_task_mutex); 198 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 199 | if(tid == node->info.tid) 200 | { 201 | name = node->info.name; 202 | break; 203 | } 204 | } 205 | pthread_mutex_unlock(&os_task_mutex); 206 | return name; 207 | } 208 | 209 | 210 | os_async_queue_t *os_task_aq_self(void) 211 | { 212 | os_async_queue_t *qq = NULL; 213 | os_task_t *node = NULL,*tmp = NULL; 214 | unsigned long tid = (unsigned long)pthread_self(); 215 | pthread_mutex_lock(&os_task_mutex); 216 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 217 | if(tid == node->info.tid) 218 | { 219 | qq = node->q; 220 | break; 221 | } 222 | } 223 | pthread_mutex_unlock(&os_task_mutex); 224 | return qq; 225 | } 226 | 227 | 228 | 229 | void os_task_mm_add(unsigned long tid,os_task_mm_node_t *mnode) 230 | { 231 | os_task_t *node = NULL,*tmp = NULL; 232 | pthread_mutex_lock(&os_task_mutex); 233 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 234 | if(tid == node->info.tid) 235 | { 236 | list_add_tail(&mnode->list, &node->head); 237 | break; 238 | } 239 | } 240 | pthread_mutex_unlock(&os_task_mutex); 241 | } 242 | 243 | 244 | void os_task_mm_del(unsigned long tid,void *addr) 245 | { 246 | os_task_t *node = NULL,*tmp = NULL; 247 | pthread_mutex_lock(&os_task_mutex); 248 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 249 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 250 | list_for_each_entry_safe(mnode, tmnode,&node->head, list) { 251 | if(addr == mnode->addr) 252 | { 253 | list_del(&mnode->list); 254 | free(mnode); 255 | goto succ; 256 | } 257 | } 258 | } 259 | succ: 260 | pthread_mutex_unlock(&os_task_mutex); 261 | } 262 | 263 | 264 | void os_task_mm_show(void) 265 | { 266 | os_task_t *node = NULL,*tmp = NULL; 267 | char time[64] = {0,}; 268 | unsigned long os_task_pid = 0,os_task_tid = 0; 269 | unsigned long mem_size = 0; 270 | const char *os_task_name = NULL; 271 | unsigned long *p_arr = NULL; 272 | unsigned int arr_size = 0; 273 | int ret = 0; 274 | int i; 275 | ////////////////////////////// 276 | time2str(time,sizeof(time)); 277 | printf("\n\n=========================================== os_task_mm_show [%s] ===========================================\n",time); 278 | printf("%-20s %-20s %-20s %-20s\n","[name]","[pid]","[tid]","[size]"); 279 | 280 | ret = child_pid_get(&p_arr,&arr_size); 281 | RETURN_IF_FAIL(0 == ret); 282 | for(i = 0;i < arr_size;i++) 283 | { 284 | os_task_pid = p_arr[i]; 285 | /////////////////////////////// 286 | mem_size = 0; 287 | os_task_name = NULL; 288 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 289 | if(os_task_pid == node->info.pid) 290 | { 291 | os_task_name = node->info.name; 292 | os_task_tid = node->info.tid; 293 | /////////////////////////////// 294 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 295 | list_for_each_entry_safe(mnode, tmnode,&node->head, list) { 296 | mem_size += mnode->size; 297 | } 298 | } 299 | } 300 | printf("%-20s %-20lu %-20lu %-20lu\n",os_task_name,os_task_pid,os_task_tid,mem_size); 301 | } 302 | child_pid_free(p_arr); 303 | } 304 | 305 | int os_task_mm_json_get(char **ppjson) 306 | { 307 | #if 0 308 | char *pjson = NULL; 309 | RETURN_VAL_IF_FAIL(ppjson,-1); 310 | 311 | os_task_t *node = NULL,*tmp = NULL; 312 | 313 | char cmd[1024] = {0,}; 314 | FILE *fp = NULL; 315 | char buf[1024] = {0,}; 316 | char *pResult = NULL; 317 | unsigned long os_task_pid = 0,os_task_tid = 0; 318 | unsigned long mem_size = 0; 319 | const char *os_task_name = NULL; 320 | pid_t pid = getpid(); 321 | memset(cmd,0,sizeof(cmd)); 322 | snprintf(cmd,sizeof(cmd),"ls /proc/%d/task/ | xargs",pid); 323 | fp = popen (cmd, "r"); 324 | assert(fp); 325 | memset(buf,0,sizeof(buf)); 326 | pResult = fgets (buf, sizeof (buf), fp); 327 | assert(pResult); 328 | fclose(fp); 329 | fp = NULL; 330 | ////////////////////// 331 | 332 | char *sub = NULL,*str = NULL; 333 | str = buf; 334 | JSON_Value *jValRoot = json_value_init_array(); 335 | assert(jValRoot); 336 | JSON_Array *jArrRoot = json_array(jValRoot); 337 | assert(jArrRoot); 338 | do { 339 | sub = strtok(str," "); 340 | if(NULL == sub) 341 | break; 342 | sscanf(str,"%lu",&os_task_pid); 343 | //printf("pid=%d, os_task_pid: %d\n",pid,os_task_pid); 344 | str += (strlen(sub)+1); 345 | 346 | /////////////////////////////// 347 | mem_size = 0; 348 | os_task_name = NULL; 349 | list_for_each_entry_safe(node, tmp,&os_task_list, list) { 350 | if(os_task_pid == node->info.pid) 351 | { 352 | os_task_name = node->info.name; 353 | os_task_tid = node->info.tid; 354 | /////////////////////////////// 355 | os_task_mm_node_t *mnode = NULL,*tmnode = NULL; 356 | list_for_each_entry_safe(mnode, tmnode,&node->head, list) { 357 | mem_size += mnode->size; 358 | } 359 | } 360 | } 361 | ///////////////////////////// 362 | JSON_Value *jVal = NULL; 363 | JSON_Object *jObj = NULL; 364 | jVal = json_value_init_object(); 365 | assert(jVal); 366 | jObj = json_value_get_object(jVal); 367 | assert(jObj); 368 | json_object_dotset_string(jObj, "name", os_task_name); 369 | json_object_dotset_number(jObj, "pid", os_task_pid); 370 | json_object_dotset_number(jObj, "tid", os_task_tid); 371 | json_object_dotset_number(jObj, "size", mem_size); 372 | json_array_append_value(jArrRoot,jVal); 373 | } while(1); 374 | pjson = json_serialize_to_string(jValRoot); 375 | json_value_free(jValRoot); 376 | /////////////////////////////////////////// 377 | *ppjson = pjson; 378 | return 0; 379 | #else 380 | return -1; 381 | #endif 382 | } 383 | 384 | 385 | 386 | static os_task_t _task; 387 | static int __os_task_init(void) 388 | { 389 | //printf("enter __os_task_init()\n"); 390 | static char _name[32] = {0,}; 391 | memset(&_task,0,sizeof(_task)); 392 | prctl(PR_GET_NAME,_name); 393 | _task.info.name = _name; 394 | _task.info.stack_size = 0; 395 | _task.info.priority = 0; 396 | _task.info.func = NULL; 397 | _task.info.arg = NULL; 398 | 399 | _task.info.tid = (unsigned long)pthread_self(); 400 | _task.info.pid = (unsigned long)gettid(); 401 | 402 | INIT_LIST_HEAD(&_task.head); 403 | pthread_mutex_init(&_task.mutex, NULL); 404 | pthread_mutex_lock(&os_task_mutex); 405 | list_add_tail(&_task.list, &os_task_list); 406 | pthread_mutex_unlock(&os_task_mutex); 407 | return 0; 408 | } 409 | 410 | static void __os_task_exit(void) 411 | { 412 | pthread_mutex_destroy(&_task.mutex); 413 | } 414 | 415 | pure_init(__os_task_init); 416 | module_exit(__os_task_exit); 417 | 418 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_tcp_client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include "os_tcp_client.h" 9 | 10 | 11 | struct os_tcp_client_s { 12 | const char *name; 13 | int fd; 14 | }; 15 | 16 | os_tcp_client_t *os_tcp_client_create(const char *name,const char *ip,unsigned short port) 17 | { 18 | return NULL; 19 | } 20 | 21 | void os_tcp_client_destroy(os_tcp_client_t *c) 22 | { 23 | 24 | } 25 | 26 | int os_tcp_client_write(os_tcp_client_t *c,void *data,unsigned int size) 27 | { 28 | return 0; 29 | } 30 | 31 | int os_tcp_client_read(os_tcp_client_t *c,void *data,unsigned int size) 32 | { 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_tcp_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "os_tcp_server.h" 8 | 9 | 10 | struct os_tcp_server_s { 11 | int listen_fd; 12 | }; 13 | 14 | 15 | os_tcp_server_t *os_tcp_server_create(int threads,unsigned int listen_fds,int port,os_tcp_server_callback_t cb,void *user_data) 16 | { 17 | return NULL; 18 | } 19 | 20 | void os_tcp_server_destroy(os_tcp_server_t *s) 21 | { 22 | 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | #include "os_log.h" 9 | 10 | #include "os_thread.h" 11 | 12 | 13 | int os_pthread_create(os_pthread_t *tid, const os_pthread_attr_t *attr, void *(*func) (void *), void *arg) 14 | { 15 | pthread_t id = 0; 16 | int ret = -1; 17 | ret = pthread_create(&id,NULL,func,arg); 18 | RETURN_VAL_IF_FAIL(0 == ret, -1); 19 | *tid = (os_pthread_t)id; 20 | return 0; 21 | } 22 | 23 | int os_pthread_join (os_pthread_t tid, void ** status) 24 | { 25 | return pthread_join((pthread_t)tid,status); 26 | } 27 | 28 | os_pthread_t os_pthread_self (void) 29 | { 30 | return (os_pthread_t)pthread_self(); 31 | } 32 | 33 | int os_pthread_detach (os_pthread_t tid) 34 | { 35 | return pthread_detach((pthread_t)tid); 36 | } 37 | 38 | void os_pthread_exit (void *status) 39 | { 40 | pthread_exit(status); 41 | } 42 | 43 | /////////////////////////////////////////////////////////////////// 44 | int os_pthread_mutex_init(os_pthread_mutex_t *mutex,const os_pthread_mutexattr_t *attr) 45 | { 46 | int ret = -1; 47 | pthread_mutex_t *p = malloc(sizeof(pthread_mutex_t)); 48 | RETURN_VAL_IF_FAIL(p, -1); 49 | ret = pthread_mutex_init(p,NULL); 50 | GOTO_LABEL_IF_FAIL(0 == ret, fail); 51 | *mutex = (os_pthread_mutex_t)p; 52 | return ret; 53 | fail: 54 | if(p) free(p); 55 | return -1; 56 | } 57 | int os_pthread_mutex_destroy(os_pthread_mutex_t *mutex){ 58 | return pthread_mutex_destroy((pthread_mutex_t *)(*mutex)); 59 | } 60 | int os_pthread_mutex_lock(os_pthread_mutex_t *mutex){ 61 | pthread_mutex_t *p = (pthread_mutex_t *)(*mutex); 62 | return pthread_mutex_lock(p); 63 | } 64 | int os_pthread_mutex_trylock(os_pthread_mutex_t *mutex){ 65 | pthread_mutex_t *p = (pthread_mutex_t *)(*mutex); 66 | return pthread_mutex_trylock(p); 67 | } 68 | int os_pthread_mutex_unlock(os_pthread_mutex_t *mutex){ 69 | pthread_mutex_t *p = (pthread_mutex_t *)(*mutex); 70 | return pthread_mutex_unlock(p); 71 | } 72 | 73 | /////////////////////////////////////////////////////////////////// 74 | int os_pthread_cond_init(os_pthread_cond_t * cond, 75 | const os_pthread_condattr_t * attr){ 76 | int ret = -1; 77 | pthread_cond_t *p = malloc(sizeof(pthread_cond_t)); 78 | RETURN_VAL_IF_FAIL(p, -1); 79 | ret = pthread_cond_init(p,NULL); 80 | GOTO_LABEL_IF_FAIL(0 == ret, fail); 81 | *cond = (os_pthread_cond_t)p; 82 | return ret; 83 | fail: 84 | if(p) free(p); 85 | return -1; 86 | } 87 | 88 | int os_pthread_cond_destroy(os_pthread_cond_t *cond) { 89 | return pthread_cond_destroy((pthread_cond_t *)(*cond)); 90 | } 91 | int os_pthread_cond_timedwait(os_pthread_cond_t * cond, 92 | os_pthread_mutex_t * mutex, 93 | const os_timespec_t * abstime) { 94 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 95 | pthread_mutex_t *m = (pthread_mutex_t *)(*mutex); 96 | struct timespec time; 97 | memset(&time,0,sizeof(time)); 98 | //TODO: timer 99 | return pthread_cond_timedwait(c,m,&time); 100 | } 101 | int os_pthread_cond_wait(os_pthread_cond_t * cond, 102 | os_pthread_mutex_t *mutex) { 103 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 104 | pthread_mutex_t *m = (pthread_mutex_t *)(*mutex); 105 | return pthread_cond_wait(c,m); 106 | } 107 | int os_pthread_cond_broadcast(os_pthread_cond_t *cond) { 108 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 109 | return pthread_cond_broadcast(c); 110 | } 111 | int os_pthread_cond_signal(os_pthread_cond_t *cond) { 112 | pthread_cond_t *c = (pthread_cond_t *)(*cond); 113 | return pthread_cond_signal(c); 114 | } 115 | 116 | 117 | /////////////////////////////////////////////////////////////////// 118 | int os_sem_init(os_sem_t *sem, int pshared, unsigned int value){ 119 | int ret = -1; 120 | sem_t *p = malloc(sizeof(sem_t)); 121 | RETURN_VAL_IF_FAIL(p, -1); 122 | ret = sem_init(p,pshared,value); 123 | GOTO_LABEL_IF_FAIL(0 == ret, fail); 124 | *sem = (os_sem_t)p; 125 | return ret; 126 | fail: 127 | if(p) free(p); 128 | return -1; 129 | } 130 | 131 | int os_sem_wait(os_sem_t *sem){ 132 | sem_t *s = (sem_t *)(*sem); 133 | return sem_wait(s); 134 | } 135 | 136 | int os_sem_trywait(os_sem_t *sem){ 137 | sem_t *s = (sem_t *)(*sem); 138 | return sem_trywait(s); 139 | } 140 | 141 | int os_sem_post(os_sem_t * sem){ 142 | sem_t *s = (sem_t *)(*sem); 143 | return sem_post(s); 144 | } 145 | 146 | int os_sem_destroy(os_sem_t * sem){ 147 | sem_t *s = (sem_t *)(*sem); 148 | return sem_destroy(s); 149 | } 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /platform/os/linux_glib/base/os_threadpool.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "os_async_queue.h" 10 | #include "os_threadpool.h" 11 | #include "os_log.h" 12 | #include "os_misc.h" 13 | 14 | 15 | typedef struct { 16 | void (*function)(void *); 17 | void *argument; 18 | } os_threadpool_task_t; 19 | 20 | struct os_threadpool_s { 21 | int thread_count; 22 | int queue_size; 23 | int shutdown; 24 | int started; 25 | pthread_t *threads; 26 | os_async_queue_t *queue; 27 | }; 28 | 29 | static void *os_threadpool_thread(void *threadpool) 30 | { 31 | os_threadpool_t *pool = (os_threadpool_t *)threadpool; 32 | RETURN_VAL_IF_FAIL(pool, NULL); 33 | os_threadpool_task_t *task = NULL; 34 | thread_name_set("threadpool"); 35 | while(1) 36 | { 37 | task = (os_threadpool_task_t *)os_async_queue_pop(pool->queue); 38 | if(task && task->function) 39 | { 40 | task->function(task->argument); 41 | free(task); 42 | task = NULL; 43 | } 44 | } 45 | 46 | return NULL; 47 | } 48 | 49 | os_threadpool_t *os_threadpool_create(int thread_count, int queue_size, int flags) 50 | { 51 | os_threadpool_t *pool = NULL; 52 | int i; 53 | RETURN_VAL_IF_FAIL(thread_count > 0 && thread_count < MAX_THREADS 54 | && queue_size > 0 && queue_size < MAX_QUEUE, NULL); 55 | pool = (os_threadpool_t *)malloc(sizeof(os_threadpool_t)); 56 | RETURN_VAL_IF_FAIL(pool, NULL); 57 | /* Initialize */ 58 | pool->thread_count = 0; 59 | pool->queue_size = queue_size; 60 | pool->shutdown = pool->started = 0; 61 | /* Allocate thread and task queue */ 62 | pool->queue = os_async_queue_new(); 63 | GOTO_LABEL_IF_FAIL(pool->queue, label1); 64 | pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count); 65 | GOTO_LABEL_IF_FAIL(pool->threads, label2); 66 | /* Start worker threads */ 67 | for(i = 0; i < thread_count; i++) { 68 | if(pthread_create(&(pool->threads[i]), NULL, 69 | os_threadpool_thread, (void*)pool) != 0) { 70 | os_threadpool_destroy(pool, 0); 71 | return NULL; 72 | } 73 | pool->thread_count++; 74 | pool->started++; 75 | } 76 | 77 | return pool; 78 | 79 | label2: 80 | if(pool && pool->queue) os_async_queue_free(pool->queue); 81 | pool->queue = NULL; 82 | 83 | label1: 84 | if(pool) free(pool); 85 | pool = NULL; 86 | 87 | return NULL; 88 | } 89 | 90 | void os_threadpool_destroy(os_threadpool_t *pool, int flags) 91 | { 92 | int i; 93 | if(pool) 94 | { 95 | /* Join all worker thread */ 96 | for(i = 0; i < pool->thread_count; i++) { 97 | if(pthread_join(pool->threads[i], NULL) != 0) { 98 | ASSERT(0); 99 | } 100 | } 101 | if(pool && pool->threads) free(pool->threads); 102 | pool->threads = NULL; 103 | if(pool && pool->queue) os_async_queue_free(pool->queue); 104 | pool->queue = NULL; 105 | } 106 | } 107 | 108 | int os_threadpool_add(os_threadpool_t *pool, void (*routine)(void *),void *arg, int flags) 109 | { 110 | os_threadpool_task_t *task = NULL; 111 | RETURN_VAL_IF_FAIL(pool, -1); 112 | RETURN_VAL_IF_FAIL(pool->queue_size < MAX_QUEUE, -1); 113 | task = malloc(sizeof(*task)); 114 | RETURN_VAL_IF_FAIL(task, -1); 115 | task->function = routine; 116 | task->argument = arg; 117 | os_async_queue_push(pool->queue, task); 118 | return 0; 119 | } 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /platform/os/linux_glib/glib/Makefile: -------------------------------------------------------------------------------- 1 | 2 | current_dir := ${CURDIR}/platform/os/linux_glib/glib 3 | 4 | configure_zlib_args := --static --enable-shared --prefix=${CURDIR}/target 5 | 6 | configure_libffi_args := --enable-static --host=${HOST_NAME} --prefix=${CURDIR}/target LDFLAGS="" 7 | 8 | configure_glib_args := --enable-silent-rules --enable-static --enable-shared --with-pcre=internal --disable-gtk-doc-html --disable-man 9 | configure_glib_args += glib_cv_stack_grows=no glib_cv_uscore=no ac_cv_func_posix_getpwuid_r=no ac_cv_func_posix_getgrgid_r=no 10 | configure_glib_args += --host=${HOST_NAME} --prefix=${CURDIR}/target 11 | configure_glib_args += CFLAGS="${CFLAGS} -Wno-format-nonliteral -Wno-format-overflow -Wno-format-security -I ${CURDIR}/target/lib/libffi-3.2.1/include" 12 | configure_glib_args += LDFLAGS="-L${CURDIR}/target/lib" 13 | 14 | lib_name_zlib := zlib-1.2.11 15 | lib_name_libffi := libffi-3.2.1 16 | lib_name_glib := glib-2.40.2 17 | lib_name_suffix :=.tar.gz 18 | 19 | sub_target: 20 | $(call auto_make_install_compressed_lib_once,${current_dir},${lib_name_zlib},$(lib_name_suffix),$(configure_zlib_args)) 21 | $(call auto_make_install_compressed_lib_once,${current_dir},${lib_name_libffi},$(lib_name_suffix),$(configure_libffi_args)) 22 | $(call auto_make_install_compressed_lib_once_dirty,${current_dir},${lib_name_glib},$(lib_name_suffix),$(configure_glib_args)) 23 | 24 | 25 | -------------------------------------------------------------------------------- /platform/os/linux_glib/glib/glib-2.40.2.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/linux_glib/glib/glib-2.40.2.tar.gz -------------------------------------------------------------------------------- /platform/os/linux_glib/glib/libffi-3.2.1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/linux_glib/glib/libffi-3.2.1.tar.gz -------------------------------------------------------------------------------- /platform/os/linux_glib/glib/zlib-1.2.11.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/linux_glib/glib/zlib-1.2.11.tar.gz -------------------------------------------------------------------------------- /platform/os/ros/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/platform/os/ros/Makefile -------------------------------------------------------------------------------- /sample/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | obj-y += sample.o 4 | obj-y += task_show.o 5 | obj-y += task_json.o 6 | obj-y += task_broadcast.o 7 | #obj-y += task_multicast.o 8 | obj-y += mm_broadcast.o 9 | 10 | obj-y += tcp_client.o 11 | obj-y += tcp_server.o 12 | obj-y += udp_tcp_client.o 13 | obj-y += udp_tcp_server.o 14 | 15 | 16 | -------------------------------------------------------------------------------- /sample/mm_broadcast.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | 3 | #define TASK_PRODUCER "producer" 4 | #define TASK_CONSUMER1 "consumer1" 5 | #define TASK_CONSUMER2 "consumer2" 6 | 7 | 8 | static void *task_routine_producer(void *arg) 9 | { 10 | os_msg_t *msg; 11 | while (1) 12 | { 13 | msg = MALLOC(sizeof(*msg)+32); 14 | ASSERT(msg); 15 | MLOGM("msg1=%p\n",msg); 16 | os_memset(msg,0,(sizeof(*msg)+32)); 17 | os_strcpy((char *)msg->data,"hello"); 18 | msg->src = TASK_PRODUCER; 19 | msg->dst = TASK_CONSUMER1; 20 | msg->priority = 0; 21 | msg->size = 32; 22 | if(os_msg_send(msg)) 23 | { 24 | FREE(msg); 25 | msg = NULL; 26 | } 27 | os_sleep(1); 28 | 29 | msg = MALLOC(sizeof(*msg)+32); 30 | ASSERT(msg); 31 | MLOGM("msg2=%p\n",msg); 32 | os_memset(msg,0,(sizeof(*msg)+32)); 33 | os_strcpy((char *)msg->data,"world"); 34 | msg->src = TASK_PRODUCER; 35 | msg->dst = TASK_CONSUMER2; 36 | msg->priority = 0; 37 | msg->size = 32; 38 | if(os_msg_send(msg)) 39 | { 40 | FREE(msg); 41 | msg = NULL; 42 | } 43 | os_sleep(3); 44 | } 45 | 46 | return NULL; 47 | } 48 | 49 | static void *task_routine_consumer1(void *arg) 50 | { 51 | const char *str = NULL; 52 | os_msg_t *msg; 53 | int cnt = 0; 54 | while (1) 55 | { 56 | msg = os_msg_recv(); 57 | str = (char *)msg->data; 58 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 59 | FREE(msg); 60 | if(cnt++ > 20) 61 | break; 62 | } 63 | 64 | return NULL; 65 | } 66 | 67 | static void *task_routine_consumer2(void *arg) 68 | { 69 | const char *str = NULL; 70 | os_msg_t *msg; 71 | int cnt = 0; 72 | while (1) 73 | { 74 | msg = os_msg_recv(); 75 | str = (char *)msg->data; 76 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 77 | FREE(msg); 78 | if(cnt++ > 2) 79 | break; 80 | } 81 | 82 | return NULL; 83 | } 84 | 85 | os_broadcast_t *g_b = NULL; 86 | 87 | static void __mm_broadcast(const char *s) 88 | { 89 | //printf("%s",s); 90 | os_broadcast_send(g_b, (unsigned char *)s, os_strlen(s)); 91 | } 92 | 93 | int main(int argc ,char *argv[]) 94 | { 95 | os_broadcast_t *b = NULL; 96 | unsigned short port = 6666; 97 | if (argc != 2) { 98 | MLOGE("Usage: %s \n", argv[0]); 99 | return -1; 100 | } 101 | ASSERT(1 == os_sscanf(argv[1],"%hu",&port)); 102 | 103 | os_task_create(TASK_PRODUCER,0,0, task_routine_producer, (void *)NULL); 104 | os_task_create(TASK_CONSUMER1,0,0, task_routine_consumer1, (void *)NULL); 105 | os_task_create(TASK_CONSUMER2,0,0, task_routine_consumer2, (void *)NULL); 106 | 107 | b = os_broadcast_create(BROADCAST_TYPE_SERVER,port); 108 | ASSERT(b); 109 | g_b = b; 110 | 111 | while(1) 112 | { 113 | os_mm_show2(__mm_broadcast); 114 | ///////////////////////////// 115 | os_sleep(5); 116 | } 117 | 118 | os_broadcast_destroy(b); 119 | 120 | return 0; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /sample/sample.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | 3 | 4 | static void *task_routine_no1(void *arg) 5 | { 6 | int cnt = 0; 7 | void *p = NULL; 8 | while (1) 9 | { 10 | p = MALLOC((cnt+1)*16); 11 | ASSERT(p); 12 | os_sleep(1); 13 | cnt++; 14 | if(cnt > 5) 15 | break; 16 | } 17 | return NULL; 18 | } 19 | 20 | static void *task_routine_no2(void *arg) 21 | { 22 | int cnt = 0; 23 | void *p = NULL; 24 | while (1) 25 | { 26 | p = MALLOC((cnt+1)*512); 27 | ASSERT(p); 28 | os_sleep(1); 29 | cnt++; 30 | if(cnt > 10) 31 | break; 32 | } 33 | return NULL; 34 | } 35 | 36 | 37 | static void *task_routine_normal(void *arg) 38 | { 39 | int cnt = 0; 40 | void *p = NULL; 41 | while (1) 42 | { 43 | p = MALLOC((cnt+1)*2048); 44 | ASSERT(p); 45 | cnt++; 46 | FREE(p); 47 | os_sleep(1); 48 | if(cnt > 15) 49 | break; 50 | 51 | } 52 | return NULL; 53 | } 54 | 55 | 56 | static void *task_routine_dummy(void *arg) 57 | { 58 | int cnt = 0; 59 | void *p = NULL; 60 | while (1) 61 | { 62 | p = MALLOC((cnt+1)*64); 63 | ASSERT(p); 64 | cnt++; 65 | os_sleep(1); 66 | if(cnt > 20) 67 | break; 68 | } 69 | 70 | return NULL; 71 | } 72 | 73 | 74 | int main(void) 75 | { 76 | void *p1,*p2,*p3; 77 | int cnt = 0; 78 | p1 = MALLOC(10); 79 | ASSERT(p1); 80 | p2 = MALLOC(20); 81 | ASSERT(p1); 82 | p3 = MALLOC(30); 83 | ASSERT(p1); 84 | 85 | 86 | os_task_t *t1 = NULL,*t2 = NULL,*t3 = NULL,*t4 = NULL; 87 | 88 | t1 = os_task_create("no1",0,0, task_routine_no1, (void *)NULL); 89 | ASSERT(t1); 90 | t2 = os_task_create("no2",0,0, task_routine_no2, (void *)NULL); 91 | ASSERT(t2); 92 | t3 = os_task_create("normal",0,0, task_routine_normal, (void *)NULL); 93 | ASSERT(t3); 94 | t4 = os_task_create("dummy",0,0, task_routine_dummy, (void *)NULL); 95 | ASSERT(t4); 96 | 97 | while(1) 98 | { 99 | os_system("clear"); 100 | os_task_mm_show(); 101 | os_sleep(1); 102 | 103 | if(cnt == 3*1) 104 | { 105 | if(p1) {FREE(p1); p1=NULL;} 106 | } 107 | if(cnt == 3*2) 108 | { 109 | if(p2) {FREE(p2); p2=NULL;} 110 | } 111 | if(cnt == 3*3) 112 | { 113 | if(p3) {FREE(p3); p3=NULL;} 114 | } 115 | cnt++; 116 | if(cnt > 10) 117 | break; 118 | } 119 | 120 | os_mm_show(); 121 | 122 | return 0; 123 | } 124 | 125 | -------------------------------------------------------------------------------- /sample/task.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "multitask.h" 11 | #include "os_msg.h" 12 | #include "os_log.h" 13 | 14 | 15 | #define TASK_PRODUCER "producer" 16 | #define TASK_CONSUMER1 "consumer1" 17 | #define TASK_CONSUMER2 "consumer2" 18 | 19 | static void *task_routine_no1(task_self_t *self,void *arg) 20 | { 21 | int cnt = 0; 22 | void *p = NULL; 23 | while (1) 24 | { 25 | p = MALLOC((cnt+1)*16); 26 | sleep(1); 27 | cnt++; 28 | if(cnt > 5) 29 | break; 30 | } 31 | return NULL; 32 | } 33 | 34 | static void *task_routine_no2(task_self_t *self,void *arg) 35 | { 36 | int cnt = 0; 37 | void *p = NULL; 38 | while (1) 39 | { 40 | p = MALLOC((cnt+1)*512); 41 | sleep(1); 42 | cnt++; 43 | if(cnt > 10) 44 | break; 45 | } 46 | return NULL; 47 | } 48 | 49 | 50 | static void *task_routine_normal(task_self_t *self,void *arg) 51 | { 52 | int cnt = 0; 53 | void *p = NULL; 54 | while (1) 55 | { 56 | p = MALLOC((cnt+1)*2048); 57 | cnt++; 58 | FREE(p); 59 | sleep(1); 60 | if(cnt > 15) 61 | break; 62 | 63 | } 64 | return NULL; 65 | } 66 | 67 | 68 | static void *task_routine_producer(task_self_t *self,void *arg) 69 | { 70 | os_msg_t *msg; 71 | while (1) 72 | { 73 | msg = MALLOC(sizeof(*msg)+32); 74 | assert(msg); 75 | memset(msg,0,(sizeof(*msg)+32)); 76 | strcpy(msg->data,"hello"); 77 | msg->src = TASK_PRODUCER; 78 | msg->dst = TASK_CONSUMER1; 79 | msg->priority = 0; 80 | msg->size = 32; 81 | os_msg_send(msg); 82 | sleep(1); 83 | 84 | msg = MALLOC(sizeof(*msg)+32); 85 | assert(msg); 86 | memset(msg,0,(sizeof(*msg)+32)); 87 | strcpy(msg->data,"world"); 88 | msg->src = TASK_PRODUCER; 89 | msg->dst = TASK_CONSUMER2; 90 | msg->priority = 0; 91 | msg->size = 32; 92 | os_msg_send(msg); 93 | sleep(2); 94 | } 95 | 96 | return NULL; 97 | } 98 | 99 | static void *task_routine_consumer1(task_self_t *self,void *arg) 100 | { 101 | const char *str = NULL; 102 | os_msg_t *msg; 103 | while (1) 104 | { 105 | msg = os_msg_recv(); 106 | str = msg->data; 107 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 108 | FREE(msg); 109 | } 110 | 111 | return NULL; 112 | } 113 | 114 | static void *task_routine_consumer2(task_self_t *self,void *arg) 115 | { 116 | const char *str = NULL; 117 | os_msg_t *msg; 118 | while (1) 119 | { 120 | msg = os_msg_recv(); 121 | str = msg->data; 122 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 123 | FREE(msg); 124 | } 125 | 126 | return NULL; 127 | } 128 | 129 | 130 | 131 | 132 | int main(void) 133 | { 134 | void *p1,*p2,*p3; 135 | int cnt = 0; 136 | MLOGD("Task start ...."); 137 | p1 = MALLOC(10); 138 | assert(p1); 139 | p2 = MALLOC(20); 140 | assert(p1); 141 | p3 = MALLOC(30); 142 | assert(p1); 143 | 144 | os_task_create("no1",0,0, task_routine_no1, (void *)NULL); 145 | os_task_create("no2",0,0, task_routine_no2, (void *)NULL); 146 | os_task_create("normal",0,0, task_routine_normal, (void *)NULL); 147 | os_task_create(TASK_PRODUCER,0,0, task_routine_producer, (void *)NULL); 148 | os_task_create(TASK_CONSUMER1,0,0, task_routine_consumer1, (void *)NULL); 149 | os_task_create(TASK_CONSUMER2,0,0, task_routine_consumer2, (void *)NULL); 150 | 151 | while(1) 152 | { 153 | //system("clear"); 154 | os_task_mm_show(); 155 | os_mm_show(); 156 | sleep(1); 157 | 158 | if(cnt == 3*1) 159 | { 160 | if(p1) {FREE(p1); p1=NULL;} 161 | } 162 | if(cnt == 3*2) 163 | { 164 | if(p2) {FREE(p2); p2=NULL;} 165 | } 166 | if(cnt == 3*3) 167 | { 168 | if(p3) {FREE(p3); p3=NULL;} 169 | } 170 | cnt++; 171 | 172 | } 173 | 174 | 175 | return 0; 176 | } 177 | 178 | -------------------------------------------------------------------------------- /sample/task_broadcast.c: -------------------------------------------------------------------------------- 1 | 2 | #include "multitask.h" 3 | 4 | #define TASK_PRODUCER "producer" 5 | #define TASK_CONSUMER1 "consumer1" 6 | #define TASK_CONSUMER2 "consumer2" 7 | 8 | 9 | static void *task_routine_producer(void *arg) 10 | { 11 | os_msg_t *msg; 12 | int cnt = 0; 13 | while (1) 14 | { 15 | msg = MALLOC(sizeof(*msg)+32); 16 | ASSERT(msg); 17 | MLOGM("msg1=%p\n",msg); 18 | os_memset(msg,0,(sizeof(*msg)+32)); 19 | os_strcpy((char *)msg->data,"hello"); 20 | msg->src = TASK_PRODUCER; 21 | msg->dst = TASK_CONSUMER1; 22 | msg->priority = 0; 23 | msg->size = 32; 24 | if(os_msg_send(msg)) 25 | { 26 | FREE(msg); 27 | msg = NULL; 28 | } 29 | os_sleep(1); 30 | 31 | msg = MALLOC(sizeof(*msg)+32); 32 | ASSERT(msg); 33 | MLOGM("msg2=%p\n",msg); 34 | os_memset(msg,0,(sizeof(*msg)+32)); 35 | os_strcpy((char *)msg->data,"world"); 36 | msg->src = TASK_PRODUCER; 37 | msg->dst = TASK_CONSUMER2; 38 | msg->priority = 0; 39 | msg->size = 32; 40 | if(os_msg_send(msg)) 41 | { 42 | FREE(msg); 43 | msg = NULL; 44 | } 45 | os_sleep(1); 46 | if(cnt++ > 5) 47 | break; 48 | } 49 | 50 | return NULL; 51 | } 52 | 53 | static void *task_routine_consumer1(void *arg) 54 | { 55 | const char *str = NULL; 56 | os_msg_t *msg; 57 | int cnt = 0; 58 | while (1) 59 | { 60 | msg = os_msg_recv(); 61 | str = (const char *)msg->data; 62 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 63 | FREE(msg); 64 | if(cnt++ > 3) 65 | break; 66 | } 67 | 68 | return NULL; 69 | } 70 | 71 | static void *task_routine_consumer2(void *arg) 72 | { 73 | const char *str = NULL; 74 | os_msg_t *msg; 75 | int cnt = 0; 76 | while (1) 77 | { 78 | msg = os_msg_recv(); 79 | str = (const char *)msg->data; 80 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 81 | FREE(msg); 82 | if(cnt++ > 2) 83 | break; 84 | } 85 | 86 | return NULL; 87 | } 88 | 89 | 90 | int main(int argc ,char *argv[]) 91 | { 92 | os_broadcast_t *b = NULL; 93 | unsigned short port = 6666; 94 | char *pjson = NULL; 95 | if (argc != 2) { 96 | MLOGE("Usage: %s \n", argv[0]); 97 | return -1; 98 | } 99 | ASSERT(1 == os_sscanf(argv[1],"%hu",&port)); 100 | 101 | os_task_create(TASK_PRODUCER,0,0, task_routine_producer, (void *)NULL); 102 | os_task_create(TASK_CONSUMER1,0,0, task_routine_consumer1, (void *)NULL); 103 | os_task_create(TASK_CONSUMER2,0,0, task_routine_consumer2, (void *)NULL); 104 | 105 | b = os_broadcast_create(BROADCAST_TYPE_SERVER,port); 106 | ASSERT(b); 107 | 108 | while(1) 109 | { 110 | if(0 == os_task_mm_json_get(&pjson)) 111 | { 112 | MLOGD("\n%s\n",pjson); 113 | os_broadcast_send(b, (unsigned char *)pjson, os_strlen(pjson)); 114 | ///////////////////////////// 115 | FREE(pjson); 116 | } 117 | ///////////////////////////// 118 | os_sleep(1); 119 | } 120 | 121 | os_broadcast_destroy(b); 122 | 123 | return 0; 124 | } 125 | 126 | -------------------------------------------------------------------------------- /sample/task_json.c: -------------------------------------------------------------------------------- 1 | 2 | #include "multitask.h" 3 | 4 | #define TASK_PRODUCER "producer" 5 | #define TASK_CONSUMER1 "consumer1" 6 | #define TASK_CONSUMER2 "consumer2" 7 | 8 | static void *task_routine_no1(void *arg) 9 | { 10 | int cnt = 0; 11 | void *p = NULL; 12 | while (1) 13 | { 14 | p = MALLOC((cnt+1)*16); 15 | ASSERT(p); 16 | os_sleep(1); 17 | cnt++; 18 | if(cnt > 5) 19 | break; 20 | } 21 | return NULL; 22 | } 23 | 24 | static void *task_routine_no2(void *arg) 25 | { 26 | int cnt = 0; 27 | void *p = NULL; 28 | while (1) 29 | { 30 | p = MALLOC((cnt+1)*512); 31 | ASSERT(p); 32 | os_sleep(1); 33 | cnt++; 34 | if(cnt > 10) 35 | break; 36 | } 37 | return NULL; 38 | } 39 | 40 | 41 | static void *task_routine_normal(void *arg) 42 | { 43 | int cnt = 0; 44 | void *p = NULL; 45 | while (1) 46 | { 47 | p = MALLOC((cnt+1)*2048); 48 | cnt++; 49 | FREE(p); 50 | ASSERT(p); 51 | os_sleep(1); 52 | if(cnt > 15) 53 | break; 54 | 55 | } 56 | return NULL; 57 | } 58 | 59 | 60 | int main(void) 61 | { 62 | void *p1,*p2,*p3; 63 | int cnt = 0; 64 | char *pjson = NULL; 65 | MLOGD("Task start ...."); 66 | p1 = MALLOC(10); 67 | ASSERT(p1); 68 | p2 = MALLOC(20); 69 | ASSERT(p1); 70 | p3 = MALLOC(30); 71 | ASSERT(p1); 72 | 73 | os_task_create("no1",0,0, task_routine_no1, (void *)NULL); 74 | os_task_create("no2",0,0, task_routine_no2, (void *)NULL); 75 | os_task_create("normal",0,0, task_routine_normal, (void *)NULL); 76 | 77 | while(1) 78 | { 79 | //system("clear"); 80 | if(0 == os_task_mm_json_get(&pjson)) 81 | { 82 | MLOGD("JSON:\n%s\n",pjson); 83 | FREE(pjson); 84 | } 85 | 86 | os_sleep(1); 87 | 88 | if(cnt == 3*1) 89 | { 90 | if(p1) {FREE(p1); p1=NULL;} 91 | } 92 | if(cnt == 3*2) 93 | { 94 | if(p2) {FREE(p2); p2=NULL;} 95 | } 96 | if(cnt == 3*3) 97 | { 98 | if(p3) {FREE(p3); p3=NULL;} 99 | } 100 | cnt++; 101 | if(cnt > 20) 102 | break; 103 | } 104 | 105 | os_mm_show(); 106 | return 0; 107 | } 108 | 109 | -------------------------------------------------------------------------------- /sample/task_multicast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "parson.h" 10 | 11 | #include "multitask.h" 12 | 13 | #define TASK_PRODUCER "producer" 14 | #define TASK_CONSUMER1 "consumer1" 15 | #define TASK_CONSUMER2 "consumer2" 16 | 17 | #define MUTICAST_ADDR "239.255.255.250" 18 | 19 | 20 | static void *task_routine_producer(void *arg) 21 | { 22 | os_msg_t *msg; 23 | int cnt = 0; 24 | while (1) 25 | { 26 | msg = MALLOC(sizeof(*msg)+32); 27 | ASSERT(msg); 28 | MLOGM("msg1=%p\n",msg); 29 | os_memset(msg,0,(sizeof(*msg)+32)); 30 | os_strcpy((char *)msg->data,"hello"); 31 | msg->src = TASK_PRODUCER; 32 | msg->dst = TASK_CONSUMER1; 33 | msg->priority = 0; 34 | msg->size = 32; 35 | if(os_msg_send(msg)) 36 | { 37 | FREE(msg); 38 | msg = NULL; 39 | } 40 | os_sleep(1); 41 | 42 | msg = MALLOC(sizeof(*msg)+32); 43 | ASSERT(msg); 44 | MLOGM("msg2=%p\n",msg); 45 | os_memset(msg,0,(sizeof(*msg)+32)); 46 | os_strcpy((char *)msg->data,"world"); 47 | msg->src = TASK_PRODUCER; 48 | msg->dst = TASK_CONSUMER2; 49 | msg->priority = 0; 50 | msg->size = 32; 51 | if(os_msg_send(msg)) 52 | { 53 | FREE(msg); 54 | msg = NULL; 55 | } 56 | os_sleep(1); 57 | if(cnt++ > 5) 58 | break; 59 | } 60 | 61 | return NULL; 62 | } 63 | 64 | static void *task_routine_consumer1(void *arg) 65 | { 66 | const char *str = NULL; 67 | os_msg_t *msg; 68 | int cnt = 0; 69 | while (1) 70 | { 71 | msg = os_msg_recv(); 72 | str = (char *)msg->data; 73 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 74 | FREE(msg); 75 | if(cnt++ > 3) 76 | break; 77 | } 78 | 79 | return NULL; 80 | } 81 | 82 | static void *task_routine_consumer2(void *arg) 83 | { 84 | const char *str = NULL; 85 | os_msg_t *msg; 86 | int cnt = 0; 87 | while (1) 88 | { 89 | msg = os_msg_recv(); 90 | str = (char *)msg->data; 91 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 92 | FREE(msg); 93 | if(cnt++ > 2) 94 | break; 95 | } 96 | 97 | return NULL; 98 | } 99 | 100 | 101 | 102 | int main(int argc ,char *argv[]) 103 | { 104 | os_multicast_t *m = NULL; 105 | unsigned short port = 6666; 106 | char *pjson = NULL; 107 | if (argc != 2) { 108 | MLOGE("Usage: %s \n", argv[0]); 109 | return -1; 110 | } 111 | 112 | ASSERT(1 == sscanf(argv[1],"%hu",&port)); 113 | 114 | os_task_create(TASK_PRODUCER,0,0, task_routine_producer, (void *)NULL); 115 | os_task_create(TASK_CONSUMER1,0,0, task_routine_consumer1, (void *)NULL); 116 | os_task_create(TASK_CONSUMER2,0,0, task_routine_consumer2, (void *)NULL); 117 | 118 | 119 | m = os_multicast_create(MULTICAST_TYPE_SERVER,MUTICAST_ADDR,port); 120 | ASSERT(m); 121 | 122 | while(1) 123 | { 124 | if(0 == os_task_mm_json_get(&pjson)) 125 | { 126 | MLOGD("\n%s\n",pjson); 127 | os_multicast_send(m, (unsigned char *)pjson, strlen(pjson)); 128 | ///////////////////////////// 129 | FREE(pjson); 130 | } 131 | ///////////////////////////// 132 | os_sleep(1); 133 | } 134 | 135 | os_multicast_destroy(m); 136 | 137 | return 0; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /sample/task_show.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | 3 | 4 | #define TASK_PRODUCER "producer" 5 | #define TASK_CONSUMER1 "consumer1" 6 | #define TASK_CONSUMER2 "consumer2" 7 | 8 | static void *task_routine_no1(void *arg) 9 | { 10 | int cnt = 0; 11 | void *p = NULL; 12 | while (1) 13 | { 14 | p = MALLOC((cnt+1)*16); 15 | ASSERT(p); 16 | os_sleep(1); 17 | cnt++; 18 | if(cnt > 5) 19 | break; 20 | } 21 | return NULL; 22 | } 23 | 24 | static void *task_routine_no2(void *arg) 25 | { 26 | int cnt = 0; 27 | void *p = NULL; 28 | while (1) 29 | { 30 | p = MALLOC((cnt+1)*512); 31 | ASSERT(p); 32 | os_sleep(1); 33 | cnt++; 34 | if(cnt > 10) 35 | break; 36 | } 37 | return NULL; 38 | } 39 | 40 | 41 | static void *task_routine_normal(void *arg) 42 | { 43 | int cnt = 0; 44 | void *p = NULL; 45 | while (1) 46 | { 47 | p = MALLOC((cnt+1)*2048); 48 | cnt++; 49 | FREE(p); 50 | ASSERT(p); 51 | os_sleep(1); 52 | if(cnt > 15) 53 | break; 54 | 55 | } 56 | return NULL; 57 | } 58 | 59 | 60 | static void *task_routine_producer(void *arg) 61 | { 62 | os_msg_t *msg; 63 | int cnt = 0; 64 | while (1) 65 | { 66 | msg = MALLOC(sizeof(*msg)+32); 67 | ASSERT(msg); 68 | MLOGM("msg1=%p\n",msg); 69 | os_memset(msg,0,(sizeof(*msg)+32)); 70 | os_strcpy((char *)msg->data,"hello"); 71 | msg->src = TASK_PRODUCER; 72 | msg->dst = TASK_CONSUMER1; 73 | msg->priority = 0; 74 | msg->size = 32; 75 | if(os_msg_send(msg)) 76 | { 77 | FREE(msg); 78 | msg = NULL; 79 | } 80 | os_sleep(1); 81 | 82 | msg = MALLOC(sizeof(*msg)+32); 83 | ASSERT(msg); 84 | MLOGM("msg2=%p\n",msg); 85 | os_memset(msg,0,(sizeof(*msg)+32)); 86 | os_strcpy((char *)msg->data,"world"); 87 | msg->src = TASK_PRODUCER; 88 | msg->dst = TASK_CONSUMER2; 89 | msg->priority = 0; 90 | msg->size = 32; 91 | if(os_msg_send(msg)) 92 | { 93 | FREE(msg); 94 | msg = NULL; 95 | } 96 | os_sleep(1); 97 | if(cnt++ > 5) 98 | break; 99 | } 100 | 101 | return NULL; 102 | } 103 | 104 | static void *task_routine_consumer1(void *arg) 105 | { 106 | const char *str = NULL; 107 | os_msg_t *msg; 108 | int cnt = 0; 109 | while (1) 110 | { 111 | msg = os_msg_recv(); 112 | str = (const char *)msg->data; 113 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 114 | FREE(msg); 115 | if(cnt++ > 3) 116 | break; 117 | } 118 | 119 | return NULL; 120 | } 121 | 122 | static void *task_routine_consumer2(void *arg) 123 | { 124 | const char *str = NULL; 125 | os_msg_t *msg; 126 | int cnt = 0; 127 | while (1) 128 | { 129 | msg = os_msg_recv(); 130 | str = (const char *)msg->data; 131 | MLOGM("[From: %s To: %s]str: %s\n",msg->src,msg->dst,str); 132 | FREE(msg); 133 | if(cnt++ > 2) 134 | break; 135 | } 136 | 137 | return NULL; 138 | } 139 | 140 | 141 | 142 | 143 | int main(void) 144 | { 145 | void *p1,*p2,*p3; 146 | int cnt = 0; 147 | MLOGD("Task start ...."); 148 | p1 = MALLOC(10); 149 | ASSERT(p1); 150 | p2 = MALLOC(20); 151 | ASSERT(p1); 152 | p3 = MALLOC(30); 153 | ASSERT(p1); 154 | 155 | os_task_create("no1",0,0, task_routine_no1, (void *)NULL); 156 | os_task_create("no2",0,0, task_routine_no2, (void *)NULL); 157 | os_task_create("normal",0,0, task_routine_normal, (void *)NULL); 158 | os_task_create(TASK_PRODUCER,0,0, task_routine_producer, (void *)NULL); 159 | os_task_create(TASK_CONSUMER1,0,0, task_routine_consumer1, (void *)NULL); 160 | os_task_create(TASK_CONSUMER2,0,0, task_routine_consumer2, (void *)NULL); 161 | 162 | while(1) 163 | { 164 | //system("clear"); 165 | os_task_mm_show(); 166 | os_sleep(1); 167 | 168 | if(cnt == 3*1) 169 | { 170 | if(p1) {FREE(p1); p1=NULL;} 171 | } 172 | if(cnt == 3*2) 173 | { 174 | if(p2) {FREE(p2); p2=NULL;} 175 | } 176 | if(cnt == 3*3) 177 | { 178 | if(p3) {FREE(p3); p3=NULL;} 179 | } 180 | cnt++; 181 | if(cnt > 20) 182 | break; 183 | } 184 | 185 | os_mm_show(); 186 | return 0; 187 | } 188 | 189 | -------------------------------------------------------------------------------- /sample/tcp_client.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | os_tcp_client_t *c = NULL; 6 | const char *str = NULL; 7 | int ret = -1; 8 | char buf[1024] = {0}; 9 | if (argc != 2) { 10 | MLOGE("Usage: %s \n", argv[0]); 11 | return -1; 12 | } 13 | str = argv[1]; 14 | c = os_tcp_client_create("tcp_client", "127.0.0.1",6666); 15 | RETURN_VAL_IF_FAIL(c, -1); 16 | MLOGD("os_tcp_client_create succ!!!"); 17 | ret = os_tcp_client_write(c,(void *)str, os_strlen(str)); 18 | RETURN_VAL_IF_FAIL(ret > 0, -1); 19 | ret = os_tcp_client_read(c,(void *)buf, sizeof(buf)); 20 | RETURN_VAL_IF_FAIL(ret > 0, -1); 21 | MLOGD("read %d byte: %s",ret,buf); 22 | os_tcp_client_destroy(c); 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /sample/tcp_server.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | 3 | 4 | static void __tcp_server_callback(int fd,void *user_data) 5 | { 6 | char buf[1024] = {0}; 7 | int ret = -1; 8 | ret = os_read(fd,buf, sizeof(buf)); 9 | RETURN_IF_FAIL(ret > 0); 10 | MLOGD("os_read: %s",buf); 11 | ret = os_write(fd,buf, ret); 12 | RETURN_IF_FAIL(ret > 0); 13 | MLOGD("os_write: %d byte",ret); 14 | } 15 | 16 | 17 | int main(int argc, char *argv[]) 18 | { 19 | os_tcp_server_t *s = NULL; 20 | s = os_tcp_server_create(5, 5, 6666, __tcp_server_callback, NULL); 21 | RETURN_VAL_IF_FAIL(s, -1); 22 | MLOGD("os_tcp_server_create succ!!!"); 23 | os_tcp_server_destroy(s); 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /sample/udp_tcp_client.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | #include "parson.h" 3 | 4 | #define TCP_CLINET2_PORT 6666 5 | #define UDP_CLINET2_PORT 9999 6 | 7 | typedef struct test_ip_info_s { 8 | char ip[32]; 9 | unsigned short port; 10 | unsigned char mac[6]; 11 | }test_ip_info_t; 12 | 13 | static test_ip_info_t g_test_ip_info = {0}; 14 | 15 | static void *task_routine_tcp_client2(void *arg) 16 | { 17 | static os_tcp_client_t *c = NULL; 18 | const char *str = "i'm task_routine_tcp_client2..."; 19 | int ret = -1; 20 | char buf[1024] = {0}; 21 | 22 | while(1) 23 | { 24 | os_memset(buf, 0, sizeof(buf)); 25 | if(0 != g_test_ip_info.ip[0]) 26 | { 27 | MLOGI("ip:%s , port:%d , mac[%02x:%02x:%02x:%02x:%02x:%02x]\n", 28 | g_test_ip_info.ip,g_test_ip_info.port,g_test_ip_info.mac[0],g_test_ip_info.mac[1], 29 | g_test_ip_info.mac[2],g_test_ip_info.mac[3],g_test_ip_info.mac[4],g_test_ip_info.mac[5]); 30 | c = os_tcp_client_create("tcp_client", g_test_ip_info.ip,g_test_ip_info.port); 31 | 32 | ret = os_tcp_client_write(c,(void *)str, os_strlen(str)); 33 | RETURN_VAL_IF_FAIL(ret > 0, NULL); 34 | ret = os_tcp_client_read(c,(void *)buf, sizeof(buf)); 35 | RETURN_VAL_IF_FAIL(ret > 0, NULL); 36 | MLOGD("read %d byte: %s",ret,buf); 37 | os_tcp_client_destroy(c); 38 | c = NULL; 39 | os_sleep(1); 40 | } 41 | else 42 | { 43 | os_sleep(1); 44 | } 45 | } 46 | return NULL; 47 | } 48 | 49 | 50 | static void *task_routine_udp_client2(void *arg) 51 | { 52 | os_broadcast_t *b = NULL; 53 | char *pjson = NULL; 54 | unsigned char buf[1024] = {0}; 55 | int ret = -1; 56 | PJSON_Value *jvalRoot = NULL; 57 | PJSON_Object *jobjRoot = NULL; 58 | 59 | b = os_broadcast_create(BROADCAST_TYPE_CLIENT,UDP_CLINET2_PORT); 60 | ASSERT(b); 61 | 62 | while(1) 63 | { 64 | ret = os_broadcast_recv(b, buf, sizeof(buf)); 65 | CONTINUE_IF_FAIL(ret > 0); 66 | //json parse 67 | MLOGD("buf: %s",buf); 68 | pjson = (char *)buf; 69 | jvalRoot = pjson_parse_string(pjson); 70 | RETURN_VAL_IF_FAIL(jvalRoot,NULL); 71 | jobjRoot = pjson_value_get_object(jvalRoot); 72 | RETURN_VAL_IF_FAIL(jobjRoot,NULL); 73 | os_strcpy(g_test_ip_info.ip,pjson_object_dotget_string(jobjRoot, "ip")); 74 | g_test_ip_info.port = (unsigned short)pjson_object_dotget_number(jobjRoot, "port"); 75 | /* 76 | const char *mac = pjson_object_dotget_string(jobjRoot, "mac"); 77 | os_sscanf(mac, "%x:%02x:%02x:%02x:%02x:%02x",&ipinfo->mac[0],&ipinfo->mac[1] 78 | ,&ipinfo->mac[2],&ipinfo->mac[3],&ipinfo->mac[4],&ipinfo->mac[5]); 79 | */ 80 | pjson_value_free(jvalRoot); 81 | ///////////////////////////// 82 | //os_sleep(1); 83 | } 84 | 85 | os_broadcast_destroy(b); 86 | return NULL; 87 | } 88 | 89 | 90 | int main(int argc, char *argv[]) 91 | { 92 | os_task_create("tcp_client2",0,0, task_routine_tcp_client2, (void *)NULL); 93 | os_task_create("udp_client2",0,0, task_routine_udp_client2, (void *)NULL); 94 | 95 | while(1) 96 | os_sleep(5); 97 | return 0; 98 | } 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /sample/udp_tcp_server.c: -------------------------------------------------------------------------------- 1 | #include "multitask.h" 2 | #include "parson.h" 3 | 4 | #define TCP_SERVER2_PORT 6666 5 | #define TCP_SERVER2_IP "127.0.0.1" 6 | #define TCP_SERVER2_MAC "ff:ff:ff:ff:ff:ff" 7 | 8 | #define UDP_SERVER2_PORT 9999 9 | 10 | static void __tcp_server_callback2(int fd,void *user_data) 11 | { 12 | char buf[1024] = {0}; 13 | int ret = -1; 14 | //while(1) 15 | { 16 | ret = os_read(fd,buf, sizeof(buf)); 17 | RETURN_IF_FAIL(ret > 0); 18 | MLOGD("os_read: %s",buf); 19 | ret = os_write(fd,buf, ret); 20 | RETURN_IF_FAIL(ret > 0); 21 | MLOGD("os_write: %d byte",ret); 22 | } 23 | } 24 | 25 | static void *task_routine_tcp_server2(void *arg) 26 | { 27 | os_tcp_server_t *s = NULL; 28 | s = os_tcp_server_create(5, 5, TCP_SERVER2_PORT, __tcp_server_callback2, NULL); 29 | RETURN_VAL_IF_FAIL(s, NULL); 30 | MLOGD("os_tcp_server_create succ!!!"); 31 | os_tcp_server_destroy(s); 32 | return NULL; 33 | } 34 | 35 | 36 | static void *task_routine_udp_server2(void *arg) 37 | { 38 | os_broadcast_t *b = NULL; 39 | char *pjson = NULL; 40 | 41 | b = os_broadcast_create(BROADCAST_TYPE_SERVER,UDP_SERVER2_PORT); 42 | RETURN_VAL_IF_FAIL(b,NULL); 43 | PJSON_Value *jvalRoot = pjson_value_init_object(); 44 | RETURN_VAL_IF_FAIL(jvalRoot,NULL); 45 | PJSON_Object *jobjRoot = pjson_value_get_object(jvalRoot); 46 | RETURN_VAL_IF_FAIL(jobjRoot,NULL); 47 | pjson_object_dotset_string(jobjRoot,"ip",TCP_SERVER2_IP); 48 | pjson_object_dotset_number(jobjRoot,"port",TCP_SERVER2_PORT); 49 | pjson_object_dotset_string(jobjRoot,"mac",TCP_SERVER2_MAC); 50 | pjson = pjson_serialize_to_string(jvalRoot); 51 | RETURN_VAL_IF_FAIL(pjson,NULL); 52 | while(1) 53 | { 54 | MLOGD("json: %s\n",pjson); 55 | os_broadcast_send(b, (unsigned char *)pjson, os_strlen(pjson)); 56 | ///////////////////////////// 57 | ///////////////////////////// 58 | os_sleep(1); 59 | } 60 | 61 | os_broadcast_destroy(b); 62 | pjson_value_free(jvalRoot); 63 | return NULL; 64 | } 65 | 66 | 67 | int main(int argc, char *argv[]) 68 | { 69 | os_task_create("tcp_server2",0,0, task_routine_tcp_server2, (void *)NULL); 70 | os_task_create("udp_server2",0,0, task_routine_udp_server2, (void *)NULL); 71 | 72 | while(1) 73 | os_sleep(5); 74 | return 0; 75 | } 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /scripts/Makefile.build: -------------------------------------------------------------------------------- 1 | 2 | src := $(obj) 3 | lib_name := ${strip $(subst /,_, ${obj})} 4 | 5 | PHONY := __build 6 | PHONY += sub_target 7 | 8 | obj-y := 9 | 10 | private-cflags := 11 | 12 | install_lib ?= target/lib 13 | install_bin ?= target/bin 14 | 15 | -include $(BUILD_FILE) 16 | include ./${src}/Makefile 17 | 18 | CFLAGS += ${private-cflags} 19 | 20 | __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) 21 | subdir-y := $(addprefix $(obj)/,$(__subdir-y)) 22 | 23 | obj-y := $(filter %.o, $(obj-y)) 24 | ifneq ($(strip $(obj-y)),) 25 | builtin-target := ${strip $(obj)/lib$(lib_name)} 26 | obj-y := $(addprefix $(obj)/,$(obj-y)) 27 | endif 28 | 29 | depends := $(obj-y:.o=.dep) 30 | 31 | __build: ${builtin-target} $(subdir-y) ${depends} FORCE 32 | 33 | ifdef builtin-target 34 | $(builtin-target): $(obj-y) FORCE 35 | $(if ${obj-y},mkdir -p ${install_lib}) 36 | # $(if ${obj-y},${CC} ${LDFLAGS} -o $@.so $(obj-y)) 37 | # $(if ${obj-y},install -p -m 0644 $@.so ${install_lib}) 38 | $(if ${obj-y},${LD} -r -o $@.a $(obj-y) ) 39 | $(if ${obj-y},install -p -m 0644 $@.a ${install_lib}) 40 | endif # builtin-target 41 | 42 | PHONY += $(subdir-y) 43 | $(subdir-y): FORCE 44 | @make -f ${MAKEFILE_BUILD} obj=$@ 45 | 46 | #################### rule #################### 47 | $(obj)/%.o: $(src)/%.c 48 | $(CC) ${CFLAGS} -MMD -MP -MF"$(@:%.o=%.dep)" -o $@ -c $< 49 | 50 | $(obj)/%.o: $(src)/%.cpp 51 | $(CC) ${CFLAGS} -MMD -MP -MF"$(@:%.o=%.dep)" -o $@ -c $< 52 | 53 | 54 | -include $(obj)/*.dep 55 | include scripts/Makefile.function 56 | 57 | PHONY += FORCE 58 | FORCE: 59 | 60 | 61 | -------------------------------------------------------------------------------- /scripts/Makefile.function: -------------------------------------------------------------------------------- 1 | #################################################### 2 | # Make Function 3 | #################################################### 4 | 5 | 6 | # dir , lib name , suffix ,configure args 7 | define auto_make_install_compressed_lib 8 | @echo "" ">>>>>>> Build File: $(2)$(3)" "" ; 9 | @set -x; 10 | tar -zxf $(1)/$(2)$(3) -C $(1)/ ; 11 | cd $(1)/$(2) && ./configure $(4) && make -j8 && make install; 12 | endef 13 | 14 | # dir , lib name , suffix ,configure args 15 | define auto_make_install_compressed_lib_once 16 | @echo "" ">>>>>>> Build File: $(2)$(3)" "" ; 17 | @set -x; 18 | @if [ ! -d $(1)/$(2) ]; then \ 19 | {\ 20 | tar -zxf $(1)/$(2)$(3) -C $(1)/ ;\ 21 | cd $(1)/$(2) && ./configure $(4) && make -j8 && make install;\ 22 | };\ 23 | else echo "" "$(2): nothing to do!!! " "" ;\ 24 | fi 25 | endef 26 | 27 | # dir , lib name , suffix ,configure args 28 | define auto_make_install_compressed_lib_once_dirty 29 | @echo "" ">>>>>>> Build File: $(2)$(3)" "" ; 30 | @set -x; 31 | @if [ ! -d $(1)/$(2) ]; then \ 32 | {\ 33 | tar -zxf $(1)/$(2)$(3) -C $(1)/ ;\ 34 | rm $(1)/$(2)/install -f && ln -s `which install` $(1)/$(2)/install ; \ 35 | cd $(1)/$(2) && ./configure $(4) && make -j8 && make install;\ 36 | };\ 37 | else echo "" "$(2): nothing to do!!! " "" ;\ 38 | fi 39 | endef 40 | 41 | 42 | # path 43 | define file-chk 44 | if [ -f $1 ]; then echo "y";\ 45 | else echo "n";\ 46 | fi 47 | endef 48 | 49 | 50 | 51 | 52 | # dir , lib name , suffix ,configure args 53 | define auto_cmake_install_compressed_lib 54 | @echo "" ">>>>>>> Build File: $(2)$(3)" "" ; 55 | @set -x; 56 | tar -zxf $(1)/$(2)$(3) -C $(1)/ ; 57 | cd $(1)/$(2) && mkdir -p build && cd build && cmake $(4) .. && make -j8 && make install; 58 | endef 59 | 60 | # dir , lib name , suffix ,configure args 61 | define auto_cmake_install_compressed_lib_once 62 | @echo "" ">>>>>>> Build File: $(2)$(3)" "" ; 63 | @set -x; 64 | @if [ ! -d $(1)/$(2) ]; then \ 65 | {\ 66 | tar -zxf $(1)/$(2)$(3) -C $(1)/ ;\ 67 | cd $(1)/$(2) && mkdir -p build && cd build && cmake $(4) .. && make -j8 && make install;\ 68 | };\ 69 | else echo "" "$(2): nothing to do!!! " "" ;\ 70 | fi 71 | endef 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /scripts/Makefile.test.build: -------------------------------------------------------------------------------- 1 | # ========================================================================== 2 | # Makefile build 3 | # ========================================================================== 4 | 5 | src := $(obj) 6 | 7 | PHONY := __build 8 | 9 | obj-y := 10 | 11 | 12 | install_lib ?= target/lib 13 | install_bin ?= target/bin 14 | 15 | 16 | -include $(BUILD_FILE) 17 | include ./${src}/Makefile 18 | 19 | 20 | __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) 21 | subdir-y := $(addprefix $(obj)/,$(__subdir-y)) 22 | 23 | obj-y := $(filter %.o, $(obj-y)) 24 | ifneq ($(strip $(obj-y)),) 25 | builtin-target := ${obj-y:.o=} 26 | builtin-target := $(addprefix $(obj)/,$(builtin-target)) 27 | endif 28 | 29 | 30 | __build: ${builtin-target} $(subdir-y) FORCE 31 | 32 | ifdef builtin-target 33 | $(builtin-target): FORCE 34 | $(if ${obj-y},mkdir -p $(install_bin)) 35 | $(if ${obj-y},$(CC) ${TEST_CFLAGS} ${LINK_PATH} -o $@ $@.c ${LD_COMM_NAME} ${LD_LIBS}) 36 | $(if ${obj-y},install -p -m 0555 $@ $(install_bin)) 37 | endif # builtin-target 38 | 39 | PHONY += $(subdir-y) 40 | $(subdir-y): FORCE 41 | @make -f ${MAKEFILE_TEST_BUILD} obj=$@ 42 | 43 | #################### rule #################### 44 | $(obj)/%.o: $(src)/%.c 45 | $(CC) ${CFLAGS} -MMD -MP -MF"$(@:%.o=%.dep)" -o $@ -c $< 46 | 47 | $(obj)/%.o: $(src)/%.cpp 48 | $(CC) ${CFLAGS} -MMD -MP -MF"$(@:%.o=%.dep)" -o $@ -c $< 49 | 50 | -include $(obj)/*.dep 51 | 52 | 53 | PHONY += FORCE 54 | FORCE: 55 | -------------------------------------------------------------------------------- /scripts/common.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #set -e 4 | 5 | # for debug 6 | DEBUG_LOG_FILE='&2' 7 | DEBUG_LOG_LEVEL=0 8 | 9 | # ANSI COLORS 10 | COLOR_CRE="" 11 | COLOR_NORMAL="" 12 | COLOR_RED="" 13 | COLOR_GREEN="" 14 | COLOR_YELLOW="" 15 | COLOR_BLUE="" 16 | COLOR_MAGENTA="" 17 | COLOR_CYAN="" 18 | COLOR_WHITE="" 19 | 20 | # Shell command 21 | TAR=tar 22 | CP=/bin/cp 23 | RM=/bin/rm 24 | GREP=grep 25 | SED=sed 26 | MKDIR=mkdir 27 | CHMOD=chmod 28 | MV=mv 29 | CD=cd 30 | LN=ln 31 | MAKE=make 32 | MKNOD=mknod 33 | PUSHD=pushd 34 | POPD=popd 35 | RMDIR=rmdir 36 | DEPMOD=/sbin/depmod 37 | RMDIR=rmdir 38 | MKIMG=mkimage 39 | PATCH=patch 40 | DIFF=diff 41 | TOUCH=touch 42 | CAT=cat 43 | PWD=`pwd` 44 | 45 | 46 | 47 | #unset linux environment variable 48 | ENV_VAR_UNSET() 49 | { 50 | # cross compile 51 | unset CC 52 | unset AR 53 | unset RANLIB 54 | unset CFLAGS 55 | unset LDFLAGS 56 | unset CROSS_COMPILE 57 | unset PROCESSOR 58 | 59 | # path 60 | unset DESTDIR 61 | unset LIBDIR 62 | unset INCDIR 63 | unset BINDIR 64 | unset LIBPATH 65 | unset INCPATH 66 | unset BINPATH 67 | unset DATAPATH 68 | unset PREFIX 69 | 70 | #misc 71 | unset PKG_CONFIG 72 | unset PKG_CONFIG_PATH 73 | } 74 | 75 | e_blank='[ ][ ]*' 76 | e_year='20[0-9][0-9]' 77 | e_month='([1-9]|0[1-9]|1[0-2])' 78 | e_day='([1-9]|0[1-9]|[12][0-9]|3[0-1])' 79 | e_time='([01][0-9]|2[0-3]):[0-5][0-9]' 80 | e_employid='[a-zA-Z][a-zA-Z]*[0-9]{4,}' 81 | 82 | #$1: string 83 | #$2: color 84 | ECHO() 85 | { 86 | [ -n "$2" ] && eval echo -n \"\${${2}}\"; 87 | echo "${1}${COLOR_NORMAL}" 88 | } 89 | 90 | #$1 FUNC-LINE 91 | #$2 string 92 | ERR() 93 | { 94 | echo "${COLOR_RED} ERR${1}: ${2}${COLOR_NORMAL}" >&2 95 | } 96 | 97 | #$1 FUNC-LINE 98 | #$2 string 99 | WARN() 100 | { 101 | echo "${COLOR_YELLOW}WARN${1}: ${2}${COLOR_NORMAL}" >&2 102 | } 103 | 104 | #$1 FUNC-LINE 105 | #$2 string 106 | CMD() 107 | { 108 | echo "${COLOR_GREEN} CMD${1}: ${2}${COLOR_NORMAL}" >&2 109 | } 110 | 111 | # $1: string 112 | LOG() 113 | { 114 | echo "${COLOR_CYAN} LOG: ${1}${COLOR_NORMAL}" >&2 115 | } 116 | 117 | 118 | #$1: string 119 | #$2: level 120 | DEBUG() 121 | { 122 | local level=$2 123 | [ -z "$level" ] && { level=0; } 124 | [ $level -lt $DEBUG_LOG_LEVEL ] && return 0; 125 | 126 | echo "$COLOR_WHITE$1$COLOR_NORMAL" > $DEBUG_LOG_FILE 127 | } 128 | 129 | 130 | #$1: self script name 131 | dependencies_info() 132 | { 133 | ERR [$FUNCNAME-$LINENO] "usage: $1 [language] [action] [arch] [soc] [dirname] [clibs]" 134 | CMD [$FUNCNAME-$LINENO] "language options: [c/cpp]" 135 | CMD [$FUNCNAME-$LINENO] "action options: [make/clean/install/uninstall]" 136 | CMD [$FUNCNAME-$LINENO] "arch options: [arm/x86/mips]" 137 | CMD [$FUNCNAME-$LINENO] "soc options: [goke/hi3518e/hi3518ev200/xilink]" 138 | CMD [$FUNCNAME-$LINENO] "clibs options: [uclibc/glibc]" 139 | } 140 | 141 | 142 | #$1: language 143 | #$2: action 144 | #$3: arch 145 | #$4: soc 146 | #$5: clibs 147 | dependencies_assert() 148 | { 149 | if [[ "$1" != "c" ]] \ 150 | && [[ "$1" != "cpp" ]] ;then 151 | ERR [$FUNCNAME-$LINENO] "language: $1 unsupport..." 152 | exit 1 153 | fi 154 | 155 | if [ "$2" != "make" ] \ 156 | && [ "$2" != "clean" ] \ 157 | && [ "$2" != "install" ] \ 158 | && [ "$2" != "uninstall" ] ;then 159 | ERR [$FUNCNAME-$LINENO] "action: $2 unsupport..." 160 | exit 1 161 | fi 162 | 163 | if [ "$3" != "arm" ] \ 164 | && [ "$3" != "x86" ] \ 165 | && [ "$3" != "mips" ] ;then 166 | ERR [$FUNCNAME-$LINENO] "arch: $3 unsupport..." 167 | exit 1 168 | fi 169 | 170 | if [[ "$4" != "goke" ]] \ 171 | && [[ "$4" != "hi3518e" ]] \ 172 | && [[ "$4" != "hi3518ev200" ]] \ 173 | && [[ "$4" != "ingenic_t31" ]] \ 174 | && [[ "$4" != "x86" ]] \ 175 | && [[ "$4" != "xilink" ]] ;then 176 | ERR [$FUNCNAME-$LINENO] "soc: $4 unsupport..." 177 | exit 1 178 | fi 179 | 180 | if [ "$5" != "uclibc" ] \ 181 | && [ "$5" != "glibc" ] ;then 182 | ERR [$FUNCNAME-$LINENO] "clibs: $5 unsupport..." 183 | exit 1 184 | fi 185 | 186 | } -------------------------------------------------------------------------------- /scripts/create_3thlibs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # ANSI COLORS 4 | COLOR_CRE="" 5 | COLOR_NORMAL="" 6 | COLOR_RED="" 7 | COLOR_GREEN="" 8 | COLOR_YELLOW="" 9 | COLOR_BLUE="" 10 | COLOR_MAGENTA="" 11 | COLOR_CYAN="" 12 | COLOR_WHITE="" 13 | 14 | #$1: string 15 | #$2: color 16 | ECHO() 17 | { 18 | [ -n "$2" ] && eval echo -n \"\${${2}}\"; 19 | echo "${1}${COLOR_NORMAL}" 20 | } 21 | 22 | #$1 FUNC-LINE 23 | #$2 string 24 | ERR() 25 | { 26 | echo "${COLOR_RED} ERR${1}: ${2}${COLOR_NORMAL}" >&2 27 | } 28 | 29 | #$1 FUNC-LINE 30 | #$2 string 31 | WARN() 32 | { 33 | echo "${COLOR_YELLOW}WARN${1}: ${2}${COLOR_NORMAL}" >&2 34 | } 35 | 36 | #$1 FUNC-LINE 37 | #$2 string 38 | CMD() 39 | { 40 | echo "${COLOR_GREEN} CMD${1}: ${2}${COLOR_NORMAL}" >&2 41 | } 42 | 43 | # $1: string 44 | LOG() 45 | { 46 | echo "${COLOR_CYAN} LOG: ${1}${COLOR_NORMAL}" >&2 47 | } 48 | 49 | 50 | #$1: string 51 | #$2: level 52 | DEBUG() 53 | { 54 | local level=$2 55 | [ -z "$level" ] && { level=0; } 56 | [ $level -lt $DEBUG_LOG_LEVEL ] && return 0; 57 | 58 | echo "$COLOR_WHITE$1$COLOR_NORMAL" > $DEBUG_LOG_FILE 59 | } 60 | 61 | CUR_DIR=$PWD 62 | 63 | if [ $# -lt 1 ];then 64 | ERR [$FUNCNAME-$LINENO] "usage: $0 [soc]" 65 | CMD [$FUNCNAME-$LINENO] "soc options: [x86]" 66 | exit 1 67 | fi 68 | 69 | soc=$1 70 | LOG "soc : $soc" 71 | 72 | 73 | if [[ "$soc" == "x86" ]] ; then 74 | cross_compiler="g++" 75 | shared_lib_name="./3th/lib/lib3th-${soc}.so" 76 | root_path="./3th/lib/x86/${soc}" 77 | static_librarys="libgio-2.0.a libgmodule-2.0.a libgobject-2.0.a libgthread-2.0.a libglib-2.0.a libffi.a libdbus-1.a libexpat.a" 78 | static_librarys="${static_librarys} libIlmImf.a libittnotify.a liblibjasper.a liblibjpeg.a liblibprotobuf.a liblibtiff.a liblibwebp.a libopencv_calib3d.a " 79 | static_librarys="${static_librarys} libopencv_calib3d_pch_dephelp.a libopencv_core.a libopencv_core_pch_dephelp.a libopencv_dnn.a libopencv_dnn_pch_dephelp.a libopencv_features2d.a libopencv_features2d_pch_dephelp.a " 80 | static_librarys="${static_librarys} libopencv_flann.a libopencv_flann_pch_dephelp.a libopencv_highgui.a libopencv_highgui_pch_dephelp.a libopencv_imgcodecs.a libopencv_imgcodecs_pch_dephelp.a libopencv_imgproc.a " 81 | static_librarys="${static_librarys} libopencv_imgproc_pch_dephelp.a libopencv_ml.a libopencv_ml_pch_dephelp.a libopencv_objdetect.a libopencv_objdetect_pch_dephelp.a libopencv_perf_calib3d_pch_dephelp.a " 82 | static_librarys="${static_librarys} libopencv_perf_core_pch_dephelp.a libopencv_perf_dnn_pch_dephelp.a libopencv_perf_features2d_pch_dephelp.a libopencv_perf_imgcodecs_pch_dephelp.a libopencv_perf_imgproc_pch_dephelp.a " 83 | static_librarys="${static_librarys} libopencv_perf_objdetect_pch_dephelp.a libopencv_perf_photo_pch_dephelp.a libopencv_perf_stitching_pch_dephelp.a libopencv_perf_superres_pch_dephelp.a libopencv_perf_videoio_pch_dephelp.a " 84 | static_librarys="${static_librarys} libopencv_perf_video_pch_dephelp.a libopencv_photo.a libopencv_photo_pch_dephelp.a libopencv_shape.a libopencv_shape_pch_dephelp.a libopencv_stitching.a libopencv_stitching_pch_dephelp.a " 85 | static_librarys="${static_librarys} libopencv_superres.a libopencv_superres_pch_dephelp.a libopencv_test_calib3d_pch_dephelp.a libopencv_test_core_pch_dephelp.a libopencv_test_dnn_pch_dephelp.a libopencv_test_features2d_pch_dephelp.a " 86 | static_librarys="${static_librarys} libopencv_test_flann_pch_dephelp.a libopencv_test_highgui_pch_dephelp.a libopencv_test_imgcodecs_pch_dephelp.a libopencv_test_imgproc_pch_dephelp.a libopencv_test_ml_pch_dephelp.a " 87 | static_librarys="${static_librarys} libopencv_test_objdetect_pch_dephelp.a libopencv_test_photo_pch_dephelp.a libopencv_test_shape_pch_dephelp.a libopencv_test_stitching_pch_dephelp.a libopencv_test_superres_pch_dephelp.a " 88 | static_librarys="${static_librarys} libopencv_test_videoio_pch_dephelp.a libopencv_test_video_pch_dephelp.a libopencv_test_videostab_pch_dephelp.a libopencv_ts.a libopencv_ts_pch_dephelp.a libopencv_video.a libopencv_videoio.a " 89 | static_librarys="${static_librarys} libopencv_videoio_pch_dephelp.a libopencv_video_pch_dephelp.a libopencv_videostab.a libopencv_videostab_pch_dephelp.a " 90 | 91 | elif [[ "$soc" == "x64" ]] ; then 92 | cross_compiler="g++" 93 | shared_lib_name="./3th/lib/lib3th-${soc}.so" 94 | root_path="./3th/lib/x64/${soc}" 95 | static_librarys="libgio-2.0.a libgmodule-2.0.a libgobject-2.0.a libgthread-2.0.a libglib-2.0.a libffi.a libdbus-1.a libexpat.a libz.a" 96 | elif [[ "$soc" == "hi3518ev200" ]] ; then 97 | cross_compiler= 98 | shared_lib_name= 99 | root_path= 100 | static_librarys= 101 | ERR [$FUNCNAME-$LINENO] "soc: $soc unsupport..." 102 | exit 1 103 | else 104 | ERR [$FUNCNAME-$LINENO] "soc: $soc unsupport..." 105 | exit 1 106 | fi 107 | 108 | 109 | ###################################################################### 110 | # fuctions 111 | ###################################################################### 112 | # 113 | do_create_3th_shared_lib() 114 | { 115 | LOG "./script/create_shared_lib.sh ${soc} ${cross_compiler} ${shared_lib_name} ${root_path} ${static_librarys}" 116 | 117 | ./script/create_shared_lib.sh ${soc} ${cross_compiler} ${shared_lib_name} ${root_path} ${static_librarys} 118 | } 119 | 120 | do_create_3th_shared_lib 121 | 122 | 123 | -------------------------------------------------------------------------------- /scripts/create_shared_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # ANSI COLORS 4 | COLOR_CRE="" 5 | COLOR_NORMAL="" 6 | COLOR_RED="" 7 | COLOR_GREEN="" 8 | COLOR_YELLOW="" 9 | COLOR_BLUE="" 10 | COLOR_MAGENTA="" 11 | COLOR_CYAN="" 12 | COLOR_WHITE="" 13 | 14 | #$1: string 15 | #$2: color 16 | ECHO() 17 | { 18 | [ -n "$2" ] && eval echo -n \"\${${2}}\"; 19 | echo "${1}${COLOR_NORMAL}" 20 | } 21 | 22 | #$1 FUNC-LINE 23 | #$2 string 24 | ERR() 25 | { 26 | echo "${COLOR_RED} ERR${1}: ${2}${COLOR_NORMAL}" >&2 27 | } 28 | 29 | #$1 FUNC-LINE 30 | #$2 string 31 | WARN() 32 | { 33 | echo "${COLOR_YELLOW}WARN${1}: ${2}${COLOR_NORMAL}" >&2 34 | } 35 | 36 | #$1 FUNC-LINE 37 | #$2 string 38 | CMD() 39 | { 40 | echo "${COLOR_GREEN} CMD${1}: ${2}${COLOR_NORMAL}" >&2 41 | } 42 | 43 | # $1: string 44 | LOG() 45 | { 46 | echo "${COLOR_CYAN} LOG: ${1}${COLOR_NORMAL}" >&2 47 | } 48 | 49 | 50 | #$1: string 51 | #$2: level 52 | DEBUG() 53 | { 54 | local level=$2 55 | [ -z "$level" ] && { level=0; } 56 | [ $level -lt $DEBUG_LOG_LEVEL ] && return 0; 57 | 58 | echo "$COLOR_WHITE$1$COLOR_NORMAL" > $DEBUG_LOG_FILE 59 | } 60 | 61 | CUR_DIR=$PWD 62 | 63 | if [ $# -lt 5 ];then 64 | ERR [$FUNCNAME-$LINENO] "usage: $0 [cross_compiler] [shared_lib_name] [root_path] [static_librarys ...]" 65 | CMD [$FUNCNAME-$LINENO] "soc options: [x86/x64/hi3518ev200]" 66 | CMD [$FUNCNAME-$LINENO] "cross_compiler options: [gcc/g++/arm-hisiv400-linux-gcc/arm-hisiv400-linux-g++]" 67 | CMD [$FUNCNAME-$LINENO] "shared_lib_name options: [test.so]" 68 | CMD [$FUNCNAME-$LINENO] "root_path options: [/home/leon/install]" 69 | CMD [$FUNCNAME-$LINENO] "static_librarys options: [libtest.a/libglib-2.0.a]" 70 | exit 1 71 | fi 72 | 73 | soc=$1 74 | cross_compiler=$2 75 | shared_lib_name=$3 76 | root_path=$4 77 | 78 | # 79 | static_libs="$(until [ -z "$5" ]; do echo "$5" ; shift ;done)" 80 | static_libs="`echo ${static_libs} | xargs`" 81 | link_staticlibs=`for i in ${static_libs};do echo $i | sed 's/.*\///g' | sed 's/lib\(.*\)\.a/\-l\1/g' ;done | xargs` 82 | 83 | CMD [$FUNCNAME-$LINENO] "$0 ${cross_compiler} ${shared_lib_name} ${root_path} ${link_staticlibs} " 84 | 85 | cflags="-rdynamic -fPIC -pipe -O3 -Wall " 86 | ld_flags="${new_symbols} -t -z defs -z muldefs -undefined -Bsymbolic -shared " 87 | link_static="-Wl,-Bstatic -Wl,--whole-archive" 88 | link_shared="-Wl,--no-whole-archive -Wl,-Bdynamic" 89 | 90 | 91 | #dynamic library 92 | if [[ "$soc" == "x86" ]] ; then 93 | link_sharedlibs="-lgtk-3 -lgdk-3 -lgdk_pixbuf-2.0 -lcairo -lgstreamer-1.0 -lgstapp-1.0 -lgstriff-1.0 -lgstpbutils-1.0 " 94 | link_sharedlibs="${link_sharedlibs} -lpng -lpthread -ldl -lresolv -lz -lstdc++" 95 | elif [[ "$soc" == "x64" ]] ; then 96 | link_sharedlibs="-lpthread -ldl -lresolv" 97 | else 98 | link_sharedlibs="-lpthread -ldl -lresolv" 99 | fi 100 | ###################################################################### 101 | # fuctions 102 | ###################################################################### 103 | # 104 | do_create_shared_lib() 105 | { 106 | 107 | # Compiled dynamic linked library 108 | CMD [$FUNCNAME-$LINENO] "${cross_compiler} ${cflags} ${ld_flags} -o ${shared_lib_name} -L ${root_path}/lib ${link_static} ${link_staticlibs} ${link_shared} ${link_sharedlibs}" 109 | ${cross_compiler} ${cflags} ${ld_flags} -o ${shared_lib_name} -L ${root_path}/lib ${link_static} ${link_staticlibs} ${link_shared} ${link_sharedlibs} 110 | } 111 | 112 | do_create_shared_lib 113 | -------------------------------------------------------------------------------- /scripts/create_so.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | source ./script/common.sh 4 | 5 | ###################################################################### 6 | # Parameter Verification ${cross-compiler} ${so_name} ${dir} 7 | ###################################################################### 8 | if [ $# -lt 3 ];then 9 | ERR [$FUNCNAME-$LINENO] "usage: $0 [cross-compiler] [so_name] [dir]" 10 | exit 1 11 | fi 12 | 13 | LOG "[cross-compiler]: $1 , [so_name]: $2 , [dir]: $3" 14 | 15 | # cross_compiler 16 | cross_compiler=$1 17 | so_name=$2 18 | dir=$3 19 | 20 | #link libs 21 | LOG "link_libs: `cd ${dir} && ls *.a | sed 's/lib\(.*\)\.a/-l\1/' | xargs `" 22 | 23 | ${cross_compiler} -Wall -fPIC -rdynamic -Wstrict-prototypes -O2 -fomit-frame-pointer \ 24 | -t -z defs -z muldefs -undefined -Bsymbolic -shared -L ${dir} \ 25 | -o ${dir}/${so_name} -Wl,-Bstatic -Wl,--whole-archive `cd ${dir} && ls *.a | sed 's/lib\(.*\)\.a/-l\1/' ` -Wl,--no-whole-archive \ 26 | -Wl,-Bdynamic -lpthread -ldl -lm 27 | -------------------------------------------------------------------------------- /service/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuninChina/MultiTaskApp/8f03ea6c92341b8d7052fdfb746ae9feef7a4466/service/Makefile -------------------------------------------------------------------------------- /util/Makefile: -------------------------------------------------------------------------------- 1 | 2 | obj-y := 3 | 4 | obj-$(CONFIG_PARSON) += parson.o 5 | 6 | --------------------------------------------------------------------------------