This commit was manufactured by cvs2svn to create tag 'bp_uc'.
[gnokii.git] / common / newmodules / newat.c
index 985eb8c..01e051f 100644 (file)
@@ -132,6 +132,7 @@ GSM_Information Nat_Information = {
   "at", /* Supported models in AT commands mode */
   "",
   "",
+  "",
   4,                     /* Max RF Level */
   0,                     /* Min RF Level */
   GRF_Arbitrary,         /* RF level units */
@@ -237,20 +238,30 @@ void Nat_ReplyGetID(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
     fprintf(stdout, _("   Model: RAE-3\n"));
 #endif
     strcpy(Current_Model,"RAE-3");
-
-  } else {
-    while (MessageBuffer[i]!=0x0d && MessageBuffer[i]!=0x0a) {
-      Current_Model[i-10]=MessageBuffer[i];
-      i++;
-    }
-    Current_Model[i-9]=0;
-
+    CurrentMagicError=GE_NONE;
+    return;
+  }
+  if (strncmp("Nokia 6210",MessageBuffer+i,10)==0) {
 #ifdef DEBUG
     fprintf(stdout, _("Message: Mobile phone model identification received:\n"));
-    fprintf(stdout, _("   Model: %s\n"),Current_Model);
+    fprintf(stdout, _("   Model: NPE-3\n"));
 #endif
+    strcpy(Current_Model,"NPE-3");
+    CurrentMagicError=GE_NONE;
+    return;
   }
 
+  while (MessageBuffer[i]!=0x0d && MessageBuffer[i]!=0x0a) {
+    Current_Model[i-10]=MessageBuffer[i];
+    i++;
+  }
+  Current_Model[i-9]=0;
+
+#ifdef DEBUG
+  fprintf(stdout, _("Message: Mobile phone model identification received:\n"));
+  fprintf(stdout, _("   Model: %s\n"),Current_Model);
+#endif
+
   CurrentMagicError=GE_NONE;
 }
 
@@ -277,7 +288,7 @@ void Nat_ReplyGetHW(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
       for (i=12;i<17;i++) fprintf(stdout,_("%c"),MessageBuffer[i]);
       fprintf(stdout, _("\n"));
       /* Some Nokia phones like 9210 return only firmware */
-      if (MessageLength>22) {
+      if (MessageLength>24) {
         fprintf(stdout, _("   Hardware: "));
         for (i=21;i<25;i++) fprintf(stdout,_("%c"),MessageBuffer[i]);
         fprintf(stdout, _("\n"));
@@ -290,7 +301,7 @@ void Nat_ReplyGetHW(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
       Current_Revision[current++]=' ';
       Current_Revision[current++]='H';
       Current_Revision[current++]='W';
-      if (MessageLength>22) {
+      if (MessageLength>24) {
         for (i=21;i<25;i++) Current_Revision[current++]=MessageBuffer[i];
       } else {
         /* Some Nokia phones like 9210 return only firmware */
@@ -385,10 +396,10 @@ GSM_Error Nat_Initialise(char *port_device, char *initlength,
 
   if (Nat_SendIMEIFrame()!=GE_NONE) return GE_TIMEOUT;
 
-  if (Nat_SendHWFrame()!=GE_NONE) return GE_TIMEOUT;
-
   if (Nat_SendIDFrame()!=GE_NONE) return GE_TIMEOUT;
 
+  if (Nat_SendHWFrame()!=GE_NONE) return GE_TIMEOUT;
+
   /* In AT doesn't have any init strings, etc. Phone answered with frame,
      so connection should be enabled ;-) */
   CurrentLinkOK = true;                           
@@ -407,6 +418,15 @@ GSM_Error Nat_GetManufacturer(char* manufacturer)
   return GE_NONE;
 }
 
+void Nat_ReplyPBKSetMemoryTypeError(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
+
+#ifdef DEBUG
+  fprintf(stdout, _("Message: memory setting error\n"));
+#endif
+
+  CurrentPhonebookError=GE_UNKNOWN;
+}
+
 void Nat_ReplyPBKSetMemoryType(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
 
 #ifdef DEBUG
@@ -469,6 +489,15 @@ GSM_Error SetCharset()
   return error;
 }
 
+void Nat_ReplyGetMemoryLocationError(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
+
+#ifdef DEBUG
+  fprintf(stdout, _("Message: phonebook entry receiving error\n"));
+#endif
+
+  CurrentPhonebookError=GE_UNKNOWN;
+}
+
 /* FIXME: M20 can have " inside name. We can't get endpos by finding first " */
 void Nat_ReplyGetMemoryLocation(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
 
@@ -620,6 +649,13 @@ void Nat_ReplyGetMemoryStatusCPBS(u16 MessageLength, u8 *MessageBuffer, u8 Messa
   } else CurrentMemoryStatusError=GE_UNKNOWN;
 }
 
+void Nat_ReplyGetMemoryStatusCPBSError(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
+#ifdef DEBUG
+  fprintf(stdout, _("Message: memory status receiving error\n"));
+#endif
+  CurrentMemoryStatusError=GE_UNKNOWN;
+}
+
 void Nat_ReplyGetMemoryStatusCPBR(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
 
   char *start;
@@ -735,6 +771,8 @@ void Nat_ReplyGetNetworkInfo(u16 MessageLength, u8 *MessageBuffer, u8 MessageTyp
   int i,current;
   GSM_NetworkInfo NullNetworkInfo;
 
+  if (!strncmp(MessageBuffer,"AT+CREG=",8)) return;
+
   /* TODO: checking if phone really registered to network */
 
   current=23;
@@ -771,6 +809,26 @@ void Nat_ReplyGetNetworkInfo(u16 MessageLength, u8 *MessageBuffer, u8 MessageTyp
   CurrentNetworkInfoError = GE_NONE;      
 }
 
+void Nat_ReplyGetNetworkInfoCOPS(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
+  /* Make sure we are expecting NetworkInfo frame */
+  if (CurrentNetworkInfo && CurrentNetworkInfoError == GE_BUSY) {
+#ifdef DEBUG
+    fprintf(stdout, _("Message: network info received\n"));
+#endif
+    if (MessageBuffer[20]=='2') {
+      CurrentNetworkInfo->NetworkCode[0]=MessageBuffer[23];
+      CurrentNetworkInfo->NetworkCode[1]=MessageBuffer[24];
+      CurrentNetworkInfo->NetworkCode[2]=MessageBuffer[25];
+      CurrentNetworkInfo->NetworkCode[3]=' ';
+      CurrentNetworkInfo->NetworkCode[4]=MessageBuffer[26];
+      CurrentNetworkInfo->NetworkCode[5]=MessageBuffer[27];
+      CurrentNetworkInfo->NetworkCode[6]=0;
+
+      CurrentNetworkInfoError = GE_NONE;      
+    }
+  }
+}
+
 GSM_Error Nat_GetNetworkInfo(GSM_NetworkInfo *NetworkInfo)
 {
   GSM_Error error;
@@ -781,6 +839,10 @@ GSM_Error Nat_GetNetworkInfo(GSM_NetworkInfo *NetworkInfo)
   
   error=NULL_SendMessageSequence
     (20, &CurrentNetworkInfoError, 9, 0x00, "AT+CREG?\r");
+  if (error!=GE_NONE) return error;
+
+  error=NULL_SendMessageSequence
+    (20, &CurrentNetworkInfoError, 9, 0x00, "AT+COPS?\r");
 
   CurrentNetworkInfo = NULL;
 
@@ -912,10 +974,11 @@ GSM_Error Nat_PressKey(int key, int event)
 {
   char req[128];
 
-  sprintf(req, "AT+CKPD=?\r");
+  sprintf(req, "AT+CKPD=\"1\",\"20\"\r");
 
   return NULL_SendMessageSequence
     (50, &CurrentPressKeyError, strlen(req), 0x00, req);
+//  return GE_NOTSUPPORTED;
 }
 
 GSM_Error Nat_DialVoice(char *Number) {
@@ -1055,6 +1118,14 @@ void Nat_ReplySetDateTime(u16 MessageLength, u8 *MessageBuffer, u8 MessageType)
   CurrentSetDateTimeError=GE_NONE;
 }
 
+void Nat_ReplyDateTimeError(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
+#ifdef DEBUG
+  fprintf(stdout,_("Message: date & time functions error\n"));
+#endif
+  CurrentSetDateTimeError=GE_UNKNOWN;
+  CurrentDateTimeError=GE_UNKNOWN;
+}
+
 GSM_Error Nat_SetDateTime(GSM_DateTime *date_time)
 {
   char req[128];
@@ -1114,35 +1185,45 @@ GSM_Error Nat_SetAlarm(int alarm_number, GSM_DateTime *date_time)
     (50, &CurrentSetAlarmError, strlen(req), 0x00, req);
 }
 
+void Nat_ReplyAlarmError(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
+#ifdef DEBUG
+  fprintf(stdout,_("Message: alarm functions error\n"));
+#endif
+  CurrentSetAlarmError=GE_UNKNOWN;
+  CurrentAlarmError=GE_UNKNOWN;
+}
+
 void Nat_ReplyGetSMSCenter(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
   int i,start;
   unsigned char buffer[300];
 
   start=18;
 
+#ifdef DEBUG
+  fprintf(stdout, _("Message: SMSC data received\n"));
+#endif
+
   /* FIXME: support for all formats */        
   start+=ExtractOneParameter(MessageBuffer+start, buffer);
   for (i=1;i<strlen(buffer)-1;i++) buffer[i-1]=buffer[i];
   buffer[i-1]=0;
 
-  /* FIXME: when SMSC in empty, I set default (for my network) */
-  /* Any better solution ? */
-//  if (buffer[0]==0) strcpy(buffer,"+48602951111");
-  if (buffer[0]==0) strcpy(buffer,"+48601000310");
-
-  strcpy(CurrentMessageCenter->Number,buffer);
+  if (buffer[0]==0) CurrentMessageCenterError=GE_EMPTYSMSC;
+  else {
+    strcpy(CurrentMessageCenter->Number,buffer);
 
-  /* Some default values. Is it possible to get them ? */
-  CurrentMessageCenter->Name[0]=0;
-  CurrentMessageCenter->DefaultRecipient[0]=0;
-  CurrentMessageCenter->Format=GSMF_Text;
-  CurrentMessageCenter->Validity=GSMV_Max_Time;
+    /* Some default values. Is it possible to get them ? */
+    CurrentMessageCenter->Name[0]=0;
+    CurrentMessageCenter->DefaultRecipient[0]=0;
+    CurrentMessageCenter->Format=GSMF_Text;
+    CurrentMessageCenter->Validity=GSMV_Max_Time;
 
 #ifdef DEBUG
-  fprintf(stdout, _("Message: SMSC number received\n   Number: '%s'\n"),buffer);
+    fprintf(stdout, _("   Number: '%s'\n"),buffer);
 #endif
 
-  CurrentMessageCenterError=GE_NONE;
+    CurrentMessageCenterError=GE_NONE;
+  }
 }
 
 /* We only get SMSC number */
@@ -1232,6 +1313,7 @@ GSM_Error GSM_DecodeETSISMSFrame(GSM_SMSMessage *SMS, unsigned char *req, int le
   SMS_MessageType PDU=SMS_Deliver;
   GSM_ETSISMSMessage ETSI;
   int i,current=0,current2=0;
+  GSM_Error error;
 
   for(i=0;i<req[0]+1;i++) ETSI.SMSCNumber[i]=req[current++];
 
@@ -1287,11 +1369,37 @@ GSM_Error GSM_DecodeETSISMSFrame(GSM_SMSMessage *SMS, unsigned char *req, int le
       break;
   }
 
-  GSM_DecodeETSISMS(SMS, &ETSI);
+  error=GSM_DecodeETSISMS(SMS, &ETSI);
+  if (error!=GE_NONE) return error;
 
   return GE_NONE;
 }
 
+void Nat_ReplyGetSMSMessageError(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
+  int error;
+
+  error=MessageBuffer[MessageLength-2]-'0';
+  error=error+(MessageBuffer[MessageLength-3]-'0')*10;
+  error=error+(MessageBuffer[MessageLength-4]-'0')*100;
+
+#ifdef DEBUG
+  fprintf(stdout, _("Message: SMS Reading failed\n"));
+
+  switch (error) {
+    case 311:fprintf(stdout, _("   No access to memory (no PIN on card ?)\n"));break;
+    case 321:fprintf(stdout, _("   Empty location!\n"));break;
+    default :fprintf(stdout, _("   Error code %i - please report it \n"),error);break;
+  }
+
+#endif
+
+  switch (error) {
+    case 311:CurrentSMSMessageError = GE_NOACCESS;break;
+    case 321:CurrentSMSMessageError = GE_EMPTYSMSLOCATION;break;
+    default :CurrentSMSMessageError = GE_UNKNOWN;break;
+  }
+}
+
 void Nat_ReplyGetSMSMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
   int i,start,j,end;
   unsigned char buffer[300],buffer2[300];
@@ -1570,9 +1678,11 @@ GSM_Error Nat_GetSMSMessage(GSM_SMSMessage *message)
 GSM_Error GSM_EncodeETSISMSFrame(GSM_SMSMessage *SMS, unsigned char *req, int *length, SMS_MessageType PDU)
 {
   GSM_ETSISMSMessage ETSI;
+  GSM_Error error;
   int i,current=0;
 
-  GSM_EncodeETSISMS(SMS, &ETSI, PDU, length);
+  error=GSM_EncodeETSISMS(SMS, &ETSI, PDU, length);
+  if (error!=GE_NONE) return error;
 
   for (i=0;i<ETSI.SMSCNumber[0]+1;i++)
     req[current++]=ETSI.SMSCNumber[i];
@@ -1651,7 +1761,8 @@ GSM_Error Nat_SendSaveSMSMessage(GSM_SMSMessage *message,bool savesms)
 
   switch (SMSATCMGF) {
     case SMS_AT_PDU: /* PDU mode */
-      GSM_EncodeETSISMSFrame(message, req3, &length, PDU);
+      error=GSM_EncodeETSISMSFrame(message, req3, &length, PDU);
+      if (error!=GE_NONE) return error;
 
       if (savesms) 
         sprintf(req2, "AT+CMGW=%i,%i\r",(length-(req3[0]+1)),stat); //saves SMS
@@ -1676,7 +1787,8 @@ GSM_Error Nat_SendSaveSMSMessage(GSM_SMSMessage *message,bool savesms)
         }
       }
 
-      GSM_EncodeETSISMS(message, &ETSISMS, PDU, &length);
+      error=GSM_EncodeETSISMS(message, &ETSISMS, PDU, &length);
+      if (error!=GE_NONE) return error;
 
       sprintf(req4, "AT+CSMP=%i,%i,%i,%i\r",
                 ETSISMS.firstbyte, ETSISMS.TPVP,
@@ -1828,15 +1940,27 @@ GSM_Error Nat_GetSMSStatus(GSM_SMSStatus *Status)
     (10, &CurrentSMSStatusError, 13, 0x00, "AT+CPMS=\"SM\"\r");
 }
 
+struct AT_OneReply ATRepliesError[] = {
+
+{"AT+CALA"  ,Nat_ReplyAlarmError              },
+{"AT+CCLK"  ,Nat_ReplyDateTimeError           },
+{"AT+CMGR=" ,Nat_ReplyGetSMSMessageError      },
+{"AT+CPBR=" ,Nat_ReplyGetMemoryLocationError  },
+{"AT+CPBS?" ,Nat_ReplyGetMemoryStatusCPBSError},
+{"AT+CPBS=" ,Nat_ReplyPBKSetMemoryTypeError   },
+
+{"",NULL}
+};
+
 struct AT_OneReply ATRepliesOK[] = {
 
 {"AT+CCFC=" ,Nat_ReplyCallDivert            },
 {"AT+CGMI\r",Nat_ReplyGetManufacturer       },
 {"AT+CPBS?" ,Nat_ReplyGetMemoryStatusCPBS   },
+{"AT+CPBS=" ,Nat_ReplyPBKSetMemoryType      },
 {"AT+CPBR=?",Nat_ReplyGetMemoryStatusCPBR   },
 {"AT+CPBR=" ,Nat_ReplyGetMemoryLocation     },
 {"AT+CPBW=" ,Nat_ReplyWritePhonebookLocation},
-{"AT+CPBS=" ,Nat_ReplyPBKSetMemoryType      },
 {"AT+CSCS=\"HEX\"",Nat_ReplySetCharset      },
 {"AT+CSCS=\"GSM\"",Nat_ReplySetCharset      },
 {"AT+CSCA?" ,Nat_ReplyGetSMSCenter          },
@@ -1856,7 +1980,8 @@ struct AT_OneReply ATRepliesOK[] = {
 {"AT+CALA?" ,Nat_ReplyGetAlarm              },
 {"AT+CGMM\r",Nat_ReplyGetID                 },
 {"AT+CGMR\r",Nat_ReplyGetHW                 },
-{"AT+CREG?" ,Nat_ReplyGetNetworkInfo        },
+{"AT+CREG"  ,Nat_ReplyGetNetworkInfo        },
+{"AT+COPS?" ,Nat_ReplyGetNetworkInfoCOPS    },
 {"AT+CGSN\r",Nat_ReplyGetIMEI               },
 {"AT+CHUP"  ,Nat_ReplyCancelCall            },
 {"AT+CBC"   ,Nat_ReplyGetBatteryLevel       },
@@ -1882,6 +2007,7 @@ void Nat_DispatchMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
 
   AnswerError=false;
   if (strncmp("ERROR\r",MessageBuffer+(MessageLength-6),6)==0) AnswerError=true;
+  if (strncmp("+CMS ERROR: ",MessageBuffer+(MessageLength-16),12)==0) AnswerError=true;
 
   AnswerOK=false;
   if (strncmp("OK\r",MessageBuffer+(MessageLength-3),3)==0) AnswerOK=true;
@@ -1902,34 +2028,23 @@ void Nat_DispatchMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
     if (ATRepliesOK[i].ReplyFunction==NULL)
       fprintf(stdout,_("Message: unknown AT command\n"));
 #endif
-   
-    if (strncmp("AT\r",MessageBuffer+start,3)==0) {
-#ifdef DEBUG
-      fprintf(stdout, _("Message: used for setting DLR3 to FBUS - part 1 OK\n"));
-#endif
-      CurrentGetHWError=GE_NONE;
-    }
-    if (strncmp("AT&F\r",MessageBuffer+start,5)==0) {
-#ifdef DEBUG
-      fprintf(stdout, _("Message: used for setting DLR3 to FBUS - part 2 OK\n"));
-#endif
-      CurrentGetHWError=GE_NONE;
-    }
-    if (strncmp("AT*NOKIAFBUS\r",MessageBuffer+start,13)==0) {
-#ifdef DEBUG
-      fprintf(stdout, _("Message: Setting DLR3 to FBUS - part 3 OK\n"));
-#endif
-      CurrentGetHWError=GE_NONE;
-    }
   }
 
   if (AnswerError) {
-    if (strncmp("AT*NOKIAFBUS\r",MessageBuffer+start,13)==0) {
+    i=0;
+    while (true) {
+      if (ATRepliesError[i].ReplyFunction==NULL) break;
+      if (strncmp(ATRepliesError[i].ReplyString,MessageBuffer+start,
+           strlen(ATRepliesError[i].ReplyString))==0) {
+        ATRepliesError[i].ReplyFunction(MessageLength-start,MessageBuffer+start,MessageType);
+        break;
+      }
+      i++;
+    }
+
 #ifdef DEBUG
-      fprintf(stdout, _("Message: Setting DLR3 to FBUS - part 3 ERROR\n"));
+    if (ATRepliesError[i].ReplyFunction==NULL)
+      fprintf(stdout,_("Message: unknown error AT command\n"));
 #endif
-
-      CurrentGetHWError=GE_INTERNALERROR;
-    }
   }
 }