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 AT protocol
13 /* "Turn on" prototypes in AT.h */
16 /* System header files */
23 #include "misc_win32.h"
28 /* Various header file */
29 #include "devices/device.h"
31 #include "protocol/at.h"
33 GSM_Protocol AT_Functions = {
47 u8 MessageBuffer[AT_MAX_RECEIVE_LENGTH * 6];
49 static bool wascrlf = false;
50 static char *linestart = MessageBuffer;
52 /* N61_RX_DisplayMessage is called when a message we don't know about is
53 received so that the user can see what is going back and forth, and perhaps
54 shed some more light/explain another message type! */
57 void AT_RX_DisplayMessage()
59 fprintf(stdout, _("Phone\n"));
61 hexdump(MessageLength,MessageBuffer);
65 /* Prepares the message header and sends it, prepends the message start byte
66 (0x1e) and other values according the value specified when called.
67 Calculates checksum and then sends the lot down the pipe... */
68 int AT_SendFrame(u16 message_length, u8 message_type, u8 *buffer) {
74 /* FIXME - we should check for the message length ... */
77 fprintf(stdout, _("PC: "));
79 for (count = 0; count < message_length-1; count++)
80 fprintf(stdout, "%c", buffer[count]);
82 fprintf(stdout, "\n");
86 if (!AT_WritePhone(message_length,buffer))
92 int AT_SendMessage(u16 message_length, u8 message_type, u8 *buffer) {
94 AT_SendFrame(message_length, message_type, buffer);
99 int AT_SendAck(u8 message_type, u8 message_seq) {
104 /* Applications should call AT_Terminate to shut down the AT thread and
105 close the serial port. */
106 void AT_Terminate(void)
108 /* Request termination of thread */
109 CurrentRequestTerminate = true;
111 /* Close serial port. */
115 /* RX_State machine for receive handling. Called once for each character
116 received from the phone/phone. */
118 void AT_RX_StateMachine(unsigned char rx_byte) {
120 /* FIXME: Check for overflow */
121 MessageBuffer[BufferCount] = rx_byte;
122 MessageBuffer[BufferCount + 1] = '\0';
124 // fprintf(stdout,_("%02x %c\n"),rx_byte,rx_byte);
126 if (wascrlf && (rx_byte != 10 && rx_byte != 13)) {
127 linestart = MessageBuffer + BufferCount;
131 if ((rx_byte== 10 || rx_byte==13)) wascrlf = true;
133 if ((BufferCount > 0) && (10 == MessageBuffer[BufferCount])
134 && (13 == MessageBuffer[BufferCount-1])
135 && ((0 == strncmp("OK", linestart, 2))
136 || (0 == strncmp("ERROR", linestart, 5))
137 || (0 == strncmp("+CME ERROR:", linestart, 11))
138 || (0 == strncmp("+CMS ERROR:", linestart, 11)))) {
140 MessageLength=BufferCount;
143 AT_RX_DisplayMessage();
146 GSM->DispatchMessage(MessageLength,MessageBuffer, 0x00);
149 linestart = MessageBuffer;
152 } else BufferCount++;
155 /* Called by initialisation code to open comm port in asynchronous mode. */
156 bool AT_OpenSerial(void)
158 // device_changespeed(115200);
160 device_changespeed(19200);
165 /* Initialise variables and state machine. */
166 GSM_Error AT_Initialise(char *port_device, char *initlength,
167 GSM_ConnectionType connection,
168 void (*rlp_callback)(RLP_F96Frame *frame))
171 if (!StartConnection (port_device,false,connection))
172 return GE_INTERNALERROR;
174 CurrentConnectionType = connection;
176 if (AT_OpenSerial() != true) return GE_INTERNALERROR;
183 bool AT_WritePhone (u16 length, u8 *buffer) {
186 for (i=0;i<length;i++) {
187 if (device_write(buffer+i,1)!=1) return false;
188 /* For some phones like Siemens M20 we need to wait a little
189 after writing each char. Possible reason: these phones
190 can't receive so fast chars or there is bug here in mygnokii */