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.8.1 2001/11/27 23:06:09 short
19 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
21 Revision 1.1.1.1.2.1 2001/11/27 22:48:37 short
22 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
24 Revision 1.1.1.2 2001/11/27 22:01:13 short
25 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
27 Revision 1.6 2001/11/27 12:19:01 pkot
28 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
30 Revision 1.5 2001/11/08 16:34:19 pkot
31 Updates to work with new libsms
33 Revision 1.4 2001/07/03 15:27:03 pkot
34 AT commands for SMS handling support (Tamas Bondar)
35 Small at-emulator code cleanup (me)
37 Revision 1.3 2001/02/21 19:56:59 chris
38 More fiddling with the directory layout
43 #define __data_at_emulator_c
53 #include <sys/types.h>
66 #include "gsm-common.h"
68 #include "data/at-emulator.h"
69 #include "data/virtmodem.h"
70 #include "data/datapump.h"
72 #define MAX_LINE_LENGTH 256
74 /* Global variables */
75 bool ATEM_Initialised = false; /* Set to true once initialised */
76 extern bool CommandMode;
77 extern int ConnectCount;
79 char ModelName[80]; /* This seems to be needed to avoid seg-faults */
84 int PtyRDFD; /* File descriptor for reading and writing to/from */
85 int PtyWRFD; /* pty interface - only different in debug mode. */
87 u8 ModemRegisters[MAX_MODEM_REGISTERS];
88 char CmdBuffer[MAX_CMD_BUFFERS][CMD_BUFFER_LENGTH];
90 int CurrentCmdBufferIndex;
91 bool VerboseResponse; /* Switch betweek numeric (4) and text responses (ERROR) */
93 int MessageFormat; /* Message Format (text or pdu) */
95 /* Current command parser */
96 void (*Parser)(char *);
97 //void (*Parser)(char *) = ATEM_ParseAT; /* Current command parser */
99 GSM_MemoryType SMSType;
102 /* If initialised in debug mode, stdin/out is used instead
103 of ptys for interface. */
104 bool ATEM_Initialise(int read_fd, int write_fd, char *model, char *port)
109 strncpy(ModelName,model,80);
110 strncpy(PortName,port,80);
112 /* Initialise command buffer variables */
113 CurrentCmdBuffer = 0;
114 CurrentCmdBufferIndex = 0;
116 /* Default to verbose reponses */
117 VerboseResponse = true;
119 /* Initialise registers */
120 ATEM_InitRegisters();
122 /* Initial parser is AT routine */
123 Parser = ATEM_ParseAT;
125 /* Setup defaults for AT*C interpreter. */
129 /* Default message format is PDU */
130 MessageFormat = PDU_MODE;
132 /* Set the call passup so that we get notified of incoming calls */
133 GSM->DialData(NULL,-1,&ATEM_CallPassup);
135 /* We're ready to roll... */
136 ATEM_Initialised = true;
140 /* Initialise the "registers" used by the virtual modem. */
141 void ATEM_InitRegisters(void)
144 ModemRegisters[REG_RINGATA] = 0;
145 ModemRegisters[REG_RINGCNT] = 2;
146 ModemRegisters[REG_ESC] = '+';
147 ModemRegisters[REG_CR] = 10;
148 ModemRegisters[REG_LF] = 13;
149 ModemRegisters[REG_BS] = 8;
150 ModemRegisters[S35]=7;
151 ModemRegisters[REG_ECHO] = BIT_ECHO;
156 /* This gets called to indicate an incoming call */
158 void ATEM_CallPassup(char c)
160 if ((c >= 0) && (c < 9)) {
161 ATEM_ModemResult(MR_RING);
167 /* Handler called when characters received from serial port.
168 calls state machine code to process it. */
170 void ATEM_HandleIncomingData(char *buffer, int length)
173 unsigned char out_buf[3];
175 for (count = 0; count < length ; count++) {
176 /* Echo character if appropriate. */
177 if (ModemRegisters[REG_ECHO] & BIT_ECHO) {
178 out_buf[0] = buffer[count];
180 ATEM_StringOut(out_buf);
183 /* If it's a command terminator character, parse what
184 we have so far then go to next buffer. */
185 if (buffer[count] == ModemRegisters[REG_CR] ||
186 buffer[count] == ModemRegisters[REG_LF]) {
188 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = 0x00;
189 Parser(CmdBuffer[CurrentCmdBuffer]);
192 if (CurrentCmdBuffer >= MAX_CMD_BUFFERS) {
193 CurrentCmdBuffer = 0;
195 CurrentCmdBufferIndex = 0;
197 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = buffer[count];
198 CurrentCmdBufferIndex++;
199 if (CurrentCmdBufferIndex >= CMD_BUFFER_LENGTH) {
200 CurrentCmdBufferIndex = CMD_BUFFER_LENGTH;
207 /* Parser for standard AT commands. cmd_buffer must be null terminated. */
208 void ATEM_ParseAT(char *cmd_buffer)
213 if (strncasecmp (cmd_buffer, "AT", 2) != 0) {
214 ATEM_ModemResult(MR_ERROR);
218 for (buf = &cmd_buffer[2]; *buf;) {
219 switch (toupper(*buf)) {
226 /* For now we'll also initialise the datapump + rlp code again */
227 DP_Initialise(PtyRDFD, PtyWRFD);
228 GSM->DialData(NULL, -1, &DP_CallPassup);
229 GSM->AnswerCall(IncomingCallNo);
235 /* FIXME - should parse this better */
236 /* For now we'll also initialise the datapump + rlp code again */
237 DP_Initialise(PtyRDFD, PtyWRFD);
239 if (toupper(*buf) == 'T') buf++;
240 if (*buf == ' ') buf++;
241 strncpy(number, buf, 30);
242 if (ModemRegisters[S35] == 0) GSM->DialData(number, 1, &DP_CallPassup);
243 else GSM->DialData(number, 0, &DP_CallPassup);
244 ATEM_StringOut("\n\r");
251 RLP_SetUserRequest(Disc_Req, true);
255 /* Change registers - only no. 35 for now */
257 if (memcmp(buf, "35=", 3) == 0) {
259 ModemRegisters[S35] = *buf - '0';
263 /* E - Turn Echo on/off */
266 switch (ATEM_GetNum(&buf)) {
268 ModemRegisters[REG_ECHO] &= ~BIT_ECHO;
271 ModemRegisters[REG_ECHO] |= BIT_ECHO;
274 ATEM_ModemResult(MR_ERROR);
279 /* Handle AT* commands (Nokia proprietary I think) */
282 if (!strcasecmp(buf, "NOKIATEST")) {
283 ATEM_ModemResult(MR_OK); /* FIXME? */
286 if (!strcasecmp(buf, "C")) {
287 ATEM_ModemResult(MR_OK);
288 Parser = ATEM_ParseSMS;
294 /* + is the precursor to another set of commands +CSQ, +FCLASS etc. */
297 switch (toupper(*buf)) {
300 /* Returns true if error occured */
301 if (ATEM_CommandPlusC(&buf) == true) {
308 /* Returns true if error occured */
309 if (ATEM_CommandPlusG(&buf) == true) {
315 ATEM_ModemResult(MR_ERROR);
321 ATEM_ModemResult(MR_ERROR);
326 ATEM_ModemResult(MR_OK);
329 static GSM_Error ATEM_ReadSMS(int number, GSM_MemoryType type, GSM_SMSMessage *message)
333 message->MemoryType = type;
334 message->Number = number;
335 error = GSM->GetSMSMessage(message);
340 static void ATEM_PrintSMS(char *line, GSM_SMSMessage *message, int mode)
344 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);
347 if ((message->DCS.Type == SMS_GeneralDataCoding) &&
348 (message->DCS.u.General.Alphabet == SMS_8bit))
349 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>"));
351 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);
354 gsprintf(line, MAX_LINE_LENGTH, _("<Not implemented>"));
357 gsprintf(line, MAX_LINE_LENGTH, _("<Unknown mode>"));
362 static void ATEM_EraseSMS(int number, GSM_MemoryType type)
364 GSM_SMSMessage message;
365 message.MemoryType = type;
366 message.Number = number;
367 if (GSM->DeleteSMSMessage(&message) == GE_NONE) {
368 ATEM_ModemResult(MR_OK);
370 ATEM_ModemResult(MR_ERROR);
375 static void ATEM_HandleSMS()
377 GSM_SMSMessage message;
379 char buffer[MAX_LINE_LENGTH];
381 error = ATEM_ReadSMS(SMSNumber, SMSType, &message);
384 ATEM_PrintSMS(buffer, &message, INTERACT_MODE);
385 ATEM_StringOut(buffer);
388 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rNo message under number %d\n\r"), SMSNumber);
389 ATEM_StringOut(buffer);
395 /* Parser for SMS interactive mode */
396 void ATEM_ParseSMS(char *buff)
398 if (!strcasecmp(buff, "HELP")) {
399 ATEM_StringOut(_("\n\rThe following commands work...\n\r"));
400 ATEM_StringOut("DIR\n\r");
401 ATEM_StringOut("EXIT\n\r");
402 ATEM_StringOut("HELP\n\r");
406 if (!strcasecmp(buff, "DIR")) {
409 Parser = ATEM_ParseDIR;
412 if (!strcasecmp(buff, "EXIT")) {
413 Parser = ATEM_ParseAT;
414 ATEM_ModemResult(MR_OK);
417 ATEM_ModemResult(MR_ERROR);
420 /* Parser for DIR sub mode of SMS interactive mode. */
421 void ATEM_ParseDIR(char *buff)
423 switch (toupper(*buff)) {
433 ATEM_EraseSMS(SMSNumber, SMSType);
436 Parser= ATEM_ParseSMS;
437 ATEM_ModemResult(MR_OK);
440 ATEM_ModemResult(MR_ERROR);
443 /* Handle AT+C commands, this is a quick hack together at this
445 bool ATEM_CommandPlusC(char **buf)
448 GSM_RFUnits rfunits = GRF_CSQ;
449 char buffer[MAX_LINE_LENGTH], buffer2[MAX_LINE_LENGTH];
452 GSM_SMSMessage message;
454 if (strncasecmp(*buf, "SQ", 2) == 0) {
457 if (GSM->GetRFLevel(&rfunits, &rflevel) == GE_NONE) {
458 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CSQ: %0.0f, 99", rflevel);
459 ATEM_StringOut(buffer);
465 /* AT+CGMI is Manufacturer information for the ME (phone) so
466 it should be Nokia rather than gnokii... */
467 if (strncasecmp(*buf, "GMI", 3) == 0) {
469 ATEM_StringOut(_("\n\rNokia Mobile Phones"));
473 /* AT+CGSN is IMEI */
474 if (strncasecmp(*buf, "GSN", 3) == 0) {
476 if (GSM->GetIMEI(buffer2) == GE_NONE) {
477 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
478 ATEM_StringOut(buffer);
485 /* AT+CGMR is Revision (hardware) */
486 if (strncasecmp(*buf, "GMR", 3) == 0) {
488 if (GSM->GetRevision(buffer2) == GE_NONE) {
489 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
490 ATEM_StringOut(buffer);
497 /* AT+CGMM is Model code */
498 if (strncasecmp(*buf, "GMM", 3) == 0) {
500 if (GSM->GetModel(buffer2) == GE_NONE) {
501 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
502 ATEM_StringOut(buffer);
509 /* AT+CMGF is mode selection for message format */
510 if (strncasecmp(*buf, "MGF", 3) == 0) {
518 MessageFormat = PDU_MODE;
522 MessageFormat = TEXT_MODE;
530 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGF: %d", MessageFormat);
531 ATEM_StringOut(buffer);
539 /* AT+CMGR is reading a message */
540 if (strncasecmp(*buf, "MGR", 3) == 0) {
545 sscanf(*buf, "%d", &index);
546 buf[0] += strlen(*buf);
548 error = ATEM_ReadSMS(index, SMSType, &message);
551 ATEM_PrintSMS(buffer2, &message, MessageFormat);
552 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGR: %s", buffer2);
553 ATEM_StringOut(buffer);
556 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
557 ATEM_StringOut(buffer);
567 /* AT+CMGL is listing messages */
568 if (strncasecmp(*buf, "MGL", 3) == 0) {
576 /* process <stat> parameter */
577 if (*(*buf-1) == 0 || /* i.e. no parameter given */
578 strcasecmp(*buf, "1") == 0 ||
579 strcasecmp(*buf, "3") == 0 ||
580 strcasecmp(*buf, "\"REC READ\"") == 0 ||
581 strcasecmp(*buf, "\"STO SENT\"") == 0) {
583 } else if (strcasecmp(*buf, "0") == 0 ||
584 strcasecmp(*buf, "2") == 0 ||
585 strcasecmp(*buf, "\"REC UNREAD\"") == 0 ||
586 strcasecmp(*buf, "\"STO UNSENT\"") == 0) {
588 } else if (strcasecmp(*buf, "4") == 0 ||
589 strcasecmp(*buf, "\"ALL\"") == 0) {
590 status = 4; /* ALL */
594 buf[0] += strlen(*buf);
596 /* check all message storages */
597 for (index = 1; index <= 20; index++) {
598 error = ATEM_ReadSMS(index, SMSType, &message);
601 /* print messsage if it has the required status */
602 if (message.Status == status || status == 4 /* ALL */) {
603 ATEM_PrintSMS(buffer2, &message, MessageFormat);
604 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGL: %d,%s", index, buffer2);
605 ATEM_StringOut(buffer);
608 case GE_EMPTYSMSLOCATION:
609 /* don't care if this storage is empty */
612 /* print other error codes and quit */
613 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
614 ATEM_StringOut(buffer);
628 /* AT+G commands. Some of these responses are a bit tongue in cheek... */
629 bool ATEM_CommandPlusG(char **buf)
631 char buffer[MAX_LINE_LENGTH];
633 /* AT+GMI is Manufacturer information for the TA (Terminal Adaptor) */
634 if (strncasecmp(*buf, "MI", 3) == 0) {
637 ATEM_StringOut(_("\n\rHugh Blemings, Pavel Janík ml. and others..."));
641 /* AT+GMR is Revision information for the TA (Terminal Adaptor) */
642 if (strncasecmp(*buf, "MR", 3) == 0) {
644 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s %s %s", VERSION, __TIME__, __DATE__);
646 ATEM_StringOut(buffer);
650 /* AT+GMM is Model information for the TA (Terminal Adaptor) */
651 if (strncasecmp(*buf, "MM", 3) == 0) {
654 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rgnokii configured for %s on %s"), ModelName, PortName);
655 ATEM_StringOut(buffer);
659 /* AT+GSN is Serial number for the TA (Terminal Adaptor) */
660 if (strncasecmp(*buf, "SN", 3) == 0) {
663 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rnone built in, choose your own"));
664 ATEM_StringOut(buffer);
671 /* Send a result string back. There is much work to do here, see
672 the code in the isdn driver for an idea of where it's heading... */
673 void ATEM_ModemResult(int code)
677 if (VerboseResponse == false) {
678 sprintf(buffer, "\n\r%d\n\r", code);
679 ATEM_StringOut(buffer);
683 ATEM_StringOut("\n\rOK\n\r");
687 ATEM_StringOut("\n\rERROR\n\r");
691 ATEM_StringOut("\n\rCARRIER\n\r");
695 ATEM_StringOut("\n\rCONNECT\n\r");
699 ATEM_StringOut("\n\rNO CARRIER\n\r");
702 ATEM_StringOut("RING\n\r");
705 ATEM_StringOut(_("\n\rUnknown Result Code!\n\r"));
713 /* Get integer from char-pointer, set pointer to end of number
714 stolen basically verbatim from ISDN code. */
715 int ATEM_GetNum(char **p)
719 while (*p[0] >= '0' && *p[0] <= '9') {
720 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
726 /* Write string to virtual modem port, either pty or
727 STDOUT as appropriate. This function is only used during
728 command mode - data pump is used when connected. */
729 void ATEM_StringOut(char *buffer)
734 while (count < strlen(buffer)) {
736 /* Translate CR/LF/BS as appropriate */
737 switch (buffer[count]) {
739 out_char = ModemRegisters[REG_CR];
742 out_char = ModemRegisters[REG_LF];
745 out_char = ModemRegisters[REG_BS];
748 out_char = buffer[count];
752 write(PtyWRFD, &out_char, 1);