5 A Linux/Unix toolset and driver for Nokia mobile phones.
7 Released under the terms of the GNU GPL, see file COPYING for more details.
9 This file provides an API for accessing functions on the at and similar
16 /* "Turn on" prototypes in n-at.h */
20 /* System header files */
26 #include "devices/device.h"
30 #include "misc_win32.h"
33 /* Various header file */
39 #include "gsm-coding.h"
40 #include "newmodules/newat.h"
41 #include "newmodules/n6110.h"
42 /* Global variables used by code in gsm-api.c to expose the functions
43 supported by this model of phone. */
50 /* Here we initialise model specific functions. */
52 GSM_Functions Nat_Functions = {
57 Nat_GetMemoryLocation,
58 Nat_WritePhonebookLocation,
72 Nat_GetDisplayStatus, //fill it
73 Nat_EnterSecurityCode,
74 Nat_GetSecurityCodeStatus,
82 Nat_SetAlarm, //doesn't work correctly...
102 Nat_PressKey, //fill it
103 UNIMPLEMENTED, //DisplayOutput= AT+CDIS
127 /* Mobile phone information */
129 GSM_Information Nat_Information = {
130 "", /* Supported models in FBUS */
131 "at", /* Supported models in MBUS */
132 "", /* Supported models in FBUS over infrared */
133 "", /* Supported models in FBUS over DLR3 */
134 "at", /* Supported models in AT commands mode */
137 4, /* Max RF Level */
138 0, /* Min RF Level */
139 GRF_Arbitrary, /* RF level units */
140 4, /* Max Battery Level */
141 0, /* Min Battery Level */
142 GBU_Arbitrary, /* Battery level units */
143 GDT_None, /* No date/time support */
144 GDT_None, /* No alarm support */
145 0 /* Max alarms = 0 */
148 /* Type of SMS mode. 0=PDU mode; 1=text mode */
149 int SMSATCMGF=-1; //-1 when not used earlier
151 #define SMS_AT_TEXT 1
153 /* Type of charset for phonebook 0=GSM; 1=HEX */
154 int PBKATCSCS=-1; //-1 when not used earlier
158 /* Manufacturer of connected AT compatible phone */
159 int ATMan=-1; // -1 when not used earlier
160 #define AT_MAN_NOKIA 0
161 #define AT_MAN_SIEMENS 1
163 int ExtractOneParameter(unsigned char *input, unsigned char *output)
167 while (*input!=',' && *input!=0x0d) {
178 void ATDecodeDateTime(GSM_DateTime *dt, unsigned char *input)
180 dt->Year=2000+(*input-'0')*10; input++;
181 dt->Year=dt->Year+(*input-'0'); input++;
184 dt->Month=(*input-'0')*10; input++;
185 dt->Month=dt->Month+(*input-'0'); input++;
188 dt->Day=(*input-'0')*10; input++;
189 dt->Day=dt->Day+(*input-'0'); input++;
192 dt->Hour=(*input-'0')*10; input++;
193 dt->Hour=dt->Hour+(*input-'0'); input++;
196 dt->Minute=(*input-'0')*10; input++;
197 dt->Minute=dt->Minute+(*input-'0');input++;
200 dt->Second=(*input-'0')*10; input++;
201 dt->Second=dt->Second+(*input-'0');input++;
205 dt->Timezone=(*input-'0')*10; input++;
206 dt->Timezone=dt->Timezone+(*input-'0');input++;
208 if (*input=='-') dt->Timezone=-dt->Timezone;
212 void Nat_ReplyGetIMEI(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
214 strncpy(Current_IMEI,MessageBuffer+10,15);
217 fprintf(stdout, _("Message: IMEI %s received\n"),Current_IMEI);
220 CurrentGetIMEIError=GE_NONE;
223 GSM_Error Nat_SendIMEIFrame()
225 unsigned char req[8] = {"AT+CGSN\r"};
227 return NULL_SendMessageSequence
228 (50, &CurrentGetIMEIError, 8, 0x00, req);
231 void Nat_ReplyGetID(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
236 if (strncmp("Nokia Communicator GSM900/1800",MessageBuffer+i,30)==0) {
238 fprintf(stdout, _("Message: Mobile phone model identification received:\n"));
239 fprintf(stdout, _(" Model: RAE-3\n"));
241 strcpy(Current_Model,"RAE-3");
244 while (MessageBuffer[i]!=0x0d && MessageBuffer[i]!=0x0a) {
245 Current_Model[i-10]=MessageBuffer[i];
248 Current_Model[i-9]=0;
251 fprintf(stdout, _("Message: Mobile phone model identification received:\n"));
252 fprintf(stdout, _(" Model: %s\n"),Current_Model);
256 CurrentMagicError=GE_NONE;
259 GSM_Error Nat_SendIDFrame()
261 unsigned char req[8] = {"AT+CGMM\r"};
263 return NULL_SendMessageSequence
264 (50, &CurrentMagicError, 8, 0x00, req);
267 void Nat_ReplyGetHW(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
272 fprintf(stdout, _("Message: Mobile phone model identification received:\n"));
278 fprintf(stdout, _(" Firmware: "));
279 for (i=12;i<17;i++) fprintf(stdout,_("%c"),MessageBuffer[i]);
280 fprintf(stdout, _("\n"));
281 /* Some Nokia phones like 9210 return only firmware */
282 if (MessageLength>22) {
283 fprintf(stdout, _(" Hardware: "));
284 for (i=21;i<25;i++) fprintf(stdout,_("%c"),MessageBuffer[i]);
285 fprintf(stdout, _("\n"));
289 strcpy(Current_Revision,"SW");current=2;
290 for (i=12;i<17;i++) Current_Revision[current++]=MessageBuffer[i];
291 Current_Revision[current++]=',';
292 Current_Revision[current++]=' ';
293 Current_Revision[current++]='H';
294 Current_Revision[current++]='W';
295 if (MessageLength>22) {
296 for (i=21;i<25;i++) Current_Revision[current++]=MessageBuffer[i];
298 /* Some Nokia phones like 9210 return only firmware */
299 /* Here we copy firmware as hardware */
300 for (i=12;i<17;i++) Current_Revision[current++]=MessageBuffer[i];
305 strcpy(Current_Revision,"SW");current=2;
306 while (MessageBuffer[i]!=0x0d && MessageBuffer[i]!=0x0a) {
307 Current_Revision[current++]=MessageBuffer[i];
310 Current_Revision[current++]=',';
311 Current_Revision[current++]=' ';
312 Current_Revision[current++]='H';
313 Current_Revision[current++]='W';
315 while (MessageBuffer[i]!=0x0d && MessageBuffer[i]!=0x0a) {
316 Current_Revision[current++]=MessageBuffer[i];
322 strcpy(Current_Revision,"SWxx.xx, HWxxxx");
326 CurrentGetHWError=GE_NONE;
329 GSM_Error Nat_SendHWFrame()
331 unsigned char req[8] = {"AT+CGMR\r"};
333 return NULL_SendMessageSequence
334 (50, &CurrentGetHWError, 8, 0x00, req);
337 void Nat_ReplyGetManufacturer(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
340 fprintf(stdout, _("Message: phone manufacturer received\n"));
343 if (strstr(MessageBuffer,"Nokia")) {
345 fprintf(stdout, _(" Nokia\n"));
350 if (strstr(MessageBuffer,"SIEMENS")) {
352 fprintf(stdout, _(" Siemens\n"));
354 ATMan=AT_MAN_SIEMENS;
357 CurrentMagicError=GE_NONE;
360 GSM_Error Nat_SendManufacturerFrame()
362 unsigned char req[8] = {"AT+CGMI\r"};
364 return NULL_SendMessageSequence
365 (50, &CurrentMagicError, 8, 0x00, req);
368 /* Initialise variables and state machine. */
369 GSM_Error Nat_Initialise(char *port_device, char *initlength,
370 GSM_ConnectionType connection,
371 void (*rlp_callback)(RLP_F96Frame *frame))
373 if (Protocol->Initialise(port_device,initlength,connection,rlp_callback)!=GE_NONE)
375 return GE_NOTSUPPORTED;
380 if (Nat_SendManufacturerFrame()!=GE_NONE) return GE_TIMEOUT;
382 if (Nat_SendIMEIFrame()!=GE_NONE) return GE_TIMEOUT;
384 if (Nat_SendHWFrame()!=GE_NONE) return GE_TIMEOUT;
386 if (Nat_SendIDFrame()!=GE_NONE) return GE_TIMEOUT;
388 /* In AT doesn't have any init strings, etc. Phone answered with frame,
389 so connection should be enabled ;-) */
390 CurrentLinkOK = true;
395 GSM_Error Nat_GetManufacturer(char* manufacturer)
398 case AT_MAN_NOKIA :strcpy (manufacturer, "Nokia"); break;
399 case AT_MAN_SIEMENS:strcpy (manufacturer, "Siemens");break;
400 default :strcpy (manufacturer, "unknown");break;
406 void Nat_ReplyPBKSetMemoryType(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
409 fprintf(stdout, _("Message: memory type set OK\n"));
412 CurrentPhonebookError=GE_NONE;
415 static GSM_Error PBKSetMemoryType(GSM_MemoryType type)
417 char req[] = "AT+CPBS=\"XX\"\r";
420 GetMemoryTypeString(req2, &type);
425 return NULL_SendMessageSequence
426 (50, &CurrentPhonebookError, strlen(req), 0x00, req);
429 void Nat_ReplySetCharset(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
431 if (strncmp("AT+CSCS=\"HEX\"",MessageBuffer,13)==0) {
433 fprintf(stdout, _("Message: charset set to HEX\n"));
435 CurrentPhonebookError=GE_NONE;
437 if (strncmp("AT+CSCS=\"GSM\"",MessageBuffer,13)==0) {
439 fprintf(stdout, _("Message: charset set to GSM\n"));
441 CurrentPhonebookError=GE_NONE;
445 GSM_Error SetCharset()
447 unsigned char req [14] = {"AT+CSCS=\"HEX\"\r"}; // HEX charset
448 unsigned char req2[14] = {"AT+CSCS=\"GSM\"\r"}; // GSM charset
451 error=NULL_SendMessageSequence
452 (50, &CurrentPhonebookError, 14, 0x00, req);
453 if (error==GE_NONE) {
454 PBKATCSCS=PBK_AT_HEX;
458 error=NULL_SendMessageSequence
459 (50, &CurrentPhonebookError, 14, 0x00, req2);
460 if (error==GE_NONE) {
461 PBKATCSCS=PBK_AT_GSM;
468 void Nat_ReplyGetMemoryLocation(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
474 fprintf(stdout, _("Message: phonebook entry received\n"));
477 CurrentPhonebookEntry->Empty = true;
478 *(CurrentPhonebookEntry->Name) = '\0';
479 *(CurrentPhonebookEntry->Number) = '\0';
480 CurrentPhonebookEntry->SubEntriesCount=0;
481 CurrentPhonebookEntry->Group = 0;
483 pos = strchr(MessageBuffer, '\"');
486 endpos = strchr(++pos, '\"');
489 strcpy(CurrentPhonebookEntry->Number, pos);
492 if (endpos) pos = strchr(++endpos, '\"');
496 l = pos - (char *)MessageBuffer;
497 endpos = memchr(pos, '\"', MessageLength - l);
503 DecodeDefault(CurrentPhonebookEntry->Name, pos, l);
506 DecodeHexBin(CurrentPhonebookEntry->Name, pos, l);
512 CurrentPhonebookError=GE_NONE;
515 GSM_Error Nat_GetMemoryLocation(GSM_PhonebookEntry *entry)
518 char req[] = "AT+CPBR=00000\r";
522 if (error!=GE_NONE) return error;
525 error = PBKSetMemoryType(entry->MemoryType);
526 if (error != GE_NONE) return error;
528 CurrentPhonebookEntry = entry;
530 sprintf(req + 8, "%5d\r", entry->Location);
532 return NULL_SendMessageSequence
533 (50, &CurrentPhonebookError, strlen(req), 0x00, req);
536 void Nat_ReplyWritePhonebookLocation(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
539 fprintf(stdout, _("Message: phonebook entry set OK\n"));
542 CurrentPhonebookError=GE_NONE;
545 GSM_Error Nat_WritePhonebookLocation(GSM_PhonebookEntry * entry)
553 switch (entry->MemoryType) {
555 /* FIXME: the 7110, 6210... supports long phonebookentries. in lack
556 of documentation we only support SIM memory */
557 if(GetModelFeature (FN_PHONEBOOK)==F_PBK71) return GE_NOTIMPLEMENTED;
566 if (error!=GE_NONE) return error;
569 error = PBKSetMemoryType(entry->MemoryType);
570 if (error != GE_NONE) return error;
574 EncodeHexBin (Name, entry->Name, strlen(entry->Name));
575 Name[strlen(entry->Name)*2]=0;
578 EncodeDefault (Name, entry->Name, strlen(entry->Name));
579 Name[strlen(entry->Name)]=0;
583 GSM_PackSemiOctetNumber(entry->Number,Number,false);
586 sprintf(req, "AT+CPBW=%d, \"%s\", %i, \"%s\"\r",
587 entry->Location, entry->Number, i, Name);
589 return NULL_SendMessageSequence
590 (50, &CurrentPhonebookError, strlen(req), 0x00, req);
593 void Nat_ReplyGetMemoryStatusCPBS(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
598 fprintf(stdout, _("Message: memory status received\n"));
601 CurrentMemoryStatus->Used = 0;
602 CurrentMemoryStatus->Free = 0;
604 start = strchr(MessageBuffer, ',');
608 CurrentMemoryStatus->Used = atoi(start);
609 start = strchr(start, ',');
612 CurrentMemoryStatus->Free = atoi(start) - CurrentMemoryStatus->Used;
613 CurrentMemoryStatusError=GE_NONE;
614 } else CurrentMemoryStatusError=GE_UNKNOWN;
615 } else CurrentMemoryStatusError=GE_UNKNOWN;
618 void Nat_ReplyGetMemoryStatusCPBR(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
623 fprintf(stdout, _("Message: memory size received\n"));
626 CurrentMemoryStatus->Used = 0;
627 CurrentMemoryStatus->Free = 0;
629 start = strchr(MessageBuffer, '-');
633 /* Parse +CPBR: (first-last),max_number_len,max_name_len */
634 /* We cannot get Used/Free info. We can get only size of memory
635 we don't have size in memory status and because of it
636 we make assigment used=size_of_memory, free=0.
637 It's better than nothing */
638 CurrentMemoryStatus->Used = atoi(start);
639 CurrentMemoryStatusError=GE_NONE;
640 } else CurrentMemoryStatusError=GE_UNKNOWN;
643 GSM_Error Nat_GetMemoryStatus(GSM_MemoryStatus *Status)
646 char req [] ="AT+CPBS?\r"; /* in some phones doesn't work or doesn't return
647 memory status inside */
648 char req2[] ="AT+CPBR=?\r";
650 error = PBKSetMemoryType(Status->MemoryType);
651 if (error != GE_NONE) return error;
653 CurrentMemoryStatus = Status;
655 error=NULL_SendMessageSequence
656 (20, &CurrentMemoryStatusError, strlen(req), 0x00, req);
657 if (error==GE_NONE) {
658 CurrentMemoryStatus = NULL;
662 error=NULL_SendMessageSequence
663 (20, &CurrentMemoryStatusError, strlen(req2), 0x00, req2);
665 CurrentMemoryStatus = NULL;
670 void Nat_ReplyCallDivert(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
674 fprintf(stdout, _("Message: call forwarding info\n"));
676 if (MessageLength>0x20) {
678 while (MessageBuffer[current]!='"') {
679 CurrentCallDivert->Number[current-27]=MessageBuffer[current];
682 CurrentCallDivert->Enabled=true;
683 CurrentCallDivert->Number[current-27]=0x00;
684 CurrentCallDivert->Timeout=999;//where is it in frame ?
686 CurrentCallDivert->Enabled=false;
688 CurrentCallDivertError=GE_NONE;
691 GSM_Error Nat_CallDivert(GSM_CallDivert *cd)
698 sprintf(req, "AT+CCFC=");
701 case GSM_CDV_AllTypes : strcat(req, "4"); break;
702 case GSM_CDV_Busy : strcat(req, "1"); break;
703 case GSM_CDV_NoAnswer : strcat(req, "2"); break;
704 case GSM_CDV_OutOfReach: strcat(req, "3"); break;
705 default : return GE_NOTIMPLEMENTED;
708 if (cd->Operation == GSM_CDV_Register) {
709 GSM_PackSemiOctetNumber(cd->Number,Number,false);
712 sprintf(req, "%s,%d,\"%s\",%i,,,%d", req,
713 cd->Operation,cd->Number,i,cd->Timeout);
715 sprintf(req, "%s,%d", req, cd->Operation);
719 CurrentCallDivert = cd;
721 error=NULL_SendMessageSequence
722 (100, &CurrentCallDivertError, strlen(req), 0x00, req);
724 CurrentCallDivert = NULL;
729 void Nat_ReplyGetNetworkInfo(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
731 GSM_NetworkInfo NullNetworkInfo;
733 /* TODO: checking if phone really registered to network */
737 /* Make sure we are expecting NetworkInfo frame */
738 if (CurrentNetworkInfo && CurrentNetworkInfoError == GE_BUSY) {
740 fprintf(stdout, _("Message: network info received\n"));
743 NullNetworkInfo.NetworkCode[0]=0; //no exist
745 for (i=0;i<4;i++) NullNetworkInfo.CellID[i]=MessageBuffer[i+current];
747 NullNetworkInfo.CellID[4]=0;
751 for (i=0;i<4;i++) NullNetworkInfo.LAC[i]=MessageBuffer[i+current];
753 NullNetworkInfo.LAC[4]=0;
756 fprintf(stdout, _(" CellID: %s\n"), NullNetworkInfo.CellID);
757 fprintf(stdout, _(" LAC: %s\n"), NullNetworkInfo.LAC);
762 /* Make sure we are expecting NetworkInfo frame */
763 if (CurrentNetworkInfo && CurrentNetworkInfoError == GE_BUSY)
764 *CurrentNetworkInfo=NullNetworkInfo;
766 CurrentNetworkInfoError = GE_NONE;
769 GSM_Error Nat_GetNetworkInfo(GSM_NetworkInfo *NetworkInfo)
773 Protocol->SendMessage(10, 0x00, "AT+CREG=2\r");
775 CurrentNetworkInfo = NetworkInfo;
777 error=NULL_SendMessageSequence
778 (20, &CurrentNetworkInfoError, 9, 0x00, "AT+CREG?\r");
780 CurrentNetworkInfo = NULL;
785 void Nat_ReplyGetBatteryLevel(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
788 fprintf(stdout, _("Message: power source and battery info received\n"));
791 CurrentPowerSource=atoi(MessageBuffer+15);
792 CurrentBatteryLevel=atoi(MessageBuffer+17);
795 GSM_Error Nat_GetBatteryLevel(GSM_BatteryUnits *units, float *level)
800 CurrentBatteryLevel=-1;
802 Protocol->SendMessage(7, 0x00, "AT+CBC\r");
804 /* Wait for timeout or other error. */
805 while (timeout != 0 && CurrentBatteryLevel == -1 ) {
813 /* Take copy in case it changes. */
814 batt_level = CurrentBatteryLevel;
816 if (batt_level != -1) {
818 /* Only units we handle at present are GBU_Arbitrary */
819 if (*units == GBU_Arbitrary) {
820 if (batt_level >= 70) { *level = 4; return (GE_NONE); }
821 if (batt_level >= 50) { *level = 3; return (GE_NONE); }
822 if (batt_level >= 30) { *level = 2; return (GE_NONE); }
823 if (batt_level >= 10) { *level = 1; return (GE_NONE); }
828 return (GE_INTERNALERROR);
834 GSM_Error Nat_GetPowerSource(GSM_PowerSource * source)
836 *source = CurrentPowerSource;
840 void Nat_ReplyGetRFLevel(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
843 fprintf(stdout, _("Message: RF level info received\n"));
845 CurrentRFLevel=atoi(MessageBuffer+15);
848 GSM_Error Nat_GetRFLevel(GSM_RFUnits *units, float *level)
855 Protocol->SendMessage(7, 0x00, "AT+CSQ\r");
857 /* Wait for timeout or other error. */
858 while (timeout != 0 && CurrentRFLevel == -1 ) {
866 /* Make copy in case it changes. */
867 rf_level = CurrentRFLevel;
872 /* Now convert between the different units we support. */
874 if (*units == GRF_Arbitrary) {
875 if (CurrentRFLevel == 99) { *level = 0;return (GE_NONE); }
876 if (CurrentRFLevel > 23) { *level = 4;return (GE_NONE); }
877 if (CurrentRFLevel > 17) { *level = 3;return (GE_NONE); }
878 if (CurrentRFLevel > 11) { *level = 2;return (GE_NONE); }
879 if (CurrentRFLevel > 5) { *level = 1;return (GE_NONE); }
885 if (*units == GRF_CSQ) {
886 if ((CurrentRFLevel <= 31) || (CurrentRFLevel >= 0))
887 *level = CurrentRFLevel;
889 *level = 99; /* Unknown/undefined */
893 /* Unit type is one we don't handle so return error */
894 return (GE_INTERNALERROR);
897 GSM_Error Nat_GetDisplayStatus(int *Status) {
900 sprintf(req, "AT+CIND?\r");
902 return NULL_SendMessageSequence
903 (50, &CurrentDisplayStatusError, strlen(req), 0x00, req);
906 GSM_Error Nat_PressKey(int key, int event)
910 sprintf(req, "AT+CKPD=?\r");
912 return NULL_SendMessageSequence
913 (50, &CurrentPressKeyError, strlen(req), 0x00, req);
916 GSM_Error Nat_DialVoice(char *Number) {
917 char req[39] = "ATDT";
919 if (strlen(Number) > 32)
920 return (GE_INTERNALERROR);
925 return NULL_SendMessageSequence
926 (50, &CurrentDialVoiceError, 4+2+strlen(Number), 0x00, req);
929 void Nat_ReplyCancelCall(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
932 fprintf(stdout, _("Message: call hangup OK\n"));
935 CurrentDialVoiceError=GE_NONE;
938 GSM_Error Nat_CancelCall(void)
940 char req[] = "AT+CHUP\r";
942 return NULL_SendMessageSequence
943 (50, &CurrentDialVoiceError, strlen(req), 0x00, req);
946 GSM_Error Nat_SendDTMF(char *String)
949 char req[80] = "AT+VTS=";
951 for (n = 0; n < 32; n++) {
952 if (String[n] == '\0') break;
953 if (n != 0) req[6 + 2 * n] = ',';
954 req[7 + 2 * n] = String[n];
959 return NULL_SendMessageSequence
960 (50, &CurrentSendDTMFError,7+2+2*strlen(String) , 0x00, req);
963 GSM_Error Nat_EnterSecurityCode(GSM_SecurityCode SecurityCode)
967 if ((SecurityCode.Type != GSCT_Pin)
968 && (SecurityCode.Type != GSCT_Pin2)) return (GE_NOTIMPLEMENTED);
970 sprintf(req, "AT+CPIN=\"%s\"\r", SecurityCode.Code);
972 return NULL_SendMessageSequence
973 (20, &CurrentSecurityCodeError, strlen(req), 0x00, req);
976 void Nat_ReplyGetSecurityCodeStatus(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
981 fprintf(stdout, _("Message: security code status received\n"));
984 start = strchr(MessageBuffer, ':');
987 *CurrentSecurityCodeStatus = 0;
989 if (!strncmp(start, "READY", 5)) *CurrentSecurityCodeStatus = GSCT_None;
991 if (!strncmp(start, "SIM ", 4)) {
993 if (!strncmp(start, "PIN2", 4)) *CurrentSecurityCodeStatus = GSCT_Pin2;
994 if (!strncmp(start, "PUK2", 4)) *CurrentSecurityCodeStatus = GSCT_Puk2;
995 if (!strncmp(start, "PIN", 3)) *CurrentSecurityCodeStatus = GSCT_Pin;
996 if (!strncmp(start, "PUK", 3)) *CurrentSecurityCodeStatus = GSCT_Puk;
999 CurrentSecurityCodeError=GE_NONE;
1002 GSM_Error Nat_GetSecurityCodeStatus(int *Status)
1004 CurrentSecurityCodeStatus = Status;
1006 return NULL_SendMessageSequence
1007 (20, &CurrentSecurityCodeError, 9, 0x00, "AT+CPIN?\r");
1010 void Nat_ReplyGetDateTime(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1016 fprintf(stdout,_("Message: date/time received\n"));
1019 if (MessageBuffer[current]==0x0d) {
1020 CurrentDateTime->IsSet=false;
1022 CurrentDateTime->IsSet=true;
1024 ATDecodeDateTime(CurrentDateTime, MessageBuffer+(current));
1027 fprintf(stdout,_(" %i/%i/%i %i:%i:%i\n"),
1028 CurrentDateTime->Day,CurrentDateTime->Month,CurrentDateTime->Year,
1029 CurrentDateTime->Hour,CurrentDateTime->Minute,CurrentDateTime->Second);
1032 CurrentDateTimeError=GE_NONE;
1035 GSM_Error Nat_GetDateTime(GSM_DateTime *date_time)
1037 unsigned char req[9] = {"AT+CCLK?\r"};
1039 CurrentDateTime=date_time;
1041 return NULL_SendMessageSequence
1042 (50, &CurrentDateTimeError, 9, 0x00, req);
1045 void Nat_ReplySetDateTime(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1048 fprintf(stdout,_("Message: date/time set OK\n"));
1050 CurrentSetDateTimeError=GE_NONE;
1053 GSM_Error Nat_SetDateTime(GSM_DateTime *date_time)
1057 sprintf(req, "AT+CCLK=\"%02i/%02i/%02i,%02i:%02i:%02i+00\"\r",
1058 date_time->Year-2000,date_time->Month,date_time->Day,
1059 date_time->Hour,date_time->Minute,date_time->Second);
1061 return NULL_SendMessageSequence
1062 (20, &CurrentSetDateTimeError, strlen(req), 0x00, req);
1065 void Nat_ReplyGetAlarm(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1070 fprintf(stdout,_("Message: alarm info received\n"));
1072 if (MessageBuffer[current-1]==0x0d) {
1073 CurrentAlarm->IsSet=false;
1074 CurrentAlarm->Hour=0;
1075 CurrentAlarm->Minute=0;
1077 CurrentAlarm->IsSet=true;
1079 ATDecodeDateTime(CurrentAlarm, MessageBuffer+(current));
1082 fprintf(stdout,_(" %i:%i\n"),CurrentAlarm->Hour,CurrentAlarm->Minute);
1085 CurrentAlarmError=GE_NONE;
1088 GSM_Error Nat_GetAlarm(int alarm_number, GSM_DateTime *date_time)
1090 unsigned char req[9] = {"AT+CALA?\r"};
1092 CurrentAlarm=date_time;
1094 return NULL_SendMessageSequence
1095 (50, &CurrentAlarmError, 9, 0x00, req);
1098 /* FIXME: we should also allow to set the alarm off :-) */
1099 /* Doesn't work ? */
1100 GSM_Error Nat_SetAlarm(int alarm_number, GSM_DateTime *date_time)
1105 sprintf(req, "AT+CALA=\"%02i/%02i/%02i,%02i:%02i:%02i+00\",0,1\r",
1106 i,i,i,date_time->Hour,date_time->Minute,i);
1108 return NULL_SendMessageSequence
1109 (50, &CurrentSetAlarmError, strlen(req), 0x00, req);
1112 void Nat_ReplyGetSMSCenter(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1114 unsigned char buffer[300];
1118 /* FIXME: support for all formats */
1119 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1120 for (i=1;i<strlen(buffer)-1;i++) buffer[i-1]=buffer[i];
1123 /* FIXME: when SMSC in empty, I set default (for my network) */
1124 /* Any better solution ? */
1125 // if (buffer[0]==0) strcpy(buffer,"+48602951111");
1126 if (buffer[0]==0) strcpy(buffer,"+48601000310");
1128 strcpy(CurrentMessageCenter->Number,buffer);
1130 /* Some default values. Is it possible to get them ? */
1131 CurrentMessageCenter->Name[0]=0;
1132 CurrentMessageCenter->DefaultRecipient[0]=0;
1133 CurrentMessageCenter->Format=GSMF_Text;
1134 CurrentMessageCenter->Validity=GSMV_Max_Time;
1137 fprintf(stdout, _("Message: SMSC number received\n Number: '%s'\n"),buffer);
1140 CurrentMessageCenterError=GE_NONE;
1143 /* We only get SMSC number */
1144 /* Is it possible to get more ? */
1145 GSM_Error Nat_GetSMSCenter(GSM_MessageCenter *MessageCenter)
1147 unsigned char req[] = {"AT+CSCA?\r"};
1149 if (MessageCenter->No!=1) return GE_NOTSUPPORTED;
1151 CurrentMessageCenter=MessageCenter;
1153 return NULL_SendMessageSequence
1154 (50, &CurrentMessageCenterError, strlen(req), 0x00, req);
1157 void Nat_ReplySetSMSCenter(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1160 fprintf(stdout, _("Message: SMSC number set OK\n"));
1163 CurrentMessageCenterError=GE_NONE;
1166 GSM_Error Nat_SetSMSCenter(GSM_MessageCenter *MessageCenter)
1168 unsigned char req[50];
1170 if (MessageCenter->No!=1) return GE_NOTSUPPORTED;
1172 sprintf(req, "AT+CSCA=\"%s\"\r",MessageCenter->Number);
1174 return NULL_SendMessageSequence
1175 (50, &CurrentMessageCenterError, strlen(req), 0x00, req);
1178 void Nat_ReplySMSGetPDUType(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1180 if (strncmp("AT+CSDH=1\r",MessageBuffer,10)==0) {
1182 fprintf(stdout, _("Message: all SMS parameters will be displayed in text mode\n"));
1184 CurrentSMSMessageError=GE_NONE;
1186 if (strncmp("AT+CMGF=0\r",MessageBuffer,10)==0) {
1188 fprintf(stdout, _("Message: set PDU mode for SMS\n"));
1190 CurrentSMSMessageError=GE_NONE;
1192 if (strncmp("AT+CMGF=1\r",MessageBuffer,10)==0) {
1194 fprintf(stdout, _("Message: set text mode for SMS\n"));
1196 CurrentSMSMessageError=GE_NONE;
1200 GSM_Error SMSGetPDUType()
1202 unsigned char req [10] = {"AT+CMGF=0\r"}; //sets PDU mode
1204 unsigned char req2[10] = {"AT+CMGF=1\r"}; //sets text mode
1205 unsigned char req3[10] = {"AT+CSDH=1\r"}; //shows all parameters in text mode
1209 error=NULL_SendMessageSequence (50, &CurrentSMSMessageError, 10, 0x00, req);
1210 if (error==GE_NONE) {
1211 SMSATCMGF=SMS_AT_PDU;
1215 error=NULL_SendMessageSequence (50, &CurrentSMSMessageError, 10, 0x00, req2);
1216 if (error==GE_NONE) {
1217 SMSATCMGF=SMS_AT_TEXT;
1219 error=NULL_SendMessageSequence (50, &CurrentSMSMessageError, 10, 0x00, req3);
1225 GSM_Error GSM_DecodeETSISMSFrame(GSM_SMSMessage *SMS, unsigned char *req, int length)
1227 SMS_MessageType PDU=SMS_Deliver;
1228 GSM_ETSISMSMessage ETSI;
1229 int i,current=0,current2=0;
1231 for(i=0;i<req[0]+1;i++) ETSI.SMSCNumber[i]=req[current++];
1233 ETSI.firstbyte=req[current++];
1235 /* See GSM 03.40 section 9.2.3.1 */
1236 if ((ETSI.firstbyte & 0x03) == 0x01) PDU=SMS_Submit;
1237 if ((ETSI.firstbyte & 0x03) == 0x02) PDU=SMS_Status_Report;
1241 SMS->Type = GST_SMS;
1242 SMS->folder= GST_OUTBOX;
1243 current++; //TPMR is ignored now
1244 current2=((req[current])+1)/2+1;
1245 for(i=0;i<current2+1;i++) ETSI.Number[i]=req[current++];
1246 ETSI.TPPID=req[current++];
1247 ETSI.TPDCS=req[current++];
1249 /* See GSM 03.40 9.2.3.3 TPVP can not exist in frame */
1250 if ((ETSI.firstbyte & 0x18)!=0) current++; //TPVP is ignored now
1252 ETSI.TPUDL=req[current++];
1254 for(i=current2;i<length;i++) ETSI.MessageText[i-current2]=req[current++];
1258 SMS->Type = GST_SMS;
1259 SMS->folder= GST_INBOX;
1260 current2=((req[current])+1)/2+1;
1261 for(i=0;i<current2+1;i++) ETSI.Number[i]=req[current++];
1262 ETSI.TPPID=req[current++];
1263 ETSI.TPDCS=req[current++];
1264 for(i=0;i<7;i++) ETSI.DeliveryDateTime[i]=req[current++];
1265 ETSI.TPUDL=req[current++];
1267 for(i=current2;i<length;i++) ETSI.MessageText[i-current2]=req[current++];
1269 case SMS_Status_Report:
1272 SMS->folder= GST_INBOX;
1273 current++; //TPMR is ignored now
1274 current2=((req[current])+1)/2+1;
1275 for(i=0;i<current2+1;i++) ETSI.Number[i]=req[current++];
1276 for(i=0;i<7;i++) ETSI.DeliveryDateTime[i]=req[current++];
1277 for(i=0;i<7;i++) ETSI.SMSCDateTime[i]=req[current++];
1278 ETSI.TPStatus=req[current];
1285 GSM_DecodeETSISMS(SMS, &ETSI);
1290 void Nat_ReplyGetSMSMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1292 unsigned char buffer[300],buffer2[300];
1293 GSM_ETSISMSMessage ETSISMS;
1297 fprintf(stdout, _("Message: SMS Message Received\n"));
1299 switch (SMSATCMGF) {
1300 case SMS_AT_PDU: /* PDU mode */
1304 if (MessageBuffer[start]==' ' ) break;
1309 if (MessageBuffer[start]=='0' || MessageBuffer[start]=='2')
1310 CurrentSMSMessage->Status=GSS_NOTSENTREAD;
1311 if (MessageBuffer[start]=='1' || MessageBuffer[start]=='3')
1312 CurrentSMSMessage->Status=GSS_SENTREAD;
1315 if (MessageBuffer[start]==0x0a) break;
1320 /* We need to find last char of PDU text. We can't use MessageLength,
1321 because sometimes some phones (like Siemens M20) return
1322 some strange things after main PDU */
1325 if (MessageBuffer[end]==0x0a) break;
1329 DecodeHexBin (buffer, MessageBuffer+start, end-start-1);
1331 GSM_DecodeETSISMSFrame(CurrentSMSMessage,buffer,(end-start-1)/2);
1333 CurrentSMSMessageError=GE_NONE;
1338 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1340 if (!strcmp(buffer,"\"0\"") || !strcmp(buffer,"\"REC UNREAD\"")) {
1341 CurrentSMSMessage->Type=GST_SMS;
1342 CurrentSMSMessage->folder=GST_INBOX;
1343 CurrentSMSMessage->SMSData=true;
1344 CurrentSMSMessage->Status=GSS_NOTSENTREAD;
1345 } else if (!strcmp(buffer,"\"1\"") || !strcmp(buffer,"\"REC READ\"")) {
1346 CurrentSMSMessage->Type=GST_SMS;
1347 CurrentSMSMessage->folder=GST_INBOX;
1348 CurrentSMSMessage->SMSData=true;
1349 CurrentSMSMessage->Status=GSS_SENTREAD;
1350 } else if (!strcmp(buffer,"\"2\"") || !strcmp(buffer,"\"STO UNSENT\"")) {
1351 CurrentSMSMessage->Type=GST_SMS;
1352 CurrentSMSMessage->folder=GST_OUTBOX;
1353 CurrentSMSMessage->SMSData=false;
1354 CurrentSMSMessage->Status=GSS_NOTSENTREAD;
1355 } else if (!strcmp(buffer,"\"3\"") || !strcmp(buffer,"\"STO SENT\"")) {
1356 CurrentSMSMessage->Type=GST_SMS;
1357 CurrentSMSMessage->folder=GST_OUTBOX;
1358 CurrentSMSMessage->SMSData=false;
1359 CurrentSMSMessage->Status=GSS_SENTREAD;
1362 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1364 /* It's delivery report according to Nokia AT standards */
1365 if (CurrentSMSMessage->folder==0 && buffer[0]!=0 && //GST_INBOX
1369 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1372 /* FIXME: support for all formats */
1373 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1374 for (i=1;i<strlen(buffer)-1;i++) buffer[i-1]=buffer[i];
1376 strcpy(CurrentSMSMessage->Sender,buffer);
1379 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1381 /* Sending datetime */
1382 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1383 start+=ExtractOneParameter(MessageBuffer+start, buffer2);
1385 for (i=1;i<strlen(buffer)+1;i++) buffer[i-1]=buffer[i];
1389 for (j=0;j<strlen(buffer2);j++) buffer[i++]=buffer2[j];
1392 ATDecodeDateTime(&CurrentSMSMessage->Time, buffer);
1394 /* Date of SMSC response */
1395 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1396 start+=ExtractOneParameter(MessageBuffer+start, buffer2);
1398 for (i=1;i<strlen(buffer)+1;i++) buffer[i-1]=buffer[i];
1402 for (j=0;j<strlen(buffer2);j++) buffer[i++]=buffer2[j];
1405 ATDecodeDateTime(&DT, buffer);
1407 /* for compatiblity with GSM_DecodeETSISMSStatusReportData */
1408 /* FIXME: TimeZone is cut */
1409 DT.Year=DT.Year-2000;
1410 GSM_EncodeSMSDateTime(&DT,ETSISMS.SMSCDateTime);
1413 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1416 for (i=strlen(buffer)-1;i>=0;i--) {
1417 ETSISMS.TPStatus=ETSISMS.TPStatus+(buffer[i]-'0')*j;
1421 GSM_DecodeETSISMSStatusReportData(CurrentSMSMessage, &ETSISMS);
1423 /* NO SMSC number */
1424 CurrentSMSMessage->MessageCenter.Number[0]=0;
1426 CurrentSMSMessage->Type = GST_DR;
1428 /* FIXME: make support for it */
1429 CurrentSMSMessage->ReplyViaSameSMSC=false;
1432 /* FIXME: support for all formats */
1433 for (i=1;i<strlen(buffer)-1;i++) buffer[i-1]=buffer[i];
1435 strcpy(CurrentSMSMessage->Sender,buffer);
1437 /* Sender number in alphanumeric format ? */
1438 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1439 if (strlen(buffer)!=0) strcpy(CurrentSMSMessage->Sender,buffer);
1441 /* Sending datetime */
1442 if (CurrentSMSMessage->folder==0) { //GST_INBOX
1443 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1444 start+=ExtractOneParameter(MessageBuffer+start, buffer2);
1446 for (i=1;i<strlen(buffer)+1;i++) buffer[i-1]=buffer[i];
1450 for (j=0;j<strlen(buffer2);j++) buffer[i++]=buffer2[j];
1453 ATDecodeDateTime(&CurrentSMSMessage->Time, buffer);
1456 /* Sender number format */
1457 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1460 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1461 ETSISMS.firstbyte=0;
1463 for (i=strlen(buffer)-1;i>=0;i--) {
1464 ETSISMS.firstbyte=ETSISMS.firstbyte+(buffer[i]-'0')*j;
1468 CurrentSMSMessage->ReplyViaSameSMSC=false;
1469 /* GSM 03.40 section 9.2.3.17 (TP-Reply-Path) */
1470 if ((ETSISMS.firstbyte & 128)==128)
1471 CurrentSMSMessage->ReplyViaSameSMSC=true;
1473 /* TP PID.No support now */
1474 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1477 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1480 for (i=strlen(buffer)-1;i>=0;i--) {
1481 ETSISMS.TPDCS=ETSISMS.TPDCS+(buffer[i]-'0')*j;
1485 if (CurrentSMSMessage->folder==1) { //GST_OUTBOX
1487 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1491 /* FIXME: support for all formats */
1492 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1493 for (i=1;i<strlen(buffer)-1;i++) buffer[i-1]=buffer[i];
1495 strcpy(CurrentSMSMessage->MessageCenter.Number,buffer);
1497 /* Format of SMSC number */
1498 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1501 start+=ExtractOneParameter(MessageBuffer+start, buffer);
1504 for (i=strlen(buffer)-1;i>=0;i--) {
1505 ETSISMS.TPUDL=ETSISMS.TPUDL+(buffer[i]-'0')*j;
1511 CurrentSMSMessage->Coding = GSM_Coding_Default;
1513 /* GSM 03.40 section 9.2.3.10 (TP-Data-Coding-Scheme) and GSM 03.38 section 4 */
1514 if ((ETSISMS.TPDCS & 0xf4) == 0xf4) CurrentSMSMessage->Coding=GSM_Coding_8bit;
1515 if ((ETSISMS.TPDCS & 0x08) == 0x08) CurrentSMSMessage->Coding=GSM_Coding_Unicode;
1517 switch (CurrentSMSMessage->Coding) {
1518 case GSM_Coding_Default:
1519 /* GSM 03.40 section 9.2.3.23 (TP-User-Data-Header-Indicator) */
1520 /* If not SMS with UDH, it's coded normal */
1521 /* If UDH available, treat it as Unicode or 8 bit */
1522 if ((ETSISMS.firstbyte & 0x40)!=0x40) {
1523 CurrentSMSMessage->UDHType=GSM_NoUDH;
1524 for (i=0;i<ETSISMS.TPUDL;i++) {
1525 if (i>GSM_MAX_SMS_LENGTH) break;
1526 CurrentSMSMessage->MessageText[i]=MessageBuffer[i+start];
1528 CurrentSMSMessage->Length=ETSISMS.TPUDL;
1531 case GSM_Coding_Unicode:
1532 case GSM_Coding_8bit:
1533 DecodeHexBin (ETSISMS.MessageText, MessageBuffer+start, ETSISMS.TPUDL*2);
1534 GSM_DecodeETSISMSSubmitData(CurrentSMSMessage, &ETSISMS);
1537 CurrentSMSMessage->MessageText[CurrentSMSMessage->Length]=0;
1540 CurrentSMSMessageError=GE_NONE;
1544 GSM_Error Nat_GetSMSMessage(GSM_SMSMessage *message)
1546 unsigned char req2[20];
1549 if (SMSATCMGF==-1) {
1550 error=SMSGetPDUType();
1551 if (error!=GE_NONE) return error;
1554 CurrentSMSMessage = message;
1556 sprintf(req2, "AT+CMGR=%i\r",message->Location); //gets SMS
1558 CurrentSMSMessage->MessageNumber=CurrentSMSMessage->Location;
1560 return NULL_SendMessageSequence
1561 (50, &CurrentSMSMessageError, strlen(req2), 0x00, req2);
1564 /* FIXME: do we need more than SMS_Submit and SMS_Deliver ? */
1565 GSM_Error GSM_EncodeETSISMSFrame(GSM_SMSMessage *SMS, unsigned char *req, int *length, SMS_MessageType PDU)
1567 GSM_ETSISMSMessage ETSI;
1570 GSM_EncodeETSISMS(SMS, &ETSI, PDU, length);
1572 for (i=0;i<ETSI.SMSCNumber[0]+1;i++)
1573 req[current++]=ETSI.SMSCNumber[i];
1575 req[current++]=ETSI.firstbyte;
1580 req[current++]=0; //TPMR ignored now
1581 for (i=0;i<((ETSI.Number[0]+1)/2+1)+1;i++) req[current++]=ETSI.Number[i];
1582 req[current++]=0; //ETSI.TPPID;
1583 req[current++]=ETSI.TPDCS;
1584 req[current++]=ETSI.TPVP;
1585 req[current++]=ETSI.TPUDL;
1586 for(i=0;i<*length;i++) req[current++]=ETSI.MessageText[i];
1590 for (i=0;i<((ETSI.Number[0]+1)/2+1)+1;i++) req[current++]=ETSI.Number[i];
1591 req[current++]=0; //ETSI.TPPID;
1592 req[current++]=ETSI.TPDCS;
1593 for(i=0;i<7;i++) req[current++]=ETSI.DeliveryDateTime[i];
1594 req[current++]=ETSI.TPUDL;
1595 for(i=0;i<*length;i++) req[current++]=ETSI.MessageText[i];
1607 void Nat_ReplyCSMP(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1610 fprintf(stdout, _("Message: SMS parameters set OK\n"));
1613 CurrentSMSMessageError=GE_NONE;
1616 GSM_Error Nat_SendSaveSMSMessage(GSM_SMSMessage *message,bool savesms)
1618 unsigned char req2[500];
1619 unsigned char req3[256];
1620 unsigned char req4[256];
1621 unsigned char endchar[1];
1623 int length,stat,current=0;
1624 SMS_MessageType PDU;
1625 GSM_ETSISMSMessage ETSISMS;
1626 GSM_MessageCenter SMSC;
1628 if (SMSATCMGF==-1) {
1629 error=SMSGetPDUType();
1630 if (error!=GE_NONE) return error;
1633 CurrentSMSMessage = message;
1635 if (message->folder==0x00) { //GST_INBOX
1638 if (message->Status==GSS_NOTSENTREAD) stat=0;
1642 if (message->Status==GSS_NOTSENTREAD) stat=2;
1645 endchar[0]=26; //ctrl+z;
1647 switch (SMSATCMGF) {
1648 case SMS_AT_PDU: /* PDU mode */
1649 GSM_EncodeETSISMSFrame(message, req3, &length, PDU);
1652 sprintf(req2, "AT+CMGW=%i,%i\r",(length-(req3[0]+1)),stat); //saves SMS
1654 sprintf(req2, "AT+CMGS=%i\r",(length-(req3[0]+1))); //sends SMS
1656 EncodeHexBin (req4, req3, length);
1659 Protocol->SendMessage(strlen(req2), 0x00, req2); usleep(500);
1664 GSM_EncodeETSISMS(message, &ETSISMS, PDU, &length);
1666 sprintf(req4, "AT+CSMP=%i,%i,%i,%i\r",
1667 ETSISMS.firstbyte, ETSISMS.TPVP,
1668 ETSISMS.TPPID, ETSISMS.TPDCS);
1670 error=NULL_SendMessageSequence
1671 (50, &CurrentSMSMessageError, strlen(req4), 0x00, req4);
1672 if (error!=GE_NONE) return error;
1674 strcpy(SMSC.Number,message->MessageCenter.Number);
1676 error=Nat_SetSMSCenter(&SMSC);
1677 if (error!=GE_NONE) return error;
1680 /* Tested AT implementations don't support alphanumeric numbers */
1681 if ((CurrentSMSMessage->Destination[0]<'0' ||
1682 CurrentSMSMessage->Destination[0]>'9') &&
1683 CurrentSMSMessage->Destination[0]!='+')
1685 CurrentSMSMessage->Destination[0]='1';
1686 CurrentSMSMessage->Destination[1]=0;
1689 sprintf(req4, "AT+CMGW=\"%s\"\r",CurrentSMSMessage->Destination);
1691 /* Tested AT implementations don't support alphanumeric numbers */
1692 if ((CurrentSMSMessage->Destination[0]<'0' ||
1693 CurrentSMSMessage->Destination[0]>'9') &&
1694 CurrentSMSMessage->Destination[0]!='+')
1695 return GE_NOTSUPPORTED;
1697 sprintf(req4, "AT+CMGS=\"%s\"\r",CurrentSMSMessage->Destination);
1700 Protocol->SendMessage(strlen(req4), 0x00, req4); usleep(500);
1702 switch (CurrentSMSMessage->Coding) {
1703 case GSM_Coding_Default:
1704 /* If not SMS with UDH, it's as normal text */
1705 if (CurrentSMSMessage->UDHType==GSM_NoUDH) {
1706 strcpy(req4,CurrentSMSMessage->MessageText);
1707 current=strlen(req4);
1710 case GSM_Coding_Unicode:
1711 case GSM_Coding_8bit:
1713 EncodeHexBin (req4, ETSISMS.MessageText, length);
1721 Protocol->SendMessage(current, 0x00, req4); usleep(500);
1723 return NULL_SendMessageSequence
1724 (500, &CurrentSMSMessageError, 1, 0x00, endchar);
1727 void Nat_ReplySaveSMSMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1731 fprintf(stdout, _("Message: SMS Message saved\n"));
1735 while(MessageBuffer[i]!=0x20) { i++; }
1738 CurrentSMSMessage->MessageNumber=MessageBuffer[i]-'0';
1741 if (MessageBuffer[i]!=0x0d)
1742 CurrentSMSMessage->MessageNumber=
1743 CurrentSMSMessage->MessageNumber*10+MessageBuffer[i]-'0';
1746 fprintf(stdout, _(" location: %i\n"),CurrentSMSMessage->MessageNumber);
1749 CurrentSMSMessageError=GE_NONE;
1752 GSM_Error Nat_SaveSMSMessage(GSM_SMSMessage *message)
1754 return Nat_SendSaveSMSMessage(message,true);
1757 void Nat_ReplySendSMSMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1760 fprintf(stdout, _("Message: SMS Message sent OK\n"));
1763 CurrentSMSMessageError=GE_SMSSENDOK;
1766 GSM_Error Nat_SendSMSMessage(GSM_SMSMessage *message)
1768 return Nat_SendSaveSMSMessage(message,false);
1771 void Nat_ReplyDeleteSMSMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1774 fprintf(stdout, _("Message: SMS deleted OK\n"));
1777 CurrentSMSMessageError=GE_NONE;
1780 GSM_Error Nat_DeleteSMSMessage(GSM_SMSMessage * message)
1784 sprintf(req, "AT+CMGD=%d\r", message->Location);
1786 return NULL_SendMessageSequence
1787 (50, &CurrentSMSMessageError, strlen(req), 0x00, req);
1790 void Nat_ReplyGetSMSStatus(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1794 fprintf(stdout, _("Message: SMS status received\n"));
1797 CurrentSMSStatus->UnRead=0;
1798 CurrentSMSStatus->Number=0;
1800 start2 = strstr(MessageBuffer, "+CPMS: ");
1803 CurrentSMSStatus->Number=atoi(start2);
1805 CurrentSMSStatusError=GE_NONE;
1808 GSM_Error Nat_GetSMSStatus(GSM_SMSStatus *Status)
1810 CurrentSMSStatus=Status;
1812 return NULL_SendMessageSequence
1813 (10, &CurrentSMSStatusError, 13, 0x00, "AT+CPMS=\"SM\"\r");
1816 struct AT_OneReply ATRepliesOK[] = {
1818 {"AT+CCFC=" ,Nat_ReplyCallDivert },
1819 {"AT+CGMI\r",Nat_ReplyGetManufacturer },
1820 {"AT+CPBS?" ,Nat_ReplyGetMemoryStatusCPBS },
1821 {"AT+CPBR=?",Nat_ReplyGetMemoryStatusCPBR },
1822 {"AT+CPBR=" ,Nat_ReplyGetMemoryLocation },
1823 {"AT+CPBW=" ,Nat_ReplyWritePhonebookLocation},
1824 {"AT+CPBS=" ,Nat_ReplyPBKSetMemoryType },
1825 {"AT+CSCS=\"HEX\"",Nat_ReplySetCharset },
1826 {"AT+CSCS=\"GSM\"",Nat_ReplySetCharset },
1827 {"AT+CSCA?" ,Nat_ReplyGetSMSCenter },
1828 {"AT+CSCA=" ,Nat_ReplySetSMSCenter },
1829 {"AT+CSDH=1\r",Nat_ReplySMSGetPDUType },
1830 {"AT+CMGF=0\r",Nat_ReplySMSGetPDUType },
1831 {"AT+CMGF=1\r",Nat_ReplySMSGetPDUType },
1832 {"AT+CMGR=" ,Nat_ReplyGetSMSMessage },
1833 {"AT+CSMP=" ,Nat_ReplyCSMP },
1834 {"AT+CMGW=" ,Nat_ReplySaveSMSMessage },
1835 {"AT+CMGS=" ,Nat_ReplySendSMSMessage },
1836 {"AT+CMGD=" ,Nat_ReplyDeleteSMSMessage },
1837 {"AT+CPMS=\"SM\"",Nat_ReplyGetSMSStatus },
1838 {"AT+CPIN?\r",Nat_ReplyGetSecurityCodeStatus},
1839 {"AT+CCLK?" ,Nat_ReplyGetDateTime },
1840 {"AT+CCLK=" ,Nat_ReplySetDateTime },
1841 {"AT+CALA?" ,Nat_ReplyGetAlarm },
1842 {"AT+CGMM\r",Nat_ReplyGetID },
1843 {"AT+CGMR\r",Nat_ReplyGetHW },
1844 {"AT+CREG?" ,Nat_ReplyGetNetworkInfo },
1845 {"AT+CGSN\r",Nat_ReplyGetIMEI },
1846 {"AT+CHUP" ,Nat_ReplyCancelCall },
1847 {"AT+CBC" ,Nat_ReplyGetBatteryLevel },
1848 {"AT+CSQ" ,Nat_ReplyGetRFLevel },
1853 void Nat_DispatchMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageType) {
1855 bool AnswerOK,AnswerError; //if last part is OK or ERROR
1859 /* In 9210 first answer start with different, random bytes.
1860 Here we try to find first AT to find start byte */
1861 for (i=0;i<MessageLength-1;i++) {
1862 if (strncmp("AT",MessageBuffer+i,2)==0) {
1869 if (strncmp("ERROR\r",MessageBuffer+(MessageLength-6),6)==0) AnswerError=true;
1872 if (strncmp("OK\r",MessageBuffer+(MessageLength-3),3)==0) AnswerOK=true;
1877 if (ATRepliesOK[i].ReplyFunction==NULL) break;
1878 if (strncmp(ATRepliesOK[i].ReplyString,MessageBuffer+start,
1879 strlen(ATRepliesOK[i].ReplyString))==0) {
1880 ATRepliesOK[i].ReplyFunction(MessageLength-start,MessageBuffer+start,MessageType);
1887 if (ATRepliesOK[i].ReplyFunction==NULL)
1888 fprintf(stdout,_("Message: unknown AT command\n"));
1891 if (strncmp("AT\r",MessageBuffer+start,3)==0) {
1893 fprintf(stdout, _("Message: used for setting DLR3 to FBUS - part 1 OK\n"));
1895 CurrentGetHWError=GE_NONE;
1897 if (strncmp("AT&F\r",MessageBuffer+start,5)==0) {
1899 fprintf(stdout, _("Message: used for setting DLR3 to FBUS - part 2 OK\n"));
1901 CurrentGetHWError=GE_NONE;
1903 if (strncmp("AT*NOKIAFBUS\r",MessageBuffer+start,13)==0) {
1905 fprintf(stdout, _("Message: Setting DLR3 to FBUS - part 3 OK\n"));
1907 CurrentGetHWError=GE_NONE;
1912 if (strncmp("AT*NOKIAFBUS\r",MessageBuffer+start,13)==0) {
1914 fprintf(stdout, _("Message: Setting DLR3 to FBUS - part 3 ERROR\n"));
1917 CurrentGetHWError=GE_INTERNALERROR;