elemy-utils 1.0.0
|
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <byteswap.h>
#include <time.h>
#include <malloc.h>
#include <termios.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/param.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <pthread.h>
#include <sys/resource.h>
#include "elutils/el_utils.h"
Macros | |
#define | _GNU_SOURCE |
STDERR_FILENO, tcsetattr, sysconf(_SC_PAGESIZE) ... More... | |
#define | TEST_ALLOC(PARA, POLE) if (PARA->POLE == NULL) { free(par_d); free(pars); free_conn_param(PARA); return NULL; } |
#define | BUF_SZ (1024) |
#define | INDENT_SIZE (4) |
retutn string of length INDENT_SIZE * level More... | |
Functions | |
void | el_init_rw_shared_attr (void) |
void | el_destroy_rw_shared_attr (void) |
pthread_rwlockattr_t * | el_get_rw_shared_attr (void) |
void | __attach (void) |
void | __detach (void) |
size_t | strlcpy (char *dest, const char *src, size_t dest_size) |
int | set_stdin_nowait_cr (int nowait) |
void | restore_stdin_cr (void) |
void | set_restore_stdin_cr (void) |
void | termination_handler (int signum) |
void | el_set_ctrlc_exit (int ex) |
if parameter != 0 set var to TRUE: on Ctrl-C exit. if == 0 dsont exit. More... | |
void | el_set_ctrlc_param (void *arg) |
void | el_set_ctrlc_call (el_ctrlc_call_t arg) |
void | termination_sigaction (int signum, siginfo_t *siginfo, void *param) |
void | set_termination_action (void) |
int | get_serial_stat (int fd) |
void | set_serial_stat (int fd, int serial) |
int | get_serial_dtr (int fd) |
char * | get_serial_dtr_s (int fd) |
int | get_serial_dsr (int fd) |
char * | get_serial_dsr_s (int fd) |
int | get_serial_rts (int fd) |
char * | get_serial_rts_s (int fd) |
int | get_serial_cts (int fd) |
char * | get_serial_cts_s (int fd) |
int | get_serial_cd (int fd) |
char * | get_serial_cd_s (int fd) |
void | show_serial_stat (int fd) |
void | set_serial_stat_ln (int fd, int ln_bt, int val) |
char ** | split_string (char *str, char delim, int *number) |
split string into words by delimeter, heading/trailing WhiteSpaces prohibited, possible empty words s = " ss,bb,cc,zz,,qq\0" ps_split_str(s, ',', 5, &cnt); ->{"ss", "bb", "cc", "zz", "", 0} ",ss," -> {"", "ss", "", 0} More... | |
int | cnt_splited_strings (char **ss) |
void | dbg_string_list (char **string_list) |
show NULL terminated list (array) of strings More... | |
char * | get_family_name (int family) |
return name of IPvX family More... | |
char * | get_sock_type_name (int sock_type) |
int | validate_ip_addr (const char *ip_addr) |
check is string is IPv4 or IPv6 or smth else More... | |
int | test_connect (struct addrinfo *ai_ptr, int *err) |
int | test_bind (struct addrinfo *ai_ptr, int *err) |
void | show_sa (struct sockaddr *sa, char *buf, int buf_sz) |
void | show_ai_list (struct addrinfo *ai_list) |
struct addrinfo * | tcp_get_addr_info (char *addr, char *service, int family, int slave, int *err) |
int | max_sockadddr_sz (void) |
int | conn_nonb (int fd, struct sockaddr *sa, int sa_len, uint64_t timeout, int *err) |
int | check_rs_speed (int sp) |
conn_params_t * | parse_conn_params (char *params) |
parch string with connection parameters and fill struct conn_param_t More... | |
void | free_conn_param (conn_params_t *c_p) |
int | fill_sockaddr_sinx (conn_params_t *c_p, int *err) |
const char * | argp_std_key_name (int par) |
int | wait_read_fd (int fd, uint64_t timeout, int *err) |
wait ready for read More... | |
int | wait_write_fd (int fd, uint64_t timeout, int *err) |
int | bytes_of_bits (int bits) |
char * | conn_role_name (int cr) |
void | setnonblocking (int sock) |
void | set_fd_cloexec (int fd) |
int | set_reuseaddr (int fd) |
char * | time64_to_str (uint64_t tm, char *buf) |
const char * | ps_error_text (int error) |
int | press_enter (char *msg, char *cmp_str) |
int | file_check_exist (char *fl_name) |
check file More... | |
int64_t | file_fd_size (int fd) |
int64_t | file_size (const char *filename) |
int | parse_array (void *dst, void *src, int cnt, int sz) |
parse array of strings values into array of 'sz' size elements More... | |
int | pid_exist (pid_t pid) |
check existance process(thread) with pid More... | |
void * | el_compare_mem_prepare (void *mem, int mem_sz) |
int | el_compare_mem_do (void *mem, int mem_sz, void *copy) |
long | ps_pagesize (void) |
void | ps_meminfo (long *page_sz, long *shmem_sz, long *data_sz, long *stack_sz) |
void | read_off_memory_status (statm_t *result) |
uint16_t | modbus_crc16 (uint8_t *buffer, uint16_t buffer_length) |
int | ps_put_mem (char *file_name, void *mem_ptr, int mem_sz) |
put memory to file More... | |
char * | get_n_bytes (const char *str, int pos, int cnt) |
malloc buf of cnt+1 bytes and cope there bytes from str+pos AS IS More... | |
char * | find_free_name_num (char *name) |
char * | mk_indent (int level) |
void | free_array (void **arr, int cnt) |
char * | build_msg_with_prev (const char *prev_msg, const char *format,...) |
Variables | |
static uint64_t | start_tm |
< bswap_16(x);bswap_32(x);bswap_64(x); More... | |
static pthread_rwlockattr_t | rwl_shared_attr |
static pthread_rwlockattr_t * | rwl_shared_attr_ptr = NULL |
static int | el_ctrlc_exit = 1 |
Attributes around Ctrl-C called func. More... | |
static el_ctrlc_call_t | el_ctrlc_call = NULL |
static void * | el_ctrlc_call_param = NULL |
static char * | DEFAULT_PORT = "502" |
get list of addrs parameters for pair address/port More... | |
static const uint8_t | table_crc_hi [] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 } |
static const uint8_t | table_crc_lo [] = { 0x00, 0xC0, 0xC1, 0x01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76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 } |
static char * | indent_strings [] |
#define _GNU_SOURCE |
STDERR_FILENO, tcsetattr, sysconf(_SC_PAGESIZE) ...
for 'bool' type
#define BUF_SZ (1024) |
#define INDENT_SIZE (4) |
retutn string of length INDENT_SIZE * level
up to 4 level
#define TEST_ALLOC | ( | PARA, | |
POLE | |||
) | if (PARA->POLE == NULL) { free(par_d); free(pars); free_conn_param(PARA); return NULL; } |
void __attach | ( | void | ) |
void __detach | ( | void | ) |
const char * argp_std_key_name | ( | int | par | ) |
char * build_msg_with_prev | ( | const char * | prev_msg, |
const char * | format, | ||
... | |||
) |
int bytes_of_bits | ( | int | bits | ) |
int check_rs_speed | ( | int | sp | ) |
check speed in available
sp | testing speed |
int cnt_splited_strings | ( | char ** | ss | ) |
int conn_nonb | ( | int | fd, |
struct sockaddr * | sa, | ||
int | sa_len, | ||
uint64_t | timeout, | ||
int * | err | ||
) |
char * conn_role_name | ( | int | cr | ) |
void dbg_string_list | ( | char ** | string_list | ) |
show NULL terminated list (array) of strings
int el_compare_mem_do | ( | void * | mem, |
int | mem_sz, | ||
void * | copy | ||
) |
void * el_compare_mem_prepare | ( | void * | mem, |
int | mem_sz | ||
) |
funcs detect side effect after activity
used to detect side effect after any activity with memoty block. Using: at first copy memory into malloced block. then do smth. at end compare to blocks.
first ''el_compare_mem_prepare'' return malloced block or NULL on error (ENOMEM) second ''el_compare_mem_do'' compare blocks end return 1 if differ, 0 if not or prepare bad (really impossible).
void el_destroy_rw_shared_attr | ( | void | ) |
pthread_rwlockattr_t * el_get_rw_shared_attr | ( | void | ) |
void el_init_rw_shared_attr | ( | void | ) |
void el_set_ctrlc_call | ( | el_ctrlc_call_t | arg | ) |
void el_set_ctrlc_exit | ( | int | ex | ) |
if parameter != 0 set var to TRUE: on Ctrl-C exit. if == 0 dsont exit.
void el_set_ctrlc_param | ( | void * | arg | ) |
int file_check_exist | ( | char * | fl_name | ) |
check file
fl_name | file name |
int64_t file_fd_size | ( | int | fd | ) |
int64_t file_size | ( | const char * | filename | ) |
int fill_sockaddr_sinx | ( | conn_params_t * | c_p, |
int * | err | ||
) |
char * find_free_name_num | ( | char * | name | ) |
\grief search fist free name for mask <name>_NNNN
name | masndastory part of file name |
on parameter give file name like "[[/]path_to_file/]this_is_filer_name" return first (no more then 999) free file name like "this_is_filer_name_25"
void free_array | ( | void ** | arr, |
int | cnt | ||
) |
void free_conn_param | ( | conn_params_t * | c_p | ) |
char * get_family_name | ( | int | family | ) |
return name of IPvX family
family | as in socket(), getaddrinfo() |
char * get_n_bytes | ( | const char * | str, |
int | pos, | ||
int | cnt | ||
) |
malloc buf of cnt+1 bytes and cope there bytes from str+pos AS IS
str | string for work |
pos | start of substring |
cnt | number of bytes to copy return ptr to created buffer with substring else NULL on error |
return string MUST BE FREED
int get_serial_cd | ( | int | fd | ) |
char * get_serial_cd_s | ( | int | fd | ) |
int get_serial_cts | ( | int | fd | ) |
char * get_serial_cts_s | ( | int | fd | ) |
int get_serial_dsr | ( | int | fd | ) |
char * get_serial_dsr_s | ( | int | fd | ) |
int get_serial_dtr | ( | int | fd | ) |
get fd return 0/1 status of signal/line dtr,rts,cts...
char * get_serial_dtr_s | ( | int | fd | ) |
int get_serial_rts | ( | int | fd | ) |
char * get_serial_rts_s | ( | int | fd | ) |
int get_serial_stat | ( | int | fd | ) |
char * get_sock_type_name | ( | int | sock_type | ) |
int max_sockadddr_sz | ( | void | ) |
char * mk_indent | ( | int | level | ) |
uint16_t modbus_crc16 | ( | uint8_t * | buffer, |
uint16_t | buffer_length | ||
) |
int parse_array | ( | void * | dst, |
void * | src, | ||
int | cnt, | ||
int | sz | ||
) |
parse array of strings values into array of 'sz' size elements
conn_params_t * parse_conn_params | ( | char * | params | ) |
parch string with connection parameters and fill struct conn_param_t
configure connection parameters id,slave,slave_id,tcp,pes70.ru,1331 id,master,tcp,1.2.3.4,1331 id,slave,slave_id,tcp,::1,1331 id,master,rs,/dev/ttyUSB1,57600[,CD,DTR,DSR,CTS,RTS]
int pid_exist | ( | pid_t | pid | ) |
check existance process(thread) with pid
pid | process/thread ID as getpid/gettid |
int press_enter | ( | char * | msg, |
char * | cmp_str | ||
) |
const char * ps_error_text | ( | int | error | ) |
void ps_meminfo | ( | long * | page_sz, |
long * | shmem_sz, | ||
long * | data_sz, | ||
long * | stack_sz | ||
) |
dont wotrk properly
long ps_pagesize | ( | void | ) |
int ps_put_mem | ( | char * | file_name, |
void * | mem_ptr, | ||
int | mem_sz | ||
) |
put memory to file
f_n | ptr to file name |
mem | ptr to memory area |
mem_sz | area size return 0 success -1 error |
void read_off_memory_status | ( | statm_t * | result | ) |
void restore_stdin_cr | ( | void | ) |
void set_fd_cloexec | ( | int | fd | ) |
void set_restore_stdin_cr | ( | void | ) |
int set_reuseaddr | ( | int | fd | ) |
void set_serial_stat | ( | int | fd, |
int | serial | ||
) |
void set_serial_stat_ln | ( | int | fd, |
int | ln_bt, | ||
int | val | ||
) |
int set_stdin_nowait_cr | ( | int | nowait | ) |
IF nowait THEN disable wait 'CR' ELSE restore old settings
void set_termination_action | ( | void | ) |
void setnonblocking | ( | int | sock | ) |
void show_ai_list | ( | struct addrinfo * | ai_list | ) |
void show_sa | ( | struct sockaddr * | sa, |
char * | buf, | ||
int | buf_sz | ||
) |
sa | ptr to sockaddr |
buf | must be enough for addr:port for IPv4/IPv6 |
buf_sz | size of buf |
void show_serial_stat | ( | int | fd | ) |
char ** split_string | ( | char * | str, |
char | delim, | ||
int * | number | ||
) |
split string into words by delimeter, heading/trailing WhiteSpaces prohibited, possible empty words s = " ss,bb,cc,zz,,qq\0" ps_split_str(s, ',', 5, &cnt); ->{"ss", "bb", "cc", "zz", "", 0} ",ss," -> {"", "ss", "", 0}
function change str caller MUST free return ptr
size_t strlcpy | ( | char * | dest, |
const char * | src, | ||
size_t | dest_size | ||
) |
struct addrinfo * tcp_get_addr_info | ( | char * | addr, |
char * | service, | ||
int | family, | ||
int | slave, | ||
int * | err | ||
) |
as default use port 502
void termination_handler | ( | int | signum | ) |
void termination_sigaction | ( | int | signum, |
siginfo_t * | siginfo, | ||
void * | param | ||
) |
int test_bind | ( | struct addrinfo * | ai_ptr, |
int * | err | ||
) |
int test_connect | ( | struct addrinfo * | ai_ptr, |
int * | err | ||
) |
char * time64_to_str | ( | uint64_t | tm, |
char * | buf | ||
) |
convert uint64_t timestump to time string
tm | IN - timestump in nanosec |
buf | IN - buffer for date string, If NULL - malloc it |
int validate_ip_addr | ( | const char * | ip_addr | ) |
check is string is IPv4 or IPv6 or smth else
Get string with IP address
int wait_read_fd | ( | int | fd, |
uint64_t | timeout, | ||
int * | err | ||
) |
wait ready for read
int wait_write_fd | ( | int | fd, |
uint64_t | timeout, | ||
int * | err | ||
) |
|
static |
get list of addrs parameters for pair address/port
addr | - ptr to ip_r/ip_dr_v6/FQDN. NULL - any (used in server/slave) |
service | - port as string/service name. if NULL - default 502 |
family | - set address family to look. AF_INET, AF_INET6, AF_UNSPEC (for both) |
For server addr must be NULL, then will be setted flag ai_hints.ai_flags |= AI_PASSIVE; IF ADDR == NULL that means return addresses available to bind().
ai_hints.ai_flags |= AI_ADDRCONFIG; //exclude not existing protocol (IPv4/IPv6) from addr list Else return addresses avaiilable to connect()
for free result use freeaddrinfo(GAI_ai_list);
|
static |
|
static |
|
static |
Attributes around Ctrl-C called func.
To call ''typedef void (*el_ctrlc_call_t)(void *ar);'' func at Ctrl-C: Set Ctrl-C-called func param by ''void el_set_ctrlc_param(void *arg)'' Set Ctrl-C-called func by ''void el_set_ctrlc_call(el_ctrlc_call_t arg)'' Press Ctrl-C
To change behaviour of Ctrl-C use ''void el_set_ctrlc_exit(int ex);''. If (ex != 0) then on Ctrl-C exit; else not exit.
|
static |
|
static |
|
static |
|
static |
< bswap_16(x);bswap_32(x);bswap_64(x);
|
static |
|
static |