From a197443f46e1f9f723a633fff338f2d6d1fd2bb7 Mon Sep 17 00:00:00 2001 From: gozfree Date: Wed, 23 Mar 2022 21:39:50 +0800 Subject: [PATCH 01/16] fix compile on linux --- gear-lib/libbase64/test_libbase64.c | 2 +- gear-lib/libconfig/Makefile | 1 + gear-lib/libfile/libfile.c | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/gear-lib/libbase64/test_libbase64.c b/gear-lib/libbase64/test_libbase64.c index 5237db1..d529f09 100644 --- a/gear-lib/libbase64/test_libbase64.c +++ b/gear-lib/libbase64/test_libbase64.c @@ -35,7 +35,7 @@ int main(int argc, char **argv) memset(target2, 0, sizeof(target2)); ret_bytes = base64_encode(target, source, strlen(source)); - printf("src size: %Iu , return byte: %d , target:%s\n", strlen(source), ret_bytes, target); + printf("src size: %zu , return byte: %d , target:%s\n", strlen(source), ret_bytes, target); ret_bytes = base64_decode(target2, target, ret_bytes); target[ret_bytes]='\0'; diff --git a/gear-lib/libconfig/Makefile b/gear-lib/libconfig/Makefile index 1dfab26..e5aaa9c 100644 --- a/gear-lib/libconfig/Makefile +++ b/gear-lib/libconfig/Makefile @@ -87,6 +87,7 @@ LDFLAGS := $($(ARCH)_LDFLAGS) ifeq ($(ENABLE_LUA), 1) LDFLAGS += `pkg-config --libs lua5.2` endif +LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -lfile ############################################################################### # target diff --git a/gear-lib/libfile/libfile.c b/gear-lib/libfile/libfile.c index 9e9518b..42fd55c 100644 --- a/gear-lib/libfile/libfile.c +++ b/gear-lib/libfile/libfile.c @@ -164,7 +164,7 @@ void file_close(struct file *file) if (!file || !file->ops) { return; } - file->ops->close(file->fd); + file->ops->_close(file->fd); free(file); } @@ -173,7 +173,7 @@ ssize_t file_read(struct file *file, void *data, size_t size) if (!file || !data || size == 0) { return -1; } - return file->ops->read(file->fd, data, size); + return file->ops->_read(file->fd, data, size); } ssize_t file_read_path(const char *path, void *data, size_t size) @@ -193,7 +193,7 @@ ssize_t file_write(struct file *file, const void *data, size_t size) if (!file || !data || size == 0) { return -1; } - return file->ops->write(file->fd, data, size); + return file->ops->_write(file->fd, data, size); } ssize_t file_write_path(const char *path, const void *data, size_t size) From 4826a907c04b13028799a8d194d8a93d98b99df8 Mon Sep 17 00:00:00 2001 From: gozfree Date: Tue, 4 Jan 2022 00:46:35 +0800 Subject: [PATCH 02/16] update to v1.1.20 --- INSTALL.md | 1 - gear-lib/libconfig/test_libconfig.c | 13 ++++++++++++- gear-lib/libfsm/Makefile | 2 +- gear-lib/libgevent/Makefile.nmake | 2 +- gear-lib/libjpeg-ex/libjpeg-ex.h | 1 - gear-lib/liblog/liblog.c | 1 - gear-lib/libmedia-io/video-def.h | 8 ++++---- gear-lib/libposix/libposix_ext.h | 7 +++++++ gear-lib/librpc/test_librpc.c | 2 ++ gear-lib/librtmpc/librtmpc.h | 3 --- gear-lib/librtsp/media_source_live.c | 2 +- gear-lib/librtsp/request_handle.c | 2 +- gear-lib/librtsp/rtp.c | 2 +- gear-lib/libsock/libsock.c | 4 ++-- gear-lib/libuvc/dummy.c | 8 +++++++- gear-lib/libuvc/test_libuvc.c | 4 ++-- gear-lib/libuvc/v4l2.c | 1 - gear-lib/libvector/libvector.h | 3 --- 18 files changed, 41 insertions(+), 25 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 01c3bfe..fe81148 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -28,7 +28,6 @@ `$ sudo apt install liblua5.2-dev libjansson-dev libpulse-dev libx264-dev libavcodec-dev libavformat-dev libavutil-dev protobuf-compiler` * linux platform (>= Ubuntu14.04 >= gcc-4.8.4 32/64 bit) - `$ cd libraries` `$ ./build.sh` `$ sudo ./build.sh install` default debug version,compiler release version diff --git a/gear-lib/libconfig/test_libconfig.c b/gear-lib/libconfig/test_libconfig.c index f2a829e..662ba3a 100644 --- a/gear-lib/libconfig/test_libconfig.c +++ b/gear-lib/libconfig/test_libconfig.c @@ -29,7 +29,10 @@ static int ini_test(void) { struct config *conf = conf_load("ini/example.ini"); - printf("ini_test %s\n", file_path_pwd()); + if (!conf) { + printf("conf_load failed!\n"); + return -1; + } conf_set_string(conf, "wine:year", "1122"); conf_set_string(conf, "wine:aaaa", "ddd"); conf_set_string(conf, "wine:eeee", "1.234"); @@ -47,6 +50,10 @@ static int ini_test(void) static int json_test(void) { struct config *conf = conf_load("json/all.json"); + if (!conf) { + printf("conf_load failed!\n"); + return -1; + } printf("json_test\n"); printf("id = %s\n", conf_get_string(conf, "test", "rgn", 1, "id")); printf("port= %d\n", conf_get_int(conf, "test", "rgn", 1, "port")); @@ -62,6 +69,10 @@ static int lua_test(void) { #ifdef ENABLE_LUA struct config *conf = conf_load("lua/config.lua"); + if (!conf) { + printf("conf_load failed!\n"); + return -1; + } printf("lua_test\n"); printf("[type_3][sub_type_1][my]= %s\n", conf_get_string(conf, "type_3", "sub_type_1", "my")); diff --git a/gear-lib/libfsm/Makefile b/gear-lib/libfsm/Makefile index d225630..1427414 100644 --- a/gear-lib/libfsm/Makefile +++ b/gear-lib/libfsm/Makefile @@ -81,7 +81,7 @@ $(TGT_LIB_SO): $(OBJS_LIB) @ln -sf $(TGT_LIB_SO_VER) $(TGT_LIB_SO) $(TGT_UNIT_TEST): $(OBJS_UNIT_TEST) $(ANDROID_MAIN_OBJ) - $(CC_V) -o $@ $^ $(TGT_LIB_A) $(LDFLAGS) -L$(OUTLIBPATH)/lib/gear-lib -lgevent -ldarray + $(CC_V) -o $@ $^ $(TGT_LIB_A) $(LDFLAGS) -L$(OUTLIBPATH)/lib/gear-lib -lgevent -lthread -ldarray clean: $(RM_V) -f $(OBJS) diff --git a/gear-lib/libgevent/Makefile.nmake b/gear-lib/libgevent/Makefile.nmake index 5b93817..e7130d4 100644 --- a/gear-lib/libgevent/Makefile.nmake +++ b/gear-lib/libgevent/Makefile.nmake @@ -14,7 +14,7 @@ TGT_LIB_A = $(LIBNAME).lib TGT_LIB_SO = $(LIBNAME).dll TGT_UNIT_TEST = test_$(LIBNAME).exe -OBJS_LIB = $(LIBNAME).obj iocp.obj +OBJS_LIB = $(LIBNAME).obj iocp.obj wepoll.obj OBJS_UNIT_TEST = test_$(LIBNAME).obj ############################################################################### diff --git a/gear-lib/libjpeg-ex/libjpeg-ex.h b/gear-lib/libjpeg-ex/libjpeg-ex.h index 7947077..bef4e4e 100644 --- a/gear-lib/libjpeg-ex/libjpeg-ex.h +++ b/gear-lib/libjpeg-ex/libjpeg-ex.h @@ -12,7 +12,6 @@ #include #include #include -#include #include #ifdef __cplusplus diff --git a/gear-lib/liblog/liblog.c b/gear-lib/liblog/liblog.c index ff53dd3..fd045b2 100644 --- a/gear-lib/liblog/liblog.c +++ b/gear-lib/liblog/liblog.c @@ -37,7 +37,6 @@ #include #include -#include #ifndef __CYGWIN__ #include #endif diff --git a/gear-lib/libmedia-io/video-def.h b/gear-lib/libmedia-io/video-def.h index 851e9ac..1bd73a1 100644 --- a/gear-lib/libmedia-io/video-def.h +++ b/gear-lib/libmedia-io/video-def.h @@ -111,12 +111,12 @@ struct video_frame { const char *pixel_format_to_string(enum pixel_format fmt); enum pixel_format pixel_string_to_format(const char *name); -int video_frame_init(struct video_frame *frame, enum pixel_format format, +GEAR_API int video_frame_init(struct video_frame *frame, enum pixel_format format, uint32_t width, uint32_t height, media_mem_type_t type); -struct video_frame *video_frame_create(enum pixel_format format, +GEAR_API struct video_frame *video_frame_create(enum pixel_format format, uint32_t width, uint32_t height, media_mem_type_t type); -void video_frame_destroy(struct video_frame *frame); -struct video_frame *video_frame_copy(struct video_frame *dst, +GEAR_API void video_frame_destroy(struct video_frame *frame); +GEAR_API struct video_frame *video_frame_copy(struct video_frame *dst, const struct video_frame *src); void video_producer_dump(struct video_producer *vp); diff --git a/gear-lib/libposix/libposix_ext.h b/gear-lib/libposix/libposix_ext.h index 1852b79..b80a598 100644 --- a/gear-lib/libposix/libposix_ext.h +++ b/gear-lib/libposix/libposix_ext.h @@ -38,6 +38,13 @@ extern "C" { #define UNLIKELY(x) (x) #endif +#define BUG_ON(condition) \ + do { \ + if (UNLIKELY(condition)) { \ + printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ + exit(); \ + } \ + } while (0) #define MAX_ERRNO (4095) #define IS_ERR_VALUE(x) UNLIKELY((unsigned long)(intptr_t)(void *)(intptr_t)(x) >= (unsigned long)-MAX_ERRNO) diff --git a/gear-lib/librpc/test_librpc.c b/gear-lib/librpc/test_librpc.c index ab26107..c2824e2 100644 --- a/gear-lib/librpc/test_librpc.c +++ b/gear-lib/librpc/test_librpc.c @@ -252,6 +252,8 @@ static void *rpc_client_thread(struct thread *t, void *arg) printf("get connect cnt=%d\n", connect_cnt); break; case 'a': + rpc_get_connect_cnt(r, &connect_cnt); + printf("get connect cnt=%d\n", connect_cnt); rpc_get_connect_list(r, connect_cnt); break; case 'i': diff --git a/gear-lib/librtmpc/librtmpc.h b/gear-lib/librtmpc/librtmpc.h index 3fc4faf..be70736 100644 --- a/gear-lib/librtmpc/librtmpc.h +++ b/gear-lib/librtmpc/librtmpc.h @@ -28,10 +28,7 @@ #include #include #include -#if defined (OS_LINUX) #include -#include -#endif #include "flv_mux.h" diff --git a/gear-lib/librtsp/media_source_live.c b/gear-lib/librtsp/media_source_live.c index 9eeffa0..1f1af92 100644 --- a/gear-lib/librtsp/media_source_live.c +++ b/gear-lib/librtsp/media_source_live.c @@ -31,7 +31,6 @@ #include #define __STDC_FORMAT_MACROS #include -#include #ifdef __cplusplus extern "C" { @@ -350,6 +349,7 @@ static int live_open(struct media_source *ms, const char *name) c->conf.height = 480; c->conf.fps.num = 30; c->conf.fps.den = 1; + c->conf.format = PIXEL_FORMAT_YUY2, c->uvc = uvc_open(UVC_TYPE_V4L2, "/dev/video0", &c->conf); if (!c->uvc) { loge("uvc open failed!\n"); diff --git a/gear-lib/librtsp/request_handle.c b/gear-lib/librtsp/request_handle.c index 4004f7e..e4ed8ac 100644 --- a/gear-lib/librtsp/request_handle.c +++ b/gear-lib/librtsp/request_handle.c @@ -247,7 +247,7 @@ int handle_rtsp_request(struct rtsp_request *req) char url[2*RTSP_PARAM_STRING_MAX]; strcat_url(url, req->url_prefix, req->url_suffix); - logi("rtsp request[%d]:\n==== c >>>> S ====\n%s\n==== c >>>> S ====\n", + logi("rtsp request[%d]:\n==== C >>>> S ====\n%s\n==== C >>>> S ====\n", req->raw->iov_len, req->raw->iov_base); switch (req->cmd[0]) { case 'o': diff --git a/gear-lib/librtsp/rtp.c b/gear-lib/librtsp/rtp.c index c003046..4154665 100644 --- a/gear-lib/librtsp/rtp.c +++ b/gear-lib/librtsp/rtp.c @@ -378,7 +378,7 @@ ssize_t rtp_sendto(struct rtp_socket *s, const char *ip, uint16_t port, const vo default: break; } - logd("sock_sendto[%d] %s:%d ret=%d\n", s->rtp_fd, ip, port, ret); + logi("sock_sendto[%d] %s:%d ret=%d\n", s->rtp_fd, ip, port, ret); return ret; } diff --git a/gear-lib/libsock/libsock.c b/gear-lib/libsock/libsock.c index 7d6f716..0b37169 100644 --- a/gear-lib/libsock/libsock.c +++ b/gear-lib/libsock/libsock.c @@ -879,7 +879,7 @@ int sock_sendto(int fd, const char *ip, uint16_t port, left -= n; continue; } else if (n == 0) { - perror("sendto"); + printf("%s peer connect shutdown\n", __func__); return -1; } if (errno == EINTR || errno == EAGAIN) { @@ -887,7 +887,7 @@ int sock_sendto(int fd, const char *ip, uint16_t port, break; continue; } - perror("sendto"); + printf("%s: sendto failed: %d\n", __func__, errno); return -1; } return (len - left); diff --git a/gear-lib/libuvc/dummy.c b/gear-lib/libuvc/dummy.c index 9688b3d..30cde5f 100644 --- a/gear-lib/libuvc/dummy.c +++ b/gear-lib/libuvc/dummy.c @@ -313,6 +313,12 @@ static int uvc_dummy_query_frame(struct uvc_ctx *uvc, struct video_frame *frame) return 0; } +static int uvc_dummy_ioctl(struct uvc_ctx *uvc, unsigned long int cmd, ...) +{ + printf("uvc_dummy_ioctl unsupport cmd!\n"); + return -1; +} + static void uvc_dummy_close(struct uvc_ctx *uvc) { struct dummy_ctx *c = (struct dummy_ctx *)uvc->opaque; @@ -326,7 +332,7 @@ static void uvc_dummy_close(struct uvc_ctx *uvc) struct uvc_ops dummy_ops = { .open = uvc_dummy_open, .close = uvc_dummy_close, - .ioctl = NULL, + .ioctl = uvc_dummy_ioctl, .start_stream = uvc_dummy_start_stream, .stop_stream = uvc_dummy_stop_stream, .query_frame = uvc_dummy_query_frame, diff --git a/gear-lib/libuvc/test_libuvc.c b/gear-lib/libuvc/test_libuvc.c index 6fb52e1..cf656e0 100644 --- a/gear-lib/libuvc/test_libuvc.c +++ b/gear-lib/libuvc/test_libuvc.c @@ -104,7 +104,7 @@ int dummy_test() {30, 1}, PIXEL_FORMAT_YUY2, }; - struct uvc_ctx *uvc = uvc_open(UVC_TYPE_DUMMY, "sample_yuv422p.yuv", &conf); + struct uvc_ctx *uvc = uvc_open(UVC_TYPE_DUMMY, "sample_320x240_yuv422p.yuv", &conf); if (!uvc) { printf("uvc_open failed!\n"); return -1; @@ -132,6 +132,6 @@ int dummy_test() int main(int argc, char **argv) { v4l2_test(); - //dummy_test(); + dummy_test(); return 0; } diff --git a/gear-lib/libuvc/v4l2.c b/gear-lib/libuvc/v4l2.c index bb2494c..784cf4a 100644 --- a/gear-lib/libuvc/v4l2.c +++ b/gear-lib/libuvc/v4l2.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include diff --git a/gear-lib/libvector/libvector.h b/gear-lib/libvector/libvector.h index 0033bf1..ed70f6b 100644 --- a/gear-lib/libvector/libvector.h +++ b/gear-lib/libvector/libvector.h @@ -24,10 +24,7 @@ #include #include -#if defined (OS_LINUX) #include -#include -#endif #define LIBVECTOR_VERSION "0.1.0" From e650efa1a1c8990eefeee6ba67cc420addcc5b0a Mon Sep 17 00:00:00 2001 From: gozfree Date: Sat, 26 Mar 2022 17:00:12 +0800 Subject: [PATCH 03/16] fix compile on msvc, cleanup unistd.h dependency --- .../gear-lib/libposix/libposix.vcxproj | 5 +- .../libposix/libposix.vcxproj.filters | 5 +- gear-lib/libfile/io.c | 2 - gear-lib/libgevent/test_libgevent.c | 2 - gear-lib/libmedia-io/video-def.c | 2 - gear-lib/libposix/Makefile | 2 +- gear-lib/libposix/Makefile.nmake | 2 +- gear-lib/libposix/libposix.h | 180 ++++++++++++++--- gear-lib/libposix/libposix4apple.h | 37 ++++ gear-lib/libposix/libposix4linux.h | 79 ++++++++ gear-lib/libposix/libposix4roid.h | 36 ++++ gear-lib/libposix/libposix4rtos.h | 1 + gear-lib/libposix/libposix4rtthread.h | 3 + gear-lib/libposix/libposix4win.h | 24 +-- gear-lib/libposix/libposix_ext.h | 181 ------------------ gear-lib/libposix/test_libposix.c | 22 ++- gear-lib/libposix/test_libposix4win.c | 50 ----- gear-lib/libtime/libtime.c | 4 - gear-lib/libtime/test_libtime.c | 2 - 19 files changed, 331 insertions(+), 308 deletions(-) create mode 100644 gear-lib/libposix/libposix4apple.h create mode 100644 gear-lib/libposix/libposix4linux.h create mode 100644 gear-lib/libposix/libposix4roid.h delete mode 100644 gear-lib/libposix/libposix_ext.h delete mode 100644 gear-lib/libposix/test_libposix4win.c diff --git a/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj b/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj index 73e46ea..5028f80 100644 --- a/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj +++ b/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj @@ -11,12 +11,9 @@ - - - @@ -115,7 +112,7 @@ - + {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} diff --git a/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj.filters b/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj.filters index 707cbb5..6aaadb4 100644 --- a/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj.filters +++ b/build/visual studio 2010/gear-lib/libposix/libposix.vcxproj.filters @@ -135,11 +135,8 @@ pthreads4w - - - @@ -310,6 +307,6 @@ - + \ No newline at end of file diff --git a/gear-lib/libfile/io.c b/gear-lib/libfile/io.c index be730c1..d6cf86c 100644 --- a/gear-lib/libfile/io.c +++ b/gear-lib/libfile/io.c @@ -23,13 +23,11 @@ #include #include #include -#if defined (OS_LINUX) || defined (OS_APPLE) #include #include #include #include #include -#endif #define MAX_RETRY_CNT (3) diff --git a/gear-lib/libgevent/test_libgevent.c b/gear-lib/libgevent/test_libgevent.c index 87653c9..88c501d 100644 --- a/gear-lib/libgevent/test_libgevent.c +++ b/gear-lib/libgevent/test_libgevent.c @@ -21,12 +21,10 @@ ******************************************************************************/ #include "libgevent.h" #include -#if defined (OS_LINUX) || defined (OS_APPLE) #include #if defined (OS_LINUX) #include #endif -#endif #include struct gevent_base *evbase = NULL; diff --git a/gear-lib/libmedia-io/video-def.c b/gear-lib/libmedia-io/video-def.c index 5273ed0..d305068 100644 --- a/gear-lib/libmedia-io/video-def.c +++ b/gear-lib/libmedia-io/video-def.c @@ -24,10 +24,8 @@ #include #include #include -#if defined (OS_LINUX) #define __STDC_FORMAT_MACROS #include -#endif #define ALIGNMENT 32 #define ALIGN_SIZE(size, align) (((size) + (align - 1)) & (~(align - 1))) diff --git a/gear-lib/libposix/Makefile b/gear-lib/libposix/Makefile index 21c0564..3f0b08a 100644 --- a/gear-lib/libposix/Makefile +++ b/gear-lib/libposix/Makefile @@ -25,7 +25,7 @@ LIBNAME = libposix VER_TAG = $(shell echo ${LIBNAME} | tr 'a-z' 'A-Z') VER = $(shell awk '/'"${VER_TAG}_VERSION"'/{print $$3}' ${LIBNAME}.h) TGT_LIB_H = $(LIBNAME).h -TGT_LIB_H += kernel_list.h libposix_ext.h +TGT_LIB_H += kernel_list.h ifeq ($(ARCH), win) TGT_LIB_H += libposix4win.h kernel_list_win32.h endif diff --git a/gear-lib/libposix/Makefile.nmake b/gear-lib/libposix/Makefile.nmake index 55300bd..d7a6d0b 100644 --- a/gear-lib/libposix/Makefile.nmake +++ b/gear-lib/libposix/Makefile.nmake @@ -14,7 +14,7 @@ TGT_LIB_SO = $(LIBNAME).dll TGT_UNIT_TEST = test_$(LIBNAME).exe OBJS_LIB = $(LIBNAME).obj libposix4win.obj -OBJS_UNIT_TEST = test_libposix4win.obj +OBJS_UNIT_TEST = test_$(LIBNAME).obj ############################################################################### # cflags and ldflags diff --git a/gear-lib/libposix/libposix.h b/gear-lib/libposix/libposix.h index ac36768..a2500c0 100644 --- a/gear-lib/libposix/libposix.h +++ b/gear-lib/libposix/libposix.h @@ -26,10 +26,6 @@ extern "C" { #endif -#include -#include -#include - #define LIBPOSIX_VERSION "0.1.1" /****************************************************************************** @@ -37,16 +33,8 @@ extern "C" { ******************************************************************************/ #if defined (__linux__) || defined (__CYGWIN__) #define OS_LINUX -#include -#include -#define __STDC_FORMAT_MACROS -#include -#include -#include - -#include "kernel_list.h" - #define GEAR_API __attribute__((visibility("default"))) +#include "libposix4linux.h" /****************************************************************************** * OS_WINDOWS @@ -54,50 +42,190 @@ extern "C" { #elif defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER) #define OS_WINDOWS #define GEAR_API __declspec(dllexport) - #include "libposix4win.h" -#include "kernel_list_win32.h" - /****************************************************************************** * OS_APPLE ******************************************************************************/ #elif defined (__APPLE__) #define OS_APPLE -#include -#include -#include "kernel_list.h" #define GEAR_API +#include "libposix4apple.h" /****************************************************************************** * OS_ANDROID ******************************************************************************/ #elif defined (__ANDROID__) #define OS_ANDROID -#include #define GEAR_API +#include "libposix4roid.h" /****************************************************************************** * OS_RTOS ******************************************************************************/ #elif defined (FREERTOS) || defined (THREADX) #define OS_RTOS -#include "libposix4rtos.h" -#include "kernel_list.h" #define GEAR_API +#include "libposix4rtos.h" #elif defined (RT_USING_POSIX) #define OS_RTTHREAD -#include -#include "libposix4rtthread.h" -#include "kernel_list.h" #define GEAR_API +#include "libposix4rtthread.h" #else #error "OS_UNDEFINED" #endif -#include "libposix_ext.h" + +/****************************************************************************** + * BASIC MACRO DEFINES + ******************************************************************************/ + +#ifdef __GNUC__ +#define LIKELY(x) (__builtin_expect(!!(x), 1)) +#define UNLIKELY(x) (__builtin_expect(!!(x), 0)) +#else +#define LIKELY(x) (x) +#define UNLIKELY(x) (x) +#endif + +#define BUG_ON(condition) \ + do { \ + if (UNLIKELY(condition)) { \ + printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ + exit(); \ + } \ + } while (0) + +#define MAX_ERRNO (4095) +#define IS_ERR_VALUE(x) UNLIKELY((unsigned long)(intptr_t)(void *)(intptr_t)(x) >= (unsigned long)-MAX_ERRNO) + +static inline void *ERR_PTR(long error) +{ + return (void *) (intptr_t)error; +} + +static inline long PTR_ERR(const void *ptr) +{ + return (long) (intptr_t)ptr; +} + +static inline bool IS_ERR(const void *ptr) +{ + return IS_ERR_VALUE((unsigned long)(intptr_t)ptr); +} + +static inline bool IS_ERR_OR_NULL(const void *ptr) +{ + return UNLIKELY(!ptr) || IS_ERR_VALUE((unsigned long)(intptr_t)ptr); +} + +/** + * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type + * @ptr: The pointer to cast. + * + * Explicitly cast an error-valued pointer to another pointer type in such a + * way as to make it clear that's what's going on. + */ +static inline void * ERR_CAST(const void *ptr) +{ + /* cast away the const */ + return (void *) ptr; +} + +static inline int PTR_ERR_OR_ZERO(const void *ptr) +{ + if (IS_ERR(ptr)) + return PTR_ERR(ptr); + else + return 0; +} + +/****************************************************************************** + * MACRO DEFINES ARE UPPERCASE + ******************************************************************************/ +typedef struct rational { + int num; + int den; +} rational_t; + +/** + * Variable-argument unused annotation + */ +#define UNUSED(e, ...) (void) ((void) (e), ##__VA_ARGS__) + +#define SWAP(a, b) \ + do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) + +#define MIN2(a, b) ((a) > (b) ? (b) : (a)) +#define MAX2(a, b) ((a) > (b) ? (a) : (b)) +#define ABS(x) ((x) >= 0 ? (x) : -(x)) + +#define CALLOC(size, type) (type *) calloc(size, sizeof(type)) +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +#define VERBOSE() \ + do { \ + printf("%s:%s:%d xxxxxx\n", __FILE__, __func__, __LINE__); \ + } while (0) + +#define DUMP_BUFFER(buf, len) \ + do { \ + int _i, _j=0; \ + char _tmp[128] = {0}; \ + if (buf == NULL || len <= 0) { \ + break; \ + } \ + for (_i = 0; _i < len; _i++) { \ + if (!(_i%16)) { \ + if (_i != 0) { \ + printf("%s", _tmp); \ + } \ + memset(_tmp, 0, sizeof(_tmp)); \ + _j = 0; \ + _j += snprintf(_tmp+_j, 64, "\n%p: ", buf+_i); \ + } \ + _j += snprintf(_tmp+_j, 4, "%02hhx ", *((char *)buf + _i)); \ + } \ + printf("%s\n", _tmp); \ + } while (0) + +#define ALIGN2(x, a) (((x) + (a) - 1) & ~((a) - 1)) + +#define is_alpha(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) + +/** + * Compile-time strlen(3) + * XXX: Should only used for `char[]' NOT `char *' + * Assume string ends with null byte('\0') + */ +#define STRLEN(s) (sizeof(s) - 1) + +/** + * Compile-time assurance see: linux/arch/x86/boot/boot.h + * Will fail build if condition yield true + */ +#ifndef BUILD_BUG_ON +#if defined (OS_WINDOWS) +/* + * MSVC compiler allows negative array size(treat as unsigned value) + * yet them don't allow zero-size array + */ +#define BUILD_BUG_ON(cond) ((void) sizeof(char[!(cond)])) +#else +#define BUILD_BUG_ON(cond) ((void) sizeof(char[1 - 2 * !!(cond)])) +#endif +#endif + +GEAR_API void *memdup(const void *src, size_t len); +GEAR_API struct iovec *iovec_create(size_t len); +GEAR_API void iovec_destroy(struct iovec *); + +GEAR_API bool is_little_endian(void); + +GEAR_API int get_proc_name(char *name, size_t len); + #ifdef __cplusplus } diff --git a/gear-lib/libposix/libposix4apple.h b/gear-lib/libposix/libposix4apple.h new file mode 100644 index 0000000..0c83b0e --- /dev/null +++ b/gear-lib/libposix/libposix4apple.h @@ -0,0 +1,37 @@ +/****************************************************************************** + * Copyright (C) 2014-2020 Zhifeng Gong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ +#ifndef LIBPOSIX4APPLE_H +#define LIBPOSIX4APPLE_H + +#include +#include +#include "kernel_list.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/gear-lib/libposix/libposix4linux.h b/gear-lib/libposix/libposix4linux.h new file mode 100644 index 0000000..2abc6ee --- /dev/null +++ b/gear-lib/libposix/libposix4linux.h @@ -0,0 +1,79 @@ +/****************************************************************************** + * Copyright (C) 2014-2020 Zhifeng Gong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ +#ifndef LIBPOSIX4LINUX_H +#define LIBPOSIX4LINUX_H + +#include +#include +#include +#include + +#include "kernel_list.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/****************************************************************************** + * basic types + ******************************************************************************/ + +/****************************************************************************** + * I/O string APIs + ******************************************************************************/ + +/****************************************************************************** + * sys file APIs + ******************************************************************************/ + +/****************************************************************************** + * pthread APIs + ******************************************************************************/ + +/****************************************************************************** + * time APIs + ******************************************************************************/ + +/****************************************************************************** + * driver IOC APIs + ******************************************************************************/ + +/****************************************************************************** + * socket APIs + ******************************************************************************/ + +/****************************************************************************** + * system APIs + ******************************************************************************/ +#define pipe_read read +#define pipe_write write + +/****************************************************************************** + * memory APIs + ******************************************************************************/ + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/gear-lib/libposix/libposix4roid.h b/gear-lib/libposix/libposix4roid.h new file mode 100644 index 0000000..7dde2f6 --- /dev/null +++ b/gear-lib/libposix/libposix4roid.h @@ -0,0 +1,36 @@ +/****************************************************************************** + * Copyright (C) 2014-2020 Zhifeng Gong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ +#ifndef LIBPOSIX4ROID_H +#define LIBPOSIX4ROID_H + +#include +#include "kernel_list.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/gear-lib/libposix/libposix4rtos.h b/gear-lib/libposix/libposix4rtos.h index 8cc9800..9973fdc 100644 --- a/gear-lib/libposix/libposix4rtos.h +++ b/gear-lib/libposix/libposix4rtos.h @@ -31,6 +31,7 @@ #include "freertos/semphr.h" #include "freertos/event_groups.h" #endif +#include "kernel_list.h" #ifdef __cplusplus diff --git a/gear-lib/libposix/libposix4rtthread.h b/gear-lib/libposix/libposix4rtthread.h index 28c09c5..32a7776 100644 --- a/gear-lib/libposix/libposix4rtthread.h +++ b/gear-lib/libposix/libposix4rtthread.h @@ -22,6 +22,9 @@ #ifndef LIBPOSIX4RTTHREAD_H #define LIBPOSIX4RTTHREAD_H +#include +#include "kernel_list.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/gear-lib/libposix/libposix4win.h b/gear-lib/libposix/libposix4win.h index 53a1d77..b8ae0cb 100644 --- a/gear-lib/libposix/libposix4win.h +++ b/gear-lib/libposix/libposix4win.h @@ -27,19 +27,14 @@ #include #include #include +#include #include #include -#include -#include #include -#include #include -#include -#include -#include -#include #include "pthreads4w/pthread.h" #include "pthreads4w/semaphore.h" +#include "kernel_list_win32.h" #ifdef __cplusplus extern "C" { @@ -48,10 +43,6 @@ extern "C" { /****************************************************************************** * basic types ******************************************************************************/ -#define false 0 -#define true 1 -#define bool int - #define inline __inline #define __func__ __FUNCTION__ @@ -76,22 +67,11 @@ GEAR_API char *dup_wchar_to_utf8(wchar_t *w); /****************************************************************************** * sys file APIs ******************************************************************************/ -#define STDIN_FILENO 0 /* standard input file descriptor */ -#define STDOUT_FILENO 1 /* standard output file descriptor */ -#define STDERR_FILENO 2 /* standard error file descriptor */ #define MAXPATHLEN 1024 -#ifndef PATH_MAX -#define PATH_MAX 4096 -#endif - -#ifndef _MODE_T_ -typedef int mode_t; -#endif /****************************************************************************** * pthread APIs ******************************************************************************/ - #define gettid GetCurrentThreadId diff --git a/gear-lib/libposix/libposix_ext.h b/gear-lib/libposix/libposix_ext.h deleted file mode 100644 index b80a598..0000000 --- a/gear-lib/libposix/libposix_ext.h +++ /dev/null @@ -1,181 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2014-2020 Zhifeng Gong - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - ******************************************************************************/ -#ifndef GEAR_MISC_H -#define GEAR_MISC_H - -#ifdef __cplusplus -extern "C" { -#endif - -/****************************************************************************** - * BASIC MACRO DEFINES - ******************************************************************************/ - -#ifdef __GNUC__ -#define LIKELY(x) (__builtin_expect(!!(x), 1)) -#define UNLIKELY(x) (__builtin_expect(!!(x), 0)) -#else -#define LIKELY(x) (x) -#define UNLIKELY(x) (x) -#endif - -#define BUG_ON(condition) \ - do { \ - if (UNLIKELY(condition)) { \ - printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ - exit(); \ - } \ - } while (0) - -#define MAX_ERRNO (4095) -#define IS_ERR_VALUE(x) UNLIKELY((unsigned long)(intptr_t)(void *)(intptr_t)(x) >= (unsigned long)-MAX_ERRNO) - -static inline void *ERR_PTR(long error) -{ - return (void *) (intptr_t)error; -} - -static inline long PTR_ERR(const void *ptr) -{ - return (long) (intptr_t)ptr; -} - -static inline bool IS_ERR(const void *ptr) -{ - return IS_ERR_VALUE((unsigned long)(intptr_t)ptr); -} - -static inline bool IS_ERR_OR_NULL(const void *ptr) -{ - return UNLIKELY(!ptr) || IS_ERR_VALUE((unsigned long)(intptr_t)ptr); -} - -/** - * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type - * @ptr: The pointer to cast. - * - * Explicitly cast an error-valued pointer to another pointer type in such a - * way as to make it clear that's what's going on. - */ -static inline void * ERR_CAST(const void *ptr) -{ - /* cast away the const */ - return (void *) ptr; -} - -static inline int PTR_ERR_OR_ZERO(const void *ptr) -{ - if (IS_ERR(ptr)) - return PTR_ERR(ptr); - else - return 0; -} - -/****************************************************************************** - * MACRO DEFINES ARE UPPERCASE - ******************************************************************************/ -typedef struct rational { - int num; - int den; -} rational_t; - -/** - * Variable-argument unused annotation - */ -#define UNUSED(e, ...) (void) ((void) (e), ##__VA_ARGS__) - -#define SWAP(a, b) \ - do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) - -#define MIN2(a, b) ((a) > (b) ? (b) : (a)) -#define MAX2(a, b) ((a) > (b) ? (a) : (b)) -#define ABS(x) ((x) >= 0 ? (x) : -(x)) - -#define CALLOC(size, type) (type *) calloc(size, sizeof(type)) -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -#define VERBOSE() \ - do { \ - printf("%s:%s:%d xxxxxx\n", __FILE__, __func__, __LINE__); \ - } while (0) - -#define DUMP_BUFFER(buf, len) \ - do { \ - int _i, _j=0; \ - char _tmp[128] = {0}; \ - if (buf == NULL || len <= 0) { \ - break; \ - } \ - for (_i = 0; _i < len; _i++) { \ - if (!(_i%16)) { \ - if (_i != 0) { \ - printf("%s", _tmp); \ - } \ - memset(_tmp, 0, sizeof(_tmp)); \ - _j = 0; \ - _j += snprintf(_tmp+_j, 64, "\n%p: ", buf+_i); \ - } \ - _j += snprintf(_tmp+_j, 4, "%02hhx ", *((char *)buf + _i)); \ - } \ - printf("%s\n", _tmp); \ - } while (0) - -#define ALIGN2(x, a) (((x) + (a) - 1) & ~((a) - 1)) - -#define is_alpha(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) - -/** - * Compile-time strlen(3) - * XXX: Should only used for `char[]' NOT `char *' - * Assume string ends with null byte('\0') - */ -#define STRLEN(s) (sizeof(s) - 1) - -/** - * Compile-time assurance see: linux/arch/x86/boot/boot.h - * Will fail build if condition yield true - */ -#ifndef BUILD_BUG_ON -#if defined (OS_WINDOWS) -/* - * MSVC compiler allows negative array size(treat as unsigned value) - * yet them don't allow zero-size array - */ -#define BUILD_BUG_ON(cond) ((void) sizeof(char[!(cond)])) -#else -#define BUILD_BUG_ON(cond) ((void) sizeof(char[1 - 2 * !!(cond)])) -#endif -#endif - -GEAR_API void *memdup(const void *src, size_t len); -GEAR_API struct iovec *iovec_create(size_t len); -GEAR_API void iovec_destroy(struct iovec *); - -GEAR_API bool is_little_endian(void); - -GEAR_API int get_proc_name(char *name, size_t len); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/gear-lib/libposix/test_libposix.c b/gear-lib/libposix/test_libposix.c index c352786..9261b52 100644 --- a/gear-lib/libposix/test_libposix.c +++ b/gear-lib/libposix/test_libposix.c @@ -21,16 +21,12 @@ ******************************************************************************/ #include "libposix.h" #include -#include -#include -#if defined (OS_LINUX) || defined (OS_APPLE) -#define __STDC_FORMAT_MACROS -#include #include #include -#include +#include +#define __STDC_FORMAT_MACROS +#include #include -#endif static void *thread_func(void *arg) { @@ -40,6 +36,9 @@ static void *thread_func(void *arg) void foo() { + int ret; + char buf[10]; + int fds[2]; pthread_t tid; char proc[128] = {0}; const char *file = "version.sh"; @@ -56,6 +55,15 @@ void foo() sleep(1); pthread_join(tid, NULL); + + memset(buf, 0, sizeof(buf)); + ret = pipe(fds); + printf("pipe ret=%d\n", ret); + printf("fds[0]=%d, fds[1]=%d\n", fds[0], fds[1]); + ret = pipe_write(fds[1], "aaa", 3); + printf("write %d ret = %d, errno=%d\n", fds[1], ret, errno); + pipe_read(fds[0], buf, 5); + printf("read pipe buf = %s\n", buf); } int main(int argc, char **argv) diff --git a/gear-lib/libposix/test_libposix4win.c b/gear-lib/libposix/test_libposix4win.c deleted file mode 100644 index d1dfdf5..0000000 --- a/gear-lib/libposix/test_libposix4win.c +++ /dev/null @@ -1,50 +0,0 @@ - -#include "libposix.h" -#include -#include - -static void *thread_func(void *arg) -{ - printf("this is thread\n"); - return NULL; -} - -void foo() -{ - int ret; - int written = 0; - char buf[10]; - int fds[2]; - pthread_t tid; - char proc[128] = {0}; - const char *file = "Makefile"; - struct stat st; - memset(&st, 0, sizeof(st)); - if (0 > stat(file, &st)) { - printf("stat %s failed\n", file); - } - printf("%s size=%d\n", file, st.st_size); - get_proc_name(proc, sizeof(proc)); - printf("proc name = %s\n", proc); - - pthread_create(&tid, NULL, thread_func, NULL); - sleep(1); - - - memset(buf, 0, sizeof(buf)); - ret = pipe(fds); - printf("pipe ret=%d\n", ret); - printf("fds[0]=%d, fds[1]=%d\n", fds[0], fds[1]); - ret = pipe_write(fds[1], "aaa", 3); - printf("write %d ret = %d, errno=%d\n", fds[1], ret, errno); - pipe_read(fds[0], buf, 5); - printf("read pipe buf = %s\n", buf); - pthread_join(tid, NULL); -} - -int main(int argc, char **argv) -{ - foo(); - sleep(10); - return 0; -} \ No newline at end of file diff --git a/gear-lib/libtime/libtime.c b/gear-lib/libtime/libtime.c index bf107f8..08c6c65 100644 --- a/gear-lib/libtime/libtime.c +++ b/gear-lib/libtime/libtime.c @@ -170,15 +170,11 @@ char *time_now_msec_str(char *str, int len) int time_sleep_ms(uint64_t ms) { -#if defined (OS_LINUX) struct timeval tv; tv.tv_sec = 0; tv.tv_usec = ms*1000; return select(0, NULL, NULL, NULL, &tv); -#elif defined (OS_WINDOWS) - usleep(ms*1000); return 0; -#endif } int time_info_by_utc(uint32_t utc, struct time_info *ti) diff --git a/gear-lib/libtime/test_libtime.c b/gear-lib/libtime/test_libtime.c index e7aa15a..e9e609f 100644 --- a/gear-lib/libtime/test_libtime.c +++ b/gear-lib/libtime/test_libtime.c @@ -24,9 +24,7 @@ #include #include #define __STDC_FORMAT_MACROS -#if defined (OS_LINUX) || defined (OS_APPLE) #include -#endif void foo() { From 35dceea2970aceeba1e5b4bdadc0112840f58d09 Mon Sep 17 00:00:00 2001 From: gozfree Date: Sat, 26 Mar 2022 20:24:25 +0800 Subject: [PATCH 04/16] cleanup with OS_LINUX --- gear-lib/libfile/fio.c | 2 -- gear-lib/libfile/libfile.c | 20 +++----------------- gear-lib/libfile/test_libfile.c | 3 --- gear-lib/libhash/libhash.c | 2 ++ gear-lib/libhash/test_libhash.c | 2 +- gear-lib/liblog/liblog.c | 12 ++++++------ gear-lib/liblog/test_liblog.c | 2 -- gear-lib/libmedia-io/Makefile | 2 +- gear-lib/libposix/Makefile | 5 +++-- gear-lib/libqueue/libqueue.c | 4 ++-- gear-lib/libqueue/libqueue.h | 2 -- gear-lib/libtime/libtime.c | 6 +----- gear-lib/libworkq/test_libworkq.c | 2 -- 13 files changed, 19 insertions(+), 45 deletions(-) diff --git a/gear-lib/libfile/fio.c b/gear-lib/libfile/fio.c index 3a0f980..8f31e86 100644 --- a/gear-lib/libfile/fio.c +++ b/gear-lib/libfile/fio.c @@ -24,9 +24,7 @@ #include #include #include -#if defined (OS_LINUX) #include -#endif #define MAX_RETRY_CNT (3) diff --git a/gear-lib/libfile/libfile.c b/gear-lib/libfile/libfile.c index 42fd55c..3460c58 100644 --- a/gear-lib/libfile/libfile.c +++ b/gear-lib/libfile/libfile.c @@ -401,7 +401,6 @@ int file_dir_create(const char *path) int dfs_remove_dir(const char *path) { -#if defined (OS_LINUX) DIR *pdir = NULL; struct dirent *ent = NULL; char full_path[PATH_MAX]; @@ -420,7 +419,7 @@ int dfs_remove_dir(const char *path) continue; } memset(full_path, 0, sizeof(full_path)); - sprintf(full_path, "%s/%s", path, ent->d_name); + snprintf(full_path, sizeof(full_path), "%s/%s", path, ent->d_name); if (ent->d_type == DT_DIR) { ret = dfs_remove_dir(full_path); if (ret != 0) { @@ -431,9 +430,6 @@ int dfs_remove_dir(const char *path) } closedir(pdir); return ret; -#else - return 0; -#endif } int file_dir_remove(const char *path) @@ -444,7 +440,6 @@ int file_dir_remove(const char *path) int file_dir_tree(const char *path) { -#if defined (OS_LINUX) DIR *pdir = NULL; struct dirent *ent = NULL; char full_path[PATH_MAX]; @@ -461,7 +456,7 @@ int file_dir_tree(const char *path) } while (NULL != (ent = readdir(pdir))) { memset(full_path, 0, sizeof(full_path)); - sprintf(full_path, "%s/%s", path, ent->d_name); + snprintf(full_path, sizeof(full_path), "%s/%s", path, ent->d_name); if (ent->d_type == DT_DIR) { if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { continue; @@ -476,13 +471,11 @@ int file_dir_tree(const char *path) } } closedir(pdir); -#endif return 0; } int dfs_dir_size(const char *path, uint64_t *size) { -#if defined (OS_LINUX) DIR *pdir = NULL; struct dirent *ent = NULL; char full_path[PATH_MAX]; @@ -498,7 +491,7 @@ int dfs_dir_size(const char *path, uint64_t *size) } while (NULL != (ent = readdir(pdir))) { memset(full_path, 0, sizeof(full_path)); - sprintf(full_path, "%s/%s", path, ent->d_name); + snprintf(full_path, sizeof(full_path), "%s/%s", path, ent->d_name); if (ent->d_type == DT_DIR) { if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { continue; @@ -513,7 +506,6 @@ int dfs_dir_size(const char *path, uint64_t *size) } } closedir(pdir); -#endif return 0; } @@ -525,7 +517,6 @@ int file_dir_size(const char *path, uint64_t *size) int file_num_in_dir(const char *path) { -#if defined (OS_LINUX) if (!path) { return -1; } @@ -544,14 +535,10 @@ int file_num_in_dir(const char *path) } closedir(dir); return num; -#else - return 0; -#endif } int file_get_info(const char *path, struct file_info *info) { -#if defined (OS_LINUX) struct stat st; if (-1 == stat(path, &st)) { printf("stat %s failed!\n", path); @@ -580,6 +567,5 @@ int file_get_info(const char *path, struct file_info *info) info->size = st.st_size; info->access_sec = st.st_atim.tv_sec; info->modify_sec = st.st_ctim.tv_sec;//using change, not modify -#endif return 0; } diff --git a/gear-lib/libfile/test_libfile.c b/gear-lib/libfile/test_libfile.c index 158318e..a331f1e 100644 --- a/gear-lib/libfile/test_libfile.c +++ b/gear-lib/libfile/test_libfile.c @@ -28,13 +28,10 @@ #include #include #include -#if defined (OS_LINUX) || defined (OS_APPLE) #define __STDC_FORMAT_MACROS #include -#endif #include - static void foo(void) { int len = 0; diff --git a/gear-lib/libhash/libhash.c b/gear-lib/libhash/libhash.c index 3ad4ce6..816d831 100644 --- a/gear-lib/libhash/libhash.c +++ b/gear-lib/libhash/libhash.c @@ -24,6 +24,8 @@ #include #include #include +#define __STDC_FORMAT_MACROS +#include /* * [opaque_list] * [bucket0] -> item[1] -> item[2] -> ... -> item[m0] diff --git a/gear-lib/libhash/test_libhash.c b/gear-lib/libhash/test_libhash.c index 9c5b66b..ff28ae7 100644 --- a/gear-lib/libhash/test_libhash.c +++ b/gear-lib/libhash/test_libhash.c @@ -23,7 +23,7 @@ #include #include #include -#include +#include #define PALIGN "%15s: %6.4f sec\n" //#define NKEYS 1024*1024 diff --git a/gear-lib/liblog/liblog.c b/gear-lib/liblog/liblog.c index fd045b2..ea6729b 100644 --- a/gear-lib/liblog/liblog.c +++ b/gear-lib/liblog/liblog.c @@ -32,19 +32,19 @@ #include #include #include -#if defined (OS_LINUX) || defined (OS_APPLE) #include - #include -#include -#ifndef __CYGWIN__ -#include -#endif #include #include #include #include +#if defined (OS_LINUX) || defined (OS_APPLE) +#include +#ifndef __CYGWIN__ +#include +#endif + #define USE_SYSLOG #elif defined (OS_ANDROID) diff --git a/gear-lib/liblog/test_liblog.c b/gear-lib/liblog/test_liblog.c index fafc166..7e144c2 100644 --- a/gear-lib/liblog/test_liblog.c +++ b/gear-lib/liblog/test_liblog.c @@ -23,9 +23,7 @@ #include "liblog.h" #include #include -#ifdef OS_LINUX #include -#endif static void test_no_init(void) { diff --git a/gear-lib/libmedia-io/Makefile b/gear-lib/libmedia-io/Makefile index 8093f5d..8f538f8 100644 --- a/gear-lib/libmedia-io/Makefile +++ b/gear-lib/libmedia-io/Makefile @@ -22,7 +22,7 @@ RM_V ?= $(RM) # target and object ############################################################################### LIBNAME = libmedia-io -VER_TAG = $(shell echo ${LIBNAME} | tr 'a-z' 'A-Z') +VER_TAG = LIBMEDIA_IO VER = $(shell awk '/'"${VER_TAG}_VERSION"'/{print $$3}' ${LIBNAME}.h) TGT_LIB_H = $(LIBNAME).h audio-def.h video-def.h TGT_LIB_A = $(LIBNAME).a diff --git a/gear-lib/libposix/Makefile b/gear-lib/libposix/Makefile index 3f0b08a..6eea335 100644 --- a/gear-lib/libposix/Makefile +++ b/gear-lib/libposix/Makefile @@ -25,8 +25,9 @@ LIBNAME = libposix VER_TAG = $(shell echo ${LIBNAME} | tr 'a-z' 'A-Z') VER = $(shell awk '/'"${VER_TAG}_VERSION"'/{print $$3}' ${LIBNAME}.h) TGT_LIB_H = $(LIBNAME).h -TGT_LIB_H += kernel_list.h -ifeq ($(ARCH), win) +ifeq ($(ARCH), linux) +TGT_LIB_H += libposix4linux.h kernel_list.h +else ifeq ($(ARCH), win) TGT_LIB_H += libposix4win.h kernel_list_win32.h endif TGT_LIB_A = $(LIBNAME).a diff --git a/gear-lib/libqueue/libqueue.c b/gear-lib/libqueue/libqueue.c index 71832fa..f7fbe24 100644 --- a/gear-lib/libqueue/libqueue.c +++ b/gear-lib/libqueue/libqueue.c @@ -19,15 +19,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ +#include "libqueue.h" #include #include #include #include #include -#include "libqueue.h" -#if defined (OS_LINUX) || defined (OS_APPLE) #include #include +#if defined (OS_LINUX) || defined (OS_APPLE) #include #endif diff --git a/gear-lib/libqueue/libqueue.h b/gear-lib/libqueue/libqueue.h index 1d7f597..c3de2ce 100644 --- a/gear-lib/libqueue/libqueue.h +++ b/gear-lib/libqueue/libqueue.h @@ -23,9 +23,7 @@ #define LIBQUEUE_H #include -#if defined (OS_LINUX) || defined (OS_RTTHREAD) || defined (OS_APPLE) #include -#endif #define LIBQUEUE_VERSION "0.2.2" diff --git a/gear-lib/libtime/libtime.c b/gear-lib/libtime/libtime.c index 08c6c65..b3dd6d4 100644 --- a/gear-lib/libtime/libtime.c +++ b/gear-lib/libtime/libtime.c @@ -25,8 +25,8 @@ #include #include #include -#if defined (OS_LINUX) || defined (OS_APPLE) #include +#if defined (OS_LINUX) || defined (OS_APPLE) #include #endif @@ -112,16 +112,12 @@ uint64_t time_now_usec(struct timeval *val) static uint64_t _time_clock_gettime(clockid_t clk_id) { -#if defined (OS_LINUX) || defined (OS_APPLE) struct timespec ts; if (-1 == clock_gettime(clk_id, &ts)) { printf("clock_gettime failed %d:%s\n", errno, strerror(errno)); return -1; } return (uint64_t)(((uint64_t)ts.tv_sec*1000*1000*1000) + (uint64_t)ts.tv_nsec); -#elif defined (OS_WINDOWS) - return 0; -#endif } uint64_t time_now_msec() diff --git a/gear-lib/libworkq/test_libworkq.c b/gear-lib/libworkq/test_libworkq.c index 52c0bf2..a375987 100644 --- a/gear-lib/libworkq/test_libworkq.c +++ b/gear-lib/libworkq/test_libworkq.c @@ -21,9 +21,7 @@ ******************************************************************************/ #include "libworkq.h" #include -#ifdef OS_LINUX #include -#endif void test(void *arg) { From 321faf63ab68ddb82fd0cf2d2dafbcb55ace8158 Mon Sep 17 00:00:00 2001 From: gozfree Date: Sat, 2 Apr 2022 12:44:45 +0800 Subject: [PATCH 05/16] [libposix] add utf8/wchar/mbs switch --- gear-lib/libposix/Makefile | 4 +- gear-lib/libposix/libposix.c | 164 +++++++++- gear-lib/libposix/libposix.h | 22 +- gear-lib/libposix/libposix4linux.c | 61 ---- gear-lib/libposix/libposix4nix.c | 300 ++++++++++++++++++ .../{libposix4linux.h => libposix4nix.h} | 4 +- gear-lib/libposix/libposix4win.c | 44 ++- 7 files changed, 528 insertions(+), 71 deletions(-) delete mode 100644 gear-lib/libposix/libposix4linux.c create mode 100644 gear-lib/libposix/libposix4nix.c rename gear-lib/libposix/{libposix4linux.h => libposix4nix.h} (98%) diff --git a/gear-lib/libposix/Makefile b/gear-lib/libposix/Makefile index 6eea335..66b019f 100644 --- a/gear-lib/libposix/Makefile +++ b/gear-lib/libposix/Makefile @@ -26,7 +26,7 @@ VER_TAG = $(shell echo ${LIBNAME} | tr 'a-z' 'A-Z') VER = $(shell awk '/'"${VER_TAG}_VERSION"'/{print $$3}' ${LIBNAME}.h) TGT_LIB_H = $(LIBNAME).h ifeq ($(ARCH), linux) -TGT_LIB_H += libposix4linux.h kernel_list.h +TGT_LIB_H += libposix4nix.h kernel_list.h else ifeq ($(ARCH), win) TGT_LIB_H += libposix4win.h kernel_list_win32.h endif @@ -37,7 +37,7 @@ TGT_UNIT_TEST = test_$(LIBNAME) OBJS_LIB = $(LIBNAME).o ifeq ($(ARCH), linux) -OBJS_LIB += libposix4linux.o +OBJS_LIB += libposix4nix.o endif ifeq ($(ARCH), win) OBJS_LIB += libposix4win.o diff --git a/gear-lib/libposix/libposix.c b/gear-lib/libposix/libposix.c index 418c966..977c4cc 100644 --- a/gear-lib/libposix/libposix.c +++ b/gear-lib/libposix/libposix.c @@ -23,10 +23,7 @@ #include #include #include - -#ifndef PATH_SPLIT -#define PATH_SPLIT '/' -#endif +#include void *memdup(const void *src, size_t len) { @@ -74,3 +71,162 @@ bool is_little_endian(void) uint16_t x = 0x01; return *((uint8_t *) &x); } + +size_t mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size) +{ + size_t out_len; + + if (!str) + return 0; + + out_len = dst ? (dst_size - 1) : mbstowcs(NULL, str, len); + if (dst) { + if (!dst_size) + return 0; + if (out_len) + out_len = mbstowcs(dst, str, out_len + 1); + dst[out_len] = 0; + } + return out_len; +} + +size_t wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size) +{ + size_t out_len; + + if (!str) + return 0; + + out_len = dst ? (dst_size - 1) : wcstombs(NULL, str, len); + if (dst) { + if (!dst_size) + return 0; + if (out_len) + out_len = wcstombs(dst, str, out_len + 1); + + dst[out_len] = 0; + } + return out_len; +} + +size_t utf8_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size) +{ + size_t in_len; + size_t out_len; + + if (!str) + return 0; + + in_len = len ? len : strlen(str); + out_len = dst ? (dst_size - 1) : utf8_to_wchar(str, in_len, NULL, 0, 0); + + if (dst) { + if (!dst_size) + return 0; + if (out_len) + out_len = utf8_to_wchar(str, in_len, dst, out_len + 1, 0); + dst[out_len] = 0; + } + return out_len; +} + +size_t wcs_to_utf8(const wchar_t *str, size_t len, char *dst, size_t dst_size) +{ + size_t in_len; + size_t out_len; + + if (!str) + return 0; + + in_len = (len != 0) ? len : wcslen(str); + out_len = dst ? (dst_size - 1) : wchar_to_utf8(str, in_len, NULL, 0, 0); + if (dst) { + if (!dst_size) + return 0; + if (out_len) + out_len = wchar_to_utf8(str, in_len, dst, out_len + 1, 0); + dst[out_len] = 0; + } + return out_len; +} + +size_t mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr) +{ + if (str) { + size_t out_len = mbs_to_wcs(str, len, NULL, 0); + + *pstr = malloc((out_len + 1) * sizeof(wchar_t)); + return mbs_to_wcs(str, len, *pstr, out_len + 1); + } else { + *pstr = NULL; + return 0; + } +} + +size_t wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr) +{ + if (str) { + size_t out_len = wcs_to_mbs(str, len, NULL, 0); + + *pstr = malloc((out_len + 1) * sizeof(char)); + return wcs_to_mbs(str, len, *pstr, out_len + 1); + } else { + *pstr = NULL; + return 0; + } +} + +size_t utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr) +{ + if (str) { + size_t out_len = utf8_to_wcs(str, len, NULL, 0); + *pstr = malloc((out_len + 1) * sizeof(wchar_t)); + return utf8_to_wcs(str, len, *pstr, out_len + 1); + } else { + *pstr = NULL; + return 0; + } +} + +size_t wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr) +{ + if (str) { + size_t out_len = wcs_to_utf8(str, len, NULL, 0); + *pstr = malloc((out_len + 1) * sizeof(char)); + return wcs_to_utf8(str, len, *pstr, out_len + 1); + } else { + *pstr = NULL; + return 0; + } +} + +size_t utf8_to_mbs_ptr(const char *str, size_t len, char **pstr) +{ + char *dst = NULL; + size_t out_len = 0; + + if (str) { + wchar_t *wstr = NULL; + size_t wlen = utf8_to_wcs_ptr(str, len, &wstr); + out_len = wcs_to_mbs_ptr(wstr, wlen, &dst); + free(wstr); + } + *pstr = dst; + return out_len; +} + +size_t mbs_to_utf8_ptr(const char *str, size_t len, char **pstr) +{ + char *dst = NULL; + size_t out_len = 0; + + if (str) { + wchar_t *wstr = NULL; + size_t wlen = mbs_to_wcs_ptr(str, len, &wstr); + out_len = wcs_to_utf8_ptr(wstr, wlen, &dst); + free(wstr); + } + *pstr = dst; + return out_len; +} + diff --git a/gear-lib/libposix/libposix.h b/gear-lib/libposix/libposix.h index a2500c0..8b6ed70 100644 --- a/gear-lib/libposix/libposix.h +++ b/gear-lib/libposix/libposix.h @@ -34,7 +34,7 @@ extern "C" { #if defined (__linux__) || defined (__CYGWIN__) #define OS_LINUX #define GEAR_API __attribute__((visibility("default"))) -#include "libposix4linux.h" +#include "libposix4nix.h" /****************************************************************************** * OS_WINDOWS @@ -154,6 +154,7 @@ typedef struct rational { * Variable-argument unused annotation */ #define UNUSED(e, ...) (void) ((void) (e), ##__VA_ARGS__) +#define UNUSED_PARAMETER(param) (void)param #define SWAP(a, b) \ do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) @@ -226,6 +227,25 @@ GEAR_API bool is_little_endian(void); GEAR_API int get_proc_name(char *name, size_t len); +/* + * mbs + * / \ + * / \ + * wcs ---- utf8 + */ +GEAR_API size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out, size_t outsize, int flags); +GEAR_API size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out, size_t outsize, int flags); +GEAR_API size_t mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size); +GEAR_API size_t wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size); +GEAR_API size_t utf8_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size); +GEAR_API size_t wcs_to_utf8(const wchar_t *str, size_t len, char *dst, size_t dst_size); + +GEAR_API size_t mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr); +GEAR_API size_t wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr); +GEAR_API size_t utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr); +GEAR_API size_t wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr); +GEAR_API size_t utf8_to_mbs_ptr(const char *str, size_t len, char **pstr); +GEAR_API size_t mbs_to_utf8_ptr(const char *str, size_t len, char **pstr); #ifdef __cplusplus } diff --git a/gear-lib/libposix/libposix4linux.c b/gear-lib/libposix/libposix4linux.c deleted file mode 100644 index 1a00491..0000000 --- a/gear-lib/libposix/libposix4linux.c +++ /dev/null @@ -1,61 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2014-2020 Zhifeng Gong - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - ******************************************************************************/ -#include "libposix.h" -#include -#include -#include -#include -#include - -#ifndef PATH_SPLIT -#define PATH_SPLIT '/' -#endif - - -int get_proc_name(char *name, size_t len) -{ - int i, ret; - char proc_name[PATH_MAX]; - char *ptr = NULL; - memset(proc_name, 0, sizeof(proc_name)); - if (-1 == readlink("/proc/self/exe", proc_name, sizeof(proc_name))) { - fprintf(stderr, "readlink failed!\n"); - return -1; - } - ret = strlen(proc_name); - for (i = ret, ptr = proc_name; i > 0; i--) { - if (ptr[i] == PATH_SPLIT) { - ptr+= i+1; - break; - } - } - if (i == 0) { - fprintf(stderr, "proc path %s is invalid\n", proc_name); - return -1; - } - if (ret-i > (int)len) { - fprintf(stderr, "proc name length %d is larger than %d\n", ret-i, (int)len); - return -1; - } - strncpy(name, ptr, ret - i); - return 0; -} diff --git a/gear-lib/libposix/libposix4nix.c b/gear-lib/libposix/libposix4nix.c new file mode 100644 index 0000000..672aa51 --- /dev/null +++ b/gear-lib/libposix/libposix4nix.c @@ -0,0 +1,300 @@ +/****************************************************************************** + * Copyright (C) 2014-2020 Zhifeng Gong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ +#include "libposix.h" +#include +#include +#include +#include +#include + +#ifndef PATH_SPLIT +#define PATH_SPLIT '/' +#endif + + +int get_proc_name(char *name, size_t len) +{ + int i, ret; + char proc_name[PATH_MAX]; + char *ptr = NULL; + memset(proc_name, 0, sizeof(proc_name)); + if (-1 == readlink("/proc/self/exe", proc_name, sizeof(proc_name))) { + fprintf(stderr, "readlink failed!\n"); + return -1; + } + ret = strlen(proc_name); + for (i = ret, ptr = proc_name; i > 0; i--) { + if (ptr[i] == PATH_SPLIT) { + ptr+= i+1; + break; + } + } + if (i == 0) { + fprintf(stderr, "proc path %s is invalid\n", proc_name); + return -1; + } + if (ret-i > (int)len) { + fprintf(stderr, "proc name length %d is larger than %d\n", ret-i, (int)len); + return -1; + } + strncpy(name, ptr, ret - i); + return 0; +} + +#define UTF8_IGNORE_ERROR 0x01 +#define UTF8_SKIP_BOM 0x02 + +#define _NXT 0x80 +#define _SEQ2 0xc0 +#define _SEQ3 0xe0 +#define _SEQ4 0xf0 +#define _SEQ5 0xf8 +#define _SEQ6 0xfc + +#define _BOM 0xfeff + +static int wchar_forbidden(wchar_t sym) +{ + /* Surrogate pairs */ + if (sym >= 0xd800 && sym <= 0xdfff) + return -1; + return 0; +} + +static int utf8_forbidden(unsigned char octet) +{ + switch (octet) { + case 0xc0: + case 0xc1: + case 0xf5: + case 0xff: + return -1; + } + return 0; +} + +size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out, size_t outsize, int flags) +{ + unsigned char *p, *lim; + wchar_t *wlim, high; + size_t n, total, i, n_bits; + + if (in == NULL || (outsize == 0 && out != NULL)) + return 0; + + total = 0; + p = (unsigned char *)in; + lim = (insize != 0) ? (p + insize) : (unsigned char *)-1; + wlim = out + outsize; + + for (; p < lim; p += n) { + if (!*p) + break; + if (utf8_forbidden(*p) != 0 && (flags & UTF8_IGNORE_ERROR) == 0) + return 0; + + /* + * Get number of bytes for one wide character. + */ + n = 1; /* default: 1 byte. Used when skipping bytes. */ + if ((*p & 0x80) == 0) + high = (wchar_t)*p; + else if ((*p & 0xe0) == _SEQ2) { + n = 2; + high = (wchar_t)(*p & 0x1f); + } else if ((*p & 0xf0) == _SEQ3) { + n = 3; + high = (wchar_t)(*p & 0x0f); + } else if ((*p & 0xf8) == _SEQ4) { + n = 4; + high = (wchar_t)(*p & 0x07); + } else if ((*p & 0xfc) == _SEQ5) { + n = 5; + high = (wchar_t)(*p & 0x03); + } else if ((*p & 0xfe) == _SEQ6) { + n = 6; + high = (wchar_t)(*p & 0x01); + } else { + if ((flags & UTF8_IGNORE_ERROR) == 0) + return 0; + continue; + } + + /* does the sequence header tell us truth about length? */ + if ((size_t)(lim - p) <= n - 1) { + if ((flags & UTF8_IGNORE_ERROR) == 0) + return 0; + n = 1; + continue; /* skip */ + } + + /* + * Validate sequence. + * All symbols must have higher bits set to 10xxxxxx + */ + if (n > 1) { + for (i = 1; i < n; i++) { + if ((p[i] & 0xc0) != _NXT) + break; + } + if (i != n) { + if ((flags & UTF8_IGNORE_ERROR) == 0) + return 0; + n = 1; + continue; /* skip */ + } + } + + total++; + if (out == NULL) + continue; + if (out >= wlim) + return 0; /* no space left */ + + *out = 0; + n_bits = 0; + for (i = 1; i < n; i++) { + *out |= (wchar_t)(p[n - i] & 0x3f) << n_bits; + n_bits += 6; /* 6 low bits in every byte */ + } + *out |= high << n_bits; + + if (wchar_forbidden(*out) != 0) { + if ((flags & UTF8_IGNORE_ERROR) == 0) + return 0; /* forbidden character */ + else { + total--; + out--; + } + } else if (*out == _BOM && (flags & UTF8_SKIP_BOM) != 0) { + total--; + out--; + } + out++; + } + return total; +} + +size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out, size_t outsize, int flags) +{ + wchar_t *w, *wlim, ch = 0; + unsigned char *p, *lim, *oc; + size_t total, n; + + if (in == NULL || (outsize == 0 && out != NULL)) + return 0; + + w = (wchar_t *)in; + wlim = (insize != 0) ? (w + insize) : (wchar_t *)-1; + p = (unsigned char *)out; + lim = p + outsize; + total = 0; + + for (; w < wlim; w++) { + if (!*w) + break; + + if (wchar_forbidden(*w) != 0) { + if ((flags & UTF8_IGNORE_ERROR) == 0) + return 0; + else + continue; + } + + if (*w == _BOM && (flags & UTF8_SKIP_BOM) != 0) + continue; + + if (*w < 0) { + if ((flags & UTF8_IGNORE_ERROR) == 0) + return 0; + continue; + } else if (*w <= 0x0000007f) + n = 1; + else if (*w <= 0x000007ff) + n = 2; + else if (*w <= 0x0000ffff) + n = 3; + else if (*w <= 0x001fffff) + n = 4; + else if (*w <= 0x03ffffff) + n = 5; + else /* if (*w <= 0x7fffffff) */ + n = 6; + + total += n; + + if (out == NULL) + continue; + + if ((size_t)(lim - p) <= n - 1) + return 0; /* no space left */ + + ch = *w; + oc = (unsigned char *)&ch; + switch (n) { + case 1: + *p = oc[0]; + break; + case 2: + p[1] = _NXT | (oc[0] & 0x3f); + p[0] = _SEQ2 | (oc[0] >> 6) | ((oc[1] & 0x07) << 2); + break; + case 3: + p[2] = _NXT | (oc[0] & 0x3f); + p[1] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2); + p[0] = _SEQ3 | ((oc[1] & 0xf0) >> 4); + break; + case 4: + p[3] = _NXT | (oc[0] & 0x3f); + p[2] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2); + p[1] = _NXT | ((oc[1] & 0xf0) >> 4) | + ((oc[2] & 0x03) << 4); + p[0] = _SEQ4 | ((oc[2] & 0x1f) >> 2); + break; + case 5: + p[4] = _NXT | (oc[0] & 0x3f); + p[3] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2); + p[2] = _NXT | ((oc[1] & 0xf0) >> 4) | + ((oc[2] & 0x03) << 4); + p[1] = _NXT | (oc[2] >> 2); + p[0] = _SEQ5 | (oc[3] & 0x03); + break; + case 6: + p[5] = _NXT | (oc[0] & 0x3f); + p[4] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2); + p[3] = _NXT | (oc[1] >> 4) | ((oc[2] & 0x03) << 4); + p[2] = _NXT | (oc[2] >> 2); + p[1] = _NXT | (oc[3] & 0x3f); + p[0] = _SEQ6 | ((oc[3] & 0x40) >> 6); + break; + } + + /* + * NOTE: do not check here for forbidden UTF-8 characters. + * They cannot appear here because we do proper conversion. + */ + + p += n; + } + + return total; +} diff --git a/gear-lib/libposix/libposix4linux.h b/gear-lib/libposix/libposix4nix.h similarity index 98% rename from gear-lib/libposix/libposix4linux.h rename to gear-lib/libposix/libposix4nix.h index 2abc6ee..c6d092a 100644 --- a/gear-lib/libposix/libposix4linux.h +++ b/gear-lib/libposix/libposix4nix.h @@ -19,8 +19,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ -#ifndef LIBPOSIX4LINUX_H -#define LIBPOSIX4LINUX_H +#ifndef LIBPOSIX4NIX_H +#define LIBPOSIX4NIX_H #include #include diff --git a/gear-lib/libposix/libposix4win.c b/gear-lib/libposix/libposix4win.c index 8fd8e36..568bd6b 100644 --- a/gear-lib/libposix/libposix4win.c +++ b/gear-lib/libposix/libposix4win.c @@ -29,6 +29,7 @@ #include #include #define HAVE_WINRT 1 +#define _CRT_SECURE_NO_WARNINGS int utf8towchar(const char *filename_utf8, wchar_t **filename_w) { @@ -234,7 +235,6 @@ int pipe(int fds[2]) if (!CreatePipe(&rd, &wr, &sa, 0)) { return -1; } - printf("%s rd=%p, wr=%p\n", __func__, rd, wr); fds[0] = (int)rd; fds[1] = (int)wr; return 0; @@ -302,3 +302,45 @@ void align_free(void *ptr) if (ptr) free((char *)ptr - ((char *)ptr)[-1]); } + +static inline bool has_utf8_bom(const char *in_char) +{ + uint8_t *in = (uint8_t *)in_char; + return (in && in[0] == 0xef && in[1] == 0xbb && in[2] == 0xbf); +} + +size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out, size_t outsize, int flags) +{ + int i_insize = (int)insize; + int ret; + + if (i_insize == 0) + i_insize = (int)strlen(in); + + /* prevent bom from being used in the string */ + if (has_utf8_bom(in)) { + if (i_insize >= 3) { + in += 3; + i_insize -= 3; + } + } + + ret = MultiByteToWideChar(CP_UTF8, 0, in, i_insize, out, (int)outsize); + + UNUSED_PARAMETER(flags); + return (ret > 0) ? (size_t)ret : 0; +} + +size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out, size_t outsize, int flags) +{ + int i_insize = (int)insize; + int ret; + + if (i_insize == 0) + i_insize = (int)wcslen(in); + + ret = WideCharToMultiByte(CP_UTF8, 0, in, i_insize, out, (int)outsize, NULL, NULL); + + UNUSED_PARAMETER(flags); + return (ret > 0) ? (size_t)ret : 0; +} From e4a3c13985891bbde4505e8efe5950470dbb0087 Mon Sep 17 00:00:00 2001 From: gozfree Date: Sat, 2 Apr 2022 17:52:55 +0800 Subject: [PATCH 06/16] [libdarray] add dynamic string operation --- gear-lib/libdarray/Makefile | 7 +- gear-lib/libdarray/libdstring.c | 799 ++++++++++++++++++++++++++++++++ gear-lib/libdarray/libdstring.h | 393 ++++++++++++++++ 3 files changed, 1196 insertions(+), 3 deletions(-) create mode 100644 gear-lib/libdarray/libdstring.c create mode 100644 gear-lib/libdarray/libdstring.h diff --git a/gear-lib/libdarray/Makefile b/gear-lib/libdarray/Makefile index 316bb8a..a6cb40e 100644 --- a/gear-lib/libdarray/Makefile +++ b/gear-lib/libdarray/Makefile @@ -24,13 +24,13 @@ RM_V ?= $(RM) LIBNAME = libdarray VER_TAG = $(shell echo ${LIBNAME} | tr 'a-z' 'A-Z') VER = $(shell awk '/'"${VER_TAG}_VERSION"'/{print $$3}' ${LIBNAME}.h) -TGT_LIB_H = $(LIBNAME).h libserializer.h +TGT_LIB_H = $(LIBNAME).h libserializer.h libdstring.h TGT_LIB_A = $(LIBNAME).a TGT_LIB_SO = $(LIBNAME).so TGT_LIB_SO_VER = $(TGT_LIB_SO).${VER} TGT_UNIT_TEST = test_$(LIBNAME) -OBJS_LIB = $(LIBNAME).o libserializer.o +OBJS_LIB = $(LIBNAME).o libserializer.o libdstring.o OBJS_UNIT_TEST = test_$(LIBNAME).o ############################################################################### @@ -54,6 +54,7 @@ CFLAGS += -I$(OUTPUT)/include/gear-lib SHARED := -shared LDFLAGS := $($(ARCH)_LDFLAGS) +LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -lposix LDFLAGS += -pthread ############################################################################### @@ -76,7 +77,7 @@ $(TGT_LIB_A): $(OBJS_LIB) $(AR_V) rcs $@ $^ $(TGT_LIB_SO): $(OBJS_LIB) - $(CC_V) -o $@ $^ $(SHARED) + $(CC_V) -o $@ $^ $(SHARED) $(LDFLAGS) @mv $(TGT_LIB_SO) $(TGT_LIB_SO_VER) @ln -sf $(TGT_LIB_SO_VER) $(TGT_LIB_SO) diff --git a/gear-lib/libdarray/libdstring.c b/gear-lib/libdarray/libdstring.c new file mode 100644 index 0000000..a6bbb60 --- /dev/null +++ b/gear-lib/libdarray/libdstring.c @@ -0,0 +1,799 @@ +/****************************************************************************** + * Copyright (C) 2014-2022 Zhifeng Gong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ +#include "libdstring.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if 0 +#include "c99defs.h" +#include "dstr.h" +#include "darray.h" +#include "bmem.h" +#include "utf8.h" +#include "lexer.h" +#include "platform.h" +#endif + +static const char *astrblank = ""; +static const wchar_t *wstrblank = L""; + +int astrcmpi(const char *str1, const char *str2) +{ + if (!str1) + str1 = astrblank; + if (!str2) + str2 = astrblank; + + do { + char ch1 = (char)toupper(*str1); + char ch2 = (char)toupper(*str2); + + if (ch1 < ch2) + return -1; + else if (ch1 > ch2) + return 1; + } while (*str1++ && *str2++); + + return 0; +} + +int wstrcmpi(const wchar_t *str1, const wchar_t *str2) +{ + if (!str1) + str1 = wstrblank; + if (!str2) + str2 = wstrblank; + + do { + wchar_t ch1 = (wchar_t)towupper(*str1); + wchar_t ch2 = (wchar_t)towupper(*str2); + + if (ch1 < ch2) + return -1; + else if (ch1 > ch2) + return 1; + } while (*str1++ && *str2++); + + return 0; +} + +int astrcmp_n(const char *str1, const char *str2, size_t n) +{ + if (!n) + return 0; + if (!str1) + str1 = astrblank; + if (!str2) + str2 = astrblank; + + do { + char ch1 = *str1; + char ch2 = *str2; + + if (ch1 < ch2) + return -1; + else if (ch1 > ch2) + return 1; + } while (*str1++ && *str2++ && --n); + + return 0; +} + +int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n) +{ + if (!n) + return 0; + if (!str1) + str1 = wstrblank; + if (!str2) + str2 = wstrblank; + + do { + wchar_t ch1 = *str1; + wchar_t ch2 = *str2; + + if (ch1 < ch2) + return -1; + else if (ch1 > ch2) + return 1; + } while (*str1++ && *str2++ && --n); + + return 0; +} + +int astrcmpi_n(const char *str1, const char *str2, size_t n) +{ + if (!n) + return 0; + if (!str1) + str1 = astrblank; + if (!str2) + str2 = astrblank; + + do { + char ch1 = (char)toupper(*str1); + char ch2 = (char)toupper(*str2); + + if (ch1 < ch2) + return -1; + else if (ch1 > ch2) + return 1; + } while (*str1++ && *str2++ && --n); + + return 0; +} + +int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n) +{ + if (!n) + return 0; + if (!str1) + str1 = wstrblank; + if (!str2) + str2 = wstrblank; + + do { + wchar_t ch1 = (wchar_t)towupper(*str1); + wchar_t ch2 = (wchar_t)towupper(*str2); + + if (ch1 < ch2) + return -1; + else if (ch1 > ch2) + return 1; + } while (*str1++ && *str2++ && --n); + + return 0; +} + +char *astrstri(const char *str, const char *find) +{ + size_t len; + + if (!str || !find) + return NULL; + + len = strlen(find); + + do { + if (astrcmpi_n(str, find, len) == 0) + return (char *)str; + } while (*str++); + + return NULL; +} + +wchar_t *wstrstri(const wchar_t *str, const wchar_t *find) +{ + size_t len; + + if (!str || !find) + return NULL; + + len = wcslen(find); + + do { + if (wstrcmpi_n(str, find, len) == 0) + return (wchar_t *)str; + } while (*str++); + + return NULL; +} + +static inline bool is_padding(char ch) +{ + return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'; +} + +char *strdepad(char *str) +{ + char *temp; + size_t len; + + if (!str) + return str; + if (!*str) + return str; + + temp = str; + + /* remove preceding spaces/tabs */ + while (is_padding(*temp)) + ++temp; + + len = strlen(str); + if (temp != str) + memmove(str, temp, len + 1); + + if (len) { + temp = str + (len - 1); + while (is_padding(*temp)) + *(temp--) = 0; + } + + return str; +} + +wchar_t *wcsdepad(wchar_t *str) +{ + wchar_t *temp; + size_t len; + + if (!str) + return str; + if (!*str) + return str; + + temp = str; + + /* remove preceding spaces/tabs */ + while (*temp == ' ' || *temp == '\t') + ++temp; + + len = wcslen(str); + if (temp != str) + memmove(str, temp, (len + 1) * sizeof(wchar_t)); + + if (len) { + temp = str + (len - 1); + while (*temp == ' ' || *temp == '\t') + *(temp--) = 0; + } + + return str; +} + +char **strlist_split(const char *str, char split_ch, bool include_empty) +{ + const char *cur_str = str; + const char *next_str; + char *out = NULL; + size_t count = 0; + size_t total_size = 0; + + if (str) { + char **table; + char *offset; + size_t cur_idx = 0; + size_t cur_pos = 0; + + next_str = strchr(str, split_ch); + + while (next_str) { + size_t size = next_str - cur_str; + + if (size || include_empty) { + ++count; + total_size += size + 1; + } + + cur_str = next_str + 1; + next_str = strchr(cur_str, split_ch); + } + + if (*cur_str || include_empty) { + ++count; + total_size += strlen(cur_str) + 1; + } + + /* ------------------ */ + + cur_pos = (count + 1) * sizeof(char *); + total_size += cur_pos; + out = malloc(total_size); + offset = out + cur_pos; + table = (char **)out; + + /* ------------------ */ + + next_str = strchr(str, split_ch); + cur_str = str; + + while (next_str) { + size_t size = next_str - cur_str; + + if (size || include_empty) { + table[cur_idx++] = offset; + strncpy(offset, cur_str, size); + offset[size] = 0; + offset += size + 1; + } + + cur_str = next_str + 1; + next_str = strchr(cur_str, split_ch); + } + + if (*cur_str || include_empty) { + table[cur_idx++] = offset; + strcpy(offset, cur_str); + } + + table[cur_idx] = NULL; + } + + return (char **)out; +} + +void strlist_free(char **strlist) +{ + free(strlist); +} + +void dstr_init_copy_strref(struct dstr *dst, const struct strref *src) +{ + dstr_init(dst); + dstr_copy_strref(dst, src); +} + +void dstr_copy(struct dstr *dst, const char *array) +{ + size_t len; + + if (!array || !*array) { + dstr_free(dst); + return; + } + + len = strlen(array); + dstr_ensure_capacity(dst, len + 1); + memcpy(dst->array, array, len + 1); + dst->len = len; +} + +void dstr_copy_strref(struct dstr *dst, const struct strref *src) +{ + if (dst->array) + dstr_free(dst); + + dstr_ncopy(dst, src->array, src->len); +} + +static inline size_t size_min(size_t a, size_t b) +{ + return (a < b) ? a : b; +} + +void dstr_ncopy(struct dstr *dst, const char *array, const size_t len) +{ + if (dst->array) + dstr_free(dst); + + if (!len) + return; + + dst->array = memdup(array, len + 1); + dst->len = len; + dst->capacity = len + 1; + + dst->array[len] = 0; +} + +void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *str, const size_t len) +{ + size_t newlen; + + if (dst->array) + dstr_free(dst); + + if (!len) + return; + + newlen = size_min(len, str->len); + dst->array = memdup(str->array, newlen + 1); + dst->len = newlen; + dst->capacity = newlen + 1; + + dst->array[newlen] = 0; +} + +void dstr_cat_dstr(struct dstr *dst, const struct dstr *str) +{ + size_t new_len; + if (!str->len) + return; + + new_len = dst->len + str->len; + + dstr_ensure_capacity(dst, new_len + 1); + memcpy(dst->array + dst->len, str->array, str->len + 1); + dst->len = new_len; +} + +void dstr_cat_strref(struct dstr *dst, const struct strref *str) +{ + dstr_ncat(dst, str->array, str->len); +} + +void dstr_ncat(struct dstr *dst, const char *array, const size_t len) +{ + size_t new_len; + if (!array || !*array || !len) + return; + + new_len = dst->len + len; + + dstr_ensure_capacity(dst, new_len + 1); + memcpy(dst->array + dst->len, array, len); + + dst->len = new_len; + dst->array[new_len] = 0; +} + +void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str, const size_t len) +{ + size_t new_len, in_len; + if (!str->array || !*str->array || !len) + return; + + in_len = size_min(len, str->len); + new_len = dst->len + in_len; + + dstr_ensure_capacity(dst, new_len + 1); + memcpy(dst->array + dst->len, str->array, in_len); + + dst->len = new_len; + dst->array[new_len] = 0; +} + +void dstr_insert(struct dstr *dst, const size_t idx, const char *array) +{ + size_t new_len, len; + if (!array || !*array) + return; + if (idx == dst->len) { + dstr_cat(dst, array); + return; + } + + len = strlen(array); + new_len = dst->len + len; + + dstr_ensure_capacity(dst, new_len + 1); + + memmove(dst->array + idx + len, dst->array + idx, dst->len - idx + 1); + memcpy(dst->array + idx, array, len); + + dst->len = new_len; +} + +void dstr_insert_dstr(struct dstr *dst, const size_t idx, + const struct dstr *str) +{ + size_t new_len; + if (!str->len) + return; + if (idx == dst->len) { + dstr_cat_dstr(dst, str); + return; + } + + new_len = dst->len + str->len; + + dstr_ensure_capacity(dst, (new_len + 1)); + + memmove(dst->array + idx + str->len, dst->array + idx, + dst->len - idx + 1); + memcpy(dst->array + idx, str->array, str->len); + + dst->len = new_len; +} + +void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch) +{ + if (idx == dst->len) { + dstr_cat_ch(dst, ch); + return; + } + + dstr_ensure_capacity(dst, (++dst->len + 1)); + memmove(dst->array + idx + 1, dst->array + idx, dst->len - idx + 1); + dst->array[idx] = ch; +} + +void dstr_remove(struct dstr *dst, const size_t idx, const size_t count) +{ + size_t end; + if (!count) + return; + if (count == dst->len) { + dstr_free(dst); + return; + } + + end = idx + count; + if (end == dst->len) + dst->array[idx] = 0; + else + memmove(dst->array + idx, dst->array + end, dst->len - end + 1); + + dst->len -= count; +} + +void dstr_printf(struct dstr *dst, const char *format, ...) +{ + va_list args; + va_start(args, format); + dstr_vprintf(dst, format, args); + va_end(args); +} + +void dstr_catf(struct dstr *dst, const char *format, ...) +{ + va_list args; + va_start(args, format); + dstr_vcatf(dst, format, args); + va_end(args); +} + +void dstr_vprintf(struct dstr *dst, const char *format, va_list args) +{ + va_list args_cp; + va_copy(args_cp, args); + + int len = vsnprintf(NULL, 0, format, args_cp); + va_end(args_cp); + + if (len < 0) + len = 4095; + + dstr_ensure_capacity(dst, ((size_t)len) + 1); + len = vsnprintf(dst->array, ((size_t)len) + 1, format, args); + + if (!*dst->array) { + dstr_free(dst); + return; + } + + dst->len = len < 0 ? strlen(dst->array) : (size_t)len; +} + +void dstr_vcatf(struct dstr *dst, const char *format, va_list args) +{ + va_list args_cp; + va_copy(args_cp, args); + + int len = vsnprintf(NULL, 0, format, args_cp); + va_end(args_cp); + + if (len < 0) + len = 4095; + + dstr_ensure_capacity(dst, dst->len + ((size_t)len) + 1); + len = vsnprintf(dst->array + dst->len, ((size_t)len) + 1, format, args); + + if (!*dst->array) { + dstr_free(dst); + return; + } + + dst->len += len < 0 ? strlen(dst->array + dst->len) : (size_t)len; +} + +void dstr_safe_printf(struct dstr *dst, const char *format, const char *val1, + const char *val2, const char *val3, const char *val4) +{ + dstr_copy(dst, format); + if (val1) + dstr_replace(dst, "$1", val1); + if (val2) + dstr_replace(dst, "$2", val2); + if (val3) + dstr_replace(dst, "$3", val3); + if (val4) + dstr_replace(dst, "$4", val4); +} + +void dstr_replace(struct dstr *str, const char *find, const char *replace) +{ + size_t find_len, replace_len; + char *temp; + + if (dstr_is_empty(str)) + return; + + if (!replace) + replace = ""; + + find_len = strlen(find); + replace_len = strlen(replace); + temp = str->array; + + if (replace_len < find_len) { + unsigned long count = 0; + + while ((temp = strstr(temp, find)) != NULL) { + char *end = temp + find_len; + size_t end_len = strlen(end); + + if (end_len) { + memmove(temp + replace_len, end, end_len + 1); + if (replace_len) + memcpy(temp, replace, replace_len); + } else { + strcpy(temp, replace); + } + + temp += replace_len; + ++count; + } + + if (count) + str->len += (replace_len - find_len) * count; + + } else if (replace_len > find_len) { + unsigned long count = 0; + + while ((temp = strstr(temp, find)) != NULL) { + temp += find_len; + ++count; + } + + if (!count) + return; + + str->len += (replace_len - find_len) * count; + dstr_ensure_capacity(str, str->len + 1); + temp = str->array; + + while ((temp = strstr(temp, find)) != NULL) { + char *end = temp + find_len; + size_t end_len = strlen(end); + + if (end_len) { + memmove(temp + replace_len, end, end_len + 1); + memcpy(temp, replace, replace_len); + } else { + strcpy(temp, replace); + } + + temp += replace_len; + } + + } else { + while ((temp = strstr(temp, find)) != NULL) { + memcpy(temp, replace, replace_len); + temp += replace_len; + } + } +} + +void dstr_depad(struct dstr *str) +{ + if (str->array) { + str->array = strdepad(str->array); + if (*str->array) + str->len = strlen(str->array); + else + dstr_free(str); + } +} + +void dstr_left(struct dstr *dst, const struct dstr *str, const size_t pos) +{ + dstr_resize(dst, pos); + if (dst != str) + memcpy(dst->array, str->array, pos); +} + +void dstr_mid(struct dstr *dst, const struct dstr *str, const size_t start, + const size_t count) +{ + struct dstr temp; + dstr_init(&temp); + dstr_copy_dstr(&temp, str); + dstr_ncopy(dst, temp.array + start, count); + dstr_free(&temp); +} + +void dstr_right(struct dstr *dst, const struct dstr *str, const size_t pos) +{ + struct dstr temp; + dstr_init(&temp); + dstr_ncopy(&temp, str->array + pos, str->len - pos); + dstr_copy_dstr(dst, &temp); + dstr_free(&temp); +} + +void dstr_from_mbs(struct dstr *dst, const char *mbstr) +{ + dstr_free(dst); + dst->len = mbs_to_utf8_ptr(mbstr, 0, &dst->array); +} + +char *dstr_to_mbs(const struct dstr *str) +{ + char *dst; + mbs_to_utf8_ptr(str->array, str->len, &dst); + return dst; +} + +wchar_t *dstr_to_wcs(const struct dstr *str) +{ + wchar_t *dst; + utf8_to_wcs_ptr(str->array, str->len, &dst); + return dst; +} + +void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr) +{ + size_t len = wchar_to_utf8(wstr, 0, NULL, 0, 0); + + if (len) { + dstr_resize(dst, len); + wchar_to_utf8(wstr, 0, dst->array, len + 1, 0); + } else { + dstr_free(dst); + } +} + +void dstr_to_upper(struct dstr *str) +{ + wchar_t *wstr; + wchar_t *temp; + + if (dstr_is_empty(str)) + return; + + wstr = dstr_to_wcs(str); + temp = wstr; + + if (!wstr) + return; + + while (*temp) { + *temp = (wchar_t)towupper(*temp); + temp++; + } + + dstr_from_wcs(str, wstr); + free(wstr); +} + +void dstr_to_lower(struct dstr *str) +{ + wchar_t *wstr; + wchar_t *temp; + + if (dstr_is_empty(str)) + return; + + wstr = dstr_to_wcs(str); + temp = wstr; + + if (!wstr) + return; + + while (*temp) { + *temp = (wchar_t)towlower(*temp); + temp++; + } + + dstr_from_wcs(str, wstr); + free(wstr); +} diff --git a/gear-lib/libdarray/libdstring.h b/gear-lib/libdarray/libdstring.h new file mode 100644 index 0000000..6f57a9b --- /dev/null +++ b/gear-lib/libdarray/libdstring.h @@ -0,0 +1,393 @@ +/****************************************************************************** + * Copyright (C) 2014-2022 Zhifeng Gong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ +#ifndef LIB_DSTRING_H +#define LIB_DSTRING_H + +#include +#include +#include +#include +#include + +#if 0 +#include +#include +#include "c99defs.h" +#include "bmem.h" +#endif + +/* + * Dynamic string + * + * Helper struct/functions for dynamically sizing string buffers. + */ + +#ifdef __cplusplus +extern "C" { +#endif + + +/* ------------------------------------------------------------------------- */ +/* string reference (string segment within an already existing array) */ + +struct strref { + const char *array; + size_t len; +}; + +static inline void strref_clear(struct strref *dst) +{ + dst->array = NULL; + dst->len = 0; +} + +static inline void strref_set(struct strref *dst, const char *array, size_t len) +{ + dst->array = array; + dst->len = len; +} + +static inline void strref_copy(struct strref *dst, const struct strref *src) +{ + dst->array = src->array; + dst->len = src->len; +} + +static inline void strref_add(struct strref *dst, const struct strref *t) +{ + if (!dst->array) + strref_copy(dst, t); + else + dst->len += t->len; +} + +static inline bool strref_is_empty(const struct strref *str) +{ + return !str || !str->array || !str->len || !*str->array; +} + +GEAR_API int strref_cmp(const struct strref *str1, const char *str2); +GEAR_API int strref_cmpi(const struct strref *str1, const char *str2); +GEAR_API int strref_cmp_strref(const struct strref *str1, + const struct strref *str2); +GEAR_API int strref_cmpi_strref(const struct strref *str1, + const struct strref *str2); + + +struct dstr { + char *array; + size_t len; /* number of characters, excluding null terminator */ + size_t capacity; +}; + +#ifndef _MSC_VER +#define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a))) +#else +#define PRINTFATTR(f, a) +#endif + +GEAR_API int astrcmpi(const char *str1, const char *str2); +GEAR_API int wstrcmpi(const wchar_t *str1, const wchar_t *str2); +GEAR_API int astrcmp_n(const char *str1, const char *str2, size_t n); +GEAR_API int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n); +GEAR_API int astrcmpi_n(const char *str1, const char *str2, size_t n); +GEAR_API int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n); + +GEAR_API char *astrstri(const char *str, const char *find); +GEAR_API wchar_t *wstrstri(const wchar_t *str, const wchar_t *find); + +GEAR_API char *strdepad(char *str); +GEAR_API wchar_t *wcsdepad(wchar_t *str); + +GEAR_API char **strlist_split(const char *str, char split_ch, bool include_empty); +GEAR_API void strlist_free(char **strlist); + +static inline void dstr_init(struct dstr *dst); +static inline void dstr_init_move(struct dstr *dst, struct dstr *src); +static inline void dstr_init_move_array(struct dstr *dst, char *str); +static inline void dstr_init_copy(struct dstr *dst, const char *src); +static inline void dstr_init_copy_dstr(struct dstr *dst, + const struct dstr *src); +GEAR_API void dstr_init_copy_strref(struct dstr *dst, const struct strref *src); + +static inline void dstr_free(struct dstr *dst); +static inline void dstr_array_free(struct dstr *array, const size_t count); + +static inline void dstr_move(struct dstr *dst, struct dstr *src); +static inline void dstr_move_array(struct dstr *dst, char *str); + +GEAR_API void dstr_copy(struct dstr *dst, const char *array); +static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src); +GEAR_API void dstr_copy_strref(struct dstr *dst, const struct strref *src); + +GEAR_API void dstr_ncopy(struct dstr *dst, const char *array, const size_t len); +GEAR_API void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src, + const size_t len); + +static inline void dstr_resize(struct dstr *dst, const size_t num); +static inline void dstr_reserve(struct dstr *dst, const size_t num); + +static inline bool dstr_is_empty(const struct dstr *str); + +static inline void dstr_cat(struct dstr *dst, const char *array); +GEAR_API void dstr_cat_dstr(struct dstr *dst, const struct dstr *str); +GEAR_API void dstr_cat_strref(struct dstr *dst, const struct strref *str); + +static inline void dstr_cat_ch(struct dstr *dst, char ch); + +GEAR_API void dstr_ncat(struct dstr *dst, const char *array, const size_t len); +GEAR_API void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str, + const size_t len); + +GEAR_API void dstr_insert(struct dstr *dst, const size_t idx, const char *array); +GEAR_API void dstr_insert_dstr(struct dstr *dst, const size_t idx, + const struct dstr *str); +GEAR_API void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch); + +GEAR_API void dstr_remove(struct dstr *dst, const size_t idx, const size_t count); + +PRINTFATTR(2, 3) +GEAR_API void dstr_printf(struct dstr *dst, const char *format, ...); +PRINTFATTR(2, 3) +GEAR_API void dstr_catf(struct dstr *dst, const char *format, ...); + +GEAR_API void dstr_vprintf(struct dstr *dst, const char *format, va_list args); +GEAR_API void dstr_vcatf(struct dstr *dst, const char *format, va_list args); + +GEAR_API void dstr_safe_printf(struct dstr *dst, const char *format, + const char *val1, const char *val2, + const char *val3, const char *val4); + +static inline const char *dstr_find_i(const struct dstr *str, const char *find); +static inline const char *dstr_find(const struct dstr *str, const char *find); + +GEAR_API void dstr_replace(struct dstr *str, const char *find, + const char *replace); + +static inline int dstr_cmp(const struct dstr *str1, const char *str2); +static inline int dstr_cmpi(const struct dstr *str1, const char *str2); +static inline int dstr_ncmp(const struct dstr *str1, const char *str2, + const size_t n); +static inline int dstr_ncmpi(const struct dstr *str1, const char *str2, + const size_t n); + +GEAR_API void dstr_depad(struct dstr *dst); + +GEAR_API void dstr_left(struct dstr *dst, const struct dstr *str, + const size_t pos); +GEAR_API void dstr_mid(struct dstr *dst, const struct dstr *str, + const size_t start, const size_t count); +GEAR_API void dstr_right(struct dstr *dst, const struct dstr *str, + const size_t pos); + +static inline char dstr_end(const struct dstr *str); + +GEAR_API void dstr_from_mbs(struct dstr *dst, const char *mbstr); +GEAR_API char *dstr_to_mbs(const struct dstr *str); +GEAR_API void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr); +GEAR_API wchar_t *dstr_to_wcs(const struct dstr *str); + +GEAR_API void dstr_to_upper(struct dstr *str); +GEAR_API void dstr_to_lower(struct dstr *str); + +#undef PRINTFATTR + +/* ------------------------------------------------------------------------- */ + +static inline void dstr_init(struct dstr *dst) +{ + dst->array = NULL; + dst->len = 0; + dst->capacity = 0; +} + +static inline void dstr_init_move_array(struct dstr *dst, char *str) +{ + dst->array = str; + dst->len = (!str) ? 0 : strlen(str); + dst->capacity = dst->len + 1; +} + +static inline void dstr_init_move(struct dstr *dst, struct dstr *src) +{ + *dst = *src; + dstr_init(src); +} + +static inline void dstr_init_copy(struct dstr *dst, const char *str) +{ + dstr_init(dst); + dstr_copy(dst, str); +} + +static inline void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src) +{ + dstr_init(dst); + dstr_copy_dstr(dst, src); +} + +static inline void dstr_free(struct dstr *dst) +{ + free(dst->array); + dst->array = NULL; + dst->len = 0; + dst->capacity = 0; +} + +static inline void dstr_array_free(struct dstr *array, const size_t count) +{ + size_t i; + for (i = 0; i < count; i++) + dstr_free(array + i); +} + +static inline void dstr_move_array(struct dstr *dst, char *str) +{ + dstr_free(dst); + dst->array = str; + dst->len = (!str) ? 0 : strlen(str); + dst->capacity = dst->len + 1; +} + +static inline void dstr_move(struct dstr *dst, struct dstr *src) +{ + dstr_free(dst); + dstr_init_move(dst, src); +} + +static inline void dstr_ensure_capacity(struct dstr *dst, const size_t new_size) +{ + size_t new_cap; + if (new_size <= dst->capacity) + return; + + new_cap = (!dst->capacity) ? new_size : dst->capacity * 2; + if (new_size > new_cap) + new_cap = new_size; + dst->array = (char *)realloc(dst->array, new_cap); + dst->capacity = new_cap; +} + +static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src) +{ + if (dst->array) + dstr_free(dst); + + if (src->len) { + dstr_ensure_capacity(dst, src->len + 1); + memcpy(dst->array, src->array, src->len + 1); + dst->len = src->len; + } +} + +static inline void dstr_reserve(struct dstr *dst, const size_t capacity) +{ + if (capacity == 0 || capacity <= dst->len) + return; + + dst->array = (char *)realloc(dst->array, capacity); + dst->capacity = capacity; +} + +static inline void dstr_resize(struct dstr *dst, const size_t num) +{ + if (!num) { + dstr_free(dst); + return; + } + + dstr_ensure_capacity(dst, num + 1); + dst->array[num] = 0; + dst->len = num; +} + +static inline bool dstr_is_empty(const struct dstr *str) +{ + if (!str->array || !str->len) + return true; + if (!*str->array) + return true; + + return false; +} + +static inline void dstr_cat(struct dstr *dst, const char *array) +{ + size_t len; + if (!array || !*array) + return; + + len = strlen(array); + dstr_ncat(dst, array, len); +} + +static inline void dstr_cat_ch(struct dstr *dst, char ch) +{ + dstr_ensure_capacity(dst, ++dst->len + 1); + dst->array[dst->len - 1] = ch; + dst->array[dst->len] = 0; +} + +static inline const char *dstr_find_i(const struct dstr *str, const char *find) +{ + return astrstri(str->array, find); +} + +static inline const char *dstr_find(const struct dstr *str, const char *find) +{ + return strstr(str->array, find); +} + +static inline int dstr_cmp(const struct dstr *str1, const char *str2) +{ + return strcmp(str1->array, str2); +} + +static inline int dstr_cmpi(const struct dstr *str1, const char *str2) +{ + return astrcmpi(str1->array, str2); +} + +static inline int dstr_ncmp(const struct dstr *str1, const char *str2, + const size_t n) +{ + return astrcmp_n(str1->array, str2, n); +} + +static inline int dstr_ncmpi(const struct dstr *str1, const char *str2, + const size_t n) +{ + return astrcmpi_n(str1->array, str2, n); +} + +static inline char dstr_end(const struct dstr *str) +{ + if (dstr_is_empty(str)) + return 0; + + return str->array[str->len - 1]; +} + +#ifdef __cplusplus +} +#endif +#endif From 3045e7e86dc2cb6a38cfc33ee0b2c9b3d861a4a3 Mon Sep 17 00:00:00 2001 From: gozfree Date: Sun, 3 Apr 2022 23:34:42 +0800 Subject: [PATCH 07/16] [libhal] update system info --- gear-lib/libhal/Makefile | 3 +- gear-lib/libhal/{libhal.c => hal_nix.c} | 88 ++++++++++++++++++++----- gear-lib/libhal/libhal.h | 25 +++++-- gear-lib/libhal/test_libhal.c | 18 ++++- 4 files changed, 108 insertions(+), 26 deletions(-) rename gear-lib/libhal/{libhal.c => hal_nix.c} (86%) diff --git a/gear-lib/libhal/Makefile b/gear-lib/libhal/Makefile index 1f285cb..b0db5c6 100644 --- a/gear-lib/libhal/Makefile +++ b/gear-lib/libhal/Makefile @@ -30,7 +30,7 @@ TGT_LIB_SO = $(LIBNAME).so TGT_LIB_SO_VER = $(TGT_LIB_SO).${VER} TGT_UNIT_TEST = test_$(LIBNAME) -OBJS_LIB = $(LIBNAME).o +OBJS_LIB = hal_nix.o OBJS_UNIT_TEST = test_$(LIBNAME).o ############################################################################### @@ -54,6 +54,7 @@ CFLAGS += -I$(OUTPUT)/include/gear-lib SHARED := -shared LDFLAGS := $($(ARCH)_LDFLAGS) +LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -ldarray -lposix LDFLAGS += -pthread ############################################################################### diff --git a/gear-lib/libhal/libhal.c b/gear-lib/libhal/hal_nix.c similarity index 86% rename from gear-lib/libhal/libhal.c rename to gear-lib/libhal/hal_nix.c index e207290..db99dae 100644 --- a/gear-lib/libhal/libhal.c +++ b/gear-lib/libhal/hal_nix.c @@ -20,6 +20,7 @@ * SOFTWARE. ******************************************************************************/ #include "libhal.h" +#include #include #include #include @@ -36,8 +37,8 @@ #include #include #include -#include -#include +//#include +//#include #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define is_str_equal(a,b) \ @@ -117,6 +118,7 @@ static ssize_t file_read_path(const char *path, void *data, size_t len) int network_get_info(const char *interface, struct network_info *info) { +#if 0 int ret = -1; int fd = 0; do { @@ -209,6 +211,8 @@ int network_get_info(const char *interface, struct network_info *info) close(fd); } return ret; +#endif + return 0; } int sdcard_get_info(const char *mount_point, struct sdcard_info *info) @@ -273,31 +277,81 @@ int sdcard_get_info(const char *mount_point, struct sdcard_info *info) int cpu_get_info(struct cpu_info *info) { FILE *fp; - char *p; - char buf[512]; + char *line = NULL; + size_t linecap = 0; + struct dstr proc_name; + struct dstr proc_speed; + int physical_id = -1; + int last_physical_id = -1; - info->cores = get_nprocs_conf(); - info->cores_available = get_nprocs(); + info->cores_physical = get_nprocs_conf(); + info->cores_logical = get_nprocs(); if (NULL == (fp = fopen("/proc/cpuinfo", "r"))) { printf("read cpuinfo failed!\n"); return -1; } - while (fgets(buf, 511, fp) != NULL) { - if (memcmp(buf, "flags", 5) == 0 ||//x86 - memcmp(buf, "Features", 8) == 0) {//arm - if (NULL != (p = strstr(buf, ": "))) { - strcpy(info->features, p+2); - } + + dstr_init(&proc_name); + dstr_init(&proc_speed); + + while (getline(&line, &linecap, fp) != -1) { + if (!strncmp(line, "model name", 10)) { + char *start = strchr(line, ':'); + if (!start || *(++start) == '\0') + continue; + dstr_copy(&proc_name, start); + dstr_resize(&proc_name, proc_name.len - 1); + dstr_depad(&proc_name); } - if (memcmp(buf, "model name", 10) == 0) { - if (NULL != (p = strstr(buf, ": "))) { - strcpy(info->name, p+2); - } + if (!strncmp(line, "physical id", 11)) { + char *start = strchr(line, ':'); + if (!start || *(++start) == '\0') + continue; + physical_id = atoi(start); + } + if (!strncmp(line, "cpu MHz", 7)) { + char *start = strchr(line, ':'); + if (!start || *(++start) == '\0') + continue; + dstr_copy(&proc_speed, start); + dstr_resize(&proc_speed, proc_speed.len - 1); + dstr_depad(&proc_speed); + } + + if (*line == '\n' && physical_id != last_physical_id) { + last_physical_id = physical_id; + strncpy(info->name, proc_name.array, sizeof(info->name)); + strncpy(info->speed, proc_speed.array, sizeof(info->speed)); } - memset(buf, 0, sizeof(buf)); } + fclose(fp); + dstr_free(&proc_name); + dstr_free(&proc_speed); + free(line); + return 0; +} + +int memory_get_info(struct memory_info *mem) +{ + struct sysinfo info; + if (sysinfo(&info) < 0) + return -1; + + mem->total = (uint64_t)info.totalram * info.mem_unit; + mem->free = ((uint64_t)info.freeram + (uint64_t)info.bufferram) * info.mem_unit; + return 0; +} + +int os_get_version(struct os_info *os) +{ + struct utsname info; + if (uname(&info) < 0) + return -1; + + strncpy(os->sysname, info.sysname, sizeof(os->sysname)); + strncpy(os->release, info.release, sizeof(os->release)); return 0; } diff --git a/gear-lib/libhal/libhal.h b/gear-lib/libhal/libhal.h index 638dae3..df4490f 100644 --- a/gear-lib/libhal/libhal.h +++ b/gear-lib/libhal/libhal.h @@ -22,12 +22,10 @@ #ifndef LIBHAL_H #define LIBHAL_H +#include #include #include #include -#include -#include -#include #define LIBHAL_VERSION "0.1.0" @@ -36,12 +34,25 @@ extern "C" { #endif struct cpu_info { - int cores; - int cores_available; + int cores_logical; + int cores_physical; char name[128]; + char speed[16]; char features[1024]; }; +struct memory_info { + uint64_t total; + uint64_t free; +}; + +struct os_info { + char sysname[128]; + char release[128]; +}; + + + struct sdcard_info { bool is_insert; bool is_mounted; @@ -59,6 +70,9 @@ struct network_info { bool is_running; char ipaddr[16]; char macaddr[32]; +#ifndef IW_ESSID_MAX_SIZE +#define IW_ESSID_MAX_SIZE 32 +#endif char ssid[IW_ESSID_MAX_SIZE+1]; char pswd[64]; }; @@ -75,6 +89,7 @@ int network_get_info(const char *interface, struct network_info *info); int network_get_port_occupied(struct network_ports *ports); int sdcard_get_info(const char *mount_point, struct sdcard_info *info); int cpu_get_info(struct cpu_info *info); +int memory_get_info(struct memory_info *info); int system_noblock(char **argv); ssize_t system_with_result(const char *cmd, void *buf, size_t count); diff --git a/gear-lib/libhal/test_libhal.c b/gear-lib/libhal/test_libhal.c index fa91e9e..0fe12d7 100644 --- a/gear-lib/libhal/test_libhal.c +++ b/gear-lib/libhal/test_libhal.c @@ -22,6 +22,8 @@ #include "libhal.h" #include #include +#define __STDC_FORMAT_MACROS +#include void foo2() { @@ -35,14 +37,24 @@ int main(int argc, char **argv) struct network_ports ports; struct network_info ni; struct cpu_info ci; + struct memory_info mi; + struct os_info oi; int i; foo2(); network_get_info("lo", &ni); cpu_get_info(&ci); printf("%s\n", ni.ipaddr); - printf("cores = %d, cores_available = %d\n", ci.cores, ci.cores_available); - printf("features = %s\n", ci.features); - printf("name = %s\n", ci.name); + printf("CPU name: %s\n", ci.name); + printf("CPU speed: %s\n", ci.name); + printf("Physical cores: %d, Logical cores: %d\n", ci.cores_physical, ci.cores_logical); + + memory_get_info(&mi); + printf("Physical Memory: %" PRIu64 "MB Total, %" PRIu64 "MB Free", + mi.total/1024/1024, mi.free/1024/1024); + + os_get_info(&oi); + + printf("Kernel Version: %s %s", oi.sysname, oi.release); network_get_port_occupied(&ports); for (i = 0; i < ports.tcp_cnt; i++) { printf("tcp_ports = %d\n", ports.tcp[i]); From 981c63430e069ce99e39a8f76e119b0eedb168a7 Mon Sep 17 00:00:00 2001 From: gozfree Date: Thu, 7 Apr 2022 11:20:57 +0800 Subject: [PATCH 08/16] [libhal] add hal_nix and hal_win --- .../visual studio 2010/gear-lib/gear-lib.sln | 10 + .../gear-lib/libdarray/libdarray.vcxproj | 2 + .../gear-lib/libhal/libhal.vcxproj | 77 +++++++ .../gear-lib/libhal/libhal.vcxproj.filters | 30 +++ .../gear-lib/libhal/libhal.vcxproj.user | 3 + gear-lib/libdarray/libdstring.c | 6 +- gear-lib/libfile/libfile.c | 6 +- gear-lib/libhal/hal_nix.c | 16 +- gear-lib/libhal/hal_win.c | 218 ++++++++++++++++++ gear-lib/libhal/libhal.h | 10 +- gear-lib/libhal/test_libhal.c | 5 +- gear-lib/libposix/Makefile.nmake | 24 +- 12 files changed, 385 insertions(+), 22 deletions(-) create mode 100644 build/visual studio 2010/gear-lib/libhal/libhal.vcxproj create mode 100644 build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.filters create mode 100644 build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.user create mode 100644 gear-lib/libhal/hal_win.c diff --git a/build/visual studio 2010/gear-lib/gear-lib.sln b/build/visual studio 2010/gear-lib/gear-lib.sln index 8b3e627..1af5c5e 100644 --- a/build/visual studio 2010/gear-lib/gear-lib.sln +++ b/build/visual studio 2010/gear-lib/gear-lib.sln @@ -96,6 +96,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libuvc", "libuvc\libuvc.vcx {1CA3BCD5-B8F1-4C37-A55E-C29E39CB7EB6} = {1CA3BCD5-B8F1-4C37-A55E-C29E39CB7EB6} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libhal", "libhal\libhal.vcxproj", "{496D075D-50CA-465D-9D0A-D9229257FC48}" + ProjectSection(ProjectDependencies) = postProject + {88F2DE94-0375-40CF-92C0-70B98C56B508} = {88F2DE94-0375-40CF-92C0-70B98C56B508} + {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} = {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -166,6 +172,10 @@ Global {E98FC40C-984D-4889-AFF4-5CD7CB03FEAB}.Debug|Win32.Build.0 = Debug|Win32 {E98FC40C-984D-4889-AFF4-5CD7CB03FEAB}.Release|Win32.ActiveCfg = Release|Win32 {E98FC40C-984D-4889-AFF4-5CD7CB03FEAB}.Release|Win32.Build.0 = Release|Win32 + {496D075D-50CA-465D-9D0A-D9229257FC48}.Debug|Win32.ActiveCfg = Debug|Win32 + {496D075D-50CA-465D-9D0A-D9229257FC48}.Debug|Win32.Build.0 = Debug|Win32 + {496D075D-50CA-465D-9D0A-D9229257FC48}.Release|Win32.ActiveCfg = Release|Win32 + {496D075D-50CA-465D-9D0A-D9229257FC48}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/build/visual studio 2010/gear-lib/libdarray/libdarray.vcxproj b/build/visual studio 2010/gear-lib/libdarray/libdarray.vcxproj index 28484a4..22ab780 100644 --- a/build/visual studio 2010/gear-lib/libdarray/libdarray.vcxproj +++ b/build/visual studio 2010/gear-lib/libdarray/libdarray.vcxproj @@ -64,10 +64,12 @@ + + diff --git a/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj new file mode 100644 index 0000000..aa752d5 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj @@ -0,0 +1,77 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {496D075D-50CA-465D-9D0A-D9229257FC48} + libhal + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + ..\..\..\..\gear-lib\libposix;..\..\..\..\gear-lib\libposix\MsvcLibX\include;..\..\..\..\gear-lib\libposix\pthreads4w;..\..\..\..\gear-lib\libdarray;%(AdditionalIncludeDirectories) + "/DUCRTINCLUDE=$(VCInstallDir)include" "/DMSVCINCLUDE=$(VCInstallDir)include" "/DWSDKINCLUDE=$(WindowsSdkDir)Include" %(AdditionalOptions) + MultiThreaded + + + true + libposix.lib;%(AdditionalDependencies) + ..\Debug;%(AdditionalLibraryDirectories) + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.filters b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.filters new file mode 100644 index 0000000..6785ee0 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.user b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.user new file mode 100644 index 0000000..695b5c7 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/gear-lib/libdarray/libdstring.c b/gear-lib/libdarray/libdstring.c index a6bbb60..587d97a 100644 --- a/gear-lib/libdarray/libdstring.c +++ b/gear-lib/libdarray/libdstring.c @@ -553,10 +553,11 @@ void dstr_catf(struct dstr *dst, const char *format, ...) void dstr_vprintf(struct dstr *dst, const char *format, va_list args) { + int len; va_list args_cp; va_copy(args_cp, args); - int len = vsnprintf(NULL, 0, format, args_cp); + len = vsnprintf(NULL, 0, format, args_cp); va_end(args_cp); if (len < 0) @@ -575,10 +576,11 @@ void dstr_vprintf(struct dstr *dst, const char *format, va_list args) void dstr_vcatf(struct dstr *dst, const char *format, va_list args) { + int len; va_list args_cp; va_copy(args_cp, args); - int len = vsnprintf(NULL, 0, format, args_cp); + len = vsnprintf(NULL, 0, format, args_cp); va_end(args_cp); if (len < 0) diff --git a/gear-lib/libfile/libfile.c b/gear-lib/libfile/libfile.c index 3460c58..79f297f 100644 --- a/gear-lib/libfile/libfile.c +++ b/gear-lib/libfile/libfile.c @@ -517,12 +517,12 @@ int file_dir_size(const char *path, uint64_t *size) int file_num_in_dir(const char *path) { - if (!path) { - return -1; - } DIR *dir = NULL; struct dirent *ent = NULL; int num = 0; + if (!path) { + return -1; + } dir = opendir(path); if (dir == NULL) { return -1; diff --git a/gear-lib/libhal/hal_nix.c b/gear-lib/libhal/hal_nix.c index db99dae..d1b55a2 100644 --- a/gear-lib/libhal/hal_nix.c +++ b/gear-lib/libhal/hal_nix.c @@ -35,10 +35,9 @@ #include #include #include +#include #include #include -//#include -//#include #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define is_str_equal(a,b) \ @@ -335,9 +334,9 @@ int cpu_get_info(struct cpu_info *info) int memory_get_info(struct memory_info *mem) { - struct sysinfo info; - if (sysinfo(&info) < 0) - return -1; + struct sysinfo info; + if (sysinfo(&info) < 0) + return -1; mem->total = (uint64_t)info.totalram * info.mem_unit; mem->free = ((uint64_t)info.freeram + (uint64_t)info.bufferram) * info.mem_unit; @@ -346,12 +345,13 @@ int memory_get_info(struct memory_info *mem) int os_get_version(struct os_info *os) { - struct utsname info; - if (uname(&info) < 0) - return -1; + struct utsname info; + if (uname(&info) < 0) + return -1; strncpy(os->sysname, info.sysname, sizeof(os->sysname)); strncpy(os->release, info.release, sizeof(os->release)); + printf("Kernel Version: %s %s\n", os->sysname, os->release); return 0; } diff --git a/gear-lib/libhal/hal_win.c b/gear-lib/libhal/hal_win.c new file mode 100644 index 0000000..8d3fbad --- /dev/null +++ b/gear-lib/libhal/hal_win.c @@ -0,0 +1,218 @@ +/****************************************************************************** + * Copyright (C) 2014-2020 Zhifeng Gong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + ******************************************************************************/ +#include "libhal.h" +#include +#include +#include +#include +#include +#include +#include +#include + +struct win_version_info { + int major; + int minor; + int build; + int revis; +}; + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define is_str_equal(a,b) \ + ((strlen(a) == strlen(b)) && (0 == strcasecmp(a,b))) + +#define MAX_NETIF_NUM 8 +#define SYS_CLASS_NET "/sys/class/net" +#define SYS_BLK_MMCBLK0 "/sys/block/mmcblk0/device" +#define DEV_MMCBLK0 "/dev/mmcblk0" +#define DEV_MMCBLK0P1 "/dev/mmcblk0p1" +#define SYSTEM_MOUNTS "/proc/self/mounts" + +int network_get_info(const char *intf, struct network_info *info) +{ + return 0; +} + +int sdcard_get_info(const char *mount_point, struct sdcard_info *info) +{ + return 0; +} + +int cpu_get_info(struct cpu_info *info) +{ + return 0; +} + +int memory_get_info(struct memory_info *mem) +{ + return 0; +} + +typedef DWORD(WINAPI *get_file_version_info_size_w_t)(LPCWSTR module, LPDWORD unused); +typedef BOOL(WINAPI *get_file_version_info_w_t)(LPCWSTR module, DWORD unused, DWORD len, LPVOID data); +typedef BOOL(WINAPI *ver_query_value_w_t)(LPVOID data, LPCWSTR subblock, LPVOID *buf, PUINT sizeout); + +static bool get_dll_ver(const wchar_t *lib, struct win_version_info *ver_info) +{ + VS_FIXEDFILEINFO *info = NULL; + UINT len = 0; + BOOL success; + LPVOID data; + DWORD size; + char utf8_lib[512]; + get_file_version_info_size_w_t get_file_version_info_size = NULL; + get_file_version_info_w_t get_file_version_info = NULL; + ver_query_value_w_t ver_query_value = NULL; + + HMODULE ver = GetModuleHandleW(L"version"); + + if (!ver) { + ver = LoadLibraryW(L"version"); + if (!ver) { + printf("Failed to load windows version library"); + return false; + } + } + + get_file_version_info_size = (get_file_version_info_size_w_t)GetProcAddress(ver, "GetFileVersionInfoSizeW"); + get_file_version_info = (get_file_version_info_w_t)GetProcAddress(ver, "GetFileVersionInfoW"); + ver_query_value = (ver_query_value_w_t)GetProcAddress(ver, "VerQueryValueW"); + + if (!get_file_version_info_size || !get_file_version_info || + !ver_query_value) { + printf("Failed to load windows version functions"); + return false; + } + + wcs_to_utf8(lib, 0, utf8_lib, sizeof(utf8_lib)); + + size = get_file_version_info_size(lib, NULL); + if (!size) { + printf("Failed to get %s version info size", utf8_lib); + return false; + } + + data = malloc(size); + if (!get_file_version_info(lib, 0, size, data)) { + printf("Failed to get %s version info", utf8_lib); + free(data); + return false; + } + + success = ver_query_value(data, L"\\", (LPVOID *)&info, &len); + if (!success || !info || !len) { + printf("Failed to get %s version info value", utf8_lib); + free(data); + return false; + } + + ver_info->major = (int)HIWORD(info->dwFileVersionMS); + ver_info->minor = (int)LOWORD(info->dwFileVersionMS); + ver_info->build = (int)HIWORD(info->dwFileVersionLS); + ver_info->revis = (int)LOWORD(info->dwFileVersionLS); + + free(data); + FreeLibrary(ver); + + return true; +} + +static void get_win_ver(struct win_version_info *info) +{ +#define WINVER_REG_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" + struct win_version_info ver = {0}; + + if (!info) + return; + + get_dll_ver(L"kernel32", &ver); + + if (ver.major == 10) { + HKEY key; + DWORD size, win10_revision; + LSTATUS status; + + status = RegOpenKeyW(HKEY_LOCAL_MACHINE, WINVER_REG_KEY, &key); + if (status != ERROR_SUCCESS) + return; + + size = sizeof(win10_revision); + + status = RegQueryValueExW(key, L"UBR", NULL, NULL, (LPBYTE)&win10_revision, &size); + if (status == ERROR_SUCCESS) + ver.revis = (int)win10_revision > ver.revis ? (int)win10_revision : ver.revis; + RegCloseKey(key); + } + + *info = ver; +} + +static bool is_64_bit_windows(void) +{ +#if defined(_WIN64) + return true; +#elif defined(_WIN32) + BOOL b64 = false; + return IsWow64Process(GetCurrentProcess(), &b64) && b64; +#endif +} + +int os_get_version(struct os_info *os) +{ + bool b64; + const char *windows_bitness; + struct win_version_info ver; + get_win_ver(&ver); + + b64 = is_64_bit_windows(); + windows_bitness = b64 ? "64" : "32"; + + os->major = ver.major; + os->minor = ver.minor; + os->build = ver.build; + os->revis = ver.revis; + os->bit = b64 ? 64 : 32; + + printf("Windows Version: %d.%d Build %d (revision: %d; %s-bit)", + ver.major, ver.minor, ver.build, ver.revis, windows_bitness); + return 0; +} + +int network_get_port_occupied(struct network_ports *np) +{ + return 0; +} + +int system_noblock(char **argv) +{ + return 0; +} + +ssize_t system_with_result(const char *cmd, void *buf, size_t count) +{ + return 0; +} + +bool proc_exist(const char *proc) +{ + return false; +} diff --git a/gear-lib/libhal/libhal.h b/gear-lib/libhal/libhal.h index df4490f..75f415d 100644 --- a/gear-lib/libhal/libhal.h +++ b/gear-lib/libhal/libhal.h @@ -49,10 +49,13 @@ struct memory_info { struct os_info { char sysname[128]; char release[128]; + int major; + int minor; + int build; + int revis; + int bit; }; - - struct sdcard_info { bool is_insert; bool is_mounted; @@ -85,11 +88,12 @@ struct network_ports { }; -int network_get_info(const char *interface, struct network_info *info); +int network_get_info(const char *inf, struct network_info *info); int network_get_port_occupied(struct network_ports *ports); int sdcard_get_info(const char *mount_point, struct sdcard_info *info); int cpu_get_info(struct cpu_info *info); int memory_get_info(struct memory_info *info); +int os_get_version(struct os_info *os); int system_noblock(char **argv); ssize_t system_with_result(const char *cmd, void *buf, size_t count); diff --git a/gear-lib/libhal/test_libhal.c b/gear-lib/libhal/test_libhal.c index 0fe12d7..042a628 100644 --- a/gear-lib/libhal/test_libhal.c +++ b/gear-lib/libhal/test_libhal.c @@ -49,12 +49,11 @@ int main(int argc, char **argv) printf("Physical cores: %d, Logical cores: %d\n", ci.cores_physical, ci.cores_logical); memory_get_info(&mi); - printf("Physical Memory: %" PRIu64 "MB Total, %" PRIu64 "MB Free", + printf("Physical Memory: %" PRIu64 "MB Total, %" PRIu64 "MB Free\n", mi.total/1024/1024, mi.free/1024/1024); - os_get_info(&oi); + os_get_version(&oi); - printf("Kernel Version: %s %s", oi.sysname, oi.release); network_get_port_occupied(&ports); for (i = 0; i < ports.tcp_cnt; i++) { printf("tcp_ports = %d\n", ports.tcp[i]); diff --git a/gear-lib/libposix/Makefile.nmake b/gear-lib/libposix/Makefile.nmake index d7a6d0b..ef854ec 100644 --- a/gear-lib/libposix/Makefile.nmake +++ b/gear-lib/libposix/Makefile.nmake @@ -2,6 +2,8 @@ # common ############################################################################### #ARCH: linux/pi/android/ios/win +#!include +!include <../../build/win.nmake> LD = link AR = lib RM = del @@ -13,20 +15,36 @@ TGT_LIB_A = $(LIBNAME).lib TGT_LIB_SO = $(LIBNAME).dll TGT_UNIT_TEST = test_$(LIBNAME).exe -OBJS_LIB = $(LIBNAME).obj libposix4win.obj +OBJS_LIB = $(LIBNAME).obj libposix4win.obj pthreads4w/pthread.obj OBJS_UNIT_TEST = test_$(LIBNAME).obj ############################################################################### # cflags and ldflags ############################################################################### -CFLAGS = /I. +CFLAGS = /I. /I./MsvcLibX/include /I./pthreads4w #"/DWSDKINCLUDE=$(WindowsSdkDir)\include" "/DMSVCINCLUDE=$(VCInstallDir)\include" "/DUCRTINCLUDE=$(VCInstallDir)\include" !IF "$(MODE)"=="release" CFLAGS = $(CFLAGS) /O2 /GF !ELSE CFLAGS = $(CFLAGS) /Od /W3 /Zi !ENDIF -LIBS = /NOLOGO ws2_32.lib +CFLAGS = $(CFLAGS) "/DUCRTINCLUDE=$(VCInstallDir)\include" +CFLAGS = $(CFLAGS) "/DMSVCINCLUDE=$(VCInstallDir)\include" +CFLAGS = $(CFLAGS) "/DWSDKINCLUDE=$(WindowsSdkDir)\include" + +LIBS = /NOLOGO KERNEL32.LIB Mpr.lib user32.lib ws2_32.lib + +#kernel32.lib +#gdi32.lib +#winspool.lib +#comdlg32.lib +#advapi32.lib +#shell32.lib +#ole32.lib +#oleaut32.lib +#uuid.lib +#odbc32.lib +#odbccp32.lib ############################################################################### # target From cda6760a82ad38dd57a81484a8a7a9660d58005f Mon Sep 17 00:00:00 2001 From: gozfree Date: Fri, 8 Apr 2022 16:44:15 +0800 Subject: [PATCH 09/16] [libuvc] support dshow on win10 --- .../gear-lib/libuvc/libuvc.vcxproj | 5 ++- .../gear-lib/libuvc/libuvc.vcxproj.filters | 3 -- gear-lib/libuvc/dshow.c | 41 +++++++++++++++---- gear-lib/libuvc/dshow.h | 2 + gear-lib/libuvc/libuvc.c | 9 ++-- gear-lib/libuvc/libuvc.h | 6 +-- gear-lib/libuvc/test_libuvc.c | 2 + 7 files changed, 49 insertions(+), 19 deletions(-) diff --git a/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj b/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj index 2f8ecd8..a2ad3e3 100644 --- a/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj +++ b/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj @@ -16,7 +16,7 @@ - StaticLibrary + Application true MultiByte @@ -47,6 +47,8 @@ true + libposix.lib;libfile.lib;libmedia-io.lib;Mpr.lib;strmiids.lib;%(AdditionalDependencies) + ..\Debug;%(AdditionalLibraryDirectories) @@ -70,7 +72,6 @@ - diff --git a/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj.filters b/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj.filters index 96b8645..025e030 100644 --- a/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj.filters +++ b/build/visual studio 2010/gear-lib/libuvc/libuvc.vcxproj.filters @@ -20,9 +20,6 @@ Source Files - - Source Files - Source Files diff --git a/gear-lib/libuvc/dshow.c b/gear-lib/libuvc/dshow.c index 00c1068..8a8df8b 100644 --- a/gear-lib/libuvc/dshow.c +++ b/gear-lib/libuvc/dshow.c @@ -19,13 +19,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ +#define _CRT_SECURE_NO_WARNINGS /* Disable safety warning for wcscpy() */ +#include "dshow.h" #include "libuvc.h" #include -#include "dshow.h" #include "libuvc.h" #include -struct file *fp; struct dshow_ctx { int fd; @@ -35,6 +35,8 @@ struct dshow_ctx { int height; uint32_t fps_num; uint32_t fps_den; + uint64_t first_ts; + uint64_t frame_id; IGraphBuilder *graph; ICaptureGraphBuilder2 *capgraph; IMediaControl *mctrl; @@ -46,6 +48,8 @@ struct dshow_ctx { HANDLE mutex; HANDLE event[2]; /* event[0] is set by DirectShow * event[1] is set by callback() */ + struct uvc_ctx *uvc; + struct video_frame frame; }; @@ -954,11 +958,32 @@ static void dshow_callback(void *priv_data, int index, uint8_t *buf, int buf_siz int64_t time, int devtype) { struct dshow_ctx *c = priv_data; + struct uvc_ctx *uvc = c->uvc; + struct video_frame *frame = &c->frame; + uint8_t *start; + int i; - //printf("%s index=%d, buf=%p, buf_size=%d, time=%d, devtype=%d\n", __func__, - // index, buf, buf_size, time, devtype); + printf("%s index=%d, buf=%p, buf_size=%d, time=%lld, devtype=%d\n", __func__, + index, buf, buf_size, time, devtype); + frame->timestamp = time; + if (c->frame_id == 0) { + c->first_ts = frame->timestamp; + } + frame->timestamp -= c->first_ts; + frame->frame_id = c->frame_id; + if (frame->total_size != buf_size) { + printf("%s:%d warn: buf_size=%d, frame_size=%d, not matched!\n", __func__, __LINE__, + buf_size, frame->total_size); + } + c->frame_id++; - file_write(fp, buf, buf_size); + start = buf; + if (uvc->on_video_frame) { + for (i = 0; i < frame->planes; ++i) { + frame->data[i] = start + frame->plane_offsets[i]; + } + uvc->on_video_frame(uvc, frame); + } #if 0 SetEvent(c->event[1]); #endif @@ -1055,10 +1080,11 @@ static void *dshow_open(struct uvc_ctx *uvc, const char *dev, struct uvc_config c->height = conf->height; c->fps_num = conf->fps.num;//unlimit fps c->fps_den = conf->fps.den; + c->uvc = uvc; + c->frame_id = 0; CoInitialize(0); - fp = file_open("uvc.yuv", F_CREATE); if (create_dshow_graph(c)) { printf("create_dshow_graph failed!\n"); goto exit; @@ -1080,9 +1106,10 @@ static void *dshow_open(struct uvc_ctx *uvc, const char *dev, struct uvc_config uvc->conf.height = c->height; uvc->conf.fps.num = c->fps_num; uvc->conf.fps.den = c->fps_den; - uvc->conf.format = PIXEL_FORMAT_I420; + uvc->conf.format = conf->format; uvc->fd = -1; + video_frame_init(&c->frame, uvc->conf.format, uvc->conf.width, uvc->conf.height, MEDIA_MEM_SHALLOW); return c; exit: if (c) diff --git a/gear-lib/libuvc/dshow.h b/gear-lib/libuvc/dshow.h index a669630..b9ba163 100644 --- a/gear-lib/libuvc/dshow.h +++ b/gear-lib/libuvc/dshow.h @@ -2,6 +2,7 @@ #define DSHOW_H #define COBJMACROS +#define CINTERFACE #define WIN32_LEAN_AND_MEAN #include #include @@ -13,6 +14,7 @@ #include #include #include +#include enum dshowDeviceType { VideoDevice = 0, diff --git a/gear-lib/libuvc/libuvc.c b/gear-lib/libuvc/libuvc.c index 045ede6..34eeece 100644 --- a/gear-lib/libuvc/libuvc.c +++ b/gear-lib/libuvc/libuvc.c @@ -28,16 +28,17 @@ #include #include -extern struct uvc_ops dummy_ops; #if defined (OS_LINUX) +extern struct uvc_ops dummy_ops; extern struct uvc_ops v4l2_ops; #elif defined (OS_WINDOWS) extern struct uvc_ops dshow_ops; #endif static struct uvc_ops *uvc_ops[] = { - &dummy_ops, + #if defined (OS_LINUX) + &dummy_ops, &v4l2_ops, #elif defined (OS_WINDOWS) &dshow_ops, @@ -62,7 +63,7 @@ struct uvc_ctx *uvc_open(enum uvc_type type, const char *dev, struct uvc_config printf("uvc->ops %d is NULL!\n", type); return NULL; } - uvc->opaque = uvc->ops->open(uvc, dev, conf); + uvc->opaque = uvc->ops->_open(uvc, dev, conf); if (!uvc->opaque) { printf("open %s failed!\n", dev); goto failed; @@ -135,6 +136,6 @@ void uvc_close(struct uvc_ctx *uvc) printf("%s:%d invalid paraments!\n", __func__, __LINE__); return; } - uvc->ops->close(uvc); + uvc->ops->_close(uvc); free(uvc); } diff --git a/gear-lib/libuvc/libuvc.h b/gear-lib/libuvc/libuvc.h index 61b9f77..8c91dc8 100644 --- a/gear-lib/libuvc/libuvc.h +++ b/gear-lib/libuvc/libuvc.h @@ -38,8 +38,8 @@ extern "C" { #endif enum uvc_type { - UVC_TYPE_DUMMY = 0, #if defined (OS_LINUX) + UVC_TYPE_DUMMY = 0, UVC_TYPE_V4L2, #elif defined (OS_WINDOWS) UVC_TYPE_DSHOW, @@ -107,8 +107,8 @@ struct video_ctrl { struct uvc_ops { - void *(*open)(struct uvc_ctx *uvc, const char *dev, struct uvc_config *conf); - void (*close)(struct uvc_ctx *c); + void *(*_open)(struct uvc_ctx *uvc, const char *dev, struct uvc_config *conf); + void (*_close)(struct uvc_ctx *c); int (*ioctl)(struct uvc_ctx *c, unsigned long int cmd, ...); int (*start_stream)(struct uvc_ctx *c); int (*stop_stream)(struct uvc_ctx *c); diff --git a/gear-lib/libuvc/test_libuvc.c b/gear-lib/libuvc/test_libuvc.c index cf656e0..5b9a5fd 100644 --- a/gear-lib/libuvc/test_libuvc.c +++ b/gear-lib/libuvc/test_libuvc.c @@ -97,6 +97,7 @@ int v4l2_test() int dummy_test() { +#if defined (OS_LINUX) struct video_frame *frm; struct uvc_config conf = { 320, @@ -126,6 +127,7 @@ int dummy_test() video_frame_destroy(frm); uvc_close(uvc); printf("write %s fininshed!\n", OUTPUT_DUMMY); +#endif return 0; } From c7c5889bec8a1a9667808a731bd9243cd77e985a Mon Sep 17 00:00:00 2001 From: gozfree Date: Sat, 9 Apr 2022 10:29:51 +0800 Subject: [PATCH 10/16] [libdarray] rm unused header file --- gear-lib/libdarray/libdstring.c | 10 ---------- gear-lib/libdarray/libdstring.h | 7 ------- gear-lib/libmedia-io/Makefile | 3 ++- gear-lib/libposix/libposix.c | 1 + 4 files changed, 3 insertions(+), 18 deletions(-) diff --git a/gear-lib/libdarray/libdstring.c b/gear-lib/libdarray/libdstring.c index 587d97a..66bef81 100644 --- a/gear-lib/libdarray/libdstring.c +++ b/gear-lib/libdarray/libdstring.c @@ -31,16 +31,6 @@ #include #include -#if 0 -#include "c99defs.h" -#include "dstr.h" -#include "darray.h" -#include "bmem.h" -#include "utf8.h" -#include "lexer.h" -#include "platform.h" -#endif - static const char *astrblank = ""; static const wchar_t *wstrblank = L""; diff --git a/gear-lib/libdarray/libdstring.h b/gear-lib/libdarray/libdstring.h index 6f57a9b..dc86157 100644 --- a/gear-lib/libdarray/libdstring.h +++ b/gear-lib/libdarray/libdstring.h @@ -28,13 +28,6 @@ #include #include -#if 0 -#include -#include -#include "c99defs.h" -#include "bmem.h" -#endif - /* * Dynamic string * diff --git a/gear-lib/libmedia-io/Makefile b/gear-lib/libmedia-io/Makefile index 8f538f8..ccb7f5a 100644 --- a/gear-lib/libmedia-io/Makefile +++ b/gear-lib/libmedia-io/Makefile @@ -54,6 +54,7 @@ CFLAGS += -I$(OUTPUT)/include/gear-lib SHARED := -shared LDFLAGS := $($(ARCH)_LDFLAGS) +LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -lposix LDFLAGS += -pthread ############################################################################### @@ -76,7 +77,7 @@ $(TGT_LIB_A): $(OBJS_LIB) $(AR_V) rcs $@ $^ $(TGT_LIB_SO): $(OBJS_LIB) - $(CC_V) -o $@ $^ $(SHARED) + $(CC_V) -o $@ $^ $(SHARED) $(LDFLAGS) @mv $(TGT_LIB_SO) $(TGT_LIB_SO_VER) @ln -sf $(TGT_LIB_SO_VER) $(TGT_LIB_SO) diff --git a/gear-lib/libposix/libposix.c b/gear-lib/libposix/libposix.c index 977c4cc..e76c9dc 100644 --- a/gear-lib/libposix/libposix.c +++ b/gear-lib/libposix/libposix.c @@ -19,6 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ +#define _CRT_SECURE_NO_WARNINGS /* Disable safety warning for mbstowcs() */ #include "libposix.h" #include #include From a86e645fa252d4ad09038cff128d98cc6e65703b Mon Sep 17 00:00:00 2001 From: pi-zero Date: Sat, 9 Apr 2022 11:25:14 +0800 Subject: [PATCH 11/16] fix compile on raspbarry pi --- gear-lib/libipc/Makefile | 2 +- gear-lib/libsock/Makefile | 2 +- gear-lib/libuvc/dummy.c | 4 ++-- gear-lib/libuvc/v4l2.c | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gear-lib/libipc/Makefile b/gear-lib/libipc/Makefile index 76ff8f7..72012fc 100644 --- a/gear-lib/libipc/Makefile +++ b/gear-lib/libipc/Makefile @@ -61,7 +61,7 @@ EXTRA_CFLAGS += -I$(OUTPUT)/include/gear-lib SHARED := -shared EXTRA_LDFLAGS := $($(ARCH)_LDFLAGS) -EXTRA_LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -ldict -lgevent -ldarray -lthread +EXTRA_LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -lposix -ldict -lgevent -ldarray -lthread EXTRA_LDFLAGS += -pthread -lrt diff --git a/gear-lib/libsock/Makefile b/gear-lib/libsock/Makefile index 967dd19..bf202cb 100644 --- a/gear-lib/libsock/Makefile +++ b/gear-lib/libsock/Makefile @@ -72,7 +72,7 @@ SHARED := -shared LDFLAGS := $($(ARCH)_LDFLAGS) LDFLAGS += -pthread ifeq ($(ENABLE_SOCK_EXT), 1) -LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -lgevent -lthread +LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -lposix -lgevent -lthread endif ifeq ($(ENABLE_PTCP), 1) LDFLAGS += -L$(OUTLIBPATH)/lib/gear-lib -lptcp diff --git a/gear-lib/libuvc/dummy.c b/gear-lib/libuvc/dummy.c index 30cde5f..67111cb 100644 --- a/gear-lib/libuvc/dummy.c +++ b/gear-lib/libuvc/dummy.c @@ -330,8 +330,8 @@ static void uvc_dummy_close(struct uvc_ctx *uvc) } struct uvc_ops dummy_ops = { - .open = uvc_dummy_open, - .close = uvc_dummy_close, + ._open = uvc_dummy_open, + ._close = uvc_dummy_close, .ioctl = uvc_dummy_ioctl, .start_stream = uvc_dummy_start_stream, .stop_stream = uvc_dummy_stop_stream, diff --git a/gear-lib/libuvc/v4l2.c b/gear-lib/libuvc/v4l2.c index 784cf4a..f0c61eb 100644 --- a/gear-lib/libuvc/v4l2.c +++ b/gear-lib/libuvc/v4l2.c @@ -1004,8 +1004,8 @@ static int uvc_v4l2_ioctl(struct uvc_ctx *uvc, unsigned long int cmd, ...) } struct uvc_ops v4l2_ops = { - .open = uvc_v4l2_open, - .close = uvc_v4l2_close, + ._open = uvc_v4l2_open, + ._close = uvc_v4l2_close, .ioctl = uvc_v4l2_ioctl, .start_stream = uvc_v4l2_start_stream, .stop_stream = uvc_v4l2_stop_stream, From 217dafe140f04c0e51cc5c42fe6f22bc1c1ac2e3 Mon Sep 17 00:00:00 2001 From: gozfree Date: Sun, 10 Apr 2022 00:09:15 +0800 Subject: [PATCH 12/16] [libgevent] adapt wepoll on win32 --- .../gear-lib/libgevent/libgevent.vcxproj | 1 + .../gear-lib/libhal/libhal.vcxproj | 2 +- gear-lib/libgevent/Makefile | 2 +- gear-lib/libgevent/epoll.c | 56 +++++++++++++------ gear-lib/libgevent/libgevent.c | 19 +++++-- gear-lib/libgevent/wepoll.c | 6 +- gear-lib/libgevent/wepoll.h | 6 +- gear-lib/libposix/libposix4win.h | 9 +-- 8 files changed, 69 insertions(+), 32 deletions(-) diff --git a/build/visual studio 2010/gear-lib/libgevent/libgevent.vcxproj b/build/visual studio 2010/gear-lib/libgevent/libgevent.vcxproj index 338ecf5..38512ba 100644 --- a/build/visual studio 2010/gear-lib/libgevent/libgevent.vcxproj +++ b/build/visual studio 2010/gear-lib/libgevent/libgevent.vcxproj @@ -69,6 +69,7 @@ + diff --git a/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj index aa752d5..754e8be 100644 --- a/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj +++ b/build/visual studio 2010/gear-lib/libhal/libhal.vcxproj @@ -16,7 +16,7 @@ - Application + StaticLibrary true MultiByte diff --git a/gear-lib/libgevent/Makefile b/gear-lib/libgevent/Makefile index 1333328..794fbcf 100644 --- a/gear-lib/libgevent/Makefile +++ b/gear-lib/libgevent/Makefile @@ -80,7 +80,7 @@ $(TGT_LIB_A): $(OBJS_LIB) $(AR_V) rcs $@ $^ $(TGT_LIB_SO): $(OBJS_LIB) - $(CC_V) -o $@ $^ $(SHARED) + $(CC_V) -o $@ $^ $(SHARED) $(LDFLAGS) @mv $(TGT_LIB_SO) $(TGT_LIB_SO_VER) @ln -sf $(TGT_LIB_SO_VER) $(TGT_LIB_SO) diff --git a/gear-lib/libgevent/epoll.c b/gear-lib/libgevent/epoll.c index 7f740ba..4291612 100644 --- a/gear-lib/libgevent/epoll.c +++ b/gear-lib/libgevent/epoll.c @@ -20,18 +20,28 @@ * SOFTWARE. ******************************************************************************/ #include "libgevent.h" -#if defined (OS_LINUX) -#ifndef __CYGWIN__ #include #include #include #include #include #include +#if defined (OS_LINUX) #include +#elif defined (OS_WINDOWS) +#include "wepoll.h" +#endif + +#if defined (OS_LINUX) +#define epoll_fd_t int +#define is_epoll_fd_invalid(fd) (fd == -1) +#elif defined (OS_WINDOWS) +#define epoll_fd_t HANDLE +#define is_epoll_fd_invalid(fd) (fd == NULL) +#endif #ifdef __ANDROID__ -#define EPOLLRDHUP (0x2000) +#define EPOLLRDHUP (1U << 13) #endif #define EPOLL_MAX_NEVENT (4096) @@ -39,17 +49,17 @@ (((LONG_MAX) - 999) / 1000) struct epoll_ctx { - int epfd; + epoll_fd_t epfd; int nevents; struct epoll_event *events; }; static void *epoll_init(void) { - int fd; + epoll_fd_t fd; struct epoll_ctx *ec; fd = epoll_create(1); - if (-1 == fd) { + if (is_epoll_fd_invalid(fd)) { printf("epoll_create errno=%d %s\n", errno, strerror(errno)); return NULL; } @@ -93,7 +103,6 @@ static int epoll_add(struct gevent_base *eb, struct gevent *e) epev.events |= EPOLLONESHOT; else epev.events &= ~EPOLLONESHOT; - epev.events |= EPOLLET; epev.data.ptr = (void *)e; @@ -129,7 +138,6 @@ static int epoll_mod(struct gevent_base *eb, struct gevent *e) epev.events |= EPOLLONESHOT; else epev.events &= ~EPOLLONESHOT; - epev.events |= EPOLLET; epev.data.ptr = (void *)e; @@ -201,12 +209,28 @@ static int epoll_dispatch(struct gevent_base *eb, struct timeval *tv) } struct gevent_ops epollops = { - .init = epoll_init, - .deinit = epoll_deinit, - .add = epoll_add, - .del = epoll_del, - .mod = epoll_mod, - .dispatch = epoll_dispatch, +#if defined (OS_LINUX) + .init = +#endif + epoll_init, +#if defined (OS_LINUX) + .deinit = +#endif + epoll_deinit, +#if defined (OS_LINUX) + .add = +#endif + epoll_add, +#if defined (OS_LINUX) + .del = +#endif + epoll_del, +#if defined (OS_LINUX) + .mod = +#endif + epoll_mod, +#if defined (OS_LINUX) + .dispatch = +#endif + epoll_dispatch, }; -#endif -#endif diff --git a/gear-lib/libgevent/libgevent.c b/gear-lib/libgevent/libgevent.c index ff1b7d4..f3902bf 100644 --- a/gear-lib/libgevent/libgevent.c +++ b/gear-lib/libgevent/libgevent.c @@ -22,20 +22,20 @@ #include "libgevent.h" #include #include -#if defined (OS_LINUX) || defined (OS_APPLE) #include #include +#include +#if defined (OS_LINUX) || defined (OS_APPLE) #ifndef __CYGWIN__ #include #endif #endif -#include #if defined (OS_LINUX) || defined (OS_RTTHREAD) || defined (OS_APPLE) extern const struct gevent_ops selectops; extern const struct gevent_ops pollops; #endif -#if defined (OS_LINUX) +#if defined (OS_LINUX) || defined (OS_WINDOWS) #ifndef __CYGWIN__ extern const struct gevent_ops epollops; #endif @@ -45,10 +45,16 @@ extern const struct gevent_ops iocpops; #endif enum gevent_backend_type { +#if defined (OS_LINUX) || defined (OS_RTTHREAD) || defined (OS_APPLE) GEVENT_SELECT, GEVENT_POLL, +#endif +#if defined (OS_LINUX) || defined (OS_WINDOWS) GEVENT_EPOLL, +#endif +#if defined (OS_WINDOWS) GEVENT_IOCP, +#endif }; struct gevent_backend { @@ -61,7 +67,7 @@ static struct gevent_backend gevent_backend_list[] = { {GEVENT_SELECT, &selectops}, {GEVENT_POLL, &pollops}, #endif -#if defined (OS_LINUX) +#if defined (OS_LINUX) || defined (OS_WINDOWS) #ifndef __CYGWIN__ {GEVENT_EPOLL, &epollops}, #endif @@ -76,7 +82,8 @@ static struct gevent_backend gevent_backend_list[] = { #elif defined (OS_APPLE) #define GEVENT_BACKEND GEVENT_POLL #elif defined (OS_WINDOWS) -#define GEVENT_BACKEND GEVENT_IOCP +//#define GEVENT_BACKEND GEVENT_IOCP +#define GEVENT_BACKEND GEVENT_EPOLL #endif static void event_in(int fd, void *arg) @@ -201,7 +208,7 @@ void gevent_base_loop_break(struct gevent_base *eb) void gevent_base_signal(struct gevent_base *eb) { - uint64_t notify; + uint64_t notify = '1'; if (sizeof(uint64_t) != write(eb->inner_fd, ¬ify, sizeof(uint64_t))) { perror("write error"); } diff --git a/gear-lib/libgevent/wepoll.c b/gear-lib/libgevent/wepoll.c index b24f64e..ebc7e71 100644 --- a/gear-lib/libgevent/wepoll.c +++ b/gear-lib/libgevent/wepoll.c @@ -47,7 +47,8 @@ enum EPOLL_EVENTS { EPOLLWRBAND = (int) (1U << 9), EPOLLMSG = (int) (1U << 10), /* Never reported. */ EPOLLRDHUP = (int) (1U << 13), - EPOLLONESHOT = (int) (1U << 31) + EPOLLONESHOT = (int) (1U << 30), + EPOLLET = (int) (1U << 31) /* Not used, just for compile compatible */ }; #define EPOLLIN (1U << 0) @@ -61,7 +62,8 @@ enum EPOLL_EVENTS { #define EPOLLWRBAND (1U << 9) #define EPOLLMSG (1U << 10) #define EPOLLRDHUP (1U << 13) -#define EPOLLONESHOT (1U << 31) +#define EPOLLONESHOT (1U << 30) +#define EPOLLET (1U << 31) #define EPOLL_CTL_ADD 1 #define EPOLL_CTL_MOD 2 diff --git a/gear-lib/libgevent/wepoll.h b/gear-lib/libgevent/wepoll.h index daf6bdb..a7781ad 100644 --- a/gear-lib/libgevent/wepoll.h +++ b/gear-lib/libgevent/wepoll.h @@ -50,7 +50,8 @@ enum EPOLL_EVENTS { EPOLLWRBAND = (int) (1U << 9), EPOLLMSG = (int) (1U << 10), /* Never reported. */ EPOLLRDHUP = (int) (1U << 13), - EPOLLONESHOT = (int) (1U << 31) + EPOLLONESHOT = (int) (1U << 30), + EPOLLET = (int) (1U << 31) /* Not used, just for compile compatible */ }; #define EPOLLIN (1U << 0) @@ -64,7 +65,8 @@ enum EPOLL_EVENTS { #define EPOLLWRBAND (1U << 9) #define EPOLLMSG (1U << 10) #define EPOLLRDHUP (1U << 13) -#define EPOLLONESHOT (1U << 31) +#define EPOLLONESHOT (1U << 30) +#define EPOLLET (1U << 31) #define EPOLL_CTL_ADD 1 #define EPOLL_CTL_MOD 2 diff --git a/gear-lib/libposix/libposix4win.h b/gear-lib/libposix/libposix4win.h index b8ae0cb..d6180c4 100644 --- a/gear-lib/libposix/libposix4win.h +++ b/gear-lib/libposix/libposix4win.h @@ -134,7 +134,10 @@ GEAR_API void usleep(unsigned long usec); typedef int clockid_t; #ifndef CLOCK_REALTIME -#define CLOCK_REALTIME ((clockid_t)1) +#define CLOCK_REALTIME ((clockid_t)0) +#endif +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC ((clockid_t)1) #endif #ifndef CLOCK_PROCESS_CPUTIME_ID #define CLOCK_PROCESS_CPUTIME_ID ((clockid_t)2) @@ -142,9 +145,7 @@ typedef int clockid_t; #ifndef CLOCK_THREAD_CPUTIME_ID #define CLOCK_THREAD_CPUTIME_ID ((clockid_t)3) #endif -#ifndef CLOCK_MONOTONIC -#define CLOCK_MONOTONIC ((clockid_t)4) -#endif + /****************************************************************************** From d3ea404bda48ff6c51fe657e73a9c16be8683274 Mon Sep 17 00:00:00 2001 From: gozfree Date: Sun, 10 Apr 2022 00:39:13 +0800 Subject: [PATCH 13/16] update readme --- README.cn.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.cn.md b/README.cn.md index 544a7b9..df60201 100644 --- a/README.cn.md +++ b/README.cn.md @@ -10,7 +10,7 @@ * 全部用POSIX C实现,目标是为了跨平台兼容linux, windows, android, ios. * 适用于物联网,嵌入式,以及网络服务开发等场景 -![struct](https://github.com/gozfree/gear-lib/blob/master/build/gear-lib.png) +![struct](./build/gear-lib.png) ## 数据结构 | | | diff --git a/README.md b/README.md index 50e69a1..673da92 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This is a collection of basic libraries. * All are written in POSIX C, aim to used compatibility on linux, windows, android, ios. * Aim to reuse for IOT, embedded and network service development -![struct](https://github.com/gozfree/gear-lib/blob/master/build/gear-lib.png) +![struct](./build/gear-lib.png) ## Data Struct | | | From b74c2e0755f314382646c5bc7f7d5c69f36653f7 Mon Sep 17 00:00:00 2001 From: gozfree Date: Mon, 11 Apr 2022 23:31:39 +0800 Subject: [PATCH 14/16] [build] add libdict libvector to win32 --- .../visual studio 2010/gear-lib/gear-lib.sln | 17 + .../gear-lib/libdict/libdict.vcxproj | 79 +++ .../gear-lib/libdict/libdict.vcxproj.filters | 34 ++ .../gear-lib/libdict/libdict.vcxproj.user | 3 + .../gear-lib/librtsp/librtsp.vcxproj | 7 +- .../gear-lib/librtsp/librtsp.vcxproj.filters | 13 +- .../gear-lib/libvector/libvector.vcxproj | 80 +++ .../libvector/libvector.vcxproj.filters | 35 ++ .../gear-lib/libvector/libvector.vcxproj.user | 3 + gear-lib/libdict/libdict.c | 1 + gear-lib/libdict/test_libdict.c | 1 - gear-lib/librtsp/librtsp_server.c | 13 +- gear-lib/librtsp/media_source.h | 8 +- gear-lib/librtsp/media_source_h264.c | 22 +- gear-lib/librtsp/media_source_live.c | 11 +- gear-lib/librtsp/request_handle.c | 18 +- gear-lib/librtsp/rtp.c | 3 +- gear-lib/librtsp/rtp.h | 2 + gear-lib/librtsp/rtsp_parser.c | 508 +++++++++--------- gear-lib/librtsp/sdp.c | 32 +- gear-lib/librtsp/test_librtsp.c | 2 +- gear-lib/librtsp/transport_session.c | 42 +- gear-lib/libuvc/libuvc.c | 26 +- gear-lib/libuvc/libuvc.h | 13 +- gear-lib/libuvc/test_libuvc.c | 11 +- gear-lib/libvector/libvector.h | 4 + 26 files changed, 634 insertions(+), 354 deletions(-) create mode 100644 build/visual studio 2010/gear-lib/libdict/libdict.vcxproj create mode 100644 build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.filters create mode 100644 build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.user create mode 100644 build/visual studio 2010/gear-lib/libvector/libvector.vcxproj create mode 100644 build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.filters create mode 100644 build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.user diff --git a/build/visual studio 2010/gear-lib/gear-lib.sln b/build/visual studio 2010/gear-lib/gear-lib.sln index 1af5c5e..5ad347f 100644 --- a/build/visual studio 2010/gear-lib/gear-lib.sln +++ b/build/visual studio 2010/gear-lib/gear-lib.sln @@ -80,8 +80,10 @@ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "librtsp", "librtsp\librtsp.vcxproj", "{968FA321-4230-4DD4-A554-2619A7D5D080}" ProjectSection(ProjectDependencies) = postProject {2B438236-335D-4F87-8D3A-A10300C7F7DA} = {2B438236-335D-4F87-8D3A-A10300C7F7DA} + {BAF9105C-FF03-4D1C-96A0-DF2F8270936F} = {BAF9105C-FF03-4D1C-96A0-DF2F8270936F} {8682FC88-8540-447B-87CF-4E2B3DAF50A9} = {8682FC88-8540-447B-87CF-4E2B3DAF50A9} {88F2DE94-0375-40CF-92C0-70B98C56B508} = {88F2DE94-0375-40CF-92C0-70B98C56B508} + {67EEB99D-920D-4E28-BC28-477A8E334023} = {67EEB99D-920D-4E28-BC28-477A8E334023} {C8C000B6-BF66-420C-A7B8-D566E955731A} = {C8C000B6-BF66-420C-A7B8-D566E955731A} {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} = {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} {819399D0-724E-4805-B513-480B317CE139} = {819399D0-724E-4805-B513-480B317CE139} @@ -102,6 +104,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libhal", "libhal\libhal.vcx {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} = {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libvector", "libvector\libvector.vcxproj", "{8CAD541B-A89A-48B0-B7EC-AF9E527EA8CE}" + ProjectSection(ProjectDependencies) = postProject + {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} = {2E965CCF-A6DE-4D7D-84A6-ED3F79659914} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libdict", "libdict\libdict.vcxproj", "{46503A91-6BD1-4B9A-8DE4-7EA7AA0091B7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -176,6 +185,14 @@ Global {496D075D-50CA-465D-9D0A-D9229257FC48}.Debug|Win32.Build.0 = Debug|Win32 {496D075D-50CA-465D-9D0A-D9229257FC48}.Release|Win32.ActiveCfg = Release|Win32 {496D075D-50CA-465D-9D0A-D9229257FC48}.Release|Win32.Build.0 = Release|Win32 + {8CAD541B-A89A-48B0-B7EC-AF9E527EA8CE}.Debug|Win32.ActiveCfg = Debug|Win32 + {8CAD541B-A89A-48B0-B7EC-AF9E527EA8CE}.Debug|Win32.Build.0 = Debug|Win32 + {8CAD541B-A89A-48B0-B7EC-AF9E527EA8CE}.Release|Win32.ActiveCfg = Release|Win32 + {8CAD541B-A89A-48B0-B7EC-AF9E527EA8CE}.Release|Win32.Build.0 = Release|Win32 + {46503A91-6BD1-4B9A-8DE4-7EA7AA0091B7}.Debug|Win32.ActiveCfg = Debug|Win32 + {46503A91-6BD1-4B9A-8DE4-7EA7AA0091B7}.Debug|Win32.Build.0 = Debug|Win32 + {46503A91-6BD1-4B9A-8DE4-7EA7AA0091B7}.Release|Win32.ActiveCfg = Release|Win32 + {46503A91-6BD1-4B9A-8DE4-7EA7AA0091B7}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj b/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj new file mode 100644 index 0000000..cb1df19 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj @@ -0,0 +1,79 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {46503A91-6BD1-4B9A-8DE4-7EA7AA0091B7} + libdict + + + + StaticLibrary + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + ..\..\..\..\gear-lib\libposix;..\..\..\..\gear-lib\libposix\MsvcLibX\include;..\..\..\..\gear-lib\libposix\pthreads4w;%(AdditionalIncludeDirectories) + "/DUCRTINCLUDE=$(VCInstallDir)include" "/DMSVCINCLUDE=$(VCInstallDir)include" "/DWSDKINCLUDE=$(WindowsSdkDir)Include" %(AdditionalOptions) + MultiThreaded + + + true + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + + + diff --git a/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.filters b/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.filters new file mode 100644 index 0000000..de4c02a --- /dev/null +++ b/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.filters @@ -0,0 +1,34 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + + + + + \ No newline at end of file diff --git a/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.user b/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.user new file mode 100644 index 0000000..695b5c7 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libdict/libdict.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj b/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj index 29909a3..2360d96 100644 --- a/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj +++ b/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj @@ -42,7 +42,7 @@ Level3 Disabled MultiThreaded - ..\..\..\..\gear-lib\libposix;..\..\..\..\gear-lib\libposix\MsvcLibX\include;..\..\..\..\gear-lib\libposix\pthreads4w;..\..\..\..\gear-lib\liblog;..\..\..\..\gear-lib\libsock;%(AdditionalIncludeDirectories) + ..\..\..\..\gear-lib\libposix;..\..\..\..\gear-lib\libposix\MsvcLibX\include;..\..\..\..\gear-lib\libposix\pthreads4w;..\..\..\..\gear-lib\liblog;..\..\..\..\gear-lib\libsock;..\..\..\..\gear-lib\libtime;..\..\..\..\gear-lib\libdict;..\..\..\..\gear-lib\libgevent;..\..\..\..\gear-lib\libdarray;..\..\..\..\gear-lib\libthread;..\..\..\..\gear-lib\libmedia-io;..\..\..\..\gear-lib\libfile;..\..\..\..\gear-lib\libuvc;..\..\..\..\gear-lib\libqueue;..\..\..\..\gear-lib\librtsp;%(AdditionalIncludeDirectories) "/DUCRTINCLUDE=$(VCInstallDir)include" "/DMSVCINCLUDE=$(VCInstallDir)include" "/DWSDKINCLUDE=$(WindowsSdkDir)Include" %(AdditionalOptions) @@ -64,6 +64,7 @@ + @@ -79,12 +80,12 @@ + + - - diff --git a/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj.filters b/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj.filters index affb51b..ca35993 100644 --- a/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj.filters +++ b/build/visual studio 2010/gear-lib/librtsp/librtsp.vcxproj.filters @@ -20,6 +20,7 @@ + @@ -49,6 +50,12 @@ Header Files + + Header Files + + + Header Files + @@ -57,12 +64,6 @@ Source Files - - Source Files - - - Source Files - Source Files diff --git a/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj b/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj new file mode 100644 index 0000000..5480874 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj @@ -0,0 +1,80 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {8CAD541B-A89A-48B0-B7EC-AF9E527EA8CE} + libvector + + + + StaticLibrary + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + MultiThreaded + ..\..\..\..\gear-lib\libposix;..\..\..\..\gear-lib\libposix\MsvcLibX\include;..\..\..\..\gear-lib\libposix\pthreads4w;%(AdditionalIncludeDirectories) + "/DUCRTINCLUDE=$(VCInstallDir)include" "/DMSVCINCLUDE=$(VCInstallDir)include" "/DWSDKINCLUDE=$(WindowsSdkDir)Include" %(AdditionalOptions) + + + true + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.filters b/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.filters new file mode 100644 index 0000000..20a12e3 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.filters @@ -0,0 +1,35 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + + + + + + \ No newline at end of file diff --git a/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.user b/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.user new file mode 100644 index 0000000..695b5c7 --- /dev/null +++ b/build/visual studio 2010/gear-lib/libvector/libvector.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/gear-lib/libdict/libdict.c b/gear-lib/libdict/libdict.c index e969ff1..cec518a 100644 --- a/gear-lib/libdict/libdict.c +++ b/gear-lib/libdict/libdict.c @@ -19,6 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ +#include #include "libdict.h" #include #include diff --git a/gear-lib/libdict/test_libdict.c b/gear-lib/libdict/test_libdict.c index 1fd0bf7..cdd3d07 100644 --- a/gear-lib/libdict/test_libdict.c +++ b/gear-lib/libdict/test_libdict.c @@ -21,7 +21,6 @@ ******************************************************************************/ #include "libdict.h" #include -#include #include #define ALIGN "%15s: %6.4f sec\n" diff --git a/gear-lib/librtsp/librtsp_server.c b/gear-lib/librtsp/librtsp_server.c index bdeb41e..57f81cf 100644 --- a/gear-lib/librtsp/librtsp_server.c +++ b/gear-lib/librtsp/librtsp_server.c @@ -19,6 +19,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ +#include +#include +#include +#include "librtsp.h" +#include #include #include #include @@ -26,10 +31,7 @@ #include #include #include -#include -#include -#include "librtsp.h" -#include + #include "media_source.h" #include "transport_session.h" #include "rtsp_parser.h" @@ -98,8 +100,9 @@ static void rtsp_connect_create(struct rtsp_server *rtsp, int fd, uint32_t ip, u static void rtsp_connect_destroy(struct rtsp_server *rtsp, int fd) { char key[9]; + struct rtsp_request *req; snprintf(key, sizeof(key), "%d", fd); - struct rtsp_request *req = (struct rtsp_request *)dict_get(rtsp->connect_pool, key, NULL); + req = (struct rtsp_request *)dict_get(rtsp->connect_pool, key, NULL); logi("fd = %d, req=%p\n", fd, req); dict_del(rtsp->connect_pool, key); gevent_del(rtsp->evbase, &req->event); diff --git a/gear-lib/librtsp/media_source.h b/gear-lib/librtsp/media_source.h index 5543896..e050f6e 100644 --- a/gear-lib/librtsp/media_source.h +++ b/gear-lib/librtsp/media_source.h @@ -40,10 +40,10 @@ typedef struct media_source { char sdp[SDP_LEN_MAX]; struct timeval tm_create; int (*sdp_generate)(struct media_source *ms); - int (*open)(struct media_source *ms, const char *uri); - int (*read)(struct media_source *ms, void **data, size_t *len); - int (*write)(struct media_source *ms, void *data, size_t len); - void (*close)(struct media_source *ms); + int (*_open)(struct media_source *ms, const char *uri); + int (*_read)(struct media_source *ms, void **data, size_t *len); + int (*_write)(struct media_source *ms, void *data, size_t len); + void (*_close)(struct media_source *ms); int (*get_frame)(); void *opaque; bool is_active; diff --git a/gear-lib/librtsp/media_source_h264.c b/gear-lib/librtsp/media_source_h264.c index b8f53d6..d3d14f2 100644 --- a/gear-lib/librtsp/media_source_h264.c +++ b/gear-lib/librtsp/media_source_h264.c @@ -40,12 +40,13 @@ struct h264_source_ctx { static void *item_alloc_hook(void *data, size_t len, void *arg) { + struct media_packet *new_pkt; struct media_packet *pkt = (struct media_packet *)arg; if (!pkt) { loge("calloc packet failed!\n"); return NULL; } - struct media_packet *new_pkt = media_packet_copy(pkt, MEDIA_MEM_SHALLOW); + new_pkt = media_packet_copy(pkt, MEDIA_MEM_SHALLOW); logd("media_packet size=%d\n", media_packet_get_size(new_pkt)); return new_pkt; } @@ -71,26 +72,29 @@ static int h264_parser_frame(struct h264_source_ctx *c, const char *name) { int ret = 0; size_t count = 0; + size_t bytes; + const uint8_t *start, *end, *nalu, *nalu2; + struct media_packet *pkt; + struct queue_item *it; struct iovec *data = file_dump(name); if (!data) { loge("file_dump %s failed!\n", name); return -1; } - const uint8_t *start = data->iov_base; - const uint8_t *end = start + data->iov_len; - const uint8_t *nalu = h264_find_start_code(start, end); + start = data->iov_base; + end = start + data->iov_len; + nalu = h264_find_start_code(start, end); while (nalu < end) { - const uint8_t *nalu2 = h264_find_start_code(nalu + 4, end); - size_t bytes = nalu2 - nalu; - - struct media_packet *pkt = media_packet_create(MEDIA_TYPE_VIDEO, MEDIA_MEM_SHALLOW, (uint8_t *)nalu, bytes); + nalu2 = h264_find_start_code(nalu + 4, end); + bytes = nalu2 - nalu; + pkt = media_packet_create(MEDIA_TYPE_VIDEO, MEDIA_MEM_SHALLOW, (uint8_t *)nalu, bytes); pkt->video->pts = 90000 * count++; pkt->video->dts = 90000 * count++; pkt->video->encoder.timebase.num = 30; pkt->video->encoder.timebase.den = 1; - struct queue_item *it = queue_item_alloc(c->q, pkt->video->data, pkt->video->size, pkt); + it = queue_item_alloc(c->q, pkt->video->data, pkt->video->size, pkt); if (!it) { loge("item_alloc packet type %d failed!\n", pkt->type); ret = -1; diff --git a/gear-lib/librtsp/media_source_live.c b/gear-lib/librtsp/media_source_live.c index 1f1af92..1649775 100644 --- a/gear-lib/librtsp/media_source_live.c +++ b/gear-lib/librtsp/media_source_live.c @@ -68,7 +68,7 @@ struct live_source_ctx { void *priv; }; -static struct live_source_ctx g_live = {.uvc_opened = false}; +static struct live_source_ctx g_live; static int pixel_format_to_x264_csp(enum pixel_format fmt) { @@ -205,6 +205,7 @@ failed: static int init_pic_data(struct x264_ctx *c, x264_picture_t *pic, struct video_frame *frame) { + int i; x264_picture_init(pic); pic->i_pts = frame->timestamp; pic->img.i_csp = c->param.i_csp; @@ -233,7 +234,7 @@ pic->img.i_plane, frame->planes); return -1; } - for (int i = 0; i < pic->img.i_plane; i++) { + for (i = 0; i < pic->img.i_plane; i++) { pic->img.i_stride[i] = (int)frame->linesize[i]; pic->img.plane[i] = frame->data[i]; } @@ -243,6 +244,7 @@ pic->img.i_plane, frame->planes); static int fill_packet(struct x264_ctx *c, struct video_packet *pkt, x264_nal_t *nals, int nal_cnt, x264_picture_t *pic_out) { + int i; if (!nal_cnt) return -1; @@ -253,7 +255,7 @@ static int fill_packet(struct x264_ctx *c, struct video_packet *pkt, pkt->encoder.extra_size = c->encoder.extra_size; c->append_extra = true; } - for (int i = 0; i < nal_cnt; i++) { + for (i = 0; i < nal_cnt; i++) { x264_nal_t *nal = nals + i; da_push_back_array(c->packet_data, nal->p_payload, nal->i_payload); } @@ -341,6 +343,7 @@ static uint32_t get_random_number() static int live_open(struct media_source *ms, const char *name) { struct live_source_ctx *c = &g_live; + g_live.uvc_opened = false; if (c->uvc_opened) { logi("uvc already opened!\n"); return 0; @@ -350,7 +353,7 @@ static int live_open(struct media_source *ms, const char *name) c->conf.fps.num = 30; c->conf.fps.den = 1; c->conf.format = PIXEL_FORMAT_YUY2, - c->uvc = uvc_open(UVC_TYPE_V4L2, "/dev/video0", &c->conf); + c->uvc = uvc_open("/dev/video0", &c->conf); if (!c->uvc) { loge("uvc open failed!\n"); return -1; diff --git a/gear-lib/librtsp/request_handle.c b/gear-lib/librtsp/request_handle.c index e4ed8ac..7f01ae9 100644 --- a/gear-lib/librtsp/request_handle.c +++ b/gear-lib/librtsp/request_handle.c @@ -91,13 +91,15 @@ int handle_rtsp_response(struct rtsp_request *req, int code, const char *msg) static int on_teardown(struct rtsp_request *req, char *url) { + struct rtsp_server *rc; + struct transport_session *ts; //int len = sock_send(req->fd, resp, strlen(resp)); if (-1 == parse_range(&req->range, (char *)req->raw->iov_base, req->raw->iov_len)) { loge("parse_range failed!\n"); return -1; } - struct rtsp_server *rc = req->rtsp_server; - struct transport_session *ts = transport_session_lookup(rc->transport_session_pool, req->session.id); + rc = req->rtsp_server; + ts = transport_session_lookup(rc->transport_session_pool, req->session.id); if (!ts) { loge("transport_session is NULL\n"); return handle_rtsp_response(req, 454, NULL); @@ -153,6 +155,7 @@ static int on_setup(struct rtsp_request *req, char *url) { char buf[RTSP_RESPONSE_LEN_MAX]; char transport[128]; + struct transport_session *ts; struct rtsp_server *rc = req->rtsp_server; if (-1 == parse_transport(&req->transport, (char *)req->raw->iov_base, req->raw->iov_len)) { @@ -171,7 +174,7 @@ static int on_setup(struct rtsp_request *req, char *url) sock_addr_ntop(req->transport.destination, req->client.ip); } - struct transport_session *ts = transport_session_lookup(rc->transport_session_pool, req->session.id); + ts = transport_session_lookup(rc->transport_session_pool, req->session.id); if (!ts) { ts = transport_session_create(rc->transport_session_pool, &req->transport); if (!ts) { @@ -210,18 +213,21 @@ static int on_play(struct rtsp_request *req, char *url) { int n = 0; char buf[RTSP_RESPONSE_LEN_MAX]; + struct rtsp_server *rc; + struct transport_session *ts; + struct media_source *ms; if (-1 == parse_range(&req->range, (char *)req->raw->iov_base, req->raw->iov_len)) { loge("parse_range failed!\n"); return -1; } - struct rtsp_server *rc = req->rtsp_server; - struct transport_session *ts = transport_session_lookup(rc->transport_session_pool, req->session.id); + rc = req->rtsp_server; + ts = transport_session_lookup(rc->transport_session_pool, req->session.id); if (!ts) { handle_rtsp_response(req, 454, NULL); return -1; } - struct media_source *ms = rtsp_media_source_lookup(url); + ms = rtsp_media_source_lookup(url); if (req->range.to > 0) { n += snprintf(buf+n, sizeof(buf)-n, "Range: npt=%.3f-%.3f\r\n", (float)(req->range.from / 1000.0f), (float)(req->range.to / 1000.0f)); diff --git a/gear-lib/librtsp/rtp.c b/gear-lib/librtsp/rtp.c index 4154665..b8cb29d 100644 --- a/gear-lib/librtsp/rtp.c +++ b/gear-lib/librtsp/rtp.c @@ -301,11 +301,12 @@ int rtp_ssrc(void) struct rtp_socket *rtp_socket_create(enum rtp_mode mode, int tcp_fd, const char* src_ip, const char *dst_ip) { + unsigned short i; struct rtp_socket *s = calloc(1, sizeof(struct rtp_socket)); if (!s) { return NULL; } - unsigned short i; + s->mode = mode; switch (mode) { case RTP_TCP: diff --git a/gear-lib/librtsp/rtp.h b/gear-lib/librtsp/rtp.h index 0fb416b..fca6e41 100644 --- a/gear-lib/librtsp/rtp.h +++ b/gear-lib/librtsp/rtp.h @@ -23,7 +23,9 @@ #define LIBRTP_H #include +#if defined (OS_LINUX) #include +#endif #include #ifdef __cplusplus diff --git a/gear-lib/librtsp/rtsp_parser.c b/gear-lib/librtsp/rtsp_parser.c index bd263a2..7ea2c2e 100644 --- a/gear-lib/librtsp/rtsp_parser.c +++ b/gear-lib/librtsp/rtsp_parser.c @@ -64,8 +64,12 @@ int parse_rtsp_request(struct rtsp_request *req) int session_id_len = sizeof(req->session.id); int cur; char c; + char *p; const char *rtsp_prefix = "rtsp://"; int rtsp_prefix_len = sizeof(rtsp_prefix); + int parse_succeed = 0; + int i, j, k, k1, k2, n; + int num, org = 0; logi("rtsp request[%d]:\n==== C >>>> S ====\n%s==== C >>>> S ====\n", len, str); @@ -82,8 +86,7 @@ int parse_rtsp_request(struct rtsp_request *req) } // Then read everything up to the next space (or tab) as the command name: - int parse_succeed = 0; - int i; + for (i = 0; i < cmd_len - 1 && i < len; ++cur, ++i) { c = str[cur]; if (c == ' ' || c == '\t') { @@ -99,7 +102,7 @@ int parse_rtsp_request(struct rtsp_request *req) } // Skip over the prefix of any "rtsp://" or "rtsp:/" URL that follows: - int j = cur+1; + j = cur+1; while (j < len && (str[j] == ' ' || str[j] == '\t')) { ++j; // skip over any additional white space } @@ -125,12 +128,11 @@ int parse_rtsp_request(struct rtsp_request *req) // Look for the URL suffix (before the following "RTSP/"): parse_succeed = 0; - int k; for (k = cur+1; k < (len-5); ++k) { if (str[k] == 'R' && str[k+1] == 'T' && str[k+2] == 'S' && str[k+3] == 'P' && str[k+4] == '/') { while (--k >= cur && str[k] == ' ') {} // go back over all spaces before "RTSP/" - int k1 = k; + k1 = k; while (k1 > cur && str[k1] != '/') --k1; // ASSERT: At this point // cur: first space or slash after "host" or "host:port" @@ -138,7 +140,8 @@ int parse_rtsp_request(struct rtsp_request *req) // k1: last slash in the range [cur,k] // The URL suffix comes from [k1+1,k] // Copy "url_suffix": - int n = 0, k2 = k1+1; + n = 0; + k2 = k1+1; if (k2 <= k) { if (k - k1 + 1 > url_suffix_len) return -1; // there's no room while (k2 <= k) req->url_suffix[n++] = str[k2++]; @@ -161,7 +164,7 @@ int parse_rtsp_request(struct rtsp_request *req) if (!parse_succeed) { return -1; } - int org = 0; + org = 0; for (org = 0; org < k-7; org++) { req->url_origin[org] = str[i+org+1]; } @@ -169,7 +172,7 @@ int parse_rtsp_request(struct rtsp_request *req) // Look for "CSeq:" (mandatory, case insensitive), skip whitespace, // then read everything up to the next \r or \n as 'CSeq': parse_succeed = 0; - int n; + for (j = cur; j < (len-5); ++j) { if (strncasecmp("CSeq:", &str[j], 5) == 0) { j += 5; @@ -206,7 +209,7 @@ int parse_rtsp_request(struct rtsp_request *req) } req->session.id[n] = '\0'; req->session.timeout = 60000; - char *p = strchr(&str[j], ';'); + p = strchr(&str[j], ';'); if (p && 0 == strncmp("timeout=", p+1, 8)) { req->session.timeout = (int)(atof(p+9) * 1000); } @@ -220,7 +223,7 @@ int parse_rtsp_request(struct rtsp_request *req) if (strncasecmp("Content-Length:", &(str[j]), 15) == 0) { j += 15; while (j < len && (str[j] == ' ' || str[j] == '\t')) ++j; - int num; + if (sscanf(&str[j], "%u", &num) == 1) { req->content_len = num; } @@ -231,6 +234,9 @@ int parse_rtsp_request(struct rtsp_request *req) int parse_transport(struct transport_header *t, char *buf, int len) { + char const* fields; + char* field; + size_t n = 0; // First, find "Transport:" while (1) { if (*buf == '\0') @@ -243,9 +249,9 @@ int parse_transport(struct transport_header *t, char *buf, int len) } // Then, run through each of the fields, looking for ones we handle: - char const* fields = buf + 10; + fields = buf + 10; while (*fields == ' ') ++fields; - char* field = (char *)calloc(1, strlen(fields)+1); + field = (char *)calloc(1, strlen(fields)+1); while (sscanf(fields, "%[^;\r\n]", field) == 1) { switch (*field) { case 'r': @@ -376,7 +382,9 @@ int parse_transport(struct transport_header *t, char *buf, int len) int parse_session(struct session_header *s, char *buf, int len) { + const char* p; int found = 0; + size_t n; while (1) { if (*buf == '\0') { break; @@ -394,10 +402,10 @@ int parse_session(struct session_header *s, char *buf, int len) return 0; } s->timeout = 60000; - const char* p; + p = strchr(buf, ';'); if (p) { - size_t n = (size_t)(p - buf); + n = (size_t)(p - buf); if (n >= sizeof(s->id)) return -1; @@ -407,7 +415,7 @@ int parse_session(struct session_header *s, char *buf, int len) if (0 == strncmp("timeout=", p+1, 8)) s->timeout = (int)(atof(p+9) * 1000); } else { - size_t n = strlen(buf); + n = strlen(buf); if (n >= sizeof(s->id)) return -1; memcpy(s->id, buf, n); @@ -533,17 +541,17 @@ struct time_smpte_t struct time_npt_t { - int64_t second; // [0,---) - int fraction; // [0,999] + int64_t second; // [0,---) + int fraction; // [0,999] }; struct time_clock_t { - int second; // [0,24*60*60) - int fraction; // [0,999] - int day; // [0,30] - int month; // [0,11] - int year; // [xxxx] + int second; // [0,24*60*60) + int fraction; // [0,999] + int day; // [0,30] + int month; // [0,11] + int year; // [xxxx] }; #define RANGE_SPECIAL ",;\r\n" @@ -557,7 +565,7 @@ static uint64_t utc_mktime(const struct tm *t) mon += 12; /* Puts Feb last since it has leap day */ year -= 1; } - + return ((((uint64_t) (year/4 - year/100 + year/400 + 367*mon/12 + t->tm_mday) + year*365 - 719499 @@ -568,56 +576,52 @@ static uint64_t utc_mktime(const struct tm *t) static inline const char* string_token_int(const char* str, int *value) { - *value = 0; - while ('0' <= *str && *str <= '9') - { - *value = (*value * 10) + (*str - '0'); - ++str; - } - return str; + *value = 0; + while ('0' <= *str && *str <= '9') { + *value = (*value * 10) + (*str - '0'); + ++str; + } + return str; } static inline const char* string_token_int64(const char* str, int64_t *value) { - *value = 0; - while ('0' <= *str && *str <= '9') - { - *value = (*value * 10) + (*str - '0'); - ++str; - } - return str; + *value = 0; + while ('0' <= *str && *str <= '9') { + *value = (*value * 10) + (*str - '0'); + ++str; + } + return str; } // smpte-time = 1*2DIGIT ":" 1*2DIGIT ":" 1*2DIGIT [ ":" 1*2DIGIT ][ "." 1*2DIGIT ] // hours:minutes:seconds:frames.subframes static const char* rtsp_header_range_smpte_time(const char* str, int *hours, int *minutes, int *seconds, int *frames, int *subframes) { - const char* p; + const char* p; - assert(str); - p = string_token_int(str, hours); - if(*p != ':') - return NULL; + assert(str); + p = string_token_int(str, hours); + if(*p != ':') + return NULL; - p = string_token_int(p+1, minutes); - if(*p != ':') - return NULL; + p = string_token_int(p+1, minutes); + if(*p != ':') + return NULL; - p = string_token_int(p+1, seconds); + p = string_token_int(p+1, seconds); - *frames = 0; - *subframes = 0; - if(*p == ':') - { - p = string_token_int(p+1, frames); - } + *frames = 0; + *subframes = 0; + if(*p == ':') { + p = string_token_int(p+1, frames); + } - if(*p == '.') - { - p = string_token_int(p+1, subframes); - } + if(*p == '.') { + p = string_token_int(p+1, subframes); + } - return p; + return p; } // 3.5 SMPTE Relative Timestamps (p16) @@ -630,36 +634,33 @@ static const char* rtsp_header_range_smpte_time(const char* str, int *hours, int // smpte-25=10:07:00-10:07:33:05.01 static int rtsp_header_range_smpte(const char* fields, struct range_header* range) { - const char *p; - int hours, minutes, seconds, frames, subframes; + const char *p; + int hours, minutes, seconds, frames, subframes; - assert(fields); - p = rtsp_header_range_smpte_time(fields, &hours, &minutes, &seconds, &frames, &subframes); - if(!p || '-' != *p) - return -1; + assert(fields); + p = rtsp_header_range_smpte_time(fields, &hours, &minutes, &seconds, &frames, &subframes); + if(!p || '-' != *p) + return -1; - range->from_value = RTSP_RANGE_TIME_NORMAL; - range->from = (hours%24)*3600 + (minutes%60)*60 + seconds; - range->from = range->from * 1000 + frames % 1000; + range->from_value = RTSP_RANGE_TIME_NORMAL; + range->from = (hours%24)*3600 + (minutes%60)*60 + seconds; + range->from = range->from * 1000 + frames % 1000; - assert('-' == *p); - if('\0' == p[1] || strchr(RANGE_SPECIAL, p[1])) - { - range->to_value = RTSP_RANGE_TIME_NOVALUE; - range->to = 0; - } - else - { - p = rtsp_header_range_smpte_time(p+1, &hours, &minutes, &seconds, &frames, &subframes); - if(!p ) return -1; - assert('\0' == p[0] || strchr(RANGE_SPECIAL, p[0])); + assert('-' == *p); + if('\0' == p[1] || strchr(RANGE_SPECIAL, p[1])) { + range->to_value = RTSP_RANGE_TIME_NOVALUE; + range->to = 0; + } else { + p = rtsp_header_range_smpte_time(p+1, &hours, &minutes, &seconds, &frames, &subframes); + if(!p ) return -1; + assert('\0' == p[0] || strchr(RANGE_SPECIAL, p[0])); - range->to_value = RTSP_RANGE_TIME_NORMAL; - range->to = (hours%24)*3600 + (minutes%60)*60 + seconds; - range->to = range->to * 1000 + frames % 1000; - } + range->to_value = RTSP_RANGE_TIME_NORMAL; + range->to = (hours%24)*3600 + (minutes%60)*60 + seconds; + range->to = range->to * 1000 + frames % 1000; + } - return 0; + return 0; } // npt-time = "now" | npt-sec | npt-hhmmss @@ -670,45 +671,45 @@ static int rtsp_header_range_smpte(const char* fields, struct range_header* rang // npt-ss = 1*2DIGIT ; 0-59 static const char* rtsp_header_range_npt_time(const char* str, uint64_t *seconds, int *fraction) { - const char* p; - int v1, v2; + const char* p; + int v1, v2; - assert(str); - p = strpbrk(str, "-\r\n"); - if(!str || (p-str==3 && 0==strncasecmp(str, "now", 3))) - { - *seconds = 0; // now - *fraction = -1; - } - else - { - p = string_token_int64(str, (int64_t*)seconds); - if(*p == ':') - { - // npt-hhmmss - p = string_token_int(p+1, &v1); - if(*p != ':') - return NULL; + assert(str); + p = strpbrk(str, "-\r\n"); + if(!str || (p-str==3 && 0==strncasecmp(str, "now", 3))) + { + *seconds = 0; // now + *fraction = -1; + } + else + { + p = string_token_int64(str, (int64_t*)seconds); + if(*p == ':') + { + // npt-hhmmss + p = string_token_int(p+1, &v1); + if(*p != ':') + return NULL; - p = string_token_int(p+1, &v2); + p = string_token_int(p+1, &v2); - assert(0 <= v1 && v1 < 60); - assert(0 <= v2 && v2 < 60); - *seconds = *seconds * 3600 + (v1%60)*60 + v2%60; - } - else - { - // npt-sec - //*seconds = hours; - } + assert(0 <= v1 && v1 < 60); + assert(0 <= v2 && v2 < 60); + *seconds = *seconds * 3600 + (v1%60)*60 + v2%60; + } + else + { + // npt-sec + //*seconds = hours; + } - if(*p == '.') - p = string_token_int(p+1, fraction); - else - *fraction = 0; - } + if(*p == '.') + p = string_token_int(p+1, fraction); + else + *fraction = 0; + } - return p; + return p; } // 3.6 Normal Play Time (p17) @@ -719,54 +720,54 @@ static const char* rtsp_header_range_npt_time(const char* str, uint64_t *seconds // npt=now- static int rtsp_header_range_npt(const char* fields, struct range_header* range) { - const char* p; - uint64_t seconds; - int fraction; + const char* p; + uint64_t seconds; + int fraction; - p = fields; - if('-' != *p) - { - p = rtsp_header_range_npt_time(p, &seconds, &fraction); - if(!p || '-' != *p) - return -1; + p = fields; + if('-' != *p) + { + p = rtsp_header_range_npt_time(p, &seconds, &fraction); + if(!p || '-' != *p) + return -1; - if(0 == seconds && -1 == fraction) - { - range->from_value = RTSP_RANGE_TIME_NOW; - range->from = 0L; - } - else - { - range->from_value = RTSP_RANGE_TIME_NORMAL; - range->from = seconds; - range->from = range->from * 1000 + fraction % 1000; - } - } - else - { - range->from_value = RTSP_RANGE_TIME_NOVALUE; - range->from = 0; - } + if(0 == seconds && -1 == fraction) + { + range->from_value = RTSP_RANGE_TIME_NOW; + range->from = 0L; + } + else + { + range->from_value = RTSP_RANGE_TIME_NORMAL; + range->from = seconds; + range->from = range->from * 1000 + fraction % 1000; + } + } + else + { + range->from_value = RTSP_RANGE_TIME_NOVALUE; + range->from = 0; + } - assert('-' == *p); - if('\0' == p[1] || strchr(RANGE_SPECIAL, p[1])) - { - assert('-' != *fields); - range->to_value = RTSP_RANGE_TIME_NOVALUE; - range->to = 0; - } - else - { - p = rtsp_header_range_npt_time(p+1, &seconds, &fraction); - if( !p ) return -1; - assert('\0' == p[0] || strchr(RANGE_SPECIAL, p[0])); + assert('-' == *p); + if('\0' == p[1] || strchr(RANGE_SPECIAL, p[1])) + { + assert('-' != *fields); + range->to_value = RTSP_RANGE_TIME_NOVALUE; + range->to = 0; + } + else + { + p = rtsp_header_range_npt_time(p+1, &seconds, &fraction); + if( !p ) return -1; + assert('\0' == p[0] || strchr(RANGE_SPECIAL, p[0])); - range->to_value = RTSP_RANGE_TIME_NORMAL; - range->to = seconds; - range->to = range->to * 1000 + fraction % 1000; - } + range->to_value = RTSP_RANGE_TIME_NORMAL; + range->to = seconds; + range->to = range->to * 1000 + fraction % 1000; + } - return 0; + return 0; } // utc-time = utc-date "T" utc-time "Z" @@ -774,36 +775,36 @@ static int rtsp_header_range_npt(const char* fields, struct range_header* range) // utc-time = 6DIGIT [ "." fraction ] ; < HHMMSS.fraction > static const char* rtsp_header_range_clock_time(const char* str, uint64_t *second, int *fraction) { - struct tm t; - const char* p; - int year, month, day; - int hour, minute; + struct tm t; + const char* p; + int year, month, day; + int hour, minute; - assert(str); - if(!str || 5 != sscanf(str, "%4d%2d%2dT%2d%2d", &year, &month, &day, &hour, &minute)) - return NULL; + assert(str); + if(!str || 5 != sscanf(str, "%4d%2d%2dT%2d%2d", &year, &month, &day, &hour, &minute)) + return NULL; - *second = 0; - *fraction = 0; - p = string_token_int64(str + 13, (int64_t*)second); + *second = 0; + *fraction = 0; + p = string_token_int64(str + 13, (int64_t*)second); assert(p); - if(*p == '.') - { - p = string_token_int(p+1, fraction); - } + if(*p == '.') + { + p = string_token_int(p+1, fraction); + } - memset(&t, 0, sizeof(t)); - t.tm_year = year - 1900; - t.tm_mon = month - 1; - t.tm_mday = day; - t.tm_hour = hour; - t.tm_min = minute; -// *second += mktime(&t); + memset(&t, 0, sizeof(t)); + t.tm_year = year - 1900; + t.tm_mon = month - 1; + t.tm_mday = day; + t.tm_hour = hour; + t.tm_min = minute; + // *second += mktime(&t); *second += utc_mktime(&t); -// assert('Z' == *p); -// assert('\0' == p[1] || strchr(RANGE_SPECIAL"-", p[1])); - return 'Z'==*p ? p+1 : p; + // assert('Z' == *p); + // assert('\0' == p[1] || strchr(RANGE_SPECIAL"-", p[1])); + return 'Z'==*p ? p+1 : p; } // 3.7 Absolute Time (p18) @@ -812,41 +813,42 @@ static const char* rtsp_header_range_clock_time(const char* str, uint64_t *secon // Range: clock=19961110T1925-19961110T2015 (p72) static int rtsp_header_range_clock(const char* fields, struct range_header* range) { - const char* p; - uint64_t second; - int fraction; + const char* p; + uint64_t second; + int fraction; - p = rtsp_header_range_clock_time(fields, &second, &fraction); - if(!p || '-' != *p) - return -1; + p = rtsp_header_range_clock_time(fields, &second, &fraction); + if(!p || '-' != *p) + return -1; - range->from_value = RTSP_RANGE_TIME_NORMAL; - range->from = second * 1000; - range->from += fraction % 1000; + range->from_value = RTSP_RANGE_TIME_NORMAL; + range->from = second * 1000; + range->from += fraction % 1000; - assert('-' == *p); - if('\0'==p[1] || strchr(RANGE_SPECIAL, p[1])) - { - range->to_value = RTSP_RANGE_TIME_NOVALUE; - range->to = 0; - } - else - { - p = rtsp_header_range_clock_time(p+1, &second, &fraction); - if( !p ) return -1; + assert('-' == *p); + if('\0'==p[1] || strchr(RANGE_SPECIAL, p[1])) + { + range->to_value = RTSP_RANGE_TIME_NOVALUE; + range->to = 0; + } + else + { + p = rtsp_header_range_clock_time(p+1, &second, &fraction); + if( !p ) return -1; - range->to_value = RTSP_RANGE_TIME_NORMAL; - range->to = second * 1000; - range->to += (unsigned int)fraction % 1000; - } + range->to_value = RTSP_RANGE_TIME_NORMAL; + range->to = second * 1000; + range->to += (unsigned int)fraction % 1000; + } - return 0; + return 0; } int parse_range(struct range_header *range, char* buf, int len) { int r = 0; int found = 0; + char *field; while (1) { if (*buf == '\0') { break; @@ -863,57 +865,45 @@ int parse_range(struct range_header *range, char* buf, int len) if (!found) { return 0; } - char *field = buf; - range->time = 0L; - while(field && 0 == r) - { - if(0 == strncasecmp("clock=", field, 6)) - { - range->type = RTSP_RANGE_CLOCK; - r = rtsp_header_range_clock(field+6, range); - } - else if(0 == strncasecmp("npt=", field, 4)) - { - range->type = RTSP_RANGE_NPT; - r = rtsp_header_range_npt(field+4, range); - } - else if(0 == strncasecmp("smpte=", field, 6)) - { - range->type = RTSP_RANGE_SMPTE; - r = rtsp_header_range_smpte(field+6, range); - if(RTSP_RANGE_TIME_NORMAL == range->from_value) - range->from = (range->from/1000 * 1000) + (1000/30 * (range->from%1000)); // frame to ms - if(RTSP_RANGE_TIME_NORMAL == range->to_value) - range->to = (range->to/1000 * 1000) + (1000/30 * (range->to%1000)); // frame to ms - } - else if(0 == strncasecmp("smpte-30-drop=", field, 15)) - { - range->type = RTSP_RANGE_SMPTE_30; - r = rtsp_header_range_smpte(field+15, range); - if(RTSP_RANGE_TIME_NORMAL == range->from_value) - range->from = (range->from/1000 * 1000) + (1000/30 * (range->from%1000)); // frame to ms - if(RTSP_RANGE_TIME_NORMAL == range->to_value) - range->to = (range->to/1000 * 1000) + (1000/30 * (range->to%1000)); // frame to ms - } - else if(0 == strncasecmp("smpte-25=", field, 9)) - { - range->type = RTSP_RANGE_SMPTE_25; - r = rtsp_header_range_smpte(field+9, range); - if(RTSP_RANGE_TIME_NORMAL == range->from_value) - range->from = (range->from/1000 * 1000) + (1000/25 * (range->from%1000)); // frame to ms - if(RTSP_RANGE_TIME_NORMAL == range->to_value) - range->to = (range->to/1000 * 1000) + (1000/25 * (range->to%1000)); // frame to ms - } - else if(0 == strncasecmp("time=", field, 5)) - { - if (rtsp_header_range_clock_time(field + 5, &range->time, &r)) - range->time = range->time * 1000 + r % 1000; - } - - field = strchr(field, ';'); - if(field) - ++field; - } + field = buf; + range->time = 0L; + while (field && 0 == r) { + if (0 == strncasecmp("clock=", field, 6)) { + range->type = RTSP_RANGE_CLOCK; + r = rtsp_header_range_clock(field+6, range); + } else if (0 == strncasecmp("npt=", field, 4)) { + range->type = RTSP_RANGE_NPT; + r = rtsp_header_range_npt(field+4, range); + } else if (0 == strncasecmp("smpte=", field, 6)) { + range->type = RTSP_RANGE_SMPTE; + r = rtsp_header_range_smpte(field+6, range); + if (RTSP_RANGE_TIME_NORMAL == range->from_value) + range->from = (range->from/1000 * 1000) + (1000/30 * (range->from%1000)); // frame to ms + if (RTSP_RANGE_TIME_NORMAL == range->to_value) + range->to = (range->to/1000 * 1000) + (1000/30 * (range->to%1000)); // frame to ms + } else if(0 == strncasecmp("smpte-30-drop=", field, 15)) { + range->type = RTSP_RANGE_SMPTE_30; + r = rtsp_header_range_smpte(field+15, range); + if (RTSP_RANGE_TIME_NORMAL == range->from_value) + range->from = (range->from/1000 * 1000) + (1000/30 * (range->from%1000)); // frame to ms + if (RTSP_RANGE_TIME_NORMAL == range->to_value) + range->to = (range->to/1000 * 1000) + (1000/30 * (range->to%1000)); // frame to ms + } else if(0 == strncasecmp("smpte-25=", field, 9)) { + range->type = RTSP_RANGE_SMPTE_25; + r = rtsp_header_range_smpte(field+9, range); + if(RTSP_RANGE_TIME_NORMAL == range->from_value) + range->from = (range->from/1000 * 1000) + (1000/25 * (range->from%1000)); // frame to ms + if(RTSP_RANGE_TIME_NORMAL == range->to_value) + range->to = (range->to/1000 * 1000) + (1000/25 * (range->to%1000)); // frame to ms + } else if(0 == strncasecmp("time=", field, 5)) { + if (rtsp_header_range_clock_time(field + 5, &range->time, &r)) + range->time = range->time * 1000 + r % 1000; + } - return r; + field = strchr(field, ';'); + if (field) + ++field; + } + + return r; } diff --git a/gear-lib/librtsp/sdp.c b/gear-lib/librtsp/sdp.c index 677ab7a..5cac079 100644 --- a/gear-lib/librtsp/sdp.c +++ b/gear-lib/librtsp/sdp.c @@ -19,11 +19,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ -#include "sdp.h" +#include #include #include #include #include +#include "sdp.h" #define SDP_TOOL_NAME ((const char *)"ipcam rtsp") #define SDP_TOOL_VERSION ((const char *)"version 2016.05.22") @@ -267,21 +268,6 @@ const char* SDP_LIVE_FMT = int get_sdp(struct media_source *ms, char *sdp, size_t len) { - int sdp_len = 0; - char sdp_filter[128]; - char sdp_range[128]; - const char *sdp_media = "m=video 0 RTP/AVP 33\r\nc=IN IP4 0.0.0.0\r\nb=AS:5000\r\na=control:track1"; - - float dur = duration(); - if (dur == 0.0) { - snprintf(sdp_range, sizeof(sdp_range), "%s", "a=range:npt=0-\r\n"); - } else if (dur > 0.0) { - snprintf(sdp_range, sizeof(sdp_range), "%s", "a=range:npt=0-%.3f\r\n"); - } else { // subsessions have differing durations, so "a=range:" lines go there - snprintf(sdp_range, sizeof(sdp_range), "%s", ""); - } - snprintf(sdp_filter, sizeof(sdp_filter), SDP_FILTER_FMT, RTSP_SERVER_IP); - char const* const sdp_prefix_fmt = "v=0\r\n" "o=- %ld%06ld %d IN IP4 %s\r\n" @@ -296,6 +282,20 @@ int get_sdp(struct media_source *ms, char *sdp, size_t len) "a=x-qt-text-nam:%s\r\n" "a=x-qt-text-inf:%s\r\n" "%s"; + int sdp_len = 0; + char sdp_filter[128]; + char sdp_range[128]; + const char *sdp_media = "m=video 0 RTP/AVP 33\r\nc=IN IP4 0.0.0.0\r\nb=AS:5000\r\na=control:track1"; + + float dur = duration(); + if (dur == 0.0) { + snprintf(sdp_range, sizeof(sdp_range), "%s", "a=range:npt=0-\r\n"); + } else if (dur > 0.0) { + snprintf(sdp_range, sizeof(sdp_range), "%s", "a=range:npt=0-%.3f\r\n"); + } else { // subsessions have differing durations, so "a=range:" lines go there + snprintf(sdp_range, sizeof(sdp_range), "%s", ""); + } + snprintf(sdp_filter, sizeof(sdp_filter), SDP_FILTER_FMT, RTSP_SERVER_IP); sdp_len = strlen(sdp_prefix_fmt) + 20 + 6 + 20 + strlen("127.0.0.1") diff --git a/gear-lib/librtsp/test_librtsp.c b/gear-lib/librtsp/test_librtsp.c index c829e55..d461cdf 100644 --- a/gear-lib/librtsp/test_librtsp.c +++ b/gear-lib/librtsp/test_librtsp.c @@ -19,10 +19,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ +#include "librtsp.h" #include #include #include -#include "librtsp.h" int main(int argc, char **argv) { diff --git a/gear-lib/librtsp/transport_session.c b/gear-lib/librtsp/transport_session.c index 3a6bb7f..4a0354b 100644 --- a/gear-lib/librtsp/transport_session.c +++ b/gear-lib/librtsp/transport_session.c @@ -19,14 +19,16 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ -#include "transport_session.h" -#include "media_source.h" -#include "rtp.h" +#include #include #include #include #include #include +#include "transport_session.h" +#include "media_source.h" +#include "rtp.h" + #include #include #include @@ -103,40 +105,45 @@ static void *send_thread(struct thread *t, void *ptr) struct media_source *ms = ts->media_source; void *data = NULL; size_t len = 0; + unsigned int ssrc, seq; + uint64_t pts; int ret; - if (-1 == ms->open(ms, "sample.264")) { + struct media_packet *mpkt; + struct video_packet *vpkt; + struct rtp_packet *rpkt; + if (-1 == ms->_open(ms, "sample.264")) { loge("open failed!\n"); return NULL; } ms->is_active = true; - unsigned int ssrc = (unsigned int)rtp_ssrc(); - uint64_t pts = time_now_msec(); - unsigned int seq = ssrc; + ssrc = (unsigned int)rtp_ssrc(); + pts = time_now_msec(); + seq = ssrc; logd("rtp send thread %s created\n", t->name); while (t->run) { - if (-1 == ms->read(ms, &data, &len) || data == NULL) { + if (-1 == ms->_read(ms, &data, &len) || data == NULL) { loge("read failed!\n"); sleep(1); continue; } - struct media_packet *pkt = data; - switch (pkt->type) { + mpkt = data; + switch (mpkt->type) { case MEDIA_TYPE_AUDIO: logd("MEDIA_TYPE_AUDIO\n"); break; case MEDIA_TYPE_VIDEO: { logd("MEDIA_TYPE_VIDEO\n"); - struct video_packet *vpkt = pkt->video; - struct rtp_packet *pkt = rtp_packet_create(RTP_PT_H264, vpkt->size, seq, ssrc); - if (!pkt) { + vpkt = mpkt->video; + rpkt = rtp_packet_create(RTP_PT_H264, vpkt->size, seq, ssrc); + if (!rpkt) { loge("rtp_packet_create failed!\n"); break; } pts = get_ms_time_v(vpkt, vpkt->dts); logd("rtp_packet_create video size=%d, pts=%d\n", vpkt->size, pts); - ret = rtp_payload_h264_encode(ts->rtp->sock, pkt, vpkt->data, vpkt->size, pts); - seq = pkt->header.seq; - rtp_packet_destroy(pkt); + ret = rtp_payload_h264_encode(ts->rtp->sock, rpkt, vpkt->data, vpkt->size, pts); + seq = rpkt->header.seq; + rtp_packet_destroy(rpkt); if (ret == -1) { loge("rtp_payload_h264_encode failed!\n"); t->run = false; @@ -154,9 +161,10 @@ static void *send_thread(struct thread *t, void *ptr) static void on_recv(int fd, void *arg) { + int ret; char buf[2048]; memset(buf, 0, sizeof(buf)); - int ret = sock_recv(fd, buf, 2048); + ret = sock_recv(fd, buf, 2048); if (ret > 0) { rtcp_parse(buf, ret); } else if (ret == 0) { diff --git a/gear-lib/libuvc/libuvc.c b/gear-lib/libuvc/libuvc.c index 34eeece..a58b8d3 100644 --- a/gear-lib/libuvc/libuvc.c +++ b/gear-lib/libuvc/libuvc.c @@ -35,8 +35,17 @@ extern struct uvc_ops v4l2_ops; extern struct uvc_ops dshow_ops; #endif -static struct uvc_ops *uvc_ops[] = { +enum uvc_type { +#if defined (OS_LINUX) + UVC_TYPE_V4L2, +#elif defined (OS_WINDOWS) + UVC_TYPE_DSHOW, +#endif + UVC_TYPE_DUMMY, + UVC_TYPE_MAX, +}; +static struct uvc_ops *uvc_ops[] = { #if defined (OS_LINUX) &dummy_ops, &v4l2_ops, @@ -46,10 +55,21 @@ static struct uvc_ops *uvc_ops[] = { NULL, }; -struct uvc_ctx *uvc_open(enum uvc_type type, const char *dev, struct uvc_config *conf) +struct uvc_ctx *uvc_open(const char *dev, struct uvc_config *conf) { + enum uvc_type type; struct uvc_ctx *uvc; - if (!dev || !conf) { +#if defined (OS_LINUX) + type = UVC_TYPE_V4L2; +#elif defined (OS_WINDOWS) + type = UVC_TYPE_DSHOW; +#endif + if (!dev) { + type = UVC_TYPE_DUMMY; + dev = conf->dev_name; + printf("%s:%d open dummy device\n", __func__, __LINE__); + } + if (!conf) { printf("%s:%d invalid paraments!\n", __func__, __LINE__); return NULL; } diff --git a/gear-lib/libuvc/libuvc.h b/gear-lib/libuvc/libuvc.h index 8c91dc8..dc9615f 100644 --- a/gear-lib/libuvc/libuvc.h +++ b/gear-lib/libuvc/libuvc.h @@ -37,16 +37,6 @@ extern "C" { #endif -enum uvc_type { -#if defined (OS_LINUX) - UVC_TYPE_DUMMY = 0, - UVC_TYPE_V4L2, -#elif defined (OS_WINDOWS) - UVC_TYPE_DSHOW, -#endif - UVC_TYPE_MAX, -}; - struct uvc_ctx; struct uvc_ops; typedef int (video_frame_cb)(struct uvc_ctx *c, struct video_frame *frame); @@ -56,6 +46,7 @@ struct uvc_config { uint32_t height; rational_t fps; enum pixel_format format; + char *dev_name; }; struct uvc_image_quality { @@ -115,7 +106,7 @@ struct uvc_ops { int (*query_frame)(struct uvc_ctx *c, struct video_frame *frame); }; -GEAR_API struct uvc_ctx *uvc_open(enum uvc_type type, const char *dev, struct uvc_config *conf); +GEAR_API struct uvc_ctx *uvc_open(const char *dev, struct uvc_config *conf); GEAR_API int uvc_ioctl(struct uvc_ctx *c, unsigned long int cmd, ...); GEAR_API void uvc_close(struct uvc_ctx *c); diff --git a/gear-lib/libuvc/test_libuvc.c b/gear-lib/libuvc/test_libuvc.c index 5b9a5fd..e57f559 100644 --- a/gear-lib/libuvc/test_libuvc.c +++ b/gear-lib/libuvc/test_libuvc.c @@ -62,13 +62,7 @@ int v4l2_test() {30, 1}, PIXEL_FORMAT_YUY2, }; - enum uvc_type type; -#if defined (OS_LINUX) - type = UVC_TYPE_V4L2; -#elif defined (OS_WINDOWS) - type = UVC_TYPE_DSHOW; -#endif - uvc = uvc_open(type, VIDEO_DEV, &conf); + uvc = uvc_open(VIDEO_DEV, &conf); if (!uvc) { printf("uvc_open failed!\n"); return -1; @@ -104,8 +98,9 @@ int dummy_test() 240, {30, 1}, PIXEL_FORMAT_YUY2, + "sample_320x240_yuv422p.yuv", }; - struct uvc_ctx *uvc = uvc_open(UVC_TYPE_DUMMY, "sample_320x240_yuv422p.yuv", &conf); + struct uvc_ctx *uvc = uvc_open(NULL, &conf); if (!uvc) { printf("uvc_open failed!\n"); return -1; diff --git a/gear-lib/libvector/libvector.h b/gear-lib/libvector/libvector.h index ed70f6b..44f2109 100644 --- a/gear-lib/libvector/libvector.h +++ b/gear-lib/libvector/libvector.h @@ -78,6 +78,8 @@ void *_vector_at(struct vector *v, int pos); #if defined (__linux__) || defined (__CYGWIN__) #define vector_create(type_t) \ ({type_t t;struct vector *v = _vector_create(sizeof(t)); v;}) +#else +#define vector_create(type_t) _vector_create(sizeof(type_t)) #endif void vector_destroy(struct vector *v); int vector_empty(struct vector *v); @@ -90,6 +92,8 @@ void vector_pop_back(struct vector *v); memcpy(&__tmp, vector_last(v), v->type_size); \ &__tmp; \ }) +#else +#define vector_back(v, type_t) (type_t *)vector_last(v) #endif #define vector_iter_valuep(vector, iter, type_t) \ From b127027912d37810442a4826dd7f923e4aee20b1 Mon Sep 17 00:00:00 2001 From: gozfree Date: Mon, 11 Apr 2022 23:41:56 +0800 Subject: [PATCH 15/16] fix compile on linux --- gear-lib/librtsp/media_source_h264.c | 8 ++++---- gear-lib/librtsp/media_source_live.c | 6 +++--- gear-lib/librtsp/rtp.h | 1 + gear-lib/librtsp/rtsp_parser.c | 1 - gear-lib/librtsp/transport_session.c | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/gear-lib/librtsp/media_source_h264.c b/gear-lib/librtsp/media_source_h264.c index d3d14f2..b274bff 100644 --- a/gear-lib/librtsp/media_source_h264.c +++ b/gear-lib/librtsp/media_source_h264.c @@ -203,8 +203,8 @@ static int sdp_generate(struct media_source *ms) struct media_source media_source_h264 = { .name = "H264", .sdp_generate = sdp_generate, - .open = h264_file_open, - .read = h264_file_read_frame, - .write = h264_file_write, - .close = h264_file_close, + ._open = h264_file_open, + ._read = h264_file_read_frame, + ._write = h264_file_write, + ._close = h264_file_close, }; diff --git a/gear-lib/librtsp/media_source_live.c b/gear-lib/librtsp/media_source_live.c index 1649775..455060a 100644 --- a/gear-lib/librtsp/media_source_live.c +++ b/gear-lib/librtsp/media_source_live.c @@ -447,7 +447,7 @@ static int live_read(struct media_source *ms, void **data, size_t *len) struct media_source media_source_uvc = { .name = "uvc", .sdp_generate = sdp_generate, - .open = live_open, - .read = live_read, - .close = live_close, + ._open = live_open, + ._read = live_read, + ._close = live_close, }; diff --git a/gear-lib/librtsp/rtp.h b/gear-lib/librtsp/rtp.h index fca6e41..19bb83e 100644 --- a/gear-lib/librtsp/rtp.h +++ b/gear-lib/librtsp/rtp.h @@ -22,6 +22,7 @@ #ifndef LIBRTP_H #define LIBRTP_H +#include #include #if defined (OS_LINUX) #include diff --git a/gear-lib/librtsp/rtsp_parser.c b/gear-lib/librtsp/rtsp_parser.c index 7ea2c2e..fa05681 100644 --- a/gear-lib/librtsp/rtsp_parser.c +++ b/gear-lib/librtsp/rtsp_parser.c @@ -236,7 +236,6 @@ int parse_transport(struct transport_header *t, char *buf, int len) { char const* fields; char* field; - size_t n = 0; // First, find "Transport:" while (1) { if (*buf == '\0') diff --git a/gear-lib/librtsp/transport_session.c b/gear-lib/librtsp/transport_session.c index 4a0354b..55bfc7e 100644 --- a/gear-lib/librtsp/transport_session.c +++ b/gear-lib/librtsp/transport_session.c @@ -155,7 +155,7 @@ static void *send_thread(struct thread *t, void *ptr) } } ms->is_active = false; - ms->close(ms); + ms->_close(ms); return NULL; } From da438cae29b922ded745137a3e1ccd557e15ede7 Mon Sep 17 00:00:00 2001 From: gozfree Date: Wed, 13 Apr 2022 18:38:12 +0800 Subject: [PATCH 16/16] add cmake support --- CMakeLists.txt | 32 ++++------------------------- build/cmake_env.inc | 24 ++++++++++++++++++++++ gear-lib/CMakeLists.txt | 32 +++++++++++++++++++++++++++++ gear-lib/libbase64/CMakeLists.txt | 7 +++++++ gear-lib/libbitmap/CMakeLists.txt | 7 +++++++ gear-lib/libconfig/CMakeLists.txt | 7 +++++++ gear-lib/libdarray/CMakeLists.txt | 7 +++++++ gear-lib/libdict/CMakeLists.txt | 7 +++++++ gear-lib/libfile/CMakeLists.txt | 13 ++++++++++++ gear-lib/libgevent/CMakeLists.txt | 14 +++++++++++++ gear-lib/liblog/CMakeLists.txt | 7 +++++++ gear-lib/libmedia-io/CMakeLists.txt | 7 +++++++ gear-lib/libposix/CMakeLists.txt | 16 +++++++++++++++ gear-lib/libqueue/CMakeLists.txt | 7 +++++++ gear-lib/librtmpc/CMakeLists.txt | 9 ++++++++ gear-lib/librtsp/CMakeLists.txt | 7 +++++++ gear-lib/libsock/CMakeLists.txt | 7 +++++++ gear-lib/libthread/CMakeLists.txt | 7 +++++++ gear-lib/libtime/CMakeLists.txt | 7 +++++++ gear-lib/libuvc/CMakeLists.txt | 14 +++++++++++++ 20 files changed, 210 insertions(+), 28 deletions(-) create mode 100644 build/cmake_env.inc create mode 100644 gear-lib/CMakeLists.txt create mode 100644 gear-lib/libbase64/CMakeLists.txt create mode 100644 gear-lib/libbitmap/CMakeLists.txt create mode 100644 gear-lib/libconfig/CMakeLists.txt create mode 100644 gear-lib/libdarray/CMakeLists.txt create mode 100644 gear-lib/libdict/CMakeLists.txt create mode 100644 gear-lib/libfile/CMakeLists.txt create mode 100644 gear-lib/libgevent/CMakeLists.txt create mode 100644 gear-lib/liblog/CMakeLists.txt create mode 100644 gear-lib/libmedia-io/CMakeLists.txt create mode 100644 gear-lib/libposix/CMakeLists.txt create mode 100644 gear-lib/libqueue/CMakeLists.txt create mode 100644 gear-lib/librtmpc/CMakeLists.txt create mode 100644 gear-lib/librtsp/CMakeLists.txt create mode 100644 gear-lib/libsock/CMakeLists.txt create mode 100644 gear-lib/libthread/CMakeLists.txt create mode 100644 gear-lib/libtime/CMakeLists.txt create mode 100644 gear-lib/libuvc/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d168c1..fe38504 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,31 +1,7 @@ -cmake_minimum_required(VERSION 3.9) +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) -include_directories(include) +PROJECT(gear-lib) -set(darray_src src/libdarray.c src/libserializer.c) -set(hash_src src/libhash.c) -set(posix_src src/libposix.c) -set(base64_src src/libbase64.c) -set(log_src src/liblog.c) -set(file_src src/filewatcher.c src/fio.c src/io.c src/libfile.c) -set(ipc_src src/libipc.c src/msgq_posix.c src/msgq_sysv.c src/netlink.c src/shm.c src/unix_socket.c) -set(gevent_src src/epoll.c src/libgevent.c src/poll.c src/select.c) -set(dict_src src/libdict.c) -set(queue_src src/libqueue.c) -set(vector_src src/libvector.c) -set(media-io_src src/audio-def.c src/libmedia-io.c src/video-def.c) - -add_library(posix SHARED ${posix_src}) -add_library(hash SHARED ${hash_src}) -add_library(media-io SHARED ${media-io_src}) -add_library(darray SHARED ${darray_src}) -add_library(gevent SHARED ${gevent_src}) -add_library(dict SHARED ${dict_src}) -add_library(ipc SHARED ${ipc_src}) -target_link_libraries(ipc rt gevent dict) - -add_library(log SHARED ${log_src}) -add_library(file SHARED ${file_src}) -add_library(queue SHARED ${queue_src}) -add_library(vector SHARED ${vector_src}) +INCLUDE(build/cmake_env.inc) +ADD_SUBDIRECTORY(gear-lib) diff --git a/build/cmake_env.inc b/build/cmake_env.inc new file mode 100644 index 0000000..a3548bf --- /dev/null +++ b/build/cmake_env.inc @@ -0,0 +1,24 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) +PROJECT(gear-lib) + +#set output directory +SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/lib) +SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build/bin) +SET(TD_TESTS_OUTPUT_DIR ${PROJECT_BINARY_DIR}/test) + +MESSAGE(STATUS "============= Compile Env ============= ") +MESSAGE(STATUS " CPU Info: " ${CMAKE_SYSTEM_PROCESSOR}) +MESSAGE(STATUS " OS Info: " ${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_VERSION}) +MESSAGE(STATUS "Host Info: " ${CMAKE_HOST_SYSTEM_NAME}) +MESSAGE(STATUS "============= Compile Env ============= ") + +MESSAGE(STATUS "Project source directory: " ${PROJECT_SOURCE_DIR}) +MESSAGE(STATUS "Project binary files output path: " ${PROJECT_BINARY_DIR}) +MESSAGE(STATUS "Project executable files output path: " ${EXECUTABLE_OUTPUT_PATH}) +MESSAGE(STATUS "Project library files output path: " ${LIBRARY_OUTPUT_PATH}) + +IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux") + SET(OS_LINUX TRUE) +ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Windows") + SET(OS_WINDOWS TRUE) +ENDIF () diff --git a/gear-lib/CMakeLists.txt b/gear-lib/CMakeLists.txt new file mode 100644 index 0000000..33cb7bb --- /dev/null +++ b/gear-lib/CMakeLists.txt @@ -0,0 +1,32 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +SET(POSIX_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libposix/) +SET(DICT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libdict/) +SET(DARRAY_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libdarray/) +SET(THREAD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libthread/) +SET(GEVENT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libgevent/) +SET(MEDIA_IO_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libmedia-io/) +SET(QUEUE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libqueue/) +SET(LOG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/liblog/) +SET(SOCK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libsock/) +SET(FILE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libfile/) +SET(UVC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libuvc/) +SET(TIME_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libtime/) + +ADD_SUBDIRECTORY(libposix) +ADD_SUBDIRECTORY(libbase64) +ADD_SUBDIRECTORY(libbitmap) +ADD_SUBDIRECTORY(libdict) +ADD_SUBDIRECTORY(libdarray) +ADD_SUBDIRECTORY(libqueue) +ADD_SUBDIRECTORY(libthread) +ADD_SUBDIRECTORY(libgevent) +ADD_SUBDIRECTORY(libfile) +ADD_SUBDIRECTORY(libtime) +ADD_SUBDIRECTORY(libsock) +ADD_SUBDIRECTORY(liblog) +ADD_SUBDIRECTORY(libmedia-io) +ADD_SUBDIRECTORY(libuvc) +ADD_SUBDIRECTORY(librtmpc) +ADD_SUBDIRECTORY(librtsp) diff --git a/gear-lib/libbase64/CMakeLists.txt b/gear-lib/libbase64/CMakeLists.txt new file mode 100644 index 0000000..8cdaf85 --- /dev/null +++ b/gear-lib/libbase64/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(base64 ${SOURCE_FILES}) diff --git a/gear-lib/libbitmap/CMakeLists.txt b/gear-lib/libbitmap/CMakeLists.txt new file mode 100644 index 0000000..afc9d89 --- /dev/null +++ b/gear-lib/libbitmap/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(bitmap ${SOURCE_FILES}) diff --git a/gear-lib/libconfig/CMakeLists.txt b/gear-lib/libconfig/CMakeLists.txt new file mode 100644 index 0000000..be863a7 --- /dev/null +++ b/gear-lib/libconfig/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SOURCE_FILES) + +add_library(config ${SOURCE_FILES}) diff --git a/gear-lib/libdarray/CMakeLists.txt b/gear-lib/libdarray/CMakeLists.txt new file mode 100644 index 0000000..ec671db --- /dev/null +++ b/gear-lib/libdarray/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(darray ${SOURCE_FILES}) diff --git a/gear-lib/libdict/CMakeLists.txt b/gear-lib/libdict/CMakeLists.txt new file mode 100644 index 0000000..244b8c4 --- /dev/null +++ b/gear-lib/libdict/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(dict ${SOURCE_FILES}) diff --git a/gear-lib/libfile/CMakeLists.txt b/gear-lib/libfile/CMakeLists.txt new file mode 100644 index 0000000..999b5e4 --- /dev/null +++ b/gear-lib/libfile/CMakeLists.txt @@ -0,0 +1,13 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR} ${DICT_INCLUDE_DIR} ${GEVENT_INCLUDE_DIR} ${DARRAY_INCLUDE_DIR} ${THREAD_INCLUDE_DIR}) + +LIST(APPEND SOURCE_FILES libfile.c fio.c io.c) + +IF (DEFINED OS_LINUX) +LIST(APPEND SOURCE_FILES filewatcher.c) +ENDIF () + +ADD_LIBRARY(file ${SOURCE_FILES}) + diff --git a/gear-lib/libgevent/CMakeLists.txt b/gear-lib/libgevent/CMakeLists.txt new file mode 100644 index 0000000..1dd03d7 --- /dev/null +++ b/gear-lib/libgevent/CMakeLists.txt @@ -0,0 +1,14 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR} ${DARRAY_INCLUDE_DIR} ${THREAD_INCLUDE_DIR}) + +LIST(APPEND SOURCE_FILES libgevent.c) + +IF (DEFINED OS_LINUX) +LIST(APPEND SOURCE_FILES epoll.c libgevent.c poll.c select.c) +ELSEIF (DEFINED OS_WINDOWS) +LIST(APPEND SOURCE_FILES wepoll.c iocp.c) +ENDIF () + +ADD_LIBRARY(gevent ${SOURCE_FILES}) diff --git a/gear-lib/liblog/CMakeLists.txt b/gear-lib/liblog/CMakeLists.txt new file mode 100644 index 0000000..992e950 --- /dev/null +++ b/gear-lib/liblog/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(log ${SOURCE_FILES}) diff --git a/gear-lib/libmedia-io/CMakeLists.txt b/gear-lib/libmedia-io/CMakeLists.txt new file mode 100644 index 0000000..c7b9d6a --- /dev/null +++ b/gear-lib/libmedia-io/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(media-io ${SOURCE_FILES}) diff --git a/gear-lib/libposix/CMakeLists.txt b/gear-lib/libposix/CMakeLists.txt new file mode 100644 index 0000000..39df381 --- /dev/null +++ b/gear-lib/libposix/CMakeLists.txt @@ -0,0 +1,16 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(.) + +LIST(APPEND SOURCE_FILES libposix.c) + +IF (DEFINED OS_LINUX) +LIST(APPEND SOURCE_FILES libposix4nix.c) +ELSEIF (DEFINED OS_WINDOWS) +AUX_SOURCE_DIRECTORY(MsvcLibX MSVCLIBX_SRC) +AUX_SOURCE_DIRECTORY(pthreads4w PTHREADS4W_SRC) +LIST(APPEND SOURCE_FILES ${MSVCLIBX_SRC} ${PTHREADS4W_SRC}) +ENDIF () + +ADD_LIBRARY(posix ${SOURCE_FILES}) diff --git a/gear-lib/libqueue/CMakeLists.txt b/gear-lib/libqueue/CMakeLists.txt new file mode 100644 index 0000000..b50c36c --- /dev/null +++ b/gear-lib/libqueue/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(queue ${SOURCE_FILES}) diff --git a/gear-lib/librtmpc/CMakeLists.txt b/gear-lib/librtmpc/CMakeLists.txt new file mode 100644 index 0000000..9d1f916 --- /dev/null +++ b/gear-lib/librtmpc/CMakeLists.txt @@ -0,0 +1,9 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR} ${DARRAY_INCLUDE_DIR} ${MEDIA_IO_INCLUDE_DIR} ${QUEUE_INCLUDE_DIR} ${THREAD_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_CRYPTO") + +ADD_LIBRARY(rtmpc ${SOURCE_FILES}) diff --git a/gear-lib/librtsp/CMakeLists.txt b/gear-lib/librtsp/CMakeLists.txt new file mode 100644 index 0000000..53861e6 --- /dev/null +++ b/gear-lib/librtsp/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR} ${LOG_INCLUDE_DIR} ${DICT_INCLUDE_DIR} ${THREAD_INCLUDE_DIR} ${GEVENT_INCLUDE_DIR} ${DARRAY_INCLUDE_DIR} ${SOCK_INCLUDE_DIR} ${MEDIA_IO_INCLUDE_DIR} ${FILE_INCLUDE_DIR} ${QUEUE_INCLUDE_DIR} ${UVC_INCLUDE_DIR} ${TIME_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(rtsp ${SOURCE_FILES}) diff --git a/gear-lib/libsock/CMakeLists.txt b/gear-lib/libsock/CMakeLists.txt new file mode 100644 index 0000000..12ab42c --- /dev/null +++ b/gear-lib/libsock/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR} ${GEVENT_INCLUDE_DIR} ${DARRAY_INCLUDE_DIR} ${THREAD_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(sock ${SOURCE_FILES}) diff --git a/gear-lib/libthread/CMakeLists.txt b/gear-lib/libthread/CMakeLists.txt new file mode 100644 index 0000000..fdd98ea --- /dev/null +++ b/gear-lib/libthread/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(thread ${SOURCE_FILES}) diff --git a/gear-lib/libtime/CMakeLists.txt b/gear-lib/libtime/CMakeLists.txt new file mode 100644 index 0000000..6f4ee15 --- /dev/null +++ b/gear-lib/libtime/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR}) +AUX_SOURCE_DIRECTORY(. SOURCE_FILES) + +ADD_LIBRARY(time ${SOURCE_FILES}) diff --git a/gear-lib/libuvc/CMakeLists.txt b/gear-lib/libuvc/CMakeLists.txt new file mode 100644 index 0000000..167fbf6 --- /dev/null +++ b/gear-lib/libuvc/CMakeLists.txt @@ -0,0 +1,14 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20) +PROJECT(gear-lib) + +INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR} ${MEDIA_IO_INCLUDE_DIR} ${THREAD_INCLUDE_DIR}) + +LIST(APPEND SOURCE_FILES libuvc.c) + +IF (DEFINED OS_LINUX) +LIST(APPEND SOURCE_FILES v4l2.c) +ELSEIF (DEFINED OS_WINDOWS) +LIST(APPEND SOURCE_FILES dshow.c) +ENDIF () + +ADD_LIBRARY(uvc ${SOURCE_FILES})