|
| 1 | +using SmsTools.Commands; |
| 2 | +using SmsTools.Operations; |
| 3 | +using SmsTools.PduProfile; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Text.RegularExpressions; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace SmsTools |
| 12 | +{ |
| 13 | + public class MessageStorage |
| 14 | + { |
| 15 | + private PduProfileManager _manager = new PduProfileManager(); |
| 16 | + private ICommandParameter _empty = null; |
| 17 | + private IATCommand _storageQuery = null; |
| 18 | + private IATCommand _listQuery = null; |
| 19 | + private IATCommand _mfCmd = null; |
| 20 | + |
| 21 | + |
| 22 | + public MessageStorage() |
| 23 | + { |
| 24 | + init(); |
| 25 | + } |
| 26 | + |
| 27 | + private void init() |
| 28 | + { |
| 29 | + _empty = CommandParameter.CreateEmpty(Constants.BasicSuccessfulResponse); |
| 30 | + _storageQuery = new SimpleATCommand(ATCommand.MessageStorageInfo.Command(), _empty); |
| 31 | + |
| 32 | + var listParam = new CommandParameter(Constants.MessageStatus.Any.ToValueString(), Constants.BasicSuccessfulResponse); |
| 33 | + _listQuery = new ParamATCommand(ATCommand.MessageList.Command(), listParam); |
| 34 | + |
| 35 | + var mfParam = new CommandParameter(Constants.MessageFormat.Pdu.ToValueString(), Constants.BasicSuccessfulResponse); |
| 36 | + _mfCmd = new ParamATCommand(ATCommand.MessageFormat.Command(), mfParam); |
| 37 | + } |
| 38 | + |
| 39 | + public async Task<bool> SelectStorage(IPortPlug port, Constants.MessageStorage readStore = Constants.MessageStorage.MobileEquipment, Constants.MessageStorage writeStore = Constants.MessageStorage.Unspecified, Constants.MessageStorage receivedStore = Constants.MessageStorage.Unspecified) |
| 40 | + { |
| 41 | + if (readStore == Constants.MessageStorage.Unspecified) |
| 42 | + return false; |
| 43 | + |
| 44 | + var storageParam = new CommandParameter(getStorageParam(readStore, writeStore, receivedStore), Constants.BasicSuccessfulResponse); |
| 45 | + var storageCmd = new ParamATCommand(ATCommand.MessageStorage.Command(), storageParam); |
| 46 | + |
| 47 | + await storageCmd.ExecuteAsync(port); |
| 48 | + |
| 49 | + return storageCmd.Succeeded(); |
| 50 | + } |
| 51 | + |
| 52 | + public async Task<IEnumerable<MessageStorageState>> StorageState(IPortPlug port) |
| 53 | + { |
| 54 | + var result = Enumerable.Empty<MessageStorageState>(); |
| 55 | + |
| 56 | + await _storageQuery.ExecuteAsync(port); |
| 57 | + if (_storageQuery.Succeeded()) |
| 58 | + { |
| 59 | + result = getStorageState(_storageQuery.Response); |
| 60 | + } |
| 61 | + |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + public async Task<IEnumerable<MessageStorageItem>> List(IPortPlug port) |
| 66 | + { |
| 67 | + var result = Enumerable.Empty<MessageStorageItem>(); |
| 68 | + |
| 69 | + if (await setFormat(port)) |
| 70 | + { |
| 71 | + await _listQuery.ExecuteAsync(port); |
| 72 | + if (_listQuery.Succeeded()) |
| 73 | + { |
| 74 | + result = getStorageItems(_listQuery.Response); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return result; |
| 79 | + } |
| 80 | + |
| 81 | + public async Task<MessageInfo> Read(IPortPlug port, MessageStorageItem item) |
| 82 | + { |
| 83 | + MessageInfo result = new MessageInfo(); |
| 84 | + |
| 85 | + if (item == null || !item.IsValid || !_manager.ContainsProfile("default-receive")) |
| 86 | + return result; |
| 87 | + |
| 88 | + if (await setFormat(port)) |
| 89 | + { |
| 90 | + var readParam = new CommandParameter(item.Index.ToString(), Constants.BasicSuccessfulResponse); |
| 91 | + var readCmd = new ParamATCommand(ATCommand.MessageRead.Command(), readParam); |
| 92 | + |
| 93 | + await readCmd.ExecuteAsync(port); |
| 94 | + if (readCmd.Succeeded()) |
| 95 | + { |
| 96 | + result = getMessage(readCmd.Response); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + private MessageInfo getMessage(string response) |
| 105 | + { |
| 106 | + MessageInfo result = new MessageInfo(); |
| 107 | + |
| 108 | + try |
| 109 | + { |
| 110 | + var lengthMatch = Regex.Match(response, @"^\S*(?:cmgr:.+,(\d+)\s*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline); |
| 111 | + var pduMatch = Regex.Match(response, @"^(?:\s*([a-fA-F0-9]+)\s*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline); |
| 112 | + |
| 113 | + if (lengthMatch.Success && lengthMatch.Groups.Count > 1 && pduMatch.Success && pduMatch.Groups.Count > 1) |
| 114 | + { |
| 115 | + int length = int.Parse(lengthMatch.Groups[1].Value.Trim()); |
| 116 | + string pdu = pduMatch.Groups[1].Value.Trim(); |
| 117 | + |
| 118 | + var profile = _manager["default-receive"]; |
| 119 | + |
| 120 | + result = profile.GetMessage(pdu, length); |
| 121 | + } |
| 122 | + } |
| 123 | + catch { } |
| 124 | + |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + private IEnumerable<MessageStorageItem> getStorageItems(string response) |
| 129 | + { |
| 130 | + var result = Enumerable.Empty<MessageStorageItem>(); |
| 131 | + |
| 132 | + try |
| 133 | + { |
| 134 | + var matches = Regex.Matches(response, @"^\S*(?:cmgl:(.+))$", RegexOptions.IgnoreCase | RegexOptions.Multiline); |
| 135 | + if (matches.Count > 0) |
| 136 | + { |
| 137 | + var items = new List<MessageStorageItem>(); |
| 138 | + |
| 139 | + for (int m = 0; m < matches.Count; ++m) |
| 140 | + { |
| 141 | + var match = matches[m]; |
| 142 | + if (!match.Success || match.Groups.Count < 2) |
| 143 | + throw new Exception(); |
| 144 | + |
| 145 | + var itemValue = match.Groups[1].Value.Trim(); |
| 146 | + var itemValues = itemValue.Split(','); |
| 147 | + int value = 0; |
| 148 | + if (itemValues.Length < 3 || itemValues.Any(v => !int.TryParse(v, out value))) |
| 149 | + throw new Exception(); |
| 150 | + |
| 151 | + var item = itemValues.Select(v => int.Parse(v)).ToArray(); |
| 152 | + |
| 153 | + items.Add(new MessageStorageItem() { Index = item[0], Status = (Constants.MessageStatus)item[1], Length = item.Last() }); |
| 154 | + } |
| 155 | + |
| 156 | + result = items; |
| 157 | + } |
| 158 | + } |
| 159 | + catch { } |
| 160 | + |
| 161 | + return result; |
| 162 | + } |
| 163 | + |
| 164 | + private IEnumerable<MessageStorageState> getStorageState(string response) |
| 165 | + { |
| 166 | + var result = Enumerable.Empty<MessageStorageState>(); |
| 167 | + |
| 168 | + try |
| 169 | + { |
| 170 | + var state = Regex.Match(response, $@"(?:cpms:(.+){Constants.BasicSuccessfulResponse})", RegexOptions.IgnoreCase); |
| 171 | + if (state.Success && state.Groups.Count > 1) |
| 172 | + { |
| 173 | + var stateValue = state.Groups[1].Value.Trim(); |
| 174 | + var stateValues = stateValue.Split(','); |
| 175 | + if (stateValues.Length % 3 == 0 && stateValues.All(s => !string.IsNullOrWhiteSpace(s))) |
| 176 | + { |
| 177 | + var storages = new List<MessageStorageState>(); |
| 178 | + |
| 179 | + for (int i = 0; i < stateValues.Length; i += 3) |
| 180 | + { |
| 181 | + var storageValue = stateValues[i]; |
| 182 | + var countValue = stateValues[i + 1]; |
| 183 | + var limitValue = stateValues[i + 2]; |
| 184 | + |
| 185 | + int count = 0, limit = 0; |
| 186 | + if (!int.TryParse(countValue, out count) || !int.TryParse(limitValue, out limit)) |
| 187 | + throw new Exception(); |
| 188 | + |
| 189 | + storages.Add(new MessageStorageState() { Storage = storageValue.ToMessageStorage(), Count = count, Limit = limit }); |
| 190 | + } |
| 191 | + |
| 192 | + result = storages; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + catch { } |
| 197 | + |
| 198 | + return result; |
| 199 | + } |
| 200 | + |
| 201 | + private string getStorageParam(Constants.MessageStorage readStore, Constants.MessageStorage writeStore, Constants.MessageStorage receivedStore) |
| 202 | + { |
| 203 | + var param = new StringBuilder(readStore.Description()); |
| 204 | + |
| 205 | + if (writeStore != Constants.MessageStorage.Unspecified) |
| 206 | + { |
| 207 | + param.Append(",").Append(writeStore.Description()); |
| 208 | + } |
| 209 | + |
| 210 | + if (receivedStore != Constants.MessageStorage.Unspecified) |
| 211 | + { |
| 212 | + param.Append(",").Append(receivedStore.Description()); |
| 213 | + } |
| 214 | + |
| 215 | + return param.ToString(); |
| 216 | + } |
| 217 | + |
| 218 | + private async Task<bool> setFormat(IPortPlug port) |
| 219 | + { |
| 220 | + await _mfCmd.ExecuteAsync(port); |
| 221 | + return _mfCmd.Succeeded(); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + |
| 226 | + public class MessageStorageState |
| 227 | + { |
| 228 | + public Constants.MessageStorage Storage { get; set; } |
| 229 | + public int Count { get; set; } |
| 230 | + public int Limit { get; set; } |
| 231 | + public bool IsFull { get { return Count == Limit; } } |
| 232 | + public bool IsEmpty { get { return Count == 0; } } |
| 233 | + } |
| 234 | + |
| 235 | + public class MessageStorageItem |
| 236 | + { |
| 237 | + public int Index { get; set; } |
| 238 | + public Constants.MessageStatus Status { get; set; } |
| 239 | + public int Length { get; set; } |
| 240 | + public bool IsValid { get { return Index >= 0 && Length >= 0; } } |
| 241 | + } |
| 242 | +} |
0 commit comments