5 A Linux/Unix toolset and driver for Nokia mobile phones.
7 Released under the terms of the GNU GPL, see file COPYING for more details.
9 This file provides an API for support for fbusirda protocol
13 /* "Turn on" prototypes in fbusirda.h */
16 /* System header files */
24 #include "misc_win32.h"
29 /* Various header file */
30 #include "devices/device.h"
32 #include "protocol/fbusirda.h"
33 #include "protocol/fbus.h"
36 GSM_Protocol FBUSIRDA_Functions = {
42 FBUSIRDA_RX_StateMachine
46 enum FBUS_RX_States RX_State;
48 u8 MessageDestination, MessageSource;
56 u8 MessageBuffer[FBUS_MAX_RECEIVE_LENGTH * 6];
59 char *FBUSIRDA_PrintDevice(int Device)
63 case FBUS_DEVICE_PHONE:
75 /* FBUSIRDA_RX_DisplayMessage is called when a message we don't know about is
76 received so that the user can see what is going back and forth, and perhaps
77 shed some more light/explain another message type! */
78 void FBUSIRDA_RX_DisplayMessage()
80 fprintf(stdout, _("Msg Source: %s\n"), FBUSIRDA_PrintDevice(MessageSource));
81 fprintf(stdout, _("Msg Dest: %s\n"), FBUSIRDA_PrintDevice(MessageDestination));
82 fprintf(stdout, _("Msg Type: %02x\n"), MessageType);
84 hexdump( MessageLength, MessageBuffer);
88 /* Prepares the message header and sends it, prepends the message start byte
89 (0x1e) and other values according the value specified when called.
90 Calculates checksum and then sends the lot down the pipe... */
91 int FBUSIRDA_SendFrame(u16 message_length, u8 message_type, u8 *buffer) {
92 u8 out_buffer[FBUSIRDA_MAX_CONTENT_LENGTH + 2];
97 /* FIXME - we should check for the message length ... */
99 /* Now construct the message header. */
101 out_buffer[current++] = FBUSIRDA_FRAME_ID; /* Start of the frame indicator */
103 out_buffer[current++] = FBUS_DEVICE_PHONE; /* Destination */
105 out_buffer[current++] = FBUS_DEVICE_PC; /* Source */
107 out_buffer[current++] = message_type; /* Type */
109 out_buffer[current++] = message_length/256; /* Length1 */
110 out_buffer[current++] = message_length%256; /* Length2 */
112 /* Copy in data if any. */
114 if (message_length != 0) {
115 memcpy(out_buffer + current, buffer, message_length);
117 current+=message_length;
121 NULL_TX_DisplayMessage( current, out_buffer);
125 return ( FBUSIRDA_WritePhone(current,out_buffer) - header);
129 int FBUSIRDA_SendMessage(u16 message_length, u8 message_type, u8 *buffer) {
131 u8 frame_buffer[FBUSIRDA_MAX_CONTENT_LENGTH + 2];
134 memcpy(frame_buffer, buffer, message_length);
135 ret = FBUSIRDA_SendFrame(message_length, message_type, frame_buffer);
137 if (ret != message_length)
139 printf("CAUTION: FBUSIRDA_SendFrame return written %d bytes of %d !\n",ret, message_length);
146 int FBUSIRDA_SendAck(u8 message_type, u8 message_seq) {
150 /* Applications should call MBUS_Terminate to shut down the MBUS thread and
151 close the serial port. */
152 void FBUSIRDA_Terminate(void)
154 /* Request termination of thread */
155 CurrentRequestTerminate = true;
157 /* Close serial port. */
161 /* RX_State machine for receive handling. Called once for each character
162 received from the phone/phone. */
163 void FBUSIRDA_RX_StateMachine(unsigned char rx_byte) {
173 case FBUS_RX_Discarding:
176 /* Messages from the phone start with an 0x14 (IRDA, Nokia:PhoNet).
177 We use this to "synchronise" with the incoming data stream. */
181 if (rx_byte == FBUSIRDA_FRAME_ID) {
183 RX_State = FBUS_RX_GetDestination;
188 case FBUS_RX_GetDestination:
190 MessageDestination=rx_byte;
191 RX_State = FBUS_RX_GetSource;
193 /* When there is a checksum error and things get out of sync we have to manage to resync */
194 /* If doing a data call at the time, finding a 0x1e etc is really quite likely in the data stream */
195 /* Then all sorts of horrible things happen because the packet length etc is wrong... */
196 /* Therefore we test here for a destination of 0x0c and return to the top if it is not */
199 if (strstr(GSM_Info->IrdaModels,"decode")!=NULL)
201 if (rx_byte!=FBUS_DEVICE_PC && rx_byte!=FBUS_DEVICE_PHONE )
203 RX_State=FBUS_RX_Sync;
204 fprintf(stdout,"decode: expected FBUS_DEVICE_PC/FBUS_DEVICE_PHONE, got %2x\n",rx_byte);
210 if (rx_byte!=FBUS_DEVICE_PC)
212 RX_State=FBUS_RX_Sync;
214 fprintf(stdout,"The FBUSIRDA stream is out of sync - expected FBUS_DEVICE_PC, got %2x\n",rx_byte);
223 case FBUS_RX_GetSource:
225 MessageSource=rx_byte;
226 RX_State = FBUS_RX_GetType;
229 if (strstr(GSM_Info->IrdaModels,"decode")!=NULL)
231 if (rx_byte!=FBUS_DEVICE_PC && rx_byte!=FBUS_DEVICE_PHONE )
233 RX_State=FBUS_RX_Sync;
234 fprintf(stdout,"decode: expected FBUS_DEVICE_PC/FBUS_DEVICE_PHONE, got %2x\n",rx_byte);
240 if (rx_byte!=FBUS_DEVICE_PHONE)
242 RX_State=FBUS_RX_Sync;
244 fprintf(stdout,"The FBUSIRDA stream is out of sync - expected FBUS_DEVICE_PHONE, got %2x\n",rx_byte);
253 case FBUS_RX_GetType:
256 RX_State = FBUS_RX_GetLength1;
260 case FBUS_RX_GetLength1:
264 /* MW:Here are problems with conversion. For chars 0-127 it's OK, for
265 higher not (probably because rx_byte is char type) - improtant
266 for MBUS. So, I make it double and strange - generally it should be
267 more simple and make simple convert rx_byte into MessageLength */
270 MessageLength=max*256;
273 RX_State = FBUS_RX_GetLength2;
277 case FBUS_RX_GetLength2:
279 /* MW:Here are problems with conversion. For chars 0-127 it's OK, for
280 higher not (probably because rx_byte is char type) - important
281 for MBUS. So, I make it double and strange - generally it should be
282 more simple and make simple convert rx_byte into MessageLength */
283 #if defined(__svr4__) || defined(__FreeBSD__)
285 for (i=0;i<rx_byte;i++)
286 MessageLength=MessageLength++;
291 MessageLength=MessageLength+max;
295 RX_State = FBUS_RX_GetMessage;
299 case FBUS_RX_GetMessage:
301 MessageBuffer[BufferCount] = rx_byte;
304 if (BufferCount == MessageLength)
306 if (MessageSource == FBUS_DEVICE_PHONE)
309 FBUSIRDA_RX_DisplayMessage();
311 GSM->DispatchMessage(MessageLength, MessageBuffer, MessageType);
316 /* for --decodefile */
317 fprintf(stdout, _("Msg Source: %s\n"), FBUSIRDA_PrintDevice(MessageSource));
318 fprintf(stdout, _("Msg Dest: %s\n"), FBUSIRDA_PrintDevice(MessageDestination));
319 fprintf(stdout, _("Msg Type: %02x\n"), MessageType);
320 hexdump( MessageLength, MessageBuffer);
324 RX_State = FBUS_RX_Sync;
330 /* Called by initialisation code to open comm port in asynchronous mode. */
331 bool FBUSIRDA_OpenSerial(void)
334 fprintf(stdout, _("Setting FBUS Irda communication...\n"));
341 /* Initialise variables and state machine. */
342 GSM_Error FBUSIRDA_Initialise(char *port_device, char *initlength,
343 GSM_ConnectionType connection,
344 void (*rlp_callback)(RLP_F96Frame *frame))
346 CurrentConnectionType = connection;
348 if (!StartConnection(port_device,false,connection)) {
349 return GE_INTERNALERROR;
351 RX_State=FBUS_RX_Sync;
356 bool FBUSIRDA_WritePhone (u16 length, u8 *buffer) {
358 if (!CurrentDisableKeepAlive)
361 return (device_write(buffer,length));