Translated using Weblate.
[gammu.git] / gammu / sniff.c
blob131a5ce7370c9fa900223e3911d2a5382c02b05c
1 /* (c) 2002-2003 by Marcin Wiacek */
3 #include "../libgammu/gsmstate.h"
5 #ifdef DEBUG
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include "../libgammu/protocol/nokia/mbus2.h"
12 #include "../libgammu/protocol/nokia/phonet.h"
13 #include "../libgammu/phone/nokia/nfunc.h"
14 #include "../libgammu/misc/misc.h"
15 #include "../libgammu/gsmcomon.h"
16 #include "../libgammu/service/gsmcal.h"
17 #include "gammu.h"
19 static GSM_Protocol_MBUS2Data MBUS2Data;
20 static GSM_Protocol_PHONETData PHONETData;
22 #define MBUS2_DEVICE_PC1 0x1D
24 static void DecodeInputMBUS2(unsigned char rx_byte)
26 GSM_Protocol_MBUS2Data *d = &MBUS2Data;
27 GSM_Debug_Info ldi = {DL_TEXTALL, stdout, FALSE, NULL, TRUE, FALSE, NULL, NULL};
29 d->Msg.CheckSum[0] = d->Msg.CheckSum[1];
30 d->Msg.CheckSum[1] ^= rx_byte;
32 if (d->MsgRXState == RX_GetMessage) {
33 d->Msg.Buffer[d->Msg.Count] = rx_byte;
34 d->Msg.Count++;
36 if (d->Msg.Count != d->Msg.Length+2) return;
38 if (d->Msg.CheckSum[0] != rx_byte) {
39 printf("[ERROR: checksum]\n");
40 printf(" 0x%02x / 0x%04lx", d->Msg.Type, (long)d->Msg.Length);
41 DumpMessage(&ldi, d->Msg.Buffer, d->Msg.Length);
42 d->MsgRXState = RX_Sync;
43 return;
46 if (d->Msg.Destination != MBUS2_DEVICE_PHONE) {
47 printf("Received frame");
48 } else {
49 printf("Sending frame");
51 printf(" 0x%02x / 0x%04lx", d->Msg.Type, (long)d->Msg.Length);
52 DumpMessage(&ldi, d->Msg.Buffer, d->Msg.Length);
53 if (d->Msg.Destination != MBUS2_DEVICE_PHONE) {
54 if (gsm->Phone.Functions != NULL) {
55 gsm->Phone.Data.RequestMsg = &d->Msg;
56 gsm->Phone.Functions->DispatchMessage(gsm);
59 d->MsgRXState = RX_Sync;
60 return;
62 if (d->MsgRXState == RX_GetLength2) {
63 if (d->Msg.Type == MBUS2_ACK_BYTE) {
64 d->MsgRXState = RX_Sync;
65 } else {
66 d->Msg.Length = d->Msg.Length + rx_byte;
67 d->MsgRXState = RX_GetMessage;
69 return;
71 if (d->MsgRXState == RX_GetLength1) {
72 d->Msg.Length = rx_byte * 256;
73 d->MsgRXState = RX_GetLength2;
74 return;
76 if (d->MsgRXState == RX_GetType) {
77 d->Msg.Type = rx_byte;
78 d->MsgRXState = RX_GetLength1;
79 return;
81 if (d->MsgRXState == RX_GetSource) {
82 if (rx_byte != MBUS2_DEVICE_PC && rx_byte != MBUS2_DEVICE_PHONE && rx_byte != MBUS2_DEVICE_PC1) {
83 d->MsgRXState = RX_Sync;
84 printf("[ERROR: incorrect char - %02x, not %02x and %02x and %02x]\n",
85 rx_byte, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC, MBUS2_DEVICE_PC1);
86 } else {
87 d->Msg.Source = rx_byte;
88 d->MsgRXState = RX_GetType;
90 return;
92 if (d->MsgRXState == RX_GetDestination) {
93 if (rx_byte != MBUS2_DEVICE_PC && rx_byte != MBUS2_DEVICE_PHONE && rx_byte != MBUS2_DEVICE_PC1) {
94 d->MsgRXState = RX_Sync;
95 printf("[ERROR: incorrect char - %02x, not %02x and %02x and %02x]\n",
96 rx_byte, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC, MBUS2_DEVICE_PC1);
97 } else {
98 d->Msg.Destination = rx_byte;
99 d->MsgRXState = RX_GetSource;
101 return;
103 if (d->MsgRXState == RX_Sync) {
104 if (rx_byte == MBUS2_FRAME_ID) {
105 d->Msg.CheckSum[1] = MBUS2_FRAME_ID;
106 d->Msg.Count = 0;
107 d->MsgRXState = RX_GetDestination;
108 } else {
109 printf("[ERROR: incorrect char - %02x, not %02x]\n", rx_byte, MBUS2_FRAME_ID);
114 #define PHONETIRDA_DEVICE_PC1 0x10
116 static void DecodeInputIRDA(unsigned char rx_byte)
118 GSM_Protocol_PHONETData *d = &PHONETData;
119 GSM_Debug_Info ldi = {DL_TEXTALL, stdout, FALSE, NULL, TRUE, FALSE, NULL, NULL};
121 if (d->MsgRXState == RX_GetMessage) {
122 d->Msg.Buffer[d->Msg.Count] = rx_byte;
123 d->Msg.Count++;
125 if (d->Msg.Count != d->Msg.Length) return;
127 if (d->Msg.Destination != PHONET_DEVICE_PHONE) {
128 printf("Received frame");
129 } else {
130 printf("Sending frame");
132 printf(" 0x%02x / 0x%04lx", d->Msg.Type, (long)d->Msg.Length);
133 DumpMessage(&ldi, d->Msg.Buffer, d->Msg.Length);
134 if (d->Msg.Destination != PHONET_DEVICE_PHONE) {
135 if (gsm->Phone.Functions != NULL) {
136 gsm->Phone.Data.RequestMsg = &d->Msg;
137 gsm->Phone.Functions->DispatchMessage(gsm);
140 d->MsgRXState = RX_Sync;
141 return;
143 if (d->MsgRXState == RX_GetLength2) {
144 d->Msg.Length = d->Msg.Length + rx_byte;
145 d->MsgRXState = RX_GetMessage;
146 return;
148 if (d->MsgRXState == RX_GetLength1) {
149 d->Msg.Length = rx_byte * 256;
150 d->MsgRXState = RX_GetLength2;
151 return;
153 if (d->MsgRXState == RX_GetType) {
154 d->Msg.Type = rx_byte;
155 d->MsgRXState = RX_GetLength1;
156 return;
158 if (d->MsgRXState == RX_GetSource) {
159 if (rx_byte != PHONET_DEVICE_PHONE && rx_byte != PHONETIRDA_DEVICE_PC1) {
160 d->MsgRXState = RX_Sync;
161 } else {
162 d->Msg.Source = rx_byte;
163 d->MsgRXState = RX_GetType;
165 return;
167 if (d->MsgRXState == RX_GetDestination) {
168 if (rx_byte != PHONETIRDA_DEVICE_PC1 && rx_byte != PHONET_DEVICE_PHONE) {
169 d->MsgRXState = RX_Sync;
170 } else {
171 d->Msg.Destination = rx_byte;
172 d->MsgRXState = RX_GetSource;
174 return;
176 if (d->MsgRXState == RX_Sync) {
177 if (rx_byte == PHONET_FRAME_ID) {
178 d->Msg.Count = 0;
179 d->MsgRXState = RX_GetDestination;
184 static char IMEI[50];
185 static GSM_DateTime DateTime;
186 static GSM_Alarm Alarm;
187 static GSM_MemoryEntry Memory;
188 static GSM_MemoryStatus MemoryStatus;
189 static GSM_SMSC SMSC;
190 static GSM_MultiSMSMessage GetSMSMessage;
191 static GSM_SMSMessage SaveSMSMessage;
192 static GSM_SMSMemoryStatus SMSStatus;
193 static GSM_SMSFolders SMSFolders;
194 static GSM_SignalQuality SignalQuality;
195 static GSM_BatteryCharge BatteryCharge;
196 static GSM_NetworkInfo NetworkInfo;
197 static GSM_Ringtone Ringtone;
198 static GSM_CalendarEntry Calendar;
199 static char SecurityCode;
200 static GSM_WAPBookmark WAPBookmark;
201 static GSM_Bitmap Bitmap;
202 static char PhoneString[500];
204 static char Model[50];
205 static char Version[50];
206 static double VersionNum;
208 static void prepareStateMachine(void)
210 GSM_Phone_Data *Phone = &(gsm->Phone.Data);
212 strcpy(Phone->IMEI, IMEI);
213 strcpy(Phone->Model, Model);
214 strcpy(Phone->Version, Version);
215 Phone->DateTime = &DateTime;
216 Phone->Alarm = &Alarm;
217 Phone->Memory = &Memory;
218 Phone->Memory->MemoryType = (GSM_MemoryType)MEM7110_CG;
219 Phone->MemoryStatus = &MemoryStatus;
220 Phone->SMSC = &SMSC;
221 Phone->GetSMSMessage = &GetSMSMessage;
222 Phone->SaveSMSMessage = &SaveSMSMessage;
223 Phone->SMSStatus = &SMSStatus;
224 Phone->SMSFolders = &SMSFolders;
225 Phone->SignalQuality = &SignalQuality;
226 Phone->BatteryCharge = &BatteryCharge;
227 Phone->NetworkInfo = &NetworkInfo;
228 Phone->Ringtone = &Ringtone;
229 Phone->Ringtone->Format = RING_NOKIABINARY;
230 Phone->Cal = &Calendar;
231 Phone->SecurityCode = &SecurityCode;
232 Phone->WAPBookmark = &WAPBookmark;
233 Phone->Bitmap = &Bitmap;
234 Phone->PhoneString = PhoneString;
235 Phone->StartPhoneString = 0;
237 Phone->EnableIncomingSMS = FALSE;
238 Phone->EnableIncomingCB = FALSE;
239 Model[0] = 0;
240 Phone->VerNum = VersionNum;
241 Version[0] = 0;
242 VersionNum = 0;
244 gsm->Phone.Functions = NULL;
245 gsm->User.UserReplyFunctions = NULL;
246 Phone->RequestID = ID_EachFrame;
249 void DecodeSniff(int argc, char *argv[])
251 GSM_ConnectionType Protocol = GCT_MBUS2;
252 unsigned char Buffer[65536]={'\0'},Buffer2[65536]={'\0'};
253 FILE *file;
254 int len=0, len2=0, pos=0, state=0, i=0;
255 unsigned char mybyte1 = 0,mybyte2;
256 GSM_Error error;
258 if (!strcmp(argv[2],"MBUS2")) {
259 Protocol = GCT_MBUS2;
260 } else if (!strcmp(argv[2],"IRDA")) {
261 Protocol = GCT_IRDAPHONET;
262 } else {
263 printf("What protocol (\"%s\") ?\n",argv[2]);
264 Terminate(2);
266 file = fopen(argv[3], "rb");
267 if (file == NULL) {
268 printf("Can not open file \"%s\"\n",argv[3]);
269 Terminate(2);
271 prepareStateMachine();
272 if (argc > 4) {
273 strcpy(gsm->CurrentConfig->Model,argv[4]);
274 error = GSM_RegisterAllPhoneModules(gsm);
275 if (error!=ERR_NONE) Print_Error(error);
277 /* Irda uses simple "raw" format */
278 if (Protocol == GCT_IRDAPHONET) {
279 PHONETData.MsgRXState=RX_Sync;
280 len2=30000;
281 while (len2==30000) {
282 len2=fread(Buffer, 1, 30000, file);
283 for (i=0;i<len2;i++) {
284 DecodeInputIRDA(Buffer[i]);
288 /* MBUS2 uses PortMon format */
289 if (Protocol == GCT_MBUS2) {
290 MBUS2Data.MsgRXState=RX_Sync;
291 len2=30000;
292 state=0;
294 while (len2==30000) {
295 len2=fread(Buffer, 1, 30000, file);
296 pos=0;
297 len=0;
299 while (pos!=len2) {
300 switch (state) {
301 case 0:
302 if (Buffer[pos]==' ') state = 1;
303 break;
304 case 1:
305 state = 2;
306 if (Buffer[pos]=='0') state = 0;
307 break;
308 case 2:
309 if (Buffer[pos]>='0' && Buffer[pos]<='9') {
310 state = 2;
311 } else {
312 if (Buffer[pos]==':') state = 3;
314 break;
315 case 3:
316 if (Buffer[pos]==' ') state = 4;
317 break;
318 case 4:
319 if (Buffer[pos]==13) {
320 state = 0;
321 break;
323 mybyte1=Buffer[pos]-'0';
324 if (Buffer[pos]>'9') mybyte1=Buffer[pos]-'A'+10;
325 state = 5;
326 break;
327 case 5:
328 mybyte2=Buffer[pos]-'0';
329 if (Buffer[pos]>'9') mybyte2=Buffer[pos]-'A'+10;
330 Buffer2[len++]=mybyte1*16+mybyte2;
331 state = 6;
332 break;
333 case 6:
334 state = 4;
335 if (Buffer[pos]!=' ') state = 0;
336 break;
338 pos++;
340 for (i=0;i<len;i++) {
341 DecodeInputMBUS2(Buffer2[i]);
345 fclose(file);
348 void DecodeBinaryDump(int argc, char *argv[])
350 FILE *file;
351 GSM_Protocol_Message msg;
352 GSM_Debug_Info ldi = {DL_TEXTALL, stdout, FALSE, NULL, TRUE, FALSE, NULL, NULL};
353 GSM_Error error;
354 unsigned char Buffer[65536]={'\0'},type=0;
355 int len=0, len2=0, i=0;
356 gboolean sent=FALSE;
358 prepareStateMachine();
360 if (argc > 3) {
361 strcpy(gsm->CurrentConfig->Model,argv[3]);
362 error = GSM_RegisterAllPhoneModules(gsm);
363 if (error!=ERR_NONE) Print_Error(error);
365 file = fopen(argv[2], "rb");
367 if (file == NULL) {
368 printf("Can not open file \"%s\"\n",argv[2]);
369 Terminate(3);
371 len2=30000;
372 msg.Buffer = NULL;
374 while (len2==30000) {
375 len2=fread(Buffer, 1, 30000, file);
376 i=0;
378 while (i!=len2) {
379 if (Buffer[i++]==0x01) {
380 smprintf(gsm, "Sending frame ");
381 sent = TRUE;
382 } else {
383 smprintf(gsm, "Receiving frame ");
384 sent = FALSE;
386 type = Buffer[i++];
387 len = Buffer[i++] * 256;
388 len = len + Buffer[i++];
389 smprintf(gsm, "0x%02x / 0x%04x", type, len);
390 DumpMessage(&ldi, Buffer+i, len);
392 if (gsm->Phone.Functions != NULL && !sent) {
393 msg.Buffer = (unsigned char *)realloc(msg.Buffer,len);
394 memcpy(msg.Buffer,Buffer+i,len);
395 msg.Type = type;
396 msg.Length = len;
397 gsm->Phone.Data.RequestMsg = &msg;
398 gsm->Phone.Functions->DispatchMessage(gsm);
400 i = i + len;
403 fclose(file);
406 #endif
408 /* How should editor hadle tabs in this file? Add editor commands here.
409 * vim: noexpandtab sw=8 ts=8 sts=8: