-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlistener.cc
62 lines (48 loc) · 1.26 KB
/
atlistener.cc
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
// Copyright: (C) 2016 ESIMTEK ALL RIGHTS RESERVED
// author: Yonghua Zheng
#include "log.h"
#include "atlistener.h"
AtListener::AtListener(Modem* _modem)
: Listener(_modem->getfd()), modem(_modem)
{
}
AtListener::~AtListener()
{
}
bool AtListener::on_data_available()
{
bool ret = true;
char response[256];
int offset, n, count;
offset = modem->read(buffer, sizeof(buffer));
while (offset < (int)sizeof(buffer) && modem->select(100, true, false))
offset += modem->read(buffer + offset, sizeof(buffer) - offset);
n = 0;
count = 0;
while (n < offset) {
for (; n < offset; n++) {
if (buffer[n] == '\r')
continue;
if (buffer[n] == '\n') {
if (count > 0) break;
continue;
}
response[count++] = buffer[n];
if (count > (int)(sizeof(response) - 1))
break;
}
if (!count)
continue;
response[count] = 0;
count = 0;
Log::Trace("<== %s", response);
AtEvent *evt = AtEvent::decodeFromMessage(response);
if (evt) {
ret = on_event(evt);
delete evt;
if (!ret)
break;
}
}
return ret;
}