7 A Linux/Unix toolset and driver for Nokia mobile phones.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
11 This file provides a virtual modem or "AT" interface to the GSM phone by
12 calling code in gsm-api.c. Inspired by and in places copied from the Linux
13 kernel AT Emulator IDSN code by Fritz Elfert and others.
17 #define __data_at_emulator_c
27 #include <sys/types.h>
40 #include "gsm-common.h"
42 #include "data/at-emulator.h"
43 #include "data/virtmodem.h"
44 #include "data/datapump.h"
46 #define MAX_LINE_LENGTH 256
48 /* Global variables */
49 bool ATEM_Initialised = false; /* Set to true once initialised */
50 extern bool CommandMode;
51 extern int ConnectCount;
53 char ModelName[80]; /* This seems to be needed to avoid seg-faults */
58 int PtyRDFD; /* File descriptor for reading and writing to/from */
59 int PtyWRFD; /* pty interface - only different in debug mode. */
61 u8 ModemRegisters[MAX_MODEM_REGISTERS];
62 char CmdBuffer[MAX_CMD_BUFFERS][CMD_BUFFER_LENGTH];
64 int CurrentCmdBufferIndex;
65 bool VerboseResponse; /* Switch betweek numeric (4) and text responses (ERROR) */
67 int MessageFormat; /* Message Format (text or pdu) */
69 /* Current command parser */
70 void (*Parser)(char *);
71 //void (*Parser)(char *) = ATEM_ParseAT; /* Current command parser */
73 GSM_MemoryType SMSType;
76 /* If initialised in debug mode, stdin/out is used instead
77 of ptys for interface. */
78 bool ATEM_Initialise(int read_fd, int write_fd, char *model, char *port)
83 strncpy(ModelName,model,80);
84 strncpy(PortName,port,80);
86 /* Initialise command buffer variables */
88 CurrentCmdBufferIndex = 0;
90 /* Default to verbose reponses */
91 VerboseResponse = true;
93 /* Initialise registers */
96 /* Initial parser is AT routine */
97 Parser = ATEM_ParseAT;
99 /* Setup defaults for AT*C interpreter. */
103 /* Default message format is PDU */
104 MessageFormat = PDU_MODE;
106 /* Set the call passup so that we get notified of incoming calls */
107 GSM->DialData(NULL,-1,&ATEM_CallPassup);
109 /* We're ready to roll... */
110 ATEM_Initialised = true;
114 /* Initialise the "registers" used by the virtual modem. */
115 void ATEM_InitRegisters(void)
118 ModemRegisters[REG_RINGATA] = 0;
119 ModemRegisters[REG_RINGCNT] = 2;
120 ModemRegisters[REG_ESC] = '+';
121 ModemRegisters[REG_CR] = 10;
122 ModemRegisters[REG_LF] = 13;
123 ModemRegisters[REG_BS] = 8;
124 ModemRegisters[S35]=7;
125 ModemRegisters[REG_ECHO] = BIT_ECHO;
130 /* This gets called to indicate an incoming call */
132 void ATEM_CallPassup(char c)
134 if ((c >= 0) && (c < 9)) {
135 ATEM_ModemResult(MR_RING);
141 /* Handler called when characters received from serial port.
142 calls state machine code to process it. */
144 void ATEM_HandleIncomingData(char *buffer, int length)
147 unsigned char out_buf[3];
149 for (count = 0; count < length ; count++) {
150 /* Echo character if appropriate. */
151 if (ModemRegisters[REG_ECHO] & BIT_ECHO) {
152 out_buf[0] = buffer[count];
154 ATEM_StringOut(out_buf);
157 /* If it's a command terminator character, parse what
158 we have so far then go to next buffer. */
159 if (buffer[count] == ModemRegisters[REG_CR] ||
160 buffer[count] == ModemRegisters[REG_LF]) {
162 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = 0x00;
163 Parser(CmdBuffer[CurrentCmdBuffer]);
166 if (CurrentCmdBuffer >= MAX_CMD_BUFFERS) {
167 CurrentCmdBuffer = 0;
169 CurrentCmdBufferIndex = 0;
171 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = buffer[count];
172 CurrentCmdBufferIndex++;
173 if (CurrentCmdBufferIndex >= CMD_BUFFER_LENGTH) {
174 CurrentCmdBufferIndex = CMD_BUFFER_LENGTH;
181 /* Parser for standard AT commands. cmd_buffer must be null terminated. */
182 void ATEM_ParseAT(char *cmd_buffer)
187 if (strncasecmp (cmd_buffer, "AT", 2) != 0) {
188 ATEM_ModemResult(MR_ERROR);
192 for (buf = &cmd_buffer[2]; *buf;) {
193 switch (toupper(*buf)) {
200 /* For now we'll also initialise the datapump + rlp code again */
201 DP_Initialise(PtyRDFD, PtyWRFD);
202 GSM->DialData(NULL, -1, &DP_CallPassup);
203 GSM->AnswerCall(IncomingCallNo);
209 /* FIXME - should parse this better */
210 /* For now we'll also initialise the datapump + rlp code again */
211 DP_Initialise(PtyRDFD, PtyWRFD);
213 if (toupper(*buf) == 'T') buf++;
214 if (*buf == ' ') buf++;
215 strncpy(number, buf, 30);
216 if (ModemRegisters[S35] == 0) GSM->DialData(number, 1, &DP_CallPassup);
217 else GSM->DialData(number, 0, &DP_CallPassup);
218 ATEM_StringOut("\n\r");
225 RLP_SetUserRequest(Disc_Req, true);
229 /* Change registers - only no. 35 for now */
231 if (memcmp(buf, "35=", 3) == 0) {
233 ModemRegisters[S35] = *buf - '0';
237 /* E - Turn Echo on/off */
240 switch (ATEM_GetNum(&buf)) {
242 ModemRegisters[REG_ECHO] &= ~BIT_ECHO;
245 ModemRegisters[REG_ECHO] |= BIT_ECHO;
248 ATEM_ModemResult(MR_ERROR);
253 /* Handle AT* commands (Nokia proprietary I think) */
256 if (!strcasecmp(buf, "NOKIATEST")) {
257 ATEM_ModemResult(MR_OK); /* FIXME? */
260 if (!strcasecmp(buf, "C")) {
261 ATEM_ModemResult(MR_OK);
262 Parser = ATEM_ParseSMS;
268 /* + is the precursor to another set of commands +CSQ, +FCLASS etc. */
271 switch (toupper(*buf)) {
274 /* Returns true if error occured */
275 if (ATEM_CommandPlusC(&buf) == true) {
282 /* Returns true if error occured */
283 if (ATEM_CommandPlusG(&buf) == true) {
289 ATEM_ModemResult(MR_ERROR);
295 ATEM_ModemResult(MR_ERROR);
300 ATEM_ModemResult(MR_OK);
303 static GSM_Error ATEM_ReadSMS(int number, GSM_MemoryType type, GSM_SMSMessage *message)
307 message->MemoryType = type;
308 message->Location = number;
309 error = GSM->GetSMSMessage(message);
314 static void ATEM_PrintSMS(char *line, GSM_SMSMessage *message, int mode)
318 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->Sender, message->MessageCenter.Number, message->MessageText);
321 if (message->Coding==GSM_Coding_8bit)
322 gsprintf(line, MAX_LINE_LENGTH, _("\"%s\",\"%s\",,\"%02d/%02d/%02d,%02d:%02d:%02d+%02d\"\n\r%s"), (message->Status ? _("REC READ") : _("REC UNREAD")), message->Sender, message->Time.Year, message->Time.Month, message->Time.Day, message->Time.Hour, message->Time.Minute, message->Time.Second, message->Time.Timezone, _("<Not implemented>"));
324 gsprintf(line, MAX_LINE_LENGTH, _("\"%s\",\"%s\",,\"%02d/%02d/%02d,%02d:%02d:%02d+%02d\"\n\r%s"), (message->Status ? _("REC READ") : _("REC UNREAD")), message->Sender, message->Time.Year, message->Time.Month, message->Time.Day, message->Time.Hour, message->Time.Minute, message->Time.Second, message->Time.Timezone, message->MessageText);
327 gsprintf(line, MAX_LINE_LENGTH, _("<Not implemented>"));
330 gsprintf(line, MAX_LINE_LENGTH, _("<Unknown mode>"));
335 static void ATEM_EraseSMS(int number, GSM_MemoryType type)
337 GSM_SMSMessage message;
338 message.MemoryType = type;
339 message.Location = number;
340 if (GSM->DeleteSMSMessage(&message) == GE_NONE) {
341 ATEM_ModemResult(MR_OK);
343 ATEM_ModemResult(MR_ERROR);
348 static void ATEM_HandleSMS()
350 GSM_SMSMessage message;
352 char buffer[MAX_LINE_LENGTH];
354 error = ATEM_ReadSMS(SMSNumber, SMSType, &message);
357 ATEM_PrintSMS(buffer, &message, INTERACT_MODE);
358 ATEM_StringOut(buffer);
361 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rNo message under number %d\n\r"), SMSNumber);
362 ATEM_StringOut(buffer);
368 /* Parser for SMS interactive mode */
369 void ATEM_ParseSMS(char *buff)
371 if (!strcasecmp(buff, "HELP")) {
372 ATEM_StringOut(_("\n\rThe following commands work...\n\r"));
373 ATEM_StringOut("DIR\n\r");
374 ATEM_StringOut("EXIT\n\r");
375 ATEM_StringOut("HELP\n\r");
379 if (!strcasecmp(buff, "DIR")) {
382 Parser = ATEM_ParseDIR;
385 if (!strcasecmp(buff, "EXIT")) {
386 Parser = ATEM_ParseAT;
387 ATEM_ModemResult(MR_OK);
390 ATEM_ModemResult(MR_ERROR);
393 /* Parser for DIR sub mode of SMS interactive mode. */
394 void ATEM_ParseDIR(char *buff)
396 switch (toupper(*buff)) {
406 ATEM_EraseSMS(SMSNumber, SMSType);
409 Parser= ATEM_ParseSMS;
410 ATEM_ModemResult(MR_OK);
413 ATEM_ModemResult(MR_ERROR);
416 /* Handle AT+C commands, this is a quick hack together at this
418 bool ATEM_CommandPlusC(char **buf)
421 GSM_RFUnits rfunits = GRF_CSQ;
422 char buffer[MAX_LINE_LENGTH], buffer2[MAX_LINE_LENGTH];
425 GSM_SMSMessage message;
427 if (strncasecmp(*buf, "SQ", 2) == 0) {
430 if (GSM->GetRFLevel(&rfunits, &rflevel) == GE_NONE) {
431 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CSQ: %0.0f, 99", rflevel);
432 ATEM_StringOut(buffer);
438 /* AT+CGMI is Manufacturer information for the ME (phone) so
439 it should be Nokia rather than gnokii... */
440 if (strncasecmp(*buf, "GMI", 3) == 0) {
442 ATEM_StringOut(_("\n\rNokia Mobile Phones"));
446 /* AT+CGSN is IMEI */
447 if (strncasecmp(*buf, "GSN", 3) == 0) {
449 if (GSM->GetIMEI(buffer2) == GE_NONE) {
450 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
451 ATEM_StringOut(buffer);
458 /* AT+CGMR is Revision (hardware) */
459 if (strncasecmp(*buf, "GMR", 3) == 0) {
461 if (GSM->GetRevision(buffer2) == GE_NONE) {
462 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
463 ATEM_StringOut(buffer);
470 /* AT+CGMM is Model code */
471 if (strncasecmp(*buf, "GMM", 3) == 0) {
473 if (GSM->GetModel(buffer2) == GE_NONE) {
474 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
475 ATEM_StringOut(buffer);
482 /* AT+CMGF is mode selection for message format */
483 if (strncasecmp(*buf, "MGF", 3) == 0) {
491 MessageFormat = PDU_MODE;
495 MessageFormat = TEXT_MODE;
503 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGF: %d", MessageFormat);
504 ATEM_StringOut(buffer);
512 /* AT+CMGR is reading a message */
513 if (strncasecmp(*buf, "MGR", 3) == 0) {
518 sscanf(*buf, "%d", &index);
519 buf[0] += strlen(*buf);
521 error = ATEM_ReadSMS(index, SMSType, &message);
524 ATEM_PrintSMS(buffer2, &message, MessageFormat);
525 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGR: %s", buffer2);
526 ATEM_StringOut(buffer);
529 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
530 ATEM_StringOut(buffer);
540 /* AT+CMGL is listing messages */
541 if (strncasecmp(*buf, "MGL", 3) == 0) {
549 /* process <stat> parameter */
550 if (*(*buf-1) == 0 || /* i.e. no parameter given */
551 strcasecmp(*buf, "1") == 0 ||
552 strcasecmp(*buf, "3") == 0 ||
553 strcasecmp(*buf, "\"REC READ\"") == 0 ||
554 strcasecmp(*buf, "\"STO SENT\"") == 0) {
555 status = GSS_SENTREAD;
556 } else if (strcasecmp(*buf, "0") == 0 ||
557 strcasecmp(*buf, "2") == 0 ||
558 strcasecmp(*buf, "\"REC UNREAD\"") == 0 ||
559 strcasecmp(*buf, "\"STO UNSENT\"") == 0) {
560 status = GSS_NOTSENTREAD;
561 } else if (strcasecmp(*buf, "4") == 0 ||
562 strcasecmp(*buf, "\"ALL\"") == 0) {
563 status = 4; /* ALL */
567 buf[0] += strlen(*buf);
569 /* check all message storages */
570 for (index = 1; index <= 20; index++) {
571 error = ATEM_ReadSMS(index, SMSType, &message);
574 /* print messsage if it has the required status */
575 if (message.Status == status || status == 4 /* ALL */) {
576 ATEM_PrintSMS(buffer2, &message, MessageFormat);
577 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGL: %d,%s", index, buffer2);
578 ATEM_StringOut(buffer);
581 case GE_EMPTYSMSLOCATION:
582 /* don't care if this storage is empty */
585 /* print other error codes and quit */
586 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
587 ATEM_StringOut(buffer);
601 /* AT+G commands. Some of these responses are a bit tongue in cheek... */
602 bool ATEM_CommandPlusG(char **buf)
604 char buffer[MAX_LINE_LENGTH];
606 /* AT+GMI is Manufacturer information for the TA (Terminal Adaptor) */
607 if (strncasecmp(*buf, "MI", 3) == 0) {
610 ATEM_StringOut(_("\n\rHugh Blemings, Pavel JanÃk ml. and others..."));
614 /* AT+GMR is Revision information for the TA (Terminal Adaptor) */
615 if (strncasecmp(*buf, "MR", 3) == 0) {
617 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s %s %s", VERSION, __TIME__, __DATE__);
619 ATEM_StringOut(buffer);
623 /* AT+GMM is Model information for the TA (Terminal Adaptor) */
624 if (strncasecmp(*buf, "MM", 3) == 0) {
627 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rgnokii configured for %s on %s"), ModelName, PortName);
628 ATEM_StringOut(buffer);
632 /* AT+GSN is Serial number for the TA (Terminal Adaptor) */
633 if (strncasecmp(*buf, "SN", 3) == 0) {
636 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rnone built in, choose your own"));
637 ATEM_StringOut(buffer);
644 /* Send a result string back. There is much work to do here, see
645 the code in the isdn driver for an idea of where it's heading... */
646 void ATEM_ModemResult(int code)
650 if (VerboseResponse == false) {
651 sprintf(buffer, "\n\r%d\n\r", code);
652 ATEM_StringOut(buffer);
656 ATEM_StringOut("\n\rOK\n\r");
660 ATEM_StringOut("\n\rERROR\n\r");
664 ATEM_StringOut("\n\rCARRIER\n\r");
668 ATEM_StringOut("\n\rCONNECT\n\r");
672 ATEM_StringOut("\n\rNO CARRIER\n\r");
675 ATEM_StringOut("RING\n\r");
678 ATEM_StringOut(_("\n\rUnknown Result Code!\n\r"));
686 /* Get integer from char-pointer, set pointer to end of number
687 stolen basically verbatim from ISDN code. */
688 int ATEM_GetNum(char **p)
692 while (*p[0] >= '0' && *p[0] <= '9') {
693 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
699 /* Write string to virtual modem port, either pty or
700 STDOUT as appropriate. This function is only used during
701 command mode - data pump is used when connected. */
702 void ATEM_StringOut(char *buffer)
707 while (count < strlen(buffer)) {
709 /* Translate CR/LF/BS as appropriate */
710 switch (buffer[count]) {
712 out_char = ModemRegisters[REG_CR];
715 out_char = ModemRegisters[REG_LF];
718 out_char = ModemRegisters[REG_BS];
721 out_char = buffer[count];
725 write(PtyWRFD, &out_char, 1);