summaryrefslogtreecommitdiffabout
path: root/gammu/emb/gammu/sniff.c
blob: 0e3bc69699995e02ba43fb6170458af7b71fa779 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* (c) 2002-2003 by Marcin Wiacek */

#include "../common/gsmstate.h"

#ifdef DEBUG

#include <string.h>
#include <stdio.h>

#include "../common/protocol/nokia/mbus2.h"
#include "../common/protocol/nokia/phonet.h"
#include "../common/phone/nokia/nfunc.h"
#include "../common/misc/misc.h"
#include "../common/gsmcomon.h"
#include "../common/service/gsmcal.h"
#include "gammu.h"

static GSM_Protocol_MBUS2Data	MBUS2Data;
static GSM_Protocol_PHONETData	PHONETData;

#define MBUS2_DEVICE_PC1     0x1D

static void DecodeInputMBUS2(unsigned char rx_byte)
{
	GSM_Protocol_MBUS2Data *d = &MBUS2Data;

	d->Msg.CheckSum[0] = d->Msg.CheckSum[1];
	d->Msg.CheckSum[1] ^= rx_byte;

	if (d->MsgRXState == RX_GetMessage) {
		d->Msg.Buffer[d->Msg.Count] = rx_byte;
		d->Msg.Count++;

		if (d->Msg.Count != d->Msg.Length+2) return;

		if (d->Msg.CheckSum[0] != rx_byte) {
			printf("[ERROR: checksum]\n");
			printf(" 0x%02x / 0x%04x", d->Msg.Type, d->Msg.Length);
			DumpMessage(stdout, DL_TEXTALL, d->Msg.Buffer, d->Msg.Length);
			d->MsgRXState = RX_Sync;
			return;
		}

		if (d->Msg.Destination != MBUS2_DEVICE_PHONE) {
			printf("Received frame");
		} else {
			printf("Sending frame");
		}
		printf(" 0x%02x / 0x%04x", d->Msg.Type, d->Msg.Length);
		DumpMessage(stdout, DL_TEXTALL, d->Msg.Buffer, d->Msg.Length);
		if (d->Msg.Destination != MBUS2_DEVICE_PHONE) {
			if (s.Phone.Functions != NULL) {
				s.Phone.Data.RequestMsg = &d->Msg;
				s.Phone.Functions->DispatchMessage(&s);
			}
		}
		d->MsgRXState = RX_Sync;
		return;
	}
	if (d->MsgRXState == RX_GetLength2) {
		if (d->Msg.Type == MBUS2_ACK_BYTE) {
			d->MsgRXState = RX_Sync;
		} else {
			d->Msg.Length = d->Msg.Length + rx_byte;
			d->MsgRXState = RX_GetMessage;
		}
		return;
	}
	if (d->MsgRXState == RX_GetLength1) {
		d->Msg.Length = rx_byte * 256;
		d->MsgRXState = RX_GetLength2;
		return;
	}
	if (d->MsgRXState == RX_GetType) {
		d->Msg.Type 	= rx_byte;
		d->MsgRXState 	= RX_GetLength1;
		return;
	}
	if (d->MsgRXState == RX_GetSource) {
		if (rx_byte != MBUS2_DEVICE_PC && rx_byte != MBUS2_DEVICE_PHONE && rx_byte != MBUS2_DEVICE_PC1) {
			d->MsgRXState = RX_Sync;
			printf("[ERROR: incorrect char - %02x, not %02x and %02x and %02x]\n",
				rx_byte, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC, MBUS2_DEVICE_PC1);
		} else {
			d->Msg.Source = rx_byte;
			d->MsgRXState = RX_GetType;
		}
		return;
	}
	if (d->MsgRXState == RX_GetDestination) {
		if (rx_byte != MBUS2_DEVICE_PC && rx_byte != MBUS2_DEVICE_PHONE && rx_byte != MBUS2_DEVICE_PC1) {
			d->MsgRXState = RX_Sync;
			printf("[ERROR: incorrect char - %02x, not %02x and %02x and %02x]\n",
				rx_byte, MBUS2_DEVICE_PHONE, MBUS2_DEVICE_PC, MBUS2_DEVICE_PC1);
		} else {
			d->Msg.Destination 	= rx_byte;
			d->MsgRXState 		= RX_GetSource;
		}
		return;
	}
	if (d->MsgRXState == RX_Sync) {
		if (rx_byte == MBUS2_FRAME_ID) {
			d->Msg.CheckSum[1] 	= MBUS2_FRAME_ID;
			d->Msg.Count 		= 0;
			d->MsgRXState 		= RX_GetDestination;	
		} else {
			printf("[ERROR: incorrect char - %02x, not %02x]\n", rx_byte, MBUS2_FRAME_ID);
		}
	}
}

#define PHONETIRDA_DEVICE_PC1     0x10

static void DecodeInputIRDA(unsigned char rx_byte)
{
	GSM_Protocol_PHONETData *d = &PHONETData;

	if (d->MsgRXState == RX_GetMessage) {
		d->Msg.Buffer[d->Msg.Count] = rx_byte;
		d->Msg.Count++;

		if (d->Msg.Count != d->Msg.Length) return;

		if (d->Msg.Destination != PHONET_DEVICE_PHONE) {
			printf("Received frame");
		} else {
			printf("Sending frame");
		}
		printf(" 0x%02x / 0x%04x", d->Msg.Type, d->Msg.Length);
		DumpMessage(stdout, DL_TEXTALL, d->Msg.Buffer, d->Msg.Length);
		if (d->Msg.Destination != PHONET_DEVICE_PHONE) {
			if (s.Phone.Functions != NULL) {
				s.Phone.Data.RequestMsg = &d->Msg;
				s.Phone.Functions->DispatchMessage(&s);
			}
		}
		d->MsgRXState = RX_Sync;
		return;
	}
	if (d->MsgRXState == RX_GetLength2) {
		d->Msg.Length = d->Msg.Length + rx_byte;
		d->MsgRXState = RX_GetMessage;
		return;
	}
	if (d->MsgRXState == RX_GetLength1) {
		d->Msg.Length = rx_byte * 256;
		d->MsgRXState = RX_GetLength2;
		return;
	}
	if (d->MsgRXState == RX_GetType) {
		d->Msg.Type 	= rx_byte;
		d->MsgRXState 	= RX_GetLength1;
		return;
	}
	if (d->MsgRXState == RX_GetSource) {
		if (rx_byte != PHONET_DEVICE_PHONE && rx_byte != PHONETIRDA_DEVICE_PC1) {
			d->MsgRXState = RX_Sync;
		} else {
			d->Msg.Source = rx_byte;
			d->MsgRXState = RX_GetType;
		}
		return;
	}
	if (d->MsgRXState == RX_GetDestination) {
		if (rx_byte != PHONETIRDA_DEVICE_PC1 && rx_byte != PHONET_DEVICE_PHONE) {
			d->MsgRXState = RX_Sync;
		} else {
			d->Msg.Destination 	= rx_byte;
			d->MsgRXState 		= RX_GetSource;
		}
		return;
	}
	if (d->MsgRXState == RX_Sync) {
		if (rx_byte == PHONET_FRAME_ID) {
			d->Msg.Count = 0;
			d->MsgRXState = RX_GetDestination;	
		}
	}
}

static char				IMEI[50];
static GSM_DateTime			DateTime;
static GSM_Alarm			Alarm;
static GSM_MemoryEntry			Memory;
static GSM_MemoryStatus			MemoryStatus;
static GSM_SMSC				SMSC;
static GSM_MultiSMSMessage		GetSMSMessage;
static GSM_SMSMessage			SaveSMSMessage;
static GSM_SMSMemoryStatus		SMSStatus;
static GSM_SMSFolders			SMSFolders;
static GSM_SignalQuality		SignalQuality;
static GSM_BatteryCharge		BatteryCharge;
static GSM_NetworkInfo			NetworkInfo;
static GSM_Ringtone			Ringtone;
static GSM_CalendarEntry		Calendar;
static char				SecurityCode;
static GSM_WAPBookmark			WAPBookmark;
static GSM_Bitmap			Bitmap;
static char				PhoneString[500];

static char				Model[50];
static char				Version[50];
static double				VersionNum;

static void prepareStateMachine()
{
	GSM_Phone_Data	*Phone = &s.Phone.Data;

	strcpy(Phone->IMEI,		IMEI);
	strcpy(Phone->Model,		Model);
	strcpy(Phone->Version,		Version);
	Phone->DateTime			= &DateTime;
	Phone->Alarm			= &Alarm;
	Phone->Memory			= &Memory;
	Phone->Memory->MemoryType	= MEM7110_CG;
	Phone->MemoryStatus		= &MemoryStatus;
	Phone->SMSC			= &SMSC;
	Phone->GetSMSMessage		= &GetSMSMessage;
	Phone->SaveSMSMessage		= &SaveSMSMessage;
	Phone->SMSStatus		= &SMSStatus;
	Phone->SMSFolders		= &SMSFolders;
	Phone->SignalQuality		= &SignalQuality;
	Phone->BatteryCharge		= &BatteryCharge;
	Phone->NetworkInfo		= &NetworkInfo;
	Phone->Ringtone			= &Ringtone;
	Phone->Ringtone->Format		= RING_NOKIABINARY;
	Phone->Cal			= &Calendar;
	Phone->SecurityCode		= &SecurityCode;
	Phone->WAPBookmark		= &WAPBookmark;
	Phone->Bitmap			= &Bitmap;
	Phone->PhoneString		= PhoneString;
	Phone->StartPhoneString 	= 0;

	Phone->EnableIncomingSMS 	= false;
	Phone->EnableIncomingCB 	= false;
	Model[0]			= 0;
	Phone->VerNum			= VersionNum;
	Version[0]			= 0;
	VersionNum			= 0;

	s.Phone.Functions		= NULL;
	s.User.UserReplyFunctions	= NULL;
	Phone->RequestID		= ID_EachFrame;
}

void decodesniff(int argc, char *argv[])
{
	GSM_ConnectionType	Protocol = GCT_MBUS2;
	unsigned char 		Buffer[50000];
	unsigned char 		Buffer2[50000];
	FILE			*file;
	int			len, len2, pos, state, i;
	unsigned char		mybyte1 = 0,mybyte2;

	if (!strcmp(argv[2],"MBUS2")) {
		Protocol = GCT_MBUS2;
    	} else if (!strcmp(argv[2],"IRDA")) {
		Protocol = GCT_IRDAPHONET;
	} else {
		printf("What protocol (\"%s\") ?\n",argv[2]);
		exit(-1);
	}
	file = fopen(argv[3], "rb");
	if (!file) {
		printf("Can not open file \"%s\"\n",argv[3]);
		exit(-1);
	}
	prepareStateMachine();
	if (argc > 4) {
		strcpy(s.CurrentConfig->Model,argv[4]);
		error=GSM_RegisterAllPhoneModules(&s);
		if (error!=ERR_NONE) Print_Error(error);
	}
	/* Irda uses simple "raw" format */
	if (Protocol == GCT_IRDAPHONET) {
		PHONETData.MsgRXState=RX_Sync;
		len2=30000;
		while (len2==30000) {	
			len2=fread(Buffer, 1, 30000, file);
			for (i=0;i<len2;i++) {
				DecodeInputIRDA(Buffer[i]);
			}
		}
	}
	/* MBUS2 uses PortMon format */
	if (Protocol == GCT_MBUS2) {
		MBUS2Data.MsgRXState=RX_Sync;
		len2=30000;
		state=0;
		while (len2==30000) {	
			len2=fread(Buffer, 1, 30000, file);
			pos=0;
			len=0;
			while (pos!=len2) {
				switch (state) {
				case 0:
					if (Buffer[pos]==' ') state = 1;
					break;
				case 1:
					state = 2;
					if (Buffer[pos]=='0') state = 0;
					break;
				case 2:
					if (Buffer[pos]>='0' && Buffer[pos]<='9') {
						state = 2;
					} else {
						if (Buffer[pos]==':') state = 3;
					}
					break;
				case 3:
					if (Buffer[pos]==' ') state = 4;
					break;
				case 4:
					if (Buffer[pos]==13) {
						state = 0;
						break;
					}
					mybyte1=Buffer[pos]-'0';
					if (Buffer[pos]>'9') mybyte1=Buffer[pos]-'A'+10;
					state = 5;
					break;
				case 5:
					mybyte2=Buffer[pos]-'0';
					if (Buffer[pos]>'9') mybyte2=Buffer[pos]-'A'+10;
					Buffer2[len++]=mybyte1*16+mybyte2;
					state = 6;
					break;
				case 6:
					state = 4;
					if (Buffer[pos]!=' ') state = 0;
					break;
				}
				pos++;		
			}	
			for (i=0;i<len;i++) {
				DecodeInputMBUS2(Buffer2[i]);
			}
		}
	}
	fclose(file);
}

void decodebinarydump(int argc, char *argv[])
{
	unsigned char 		Buffer[50000];
	FILE			*file;
	int			len, len2, i;
	unsigned char		type;
	bool			sent;
	GSM_Protocol_Message	msg;

	prepareStateMachine();
	if (argc > 3) {
		strcpy(s.CurrentConfig->Model,argv[3]);
		error=GSM_RegisterAllPhoneModules(&s);
		if (error!=ERR_NONE) Print_Error(error);
	}
	file = fopen(argv[2], "rb");
	if (!file) {
		printf("Can not open file \"%s\"\n",argv[2]);
		exit(-1);
	}
//	len2=fread(Buffer, 1, 1, file);
//	len2=Buffer[0];
//	len =fread(Buffer, 1, len2, file);
//	Buffer[len2]=0;
//	dbgprintf("[Gammu            - version %s]\n",Buffer);
	len2=30000;
	msg.Buffer = NULL;
	while (len2==30000) {	
		len2=fread(Buffer, 1, 30000, file);
		i=0;
		while (i!=len2) {
			if (Buffer[i++]==0x01) {
				dbgprintf("Sending frame ");
				sent = true;
			} else {
				dbgprintf("Receiving frame ");
				sent = false;
			}
			type 	= Buffer[i++];
			len 	= Buffer[i++] * 256;
			len 	= len + Buffer[i++];
			dbgprintf("0x%02x / 0x%04x", type, len);
			DumpMessage(stdout, DL_TEXTALL, Buffer+i, len);
			fflush(stdout);
			if (s.Phone.Functions != NULL && !sent) {
				msg.Buffer = (unsigned char *)realloc(msg.Buffer,len);
				memcpy(msg.Buffer,Buffer+i,len);
				msg.Type		= type;
				msg.Length		= len;
				s.Phone.Data.RequestMsg = &msg;
				s.Phone.Functions->DispatchMessage(&s);
			}
			i = i + len;
		}
	}

}

#endif

/* How should editor hadle tabs in this file? Add editor commands here.
 * vim: noexpandtab sw=8 ts=8 sts=8:
 */