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
15 /* "Turn on" prototypes in AT.h */
18 /* System header files */
25 #include "misc_win32.h"
30 /* Various header file */
31 #include "devices/device.h"
33 #include "protocol/at.h"
35 GSM_Protocol AT_Functions = {
47 static u16 MessageLength;
49 u8 MessageBuffer[AT_MAX_RECEIVE_LENGTH * 6];
51 static bool wascrlf = false;
52 static char *linestart = MessageBuffer;
54 /* N61_RX_DisplayMessage is called when a message we don't know about is
55 received so that the user can see what is going back and forth, and perhaps
56 shed some more light/explain another message type! */
59 void AT_RX_DisplayMessage()
61 fprintf(stdout, _("Phone\n"));
63 hexdump(MessageLength,MessageBuffer);
67 /* Prepares the message header and sends it, prepends the message start byte
68 (0x1e) and other values according the value specified when called.
69 Calculates checksum and then sends the lot down the pipe... */
70 int AT_SendFrame(u16 message_length, u8 message_type, u8 *buffer) {
76 /* FIXME - we should check for the message length ... */
79 fprintf(stdout, _("PC: "));
81 for (count = 0; count < message_length-1; count++)
82 fprintf(stdout, "%c", buffer[count]);
84 fprintf(stdout, "\n");
88 if (!AT_WritePhone(message_length,buffer))
94 int AT_SendMessage(u16 message_length, u8 message_type, u8 *buffer) {
96 AT_SendFrame(message_length, message_type, buffer);
101 int AT_SendAck(u8 message_type, u8 message_seq) {
106 /* Applications should call AT_Terminate to shut down the AT thread and
107 close the serial port. */
108 void AT_Terminate(void)
110 /* Request termination of thread */
111 CurrentRequestTerminate = true;
113 /* Close serial port. */
117 /* RX_State machine for receive handling. Called once for each character
118 received from the phone/phone. */
120 void AT_RX_StateMachine(unsigned char rx_byte) {
122 /* FIXME: Check for overflow */
123 MessageBuffer[BufferCount] = rx_byte;
124 MessageBuffer[BufferCount + 1] = '\0';
126 // fprintf(stdout,_("%02x %c\n"),rx_byte,rx_byte);
128 if (wascrlf && (rx_byte != 10 && rx_byte != 13)) {
129 linestart = MessageBuffer + BufferCount;
133 if ((rx_byte== 10 || rx_byte==13)) wascrlf = true;
135 if ((BufferCount > 0) && (10 == MessageBuffer[BufferCount])
136 && (13 == MessageBuffer[BufferCount-1])
137 && ((0 == strncmp("OK", linestart, 2))
138 || (0 == strncmp("ERROR", linestart, 5))
139 || (0 == strncmp("+CME ERROR:", linestart, 11))
140 || (0 == strncmp("+CMS ERROR:", linestart, 11)))) {
142 MessageLength=BufferCount;
145 AT_RX_DisplayMessage();
148 GSM->DispatchMessage(MessageLength,MessageBuffer, 0x00);
151 linestart = MessageBuffer;
154 } else BufferCount++;
157 /* Called by initialisation code to open comm port in asynchronous mode. */
158 bool AT_OpenSerial(void)
160 // device_changespeed(115200);
162 device_changespeed(19200);
167 /* Initialise variables and state machine. */
168 GSM_Error AT_Initialise(char *port_device, char *initlength,
169 GSM_ConnectionType connection,
170 void (*rlp_callback)(RLP_F96Frame *frame))
173 if (!StartConnection (port_device,false,connection))
174 return GE_INTERNALERROR;
176 CurrentConnectionType = connection;
178 if (AT_OpenSerial() != true) return GE_INTERNALERROR;
185 bool AT_WritePhone (u16 length, u8 *buffer) {
188 for (i=0;i<length;i++) {
189 if (device_write(buffer+i,1)!=1) return false;
190 /* For some phones like Siemens M20 we need to wait a little
191 after writing each char. Possible reason: these phones
192 can't receive so fast chars or there is bug here in mygnokii */