Skip to content

Commit

Permalink
Add API for deleting all/multiple stored messages
Browse files Browse the repository at this point in the history
  • Loading branch information
faucamp committed Jul 18, 2013
1 parent 1bbb79f commit 6059481
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
32 changes: 29 additions & 3 deletions gsmmodem/modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False):
if delete:
if status == Sms.STATUS_ALL:
# Delete all messages
self.write('AT+CMGD=1,4')
self.deleteMultipleStoredSms()
else:
for msgIndex in delMessages:
self.deleteStoredSms(msgIndex)
Expand Down Expand Up @@ -1058,14 +1058,40 @@ def deleteStoredSms(self, index, memory=None):
@param index: The index of the SMS message in the specified memory
@type index: int
@param memory: The memory type to read from. If None, use the current default SMS read memory
@param memory: The memory type to delete from. If None, use the current default SMS read/delete memory
@type memory: str or None
@raise CommandError: if unable to read the stored message
@raise CommandError: if unable to delete the stored message
"""
self._setSmsMemory(readDelete=memory)
self.write('AT+CMGD={0},0'.format(index))

def deleteMultipleStoredSms(self, delFlag=4, memory=None):
""" Deletes all SMS messages that have the specified read status.
The messages are read from the memory set by the "memory" parameter.
The value of the "delFlag" paramater is the same as the "DelFlag" parameter of the +CMGD command:
1: Delete All READ messages
2: Delete All READ and SENT messages
3: Delete All READ, SENT and UNSENT messages
4: Delete All messages (this is the default)
@param delFlag: Controls what type of messages to delete; see description above.
@type delFlag: int
@param memory: The memory type to delete from. If None, use the current default SMS read/delete memory
@type memory: str or None
@param delete: If True, delete returned messages from the device/SIM card
@type delete: bool
@raise ValueErrror: if "delFlag" is not in range [1,4]
@raise CommandError: if unable to delete the stored messages
"""
if 0 < delFlag <= 4:
self._setSmsMemory(readDelete=memory)
self.write('AT+CMGD=1,{0}'.format(delFlag))
else:
raise ValueError('"delFlag" must be in range [1,4]')

def _handleUssd(self, lines):
""" Handler for USSD event notification line(s) """
if self._ussdSessionEvent:
Expand Down
33 changes: 32 additions & 1 deletion test/test_modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,6 @@ def test_deleteStoredSms(self):

tests = (1,2,3)
for index in tests:
# Test getting all messages
def writeCallbackFunc(data):
self.assertEqual('AT+CMGD={0},0\r'.format(index), data, 'Invalid data written to modem; expected "{0}", got: "{1}"'.format('AT+CMGD={0},0'.format(index), data))
self.modem.serial.writeCallbackFunc = writeCallbackFunc
Expand All @@ -1774,6 +1773,38 @@ def writeCallbackFunc2(data):
self.modem.serial.writeCallbackFunc = writeCallbackFunc2
self.modem.serial.writeCallbackFunc = writeCallbackFunc
self.modem.deleteStoredSms(index, memory=mem)

def test_deleteMultipleStoredSms(self):
self.initFakeModemResponses(textMode=True)
self.initModem(True, None)

tests = (4,3,2,1)
for delFlag in tests:
# Test getting all messages
def writeCallbackFunc(data):
self.assertEqual('AT+CMGD=1,{0}\r'.format(delFlag), data, 'Invalid data written to modem; expected "{0}", got: "{1}"'.format('AT+CMGD=1,{0}'.format(delFlag), data))
self.modem.serial.writeCallbackFunc = writeCallbackFunc
self.modem.deleteMultipleStoredSms(delFlag)
# Test switching SMS memory
tests = ((4, 'TEST1'), (4, 'ME'))
for delFlag, mem in tests:
def writeCallbackFunc(data):
self.assertEqual('AT+CPMS="{0}"\r'.format(mem), data, 'Invalid data written to modem; expected "{0}", got: "{1}"'.format('AT+CPMS="{0}"'.format(mem), data))
def writeCallbackFunc2(data):
self.assertEqual('AT+CMGD=1,{0}\r'.format(delFlag), data, 'Invalid data written to modem; expected "{0}", got: "{1}"'.format('AT+CMGD=1,{0}'.format(delFlag), data))
self.modem.serial.writeCallbackFunc = writeCallbackFunc2
self.modem.serial.writeCallbackFunc = writeCallbackFunc
self.modem.deleteMultipleStoredSms(delFlag, memory=mem)
# Test default delFlag value
delFlag = 4
def writeCallbackFunc3(data):
self.assertEqual('AT+CMGD=1,{0}\r'.format(delFlag), data, 'Invalid data written to modem; expected "{0}", got: "{1}"'.format('AT+CMGD=1,{0}'.format(delFlag), data))
self.modem.serial.writeCallbackFunc = writeCallbackFunc3
self.modem.deleteMultipleStoredSms()
# Test invalid delFlag values
tests = (0, 5, -3)
for delFlag in tests:
self.assertRaises(ValueError, self.modem.deleteMultipleStoredSms, **{'delFlag': delFlag})

def test_readStoredSms_pdu(self):
""" Tests reading stored SMS messages (PDU mode) """
Expand Down

0 comments on commit 6059481

Please sign in to comment.