1 unit Unit1;
2 
3 interface
4 
5 uses
6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7   Dialogs, StdCtrls, Gammu, ExtCtrls, ComCtrls;
8 
9 type
10   TForm1 = class(TForm)
11     GroupBox2: TGroupBox;
12     InitButton: TButton;
13     GetNetInfoButton: TButton;
14     InfoListBox: TListBox;
15     GetAllInboxSMSButton: TButton;
16     StatusBar1: TStatusBar;
17     DeviceComboBox: TComboBox;
18     Pin: TLabel;
19     PINEdit: TEdit;
20     ConnectionLabel: TLabel;
21     GroupBox1: TGroupBox;
22     Label2: TLabel;
23     DestinationNumberEdit: TEdit;
24     DeliveryReportCheckBox: TCheckBox;
25     Label3: TLabel;
26     SMSTextEdit: TEdit;
27     SendButton: TButton;
28     GetInfoButton: TButton;
29     ResetButton: TButton;
30     TerminateButton: TButton;
31     Button1: TButton;
32     procedure GetNetInfoButtonClick(Sender: TObject);
33     procedure GetAllInboxSMSButtonClick(Sender: TObject);
34     procedure InitButtonClick(Sender: TObject);
35     procedure SendButtonClick(Sender: TObject);
36     procedure GetInfoButtonClick(Sender: TObject);
37     procedure ResetButtonClick(Sender: TObject);
38     procedure TerminateButtonClick(Sender: TObject);
39     procedure Button1Click(Sender: TObject);
40   private
41     { Private declarations }
42         PhoneID                        : integer;
43         SendSMS                        : GSM_SMSMessage;
44         PhoneCallBackPointer           : PPhoneCallBackProc;
45         SecurityCallBackPointer        : PSecurityCallBackProc;
46         SMSCallBackPointer             : PSMSCallBackProc;
47   public
48     { Public declarations }
49   end;
50 
51 var
52   Form1: TForm1;
53 
54   implementation
55 
56 {$R *.dfm}
57 
58 //user pressed GetNetInfo button - we get network info
59 procedure TForm1.GetNetInfoButtonClick(Sender: TObject);
60 var
61         NetInfo : GSM_NetworkInfo;
62         error   : GSM_Error;
63 begin
64         InfoListBox.Items.Add('Getting network info');
65         error:=GSM_GetNetworkInfo(PhoneID,@NetInfo);
66         if (error = ERR_NONE) then
67         begin
68                 if ((NetInfo.State = GSM_HomeNetwork) or
69                     (NetInfo.State = GSM_RoamingNetwork)) then
70                 begin
71                         InfoListBox.Items.Add('Network code ' + NetInfo.NetworkCode);
72                 end;
73                 GSM_GetNetworkName(@NetInfo.NetworkCode,@NetInfo.NetworkName);
74                 InfoListBox.Items.Add('Network name ' + GetGammuUnicodeString(NetInfo.NetworkName));
75         end else begin
76                 InfoListBox.Items.Add('Error '+inttostr(integer(error)));
77         end;
78 end;
79 
80 //user pressed GetALLInboxSMS button - we get all sms from phone
81 //and display info about Inbox SMS only
82 procedure TForm1.GetAllInboxSMSButtonClick(Sender: TObject);
83 var
84         error   : GSM_Error;
85         sms     : GSM_MultiSMSMessage;
86         start   : Boolean;
87 begin
88         InfoListBox.Items.Add('Getting all sms');
89         error := ERR_NONE;
90         start := True; //first set to true to allow init some internal DLL variables
91         while error = ERR_NONE do
92         begin
93                 sms.SMS[1].Folder := 0;
94                 error := GSM_GetNextSMSMessage (PhoneID,@sms,start);
95                 if (error = ERR_NONE) then
96                 begin
97                         if (sms.SMS[1].InboxFolder) then
98                         begin
99                                 InfoListBox.Items.Add('sms from '+GetGammuUnicodeString(sms.SMS[1].Number));
100                                 InfoListBox.Items.Add('Text '+GetGammuUnicodeString(sms.SMS[1].Text));
101                                 InfoListBox.Items.Add('Length '+inttostr(sms.SMS[1].Length));
102 
103                                 //now we delete our read SMS after reading
104                                 sms.SMS[1].Folder:=0;
105                                 GSM_DeleteSMSMessage(PhoneID,@sms.SMS[1]);
106                         end;
107                 end;
108                 start := false;
109         end;
110 end;
111 
112 procedure TForm1.GetInfoButtonClick(Sender: TObject);
113 var
114         buffer : array[1..100] of char;
115         error  : GSM_Error;
116         ver    : Double;
117 begin
118   GSM_GetGammuVersion(@buffer);
119   InfoListBox.Items.Add('Gammu DLL version is '+buffer);
120   error:=GSM_GetIMEI(PhoneID,@buffer);
121   if (error = ERR_NONE) then
122   begin
123           InfoListBox.Items.Add('Device IMEI sent to network is '+buffer);
124   end;
125   error:=GSM_GetManufacturer(PhoneID,@buffer);
126   if (error = ERR_NONE) then
127   begin
128           InfoListBox.Items.Add('Device manufacturer is '+buffer);
129   end;
130   error:=GSM_GetModel(PhoneID,@buffer);
131   if (error = ERR_NONE) then
132   begin
133           InfoListBox.Items.Add('Device model is '+buffer);
134   end;
135   error:=GSM_GetFirmwareVersion(PhoneID,@ver);
136   if (error = ERR_NONE) then
137   begin
138           InfoListBox.Items.Add('Device firmware version is '+floattostr(ver));
139   end;
140 
141 end;
142 
143 //called, when phone is connected or disconnected
144 procedure ChangePhoneState1(x:integer;ID:integer;status:boolean);stdcall;
145 begin
146    Form1.StatusBar1.Panels.Items[0].Text:='Not connected';
147    if (status=True) then
148    begin
149         Form1.StatusBar1.Panels.Items[0].Text:='Connected';
150         Form1.GetInfoButtonClick(NIL);
151    end;
152 end;
153 
154 //called, when phone needs PIN, PUK, etc.
155 procedure ChangeSecurityState(x:integer;ID:integer;SecurityState:GSM_SecurityCodeType);stdcall;
156 var
157         Code    : GSM_SecurityCode;
158         i       : integer;
159         error   : GSM_Error;
160 begin
161    //we show type of required code
162    Form1.StatusBar1.Panels.Items[1].Text:='';
163    case SecurityState of
164         SEC_SecurityCode:Form1.StatusBar1.Panels.Items[1].Text:='Security code';
165         SEC_Pin         :Form1.StatusBar1.Panels.Items[1].Text:='PIN';
166         SEC_Pin2        :Form1.StatusBar1.Panels.Items[1].Text:='PIN2';
167         SEC_Puk         :Form1.StatusBar1.Panels.Items[1].Text:='PUK';
168         SEC_Puk2        :Form1.StatusBar1.Panels.Items[1].Text:='PUK2';
169         SEC_None        :Form1.StatusBar1.Panels.Items[1].Text:='';
170    end;
171 
172    //we want to enter PIN
173    if (SecurityState = SEC_Pin) then
174    begin
175         //in PINEdit there was some text possibly with PIN
176         if (Form1.PINEdit.Text<>'') then
177         begin
178                 Code.CodeType:=SEC_Pin;
179                 for i:=1 to 4 do Code.Code[i]:=Form1.PINEdit.Text[i];
180                 Code.Code[5]:=chr(0);
181                 error := GSM_EnterSecurityCode(ID,@Code);
182                 //code was probably incorrect
183                 if (error <> ERR_NONE) then
184                 begin
185                         Form1.InfoListBox.Items.Add('Error '+inttostr(integer(error)));
186                         Form1.PINEdit.Text:='';
187                 end;
188         end;
189    end;
190 end;
191 
192 //called, where there are ANY SMS on SIM
193 procedure HandleIncomingSMS(x:integer;ID:integer);stdcall;
194 begin
195         Form1.InfoListBox.Items.Add('SMS on sim');
196         Form1.GetAllInboxSMSButtonClick(nil);
197 end;
198 
199 //after pressing Init button
200 procedure TForm1.InitButtonClick(Sender: TObject);
201 var
202         Device: PChar;
203         Connection: PChar;
204         error: GSM_Error;
205 begin
206    GetMem(Device,Length(DeviceComboBox.Text) + 1);
207    StrCopy(Device, PChar(DeviceComboBox.Text));
208 
209    GetMem(Connection,50);
210    Connection[0] := chr(0);
211    PhoneCallBackPointer    := @ChangePhoneState1;
212    SecurityCallBackPointer := @ChangeSecurityState;
213    SMSCallBackPointer      := @HandleIncomingSMS;
214    error:=GSM_StartConnection(@PhoneID,Device,Connection,'','logfile','text',false,@PhoneCallBackPointer,@SecurityCallBackPointer,@SMSCallBackPointer);
215    if (error=ERR_NONE) then
216    begin
217            ConnectionLabel.Caption:=Connection;
218            InitButton.Enabled:=False;
219            DeviceComboBox.Enabled:=False;
220            PINEdit.Enabled:=False;
221            Pin.Enabled:=False;
222            ConnectionLabel.Enabled:=False;
223            TerminateButton.Enabled:=True;
224    end else
225    begin
226         application.MessageBox(pchar('GSM device not found, error '+inttostr(integer(error))),'',0);
227    end;
228    FreeMem(Device);
229    FreeMem(Connection);
230    Form1.GetInfoButtonClick(Sender);
231 end;
232 
233 //After pressing Send Button
234 procedure TForm1.SendButtonClick(Sender: TObject);
235 var     i:word;
236         error:GSM_Error;
237 begin
238    //destination number
239    i:=0;
240    while i<strlen(PChar(DestinationNumberEdit.Text)) do
241    begin
242         SendSMS.Number[i*2+1]:=DestinationNumberEdit.Text[i+1];
243         SendSMS.Number[i*2+2]:=chr(0);
244         i:=i+1;
245    end;
246    SendSMS.Number[i*2+1]:=chr(0);
247    SendSMS.Number[i*2+2]:=chr(0);
248 
249    //sms text
250    SendSMS.Length:=strlen(PChar(SMSTextEdit.Text));
251    for i:=1 to 20 do SendSMS.Text[i]:='a';
252    i:=0;
253    while i<strlen(PChar(SMSTextEdit.Text)) do
254    begin
255         SendSMS.Text[i*2+1]:=SMSTextEdit.Text[i+1];
256         SendSMS.Text[i*2+2]:=chr(0);
257         i:=i+1;
258    end;
259    SendSMS.Text[i*2+1]:=chr(0);
260    SendSMS.Text[i*2+2]:=chr(0);
261    SendSMS.Coding:=GSM_Coding_Default;
262    SendSMS.UDH.UDHType:=UDH_NoUDH;
263 
264    SendSMS.SMSC.Location:=1; //we will get SMSC settings from phone
265    SendSMS.ReplyViaSameSMSC:=false; //don't allow to reply for the same SMSC
266    if form1.DeliveryReportCheckBox.Checked then
267    begin
268            SendSMS.PDU:=SMS_Status_Report; //sms with delivery report
269    end else
270    begin
271            SendSMS.PDU:=SMS_Submit; //sms without delivery report
272    end;
273    SendSMS.SMSClass:=-1; //no sms class - will be saved to SIM in destination
274    SendSMS.RejectDuplicates:=false;
275    SendSMS.MessageReference:=chr(0);
276    SendSMS.ReplaceMessage:=chr(0);
277 
278 //   SendSMS.Name[1]:=chr(0);
279 //   SendSMS.Name[2]:=chr(0);
280 //   SendSMS.PDU:=SMS_Deliver;
281 //   SendSMS.Folder:=1;
282 //   SendSMS.Location:=0;
283 //   error:=GSM_SaveSMSMessage(PhoneID,@SendSMS);
284 
285    //30 is timeout in seconds. Phone waits so long time for network answer
286    error:=GSM_SendSMSMessage(PhoneID,@SendSMS,30);
287    InfoListBox.Items.Add('Error '+inttostr(integer(error)));
288 end;
289 
290 procedure TForm1.ResetButtonClick(Sender: TObject);
291 var error:GSM_Error;
292 begin
293    error:=GSM_Reset(PhoneID,true);
294    if (error<>ERR_NONE) then InfoListBox.Items.Add('Error '+inttostr(integer(error)));
295 end;
296 
297 procedure TForm1.TerminateButtonClick(Sender: TObject);
298 var error: GSM_Error;
299 begin
300    error:=GSM_EndConnection(PhoneID);
301    if (error=ERR_NONE) then
302    begin
303            ConnectionLabel.Caption:='>>>connection<<<';
304            InitButton.Enabled:=True;
305            DeviceComboBox.Enabled:=True;
306            PINEdit.Enabled:=True;
307            Pin.Enabled:=True;
308            ConnectionLabel.Enabled:=True;
309            TerminateButton.Enabled:=False;
310    end else
311    begin
312         application.MessageBox(pchar('GSM device not found, error '+inttostr(integer(error))),'',0);
313    end;
314 end;
315 
316 //called, when phone is connected or disconnected
317 procedure ChangePhoneState2(ID:integer;status:boolean);stdcall;
318 begin
319    Form1.StatusBar1.Panels.Items[0].Text:='Not connected - CallBack2';
320    if (status=True) then
321    begin
322         Form1.StatusBar1.Panels.Items[0].Text:='Connected - CallBack2';
323    end;
324 end;
325 
326 procedure TForm1.Button1Click(Sender: TObject);
327 begin
328    PhoneCallBackPointer := @ChangePhoneState2;
329 end;
330 
331 end.
332