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.
19 #define __data_at_emulator_c
29 #include <sys/types.h>
42 #include "gsm-common.h"
44 #include "data/at-emulator.h"
45 #include "data/virtmodem.h"
46 #include "data/datapump.h"
48 #define MAX_LINE_LENGTH 256
50 /* Global variables */
51 bool ATEM_Initialised = false; /* Set to true once initialised */
52 extern bool CommandMode;
53 extern int ConnectCount;
55 char ModelName[80]; /* This seems to be needed to avoid seg-faults */
60 int PtyRDFD; /* File descriptor for reading and writing to/from */
61 int PtyWRFD; /* pty interface - only different in debug mode. */
63 u8 ModemRegisters[MAX_MODEM_REGISTERS];
64 char CmdBuffer[MAX_CMD_BUFFERS][CMD_BUFFER_LENGTH];
66 int CurrentCmdBufferIndex;
67 bool VerboseResponse; /* Switch betweek numeric (4) and text responses (ERROR) */
69 int MessageFormat; /* Message Format (text or pdu) */
71 /* Current command parser */
72 void (*Parser)(char *);
73 //void (*Parser)(char *) = ATEM_ParseAT; /* Current command parser */
75 GSM_MemoryType SMSType;
78 /* If initialised in debug mode, stdin/out is used instead
79 of ptys for interface. */
80 bool ATEM_Initialise(int read_fd, int write_fd, char *model, char *port)
85 strncpy(ModelName,model,80);
86 strncpy(PortName,port,80);
88 /* Initialise command buffer variables */
90 CurrentCmdBufferIndex = 0;
92 /* Default to verbose reponses */
93 VerboseResponse = true;
95 /* Initialise registers */
98 /* Initial parser is AT routine */
99 Parser = ATEM_ParseAT;
101 /* Setup defaults for AT*C interpreter. */
105 /* Default message format is PDU */
106 MessageFormat = PDU_MODE;
108 /* Set the call passup so that we get notified of incoming calls */
109 GSM->DialData(NULL,-1,&ATEM_CallPassup);
111 /* We're ready to roll... */
112 ATEM_Initialised = true;
116 /* Initialise the "registers" used by the virtual modem. */
117 void ATEM_InitRegisters(void)
120 ModemRegisters[REG_RINGATA] = 0;
121 ModemRegisters[REG_RINGCNT] = 2;
122 ModemRegisters[REG_ESC] = '+';
123 ModemRegisters[REG_CR] = 10;
124 ModemRegisters[REG_LF] = 13;
125 ModemRegisters[REG_BS] = 8;
126 ModemRegisters[S35]=7;
127 ModemRegisters[REG_ECHO] = BIT_ECHO;
132 /* This gets called to indicate an incoming call */
134 void ATEM_CallPassup(char c)
136 if ((c >= 0) && (c < 9)) {
137 ATEM_ModemResult(MR_RING);
143 /* Handler called when characters received from serial port.
144 calls state machine code to process it. */
146 void ATEM_HandleIncomingData(char *buffer, int length)
149 unsigned char out_buf[3];
151 for (count = 0; count < length ; count++) {
152 /* Echo character if appropriate. */
153 if (ModemRegisters[REG_ECHO] & BIT_ECHO) {
154 out_buf[0] = buffer[count];
156 ATEM_StringOut(out_buf);
159 /* If it's a command terminator character, parse what
160 we have so far then go to next buffer. */
161 if (buffer[count] == ModemRegisters[REG_CR] ||
162 buffer[count] == ModemRegisters[REG_LF]) {
164 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = 0x00;
165 Parser(CmdBuffer[CurrentCmdBuffer]);
168 if (CurrentCmdBuffer >= MAX_CMD_BUFFERS) {
169 CurrentCmdBuffer = 0;
171 CurrentCmdBufferIndex = 0;
173 CmdBuffer[CurrentCmdBuffer][CurrentCmdBufferIndex] = buffer[count];
174 CurrentCmdBufferIndex++;
175 if (CurrentCmdBufferIndex >= CMD_BUFFER_LENGTH) {
176 CurrentCmdBufferIndex = CMD_BUFFER_LENGTH;
183 /* Parser for standard AT commands. cmd_buffer must be null terminated. */
184 void ATEM_ParseAT(char *cmd_buffer)
189 if (strncasecmp (cmd_buffer, "AT", 2) != 0) {
190 ATEM_ModemResult(MR_ERROR);
194 for (buf = &cmd_buffer[2]; *buf;) {
195 switch (toupper(*buf)) {
202 /* For now we'll also initialise the datapump + rlp code again */
203 DP_Initialise(PtyRDFD, PtyWRFD);
204 GSM->DialData(NULL, -1, &DP_CallPassup);
205 GSM->AnswerCall(IncomingCallNo);
211 /* FIXME - should parse this better */
212 /* For now we'll also initialise the datapump + rlp code again */
213 DP_Initialise(PtyRDFD, PtyWRFD);
215 if (toupper(*buf) == 'T') buf++;
216 if (*buf == ' ') buf++;
217 strncpy(number, buf, 30);
218 if (ModemRegisters[S35] == 0) GSM->DialData(number, 1, &DP_CallPassup);
219 else GSM->DialData(number, 0, &DP_CallPassup);
220 ATEM_StringOut("\n\r");
227 RLP_SetUserRequest(Disc_Req, true);
231 /* Change registers - only no. 35 for now */
233 if (memcmp(buf, "35=", 3) == 0) {
235 ModemRegisters[S35] = *buf - '0';
239 /* E - Turn Echo on/off */
242 switch (ATEM_GetNum(&buf)) {
244 ModemRegisters[REG_ECHO] &= ~BIT_ECHO;
247 ModemRegisters[REG_ECHO] |= BIT_ECHO;
250 ATEM_ModemResult(MR_ERROR);
255 /* Handle AT* commands (Nokia proprietary I think) */
258 if (!strcasecmp(buf, "NOKIATEST")) {
259 ATEM_ModemResult(MR_OK); /* FIXME? */
262 if (!strcasecmp(buf, "C")) {
263 ATEM_ModemResult(MR_OK);
264 Parser = ATEM_ParseSMS;
270 /* + is the precursor to another set of commands +CSQ, +FCLASS etc. */
273 switch (toupper(*buf)) {
276 /* Returns true if error occured */
277 if (ATEM_CommandPlusC(&buf) == true) {
284 /* Returns true if error occured */
285 if (ATEM_CommandPlusG(&buf) == true) {
291 ATEM_ModemResult(MR_ERROR);
297 ATEM_ModemResult(MR_ERROR);
302 ATEM_ModemResult(MR_OK);
305 static GSM_Error ATEM_ReadSMS(int number, GSM_MemoryType type, GSM_SMSMessage *message)
309 message->MemoryType = type;
310 message->Number = number;
311 error = GSM->GetSMSMessage(message);
316 static void ATEM_PrintSMS(char *line, GSM_SMSMessage *message, int mode)
320 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);
323 if ((message->DCS.Type == SMS_GeneralDataCoding) &&
324 (message->DCS.u.General.Alphabet == SMS_8bit))
325 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>"));
327 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);
330 gsprintf(line, MAX_LINE_LENGTH, _("<Not implemented>"));
333 gsprintf(line, MAX_LINE_LENGTH, _("<Unknown mode>"));
338 static void ATEM_EraseSMS(int number, GSM_MemoryType type)
340 GSM_SMSMessage message;
341 message.MemoryType = type;
342 message.Number = number;
343 if (GSM->DeleteSMSMessage(&message) == GE_NONE) {
344 ATEM_ModemResult(MR_OK);
346 ATEM_ModemResult(MR_ERROR);
351 static void ATEM_HandleSMS()
353 GSM_SMSMessage message;
355 char buffer[MAX_LINE_LENGTH];
357 error = ATEM_ReadSMS(SMSNumber, SMSType, &message);
360 ATEM_PrintSMS(buffer, &message, INTERACT_MODE);
361 ATEM_StringOut(buffer);
364 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rNo message under number %d\n\r"), SMSNumber);
365 ATEM_StringOut(buffer);
371 /* Parser for SMS interactive mode */
372 void ATEM_ParseSMS(char *buff)
374 if (!strcasecmp(buff, "HELP")) {
375 ATEM_StringOut(_("\n\rThe following commands work...\n\r"));
376 ATEM_StringOut("DIR\n\r");
377 ATEM_StringOut("EXIT\n\r");
378 ATEM_StringOut("HELP\n\r");
382 if (!strcasecmp(buff, "DIR")) {
385 Parser = ATEM_ParseDIR;
388 if (!strcasecmp(buff, "EXIT")) {
389 Parser = ATEM_ParseAT;
390 ATEM_ModemResult(MR_OK);
393 ATEM_ModemResult(MR_ERROR);
396 /* Parser for DIR sub mode of SMS interactive mode. */
397 void ATEM_ParseDIR(char *buff)
399 switch (toupper(*buff)) {
409 ATEM_EraseSMS(SMSNumber, SMSType);
412 Parser= ATEM_ParseSMS;
413 ATEM_ModemResult(MR_OK);
416 ATEM_ModemResult(MR_ERROR);
419 /* Handle AT+C commands, this is a quick hack together at this
421 bool ATEM_CommandPlusC(char **buf)
424 GSM_RFUnits rfunits = GRF_CSQ;
425 char buffer[MAX_LINE_LENGTH], buffer2[MAX_LINE_LENGTH];
428 GSM_SMSMessage message;
430 if (strncasecmp(*buf, "SQ", 2) == 0) {
433 if (GSM->GetRFLevel(&rfunits, &rflevel) == GE_NONE) {
434 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CSQ: %0.0f, 99", rflevel);
435 ATEM_StringOut(buffer);
441 /* AT+CGMI is Manufacturer information for the ME (phone) so
442 it should be Nokia rather than gnokii... */
443 if (strncasecmp(*buf, "GMI", 3) == 0) {
445 ATEM_StringOut(_("\n\rNokia Mobile Phones"));
449 /* AT+CGSN is IMEI */
450 if (strncasecmp(*buf, "GSN", 3) == 0) {
452 if (GSM->GetIMEI(buffer2) == GE_NONE) {
453 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
454 ATEM_StringOut(buffer);
461 /* AT+CGMR is Revision (hardware) */
462 if (strncasecmp(*buf, "GMR", 3) == 0) {
464 if (GSM->GetRevision(buffer2) == GE_NONE) {
465 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
466 ATEM_StringOut(buffer);
473 /* AT+CGMM is Model code */
474 if (strncasecmp(*buf, "GMM", 3) == 0) {
476 if (GSM->GetModel(buffer2) == GE_NONE) {
477 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s", buffer2);
478 ATEM_StringOut(buffer);
485 /* AT+CMGF is mode selection for message format */
486 if (strncasecmp(*buf, "MGF", 3) == 0) {
494 MessageFormat = PDU_MODE;
498 MessageFormat = TEXT_MODE;
506 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGF: %d", MessageFormat);
507 ATEM_StringOut(buffer);
515 /* AT+CMGR is reading a message */
516 if (strncasecmp(*buf, "MGR", 3) == 0) {
521 sscanf(*buf, "%d", &index);
522 buf[0] += strlen(*buf);
524 error = ATEM_ReadSMS(index, SMSType, &message);
527 ATEM_PrintSMS(buffer2, &message, MessageFormat);
528 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGR: %s", buffer2);
529 ATEM_StringOut(buffer);
532 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
533 ATEM_StringOut(buffer);
543 /* AT+CMGL is listing messages */
544 if (strncasecmp(*buf, "MGL", 3) == 0) {
552 /* process <stat> parameter */
553 if (*(*buf-1) == 0 || /* i.e. no parameter given */
554 strcasecmp(*buf, "1") == 0 ||
555 strcasecmp(*buf, "3") == 0 ||
556 strcasecmp(*buf, "\"REC READ\"") == 0 ||
557 strcasecmp(*buf, "\"STO SENT\"") == 0) {
559 } else if (strcasecmp(*buf, "0") == 0 ||
560 strcasecmp(*buf, "2") == 0 ||
561 strcasecmp(*buf, "\"REC UNREAD\"") == 0 ||
562 strcasecmp(*buf, "\"STO UNSENT\"") == 0) {
564 } else if (strcasecmp(*buf, "4") == 0 ||
565 strcasecmp(*buf, "\"ALL\"") == 0) {
566 status = 4; /* ALL */
570 buf[0] += strlen(*buf);
572 /* check all message storages */
573 for (index = 1; index <= 20; index++) {
574 error = ATEM_ReadSMS(index, SMSType, &message);
577 /* print messsage if it has the required status */
578 if (message.Status == status || status == 4 /* ALL */) {
579 ATEM_PrintSMS(buffer2, &message, MessageFormat);
580 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMGL: %d,%s", index, buffer2);
581 ATEM_StringOut(buffer);
584 case GE_EMPTYSMSLOCATION:
585 /* don't care if this storage is empty */
588 /* print other error codes and quit */
589 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r+CMS ERROR: %d\n\r", error);
590 ATEM_StringOut(buffer);
604 /* AT+G commands. Some of these responses are a bit tongue in cheek... */
605 bool ATEM_CommandPlusG(char **buf)
607 char buffer[MAX_LINE_LENGTH];
609 /* AT+GMI is Manufacturer information for the TA (Terminal Adaptor) */
610 if (strncasecmp(*buf, "MI", 3) == 0) {
613 ATEM_StringOut(_("\n\rHugh Blemings, Pavel Janík ml. and others..."));
617 /* AT+GMR is Revision information for the TA (Terminal Adaptor) */
618 if (strncasecmp(*buf, "MR", 3) == 0) {
620 gsprintf(buffer, MAX_LINE_LENGTH, "\n\r%s %s %s", VERSION, __TIME__, __DATE__);
622 ATEM_StringOut(buffer);
626 /* AT+GMM is Model information for the TA (Terminal Adaptor) */
627 if (strncasecmp(*buf, "MM", 3) == 0) {
630 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rgnokii configured for %s on %s"), ModelName, PortName);
631 ATEM_StringOut(buffer);
635 /* AT+GSN is Serial number for the TA (Terminal Adaptor) */
636 if (strncasecmp(*buf, "SN", 3) == 0) {
639 gsprintf(buffer, MAX_LINE_LENGTH, _("\n\rnone built in, choose your own"));
640 ATEM_StringOut(buffer);
647 /* Send a result string back. There is much work to do here, see
648 the code in the isdn driver for an idea of where it's heading... */
649 void ATEM_ModemResult(int code)
653 if (VerboseResponse == false) {
654 sprintf(buffer, "\n\r%d\n\r", code);
655 ATEM_StringOut(buffer);
659 ATEM_StringOut("\n\rOK\n\r");
663 ATEM_StringOut("\n\rERROR\n\r");
667 ATEM_StringOut("\n\rCARRIER\n\r");
671 ATEM_StringOut("\n\rCONNECT\n\r");
675 ATEM_StringOut("\n\rNO CARRIER\n\r");
678 ATEM_StringOut("RING\n\r");
681 ATEM_StringOut(_("\n\rUnknown Result Code!\n\r"));
689 /* Get integer from char-pointer, set pointer to end of number
690 stolen basically verbatim from ISDN code. */
691 int ATEM_GetNum(char **p)
695 while (*p[0] >= '0' && *p[0] <= '9') {
696 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
702 /* Write string to virtual modem port, either pty or
703 STDOUT as appropriate. This function is only used during
704 command mode - data pump is used when connected. */
705 void ATEM_StringOut(char *buffer)
710 while (count < strlen(buffer)) {
712 /* Translate CR/LF/BS as appropriate */
713 switch (buffer[count]) {
715 out_char = ModemRegisters[REG_CR];
718 out_char = ModemRegisters[REG_LF];
721 out_char = ModemRegisters[REG_BS];
724 out_char = buffer[count];
728 write(PtyWRFD, &out_char, 1);