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.1.2.1 2001/11/27 22:48:37 short
19 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
21 Revision 1.1.1.2 2001/11/27 22:01:13 short
22 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
24 Revision 1.6 2001/11/27 12:19:01 pkot
25 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
27 Revision 1.5 2001/11/08 16:34:19 pkot
28 Updates to work with new libsms
30 Revision 1.4 2001/07/03 15:27:03 pkot
31 AT commands for SMS handling support (Tamas Bondar)
32 Small at-emulator code cleanup (me)
34 Revision 1.3 2001/02/21 19:56:59 chris
35 More fiddling with the directory layout
40 #define __data_at_emulator_c
50 #include <sys/types.h>
63 #include "gsm-common.h"
65 #include "data/at-emulator.h"
66 #include "data/virtmodem.h"
67 #include "data/datapump.h"
69 #define MAX_LINE_LENGTH 256
71 /* Global variables */
72 bool ATEM_Initialised = false; /* Set to true once initialised */
73 extern bool CommandMode;
74 extern int ConnectCount;
76 char ModelName[80]; /* This seems to be needed to avoid seg-faults */
81 int PtyRDFD; /* File descriptor for reading and writing to/from */
82 int PtyWRFD; /* pty interface - only different in debug mode. */
84 u8 ModemRegisters[MAX_MODEM_REGISTERS];
85 char CmdBuffer[MAX_CMD_BUFFERS][CMD_BUFFER_LENGTH];
87 int CurrentCmdBufferIndex;
88 bool VerboseResponse; /* Switch betweek numeric (4) and text responses (ERROR) */
90 int MessageFormat; /* Message Format (text or pdu) */
92 /* Current command parser */
93 void (*Parser)(char *);
94 //void (*Parser)(char *) = ATEM_ParseAT; /* Current command parser */
96 GSM_MemoryType SMSType;
99 /* If initialised in debug mode, stdin/out is used instead
100 of ptys for interface. */
101 bool ATEM_Initialise(int read_fd, int write_fd, char *model, char *port)
106 strncpy(ModelName,model,80);
107 strncpy(PortName,port,80);
109 /* Initialise command buffer variables */
110 CurrentCmdBuffer = 0;
111 CurrentCmdBufferIndex = 0;
113 /* Default to verbose reponses */
114 VerboseResponse = true;
116 /* Initialise registers */
117 ATEM_InitRegisters();
119 /* Initial parser is AT routine */
120 Parser = ATEM_ParseAT;
122 /* Setup defaults for AT*C interpreter. */
126 /* Default message format is PDU */
127 MessageFormat = PDU_MODE;
129 /* Set the call passup so that we get notified of incoming calls */
130 GSM->DialData(NULL,-1,&ATEM_CallPassup);
132 /* We're ready to roll... */
133 ATEM_Initialised = true;
137 /* Initialise the "registers" used by the virtual modem. */
138 void ATEM_InitRegisters(void)
141 ModemRegisters[REG_RINGATA] = 0;
142 ModemRegisters[REG_RINGCNT] = 2;
143 ModemRegisters[REG_ESC] = '+';
144 ModemRegisters[REG_CR] = 10;
145 ModemRegisters[REG_LF] = 13;
146 ModemRegisters[REG_BS] = 8;
147 ModemRegisters[S35]=7;
148 ModemRegisters[REG_ECHO] = BIT_ECHO;
153 /* This gets called to indicate an incoming call */
155 void ATEM_CallPassup(char c)
157 if ((c >= 0) && (c < 9)) {
158 ATEM_ModemResult(MR_RING);
164 /* Handler called when characters received from serial port.
165 calls state machine code to process it. */
167 void ATEM_HandleIncomingData(char *buffer, int length)
170 unsigned char out_buf[3];
172 for (count = 0; count < length ; count++) {
173 /* Echo character if appropriate. */
174 if (ModemRegisters[REG_ECHO] & BIT_ECHO) {
175 out_buf[0] = buffer[count];
177 ATEM_StringOut(out_buf);
180 /* If it's a command terminator character, parse what
181 we have so far then go to next buffer. */
182 if (buffer[count] == ModemRegisters[REG_CR] ||
183 buffer[count] == ModemRegisters[REG_LF]) {
185 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = 0x00;
186 Parser(CmdBuffer[CurrentCmdBuffer]);
189 if (CurrentCmdBuffer >= MAX_CMD_BUFFERS) {
190 CurrentCmdBuffer = 0;
192 CurrentCmdBufferIndex = 0;
194 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = buffer[count];
195 CurrentCmdBufferIndex++;
196 if (CurrentCmdBufferIndex >= CMD_BUFFER_LENGTH) {
197 CurrentCmdBufferIndex = CMD_BUFFER_LENGTH;
204 /* Parser for standard AT commands. cmd_buffer must be null terminated. */
205 void ATEM_ParseAT(char *cmd_buffer)
210 if (strncasecmp (cmd_buffer, "AT", 2) != 0) {
211 ATEM_ModemResult(MR_ERROR);
215 for (buf = &cmd_buffer[2]; *buf;) {
216 switch (toupper(*buf)) {
223 /* For now we'll also initialise the datapump + rlp code again */
224 DP_Initialise(PtyRDFD, PtyWRFD);
225 GSM->DialData(NULL, -1, &DP_CallPassup);
226 GSM->AnswerCall(IncomingCallNo);
232 /* FIXME - should parse this better */
233 /* For now we'll also initialise the datapump + rlp code again */
234 DP_Initialise(PtyRDFD, PtyWRFD);
236 if (toupper(*buf) == 'T') buf++;
237 if (*buf == ' ') buf++;
238 strncpy(number, buf, 30);
239 if (ModemRegisters[S35] == 0) GSM->DialData(number, 1, &DP_CallPassup);
240 else GSM->DialData(number, 0, &DP_CallPassup);
241 ATEM_StringOut("\n\r");
248 RLP_SetUserRequest(Disc_Req, true);
252 /* Change registers - only no. 35 for now */
254 if (memcmp(buf, "35=", 3) == 0) {
256 ModemRegisters[S35] = *buf - '0';
260 /* E - Turn Echo on/off */
263 switch (ATEM_GetNum(&buf)) {
265 ModemRegisters[REG_ECHO] &= ~BIT_ECHO;
268 ModemRegisters[REG_ECHO] |= BIT_ECHO;
271 ATEM_ModemResult(MR_ERROR);
276 /* Handle AT* commands (Nokia proprietary I think) */
279 if (!strcasecmp(buf, "NOKIATEST")) {
280 ATEM_ModemResult(MR_OK); /* FIXME? */
283 if (!strcasecmp(buf, "C")) {
284 ATEM_ModemResult(MR_OK);
285 Parser = ATEM_ParseSMS;
291 /* + is the precursor to another set of commands +CSQ, +FCLASS etc. */
294 switch (toupper(*buf)) {
297 /* Returns true if error occured */
298 if (ATEM_CommandPlusC(&buf) == true) {
305 /* Returns true if error occured */
306 if (ATEM_CommandPlusG(&buf) == true) {
312 ATEM_ModemResult(MR_ERROR);
318 ATEM_ModemResult(MR_ERROR);
323 ATEM_ModemResult(MR_OK);
326 static GSM_Error ATEM_ReadSMS(int number, GSM_MemoryType type, GSM_SMSMessage *message)
330 message->MemoryType = type;
331 message->Number = number;
332 error = GSM->GetSMSMessage(message);
337 static void ATEM_PrintSMS(char *line, GSM_SMSMessage *message, int mode)
341 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);
344 if ((message->DCS.Type == SMS_GeneralDataCoding) &&
345 (message->DCS.u.General.Alphabet == SMS_8bit))
346 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>"));
348 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);
351 gsprintf(line, MAX_LINE_LENGTH, _("<Not implemented>"));
354 gsprintf(line, MAX_LINE_LENGTH, _("<Unknown mode>"));
359 static void ATEM_EraseSMS(int number, GSM_MemoryType type)
361 GSM_SMSMessage message;
362 message.MemoryType = type;
363 message.Number = number;
364 if (GSM->DeleteSMSMessage(&message) == GE_NONE) {
365 ATEM_ModemResult(MR_OK);
367 ATEM_ModemResult(MR_ERROR);
372 static void ATEM_HandleSMS()
374 GSM_SMSMessage message;
376 char buffer[MAX_LINE_LENGTH];
378 error = ATEM_ReadSMS(SMSNumber, SMSType, &message);
381 ATEM_PrintSMS(buffer, &message, INTERACT_MODE);
382 ATEM_StringOut(buffer);
385 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rNo message under number %d\n\r"), SMSNumber);
386 ATEM_StringOut(buffer);
392 /* Parser for SMS interactive mode */
393 void ATEM_ParseSMS(char *buff)
395 if (!strcasecmp(buff, "HELP")) {
396 ATEM_StringOut(_("\n\rThe following commands work...\n\r"));
397 ATEM_StringOut("DIR\n\r");
398 ATEM_StringOut("EXIT\n\r");
399 ATEM_StringOut("HELP\n\r");
403 if (!strcasecmp(buff, "DIR")) {
406 Parser = ATEM_ParseDIR;
409 if (!strcasecmp(buff, "EXIT")) {
410 Parser = ATEM_ParseAT;
411 ATEM_ModemResult(MR_OK);
414 ATEM_ModemResult(MR_ERROR);
417 /* Parser for DIR sub mode of SMS interactive mode. */
418 void ATEM_ParseDIR(char *buff)
420 switch (toupper(*buff)) {
430 ATEM_EraseSMS(SMSNumber, SMSType);
433 Parser= ATEM_ParseSMS;
434 ATEM_ModemResult(MR_OK);
437 ATEM_ModemResult(MR_ERROR);
440 /* Handle AT+C commands, this is a quick hack together at this
442 bool ATEM_CommandPlusC(char **buf)
445 GSM_RFUnits rfunits = GRF_CSQ;
446 char buffer[MAX_LINE_LENGTH], buffer2[MAX_LINE_LENGTH];
449 GSM_SMSMessage message;
451 if (strncasecmp(*buf, "SQ", 2) == 0) {
454 if (GSM->GetRFLevel(&rfunits, &rflevel) == GE_NONE) {
455 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CSQ: %0.0f, 99", rflevel);
456 ATEM_StringOut(buffer);
462 /* AT+CGMI is Manufacturer information for the ME (phone) so
463 it should be Nokia rather than gnokii... */
464 if (strncasecmp(*buf, "GMI", 3) == 0) {
466 ATEM_StringOut(_("\n\rNokia Mobile Phones"));
470 /* AT+CGSN is IMEI */
471 if (strncasecmp(*buf, "GSN", 3) == 0) {
473 if (GSM->GetIMEI(buffer2) == GE_NONE) {
474 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
475 ATEM_StringOut(buffer);
482 /* AT+CGMR is Revision (hardware) */
483 if (strncasecmp(*buf, "GMR", 3) == 0) {
485 if (GSM->GetRevision(buffer2) == GE_NONE) {
486 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
487 ATEM_StringOut(buffer);
494 /* AT+CGMM is Model code */
495 if (strncasecmp(*buf, "GMM", 3) == 0) {
497 if (GSM->GetModel(buffer2) == GE_NONE) {
498 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
499 ATEM_StringOut(buffer);
506 /* AT+CMGF is mode selection for message format */
507 if (strncasecmp(*buf, "MGF", 3) == 0) {
515 MessageFormat = PDU_MODE;
519 MessageFormat = TEXT_MODE;
527 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGF: %d", MessageFormat);
528 ATEM_StringOut(buffer);
536 /* AT+CMGR is reading a message */
537 if (strncasecmp(*buf, "MGR", 3) == 0) {
542 sscanf(*buf, "%d", &index);
543 buf[0] += strlen(*buf);
545 error = ATEM_ReadSMS(index, SMSType, &message);
548 ATEM_PrintSMS(buffer2, &message, MessageFormat);
549 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGR: %s", buffer2);
550 ATEM_StringOut(buffer);
553 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
554 ATEM_StringOut(buffer);
564 /* AT+CMGL is listing messages */
565 if (strncasecmp(*buf, "MGL", 3) == 0) {
573 /* process <stat> parameter */
574 if (*(*buf-1) == 0 || /* i.e. no parameter given */
575 strcasecmp(*buf, "1") == 0 ||
576 strcasecmp(*buf, "3") == 0 ||
577 strcasecmp(*buf, "\"REC READ\"") == 0 ||
578 strcasecmp(*buf, "\"STO SENT\"") == 0) {
580 } else if (strcasecmp(*buf, "0") == 0 ||
581 strcasecmp(*buf, "2") == 0 ||
582 strcasecmp(*buf, "\"REC UNREAD\"") == 0 ||
583 strcasecmp(*buf, "\"STO UNSENT\"") == 0) {
585 } else if (strcasecmp(*buf, "4") == 0 ||
586 strcasecmp(*buf, "\"ALL\"") == 0) {
587 status = 4; /* ALL */
591 buf[0] += strlen(*buf);
593 /* check all message storages */
594 for (index = 1; index <= 20; index++) {
595 error = ATEM_ReadSMS(index, SMSType, &message);
598 /* print messsage if it has the required status */
599 if (message.Status == status || status == 4 /* ALL */) {
600 ATEM_PrintSMS(buffer2, &message, MessageFormat);
601 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGL: %d,%s", index, buffer2);
602 ATEM_StringOut(buffer);
605 case GE_EMPTYSMSLOCATION:
606 /* don't care if this storage is empty */
609 /* print other error codes and quit */
610 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
611 ATEM_StringOut(buffer);
625 /* AT+G commands. Some of these responses are a bit tongue in cheek... */
626 bool ATEM_CommandPlusG(char **buf)
628 char buffer[MAX_LINE_LENGTH];
630 /* AT+GMI is Manufacturer information for the TA (Terminal Adaptor) */
631 if (strncasecmp(*buf, "MI", 3) == 0) {
634 ATEM_StringOut(_("\n\rHugh Blemings, Pavel Janík ml. and others..."));
638 /* AT+GMR is Revision information for the TA (Terminal Adaptor) */
639 if (strncasecmp(*buf, "MR", 3) == 0) {
641 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s %s %s", VERSION, __TIME__, __DATE__);
643 ATEM_StringOut(buffer);
647 /* AT+GMM is Model information for the TA (Terminal Adaptor) */
648 if (strncasecmp(*buf, "MM", 3) == 0) {
651 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rgnokii configured for %s on %s"), ModelName, PortName);
652 ATEM_StringOut(buffer);
656 /* AT+GSN is Serial number for the TA (Terminal Adaptor) */
657 if (strncasecmp(*buf, "SN", 3) == 0) {
660 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rnone built in, choose your own"));
661 ATEM_StringOut(buffer);
668 /* Send a result string back. There is much work to do here, see
669 the code in the isdn driver for an idea of where it's heading... */
670 void ATEM_ModemResult(int code)
674 if (VerboseResponse == false) {
675 sprintf(buffer, "\n\r%d\n\r", code);
676 ATEM_StringOut(buffer);
680 ATEM_StringOut("\n\rOK\n\r");
684 ATEM_StringOut("\n\rERROR\n\r");
688 ATEM_StringOut("\n\rCARRIER\n\r");
692 ATEM_StringOut("\n\rCONNECT\n\r");
696 ATEM_StringOut("\n\rNO CARRIER\n\r");
699 ATEM_StringOut("RING\n\r");
702 ATEM_StringOut(_("\n\rUnknown Result Code!\n\r"));
710 /* Get integer from char-pointer, set pointer to end of number
711 stolen basically verbatim from ISDN code. */
712 int ATEM_GetNum(char **p)
716 while (*p[0] >= '0' && *p[0] <= '9') {
717 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
723 /* Write string to virtual modem port, either pty or
724 STDOUT as appropriate. This function is only used during
725 command mode - data pump is used when connected. */
726 void ATEM_StringOut(char *buffer)
731 while (count < strlen(buffer)) {
733 /* Translate CR/LF/BS as appropriate */
734 switch (buffer[count]) {
736 out_char = ModemRegisters[REG_CR];
739 out_char = ModemRegisters[REG_LF];
742 out_char = ModemRegisters[REG_BS];
745 out_char = buffer[count];
749 write(PtyWRFD, &out_char, 1);