-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm-idev.c
329 lines (294 loc) · 7.85 KB
/
dm-idev.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
/*
dm-idev.c : device monitor base structure and functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "dm.h"
#include "rbuf.h"
#include "mod-common.h"
#include "lc6311/lc6311.h"
#include "sim4100/sim4100.h"
#include "mc703/mc703.h"
#include "nms.h"
const DEV_MODEL dev_model[SUPPORTED_DEVICES] = {
{
.name = "UNKNOWN",
.open_port = common_open_port,
.close_port = common_close_port,
.check_device_file = common_check_device_file,
.get_related_device = common_get_related_device,
.module_startup = common_module_startup,
.device_file_adoptation = common_device_file_adoptation,
.send = common_send,
.send_sms = common_send_sms,
.forward = common_forward,
.parse_line = common_parse_line,
.start_call = common_start_call,
.stop_call = common_stop_call,
.network_status = common_network_status,
.probe = common_probe,
},
{
.name = "LC6311",
.open_port = common_open_port,
.close_port = common_close_port,
.check_device_file = lc6311_check_device_file,
.get_related_device = lc6311_get_related_device,
.module_startup = lc6311_module_startup,
.device_file_adoptation = lc6311_device_file_adoptation,
.send = common_send,
.send_sms = common_send_sms,
.forward = common_forward,
.parse_line = common_parse_line,
.start_call = common_start_call,
.stop_call = common_stop_call,
.network_status = common_network_status,
.probe = common_probe,
},
{
.name = "SIM4100",
.open_port = common_open_port,
.close_port = common_close_port,
.check_device_file = sim4100_check_device_file,
.get_related_device = common_get_related_device,
.module_startup = sim4100_module_startup,
.device_file_adoptation = sim4100_device_file_adoptation,
.send = common_send,
.send_sms = common_send_sms,
.forward = common_forward,
.parse_line = common_parse_line,
.start_call = common_start_call,
.stop_call = common_stop_call,
.network_status = common_network_status,
.probe = common_probe,
},
{
.name = "MC703",
.open_port = common_open_port,
.close_port = common_close_port,
.check_device_file = mc703_check_device_file,
.get_related_device = mc703_get_related_device,
.module_startup = mc703_module_startup,
.device_file_adoptation = mc703_device_file_adoptation,
.send = common_send,
.send_sms = mc703_send_sms,
.forward = common_forward,
.parse_line = mc703_parse_line,
.start_call = mc703_start_call,
.stop_call = mc703_stop_call,
.network_status = mc703_network_status,
.probe = common_probe,
}
};
/* check if the device is ok or not */
int idev_is_sick(IDEV *p)
{
if (p == NULL)
return -1;
return (idev_get_status(p) >= SICK);
}
/* call this to check if your at cmd has recved */
int check_at_recved(IDEV *p)
{
if (idev_is_sick(p))
return 1; /* force return true */
return (p->at.status == AT_RECVED);
}
/* if you have collected all the data you need in AT_BUF, call it to
tell the daemon. */
void at_recved_done(IDEV *p)
{
idev_lock(p);
p->at.status = AT_READY;
idev_unlock(p);
}
/* check if at buf is ready for your command */
int check_at_ready(IDEV *p)
{
if (idev_is_sick(p))
return 1; /* force return true */
return (p->at.status == AT_READY);
}
/* when you have fulfilled at_sendbuf and keyword as you want, call it
to let daemon know. */
void at_send_it(IDEV *p)
{
idev_lock(p);
p->at.status = AT_REQUEST;
idev_unlock(p);
}
int idev_lock(IDEV *p)
{
return pthread_mutex_lock(&p->mutex);
}
int idev_unlock(IDEV *p)
{
return pthread_mutex_unlock(&p->mutex);
}
int lock_device(IDEV *p)
{
return pthread_mutex_lock(&p->dev_mutex);
}
int unlock_device(IDEV *p)
{
return pthread_mutex_unlock(&p->dev_mutex);
}
/* ONLY IF we do the assignment of the functions here,
can we use these functions in the host thread later on. */
void idev_register_methods(IDEV *p, IDEV_TYPE type)
{
p->open_port = dev_model[type].open_port;
p->close_port = dev_model[type].close_port;
p->module_startup = dev_model[type].module_startup;
p->send = dev_model[type].send;
p->send_sms = dev_model[type].send_sms;
p->forward = dev_model[type].forward;
p->parse_line = dev_model[type].parse_line;
p->start_call = dev_model[type].start_call;
p->stop_call = dev_model[type].stop_call;
p->network_status = dev_model[type].network_status;
p->probe = dev_model[type].probe;
}
#define RBUF_SIZE 4096
static int device_count[SUPPORTED_DEVICES] = {0, 0, 0, 0};
/* init a idevice, the name string is used to identify the device,
type is used to decide which driver the device will use. */
IDEV * idev_init(char *name, RELATED_DEV *rdev, IDEV_TYPE type)
{
int type_n;
IDEV *pidev = NULL;
/* do the malloc */
pidev = malloc(sizeof(IDEV));
if (pidev == NULL)
return NULL;
bzero(pidev, sizeof(IDEV));
/* malloc for rbuf */
pidev->r = rbuf_init(RBUF_SIZE);
if (pidev->r == NULL)
goto free_idev;
if (pidev) {
/* malloc ok, do the initialize */
pidev->enabled = 0;
/* type cannot be changed after init of the idev */
pidev->type = type;
/* default group is NONE */
pidev->group = NONE;
/* default status is INIT */
pidev->status = INIT;
/* default users count is 0 */
pidev->users_count = 0;
/* init mutex */
// pidev->mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_init(&(pidev->mutex), NULL);
pthread_mutex_init(&(pidev->dev_mutex), NULL);
strcpy(pidev->dev_file.base_device, name);
// strcpy(pidev->name, name);
type_n = (int)pidev->type;
snprintf(pidev->name, 32, "%s/%d", dev_model[type_n].name,
device_count[type_n]++);
memcpy(&pidev->dev_file.related_device, rdev, sizeof(RELATED_DEV));
/* register related model functions to this device */
idev_register_methods(pidev, type);
/* clear UID and simcard number */
bzero(&pidev->uid, sizeof(UID));
/* init simcard number by 0000 */
strcpy(pidev->sim, "0000");
}
return pidev;
free_idev:
free(pidev);
return NULL;
}
/* free a idev structure */
void idev_release(IDEV *pidev)
{
pidev->close_port(pidev);
pidev->r = rbuf_release(pidev->r);
if (pthread_mutex_destroy(&pidev->mutex) ||
pthread_mutex_destroy(&pidev->dev_mutex)) {
dm_log(pidev, "VITAL!! mutex is destroyed while some thread "
"is using it!");
}
free(pidev);
}
void idev_user_malloc(IDEV *pidev)
{
idev_lock(pidev);
pidev->users_count++;
dm_log(pidev, "%d USERS.", pidev->users_count);
if (pidev->users_count > MAX_USERS_PER_MODEM) {
dm_log(pidev, "VITAL ERROR!!! USER OVER MAX!!");
exit(0);
}
idev_unlock(pidev);
}
void idev_user_free(IDEV *pidev)
{
idev_lock(pidev);
pidev->users_count--;
dm_log(pidev, "%d USERS.", pidev->users_count);
if (pidev->users_count > MAX_USERS_PER_MODEM) {
dm_log(pidev, "VITAL ERROR!!! USER OVER MAX!!");
exit(0);
}
idev_unlock(pidev);
}
void idev_set_enable(IDEV *pidev)
{
idev_lock(pidev);
pidev->enabled = 1;
idev_unlock(pidev);
}
int idev_get_enable(IDEV *pidev)
{
if (pidev == NULL)
return -1;
return pidev->enabled;
}
void idev_set_status(IDEV *pidev, IDEV_STATUS status)
{
idev_lock(pidev);
pidev->status = status;
idev_unlock(pidev);
dm_log(pidev, "change status to %d.", status);
}
IDEV_STATUS idev_get_status(IDEV *pidev)
{
return pidev->status;
}
void idev_set_disable(IDEV *pidev)
{
idev_lock(pidev);
pidev->enabled = 0;
idev_unlock(pidev);
}
void idev_set_group(IDEV *pidev, IDEV_GROUP group)
{
idev_lock(pidev);
pidev->group = group;
idev_unlock(pidev);
}
IDEV_GROUP idev_get_group(IDEV *p)
{
if (p == NULL)
return -1;
return p->group;
}
char *idev_get_sim(IDEV *p)
{
if (p == NULL)
return NULL;
return p->sim;
}
int idev_check_active(IDEV *pidev)
{
/* if the device's "if00-port0" device file is still here,
we say it's still alive. */
// char file_name[FULL_FILE_NAME_LEN];
// format_full_dev_file_name(file_name, pidev->name, 0, 0);
// return (!access(file_name, F_OK));
return 1;
}