From 3e775b715f50f5e1bf8e93aec6993dd9c71686a5 Mon Sep 17 00:00:00 2001 From: chen Date: Fri, 13 Apr 2018 20:21:57 +0800 Subject: [PATCH 1/2] add long sms concat info support for recived sms add `concat` property for received sms //get Concantion from pdu `smsDict['udh']` --- examples/sms_handler_demo.py | 4 +++- gsmmodem/modem.py | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/examples/sms_handler_demo.py b/examples/sms_handler_demo.py index ef1cb69..5118fd3 100755 --- a/examples/sms_handler_demo.py +++ b/examples/sms_handler_demo.py @@ -18,7 +18,9 @@ from gsmmodem.modem import GsmModem def handleSms(sms): - print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text)) + # long sms Concatenation support: reference, parts, number + concat = sms.concat.__dict__ if sms.concat else {} + print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nconcat: {2}\nMessage:\n{3}\n'.format(sms.number, sms.time, concat, sms.text)) print('Replying to SMS...') sms.reply(u'SMS received: "{0}{1}"'.format(sms.text[:20], '...' if len(sms.text) > 20 else '')) print('SMS sent.\n') diff --git a/gsmmodem/modem.py b/gsmmodem/modem.py index e468f34..6b7c543 100644 --- a/gsmmodem/modem.py +++ b/gsmmodem/modem.py @@ -7,7 +7,7 @@ from .serial_comms import SerialComms from .exceptions import CommandError, InvalidStateException, CmeError, CmsError, InterruptedException, TimeoutException, PinRequiredError, IncorrectPinError, SmscNumberUnknownError -from .pdu import encodeSmsSubmitPdu, decodeSmsPdu +from .pdu import encodeSmsSubmitPdu, decodeSmsPdu, Concatenation from .util import SimpleOffsetTzInfo, lineStartingWith, allLinesMatchingPattern, parseTextModeTimeStr from . import compat # For Python 2.6 compatibility @@ -49,11 +49,12 @@ def __init__(self, number, text, smsc=None): class ReceivedSms(Sms): """ An SMS message that has been received (MT) """ - def __init__(self, gsmModem, status, number, time, text, smsc=None): + def __init__(self, gsmModem, status, number, time, text, smsc=None, concat=None): super(ReceivedSms, self).__init__(number, text, smsc) self._gsmModem = weakref.proxy(gsmModem) self.status = status self.time = time + self.concat = concat def reply(self, message): """ Convenience method that sends a reply SMS to the sender of this message """ @@ -761,6 +762,15 @@ def processStoredSms(self, unreadOnly=False): for sms in messages: self.smsReceivedCallback(sms) + def _getConcat(self, smsDict): + concat = None + if smsDict.has_key('udh'): + for i in smsDict['udh']: + if isinstance(i, Concatenation): + concat = i + break + return concat + def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False): """ Returns SMS messages currently stored on the device/SIM card. @@ -827,7 +837,8 @@ def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False): self.log.debug('Discarding line from +CMGL response: %s', line) else: if smsDict['type'] == 'SMS-DELIVER': - sms = ReceivedSms(self, int(msgStat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc']) + concat = self._getConcat(smsDict) + sms = ReceivedSms(self, int(msgStat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], concat) elif smsDict['type'] == 'SMS-STATUS-REPORT': sms = StatusReport(self, int(msgStat), smsDict['reference'], smsDict['number'], smsDict['time'], smsDict['discharge'], smsDict['status']) else: @@ -1066,7 +1077,8 @@ def readStoredSms(self, index, memory=None): pdu = msgData[1] smsDict = decodeSmsPdu(pdu) if smsDict['type'] == 'SMS-DELIVER': - return ReceivedSms(self, int(stat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc']) + concat = self._getConcat(smsDict) + return ReceivedSms(self, int(stat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], concat) elif smsDict['type'] == 'SMS-STATUS-REPORT': return StatusReport(self, int(stat), smsDict['reference'], smsDict['number'], smsDict['time'], smsDict['discharge'], smsDict['status']) else: From 9c7ae29b47acccda264d98b7115e228fab1d9b36 Mon Sep 17 00:00:00 2001 From: chen Date: Fri, 13 Apr 2018 20:21:57 +0800 Subject: [PATCH 2/2] Add long sms concat info for received sms Add `concat` property for `ReceivedSms`: - Get Concantion from pdu `smsDict['udh']` - Update examples/sms_handler_demo.py --- examples/sms_handler_demo.py | 4 +++- gsmmodem/modem.py | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/examples/sms_handler_demo.py b/examples/sms_handler_demo.py index ef1cb69..5118fd3 100755 --- a/examples/sms_handler_demo.py +++ b/examples/sms_handler_demo.py @@ -18,7 +18,9 @@ from gsmmodem.modem import GsmModem def handleSms(sms): - print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text)) + # long sms Concatenation support: reference, parts, number + concat = sms.concat.__dict__ if sms.concat else {} + print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nconcat: {2}\nMessage:\n{3}\n'.format(sms.number, sms.time, concat, sms.text)) print('Replying to SMS...') sms.reply(u'SMS received: "{0}{1}"'.format(sms.text[:20], '...' if len(sms.text) > 20 else '')) print('SMS sent.\n') diff --git a/gsmmodem/modem.py b/gsmmodem/modem.py index e468f34..6b7c543 100644 --- a/gsmmodem/modem.py +++ b/gsmmodem/modem.py @@ -7,7 +7,7 @@ from .serial_comms import SerialComms from .exceptions import CommandError, InvalidStateException, CmeError, CmsError, InterruptedException, TimeoutException, PinRequiredError, IncorrectPinError, SmscNumberUnknownError -from .pdu import encodeSmsSubmitPdu, decodeSmsPdu +from .pdu import encodeSmsSubmitPdu, decodeSmsPdu, Concatenation from .util import SimpleOffsetTzInfo, lineStartingWith, allLinesMatchingPattern, parseTextModeTimeStr from . import compat # For Python 2.6 compatibility @@ -49,11 +49,12 @@ def __init__(self, number, text, smsc=None): class ReceivedSms(Sms): """ An SMS message that has been received (MT) """ - def __init__(self, gsmModem, status, number, time, text, smsc=None): + def __init__(self, gsmModem, status, number, time, text, smsc=None, concat=None): super(ReceivedSms, self).__init__(number, text, smsc) self._gsmModem = weakref.proxy(gsmModem) self.status = status self.time = time + self.concat = concat def reply(self, message): """ Convenience method that sends a reply SMS to the sender of this message """ @@ -761,6 +762,15 @@ def processStoredSms(self, unreadOnly=False): for sms in messages: self.smsReceivedCallback(sms) + def _getConcat(self, smsDict): + concat = None + if smsDict.has_key('udh'): + for i in smsDict['udh']: + if isinstance(i, Concatenation): + concat = i + break + return concat + def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False): """ Returns SMS messages currently stored on the device/SIM card. @@ -827,7 +837,8 @@ def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False): self.log.debug('Discarding line from +CMGL response: %s', line) else: if smsDict['type'] == 'SMS-DELIVER': - sms = ReceivedSms(self, int(msgStat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc']) + concat = self._getConcat(smsDict) + sms = ReceivedSms(self, int(msgStat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], concat) elif smsDict['type'] == 'SMS-STATUS-REPORT': sms = StatusReport(self, int(msgStat), smsDict['reference'], smsDict['number'], smsDict['time'], smsDict['discharge'], smsDict['status']) else: @@ -1066,7 +1077,8 @@ def readStoredSms(self, index, memory=None): pdu = msgData[1] smsDict = decodeSmsPdu(pdu) if smsDict['type'] == 'SMS-DELIVER': - return ReceivedSms(self, int(stat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc']) + concat = self._getConcat(smsDict) + return ReceivedSms(self, int(stat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], concat) elif smsDict['type'] == 'SMS-STATUS-REPORT': return StatusReport(self, int(stat), smsDict['reference'], smsDict['number'], smsDict['time'], smsDict['discharge'], smsDict['status']) else: