1 /* (c) 2002-2003 by Marcin Wiacek */
3 #include "../libgammu/gsmstate.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"
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
;
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
;
46 if (d
->Msg
.Destination
!= MBUS2_DEVICE_PHONE
) {
47 printf("Received frame");
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
;
62 if (d
->MsgRXState
== RX_GetLength2
) {
63 if (d
->Msg
.Type
== MBUS2_ACK_BYTE
) {
64 d
->MsgRXState
= RX_Sync
;
66 d
->Msg
.Length
= d
->Msg
.Length
+ rx_byte
;
67 d
->MsgRXState
= RX_GetMessage
;
71 if (d
->MsgRXState
== RX_GetLength1
) {
72 d
->Msg
.Length
= rx_byte
* 256;
73 d
->MsgRXState
= RX_GetLength2
;
76 if (d
->MsgRXState
== RX_GetType
) {
77 d
->Msg
.Type
= rx_byte
;
78 d
->MsgRXState
= RX_GetLength1
;
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
);
87 d
->Msg
.Source
= rx_byte
;
88 d
->MsgRXState
= RX_GetType
;
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
);
98 d
->Msg
.Destination
= rx_byte
;
99 d
->MsgRXState
= RX_GetSource
;
103 if (d
->MsgRXState
== RX_Sync
) {
104 if (rx_byte
== MBUS2_FRAME_ID
) {
105 d
->Msg
.CheckSum
[1] = MBUS2_FRAME_ID
;
107 d
->MsgRXState
= RX_GetDestination
;
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
;
125 if (d
->Msg
.Count
!= d
->Msg
.Length
) return;
127 if (d
->Msg
.Destination
!= PHONET_DEVICE_PHONE
) {
128 printf("Received frame");
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
;
143 if (d
->MsgRXState
== RX_GetLength2
) {
144 d
->Msg
.Length
= d
->Msg
.Length
+ rx_byte
;
145 d
->MsgRXState
= RX_GetMessage
;
148 if (d
->MsgRXState
== RX_GetLength1
) {
149 d
->Msg
.Length
= rx_byte
* 256;
150 d
->MsgRXState
= RX_GetLength2
;
153 if (d
->MsgRXState
== RX_GetType
) {
154 d
->Msg
.Type
= rx_byte
;
155 d
->MsgRXState
= RX_GetLength1
;
158 if (d
->MsgRXState
== RX_GetSource
) {
159 if (rx_byte
!= PHONET_DEVICE_PHONE
&& rx_byte
!= PHONETIRDA_DEVICE_PC1
) {
160 d
->MsgRXState
= RX_Sync
;
162 d
->Msg
.Source
= rx_byte
;
163 d
->MsgRXState
= RX_GetType
;
167 if (d
->MsgRXState
== RX_GetDestination
) {
168 if (rx_byte
!= PHONETIRDA_DEVICE_PC1
&& rx_byte
!= PHONET_DEVICE_PHONE
) {
169 d
->MsgRXState
= RX_Sync
;
171 d
->Msg
.Destination
= rx_byte
;
172 d
->MsgRXState
= RX_GetSource
;
176 if (d
->MsgRXState
== RX_Sync
) {
177 if (rx_byte
== PHONET_FRAME_ID
) {
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
;
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
;
240 Phone
->VerNum
= VersionNum
;
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'};
254 int len
=0, len2
=0, pos
=0, state
=0, i
=0;
255 unsigned char mybyte1
= 0,mybyte2
;
258 if (!strcmp(argv
[2],"MBUS2")) {
259 Protocol
= GCT_MBUS2
;
260 } else if (!strcmp(argv
[2],"IRDA")) {
261 Protocol
= GCT_IRDAPHONET
;
263 printf("What protocol (\"%s\") ?\n",argv
[2]);
266 file
= fopen(argv
[3], "rb");
268 printf("Can not open file \"%s\"\n",argv
[3]);
271 prepareStateMachine();
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
;
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
;
294 while (len2
==30000) {
295 len2
=fread(Buffer
, 1, 30000, file
);
302 if (Buffer
[pos
]==' ') state
= 1;
306 if (Buffer
[pos
]=='0') state
= 0;
309 if (Buffer
[pos
]>='0' && Buffer
[pos
]<='9') {
312 if (Buffer
[pos
]==':') state
= 3;
316 if (Buffer
[pos
]==' ') state
= 4;
319 if (Buffer
[pos
]==13) {
323 mybyte1
=Buffer
[pos
]-'0';
324 if (Buffer
[pos
]>'9') mybyte1
=Buffer
[pos
]-'A'+10;
328 mybyte2
=Buffer
[pos
]-'0';
329 if (Buffer
[pos
]>'9') mybyte2
=Buffer
[pos
]-'A'+10;
330 Buffer2
[len
++]=mybyte1
*16+mybyte2
;
335 if (Buffer
[pos
]!=' ') state
= 0;
340 for (i
=0;i
<len
;i
++) {
341 DecodeInputMBUS2(Buffer2
[i
]);
348 void DecodeBinaryDump(int argc
, char *argv
[])
351 GSM_Protocol_Message msg
;
352 GSM_Debug_Info ldi
= {DL_TEXTALL
, stdout
, FALSE
, NULL
, TRUE
, FALSE
, NULL
, NULL
};
354 unsigned char Buffer
[65536]={'\0'},type
=0;
355 int len
=0, len2
=0, i
=0;
358 prepareStateMachine();
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");
368 printf("Can not open file \"%s\"\n",argv
[2]);
374 while (len2
==30000) {
375 len2
=fread(Buffer
, 1, 30000, file
);
379 if (Buffer
[i
++]==0x01) {
380 smprintf(gsm
, "Sending frame ");
383 smprintf(gsm
, "Receiving frame ");
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
);
397 gsm
->Phone
.Data
.RequestMsg
= &msg
;
398 gsm
->Phone
.Functions
->DispatchMessage(gsm
);
408 /* How should editor hadle tabs in this file? Add editor commands here.
409 * vim: noexpandtab sw=8 ts=8 sts=8: