Diffstat (limited to 'gammu/emb/common/protocol/at/at.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/common/protocol/at/at.c | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/gammu/emb/common/protocol/at/at.c b/gammu/emb/common/protocol/at/at.c index f4a75b7..f8ddc06 100644 --- a/gammu/emb/common/protocol/at/at.c +++ b/gammu/emb/common/protocol/at/at.c | |||
@@ -1,172 +1,176 @@ | |||
1 | /* (c) 2002-2004 by Marcin Wiacek and Michal Cihar */ | 1 | /* (c) 2002-2004 by Marcin Wiacek and Michal Cihar */ |
2 | 2 | ||
3 | #include "../../gsmstate.h" | 3 | #include "../../gsmstate.h" |
4 | 4 | ||
5 | #if defined(GSM_ENABLE_AT) || defined(GSM_ENABLE_BLUEAT) || defined(GSM_ENABLE_IRDAAT) | 5 | #if defined(GSM_ENABLE_AT) || defined(GSM_ENABLE_BLUEAT) || defined(GSM_ENABLE_IRDAAT) |
6 | 6 | ||
7 | #include <stdio.h> | 7 | #include <stdio.h> |
8 | #include <string.h> | 8 | #include <string.h> |
9 | 9 | ||
10 | #include "../../gsmcomon.h" | 10 | #include "../../gsmcomon.h" |
11 | #include "at.h" | 11 | #include "at.h" |
12 | 12 | ||
13 | static GSM_Error AT_WriteMessage (GSM_StateMachine *s, unsigned char *buffer, | 13 | static GSM_Error AT_WriteMessage (GSM_StateMachine *s, unsigned char *buffer, |
14 | int length, unsigned char type) | 14 | int length, unsigned char type) |
15 | { | 15 | { |
16 | int i,sent = 0; | 16 | int i,sent = 0; |
17 | 17 | ||
18 | GSM_DumpMessageLevel2(s, buffer, length, type); | 18 | GSM_DumpMessageLevel2(s, buffer, length, type); |
19 | GSM_DumpMessageLevel3(s, buffer, length, type); | 19 | GSM_DumpMessageLevel3(s, buffer, length, type); |
20 | if (s->Protocol.Data.AT.FastWrite) { | 20 | if (s->Protocol.Data.AT.FastWrite) { |
21 | while (sent != length) { | 21 | while (sent != length) { |
22 | if ((i = s->Device.Functions->WriteDevice(s,buffer + sent, length - sent)) == 0) { | 22 | if ((i = s->Device.Functions->WriteDevice(s,buffer + sent, length - sent)) == 0) { |
23 | return ERR_DEVICEWRITEERROR; | 23 | return ERR_DEVICEWRITEERROR; |
24 | } | 24 | } |
25 | sent += i; | 25 | sent += i; |
26 | } | 26 | } |
27 | } else { | 27 | } else { |
28 | for (i=0;i<length;i++) { | 28 | for (i=0;i<length;i++) { |
29 | if (s->Device.Functions->WriteDevice(s,buffer+i,1)!=1) return ERR_DEVICEWRITEERROR; | 29 | if (s->Device.Functions->WriteDevice(s,buffer+i,1)!=1) return ERR_DEVICEWRITEERROR; |
30 | /* For some phones like Siemens M20 we need to wait a little | 30 | /* For some phones like Siemens M20 we need to wait a little |
31 | * after writing each char. Possible reason: these phones | 31 | * after writing each char. Possible reason: these phones |
32 | * can't receive so fast chars or there is bug here in Gammu */ | 32 | * can't receive so fast chars or there is bug here in Gammu */ |
33 | my_sleep(1); | 33 | my_sleep(1); |
34 | } | 34 | } |
35 | my_sleep(400); | 35 | my_sleep(400); |
36 | } | 36 | } |
37 | 37 | ||
38 | return ERR_NONE; | 38 | return ERR_NONE; |
39 | } | 39 | } |
40 | 40 | ||
41 | typedef struct { | 41 | typedef struct { |
42 | char*text; | 42 | char*text; |
43 | intlines; | 43 | intlines; |
44 | } SpecialAnswersStruct; | 44 | } SpecialAnswersStruct; |
45 | 45 | ||
46 | static GSM_Error AT_StateMachine(GSM_StateMachine *s, unsigned char rx_char) | 46 | static GSM_Error AT_StateMachine(GSM_StateMachine *s, unsigned char rx_char) |
47 | { | 47 | { |
48 | GSM_Protocol_Message Msg2; | 48 | GSM_Protocol_Message Msg2; |
49 | GSM_Protocol_ATData *d = &s->Protocol.Data.AT; | 49 | GSM_Protocol_ATData *d = &s->Protocol.Data.AT; |
50 | int i; | 50 | int i; |
51 | 51 | ||
52 | /* These are lines with end of "normal" answers */ | 52 | /* These are lines with end of "normal" answers */ |
53 | static char *StartStrings[] = { | 53 | static char *StartStrings[] = { |
54 | "OK" , "ERROR" , | 54 | "OK" , "ERROR" , |
55 | "+CME ERROR:" , "+CMS ERROR:", | 55 | "+CME ERROR:" , "+CMS ERROR:", |
56 | 56 | ||
57 | "+CPIN: " ,/*A2D issue*/ | 57 | "+CPIN: " ,/*A2D issue*/ |
58 | 58 | ||
59 | NULL}; | 59 | NULL}; |
60 | 60 | ||
61 | /* Some info from phone can be inside "normal" answers | 61 | /* Some info from phone can be inside "normal" answers |
62 | * It starts with strings written here | 62 | * It starts with strings written here |
63 | */ | 63 | */ |
64 | static SpecialAnswersStructSpecialAnswers[] = { | 64 | static SpecialAnswersStructSpecialAnswers[] = { |
65 | {"_OSIGQ:" ,1}, {"_OBS:",1}, | 65 | {"_OSIGQ:" ,1}, {"_OBS:",1}, |
66 | {"^SCN:" ,1}, {"+CGREG:",1}, | 66 | {"^SCN:" ,1}, {"+CGREG:",1}, |
67 | {"+CBM:" ,1}, {"+CMT:",2}, | 67 | {"+CBM:" ,1}, {"+CMT:",2}, |
68 | {"+CMTI:" ,1}, {"+CDS:",2}, | 68 | {"+CMTI:" ,1}, {"+CDS:",2}, |
69 | {"+CREG:",1}, | 69 | {"+CREG:" ,1}, {"+CUSD",1}, |
70 | 70 | ||
71 | {"RING" ,1}, {"NO CARRIER",1}, | 71 | {"RING" ,1}, {"NO CARRIER",1}, |
72 | {"NO ANSWER" ,1}, {"+COLP",1}, | 72 | {"NO ANSWER" ,1}, {"+COLP",1}, |
73 | {"+CLIP",1}, | 73 | {"+CLIP",1}, |
74 | 74 | ||
75 | {"SDNDCRC =" ,1},/* Samsung binary transfer end */ | ||
76 | |||
75 | {NULL ,1}}; | 77 | {NULL ,1}}; |
76 | 78 | ||
79 | //printf("%c",rx_char); | ||
80 | |||
77 | /* Ignore leading CR, LF and ESC */ | 81 | /* Ignore leading CR, LF and ESC */ |
78 | if (d->Msg.Length == 0) { | 82 | if (d->Msg.Length == 0) { |
79 | if (rx_char == 10 || rx_char == 13 || rx_char == 27) return ERR_NONE; | 83 | if (rx_char == 10 || rx_char == 13 || rx_char == 27) return ERR_NONE; |
80 | d->LineStart = d->Msg.Length; | 84 | d->LineStart = d->Msg.Length; |
81 | } | 85 | } |
82 | 86 | ||
83 | if (d->Msg.BufferUsed < d->Msg.Length + 2) { | 87 | if (d->Msg.BufferUsed < d->Msg.Length + 2) { |
84 | d->Msg.BufferUsed= d->Msg.Length + 2; | 88 | d->Msg.BufferUsed= d->Msg.Length + 2; |
85 | d->Msg.Buffer = (unsigned char *)realloc(d->Msg.Buffer,d->Msg.BufferUsed); | 89 | d->Msg.Buffer = (unsigned char *)realloc(d->Msg.Buffer,d->Msg.BufferUsed); |
86 | } | 90 | } |
87 | d->Msg.Buffer[d->Msg.Length++] = rx_char; | 91 | d->Msg.Buffer[d->Msg.Length++] = rx_char; |
88 | d->Msg.Buffer[d->Msg.Length ] = 0; | 92 | d->Msg.Buffer[d->Msg.Length ] = 0; |
89 | 93 | ||
90 | switch (rx_char) { | 94 | switch (rx_char) { |
91 | case 0: | 95 | case 0: |
92 | break; | 96 | break; |
93 | case 10: | 97 | case 10: |
94 | case 13: | 98 | case 13: |
95 | if (!d->wascrlf) d->LineEnd = d->Msg.Length-1; | 99 | if (!d->wascrlf) d->LineEnd = d->Msg.Length-1; |
96 | d->wascrlf = true; | 100 | d->wascrlf = true; |
97 | if (d->Msg.Length > 0 && rx_char == 10 && d->Msg.Buffer[d->Msg.Length-2]==13) { | 101 | if (d->Msg.Length > 0 && rx_char == 10 && d->Msg.Buffer[d->Msg.Length-2]==13) { |
98 | i = 0; | 102 | i = 0; |
99 | while (StartStrings[i] != NULL) { | 103 | while (StartStrings[i] != NULL) { |
100 | if (strncmp(StartStrings[i],d->Msg.Buffer+d->LineStart,strlen(StartStrings[i])) == 0) { | 104 | if (strncmp(StartStrings[i],d->Msg.Buffer+d->LineStart,strlen(StartStrings[i])) == 0) { |
101 | s->Phone.Data.RequestMsg= &d->Msg; | 105 | s->Phone.Data.RequestMsg= &d->Msg; |
102 | s->Phone.Data.DispatchError= s->Phone.Functions->DispatchMessage(s); | 106 | s->Phone.Data.DispatchError= s->Phone.Functions->DispatchMessage(s); |
103 | d->Msg.Length = 0; | 107 | d->Msg.Length = 0; |
104 | break; | 108 | break; |
105 | } | 109 | } |
106 | i++; | 110 | i++; |
107 | } | 111 | } |
108 | if (d->Msg.Length == 0) break; | 112 | if (d->Msg.Length == 0) break; |
109 | 113 | ||
110 | i = 0; | 114 | i = 0; |
111 | while (SpecialAnswers[i].text != NULL) { | 115 | while (SpecialAnswers[i].text != NULL) { |
112 | if (strncmp(SpecialAnswers[i].text,d->Msg.Buffer+d->LineStart,strlen(SpecialAnswers[i].text)) == 0) { | 116 | if (strncmp(SpecialAnswers[i].text,d->Msg.Buffer+d->LineStart,strlen(SpecialAnswers[i].text)) == 0) { |
113 | /* We need something better here */ | 117 | /* We need something better here */ |
114 | if (s->Phone.Data.RequestID == ID_GetNetworkInfo && strncmp(SpecialAnswers[i].text,"+CREG:",6) == 0) { | 118 | if (s->Phone.Data.RequestID == ID_GetNetworkInfo && strncmp(SpecialAnswers[i].text,"+CREG:",6) == 0) { |
115 | i++; | 119 | i++; |
116 | continue; | 120 | continue; |
117 | } | 121 | } |
118 | d->SpecialAnswerStart = d->LineStart; | 122 | d->SpecialAnswerStart = d->LineStart; |
119 | d->SpecialAnswerLines= SpecialAnswers[i].lines; | 123 | d->SpecialAnswerLines= SpecialAnswers[i].lines; |
120 | } | 124 | } |
121 | i++; | 125 | i++; |
122 | } | 126 | } |
123 | 127 | ||
124 | 128 | ||
125 | if (d->SpecialAnswerLines == 1) { | 129 | if (d->SpecialAnswerLines == 1) { |
126 | /* This is end of special answer. We copy it and send to phone module */ | 130 | /* This is end of special answer. We copy it and send to phone module */ |
127 | Msg2.Buffer = malloc(d->LineEnd - d->SpecialAnswerStart + 3); | 131 | Msg2.Buffer = malloc(d->LineEnd - d->SpecialAnswerStart + 3); |
128 | memcpy(Msg2.Buffer,d->Msg.Buffer+d->SpecialAnswerStart,d->LineEnd - d->SpecialAnswerStart + 2); | 132 | memcpy(Msg2.Buffer,d->Msg.Buffer+d->SpecialAnswerStart,d->LineEnd - d->SpecialAnswerStart + 2); |
129 | Msg2.Length = d->LineEnd - d->SpecialAnswerStart + 2; | 133 | Msg2.Length = d->LineEnd - d->SpecialAnswerStart + 2; |
130 | Msg2.Buffer[Msg2.Length] = 0; | 134 | Msg2.Buffer[Msg2.Length] = 0; |
131 | 135 | ||
132 | s->Phone.Data.RequestMsg= &Msg2; | 136 | s->Phone.Data.RequestMsg= &Msg2; |
133 | s->Phone.Data.DispatchError= s->Phone.Functions->DispatchMessage(s); | 137 | s->Phone.Data.DispatchError= s->Phone.Functions->DispatchMessage(s); |
134 | free(Msg2.Buffer); | 138 | free(Msg2.Buffer); |
135 | 139 | ||
136 | /* We cut special answer from main buffer */ | 140 | /* We cut special answer from main buffer */ |
137 | d->Msg.Length = d->SpecialAnswerStart; | 141 | d->Msg.Length = d->SpecialAnswerStart; |
138 | if (d->Msg.Length != 0) d->Msg.Length = d->Msg.Length - 2; | 142 | if (d->Msg.Length != 0) d->Msg.Length = d->Msg.Length - 2; |
139 | 143 | ||
140 | /* We need to find earlier values of all variables */ | 144 | /* We need to find earlier values of all variables */ |
141 | d->wascrlf = false; | 145 | d->wascrlf = false; |
142 | d->LineStart = 0; | 146 | d->LineStart = 0; |
143 | for (i=0;i<d->Msg.Length;i++) { | 147 | for (i=0;i<d->Msg.Length;i++) { |
144 | switch(d->Msg.Buffer[i]) { | 148 | switch(d->Msg.Buffer[i]) { |
145 | case 0: | 149 | case 0: |
146 | break; | 150 | break; |
147 | case 10: | 151 | case 10: |
148 | case 13: | 152 | case 13: |
149 | if (!d->wascrlf) d->LineEnd = d->Msg.Length-1; | 153 | if (!d->wascrlf) d->LineEnd = d->Msg.Length-1; |
150 | d->wascrlf = true; | 154 | d->wascrlf = true; |
151 | break; | 155 | break; |
152 | default: | 156 | default: |
153 | if (d->wascrlf) { | 157 | if (d->wascrlf) { |
154 | d->LineStart= d->Msg.Length-1; | 158 | d->LineStart= d->Msg.Length-1; |
155 | d->wascrlf = false; | 159 | d->wascrlf = false; |
156 | } | 160 | } |
157 | } | 161 | } |
158 | } | 162 | } |
159 | d->Msg.Buffer[d->Msg.Length] = 0; | 163 | d->Msg.Buffer[d->Msg.Length] = 0; |
160 | } | 164 | } |
161 | if (d->SpecialAnswerLines > 0) d->SpecialAnswerLines--; | 165 | if (d->SpecialAnswerLines > 0) d->SpecialAnswerLines--; |
162 | } | 166 | } |
163 | break; | 167 | break; |
164 | case 'T': | 168 | case 'T': |
165 | /* When CONNECT string received, we know there will not follow | 169 | /* When CONNECT string received, we know there will not follow |
166 | * anything AT related, after CONNECT can follow ppp data, alcabus | 170 | * anything AT related, after CONNECT can follow ppp data, alcabus |
167 | * data and also other things. | 171 | * data and also other things. |
168 | */ | 172 | */ |
169 | if (strncmp(d->Msg.Buffer+d->LineStart, "CONNECT", 7) == 0) { | 173 | if (strncmp(d->Msg.Buffer+d->LineStart, "CONNECT", 7) == 0) { |
170 | s->Phone.Data.RequestMsg = &d->Msg; | 174 | s->Phone.Data.RequestMsg = &d->Msg; |
171 | s->Phone.Data.DispatchError= s->Phone.Functions->DispatchMessage(s); | 175 | s->Phone.Data.DispatchError= s->Phone.Functions->DispatchMessage(s); |
172 | d->LineStart = -1; | 176 | d->LineStart = -1; |