VCOM Linux Driver
Advantech Inc.
Loading...
Searching...
No Matches
vcom_monitor.h
Go to the documentation of this file.
1
2#ifndef _VCOM_MONITOR_H
3#define _VCOM_MONITOR_H
4#define MSIZE 128
5#define FNAME_LEN 256
6extern void * stk_mon;
7
8struct vc_monitor{
9 void * addr;
10 int fd;
11 int pid;
12 int msgl;
14};
16
17static inline int mon_init(char * fname)
18{
19 vc_mon.fd = -1;
20 vc_mon.addr = 0;
21
22 if(fname <= 0)
23 return 0;
24
25 vc_mon.pid = getpid();
26 snprintf(vc_mon.fname, FNAME_LEN, "%s", fname);
27
28 vc_mon.fd = open(vc_mon.fname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXO|S_IRWXG|S_IRWXU);
29 if(vc_mon.fd < 0){
30 printf("create log fail...\n");
31 return -1;
32 }
33
34 ftruncate(vc_mon.fd, MSIZE);
35 vc_mon.addr = mmap(0, MSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, vc_mon.fd, 0);
36 if(vc_mon.addr == MAP_FAILED){
37 printf("mmap fail\n");
38 munmap(vc_mon.addr, MSIZE);
39 close(vc_mon.fd);
40 vc_mon.fd = 0;
41 return -1;
42 }
43
44 stk_mon = &vc_mon;
45 return 0;
46}
47
48static inline int mon_update(struct stk_vc * stk, int sig, char * dbg)
49{
50 char * ptr;
51 char * mem;
52 char * msg;
53 int msgl;
54 int len;
55
56 if(vc_mon.fd < 0){
57 return 0;
58 }
59
60 if(stk_empty(stk)){
61 printf("stack empty ...\n");
62 return -1;
63 }
64
65 msg = stk_curnt(stk)->name();
66 mem = (char *)vc_mon.addr;
67 msgl = snprintf(mem, MSIZE, "Pid : %d | State : %s ",
68 vc_mon.pid, msg);
69
70 len = MSIZE - msgl;
71
72 if(len <= 1){
73 printf("%s len <= 1\n", __func__);
74 return -1;
75 }
76
77 ptr = mem + msgl;
78 if(dbg != 0)
79 msgl += snprintf(ptr, len, "| E : %s \n", dbg);
80
81 len = MSIZE - msgl;
82 ptr = mem + msgl;
83 memset(ptr + 1, 0, len - 1);
84
85 if(sig){
86 msync(mem, MSIZE, MS_SYNC);
87 ftruncate(vc_mon.fd, MSIZE);
88 }else{
89 msync(mem, MSIZE, MS_ASYNC);
90 }
91
92 return 0;
93}
94
95#define muc2(a, b) \
96 do{if(stk_mon) \
97 mon_update(a, b, 0); \
98 }while(0)
99
100
101#define muc3(a, b, c) \
102 do{if(stk_mon) \
103 mon_update(a, b, c); \
104 }while(0)
105
106#define muc_ovrld(_1, _2, _3, func, ...) func
107#define mon_update_check(args...) muc_ovrld(args, muc3, muc2,...)(args)
108
109#endif
Definition vcom.h:25
Definition vcom_monitor.h:8
int msgl
Definition vcom_monitor.h:12
char fname[FNAME_LEN]
Definition vcom_monitor.h:13
int fd
Definition vcom_monitor.h:10
void * addr
Definition vcom_monitor.h:9
int pid
Definition vcom_monitor.h:11
#define mon_init(a)
Definition vcom.h:97
#define stk_empty(a)
Definition vcom.h:23
#define mon_update(...)
Definition vcom.h:96
struct vc_monitor vc_mon
Definition vcom_monitor.h:15
#define FNAME_LEN
Definition vcom_monitor.h:5
#define MSIZE
Definition vcom_monitor.h:4
void * stk_mon
Definition vcom_client.c:33
#define FNAME_LEN
Definition vcom_monitor_pre_stat.h:4
unsigned short len
Definition vcom_proto.h:3
char ptr[4]
Definition vcom_proto.h:2