7 A Linux/Unix toolset and driver for Nokia mobile phones.
9 Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
11 Released under the terms of the GNU GPL, see file COPYING for more details.
13 This file provides a virtual modem or "AT" interface to the GSM phone by
14 calling code in gsm-api.c. Inspired by and in places copied from the Linux
15 kernel AT Emulator IDSN code by Fritz Elfert and others.
18 Revision 1.1.1.5 2002/04/03 00:08:04 short
19 Found in "gnokii-working" directory, some November-patches version
21 Revision 1.5 2001/11/08 16:34:19 pkot
22 Updates to work with new libsms
24 Revision 1.4 2001/07/03 15:27:03 pkot
25 AT commands for SMS handling support (Tamas Bondar)
26 Small at-emulator code cleanup (me)
28 Revision 1.3 2001/02/21 19:56:59 chris
29 More fiddling with the directory layout
34 #define __data_at_emulator_c
44 #include <sys/types.h>
57 #include "gsm-common.h"
59 #include "data/at-emulator.h"
60 #include "data/virtmodem.h"
61 #include "data/datapump.h"
63 #define MAX_LINE_LENGTH 256
65 /* Global variables */
66 bool ATEM_Initialised = false; /* Set to true once initialised */
67 extern bool CommandMode;
68 extern int ConnectCount;
70 char ModelName[80]; /* This seems to be needed to avoid seg-faults */
75 int PtyRDFD; /* File descriptor for reading and writing to/from */
76 int PtyWRFD; /* pty interface - only different in debug mode. */
78 u8 ModemRegisters[MAX_MODEM_REGISTERS];
79 char CmdBuffer[MAX_CMD_BUFFERS][CMD_BUFFER_LENGTH];
81 int CurrentCmdBufferIndex;
82 bool VerboseResponse; /* Switch betweek numeric (4) and text responses (ERROR) */
84 int MessageFormat; /* Message Format (text or pdu) */
86 /* Current command parser */
87 void (*Parser)(char *);
88 //void (*Parser)(char *) = ATEM_ParseAT; /* Current command parser */
90 GSM_MemoryType SMSType;
93 /* If initialised in debug mode, stdin/out is used instead
94 of ptys for interface. */
95 bool ATEM_Initialise(int read_fd, int write_fd, char *model, char *port)
100 strncpy(ModelName,model,80);
101 strncpy(PortName,port,80);
103 /* Initialise command buffer variables */
104 CurrentCmdBuffer = 0;
105 CurrentCmdBufferIndex = 0;
107 /* Default to verbose reponses */
108 VerboseResponse = true;
110 /* Initialise registers */
111 ATEM_InitRegisters();
113 /* Initial parser is AT routine */
114 Parser = ATEM_ParseAT;
116 /* Setup defaults for AT*C interpreter. */
120 /* Default message format is PDU */
121 MessageFormat = PDU_MODE;
123 /* Set the call passup so that we get notified of incoming calls */
124 GSM->DialData(NULL,-1,&ATEM_CallPassup);
126 /* We're ready to roll... */
127 ATEM_Initialised = true;
131 /* Initialise the "registers" used by the virtual modem. */
132 void ATEM_InitRegisters(void)
135 ModemRegisters[REG_RINGATA] = 0;
136 ModemRegisters[REG_RINGCNT] = 2;
137 ModemRegisters[REG_ESC] = '+';
138 ModemRegisters[REG_CR] = 10;
139 ModemRegisters[REG_LF] = 13;
140 ModemRegisters[REG_BS] = 8;
141 ModemRegisters[S35]=7;
142 ModemRegisters[REG_ECHO] = BIT_ECHO;
147 /* This gets called to indicate an incoming call */
149 void ATEM_CallPassup(char c)
151 if ((c >= 0) && (c < 9)) {
152 ATEM_ModemResult(MR_RING);
158 /* Handler called when characters received from serial port.
159 calls state machine code to process it. */
161 void ATEM_HandleIncomingData(char *buffer, int length)
164 unsigned char out_buf[3];
166 for (count = 0; count < length ; count++) {
167 /* Echo character if appropriate. */
168 if (ModemRegisters[REG_ECHO] & BIT_ECHO) {
169 out_buf[0] = buffer[count];
171 ATEM_StringOut(out_buf);
174 /* If it's a command terminator character, parse what
175 we have so far then go to next buffer. */
176 if (buffer[count] == ModemRegisters[REG_CR] ||
177 buffer[count] == ModemRegisters[REG_LF]) {
179 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = 0x00;
180 Parser(CmdBuffer[CurrentCmdBuffer]);
183 if (CurrentCmdBuffer >= MAX_CMD_BUFFERS) {
184 CurrentCmdBuffer = 0;
186 CurrentCmdBufferIndex = 0;
188 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = buffer[count];
189 CurrentCmdBufferIndex++;
190 if (CurrentCmdBufferIndex >= CMD_BUFFER_LENGTH) {
191 CurrentCmdBufferIndex = CMD_BUFFER_LENGTH;
198 /* Parser for standard AT commands. cmd_buffer must be null terminated. */
199 void ATEM_ParseAT(char *cmd_buffer)
204 if (strncasecmp (cmd_buffer, "AT", 2) != 0) {
205 ATEM_ModemResult(MR_ERROR);
209 for (buf = &cmd_buffer[2]; *buf;) {
210 switch (toupper(*buf)) {
217 /* For now we'll also initialise the datapump + rlp code again */
218 DP_Initialise(PtyRDFD, PtyWRFD);
219 GSM->DialData(NULL, -1, &DP_CallPassup);
220 GSM->AnswerCall(IncomingCallNo);
226 /* FIXME - should parse this better */
227 /* For now we'll also initialise the datapump + rlp code again */
228 DP_Initialise(PtyRDFD, PtyWRFD);
230 if (toupper(*buf) == 'T') buf++;
231 if (*buf == ' ') buf++;
232 strncpy(number, buf, 30);
233 if (ModemRegisters[S35] == 0) GSM->DialData(number, 1, &DP_CallPassup);
234 else GSM->DialData(number, 0, &DP_CallPassup);
235 ATEM_StringOut("\n\r");
242 RLP_SetUserRequest(Disc_Req, true);
246 /* Change registers - only no. 35 for now */
248 if (memcmp(buf, "35=", 3) == 0) {
250 ModemRegisters[S35] = *buf - '0';
254 /* E - Turn Echo on/off */
257 switch (ATEM_GetNum(&buf)) {
259 ModemRegisters[REG_ECHO] &= ~BIT_ECHO;
262 ModemRegisters[REG_ECHO] |= BIT_ECHO;
265 ATEM_ModemResult(MR_ERROR);
270 /* Handle AT* commands (Nokia proprietary I think) */
273 if (!strcasecmp(buf, "NOKIATEST")) {
274 ATEM_ModemResult(MR_OK); /* FIXME? */
277 if (!strcasecmp(buf, "C")) {
278 ATEM_ModemResult(MR_OK);
279 Parser = ATEM_ParseSMS;
285 /* + is the precursor to another set of commands +CSQ, +FCLASS etc. */
288 switch (toupper(*buf)) {
291 /* Returns true if error occured */
292 if (ATEM_CommandPlusC(&buf) == true) {
299 /* Returns true if error occured */
300 if (ATEM_CommandPlusG(&buf) == true) {
306 ATEM_ModemResult(MR_ERROR);
312 ATEM_ModemResult(MR_ERROR);
317 ATEM_ModemResult(MR_OK);
320 static GSM_Error ATEM_ReadSMS(int number, GSM_MemoryType type, GSM_SMSMessage *message)
324 message->MemoryType = type;
325 message->Number = number;
326 error = GSM->GetSMSMessage(message);
331 static void ATEM_PrintSMS(char *line, GSM_SMSMessage *message, int mode)
335 gsprintf(line, MAX_LINE_LENGTH, _("\n\rDate/time: %d/%d/%d %d:%02d:%02d Sender: %s Msg Center: %s\n\rText: %s\n\r"), message->Time.Day, message->Time.Month, message->Time.Year, message->Time.Hour, message->Time.Minute, message->Time.Second, message->RemoteNumber.number, message->MessageCenter.Number, message->MessageText);
338 if ((message->DCS.Type == SMS_GeneralDataCoding) &&
339 (message->DCS.u.General.Alphabet == SMS_8bit))
340 gsprintf(line, MAX_LINE_LENGTH, _("\"%s\",\"%s\",,\"%02d/%02d/%02d,%02d:%02d:%02d+%02d\"\n\r%s"), (message->Status ? _("REC READ") : _("REC UNREAD")), message->RemoteNumber.number, message->Time.Year, message->Time.Month, message->Time.Day, message->Time.Hour, message->Time.Minute, message->Time.Second, message->Time.Timezone, _("<Not implemented>"));
342 gsprintf(line, MAX_LINE_LENGTH, _("\"%s\",\"%s\",,\"%02d/%02d/%02d,%02d:%02d:%02d+%02d\"\n\r%s"), (message->Status ? _("REC READ") : _("REC UNREAD")), message->RemoteNumber.number, message->Time.Year, message->Time.Month, message->Time.Day, message->Time.Hour, message->Time.Minute, message->Time.Second, message->Time.Timezone, message->MessageText);
345 gsprintf(line, MAX_LINE_LENGTH, _("<Not implemented>"));
348 gsprintf(line, MAX_LINE_LENGTH, _("<Unknown mode>"));
353 static void ATEM_EraseSMS(int number, GSM_MemoryType type)
355 GSM_SMSMessage message;
356 message.MemoryType = type;
357 message.Number = number;
358 if (GSM->DeleteSMSMessage(&message) == GE_NONE) {
359 ATEM_ModemResult(MR_OK);
361 ATEM_ModemResult(MR_ERROR);
366 static void ATEM_HandleSMS()
368 GSM_SMSMessage message;
370 char buffer[MAX_LINE_LENGTH];
372 error = ATEM_ReadSMS(SMSNumber, SMSType, &message);
375 ATEM_PrintSMS(buffer, &message, INTERACT_MODE);
376 ATEM_StringOut(buffer);
379 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rNo message under number %d\n\r"), SMSNumber);
380 ATEM_StringOut(buffer);
386 /* Parser for SMS interactive mode */
387 void ATEM_ParseSMS(char *buff)
389 if (!strcasecmp(buff, "HELP")) {
390 ATEM_StringOut(_("\n\rThe following commands work...\n\r"));
391 ATEM_StringOut("DIR\n\r");
392 ATEM_StringOut("EXIT\n\r");
393 ATEM_StringOut("HELP\n\r");
397 if (!strcasecmp(buff, "DIR")) {
400 Parser = ATEM_ParseDIR;
403 if (!strcasecmp(buff, "EXIT")) {
404 Parser = ATEM_ParseAT;
405 ATEM_ModemResult(MR_OK);
408 ATEM_ModemResult(MR_ERROR);
411 /* Parser for DIR sub mode of SMS interactive mode. */
412 void ATEM_ParseDIR(char *buff)
414 switch (toupper(*buff)) {
424 ATEM_EraseSMS(SMSNumber, SMSType);
427 Parser= ATEM_ParseSMS;
428 ATEM_ModemResult(MR_OK);
431 ATEM_ModemResult(MR_ERROR);
434 /* Handle AT+C commands, this is a quick hack together at this
436 bool ATEM_CommandPlusC(char **buf)
439 GSM_RFUnits rfunits = GRF_CSQ;
440 char buffer[MAX_LINE_LENGTH], buffer2[MAX_LINE_LENGTH];
443 GSM_SMSMessage message;
445 if (strncasecmp(*buf, "SQ", 2) == 0) {
448 if (GSM->GetRFLevel(&rfunits, &rflevel) == GE_NONE) {
449 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CSQ: %0.0f, 99", rflevel);
450 ATEM_StringOut(buffer);
456 /* AT+CGMI is Manufacturer information for the ME (phone) so
457 it should be Nokia rather than gnokii... */
458 if (strncasecmp(*buf, "GMI", 3) == 0) {
460 ATEM_StringOut(_("\n\rNokia Mobile Phones"));
464 /* AT+CGSN is IMEI */
465 if (strncasecmp(*buf, "GSN", 3) == 0) {
467 if (GSM->GetIMEI(buffer2) == GE_NONE) {
468 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
469 ATEM_StringOut(buffer);
476 /* AT+CGMR is Revision (hardware) */
477 if (strncasecmp(*buf, "GMR", 3) == 0) {
479 if (GSM->GetRevision(buffer2) == GE_NONE) {
480 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
481 ATEM_StringOut(buffer);
488 /* AT+CGMM is Model code */
489 if (strncasecmp(*buf, "GMM", 3) == 0) {
491 if (GSM->GetModel(buffer2) == GE_NONE) {
492 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
493 ATEM_StringOut(buffer);
500 /* AT+CMGF is mode selection for message format */
501 if (strncasecmp(*buf, "MGF", 3) == 0) {
509 MessageFormat = PDU_MODE;
513 MessageFormat = TEXT_MODE;
521 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGF: %d", MessageFormat);
522 ATEM_StringOut(buffer);
530 /* AT+CMGR is reading a message */
531 if (strncasecmp(*buf, "MGR", 3) == 0) {
536 sscanf(*buf, "%d", &index);
537 buf[0] += strlen(*buf);
539 error = ATEM_ReadSMS(index, SMSType, &message);
542 ATEM_PrintSMS(buffer2, &message, MessageFormat);
543 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGR: %s", buffer2);
544 ATEM_StringOut(buffer);
547 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
548 ATEM_StringOut(buffer);
558 /* AT+CMGL is listing messages */
559 if (strncasecmp(*buf, "MGL", 3) == 0) {
567 /* process <stat> parameter */
568 if (*(*buf-1) == 0 || /* i.e. no parameter given */
569 strcasecmp(*buf, "1") == 0 ||
570 strcasecmp(*buf, "3") == 0 ||
571 strcasecmp(*buf, "\"REC READ\"") == 0 ||
572 strcasecmp(*buf, "\"STO SENT\"") == 0) {
574 } else if (strcasecmp(*buf, "0") == 0 ||
575 strcasecmp(*buf, "2") == 0 ||
576 strcasecmp(*buf, "\"REC UNREAD\"") == 0 ||
577 strcasecmp(*buf, "\"STO UNSENT\"") == 0) {
579 } else if (strcasecmp(*buf, "4") == 0 ||
580 strcasecmp(*buf, "\"ALL\"") == 0) {
581 status = 4; /* ALL */
585 buf[0] += strlen(*buf);
587 /* check all message storages */
588 for (index = 1; index <= 20; index++) {
589 error = ATEM_ReadSMS(index, SMSType, &message);
592 /* print messsage if it has the required status */
593 if (message.Status == status || status == 4 /* ALL */) {
594 ATEM_PrintSMS(buffer2, &message, MessageFormat);
595 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGL: %d,%s", index, buffer2);
596 ATEM_StringOut(buffer);
599 case GE_EMPTYSMSLOCATION:
600 /* don't care if this storage is empty */
603 /* print other error codes and quit */
604 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
605 ATEM_StringOut(buffer);
619 /* AT+G commands. Some of these responses are a bit tongue in cheek... */
620 bool ATEM_CommandPlusG(char **buf)
622 char buffer[MAX_LINE_LENGTH];
624 /* AT+GMI is Manufacturer information for the TA (Terminal Adaptor) */
625 if (strncasecmp(*buf, "MI", 3) == 0) {
628 ATEM_StringOut(_("\n\rHugh Blemings, Pavel Janík ml. and others..."));
632 /* AT+GMR is Revision information for the TA (Terminal Adaptor) */
633 if (strncasecmp(*buf, "MR", 3) == 0) {
635 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s %s %s", VERSION, __TIME__, __DATE__);
637 ATEM_StringOut(buffer);
641 /* AT+GMM is Model information for the TA (Terminal Adaptor) */
642 if (strncasecmp(*buf, "MM", 3) == 0) {
645 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rgnokii configured for %s on %s"), ModelName, PortName);
646 ATEM_StringOut(buffer);
650 /* AT+GSN is Serial number for the TA (Terminal Adaptor) */
651 if (strncasecmp(*buf, "SN", 3) == 0) {
654 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rnone built in, choose your own"));
655 ATEM_StringOut(buffer);
662 /* Send a result string back. There is much work to do here, see
663 the code in the isdn driver for an idea of where it's heading... */
664 void ATEM_ModemResult(int code)
668 if (VerboseResponse == false) {
669 sprintf(buffer, "\n\r%d\n\r", code);
670 ATEM_StringOut(buffer);
674 ATEM_StringOut("\n\rOK\n\r");
678 ATEM_StringOut("\n\rERROR\n\r");
682 ATEM_StringOut("\n\rCARRIER\n\r");
686 ATEM_StringOut("\n\rCONNECT\n\r");
690 ATEM_StringOut("\n\rNO CARRIER\n\r");
693 ATEM_StringOut("RING\n\r");
696 ATEM_StringOut(_("\n\rUnknown Result Code!\n\r"));
704 /* Get integer from char-pointer, set pointer to end of number
705 stolen basically verbatim from ISDN code. */
706 int ATEM_GetNum(char **p)
710 while (*p[0] >= '0' && *p[0] <= '9') {
711 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
717 /* Write string to virtual modem port, either pty or
718 STDOUT as appropriate. This function is only used during
719 command mode - data pump is used when connected. */
720 void ATEM_StringOut(char *buffer)
725 while (count < strlen(buffer)) {
727 /* Translate CR/LF/BS as appropriate */
728 switch (buffer[count]) {
730 out_char = ModemRegisters[REG_CR];
733 out_char = ModemRegisters[REG_LF];
736 out_char = ModemRegisters[REG_BS];
739 out_char = buffer[count];
743 write(PtyWRFD, &out_char, 1);