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