2 #include "../common/gsmstate.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"
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
) {
33 if (rx_byte
== MBUS2_FRAME_ID
) {
34 d
->Msg
.CheckSum
[1] = MBUS2_FRAME_ID
;
36 d
->MsgRXState
= RX_GetDestination
;
38 printf("[ERROR: incorrect char - %02x, not %02x]\n", rx_byte
, MBUS2_FRAME_ID
);
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
);
49 d
->Msg
.Destination
= rx_byte
;
50 d
->MsgRXState
= 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
);
61 d
->Msg
.Source
= rx_byte
;
62 d
->MsgRXState
= RX_GetType
;
68 d
->Msg
.Type
= rx_byte
;
69 d
->MsgRXState
= RX_GetLength1
;
74 d
->Msg
.Length
= rx_byte
* 256;
75 d
->MsgRXState
= RX_GetLength2
;
80 if (d
->Msg
.Type
== MBUS2_ACK_BYTE
)
82 d
->MsgRXState
= RX_Sync
;
84 d
->Msg
.Length
= d
->Msg
.Length
+ rx_byte
;
85 d
->MsgRXState
= RX_GetMessage
;
91 d
->Msg
.Buffer
[d
->Msg
.Count
] = rx_byte
;
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");
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
);
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
;
122 #define PHONETIRDA_DEVICE_PC1 0x10
124 static void DecodeInputIRDA(unsigned char rx_byte
)
126 GSM_Protocol_PHONETData
*d
= &PHONETData
;
128 switch (d
->MsgRXState
) {
132 if (rx_byte
== PHONET_FRAME_ID
) {
134 d
->MsgRXState
= RX_GetDestination
;
137 // printf("[ERROR: incorrect char - %02x, not %02x]\n", rx_byte, FBUS2IRDA_FRAME_ID);
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);
147 d
->Msg
.Destination
= rx_byte
;
148 d
->MsgRXState
= 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);
158 d
->Msg
.Source
= rx_byte
;
159 d
->MsgRXState
= RX_GetType
;
165 d
->Msg
.Type
= rx_byte
;
166 d
->MsgRXState
= RX_GetLength1
;
171 d
->Msg
.Length
= rx_byte
* 256;
172 d
->MsgRXState
= RX_GetLength2
;
177 d
->Msg
.Length
= d
->Msg
.Length
+ rx_byte
;
178 d
->MsgRXState
= RX_GetMessage
;
183 d
->Msg
.Buffer
[d
->Msg
.Count
] = rx_byte
;
186 if (d
->Msg
.Count
== d
->Msg
.Length
) {
187 if (d
->Msg
.Destination
!= PHONET_DEVICE_PHONE
) {
188 printf("Received frame");
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
;
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
;
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;
263 Phone
->VerNum
= VersionNum
;
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];
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
;
286 printf("What protocol (\"%s\") ?\n",argv
[2]);
289 file
= fopen(argv
[3], "rb");
291 printf("Can not open file \"%s\"\n",argv
[3]);
294 prepareStateMachine();
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
;
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
;
316 while (len2
==30000) {
317 len2
=fread(Buffer
, 1, 30000, file
);
323 if (Buffer
[pos
]==' ') state
= 1;
327 if (Buffer
[pos
]=='0') state
= 0;
330 if (Buffer
[pos
]>='0' && Buffer
[pos
]<='9') {
333 if (Buffer
[pos
]==':') state
= 3;
337 if (Buffer
[pos
]==' ') state
= 4;
340 if (Buffer
[pos
]==13) {
344 mybyte1
=Buffer
[pos
]-'0';
345 if (Buffer
[pos
]>'9') mybyte1
=Buffer
[pos
]-'A'+10;
349 mybyte2
=Buffer
[pos
]-'0';
350 if (Buffer
[pos
]>'9') mybyte2
=Buffer
[pos
]-'A'+10;
351 Buffer2
[len
++]=mybyte1
*16+mybyte2
;
356 if (Buffer
[pos
]!=' ') state
= 0;
361 for (i
=0;i
<len
;i
++) {
362 DecodeInputMBUS2(Buffer2
[i
]);
369 void decodebinarydump(int argc
, char *argv
[])
371 unsigned char Buffer
[50000];
376 GSM_Protocol_Message msg
;
378 prepareStateMachine();
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");
386 printf("Can not open file \"%s\"\n",argv
[2]);
389 // len2=fread(Buffer, 1, 1, file);
391 // len =fread(Buffer, 1, len2, file);
393 // dbgprintf("[Gammu - version %s]\n",Buffer);
396 while (len2
==30000) {
397 len2
=fread(Buffer
, 1, 30000, file
);
400 if (Buffer
[i
++]==0x01) {
401 dbgprintf("Sending frame ");
404 dbgprintf("Receiving frame ");
408 len
= Buffer
[i
++] * 256;
409 len
= len
+ Buffer
[i
++];
410 dbgprintf("0x%02x / 0x%04x", type
, len
);
411 DumpMessage(stdout
, Buffer
+i
, len
);
413 if (s
.Phone
.Functions
!= NULL
&& !sent
) {
414 msg
.Buffer
= (unsigned char *)realloc(msg
.Buffer
,len
);
415 memcpy(msg
.Buffer
,Buffer
+i
,len
);
418 s
.Phone
.Data
.RequestMsg
= &msg
;
419 s
.Phone
.Functions
->DispatchMessage(&s
);
429 /* How should editor hadle tabs in this file? Add editor commands here.
430 * vim: noexpandtab sw=8 ts=8 sts=8: