-
Notifications
You must be signed in to change notification settings - Fork 0
/
iMonitor.c
529 lines (442 loc) · 12.8 KB
/
iMonitor.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
This is the iMonitor thread, which should be the main thread now.
It is a NMS Device Manager, with hot plug-in&outs.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include "dm.h"
/* FIX ME */
#include "mod-common.h"
#include "dm-base.h"
/* we'll scan all the files in the /dev/ dir, related with the keywords
below, and try to allocate every effective file node to a module. */
/* this MUST be modified when 'dev_usage' is changed. */
#define SUPPORTED_DEVICE_PREFIX_N 1
static DEV_USAGE dev_usage[SUPPORTED_DEVICE_PREFIX_N] = {
{ "ttyUSB", 0, 20},
// { "ttys", 0, 1}
};
/* this one now is public for some of the codes, e.g. dm-uid.c */
DEV_LIST dev_list;
int dev_prefix_find_index(char *prefix);
int mark_related_device_in_dev_usage(RELATED_DEV *r_dev, int value);
void dev_usage_init(void);
void dev_usage_clear_before_check(void);
int dev_list_init(void);
int find_index_in_dev_usage(char *keyword);
int clear_in_use_flag_for_device(int i);
int unregister_device(int i);
int find_empty_entry(void);
int register_device(char *name, RELATED_DEV *rdev, IDEV_TYPE type);
int add_new_unknown_device(int i, int j);
int find_device_file_in_use(int i, int j);
int remove_untached_device(int i, int j);
int device_check(void);
void show_all_active_devices(void);
int main(int argc, char *argv[]);
/* find index of the prefix in dev_usage struct */
int dev_prefix_find_index(char *prefix)
{
int i;
for (i = 0; i < SUPPORTED_DEVICE_PREFIX_N; i++)
if (!strcmp(prefix, dev_usage[i].keyword))
return i;
/* not found */
return -1;
}
int mark_related_device_in_dev_usage(RELATED_DEV *r_dev, int value)
{
int ibase, j;
if (value != 0 && value != 1)
return -1;
ibase = find_index_in_dev_usage(r_dev->prefix);
if (ibase < 0)
return -2;
for (j = r_dev->start_num; j <= r_dev->end_num; j++)
dev_usage[ibase].in_use[j] = value;
return 0;
}
/* set all the devices to 'not_in_use' */
void dev_usage_init(void)
{
int i;
for (i = 0; i < SUPPORTED_DEVICE_PREFIX_N; i++) {
bzero(dev_usage[i].in_use, sizeof(dev_usage[i].in_use));
bzero(dev_usage[i].checked, sizeof(dev_usage[i].checked));
bzero(dev_usage[i].type, sizeof(dev_usage[i].type));
/* set all enabled as default. */
memset(dev_usage[i].enabled, sizeof(dev_usage[i].enabled), 0x01);
}
}
void dev_usage_clear_before_check(void)
{
int i;
for (i = 0; i < SUPPORTED_DEVICE_PREFIX_N; i++) {
bzero(dev_usage[i].checked, sizeof(dev_usage[i].checked));
bzero(dev_usage[i].type, sizeof(dev_usage[i].type));
}
}
/* the monitor should do the init before doing anything */
int dev_list_init(void)
{
bzero(&dev_list, sizeof(dev_list));
return 0;
}
int find_index_in_dev_usage(char *keyword)
{
int i;
for (i = 0; i < SUPPORTED_DEVICE_PREFIX_N; i++)
if (!strcmp(keyword, dev_usage[i].keyword))
return i;
return -1;
}
/* remove all the 'in_use' flag in dev_usage which is related to device i */
int clear_in_use_flag_for_device(int i)
{
char buf[DEV_FILE_NAME_LEN];
int ibase, j;
IDEV_DEV_FILE *dev_file = &dev_list.dev[i].idev->dev_file;
RELATED_DEV *r_dev = &dev_file->related_device;
/* first, remove base device */
split_dev_filename(dev_file->base_device, buf, &j);
ibase = find_index_in_dev_usage(buf);
if (ibase < 0)
return -1;
dev_usage[ibase].in_use[j] = 0;
/* second, remove related devices */
mark_related_device_in_dev_usage(r_dev, 0);
return 0;
}
/* if a device is dead, use this function to unregister from
device list. */
int unregister_device(int i)
{
IDEV *p;
if (dev_list.dev[i].active == 0)
return -1;
/* 1. clear flags */
if (clear_in_use_flag_for_device(i))
return -2;
p = dev_list.dev[i].idev;
/* I don't know if it is ok or not. */
/* TOFIX: how to free a structure with mutex ? */
lock_device(p);
idev_lock(p);
idev_unlock(p);
unlock_device(p);
/* 2. shut down daemon thread */
if (pthread_cancel(dev_list.dev[i].idev->thread_id))
dm_log(NULL, "\n[VITAL ERROR] thread cancel failed. going on.\n");
/* 3. remove device structure */
dev_list.dev_total--;
dev_list.dev[i].active = 0;
dev_list.dev[i].during_test = 0;
idev_release(p);
dev_list.dev[i].idev = NULL;
dm_log(NULL, "REMOVED device index %d.", i);
/* done */
return 0;
}
/* find the first empty device entry, return index. */
int find_empty_entry(void)
{
int i;
for (i = 0; i < MAX_DEVICE_NO; i++)
if (dev_list.dev[i].active == 0)
return i;
return -1;
}
/* use this function to register a device */
/* there are 3 steps here :
1. set related 'in_use' flag in dev_usage
2. create related data structure of the device
3. create daemon thread for the device
(step 1 is done before calling register) */
int register_device(char *name, RELATED_DEV *rdev, IDEV_TYPE type)
{
IDEV *p_idev;
int i;
/* step 2 */
/* check if there is enough room */
if (dev_list.dev_total >= MAX_DEVICE_NO)
return -1;
i = find_empty_entry();
if (i >= 0) {
/* temprarily, we only support LC6311 module */
p_idev = idev_init(name, rdev, type);
if (p_idev) {
/* idev_init is also ok, then update device list. */
dev_list.dev[i].active = 1;
dev_list.dev[i].idev = p_idev;
dev_list.dev_total++;
} else {
/* idev_init failed */
return -2;
}
}
/* FIX ME */
/* temporarily, enable module as soon as possible. */
idev_set_enable(p_idev);
/* step 3 */
/* create daemon thread for the device. */
if (pthread_create(&p_idev->thread_id, NULL, device_daemon, p_idev)) {
/* thread creation error */
dm_log(NULL, "create thread for device [%d][%s] error.", i, name);
/* free the device structure */
idev_release(p_idev);
return -3;
}
/* all ok. */
return i;
}
/* find device file whose prefix is dev_usage[i].keyword,
index is j. return index in the device list, or -1 if not found */
int find_device_file_in_use(int i, int j)
{
char buf[DEV_FILE_NAME_LEN];
char *keyword = dev_usage[i].keyword;
snprintf(buf, DEV_FILE_NAME_LEN, "%s%d", keyword, j);
for (i = 0; i < MAX_DEVICE_NO; i++) {
if (dev_list.dev[i].active) {
/* a active device */
IDEV_DEV_FILE *dev_f = &dev_list.dev[i].idev->dev_file;
RELATED_DEV *r_dev = &dev_f->related_device;
/* if the base device file match? */
if (!strcmp(dev_f->base_device, buf))
return i;
/* if the related devices match? */
if ((!strcmp(r_dev->prefix, keyword)) &&
j >= r_dev->start_num && j <= r_dev->end_num)
return i;
}
}
/* not found */
return -1;
}
int remove_untached_device(int i, int j)
{
int n;
IDEV *p;
/* find which device it belongs */
n = find_device_file_in_use(i, j);
if (n == -1) /* not found */
return -1;
p = dev_list.dev[n].idev;
/* added 3-26:
before we try to obtain the device lock, we have to tell
everyone using the device, that "THE DEVICE IS DYING!!"*/
idev_set_status(p, UNTACHED);
while (idev_get_status(p) != DEAD)
/* wait the response from daemon thread */
sleep(1);
/* unregister device from list */
if (unregister_device(n))
return -2;
/* remove that device, and it's related devices */
return 0;
}
void clear_dead_modules(void)
{
int i;
for (i = 0; i < MAX_DEVICE_NO; i++) {
IDEV *p = dev_list.dev[i].idev;
if (dev_list.dev[i].active && p->status == DEAD) {
dm_log(NULL, "device[%d] %s type %s is dead, freeing the device.",
i, p->name, dev_model[p->type].name);
unregister_device(i);
}
}
}
IDEV_TYPE get_device_type(int i, int j)
{
int k;
char dev_path[256];
snprintf(dev_path, 256, "%s%d", dev_usage[i].keyword, j);
for (k = 0; k < SUPPORTED_DEVICES; k++)
if (dev_model[k].check_device_file(dev_path))
return (IDEV_TYPE)k;
return UNKNOWN;
}
void format_one_dev_file_str(char *str, unsigned char *type, IDEV_TYPE n)
{
int i;
for (i = 0; i < MAX_DEVICES_NUM_OF_SAME_PREFIX; i++) {
char c;
if (type[i] == n)
c = '1';
else
c = '0';
str[i] = c;
}
str[i] = 0x00;
}
/* check if there is new device to register, or dead to remove */
/* there are 2 kinds of devices to remove :
1. the pidev->status == DEAD (which means that the module doesn't work)
2. device file has gone (maybe the device is disconnected)
we have to take all these possibilities into consideration.
*/
int device_check(void)
{
int i, j;
/* clear all 'checked' sign */
dev_usage_clear_before_check();
/* update 'checked' */
for (i = 0; i < SUPPORTED_DEVICE_PREFIX_N; i++) {
for (j = dev_usage[i].start_num; j <= dev_usage[i].end_num; j++) {
char dev_file[256];
snprintf(dev_file, 256, "%s%s%d", DEV_DIR, dev_usage[i].keyword, j);
/* check file existance */
if (!access(dev_file, F_OK))
dev_usage[i].checked[j] = 1;
}
}
for (i = 0; i < SUPPORTED_DEVICE_PREFIX_N; i++) {
for (j = dev_usage[i].start_num; j <= dev_usage[i].end_num; j++) {
if (dev_usage[i].in_use[j] == 0 && dev_usage[i].checked[j] == 1) {
/* a new device file */
IDEV_TYPE type;
type = get_device_type(i, j);
dev_usage[i].type[j] = type;
dm_log(NULL, "%s%d[%s] found.", dev_usage[i].keyword, j,
dev_model[type].name);
} else if (dev_usage[i].in_use[j] == 1
&& dev_usage[i].checked[j] == 0) {
int ret;
dm_log(NULL, "%s%d removed.", dev_usage[i].keyword, j);
/* a model in use is untached, try to unreg it */
if ((ret = remove_untached_device(i, j)) < 0) {
/* remove error */
dm_log(NULL, "ERROR : removing device %s%d failed.[%d]",
dev_usage[i].keyword, j, ret);
} else { /* remove ok */
dm_log(NULL, "DETECT REMOVE device %s%d index %d.",
dev_usage[i].keyword, j, ret);
}
}
}
}
/* type of new discovered devices are kept in {dev_usage[*].type} */
for (i = 0; i < SUPPORTED_DEVICES; i++) {
int add_device;
IDEV_DEV_FILE dev_file;
char dev_file_str[MAX_DEVICES_NUM_OF_SAME_PREFIX+1];
unsigned char dev_taken[MAX_DEVICES_NUM_OF_SAME_PREFIX+1];
bzero(dev_taken, sizeof(dev_taken));
/* here, we statically only check dev_usage[0], whose keyword
is "ttyUSB" */
format_one_dev_file_str(dev_file_str, dev_usage[0].type, (IDEV_TYPE)i);
add_device = (int)dev_model[i].device_file_adoptation(dev_file_str,
dev_taken, &dev_file);
if (add_device == 1) {
int ret;
/* here is step 1 of register_device,
set all taken devices to 'in_use' */
for (j = 0; j < MAX_DEVICES_NUM_OF_SAME_PREFIX; j++)
if (dev_taken[j])
dev_usage[0].in_use[j] = 1;
ret = register_device(dev_file.base_device,
&dev_file.related_device, (IDEV_TYPE)i);
if (ret >= 0 && ret <= MAX_DEVICE_NO)
dm_log(NULL, "ADD device(%d) %s as %s module.",
ret, dev_file.base_device, dev_model[i].name);
else
dm_log(NULL, "ERROR add device %s as %s module. ret[%d].",
dev_file.base_device, dev_model[i].name, ret);
}
}
/* finally, let's see if there is any module delared dead */
clear_dead_modules();
return 0;
}
void show_all_active_devices(void)
{
int i;
if (dev_list.dev_total == 0)
return;
dm_log(NULL, "=================");
for (i = 0; i < MAX_DEVICE_NO; i++)
if (dev_list.dev[i].active)
show_device_info(dev_list.dev[i].idev);
dm_log(NULL, "=================");
}
/* including all the test cases */
#include "test-case.h"
// #define SIMGLE_THREAD_SINGLE_MODEM 1
// #define MULTI_THERAD_SINGLE_MODEM 1
#define MULTI_THREAD_MULTI_MODEM 1
/*************************************************/
#if MULTI_THREAD_MULTI_MODEM
/*************************************************/
int main(int argc, char *argv[])
{
pthread_t main_thread;
/* init device list */
dev_list_init();
/* set all devices to 'not_in_use', which is 0 */
dev_usage_init();
/* start testing thread */
pthread_create(&main_thread, NULL, multi_modem_testing, NULL);
while(1)
{
// dm_log(NULL, "start to check device ...");
device_check();
sleep(1);
}
return 0;
}
/*************************************************/
#elif MULTI_THERAD_SINGLE_MODEM
/*************************************************/
/* test single modem with multiple threads. */
int main(int argc, char *argv[])
{
pthread_t main_thread;
/* init device list */
dev_list_init();
/* set all devices to 'not_in_use', which is 0 */
dev_usage_init();
/* start testing thread */
// pthread_create(&main_thread, NULL, single_modem_testing, NULL);
while(1)
{
dm_log(NULL, "start to check device ...");
device_check();
sleep(1);
}
return 0;
}
/*************************************************/
#elif SINGLE_THREAD_SINGLE_MODEM/* DOING SINGLE TEST */
/*************************************************/
/* test a single module functionality, without the monitoring of iMon */
void self_test(IDEV *p)
{
p->forward(p, "13811362029", 0);
while (1) sleep(1);
return;
}
int main(int argc, char *argv[])
{
RELATED_DEV rdev;
IDEV *p;
rdev.prefix[0] = 0x00;
rdev.start_num = 1;
rdev.end_num = 0;
register_device("ttyUSB0", &rdev, SIM4100);
if (dev_list.dev[0].active == 0) {
dm_log(NULL, "error register device.");
return -1;
}
p = dev_list.dev[0].idev;
while (idev_get_status(p) != READY)
sleep(1);
self_test(p);
return 0;
}
#endif