author | zautrix <zautrix> | 2004-08-07 17:24:40 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-08-07 17:24:40 (UTC) |
commit | 88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22 (patch) (unidiff) | |
tree | 6331418973714243beb674abc87692277b83869d /gammu/emb/common/protocol/nokia/mbus2.c | |
parent | ef8a09ce74ad3f0a51484d03fdf009bd5b3677bf (diff) | |
download | kdepimpi-88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22.zip kdepimpi-88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22.tar.gz kdepimpi-88b0d33b8b0b1f6ae320cfc863ca6a47fa8fec22.tar.bz2 |
Initial revision
Diffstat (limited to 'gammu/emb/common/protocol/nokia/mbus2.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/common/protocol/nokia/mbus2.c | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/gammu/emb/common/protocol/nokia/mbus2.c b/gammu/emb/common/protocol/nokia/mbus2.c new file mode 100644 index 0000000..f07d6c5 --- a/dev/null +++ b/gammu/emb/common/protocol/nokia/mbus2.c | |||
@@ -0,0 +1,252 @@ | |||
1 | /* (c) 2001-2003 by Marcin Wiacek */ | ||
2 | /* based on some work from MyGnokii */ | ||
3 | |||
4 | #include "../../gsmstate.h" | ||
5 | |||
6 | #ifdef GSM_ENABLE_MBUS2 | ||
7 | |||
8 | #include <stdio.h> | ||
9 | #include <string.h> | ||
10 | |||
11 | #include "../../gsmcomon.h" | ||
12 | #include "mbus2.h" | ||
13 | |||
14 | static GSM_Error MBUS2_WriteMessage (GSM_StateMachine *s, | ||
15 | unsigned char *MsgBuffer, | ||
16 | int MsgLength, | ||
17 | unsigned char MsgType) | ||
18 | { | ||
19 | unsigned char *buffer2, checksum = 0; | ||
20 | GSM_Protocol_MBUS2Data *d = &s->Protocol.Data.MBUS2; | ||
21 | int i, sent, len; | ||
22 | |||
23 | GSM_DumpMessageLevel3(s, MsgBuffer, MsgLength, MsgType); | ||
24 | |||
25 | buffer2 = (unsigned char *)malloc(MsgLength + 8); | ||
26 | |||
27 | buffer2[0] = MBUS2_FRAME_ID; | ||
28 | buffer2[1] = MBUS2_DEVICE_PHONE; // destination | ||
29 | buffer2[2] = MBUS2_DEVICE_PC; // source | ||
30 | buffer2[3] = MsgType; | ||
31 | buffer2[4] = MsgLength / 256; | ||
32 | buffer2[5] = MsgLength % 256; | ||
33 | |||
34 | memcpy(buffer2 + 6, MsgBuffer, MsgLength); | ||
35 | len = 6 + MsgLength; | ||
36 | |||
37 | /* According to http://www.flosys.com/tdma/n5160.html some phones | ||
38 | * can have problems with checksum equal 0x1F. Phones can recognize | ||
39 | * received frame, but won't send ACK for it. When checksum is 0x1F, | ||
40 | * we increment the sequence number | ||
41 | */ | ||
42 | do { | ||
43 | d->MsgSequenceNumber++; | ||
44 | |||
45 | buffer2[len] = d->MsgSequenceNumber; | ||
46 | |||
47 | /* Calculating checksum */ | ||
48 | checksum = 0; | ||
49 | for (i = 0; i < len + 1; i++) checksum ^= buffer2[i]; | ||
50 | } while (checksum == 0x1f); | ||
51 | |||
52 | buffer2[len++] = d->MsgSequenceNumber; | ||
53 | buffer2[len++] = checksum; | ||
54 | |||
55 | GSM_DumpMessageLevel2(s, buffer2+6, MsgLength, MsgType); | ||
56 | |||
57 | /* Sending to phone */ | ||
58 | my_sleep(10); | ||
59 | sent=s->Device.Functions->WriteDevice(s,buffer2,len); | ||
60 | |||
61 | free(buffer2); | ||
62 | |||
63 | if (sent!=len) return ERR_DEVICEWRITEERROR; | ||
64 | return ERR_NONE; | ||
65 | } | ||
66 | |||
67 | static GSM_Error MBUS2_SendAck(GSM_StateMachine *s, | ||
68 | unsigned char type, | ||
69 | unsigned char sequence) | ||
70 | { | ||
71 | GSM_Device_Functions *Device = s->Device.Functions; | ||
72 | unsigned char buffer2[6]; | ||
73 | int i; | ||
74 | |||
75 | buffer2[0] = MBUS2_FRAME_ID; | ||
76 | buffer2[1] = MBUS2_DEVICE_PHONE;//destination | ||
77 | buffer2[2] = MBUS2_DEVICE_PC; //source | ||
78 | buffer2[3] = MBUS2_ACK_BYTE; | ||
79 | buffer2[4] = sequence; | ||
80 | buffer2[5] = 0; | ||
81 | |||
82 | /* Calculating checksum */ | ||
83 | for (i = 0; i < 5; i++) buffer2[5] ^= buffer2[i]; | ||
84 | |||
85 | if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || | ||
86 | s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE) { | ||
87 | smprintf(s,"[Sending Ack of type %02x, seq: %x]\n",type,sequence); | ||
88 | } | ||
89 | |||
90 | /* Sending to phone */ | ||
91 | my_sleep(10); | ||
92 | if (Device->WriteDevice(s,buffer2,6)!=6) return ERR_DEVICEWRITEERROR; | ||
93 | |||
94 | return ERR_NONE; | ||
95 | } | ||
96 | |||
97 | static GSM_Error MBUS2_StateMachine(GSM_StateMachine *s, unsigned char rx_char) | ||
98 | { | ||
99 | GSM_Phone_Functions *Phone= s->Phone.Functions; | ||
100 | GSM_Protocol_MBUS2Data *d= &s->Protocol.Data.MBUS2; | ||
101 | |||
102 | d->Msg.CheckSum[0] = d->Msg.CheckSum[1]; | ||
103 | d->Msg.CheckSum[1] ^= rx_char; | ||
104 | |||
105 | if (d->MsgRXState == RX_GetMessage) { | ||
106 | d->Msg.Buffer[d->Msg.Count] = rx_char; | ||
107 | d->Msg.Count++; | ||
108 | |||
109 | /* This is not last byte in frame */ | ||
110 | if (d->Msg.Count != d->Msg.Length+2) return ERR_NONE; | ||
111 | |||
112 | /* Checksum is incorrect */ | ||
113 | if (d->Msg.CheckSum[0] != rx_char) { | ||
114 | if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || s->di.dl==DL_TEXTERROR || | ||
115 | s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE || s->di.dl==DL_TEXTERRORDATE) { | ||
116 | smprintf(s,"[ERROR: checksum]\n"); | ||
117 | } | ||
118 | |||
119 | d->MsgRXState = RX_Sync; | ||
120 | return ERR_NONE; | ||
121 | } | ||
122 | |||
123 | if (d->Msg.Destination != MBUS2_DEVICE_PHONE) { | ||
124 | MBUS2_SendAck(s, d->Msg.Type, d->Msg.Buffer[d->Msg.Count-2]); | ||
125 | s->Phone.Data.RequestMsg= &d->Msg; | ||
126 | s->Phone.Data.DispatchError= Phone->DispatchMessage(s); | ||
127 | } | ||
128 | |||
129 | d->MsgRXState = RX_Sync; | ||
130 | return ERR_NONE; | ||
131 | } | ||
132 | if (d->MsgRXState == RX_GetLength2) { | ||
133 | if (d->Msg.Type == MBUS2_ACK_BYTE) { | ||
134 | if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || | ||
135 | s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE) { | ||
136 | smprintf(s,"[Received Ack]\n"); | ||
137 | } | ||
138 | |||
139 | d->MsgRXState = RX_Sync; | ||
140 | return ERR_NONE; | ||
141 | } | ||
142 | |||
143 | d->Msg.Length = d->Msg.Length + rx_char; | ||
144 | if (d->Msg.BufferUsed < d->Msg.Length+2) { | ||
145 | d->Msg.BufferUsed = d->Msg.Length+2; | ||
146 | d->Msg.Buffer = (unsigned char *)realloc(d->Msg.Buffer,d->Msg.BufferUsed); | ||
147 | } | ||
148 | |||
149 | d->MsgRXState = RX_GetMessage; | ||
150 | return ERR_NONE; | ||
151 | } | ||
152 | if (d->MsgRXState == RX_GetLength1) { | ||
153 | d->Msg.Length = rx_char * 256; | ||
154 | |||
155 | d->MsgRXState = RX_GetLength2; | ||
156 | return ERR_NONE; | ||
157 | } | ||
158 | if (d->MsgRXState == RX_GetType) { | ||
159 | d->Msg.Type = rx_char; | ||
160 | |||
161 | d->MsgRXState = RX_GetLength1; | ||
162 | return ERR_NONE; | ||
163 | } | ||
164 | if (d->MsgRXState == RX_GetSource) { | ||
165 | if (rx_char != MBUS2_DEVICE_PHONE && rx_char != MBUS2_DEVICE_PC) { | ||
166 | if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || s->di.dl==DL_TEXTERROR || | ||
167 | s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE || s->di.dl==DL_TEXTERRORDATE) { | ||
168 | smprintf(s,"[ERROR: incorrect char - %02x, not %02x and %02x]\n", rx_char, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC); | ||
169 | } | ||
170 | d->MsgRXState = RX_Sync; | ||
171 | return ERR_NONE; | ||
172 | } | ||
173 | d->Msg.Source = rx_char; | ||
174 | |||
175 | d->MsgRXState = RX_GetType; | ||
176 | return ERR_NONE; | ||
177 | } | ||
178 | if (d->MsgRXState == RX_GetDestination) { | ||
179 | if (rx_char != MBUS2_DEVICE_PC && rx_char != MBUS2_DEVICE_PHONE) { | ||
180 | if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || s->di.dl==DL_TEXTERROR || | ||
181 | s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE || s->di.dl==DL_TEXTERRORDATE) { | ||
182 | smprintf(s,"[ERROR: incorrect char - %02x, not %02x and %02x]\n", rx_char, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC); | ||
183 | } | ||
184 | d->MsgRXState = RX_Sync; | ||
185 | return ERR_NONE; | ||
186 | } | ||
187 | d->Msg.Destination = rx_char; | ||
188 | |||
189 | d->MsgRXState = RX_GetSource; | ||
190 | return ERR_NONE; | ||
191 | } | ||
192 | if (d->MsgRXState == RX_Sync) { | ||
193 | if (rx_char != MBUS2_FRAME_ID) { | ||
194 | if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || s->di.dl==DL_TEXTERROR || | ||
195 | s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE || s->di.dl==DL_TEXTERRORDATE) { | ||
196 | smprintf(s,"[ERROR: incorrect char - %02x, not %02x]\n", rx_char, MBUS2_FRAME_ID); | ||
197 | } | ||
198 | return ERR_NONE; | ||
199 | } | ||
200 | d->Msg.CheckSum[1] = MBUS2_FRAME_ID; | ||
201 | d->Msg.Count = 0; | ||
202 | |||
203 | d->MsgRXState = RX_GetDestination; | ||
204 | return ERR_NONE; | ||
205 | } | ||
206 | return ERR_NONE; | ||
207 | } | ||
208 | |||
209 | static GSM_Error MBUS2_Initialise(GSM_StateMachine *s) | ||
210 | { | ||
211 | GSM_Device_Functions *Device= s->Device.Functions; | ||
212 | GSM_Protocol_MBUS2Data *d= &s->Protocol.Data.MBUS2; | ||
213 | GSM_Error error; | ||
214 | |||
215 | d->Msg.Length = 0; | ||
216 | d->Msg.BufferUsed= 0; | ||
217 | d->Msg.Buffer = NULL; | ||
218 | |||
219 | d->MsgSequenceNumber= 0; | ||
220 | d->MsgRXState = RX_Sync; | ||
221 | |||
222 | error=Device->DeviceSetSpeed(s,9600); | ||
223 | if (error!=ERR_NONE) return error; | ||
224 | |||
225 | error=Device->DeviceSetParity(s,true); | ||
226 | if (error!=ERR_NONE) return error; | ||
227 | |||
228 | error=Device->DeviceSetDtrRts(s,false,true); /*DTR low,RTS high*/ | ||
229 | if (error!=ERR_NONE) return error; | ||
230 | my_sleep(200); | ||
231 | |||
232 | return ERR_NONE; | ||
233 | } | ||
234 | |||
235 | static GSM_Error MBUS2_Terminate(GSM_StateMachine *s) | ||
236 | { | ||
237 | free(s->Protocol.Data.MBUS2.Msg.Buffer); | ||
238 | return ERR_NONE; | ||
239 | } | ||
240 | |||
241 | GSM_Protocol_Functions MBUS2Protocol = { | ||
242 | MBUS2_WriteMessage, | ||
243 | MBUS2_StateMachine, | ||
244 | MBUS2_Initialise, | ||
245 | MBUS2_Terminate | ||
246 | }; | ||
247 | |||
248 | #endif | ||
249 | |||
250 | /* How should editor hadle tabs in this file? Add editor commands here. | ||
251 | * vim: noexpandtab sw=8 ts=8 sts=8: | ||
252 | */ | ||