elemy-utils 1.0.0
|
#include <stdint.h>
#include <stdbool.h>
#include <byteswap.h>
#include <unistd.h>
#include <time.h>
#include <malloc.h>
#include <linux/limits.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <json-c/json.h>
#include <stdarg.h>
#include <sys/syscall.h>
#include "elutils/pslist.h"
#include "elutils/psl_ndx.h"
#include "elutils/c_decls.h"
Go to the source code of this file.
Data Structures | |
union | _sau_t |
union | _sau_static_t |
struct | _conn_param_t |
struct | statm_t |
Macros | |
#define | _GNU_SOURCE |
STDERR_FILENO. More... | |
#define | NAME_BUF_SZ_MAX (NAME_MAX + 1) |
< NAME_MAX More... | |
#define | UNUSED(x) (void)(x) |
used for masquarade unuzed variables. in production must no be. More... | |
#define | PUT_ERR(ERR, ERRN) if (ERR) *ERR = ERRN |
used in funcs wich return error codes through parameter variable ptr More... | |
#define | ERROR_FD -1 |
for close file More... | |
#define | CLOSE_FD(FD) close(FD); FD = ERROR_FD |
#define | EL_WRONG_PTR (void*)(0) |
#define | NOERROR (0) |
#define | ARRAY_SIZE(foo) (sizeof(foo)/sizeof(foo[0])) |
#define | TENS_KILO 1000LL |
#define | TENS_MEGA 1000000LL |
#define | TENS_GIGA 1000000000LL |
#define | EL_DBG 1 |
#define | MB_DBG(fmt, ...) |
#define | MB_DBG_IMM(fmt, ...) |
#define | MB_DUMP_BUF_SZ 120 |
#define | MB_DUMP_BUF(DATA_PTR, DATA_SZ, fmt, ...) |
#define | MB_DEBUG(DBG_FLAG, FMT, ...) |
#define | NO_HAVE_STRLCPY |
#define | MALLOC_ARG(ARG_IS, ERR) if (ARG_IS == NULL) {ARG_IS = malloc(sizeof(*ARG_IS)); if (ARG_IS == NULL) { if (ERR != NULL) { *ERR = errno; } return NULL; } } |
#define | SET_CD(FD, VAL) set_serial_stat_ln(FD, TIOCM_CD, VAL) |
#define | SET_DTR(FD, VAL) set_serial_stat_ln(FD, TIOCM_DTR, VAL) |
#define | SET_DSR(FD, VAL) set_serial_stat_ln(FD, TIOCM_LE, VAL) |
#define | SET_RTS(FD, VAL) set_serial_stat_ln(FD, TIOCM_RTS, VAL) |
#define | SET_CTS(FD, VAL) set_serial_stat_ln(FD, TIOCM_CTS, VAL) |
#define | ARGP_KEY_ARG 0 |
#define | ARGP_KEY_ARGS 0x1000006 |
#define | ARGP_KEY_END 0x1000001 |
#define | ARGP_KEY_NO_ARGS 0x1000002 |
#define | ARGP_KEY_INIT 0x1000003 |
#define | ARGP_KEY_FINI 0x1000007 |
#define | ARGP_KEY_SUCCESS 0x1000004 |
#define | ARGP_KEY_ERROR 0x1000005 |
#define | DATA_STR_SZ (64) |
#define | ARGP_FIRST_LONG_OPT (0x100) |
key for first only long option More... | |
#define | ARGP_PARAM_NOMEM(PCFG, VAR, MSG) |
#define | COMPARE_MEM_PREPARE(MEMAS) el_compare_mem_prepare(MEMAS, sizeof(MEMAS)) |
#define | COMPARE_MEM_DO(MEMAS, COPY) el_compare_mem_do(MEMAS, sizeof(*MEMAS), COPY) |
#define | MB_MEMORY_STATUS() |
#define | CHECK_GET_OBJ_STRING(FROM, NAME, OBJ, TARGET, FIELD, EXIT) |
strdup string from json_type_object FROM by name NAME into FIELD of TARGET struct. on error goto EXIT label More... | |
#define | CHECK_GET_OBJ_STRING_NULL(FROM, NAME, OBJ, TARGET, FIELD, EXIT) |
strdup string from json_type_object FROM by name NAME into FIELD of TARGET struct. on error goto EXIT label. Absent NAME is NOT ERROR More... | |
#define | CHECK_GET_OBJ_INT(FROM, NAME, OBJ, TARGET, FIELD, EXIT) |
#define | BUILD_MSG_WITH_PREV(PREV, SHOW, FORMAT, ...) |
Typedefs | |
typedef void(* | el_ctrlc_call_t) (void *ar) |
typedef enum _IP_TYPES_T | IP_TYPES_T |
typedef union _sau_t | sau_t |
typedef union _sau_static_t | sau_static_t |
typedef enum _CONN_ROLE_T | CONN_ROLE_T |
configure connection parameters More... | |
typedef enum _CONN_TYPE_T | CONN_TYPE_T |
typedef struct _conn_param_t | conn_params_t |
Enumerations | |
enum | _IP_TYPES_T { IT_NOT_IP = AF_UNSPEC , IT_IPV4 = AF_INET , IT_IPV6 = AF_INET6 } |
enum | _CONN_ROLE_T { CR_NOTHING = 0 , CR_MASTER , CR_SLAVE , CR_GATE , CR_NODE } |
configure connection parameters More... | |
enum | _CONN_TYPE_T { CT_TCP = 0 , CT_RTU } |
Functions | |
static uint64_t | timespec_diff (struct timespec *ts1, struct timespec *ts2) |
static uint64_t | get_time_nano (void) |
size_t | strlcpy (char *dst, const char *src, size_t dstSize) |
pthread_rwlockattr_t * | el_get_rw_shared_attr (void) |
int | set_stdin_nowait_cr (int nowait) |
void | set_restore_stdin_cr (void) |
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_handler (int signum) |
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) |
void | show_serial_stat (int fd) |
void | set_serial_stat_ln (int fd, int ln_bt, int val) |
char ** | split_string (char *str, char delem, 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 | 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,...) |
Include standart uintXX_t Define declaration decorators Decleare time measer define debug messanger and data dumper
Created on: 11 мар. 2023 г. Author: ps
#define _GNU_SOURCE |
STDERR_FILENO.
uint*_t for 'bool' type bswap_16(x);bswap_32(x);bswap_64(x);
#define ARGP_FIRST_LONG_OPT (0x100) |
key for first only long option
For argp
#define ARGP_KEY_ARG 0 |
#define ARGP_KEY_ARGS 0x1000006 |
#define ARGP_KEY_END 0x1000001 |
#define ARGP_KEY_ERROR 0x1000005 |
#define ARGP_KEY_FINI 0x1000007 |
#define ARGP_KEY_INIT 0x1000003 |
#define ARGP_KEY_NO_ARGS 0x1000002 |
#define ARGP_KEY_SUCCESS 0x1000004 |
#define ARGP_PARAM_NOMEM | ( | PCFG, | |
VAR, | |||
MSG | |||
) |
for use in parsers. check var (usually arg_val or malloced var) on NULL if NULL - set in PCFG config struct err = 1 and return ARGP_ERR_UKNOWN as dont understand argument
#define ARRAY_SIZE | ( | foo | ) | (sizeof(foo)/sizeof(foo[0])) |
#define BUILD_MSG_WITH_PREV | ( | PREV, | |
SHOW, | |||
FORMAT, | |||
... | |||
) |
#define CHECK_GET_OBJ_INT | ( | FROM, | |
NAME, | |||
OBJ, | |||
TARGET, | |||
FIELD, | |||
EXIT | |||
) |
#define CHECK_GET_OBJ_STRING | ( | FROM, | |
NAME, | |||
OBJ, | |||
TARGET, | |||
FIELD, | |||
EXIT | |||
) |
strdup string from json_type_object FROM by name NAME into FIELD of TARGET struct. on error goto EXIT label
FROM | ptr to json_type_object |
NAME | name of object field |
OBJ | name for temporary json object |
#define CHECK_GET_OBJ_STRING_NULL | ( | FROM, | |
NAME, | |||
OBJ, | |||
TARGET, | |||
FIELD, | |||
EXIT | |||
) |
strdup string from json_type_object FROM by name NAME into FIELD of TARGET struct. on error goto EXIT label. Absent NAME is NOT ERROR
FROM | ptr to json_type_object |
NAME | name of object field |
OBJ | name for temporary json object |
ERROR is if NAME exists, but his type isn't json_type_string or can't dup getted string (string value == NULL isn't error);
#define CLOSE_FD | ( | FD | ) | close(FD); FD = ERROR_FD |
#define COMPARE_MEM_DO | ( | MEMAS, | |
COPY | |||
) | el_compare_mem_do(MEMAS, sizeof(*MEMAS), COPY) |
#define COMPARE_MEM_PREPARE | ( | MEMAS | ) | el_compare_mem_prepare(MEMAS, sizeof(MEMAS)) |
#define DATA_STR_SZ (64) |
#define EL_DBG 1 |
#define EL_WRONG_PTR (void*)(0) |
#define ERROR_FD -1 |
for close file
#define MALLOC_ARG | ( | ARG_IS, | |
ERR | |||
) | if (ARG_IS == NULL) {ARG_IS = malloc(sizeof(*ARG_IS)); if (ARG_IS == NULL) { if (ERR != NULL) { *ERR = errno; } return NULL; } } |
#define MB_DBG | ( | fmt, | |
... | |||
) |
#define MB_DBG_IMM | ( | fmt, | |
... | |||
) |
#define MB_DEBUG | ( | DBG_FLAG, | |
FMT, | |||
... | |||
) |
#define MB_DUMP_BUF | ( | DATA_PTR, | |
DATA_SZ, | |||
fmt, | |||
... | |||
) |
#define MB_DUMP_BUF_SZ 120 |
#define MB_MEMORY_STATUS | ( | ) |
#define NAME_BUF_SZ_MAX (NAME_MAX + 1) |
< NAME_MAX
prepared for possible using in build for MIPS __BEGIN_DECLS/__END_DECLS define in sys/cdefs.h but on MIPSEL24KC it absent. thats why I remove including it. But put defining here. NAME_MAX (255) + 1 (zero). for allocate buffers for filename (static/dynamic)
#define NO_HAVE_STRLCPY |
#define NOERROR (0) |
#define PUT_ERR | ( | ERR, | |
ERRN | |||
) | if (ERR) *ERR = ERRN |
used in funcs wich return error codes through parameter variable ptr
#define SET_CD | ( | FD, | |
VAL | |||
) | set_serial_stat_ln(FD, TIOCM_CD, VAL) |
#define SET_CTS | ( | FD, | |
VAL | |||
) | set_serial_stat_ln(FD, TIOCM_CTS, VAL) |
#define SET_DSR | ( | FD, | |
VAL | |||
) | set_serial_stat_ln(FD, TIOCM_LE, VAL) |
#define SET_DTR | ( | FD, | |
VAL | |||
) | set_serial_stat_ln(FD, TIOCM_DTR, VAL) |
#define SET_RTS | ( | FD, | |
VAL | |||
) | set_serial_stat_ln(FD, TIOCM_RTS, VAL) |
#define TENS_GIGA 1000000000LL |
#define TENS_KILO 1000LL |
#define TENS_MEGA 1000000LL |
#define UNUSED | ( | x | ) | (void)(x) |
used for masquarade unuzed variables. in production must no be.
typedef struct _conn_param_t conn_params_t |
typedef enum _CONN_ROLE_T CONN_ROLE_T |
configure connection parameters
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]
typedef enum _CONN_TYPE_T CONN_TYPE_T |
typedef void(* el_ctrlc_call_t) (void *ar) |
typedef enum _IP_TYPES_T IP_TYPES_T |
typedef union _sau_static_t sau_static_t |
enum _CONN_ROLE_T |
enum _CONN_TYPE_T |
enum _IP_TYPES_T |
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).
pthread_rwlockattr_t * el_get_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_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 | ) |
|
inlinestatic |
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 | ) |
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 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 | ) |
toggle wariants IF nowait THEN toggled disable wait cr ELSE restore original settings
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 * | dst, |
const char * | src, | ||
size_t | dstSize | ||
) |
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 |
|
inlinestatic |
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 | ||
) |