Skip to content

Commit 071aeef

Browse files
committed
Cleanup
1 parent f40c9b3 commit 071aeef

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

src/HeboTech.ATLib.TestConsole/Program.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ namespace HeboTech.ATLib.TestConsole
1111
{
1212
class Program
1313
{
14-
static async Task Main(string[] args)
14+
static void Main(string[] args)
1515
{
16-
TimeService.SetProvider(new SystemTimeProvider());
17-
18-
using (SerialPort serialPort = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One))
16+
using (SerialPort serialPort = new SerialPort(args[0], 9600, Parity.None, 8, StopBits.One))
1917
{
2018
Console.WriteLine("Opening serial port...");
2119
serialPort.Open();
@@ -39,7 +37,7 @@ static async Task Main(string[] args)
3937

4038
if (simStatus == SimStatus.SIM_PIN)
4139
{
42-
var simPinStatus = modem.EnterSimPin(new PersonalIdentificationNumber(args[0]));
40+
var simPinStatus = modem.EnterSimPin(new PersonalIdentificationNumber(args[1]));
4341
Console.WriteLine($"SIM PIN Status: {simPinStatus}");
4442

4543
simStatus = modem.GetSimStatus();
@@ -52,10 +50,10 @@ static async Task Main(string[] args)
5250
var batteryStatus = modem.GetBatteryStatus();
5351
Console.WriteLine($"Battery Status: {batteryStatus}");
5452

55-
var smsReference = modem.SendSMS(new PhoneNumber(args[1]), "Hello ATLib!");
56-
Console.WriteLine($"SMS Reference: {smsReference}");
53+
var productInfo = modem.GetProductIdentificationInformation();
54+
Console.WriteLine($"Product Information:{Environment.NewLine}{productInfo}");
5755

58-
Console.WriteLine("Done. Press 'a' to answer call, 'h' to hang up, and 'q' to exit...");
56+
Console.WriteLine("Done. Press 'a' to answer call, 'h' to hang up, 's' to send SMS and 'q' to exit...");
5957
ConsoleKey key;
6058
while ((key = Console.ReadKey().Key) != ConsoleKey.Q)
6159
{
@@ -69,6 +67,11 @@ static async Task Main(string[] args)
6967
var callDetails = modem.Hangup();
7068
Console.WriteLine($"Call Details: {callDetails}");
7169
break;
70+
case ConsoleKey.S:
71+
Console.WriteLine("Sending SMS...");
72+
var smsReference = modem.SendSMS(new PhoneNumber(args[2]), "Hello ATLib!");
73+
Console.WriteLine($"SMS Reference: {smsReference}");
74+
break;
7275
}
7376
}
7477
modem.Close();

src/HeboTech.ATLib/Modems/IModem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ public interface IModem
1414
CommandStatus DisableEcho();
1515
CommandStatus EnterSimPin(PersonalIdentificationNumber pin);
1616
BatteryStatus GetBatteryStatus();
17+
ProductIdentificationInformation GetProductIdentificationInformation();
18+
RemainingPinPukAttempts GetRemainingPinPukAttempts();
1719
SignalStrength GetSignalStrength();
1820
SimStatus GetSimStatus();
1921
CallDetails Hangup();
2022
SmsReference SendSMS(PhoneNumber phoneNumber, string message);
21-
RemainingPinPukAttempts GetRemainingPinPukAttempts();
2223
}
2324
}

src/HeboTech.ATLib/Modems/ModemBase.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using HeboTech.ATLib.Parsers;
44
using HeboTech.ATLib.Results;
55
using System;
6+
using System.Diagnostics;
67
using System.Globalization;
78
using System.Linq;
9+
using System.Text;
810
using System.Text.RegularExpressions;
911

1012
namespace HeboTech.ATLib.Modems
@@ -40,7 +42,7 @@ public virtual void Close()
4042

4143
public virtual CommandStatus DisableEcho()
4244
{
43-
var error = channel.SendCommand("ATE0", out _);
45+
var error = channel.SendCommand("ATE0");
4446

4547
if (error == AtChannel.AtError.NO_ERROR)
4648
return CommandStatus.OK;
@@ -84,9 +86,9 @@ public virtual SimStatus GetSimStatus()
8486

8587
public virtual CommandStatus EnterSimPin(PersonalIdentificationNumber pin)
8688
{
87-
var error = channel.SendCommand($"AT+CPIN={pin}", out AtResponse response);
89+
var error = channel.SendCommand($"AT+CPIN={pin}");
8890

89-
if (error == AtChannel.AtError.NO_ERROR && response.Success)
91+
if (error == AtChannel.AtError.NO_ERROR)
9092
return CommandStatus.OK;
9193
else return CommandStatus.ERROR;
9294
}
@@ -111,7 +113,7 @@ public virtual SignalStrength GetSignalStrength()
111113

112114
public virtual CommandStatus AnswerIncomingCall()
113115
{
114-
var error = channel.SendCommand("ATA", out _);
116+
var error = channel.SendCommand("ATA");
115117

116118
if (error == AtChannel.AtError.NO_ERROR)
117119
return CommandStatus.OK;
@@ -192,5 +194,22 @@ public virtual RemainingPinPukAttempts GetRemainingPinPukAttempts()
192194
}
193195
return null;
194196
}
197+
198+
public ProductIdentificationInformation GetProductIdentificationInformation()
199+
{
200+
var error = channel.SendMultilineCommand("ATI", null, out AtResponse response);
201+
202+
if (error == AtChannel.AtError.NO_ERROR)
203+
{
204+
StringBuilder builder = new StringBuilder();
205+
foreach (string line in response.Intermediates)
206+
{
207+
builder.AppendLine(line);
208+
}
209+
210+
return new ProductIdentificationInformation(builder.ToString());
211+
}
212+
return null;
213+
}
195214
}
196215
}

src/HeboTech.ATLib/Parsers/AtChannel.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public enum AtCommandType
1313
NO_RESULT, // No intermediate response expected
1414
NUMERIC, // A single intermediate response starting with a 0-9
1515
SINGELLINE, // A single intermediate response starting with a prefix
16-
MULTILINE // Multiple line intermediate response starting with a prefix
16+
MULTILINE, // Multiple line intermediate response starting with a prefix
17+
MULTILINE_NO_PREFIX // Multiple line intermediate response without a prefix
1718
}
1819

1920
public enum AtError
@@ -70,9 +71,15 @@ public AtChannel(ICommunicator comm)
7071
readerThread.Start();
7172
}
7273

73-
public AtError SendCommand(string command, out AtResponse response)
74+
/// <summary>
75+
/// Send command and get command status
76+
/// </summary>
77+
/// <param name="command"></param>
78+
/// <param name="response"></param>
79+
/// <returns></returns>
80+
public AtError SendCommand(string command)
7481
{
75-
AtError error = SendFullCommand(command, AtCommandType.NO_RESULT, null, null, TimeSpan.Zero, out response);
82+
AtError error = SendFullCommand(command, AtCommandType.NO_RESULT, null, null, TimeSpan.Zero, out _);
7683
return error;
7784
}
7885

@@ -92,7 +99,8 @@ public AtError SendSingleLineCommand(string command, string responsePrefix, out
9299

93100
public AtError SendMultilineCommand(string command, string responsePrefix, out AtResponse response)
94101
{
95-
AtError error = SendFullCommand(command, AtCommandType.MULTILINE, responsePrefix, null, TimeSpan.Zero, out response);
102+
AtCommandType commandType = responsePrefix == null ? AtCommandType.MULTILINE_NO_PREFIX : AtCommandType.MULTILINE;
103+
AtError error = SendFullCommand(command, commandType, responsePrefix, null, TimeSpan.Zero, out response);
96104
return error;
97105
}
98106

@@ -282,6 +290,9 @@ private void ProcessLine(string line)
282290
HandleUnsolicited(line);
283291
}
284292
break;
293+
case AtCommandType.MULTILINE_NO_PREFIX:
294+
AddIntermediate(line);
295+
break;
285296
default:
286297
// This should never be reached
287298
//TODO: Log error or something
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace HeboTech.ATLib.Results
2+
{
3+
public class ProductIdentificationInformation
4+
{
5+
public ProductIdentificationInformation(string information)
6+
{
7+
Information = information;
8+
}
9+
10+
public string Information { get; }
11+
12+
public override string ToString()
13+
{
14+
return Information;
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)