-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm-uid.c
150 lines (134 loc) · 2.69 KB
/
dm-uid.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
/*
* file : dm-uid.c
* desc : all the UID related codes are here
* mail : xzpeter@gmail.com
*/
#include <string.h>
#include "dm-idev.h"
/* we HAVE TO include this to finish our task */
#include "dm-base.h"
/* return 0 if pu1 == pu2 */
int uid_compare(PUID pu1, PUID pu2)
{
if (pu1->type != pu2->type)
return -1;
if (strncmp(pu1->s, pu2->s, UID_STR_LEN_MAX))
return -2;
return 0;
}
/* copy pu2 -> pu1 */
int uid_copy(PUID pu1, PUID pu2)
{
if (pu1 == NULL || pu2 == NULL)
return -1;
memcpy(pu1, pu2, sizeof(UID));
return 0;
}
/* clear uid */
int uid_clear(PUID pu)
{
if (pu == NULL)
return -1;
bzero(pu, sizeof(UID));
return 0;
}
/* to seek for a specific device by simcard
* return NULL if not found, or return the PUID pointer */
PUID uid_seek_by_keystr(char *sim)
{
int i;
IDEV *p;
PUID pu;
for (i = 0; i < MAX_DEVICE_NO; i++) {
p = dev_list.dev[i].idev;
pu = &p->uid;
if (strncmp(pu->s, sim, UID_STR_LEN_MAX))
continue;
/* find the device */
return pu;
}
return NULL;
}
/* seek UID, return IDEV pointer if exist, else NULL. */
IDEV * uid_get_idev(PUID pu)
{
int i;
IDEV *p;
PUID pu2;
for (i = 0; i < MAX_DEVICE_NO; i++) {
p = dev_list.dev[i].idev;
pu2 = &p->uid;
if (uid_compare(pu, pu2))
continue;
/* find the device */
return p;
}
return NULL;
}
/* to seek for a specific device by UID
* return 0 if not found. else 1 */
int uid_seek(PUID pu)
{
return (uid_get_idev(pu) ? 1 : 0);
}
/* fill in a UID structure, from 'type' and 'str' keyword */
int uid_fill(PUID pu, char type, char *s)
{
if (pu == NULL)
return -1;
pu->type = type;
if (strlen(s) == 0)
return -2;
strncpy(pu->s, s, UID_STR_LEN_MAX);
return 0;
}
/* return type if PUID is valid, or return -1 if err */
char uid_get_type(PUID pu)
{
if (pu == NULL)
return -1;
return pu->type;
}
/* return pointer to key str(s) of UID if PUID is valid, or return NULL */
char *uid_get_s(PUID pu)
{
if (pu == NULL)
return NULL;
return pu->s;
}
/* check if the device is available by indexing UID
* return 1 if valid, or 0.*/
int uid_is_valid(PUID pu)
{
IDEV *p = uid_get_idev(pu);
if (p == NULL)
return 0;
if (idev_is_sick(p))
return 0;
return 1;
}
/* check if device with PUID is a xxx */
static int uid_is_a_xxx(PUID pu, IDEV_GROUP group)
{
/* first, find the device */
IDEV *p = uid_get_idev(pu);
/* not found */
if (p == NULL)
return 0;
/* the device is not healthy ? */
if (idev_is_sick(p))
return 0;
if (idev_get_group(p) & group)
return 1;
return 0;
}
/* check if device with UID can be a COMMUNICATOR */
int uid_is_a_comm(PUID pu)
{
return uid_is_a_xxx(pu, COMM);
}
/* check if device with UID can be a TESTEE */
int uid_is_a_testee(PUID pu)
{
return uid_is_a_xxx(pu, TESTEE);
}