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.2 2001/11/27 22:01:13 short
19 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
21 Revision 1.6 2001/11/27 12:19:01 pkot
22 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
24 Revision 1.5 2001/11/08 16:34:19 pkot
25 Updates to work with new libsms
27 Revision 1.4 2001/07/03 15:27:03 pkot
28 AT commands for SMS handling support (Tamas Bondar)
29 Small at-emulator code cleanup (me)
31 Revision 1.3 2001/02/21 19:56:59 chris
32 More fiddling with the directory layout
37 #define __data_at_emulator_c
47 #include <sys/types.h>
60 #include "gsm-common.h"
62 #include "data/at-emulator.h"
63 #include "data/virtmodem.h"
64 #include "data/datapump.h"
66 #define MAX_LINE_LENGTH 256
68 /* Global variables */
69 bool ATEM_Initialised = false; /* Set to true once initialised */
70 extern bool CommandMode;
71 extern int ConnectCount;
73 char ModelName[80]; /* This seems to be needed to avoid seg-faults */
78 int PtyRDFD; /* File descriptor for reading and writing to/from */
79 int PtyWRFD; /* pty interface - only different in debug mode. */
81 u8 ModemRegisters[MAX_MODEM_REGISTERS];
82 char CmdBuffer[MAX_CMD_BUFFERS][CMD_BUFFER_LENGTH];
84 int CurrentCmdBufferIndex;
85 bool VerboseResponse; /* Switch betweek numeric (4) and text responses (ERROR) */
87 int MessageFormat; /* Message Format (text or pdu) */
89 /* Current command parser */
90 void (*Parser)(char *);
91 //void (*Parser)(char *) = ATEM_ParseAT; /* Current command parser */
93 GSM_MemoryType SMSType;
96 /* If initialised in debug mode, stdin/out is used instead
97 of ptys for interface. */
98 bool ATEM_Initialise(int read_fd, int write_fd, char *model, char *port)
103 strncpy(ModelName,model,80);
104 strncpy(PortName,port,80);
106 /* Initialise command buffer variables */
107 CurrentCmdBuffer = 0;
108 CurrentCmdBufferIndex = 0;
110 /* Default to verbose reponses */
111 VerboseResponse = true;
113 /* Initialise registers */
114 ATEM_InitRegisters();
116 /* Initial parser is AT routine */
117 Parser = ATEM_ParseAT;
119 /* Setup defaults for AT*C interpreter. */
123 /* Default message format is PDU */
124 MessageFormat = PDU_MODE;
126 /* Set the call passup so that we get notified of incoming calls */
127 GSM->DialData(NULL,-1,&ATEM_CallPassup);
129 /* We're ready to roll... */
130 ATEM_Initialised = true;
134 /* Initialise the "registers" used by the virtual modem. */
135 void ATEM_InitRegisters(void)
138 ModemRegisters[REG_RINGATA] = 0;
139 ModemRegisters[REG_RINGCNT] = 2;
140 ModemRegisters[REG_ESC] = '+';
141 ModemRegisters[REG_CR] = 10;
142 ModemRegisters[REG_LF] = 13;
143 ModemRegisters[REG_BS] = 8;
144 ModemRegisters[S35]=7;
145 ModemRegisters[REG_ECHO] = BIT_ECHO;
150 /* This gets called to indicate an incoming call */
152 void ATEM_CallPassup(char c)
154 if ((c >= 0) && (c < 9)) {
155 ATEM_ModemResult(MR_RING);
161 /* Handler called when characters received from serial port.
162 calls state machine code to process it. */
164 void ATEM_HandleIncomingData(char *buffer, int length)
167 unsigned char out_buf[3];
169 for (count = 0; count < length ; count++) {
170 /* Echo character if appropriate. */
171 if (ModemRegisters[REG_ECHO] & BIT_ECHO) {
172 out_buf[0] = buffer[count];
174 ATEM_StringOut(out_buf);
177 /* If it's a command terminator character, parse what
178 we have so far then go to next buffer. */
179 if (buffer[count] == ModemRegisters[REG_CR] ||
180 buffer[count] == ModemRegisters[REG_LF]) {
182 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = 0x00;
183 Parser(CmdBuffer[CurrentCmdBuffer]);
186 if (CurrentCmdBuffer >= MAX_CMD_BUFFERS) {
187 CurrentCmdBuffer = 0;
189 CurrentCmdBufferIndex = 0;
191 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = buffer[count];
192 CurrentCmdBufferIndex++;
193 if (CurrentCmdBufferIndex >= CMD_BUFFER_LENGTH) {
194 CurrentCmdBufferIndex = CMD_BUFFER_LENGTH;
201 /* Parser for standard AT commands. cmd_buffer must be null terminated. */
202 void ATEM_ParseAT(char *cmd_buffer)
207 if (strncasecmp (cmd_buffer, "AT", 2) != 0) {
208 ATEM_ModemResult(MR_ERROR);
212 for (buf = &cmd_buffer[2]; *buf;) {
213 switch (toupper(*buf)) {
220 /* For now we'll also initialise the datapump + rlp code again */
221 DP_Initialise(PtyRDFD, PtyWRFD);
222 GSM->DialData(NULL, -1, &DP_CallPassup);
223 GSM->AnswerCall(IncomingCallNo);
229 /* FIXME - should parse this better */
230 /* For now we'll also initialise the datapump + rlp code again */
231 DP_Initialise(PtyRDFD, PtyWRFD);
233 if (toupper(*buf) == 'T') buf++;
234 if (*buf == ' ') buf++;
235 strncpy(number, buf, 30);
236 if (ModemRegisters[S35] == 0) GSM->DialData(number, 1, &DP_CallPassup);
237 else GSM->DialData(number, 0, &DP_CallPassup);
238 ATEM_StringOut("\n\r");
245 RLP_SetUserRequest(Disc_Req, true);
249 /* Change registers - only no. 35 for now */
251 if (memcmp(buf, "35=", 3) == 0) {
253 ModemRegisters[S35] = *buf - '0';
257 /* E - Turn Echo on/off */
260 switch (ATEM_GetNum(&buf)) {
262 ModemRegisters[REG_ECHO] &= ~BIT_ECHO;
265 ModemRegisters[REG_ECHO] |= BIT_ECHO;
268 ATEM_ModemResult(MR_ERROR);
273 /* Handle AT* commands (Nokia proprietary I think) */
276 if (!strcasecmp(buf, "NOKIATEST")) {
277 ATEM_ModemResult(MR_OK); /* FIXME? */
280 if (!strcasecmp(buf, "C")) {
281 ATEM_ModemResult(MR_OK);
282 Parser = ATEM_ParseSMS;
288 /* + is the precursor to another set of commands +CSQ, +FCLASS etc. */
291 switch (toupper(*buf)) {
294 /* Returns true if error occured */
295 if (ATEM_CommandPlusC(&buf) == true) {
302 /* Returns true if error occured */
303 if (ATEM_CommandPlusG(&buf) == true) {
309 ATEM_ModemResult(MR_ERROR);
315 ATEM_ModemResult(MR_ERROR);
320 ATEM_ModemResult(MR_OK);
323 static GSM_Error ATEM_ReadSMS(int number, GSM_MemoryType type, GSM_SMSMessage *message)
327 message->MemoryType = type;
328 message->Number = number;
329 error = GSM->GetSMSMessage(message);
334 static void ATEM_PrintSMS(char *line, GSM_SMSMessage *message, int mode)
338 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);
341 if ((message->DCS.Type == SMS_GeneralDataCoding) &&
342 (message->DCS.u.General.Alphabet == SMS_8bit))
343 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>"));
345 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);
348 gsprintf(line, MAX_LINE_LENGTH, _("<Not implemented>"));
351 gsprintf(line, MAX_LINE_LENGTH, _("<Unknown mode>"));
356 static void ATEM_EraseSMS(int number, GSM_MemoryType type)
358 GSM_SMSMessage message;
359 message.MemoryType = type;
360 message.Number = number;
361 if (GSM->DeleteSMSMessage(&message) == GE_NONE) {
362 ATEM_ModemResult(MR_OK);
364 ATEM_ModemResult(MR_ERROR);
369 static void ATEM_HandleSMS()
371 GSM_SMSMessage message;
373 char buffer[MAX_LINE_LENGTH];
375 error = ATEM_ReadSMS(SMSNumber, SMSType, &message);
378 ATEM_PrintSMS(buffer, &message, INTERACT_MODE);
379 ATEM_StringOut(buffer);
382 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rNo message under number %d\n\r"), SMSNumber);
383 ATEM_StringOut(buffer);
389 /* Parser for SMS interactive mode */
390 void ATEM_ParseSMS(char *buff)
392 if (!strcasecmp(buff, "HELP")) {
393 ATEM_StringOut(_("\n\rThe following commands work...\n\r"));
394 ATEM_StringOut("DIR\n\r");
395 ATEM_StringOut("EXIT\n\r");
396 ATEM_StringOut("HELP\n\r");
400 if (!strcasecmp(buff, "DIR")) {
403 Parser = ATEM_ParseDIR;
406 if (!strcasecmp(buff, "EXIT")) {
407 Parser = ATEM_ParseAT;
408 ATEM_ModemResult(MR_OK);
411 ATEM_ModemResult(MR_ERROR);
414 /* Parser for DIR sub mode of SMS interactive mode. */
415 void ATEM_ParseDIR(char *buff)
417 switch (toupper(*buff)) {
427 ATEM_EraseSMS(SMSNumber, SMSType);
430 Parser= ATEM_ParseSMS;
431 ATEM_ModemResult(MR_OK);
434 ATEM_ModemResult(MR_ERROR);
437 /* Handle AT+C commands, this is a quick hack together at this
439 bool ATEM_CommandPlusC(char **buf)
442 GSM_RFUnits rfunits = GRF_CSQ;
443 char buffer[MAX_LINE_LENGTH], buffer2[MAX_LINE_LENGTH];
446 GSM_SMSMessage message;
448 if (strncasecmp(*buf, "SQ", 2) == 0) {
451 if (GSM->GetRFLevel(&rfunits, &rflevel) == GE_NONE) {
452 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CSQ: %0.0f, 99", rflevel);
453 ATEM_StringOut(buffer);
459 /* AT+CGMI is Manufacturer information for the ME (phone) so
460 it should be Nokia rather than gnokii... */
461 if (strncasecmp(*buf, "GMI", 3) == 0) {
463 ATEM_StringOut(_("\n\rNokia Mobile Phones"));
467 /* AT+CGSN is IMEI */
468 if (strncasecmp(*buf, "GSN", 3) == 0) {
470 if (GSM->GetIMEI(buffer2) == GE_NONE) {
471 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
472 ATEM_StringOut(buffer);
479 /* AT+CGMR is Revision (hardware) */
480 if (strncasecmp(*buf, "GMR", 3) == 0) {
482 if (GSM->GetRevision(buffer2) == GE_NONE) {
483 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
484 ATEM_StringOut(buffer);
491 /* AT+CGMM is Model code */
492 if (strncasecmp(*buf, "GMM", 3) == 0) {
494 if (GSM->GetModel(buffer2) == GE_NONE) {
495 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
496 ATEM_StringOut(buffer);
503 /* AT+CMGF is mode selection for message format */
504 if (strncasecmp(*buf, "MGF", 3) == 0) {
512 MessageFormat = PDU_MODE;
516 MessageFormat = TEXT_MODE;
524 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGF: %d", MessageFormat);
525 ATEM_StringOut(buffer);
533 /* AT+CMGR is reading a message */
534 if (strncasecmp(*buf, "MGR", 3) == 0) {
539 sscanf(*buf, "%d", &index);
540 buf[0] += strlen(*buf);
542 error = ATEM_ReadSMS(index, SMSType, &message);
545 ATEM_PrintSMS(buffer2, &message, MessageFormat);
546 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGR: %s", buffer2);
547 ATEM_StringOut(buffer);
550 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
551 ATEM_StringOut(buffer);
561 /* AT+CMGL is listing messages */
562 if (strncasecmp(*buf, "MGL", 3) == 0) {
570 /* process <stat> parameter */
571 if (*(*buf-1) == 0 || /* i.e. no parameter given */
572 strcasecmp(*buf, "1") == 0 ||
573 strcasecmp(*buf, "3") == 0 ||
574 strcasecmp(*buf, "\"REC READ\"") == 0 ||
575 strcasecmp(*buf, "\"STO SENT\"") == 0) {
577 } else if (strcasecmp(*buf, "0") == 0 ||
578 strcasecmp(*buf, "2") == 0 ||
579 strcasecmp(*buf, "\"REC UNREAD\"") == 0 ||
580 strcasecmp(*buf, "\"STO UNSENT\"") == 0) {
582 } else if (strcasecmp(*buf, "4") == 0 ||
583 strcasecmp(*buf, "\"ALL\"") == 0) {
584 status = 4; /* ALL */
588 buf[0] += strlen(*buf);
590 /* check all message storages */
591 for (index = 1; index <= 20; index++) {
592 error = ATEM_ReadSMS(index, SMSType, &message);
595 /* print messsage if it has the required status */
596 if (message.Status == status || status == 4 /* ALL */) {
597 ATEM_PrintSMS(buffer2, &message, MessageFormat);
598 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGL: %d,%s", index, buffer2);
599 ATEM_StringOut(buffer);
602 case GE_EMPTYSMSLOCATION:
603 /* don't care if this storage is empty */
606 /* print other error codes and quit */
607 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
608 ATEM_StringOut(buffer);
622 /* AT+G commands. Some of these responses are a bit tongue in cheek... */
623 bool ATEM_CommandPlusG(char **buf)
625 char buffer[MAX_LINE_LENGTH];
627 /* AT+GMI is Manufacturer information for the TA (Terminal Adaptor) */
628 if (strncasecmp(*buf, "MI", 3) == 0) {
631 ATEM_StringOut(_("\n\rHugh Blemings, Pavel Janík ml. and others..."));
635 /* AT+GMR is Revision information for the TA (Terminal Adaptor) */
636 if (strncasecmp(*buf, "MR", 3) == 0) {
638 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s %s %s", VERSION, __TIME__, __DATE__);
640 ATEM_StringOut(buffer);
644 /* AT+GMM is Model information for the TA (Terminal Adaptor) */
645 if (strncasecmp(*buf, "MM", 3) == 0) {
648 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rgnokii configured for %s on %s"), ModelName, PortName);
649 ATEM_StringOut(buffer);
653 /* AT+GSN is Serial number for the TA (Terminal Adaptor) */
654 if (strncasecmp(*buf, "SN", 3) == 0) {
657 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rnone built in, choose your own"));
658 ATEM_StringOut(buffer);
665 /* Send a result string back. There is much work to do here, see
666 the code in the isdn driver for an idea of where it's heading... */
667 void ATEM_ModemResult(int code)
671 if (VerboseResponse == false) {
672 sprintf(buffer, "\n\r%d\n\r", code);
673 ATEM_StringOut(buffer);
677 ATEM_StringOut("\n\rOK\n\r");
681 ATEM_StringOut("\n\rERROR\n\r");
685 ATEM_StringOut("\n\rCARRIER\n\r");
689 ATEM_StringOut("\n\rCONNECT\n\r");
693 ATEM_StringOut("\n\rNO CARRIER\n\r");
696 ATEM_StringOut("RING\n\r");
699 ATEM_StringOut(_("\n\rUnknown Result Code!\n\r"));
707 /* Get integer from char-pointer, set pointer to end of number
708 stolen basically verbatim from ISDN code. */
709 int ATEM_GetNum(char **p)
713 while (*p[0] >= '0' && *p[0] <= '9') {
714 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
720 /* Write string to virtual modem port, either pty or
721 STDOUT as appropriate. This function is only used during
722 command mode - data pump is used when connected. */
723 void ATEM_StringOut(char *buffer)
728 while (count < strlen(buffer)) {
730 /* Translate CR/LF/BS as appropriate */
731 switch (buffer[count]) {
733 out_char = ModemRegisters[REG_CR];
736 out_char = ModemRegisters[REG_LF];
739 out_char = ModemRegisters[REG_BS];
742 out_char = buffer[count];
746 write(PtyWRFD, &out_char, 1);