Imported gammu 0.90.7
[gammu.git] / gammu / sniff.c
blobdab7b2993059bdbccb17e49cd8823ca392d6c439
2 #include "../common/gsmstate.h"
4 #ifdef DEBUG
6 #include <string.h>
7 #include <stdio.h>
9 #include "../common/protocol/nokia/mbus2.h"
10 #include "../common/protocol/nokia/phonet.h"
11 #include "../common/phone/nokia/nfunc.h"
12 #include "../common/misc/misc.h"
13 #include "../common/gsmcomon.h"
14 #include "../common/service/gsmcal.h"
15 #include "gammu.h"
17 static GSM_Protocol_MBUS2Data MBUS2Data;
18 static GSM_Protocol_PHONETData PHONETData;
20 #define MBUS2_DEVICE_PC1 0x1D
22 static void DecodeInputMBUS2(unsigned char rx_byte)
24 GSM_Protocol_MBUS2Data *d = &MBUS2Data;
26 d->Msg.CheckSum[0] = d->Msg.CheckSum[1];
27 d->Msg.CheckSum[1] ^= rx_byte;
29 switch (d->MsgRXState) {
31 case RX_Sync:
33 if (rx_byte == MBUS2_FRAME_ID) {
34 d->Msg.CheckSum[1] = MBUS2_FRAME_ID;
35 d->Msg.Count = 0;
36 d->MsgRXState = RX_GetDestination;
37 } else {
38 printf("[ERROR: incorrect char - %02x, not %02x]\n", rx_byte, MBUS2_FRAME_ID);
40 break;
42 case RX_GetDestination:
44 if (rx_byte != MBUS2_DEVICE_PC && rx_byte != MBUS2_DEVICE_PHONE && rx_byte != MBUS2_DEVICE_PC1) {
45 d->MsgRXState = RX_Sync;
46 printf("[ERROR: incorrect char - %02x, not %02x and %02x and %02x]\n",
47 rx_byte, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC, MBUS2_DEVICE_PC1);
48 } else {
49 d->Msg.Destination = rx_byte;
50 d->MsgRXState = RX_GetSource;
52 break;
54 case RX_GetSource:
56 if (rx_byte != MBUS2_DEVICE_PC && rx_byte != MBUS2_DEVICE_PHONE && rx_byte != MBUS2_DEVICE_PC1) {
57 d->MsgRXState = RX_Sync;
58 printf("[ERROR: incorrect char - %02x, not %02x and %02x and %02x]\n",
59 rx_byte, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC, MBUS2_DEVICE_PC1);
60 } else {
61 d->Msg.Source = rx_byte;
62 d->MsgRXState = RX_GetType;
64 break;
66 case RX_GetType:
68 d->Msg.Type = rx_byte;
69 d->MsgRXState = RX_GetLength1;
70 break;
72 case RX_GetLength1:
74 d->Msg.Length = rx_byte * 256;
75 d->MsgRXState = RX_GetLength2;
76 break;
78 case RX_GetLength2:
80 if (d->Msg.Type == MBUS2_ACK_BYTE)
82 d->MsgRXState = RX_Sync;
83 } else {
84 d->Msg.Length = d->Msg.Length + rx_byte;
85 d->MsgRXState = RX_GetMessage;
87 break;
89 case RX_GetMessage:
91 d->Msg.Buffer[d->Msg.Count] = rx_byte;
92 d->Msg.Count++;
94 if (d->Msg.Count == d->Msg.Length+2) {
95 /* When we have last byte, it's the checksum. */
96 if (d->Msg.CheckSum[0] == rx_byte) {
97 if (d->Msg.Destination != MBUS2_DEVICE_PHONE) {
98 printf("Received frame");
99 } else {
100 printf("Sending frame");
102 printf(" 0x%02x / 0x%04x", d->Msg.Type, d->Msg.Length);
103 DumpMessage(stdout, d->Msg.Buffer, d->Msg.Length);
104 if (d->Msg.Destination != MBUS2_DEVICE_PHONE) {
105 if (s.Phone.Functions != NULL) {
106 s.Phone.Data.RequestMsg = &d->Msg;
107 s.Phone.Functions->DispatchMessage(&s);
110 } else {
111 printf("[ERROR: checksum]\n");
112 printf(" 0x%02x / 0x%04x", d->Msg.Type, d->Msg.Length);
113 DumpMessage(stdout, d->Msg.Buffer, d->Msg.Length);
115 d->MsgRXState = RX_Sync;
117 break;
122 #define PHONETIRDA_DEVICE_PC1 0x10
124 static void DecodeInputIRDA(unsigned char rx_byte)
126 GSM_Protocol_PHONETData *d = &PHONETData;
128 switch (d->MsgRXState) {
130 case RX_Sync:
132 if (rx_byte == PHONET_FRAME_ID) {
133 d->Msg.Count = 0;
134 d->MsgRXState = RX_GetDestination;
136 // } else {
137 // printf("[ERROR: incorrect char - %02x, not %02x]\n", rx_byte, FBUS2IRDA_FRAME_ID);
138 // }
139 break;
141 case RX_GetDestination:
143 if (rx_byte != PHONETIRDA_DEVICE_PC1 && rx_byte != PHONET_DEVICE_PHONE) {
144 d->MsgRXState = RX_Sync;
145 // printf("[ERROR: incorrect char - %02x, not %02x and %02x]\n", rx_byte, FBUS2IRDA_DEVICE_PC1, FBUS2IRDA_DEVICE_PHONE);
146 } else {
147 d->Msg.Destination = rx_byte;
148 d->MsgRXState = RX_GetSource;
150 break;
152 case RX_GetSource:
154 if (rx_byte != PHONET_DEVICE_PHONE && rx_byte != PHONETIRDA_DEVICE_PC1) {
155 d->MsgRXState = RX_Sync;
156 // printf("[ERROR: incorrect char - %02x, not %02x and %02x]\n", rx_byte, FBUS2IRDA_DEVICE_PHONE, FBUS2IRDA_DEVICE_PC1);
157 } else {
158 d->Msg.Source = rx_byte;
159 d->MsgRXState = RX_GetType;
161 break;
163 case RX_GetType:
165 d->Msg.Type = rx_byte;
166 d->MsgRXState = RX_GetLength1;
167 break;
169 case RX_GetLength1:
171 d->Msg.Length = rx_byte * 256;
172 d->MsgRXState = RX_GetLength2;
173 break;
175 case RX_GetLength2:
177 d->Msg.Length = d->Msg.Length + rx_byte;
178 d->MsgRXState = RX_GetMessage;
179 break;
181 case RX_GetMessage:
183 d->Msg.Buffer[d->Msg.Count] = rx_byte;
184 d->Msg.Count++;
186 if (d->Msg.Count == d->Msg.Length) {
187 if (d->Msg.Destination != PHONET_DEVICE_PHONE) {
188 printf("Received frame");
189 } else {
190 printf("Sending frame");
192 printf(" 0x%02x / 0x%04x", d->Msg.Type, d->Msg.Length);
193 DumpMessage(stdout, d->Msg.Buffer, d->Msg.Length);
194 if (d->Msg.Destination != PHONET_DEVICE_PHONE) {
195 if (s.Phone.Functions != NULL) {
196 s.Phone.Data.RequestMsg = &d->Msg;
197 s.Phone.Functions->DispatchMessage(&s);
200 d->MsgRXState = RX_Sync;
202 break;
207 static char IMEI[50];
208 static GSM_DateTime DateTime;
209 static GSM_Alarm Alarm;
210 static GSM_MemoryEntry Memory;
211 static GSM_MemoryStatus MemoryStatus;
212 static GSM_SMSC SMSC;
213 static GSM_MultiSMSMessage GetSMSMessage;
214 static GSM_SMSMessage SaveSMSMessage;
215 static GSM_SMSMemoryStatus SMSStatus;
216 static GSM_SMSFolders SMSFolders;
217 static GSM_SignalQuality SignalQuality;
218 static GSM_BatteryCharge BatteryCharge;
219 static GSM_NetworkInfo NetworkInfo;
220 static GSM_Ringtone Ringtone;
221 static GSM_CalendarEntry Calendar;
222 static char SecurityCode;
223 static GSM_WAPBookmark WAPBookmark;
224 static GSM_Bitmap Bitmap;
225 static char PhoneString[500];
227 static char Model[50];
228 static char Version[50];
229 static double VersionNum;
231 static void prepareStateMachine()
233 GSM_Phone_Data *Phone = &s.Phone.Data;
235 strcpy(Phone->IMEI, IMEI);
236 strcpy(Phone->Model, Model);
237 strcpy(Phone->Version, Version);
238 Phone->DateTime = &DateTime;
239 Phone->Alarm = &Alarm;
240 Phone->Memory = &Memory;
241 Phone->Memory->MemoryType = GMT7110_CG;
242 Phone->MemoryStatus = &MemoryStatus;
243 Phone->SMSC = &SMSC;
244 Phone->GetSMSMessage = &GetSMSMessage;
245 Phone->SaveSMSMessage = &SaveSMSMessage;
246 Phone->SMSStatus = &SMSStatus;
247 Phone->SMSFolders = &SMSFolders;
248 Phone->SignalQuality = &SignalQuality;
249 Phone->BatteryCharge = &BatteryCharge;
250 Phone->NetworkInfo = &NetworkInfo;
251 Phone->Ringtone = &Ringtone;
252 Phone->Ringtone->Format = RING_NOKIABINARY;
253 Phone->Cal = &Calendar;
254 Phone->SecurityCode = &SecurityCode;
255 Phone->WAPBookmark = &WAPBookmark;
256 Phone->Bitmap = &Bitmap;
257 Phone->PhoneString = PhoneString;
258 Phone->StartPhoneString = 0;
260 Phone->EnableIncomingSMS = false;
261 Phone->EnableIncomingCB = false;
262 Model[0] = 0;
263 Phone->VerNum = VersionNum;
264 Version[0] = 0;
265 VersionNum = 0;
267 s.Phone.Functions = NULL;
268 s.User.UserReplyFunctions = NULL;
269 Phone->RequestID = ID_EachFrame;
272 void decodesniff(int argc, char *argv[])
274 GSM_ConnectionType Protocol = GCT_MBUS2;
275 unsigned char Buffer[50000];
276 unsigned char Buffer2[50000];
277 FILE *file;
278 int len, len2, pos, state, i;
279 unsigned char mybyte1 = 0,mybyte2;
281 if (!strcmp(argv[2],"MBUS2")) {
282 Protocol = GCT_MBUS2;
283 } else if (!strcmp(argv[2],"IRDA")) {
284 Protocol = GCT_IRDAPHONET;
285 } else {
286 printf("What protocol (\"%s\") ?\n",argv[2]);
287 exit(-1);
289 file = fopen(argv[3], "rb");
290 if (!file) {
291 printf("Can not open file \"%s\"\n",argv[3]);
292 exit(-1);
294 prepareStateMachine();
295 if (argc > 4) {
296 strcpy(s.CurrentConfig->Model,argv[4]);
297 error=GSM_RegisterAllPhoneModules(&s);
298 if (error!=GE_NONE) Print_Error(error);
300 /* Irda uses simple "raw" format */
301 if (Protocol == GCT_IRDAPHONET) {
302 PHONETData.MsgRXState=RX_Sync;
303 len2=30000;
304 while (len2==30000) {
305 len2=fread(Buffer, 1, 30000, file);
306 for (i=0;i<len2;i++) {
307 DecodeInputIRDA(Buffer[i]);
311 /* MBUS2 uses PortMon format */
312 if (Protocol == GCT_MBUS2) {
313 MBUS2Data.MsgRXState=RX_Sync;
314 len2=30000;
315 state=0;
316 while (len2==30000) {
317 len2=fread(Buffer, 1, 30000, file);
318 pos=0;
319 len=0;
320 while (pos!=len2) {
321 switch (state) {
322 case 0:
323 if (Buffer[pos]==' ') state = 1;
324 break;
325 case 1:
326 state = 2;
327 if (Buffer[pos]=='0') state = 0;
328 break;
329 case 2:
330 if (Buffer[pos]>='0' && Buffer[pos]<='9') {
331 state = 2;
332 } else {
333 if (Buffer[pos]==':') state = 3;
335 break;
336 case 3:
337 if (Buffer[pos]==' ') state = 4;
338 break;
339 case 4:
340 if (Buffer[pos]==13) {
341 state = 0;
342 break;
344 mybyte1=Buffer[pos]-'0';
345 if (Buffer[pos]>'9') mybyte1=Buffer[pos]-'A'+10;
346 state = 5;
347 break;
348 case 5:
349 mybyte2=Buffer[pos]-'0';
350 if (Buffer[pos]>'9') mybyte2=Buffer[pos]-'A'+10;
351 Buffer2[len++]=mybyte1*16+mybyte2;
352 state = 6;
353 break;
354 case 6:
355 state = 4;
356 if (Buffer[pos]!=' ') state = 0;
357 break;
359 pos++;
361 for (i=0;i<len;i++) {
362 DecodeInputMBUS2(Buffer2[i]);
366 fclose(file);
369 void decodebinarydump(int argc, char *argv[])
371 unsigned char Buffer[50000];
372 FILE *file;
373 int len, len2, i;
374 unsigned char type;
375 bool sent;
376 GSM_Protocol_Message msg;
378 prepareStateMachine();
379 if (argc > 3) {
380 strcpy(s.CurrentConfig->Model,argv[3]);
381 error=GSM_RegisterAllPhoneModules(&s);
382 if (error!=GE_NONE) Print_Error(error);
384 file = fopen(argv[2], "rb");
385 if (!file) {
386 printf("Can not open file \"%s\"\n",argv[2]);
387 exit(-1);
389 // len2=fread(Buffer, 1, 1, file);
390 // len2=Buffer[0];
391 // len =fread(Buffer, 1, len2, file);
392 // Buffer[len2]=0;
393 // dbgprintf("[Gammu - version %s]\n",Buffer);
394 len2=30000;
395 msg.Buffer = NULL;
396 while (len2==30000) {
397 len2=fread(Buffer, 1, 30000, file);
398 i=0;
399 while (i!=len2) {
400 if (Buffer[i++]==0x01) {
401 dbgprintf("Sending frame ");
402 sent = true;
403 } else {
404 dbgprintf("Receiving frame ");
405 sent = false;
407 type = Buffer[i++];
408 len = Buffer[i++] * 256;
409 len = len + Buffer[i++];
410 dbgprintf("0x%02x / 0x%04x", type, len);
411 DumpMessage(stdout, Buffer+i, len);
412 fflush(stdout);
413 if (s.Phone.Functions != NULL && !sent) {
414 msg.Buffer = (unsigned char *)realloc(msg.Buffer,len);
415 memcpy(msg.Buffer,Buffer+i,len);
416 msg.Type = type;
417 msg.Length = len;
418 s.Phone.Data.RequestMsg = &msg;
419 s.Phone.Functions->DispatchMessage(&s);
421 i = i + len;
427 #endif
429 /* How should editor hadle tabs in this file? Add editor commands here.
430 * vim: noexpandtab sw=8 ts=8 sts=8: