-
Notifications
You must be signed in to change notification settings - Fork 0
/
deviceManager.c
281 lines (267 loc) · 6.97 KB
/
deviceManager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* =====================================================================================
*
* Filename: main.c
*
* Description: none
*
* Version: 1.0
* Created: 2014年11月10日 14时45分07秒
* Revision: none
* Compiler: gcc
*
* Author: dongwencai
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
#include <string.h>
#include <pthread.h>
#include <sys/msg.h>
#include "list.h"
#include "stringex.h"
#include "deviceManager.h"
#include "devmsg.h"
#include "config.h"
#include "event.h"
PLISTINFO device_list = NULL;
static int g_msgid;
static int g_retmsgid = -1;
static int load_config(const char *config,PLISTINFO pDevList);
static device_t register_device_ex(DEVCONTEXT *pContext);
static void *device_thread(void *pContext);
static int nameCompare(void *src,void *dst,int offset,int len);
static int unregister_device_ex(PLIST link);
static int msg_init();
static int msg_loop();
static void *dev_msg_loop(void *p);
static int dev_ioctl(PDEVMSG pMsg);
static int msg_release();
void print_all_divice()
{
PLIST pHead = NULL;
PDEVCONTEXT pContext = NULL;
if(device_list)
{
pthread_mutex_lock(&device_list->lock);
pHead = device_list->phead;
while(pHead)
{
pContext =(PDEVCONTEXT) pHead->data;
puts(pContext->so_name);
pHead = pHead->next;
}
pthread_mutex_unlock(&device_list->lock);
}
}
static int load_config(const char *config,PLISTINFO pDevList)
{
FILE *fp = NULL;
int cnt = 0;
PLIST link = NULL;
char so_name[256];
fp = fopen(config,"r");
if(!fp) return -DEV_FAIL;
while(1)
{
if(fgets(so_name,256,fp))
{
strip_illegal_char(so_name);
if(so_name[0] == '#')
{
continue;
}
if(register_device(so_name) >= 0 && ++ cnt);
}
else
break;
}
fclose(fp);
return cnt;
}
static int msg_release()
{
int ret;
if(g_msgid >=0)
ret = msgctl(g_msgid,IPC_RMID,0);
if(g_retmsgid >=0)
ret |= msgctl(g_retmsgid,IPC_RMID,0);
return ret;
}
static int msg_init()
{
key_t key ;
key = ftok(DEVICEMANAGER,IPCKEY);
g_msgid = msgget(key,0666 | IPC_CREAT);
key = ftok(DEVICERETMSG,IPCKEY);
g_retmsgid = msgget(key,0666 | IPC_CREAT);
return (g_msgid | g_retmsgid);
}
int main(int argc,char *argv[])
{
char *p_config_name = DEVICECONFIG;
if(argc > 1)
{
p_config_name = argv[1];
}
if(access(p_config_name,F_OK|R_OK) != 0)
{
perror("config file not exist or Limited access!\n");
exit(-1);
}
device_list = create_list(sizeof(DEVCONTEXT),nameCompare);
if(device_list && load_config(p_config_name,device_list) >= 0 && msg_init() > 0)
{
//pthread_create_detached(dev_msg_loop,NULL);
msg_loop();
}
msg_release();
perror("config file is invalid!\n");
return 0;
}
device_t register_device(const char *name)
{
PLIST link = NULL;
DEVCONTEXT context;
PDEVCONTEXT pContext;
device_t devHdl = 0;
link = lookup_node(device_list,(void *)name,MEMBER_OFF(DEVCONTEXT,so_name),strlen(name) + 1);
if(!link)
{
strncpy(context.so_name,name,SONAMELEN);
if((devHdl = register_device_ex(&context)) > 0)
{
if(context.device_open && context.device_open() >= 0)
{
link = list_add(device_list,&context);
}
if(link)
{
pContext = (PDEVCONTEXT)link->data;
pthread_create(&pContext->threadHdl,NULL,device_thread,(void *)pContext);
return devHdl;
}
}
}
return 0;
}
static device_t register_device_ex(PDEVCONTEXT pContext)
{
void *sohdl = NULL;
int ret = -1;
sohdl = dlopen(pContext->so_name,RTLD_NOW);
if(sohdl)
{
pContext->sohdl = (device_t)sohdl;
pContext->device_open = dlsym(sohdl,"device_open");
pContext->device_getmsg = dlsym(sohdl,"device_getmsg");
pContext->msg_transale = dlsym(sohdl,"msg_transale");
pContext->device_close = dlsym(sohdl,"device_close");
pContext->device_ctl = dlsym(sohdl,"device_ctl");
return (device_t)pContext->sohdl;
}
return 0;
}
int unregister_device(device_t hdl)
{
PLIST plink = NULL;
int ret = -1;
plink = lookup_node(device_list,(void *)&hdl,MEMBER_OFF(DEVCONTEXT,sohdl),sizeof(hdl));
if(plink)
{
ret = unregister_device_ex(plink);
}
return ret;
}
static int unregister_device_ex(PLIST link)
{
PDEVCONTEXT pContext = NULL;
int ret = 0;
pContext = (PDEVCONTEXT)link->data;
if(pContext->device_close)
pContext->device_close();
pthread_cancel(pContext->threadHdl);
pthread_join(pContext->threadHdl,NULL);
ret = dlclose((void *)(pContext->sohdl));
ret |= list_del(device_list,link);
return ret;
}
static void *device_thread(void *pContext)
{
PDEVCONTEXT pDevContext = NULL;
int ret = 0;
void *p = NULL;
msg_t msg;
pDevContext = (DEVCONTEXT *)pContext;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
while(1)
{
p = pDevContext->device_getmsg();
ret = pDevContext->msg_transale(p,&msg);
if(ret >= 0)
{
//将消息传递出去
}
}
return NULL;
}
static int nameCompare(void *src,void *dst,int offset,int len)
{
PDEVCONTEXT pDst = (PDEVCONTEXT)dst;
return memcmp(src,(void *)((unsigned int)pDst+offset),len);
}
static int msg_loop()
{
PDEVMSG pMsg;
int ret;
device_t hdl = 0;
pMsg = (PDEVMSG)malloc(sizeof(DEVMSG)+512);
while(pMsg)
{
ret = msgrcv(g_msgid,pMsg,MSGMAXSIZE,DEVMSGTYPE,0);
if(ret > 0)
{
ret = -1;
switch(pMsg->cmd)
{
case REG_DEV:
hdl = register_device((char *)pMsg->param);
break;
case UNREG_DEV:
ret = unregister_device(pMsg->sohdl);
break;
case DEV_CTRL:
ret = dev_ioctl(pMsg);
default:
break;
}
}
if(pMsg->ret)
{
pMsg->ret = ret;
pMsg->sohdl = hdl;
pMsg->type = pMsg->timing;
msgsnd(g_retmsgid,pMsg,MSGMAXSIZE,0);
}
}
return DEV_SUC;
}
static int dev_ioctl(PDEVMSG pMsg)
{
int ret = -1;
PLIST plink = NULL;
PDEVCONTEXT pContext = NULL;
plink = lookup_node(device_list,(void *)&pMsg->sohdl,MEMBER_OFF(DEVCONTEXT,sohdl),sizeof(pMsg->sohdl));
if(plink)
{
pContext = (PDEVCONTEXT)plink->data;
if(pContext->device_ctl)
ret = pContext->device_ctl(pMsg->cmd,pMsg->param);
}
return ret;
}