-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocole.rl
190 lines (160 loc) · 4.95 KB
/
protocole.rl
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2012 William MARTIN
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include "types.h"
#include "cmd.h"
void executeCmd(ModemParserCtx_t *ctx, int fd);
#define LEN(AT, FPC) (FPC - ctx->buffer - ctx->AT)
#define MARK(M,FPC) (ctx->M = (FPC) - ctx->buffer)
#define PTR_TO(F) (ctx->buffer + ctx->F)
/** machine **/
%%{
machine modem_gsm;
#
# Save the position of the current character in the buffer
#
action mark {
MARK(mark, fpc);
}
#
# EOL recieved with a valid payload
#
action done {
executeCmd(ctx, fd);
ModemParserInit(ctx); // Reset the parser
}
#
# Error at the start of a request,
# We have maybe lost the start of the request
# Try to parse new incomming data as request start
#
action syntax_error_initial {
fhold; fgoto errors;
}
#
# Error during the request,
# We have maybe lost the end of the previous request
# Parse new data as new request
#
action syntax_error {
fgoto errors;
}
#
# Action execute when receive a EOL, when we are in error state
#
action done_errors {
write(fd, "ERROR\r\n", 7);
ctx->arg_count = 0;
fgoto main;
}
#
# Save the current AT command
#
action cmd_at {
ctx->cmd = CMD_AT;
}
action cmd_pin_status {
ctx->cmd = CMD_PIN_STATUS;
}
action cmd_pin_unlock {
ctx->cmd = CMD_PIN_UNLOCK;
}
action cmd_pin_change {
ctx->cmd = CMD_PIN_CHANGE;
}
action cmd_send_sms {
ctx->cmd = CMD_SEND_SMS;
}
#
# Save argument position in the buffer
#
action arg {
ctx->arg[ctx->arg_count] = ctx->mark;
ctx->arg_len[ctx->arg_count] = LEN(mark, fpc);
ctx->arg_count++;
}
action send_sms_prompt {
sleep(1); // Emulate network lag
tcflush(fd, TCIFLUSH);
write(fd, "> ", 2);
}
# grammar
CR = 0x0D;
LF = 0x0A;
FF = 0x0C;
ctrlz = 0x1A;
esc = 0x1B;
phoneNumber = ("+"? digit+) >mark %arg;
phoneNumberList = phoneNumber ("," phoneNumber)*;
smsAllowChacarters = (CR | LF | FF | [a-zA-Z0-9] | space);
# elements
CMD_AT = 'AT' %cmd_at CR LF?;
CMD_PIN_STATUS = "AT+CPIN=?" %cmd_pin_status CR LF?;
CMD_PIN_UNLOCK = "AT+CPIN=" (digit{1,6}) >mark %arg %cmd_pin_unlock CR LF?;
CMD_PIN_CHANGE = "AT+CPIN=" (digit{1,6}) >mark %arg "," (digit{1,6}) >mark %arg %cmd_pin_change CR LF?;
CMD_SEND_SMS = "AT+CMGS=" %cmd_send_sms phoneNumberList (CR >send_sms_prompt LF?) smsAllowChacarters+ >mark %arg ctrlz @done;
CMDS = (CMD_AT | CMD_PIN_STATUS | CMD_PIN_UNLOCK | CMD_PIN_CHANGE | CMD_SEND_SMS);
# protocole
main := ( (CR? LF)* CMDS @done )+ >err(syntax_error_initial) @err(syntax_error);
errors := (any -- (CR | LF))* (CR LF?) @done_errors;
}%%
/** Data **/
%% write data;
void
ModemParserInit(ModemParserCtx_t *ctx) {
int cs = 0;
%% write init;
if (ctx->buffer == NULL) {
ctx->buffer_size = 1024;
ctx->buffer = malloc(ctx->buffer_size);
assert(ctx->buffer != NULL);
}
ctx->cmd = CMD_EMPTY;
ctx->arg_count = 0;
ctx->buffer_len = 0;
ctx->cs = cs;
}
void
ModemParserExecute(ModemParserCtx_t *ctx, int fd, const char *buffer, size_t len)
{
const char *p, *pe, *eof;
int cs = ctx->cs;
if ((ctx->buffer_len + len) > ctx->buffer_size) {
ctx->buffer_size += 1024;
ctx->buffer = realloc(ctx->buffer, ctx->buffer_size);
assert(ctx->buffer != NULL);
}
strncpy(ctx->buffer + ctx->buffer_len, buffer, len);
ctx->buffer_len += len;
p = ctx->buffer + ctx->buffer_len - len;
pe = ctx->buffer + ctx->buffer_len;
%% write exec;
ctx->cs = cs;
}