File: gsm_sms_store.cc

package info (click to toggle)
gsmlib 1.10+20120414.gita5e5ae9a-0.3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 3,468 kB
  • ctags: 1,907
  • sloc: cpp: 12,315; sh: 11,376; ansic: 2,366; makefile: 263; sed: 93
file content (488 lines) | stat: -rw-r--r-- 11,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// *************************************************************************
// * GSM TA/ME library
// *
// * File:    gsm_sms_store.cc
// *
// * Purpose: SMS functions, SMS store
// *          (ETSI GSM 07.05)
// *
// * Author:  Peter Hofmann (software@pxh.de)
// *
// * Created: 20.5.1999
// *************************************************************************

#ifdef HAVE_CONFIG_H
#include <gsm_config.h>
#endif
#include <gsmlib/gsm_sms_store.h>
#include <gsmlib/gsm_parser.h>
#include <gsmlib/gsm_me_ta.h>
#include <iostream>

using namespace gsmlib;

// SMSStoreEntry members

SMSStoreEntry::SMSStoreEntry() :
   _status(Unknown), _cached(false), _mySMSStore(NULL), _index(0)
{
}


SMSMessageRef SMSStoreEntry::message() const throw(GsmException)
{
  if (! cached())
  {
    assert(_mySMSStore != NULL);
    // these operations are at least "logically const"
    SMSStoreEntry *thisEntry = const_cast<SMSStoreEntry*>(this);
    _mySMSStore->readEntry(_index, thisEntry->_message, thisEntry->_status);
    thisEntry->_cached = true;
  }
  return _message;
}

CBMessageRef SMSStoreEntry::cbMessage() const throw(GsmException)
{
  assert(_mySMSStore != NULL);

  // these operations are at least "logically const"
  SMSStoreEntry *thisEntry = const_cast<SMSStoreEntry*>(this);
  // don't cache CB message for now
  thisEntry->_cached = false;

  CBMessageRef result;
  _mySMSStore->readEntry(_index, result);
  return result;
}

SMSStoreEntry::SMSMemoryStatus SMSStoreEntry::status() const
  throw(GsmException)
{
  if (! cached())
  {
    assert(_mySMSStore != NULL);
    // these operations are at least "logically const"
    SMSStoreEntry *thisEntry = const_cast<SMSStoreEntry*>(this);
    _mySMSStore->readEntry(_index, thisEntry->_message, thisEntry->_status);
    thisEntry->_cached = true;
  }
  return _status;
}

bool SMSStoreEntry::empty() const throw(GsmException)
{
  return message().isnull();
}

unsigned char SMSStoreEntry::send(Ref<SMSMessage> &ackPdu)
  throw(GsmException)
{
  return _mySMSStore->send(_index, ackPdu);
}

unsigned char SMSStoreEntry::send() throw(GsmException)
{
  SMSMessageRef mref;
  return send(mref);
}

bool SMSStoreEntry::cached() const
{
  if (_mySMSStore == NULL)
    return _cached;
  else
    return _cached && _mySMSStore->_useCache;
}

Ref<SMSStoreEntry> SMSStoreEntry::clone()
{
  Ref<SMSStoreEntry> result = new SMSStoreEntry(_message->clone());
  result->_status = _status;
  result->_index = _index;
  return result;
}

bool SMSStoreEntry::operator==(const SMSStoreEntry &e) const
{
  if (_message.isnull() || e._message.isnull())
    return _message.isnull() && e._message.isnull();
  else
    return _message->encode() == e._message->encode();
}

SMSStoreEntry::SMSStoreEntry(const SMSStoreEntry &e)
{
 _message = e._message;
 _status = e._status;
 _cached = e._cached;
 _mySMSStore = e._mySMSStore;
 _index = e._index;
}

SMSStoreEntry &SMSStoreEntry::operator=(const SMSStoreEntry &e)
{
 _message = e._message;
 _status = e._status;
 _cached = e._cached;
 _mySMSStore = e._mySMSStore;
 _index = e._index;
 return *this;
}

// iterator members

SMSStoreEntry &SMSStoreIterator::operator*()
{
  return (*_store)[_index];
}
  
SMSStoreEntry *SMSStoreIterator::operator->()
{
  return &(*_store)[_index];
}

SMSStoreIterator::operator SMSStoreEntry*()
{
  return &(*_store)[_index];
}

SMSStoreIterator &SMSStoreIterator::operator=(const SMSStoreIterator &i)
{
  _index = i._index;
  _store = i._store;
  return *this;
}

const SMSStoreEntry &SMSStoreConstIterator::operator*()
{
  return (*_store)[_index];
}
  
const SMSStoreEntry *SMSStoreConstIterator::operator->()
{
  return &(*_store)[_index];
}

// SMSStore members

void SMSStore::readEntry(int index, SMSMessageRef &message,
                         SMSStoreEntry::SMSMemoryStatus &status)
  throw(GsmException)
{
  // select SMS store
  _meTa.setSMSStore(_storeName, 1);

#ifndef NDEBUG
  if (debugLevel() >= 1)
    std::cerr << "*** Reading SMS entry " << index << std::endl;
#endif // NDEBUG

  std::string pdu;
  Ref<Parser> p;
  try
  {
    p = new Parser(_at->chat("+CMGR=" + intToStr(index + 1), "+CMGR:",
                             pdu, false, true, true));
  }
  catch (GsmException &ge)
  {
    if (ge.getErrorCode() != SMS_INVALID_MEMORY_INDEX)
      throw ge;
    else
    {
      message = SMSMessageRef();
      status = SMSStoreEntry::Unknown;
      return;
    }
  }

  if (pdu.length() == 0)
  {
    message = SMSMessageRef();
    status = SMSStoreEntry::Unknown;
  }
  else
  {
    // add missing service centre address if required by ME
    if (! _at->getMeTa().getCapabilities()._hasSMSSCAprefix)
      pdu = "00" + pdu;

    status = (SMSStoreEntry::SMSMemoryStatus)p->parseInt();

    // ignore the rest of the line
    message = SMSMessageRef(
      SMSMessage::decode(pdu,
                         !(status == SMSStoreEntry::StoredUnsent ||
                           status == SMSStoreEntry::StoredSent),
                         _at.getptr()));
  }
}

void SMSStore::readEntry(int index, CBMessageRef &message)
  throw(GsmException)
{
  // select SMS store
  _meTa.setSMSStore(_storeName, 1);

#ifndef NDEBUG
  if (debugLevel() >= 1)
    std::cerr << "*** Reading CB entry " << index << std::endl;
#endif // NDEBUG

  std::string pdu;
  Ref<Parser> p;
  try
  {
    // this is just one row splitted in two part
    // (msvc6 fail with internal compiler error)
    std::string s = _at->chat("+CMGR=" + intToStr(index + 1), "+CMGR:",
                         pdu, false, true, true);
    p = new Parser(s);
  }
  catch (GsmException &ge)
  {
    if (ge.getErrorCode() != SMS_INVALID_MEMORY_INDEX)
      throw ge;
    else
    {
      message = CBMessageRef();
      return;
    }
  }

  if (pdu.length() == 0)
    message = CBMessageRef();
  else
    message = CBMessageRef(new CBMessage(pdu));
}

void SMSStore::writeEntry(int &index, SMSMessageRef message)
  throw(GsmException)
{
  // select SMS store
  _meTa.setSMSStore(_storeName, 2);

#ifndef NDEBUG
  if (debugLevel() >= 1)
    std::cerr << "*** Writing SMS entry " << index << std::endl;
#endif
  
  // compute length of pdu
  std::string pdu = message->encode();

  // set message status to "RECEIVED READ" for SMS_DELIVER, SMS_STATUS_REPORT
  std::string statusString;

  // Normally the ",1" sets the message status to "REC READ" (received read)
  // which is appropriate for all non-submit messages
  // Motorola Timeport 260 does not like this code, though
  // DELIVER messages are magically recognized anyway
  if (message->messageType() != SMSMessage::SMS_SUBMIT &&
      ! _at->getMeTa().getCapabilities()._wrongSMSStatusCode)
    statusString = ",1";

  Parser p(_at->sendPdu("+CMGW=" +
                        intToStr(pdu.length() / 2 -
                                 message->getSCAddressLen()) + statusString,
                        "+CMGW:", pdu));
  index = p.parseInt() - 1;
}

void SMSStore::eraseEntry(int index) throw(GsmException)
{
  // Select SMS store
  _meTa.setSMSStore(_storeName, 1);

#ifndef NDEBUG
  if (debugLevel() >= 1)
    std::cerr << "*** Erasing SMS entry " << index << std::endl;
#endif
  
  _at->chat("+CMGD=" + intToStr(index + 1));
}

unsigned char SMSStore::send(int index, Ref<SMSMessage> &ackPdu)
 throw(GsmException)
{
  Parser p(_at->chat("+CMSS=" + intToStr(index + 1), "+CMSS:"));
  unsigned char messageReference = p.parseInt();

  if (p.parseComma(true))
  {
    std::string pdu = p.parseEol();

    // add missing service centre address if required by ME
    if (! _at->getMeTa().getCapabilities()._hasSMSSCAprefix)
      pdu = "00" + pdu;

    ackPdu = SMSMessage::decode(pdu);
  }
  else
    ackPdu = SMSMessageRef();

  return messageReference;
}

int SMSStore::doInsert(SMSMessageRef message)
  throw(GsmException)
{
  int index;
  writeEntry(index, message);
  // it is safer to force reading back the SMS from the ME
  resizeStore(index + 1);
  _store[index]->_cached = false;
  return index;
}

SMSStore::SMSStore(std::string storeName, Ref<GsmAt> at, MeTa &meTa)
  throw(GsmException) :
  _storeName(storeName), _at(at), _meTa(meTa), _useCache(true)
{
  // select SMS store
  Parser p(_meTa.setSMSStore(_storeName, true, true));
  
  p.parseInt();                 // skip number of used mems
  p.parseComma();

  resizeStore(p.parseInt());    // ignore rest of line
}

void SMSStore::resizeStore(int newSize)
{
  int oldSize = _store.size();
  if (newSize > oldSize)
  {
    //    cout << "*** Resizing from " << oldSize << " to " << newSize << endl;
    _store.resize(newSize);
    
    // initialize store entries
    for (int i = oldSize; i < newSize; i++)
    {
      _store[i] = new SMSStoreEntry();
      _store[i]->_index = i;
      _store[i]->_cached = false;
      _store[i]->_mySMSStore = this;
    }
  }
}

SMSStore::iterator SMSStore::begin()
{
  return SMSStoreIterator(0, this);
}

SMSStore::const_iterator SMSStore::begin() const
{
  return SMSStoreConstIterator(0, this);
}

SMSStore::iterator SMSStore::end()
{
  return SMSStoreIterator(_store.size(), this);
}

SMSStore::const_iterator SMSStore::end() const
{
  return SMSStoreConstIterator(_store.size(), this);
}

SMSStore::reference SMSStore::operator[](int n)
{
  resizeStore(n + 1);
  return *_store[n];
}

SMSStore::const_reference SMSStore::operator[](int n) const
{
  const_cast<SMSStore*>(this)->resizeStore(n + 1);
  return *_store[n];
}

SMSStore::reference SMSStore::front()
{
  return *_store[0];
}

SMSStore::const_reference SMSStore::front() const
{
  return *_store[0];
}

SMSStore::reference SMSStore::back()
{
  return *_store.back();
}

SMSStore::const_reference SMSStore::back() const
{
  return *_store.back();
}

int SMSStore::size() const throw(GsmException)
{
  // select SMS store
  Parser p(_meTa.setSMSStore(_storeName, 1, true));
  
  return p.parseInt();  
}

SMSStore::iterator SMSStore::insert(iterator position,
                                    const SMSStoreEntry& x)
  throw(GsmException)
{
  int index = doInsert(x.message());
  return SMSStoreIterator(index, this);
}

SMSStore::iterator SMSStore::insert(const SMSStoreEntry& x)
  throw(GsmException)
{
  int index = doInsert(x.message());
  return SMSStoreIterator(index, this);
}

void SMSStore::insert (iterator pos, int n, const SMSStoreEntry& x)
  throw(GsmException)
{
  for (int i = 0; i < n; i++)
    doInsert(x.message());
}

void SMSStore::insert (iterator pos, long n, const SMSStoreEntry& x)
  throw(GsmException)
{
  for (long i = 0; i < n; i++)
    doInsert(x.message());
}

SMSStore::iterator SMSStore::erase(iterator position)
  throw(GsmException)
{
  eraseEntry(position->_index);
  position->_cached = false;
  return position + 1;
}

SMSStore::iterator SMSStore::erase(iterator first, iterator last)
  throw(GsmException)
{
  iterator i(0, this);
  for (i = first; i != last; ++i)
    erase(i);
  return i;
}

void SMSStore::clear() throw(GsmException)
{
  for (iterator i = begin(); i != end(); ++i)
    erase(i);
}

SMSStore::~SMSStore()
{
  for (std::vector<SMSStoreEntry*>::iterator i = _store.begin();
       i != _store.end(); ++i)
    delete *i;
}