#ifdef __cplusplus extern "C"{ #endif #ifdef __cplusplus } #endif #include #include #include #include "libcbhomebus.h" #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) hbhomebus c_bhomebus_handle(const char *so){ hbhomebus 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_bhomebus_release(hbhomebus lib){ if(lib) { dlclose(lib); } } void wrap_fn_shm_init(hbhomebus 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(hbhomebus 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(hbhomebus lib){ if (!fn_shm_destroy){ printf("shm destroy failed\n"); return; } fn_shm_destroy(); } //////////////////////////////////////////// // dgram socket mode //////////////////////////////////////////// void * wrap_fn_socket_open(hbhomebus lib) { if(!fn_socket_open){ fn_socket_open = (tfn_net_mod_socket_open)dlsym(lib,l_net_mod_socket_open); check_with_ret(fn_socket_open, lib, NULL); } return fn_socket_open(); } void wrap_fn_socket_close(hbhomebus lib, void *_socket) { if(!fn_socket_close){ fn_socket_close = (tfn_net_mod_socket_close)dlsym(lib,l_net_mod_socket_close); check_only(fn_socket_close, lib); } return fn_socket_close(_socket); } int wrap_fn_socket_bind(hbhomebus lib, void * _socket, int key) { if(!fn_socket_bind){ fn_socket_bind = (tfn_net_mod_socket_bind)dlsym(lib,l_net_mod_socket_bind); check_with_ret(fn_socket_bind, lib, -1); } return fn_socket_bind(_socket, key); } int wrap_fn_socket_force_bind(hbhomebus lib, void * _socket, int key) { if(!fn_socket_force_bind){ fn_socket_force_bind = (tfn_net_mod_socket_force_bind)dlsym(lib,l_net_mod_socket_force_bind); check_with_ret(fn_socket_force_bind, lib, -1); } return fn_socket_force_bind(_socket, key); } int wrap_fn_socket_sendto(hbhomebus lib, void *_socket, const void *buf, const int size, const int key) { if(!fn_socket_sendto){ fn_socket_sendto = (tfn_net_mod_socket_sendto)dlsym(lib,l_net_mod_socket_sendto); check_with_ret(fn_socket_sendto, lib, -1); } return fn_socket_sendto(_socket, buf, size, key); } int wrap_fn_socket_sendto_timeout(hbhomebus lib, void *_socket, const void *buf, const int size, const int key, int sec, int nsec) { if(!fn_socket_sendto_timeout){ fn_socket_sendto_timeout = (tfn_net_mod_socket_sendto_timeout)dlsym(lib,l_net_mod_socket_sendto_timeout); check_with_ret(fn_socket_sendto_timeout, lib, -1); } return fn_socket_sendto_timeout(_socket, buf, size, key, sec, nsec); } int wrap_fn_socket_sendto_nowait(hbhomebus lib, void *_socket, const void *buf, const int size, const int key) { if(!fn_socket_sendto_nowait){ fn_socket_sendto_nowait = (tfn_net_mod_socket_sendto_nowait)dlsym(lib,l_net_mod_socket_sendto_nowait); check_with_ret(fn_socket_sendto_nowait, lib, -1); } return fn_socket_sendto_nowait(_socket, buf, size, key); } int wrap_fn_socket_recvfrom(hbhomebus lib, void *_socket, void **buf, int *size, int *key) { if(!fn_socket_recvfrom){ fn_socket_recvfrom = (tfn_net_mod_socket_recvfrom)dlsym(lib,l_net_mod_socket_recvfrom); check_with_ret(fn_socket_recvfrom, lib, -1); } return fn_socket_recvfrom(_socket, buf, size, key); } int wrap_fn_socket_recvfrom_timeout(hbhomebus lib, void *_socket, void **buf, int *size, int *key, int sec, int nsec) { if(!fn_socket_recvfrom_timeout){ fn_socket_recvfrom_timeout = (tfn_net_mod_socket_recvfrom_timeout)dlsym(lib,l_net_mod_socket_recvfrom_timeout); check_with_ret(fn_socket_recvfrom_timeout, lib, -1); } return fn_socket_recvfrom_timeout(_socket, buf, size, key, sec, nsec); } int wrap_fn_socket_recvfrom_nowait(hbhomebus lib, void *_socket, void **buf, int *size, int *key) { if(!fn_socket_recvfrom_nowait){ fn_socket_recvfrom_nowait = (tfn_net_mod_socket_recvfrom_nowait)dlsym(lib,l_net_mod_socket_recvfrom_nowait); check_with_ret(fn_socket_recvfrom_nowait, lib, -1); } return fn_socket_recvfrom_nowait(_socket, buf, size, key); } int wrap_fn_socket_sendandrecv(hbhomebus lib, void *_sockt, void *node_arr, int arrlen, void *send_buf, int send_size, void ** recv_arr, int *recv_arr_size) { if(!fn_socket_sendandrecv){ fn_socket_sendandrecv = (tfn_net_mod_socket_sendandrecv)dlsym(lib,l_net_mod_socket_sendandrecv); check_with_ret(fn_socket_sendandrecv, lib, -1); } return fn_socket_sendandrecv(_sockt, (net_node_t*) node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size); } int wrap_fn_socket_sendandrecv_timeout(hbhomebus lib, void *_socket, void *node_arr, int arrlen, void *send_buf, int send_size, void ** recv_arr, int *recv_arr_size, int timeout) { if(!fn_socket_sendandrecv_timeout){ fn_socket_sendandrecv_timeout = (tfn_net_mod_socket_sendandrecv_timeout)dlsym(lib,l_net_mod_socket_sendandrecv_timeout); check_with_ret(fn_socket_sendandrecv_timeout, lib, -1); } return fn_socket_sendandrecv_timeout(_socket, (net_node_t*)node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size, timeout); } int wrap_fn_socket_sendandrecv_nowait(hbhomebus lib, void *_socket, void *node_arr, int arrlen, void *send_buf, int send_size, void ** recv_arr, int *recv_arr_size) { if(!fn_socket_sendandrecv_nowait){ fn_socket_sendandrecv_nowait = (tfn_net_mod_socket_sendandrecv_nowait)dlsym(lib,l_net_mod_socket_sendandrecv_nowait); check_with_ret(fn_socket_sendandrecv_nowait, lib, -1); } return fn_socket_sendandrecv_nowait(_socket, (net_node_t*)node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size); } int wrap_fn_socket_start_bus(hbhomebus lib, void * _socket) { if(!fn_socket_start_bus){ fn_socket_start_bus = (tfn_net_mod_socket_start_bus)dlsym(lib,l_net_mod_socket_start_bus); check_with_ret(fn_socket_start_bus, lib, -1); } return fn_socket_start_bus(_socket); } int wrap_fn_socket_pub(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size) { if(!fn_socket_pub){ fn_socket_pub = (tfn_net_mod_socket_pub)dlsym(lib,l_net_mod_socket_pub); check_with_ret(fn_socket_pub, lib, -1); } return fn_socket_pub(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size); } int wrap_fn_socket_pub_timeout(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int timeout) { if(!fn_socket_pub_timeout){ fn_socket_pub_timeout = (tfn_net_mod_socket_pub_timeout)dlsym(lib,l_net_mod_socket_pub_timeout); check_with_ret(fn_socket_pub_timeout, lib, -1); } return fn_socket_pub_timeout(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size, timeout); } int wrap_fn_socket_pub_nowait(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size) { if(!fn_socket_pub_nowait){ fn_socket_pub_nowait = (tfn_net_mod_socket_pub_nowait)dlsym(lib,l_net_mod_socket_pub_nowait); check_with_ret(fn_socket_pub_nowait, lib, -1); } return fn_socket_pub_nowait(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size); } int wrap_fn_socket_sub(hbhomebus lib, void * _socket, void *topic, int size, int key) { if(!fn_socket_sub){ fn_socket_sub = (tfn_net_mod_socket_sub)dlsym(lib,l_net_mod_socket_sub); check_with_ret(fn_socket_sub, lib, -1); } return fn_socket_sub(_socket, topic, size, key); } int wrap_fn_socket_sub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int key, int sec, int nsec) { if(!fn_socket_sub_timeout){ fn_socket_sub_timeout = (tfn_net_mod_socket_sub_timeout)dlsym(lib,l_net_mod_socket_sub_timeout); check_with_ret(fn_socket_sub_timeout, lib, -1); } return fn_socket_sub_timeout(_socket, topic, size, key, sec, nsec); } int wrap_fn_socket_sub_nowait(hbhomebus lib, void * _socket, void *topic, int size, int key) { if(!fn_socket_sub_nowait){ fn_socket_sub_nowait = (tfn_net_mod_socket_sub_nowait)dlsym(lib,l_net_mod_socket_sub_nowait); check_with_ret(fn_socket_sub_nowait, lib, -1); } return fn_socket_sub_nowait(_socket, topic, size, key); } int wrap_fn_socket_desub(hbhomebus lib, void * _socket, void *topic, int size, int key) { if(!fn_socket_desub){ fn_socket_desub = (tfn_net_mod_socket_desub)dlsym(lib,l_net_mod_socket_desub); check_with_ret(fn_socket_desub, lib, -1); } return fn_socket_desub(_socket, topic, size, key); } int wrap_fn_socket_desub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int key, int sec, int nsec) { if(!fn_socket_desub_timeout){ fn_socket_desub_timeout = (tfn_net_mod_socket_desub_timeout)dlsym(lib,l_net_mod_socket_desub_timeout); check_with_ret(fn_socket_desub_timeout, lib, -1); } return fn_socket_desub_timeout(_socket, topic, size, key, sec, nsec); } int wrap_fn_socket_desub_nowait(hbhomebus lib, void * _socket, void *topic, int size, int key) { if(!fn_socket_desub_nowait){ fn_socket_desub_nowait = (tfn_net_mod_socket_sub_nowait)dlsym(lib,l_net_mod_socket_sub_nowait); check_with_ret(fn_socket_desub_nowait, lib, -1); } return fn_socket_desub_nowait(_socket, topic, size, key); } int wrap_fn_socket_get_key(hbhomebus lib, void * _socket) { if(!fn_socket_get_key){ fn_socket_get_key = (tfn_net_mod_socket_get_key)dlsym(lib,l_net_mod_socket_get_key); check_with_ret(fn_socket_get_key, lib, -1); } return fn_socket_get_key(_socket); } void wrap_fn_socket_free_recv_msg_arr(hbhomebus lib, void * arr, int size) { if(!fn_socket_free_recv_msg_arr){ fn_socket_free_recv_msg_arr = (tfn_net_mod_socket_free_recv_msg_arr)dlsym(lib,l_net_mod_socket_free_recv_msg_arr); check_only(fn_socket_free_recv_msg_arr, lib); } return fn_socket_free_recv_msg_arr((net_mod_recv_msg_t*)arr, size); } void wrap_fn_socket_free(hbhomebus lib, void *buf) { if(!fn_socket_free){ fn_socket_free = (tfn_net_mod_socket_free)dlsym(lib,l_net_mod_socket_free); check_only(fn_socket_free, lib); } return fn_socket_free(buf); } int wrap_fn_socket_remove_key(hbhomebus lib, int key){ if (!fn_socket_remove_key){ fn_socket_remove_key = (tfn_shm_mod_socket_remove_key)dlsym(lib, l_shm_mod_socket_remove_key); check_with_ret(fn_socket_remove_key, lib, -1); } return fn_socket_remove_key(key); } int wrap_fn_socket_remove_keys(hbhomebus lib, void *keys, int length){ if (!fn_socket_remove_keys){ fn_socket_remove_keys = (tfn_shm_mod_socket_remove_keys)dlsym(lib, l_shm_mod_socket_remove_keys); check_with_ret(fn_socket_remove_keys, lib, -1); } return fn_socket_remove_keys((int*)keys, length); }