#ifdef __cplusplus extern "C"{ #endif #include #include #include #include "libcsoftbus.h" #ifdef __cplusplus } #endif #define check_only(fn, lib) \ do{ \ if(!fn){ \ dlclose(lib); \ printf("run func %s error\n", #fn); \ return; \ } \ }while(0) #define check_with_ret(fn, lib, flag) \ do{ \ if(!fn){ \ dlclose(lib); \ printf("run func %s error\n", #fn); \ return flag; \ } \ }while(0) hcsoftbus c_softbus_handle(const char *so){ hcsoftbus lib = dlopen(so, RTLD_LAZY); if(lib){ fn_shm_destroy = (tfn_shm_destroy)dlsym(lib, l_shm_destroy); check_with_ret(fn_shm_destroy, lib, NULL); }else{ printf("softbus dlopen - %s\n", dlerror()); } return lib; } void c_softbus_release(hcsoftbus lib){ if(lib) { dlclose(lib); } } void wrap_fn_shm_init(hcsoftbus lib, int size){ if (!fn_shm_init){ fn_shm_init = (tfn_shm_init)dlsym(lib, l_shm_init); check_only(fn_shm_init, lib); } fn_shm_init(size); } int wrap_fn_shm_alloc_key(hcsoftbus lib){ if (!fn_shm_alloc_key){ fn_shm_alloc_key = (tfn_shm_alloc_key)dlsym(lib, l_shm_alloc_key); check_with_ret(fn_shm_alloc_key, lib, -1); } return fn_shm_alloc_key(); } void wrap_fn_shm_destroy(hcsoftbus lib){ if (!fn_shm_destroy){ printf("shm destroy failed\n"); return; } fn_shm_destroy(); } void wrap_fn_shm_rm_dead_queue(hcsoftbus lib, void *array, int len){ if (!fn_shm_rm_dead_queue){ fn_shm_rm_dead_queue = (tfn_shm_rm_dead_queue)dlsym(lib, l_shm_rm_dead_queue); check_only(fn_shm_rm_dead_queue, lib); } fn_shm_rm_dead_queue(array, len); } void* wrap_fn_queue_create(hcsoftbus lib, int *key, int queue_size){ if (!fn_queue_create){ fn_queue_create = (tfn_queue_create)dlsym(lib, l_queue_create); check_with_ret(fn_queue_create, lib, NULL); } return fn_queue_create(key, queue_size); } void* wrap_fn_queue_attach(hcsoftbus lib, int key){ if (!fn_queue_attach){ fn_queue_attach = (tfn_queue_attach)dlsym(lib, l_queue_attach); check_with_ret(fn_queue_attach, lib, NULL); } return fn_queue_attach(key); } void wrap_fn_queue_drop(hcsoftbus lib, void* shmq){ if (!fn_queue_drop){ fn_queue_drop = (tfn_queue_drop)dlsym(lib, l_queue_drop); check_only(fn_queue_drop, lib); } fn_queue_drop(shmq); } int wrap_fn_queue_size(hcsoftbus lib, void *shmq){ if (!fn_queue_size){ fn_queue_size = (tfn_queue_size)dlsym(lib, l_queue_size); check_with_ret(fn_queue_size, lib, 0); } return fn_queue_size(shmq); } int wrap_fn_queue_full(hcsoftbus lib, void *shmq){ if (!fn_queue_full){ fn_queue_full = (tfn_queue_full)dlsym(lib, l_queue_full); check_with_ret(fn_queue_full, lib, 1); } return fn_queue_full(shmq); } int wrap_fn_queue_empty(hcsoftbus lib, void *shmq){ if (!fn_queue_empty){ fn_queue_empty = (tfn_queue_empty)dlsym(lib, l_queue_empty); check_with_ret(fn_queue_empty, lib, 0); } return fn_queue_empty(shmq); } int wrap_fn_queue_push(hcsoftbus lib, void *shmq, void *ele, int ele_size){ if (!fn_queue_push){ fn_queue_push = (tfn_queue_push)dlsym(lib, l_queue_push); check_with_ret(fn_queue_push, lib, 0); } return fn_queue_push(shmq, ele, ele_size); } int wrap_fn_queue_push_nowait(hcsoftbus lib, void *shmq, void *ele, int ele_size){ if (!fn_queue_push_nowait){ fn_queue_push_nowait = (tfn_queue_push_nowait)dlsym(lib, l_queue_push_nowait); check_with_ret(fn_queue_push_nowait, lib, 0); } return fn_queue_push_nowait(shmq, ele, ele_size); } int wrap_fn_queue_push_timeout(hcsoftbus lib, void *shmq, void *ele, int ele_size, int sec, int nsec){ if (!fn_queue_push_timeout){ fn_queue_push_timeout = (tfn_queue_push_timeout)dlsym(lib, l_queue_push_timeout); check_with_ret(fn_queue_push_timeout, lib, 0); } return fn_queue_push_timeout(shmq, ele, ele_size, sec, nsec); } int wrap_fn_queue_pop(hcsoftbus lib, void *shmq, void **ele, int *ele_size){ if (!fn_queue_pop){ fn_queue_pop = (tfn_queue_pop)dlsym(lib, l_queue_pop); check_with_ret(fn_queue_pop, lib, 0); } return fn_queue_pop(shmq, ele, ele_size); } int wrap_fn_queue_pop_nowait(hcsoftbus lib, void *shmq, void **ele, int *ele_size){ if (!fn_queue_pop_nowait){ fn_queue_pop_nowait = (tfn_queue_pop_nowait)dlsym(lib, l_queue_pop_nowait); check_with_ret(fn_queue_pop_nowait, lib, 0); } return fn_queue_pop_nowait(shmq, ele, ele_size); } int wrap_fn_queue_pop_timeout(hcsoftbus lib, void *shmq, void **ele, int *ele_size, int sec, int nsec){ if (!fn_queue_pop_timeout){ fn_queue_pop_timeout = (tfn_queue_pop_timeout)dlsym(lib, l_queue_pop_timeout); check_with_ret(fn_queue_pop_timeout, lib, 0); } return fn_queue_pop_timeout(shmq, ele, ele_size, sec, nsec); } void wrap_fn_queue_free_poped(hcsoftbus lib, void *ptr){ if (!fn_queue_free_poped){ fn_queue_free_poped = (tfn_queue_free_poped)dlsym(lib, l_queue_free_poped); check_only(fn_queue_free_poped, lib); } fn_queue_free_poped(ptr); } //////////////////////////////////////////// // socket mode //////////////////////////////////////////// void *wrap_fn_socket_open(hcsoftbus lib, int mod){ if (!fn_socket_open) { fn_socket_open = (tfn_socket_open)dlsym(lib, l_socket_open); check_with_ret(fn_socket_open, lib, NULL); } return fn_socket_open(mod); } int wrap_fn_socket_close(hcsoftbus lib, void* s){ if(!fn_socket_close){ fn_socket_close = (tfn_socket_close)dlsym(lib, l_socket_close); check_with_ret(fn_socket_close, lib, -1); } return fn_socket_listen(s); } int wrap_fn_socket_bind(hcsoftbus lib, void *s, int port){ if (!fn_socket_bind){ fn_socket_bind = (tfn_socket_bind)dlsym(lib, l_socket_bind); check_with_ret(fn_socket_bind, lib, -1); } return fn_socket_bind(s, port); } int wrap_fn_socket_listen(hcsoftbus lib, void *s){ if (!fn_socket_listen){ fn_socket_listen = (tfn_socket_listen)dlsym(lib, l_socket_listen); check_with_ret(fn_socket_listen, lib, -1); } return fn_socket_listen(s); } int wrap_fn_socket_connect(hcsoftbus lib, void *s, int port){ if (!fn_socket_connect){ fn_socket_connect = (tfn_socket_connect)dlsym(lib, l_socket_connect); check_with_ret(fn_socket_connect, lib, -1); } return fn_socket_connect(s, port); } int wrap_fn_socket_send(hcsoftbus lib, void *s, const void *buf, const int size){ if (!fn_socket_send){ fn_socket_send = (tfn_socket_send)dlsym(lib, l_socket_send); check_with_ret(fn_socket_send, lib, -1); } return fn_socket_send(s, buf, size); } int wrap_fn_socket_recv(hcsoftbus lib, void *s, void **buf, int *size){ if (!fn_socket_recv){ fn_socket_recv = (tfn_socket_recv)dlsym(lib, l_socket_recv); check_with_ret(fn_socket_recv, lib, -1); } return fn_socket_recv(s, buf, size); } void wrap_fn_socket_buf_free(hcsoftbus lib, void *buf){ if (!fn_socket_buf_free){ fn_socket_buf_free = (tfn_socket_buf_free)dlsym(lib, l_socket_free); } if (fn_socket_buf_free){ fn_socket_buf_free(buf); } } int wrap_fn_socket_port(hcsoftbus lib, void *s){ if (!fn_socket_port){ fn_socket_port = (tfn_socket_port)dlsym(lib, l_socket_port); check_with_ret(fn_socket_port, lib, -1); } return fn_socket_port(s); } //////////////////////////////////////////// // dgram socket mode //////////////////////////////////////////// void *wrap_fn_dgram_socket_open(hcsoftbus lib){ if (!fn_dgram_socket_open){ fn_dgram_socket_open = (tfn_dgram_socket_open)dlsym(lib , l_dgram_socket_open); check_with_ret(fn_dgram_socket_open, lib, NULL); } return fn_dgram_socket_open(); } int wrap_fn_dgram_socket_close(hcsoftbus lib, void *s){ if (!fn_dgram_socket_close){ fn_dgram_socket_close = (tfn_dgram_socket_close)dlsym(lib, l_dgram_socket_close); check_with_ret(fn_dgram_socket_close, lib, -1); } return fn_dgram_socket_close(s); } int wrap_fn_dgram_socket_bind(hcsoftbus lib, void *s, int port){ if (!fn_dgram_socket_bind){ fn_dgram_socket_bind = (tfn_dgram_socket_bind)dlsym(lib, l_dgram_socket_bind); check_with_ret(fn_dgram_socket_bind, lib, -1); } return fn_dgram_socket_bind(s, port); } int wrap_fn_dgram_socket_force_bind(hcsoftbus lib, void *s, int port){ if (!fn_dgram_socket_force_bind){ fn_dgram_socket_force_bind = (tfn_dgram_socket_force_bind)dlsym(lib, l_dgram_socket_force_bind); check_with_ret(fn_dgram_socket_force_bind, lib, -1); } return fn_dgram_socket_force_bind(s, port); } int wrap_fn_dgram_socket_sendto(hcsoftbus lib, void *s, const void *buf, const int size, const int port){ if (!fn_dgram_socket_sendto){ fn_dgram_socket_sendto = (tfn_dgram_socket_sendto)dlsym(lib, l_dgram_socket_sendto); check_with_ret(fn_dgram_socket_sendto, lib, -1); } return fn_dgram_socket_sendto(s, buf, size, port); } int wrap_fn_dgram_socket_recvfrom(hcsoftbus lib, void *s, void **buf, int *size, int *port){ if (!fn_dgram_socket_recvfrom){ fn_dgram_socket_recvfrom = (tfn_dgram_socket_recvfrom)dlsym(lib, l_dgram_socket_recvfrom); check_with_ret(fn_dgram_socket_recvfrom, lib, -1); } return fn_dgram_socket_recvfrom(s, buf, size, port); } int wrap_fn_dgram_socket_sendandrecv(hcsoftbus lib, void *s, const void *sbuf, const int ssize, const int port, void **rbuf, int *rsize){ if (!fn_dgram_socket_sendandrecv){ fn_dgram_socket_sendandrecv = (tfn_dgram_socket_sendandrecv)dlsym(lib, l_dgram_socket_sendandrecv); check_with_ret(fn_dgram_socket_sendandrecv, lib, -1); } return fn_dgram_socket_sendandrecv(s, sbuf, ssize, port, rbuf, rsize); } int wrap_fn_dgram_socket_start_bus(hcsoftbus lib, void *s){ if (!fn_dgram_socket_start_bus){ fn_dgram_socket_start_bus = (tfn_dgram_socket_start_bus)dlsym(lib, l_dgram_socket_start_bus); check_with_ret(fn_dgram_socket_start_bus, lib, -1); } return fn_dgram_socket_start_bus(s); } int wrap_fn_dgram_socket_sub(hcsoftbus lib, void *s, void *topic, int size, int port){ if (!fn_dgram_socket_sub){ fn_dgram_socket_sub = (tfn_dgram_socket_sub)dlsym(lib, l_dgram_socket_sub); check_with_ret(fn_dgram_socket_sub, lib, -1); } return fn_dgram_socket_sub(s, topic, size, port); } int wrap_fn_dgram_socket_pub(hcsoftbus lib, void *s, void *topic, int tsize, void *content, int csize, int port){ if (!fn_dgram_socket_pub){ fn_dgram_socket_pub = (tfn_dgram_socket_pub)dlsym(lib, l_dgram_socket_pub); check_with_ret(fn_dgram_socket_pub, lib, -1); } return fn_dgram_socket_pub(s, topic, tsize, content, csize, port); } int wrap_fn_dgram_socket_port(hcsoftbus lib, void *s){ if (!fn_dgram_socket_port){ fn_dgram_socket_port = (tfn_dgram_socket_port)dlsym(lib, l_dgram_socket_port); check_with_ret(fn_dgram_socket_port, lib, -1); } return fn_dgram_socket_port(s); } void wrap_fn_dgram_socket_free(hcsoftbus lib, void *buf){ if (!fn_dgram_socket_free){ fn_dgram_socket_free = (tfn_dgram_socket_free)dlsym(lib, l_dgram_socket_free); } if (fn_dgram_socket_free){ fn_dgram_socket_free(buf); }else{ free(buf); } }