Skip to content

Commit 0d04314

Browse files
committed
reverted changes in callbacks, needs to be redone
1 parent 5f8b047 commit 0d04314

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

gsmmodem/modem.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ class GsmModem(SerialComms):
146146
CDSI_REGEX = re.compile('\+CDSI:\s*"([^"]+)",(\d+)$')
147147
CDS_REGEX = re.compile('\+CDS:\s*([0-9]+)"$')
148148

149-
def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsReceivedCallbackFunc=None, smsStatusReportCallbackFunc=None, requestDelivery=True, AT_CNMI="", *a, **kw):
149+
def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsReceivedCallbackFunc=None, smsStatusReportCallback=None, requestDelivery=True, AT_CNMI="", *a, **kw):
150150
super(GsmModem, self).__init__(port, baudrate, notifyCallbackFunc=self._handleModemNotification, *a, **kw)
151-
self.incomingCallCallbackFunc = incomingCallCallbackFunc
152-
self.smsReceivedCallbackFunc = smsReceivedCallbackFunc
153-
self.smsStatusReportCallbackFunc = smsStatusReportCallbackFunc
151+
self.incomingCallCallback = incomingCallCallbackFunc or self._placeholderCallback
152+
self.smsReceivedCallback = smsReceivedCallbackFunc or self._placeholderCallback
153+
self.smsStatusReportCallback = smsStatusReportCallback or self._placeholderCallback
154154
self.requestDelivery = requestDelivery
155155
self.AT_CNMI = AT_CNMI or "2,1,0,2"
156156
# Flag indicating whether caller ID for incoming call notification has been set up
@@ -377,7 +377,7 @@ def connect(self, pin=None, waitingForModemToStartInSeconds=0):
377377
del cpmsSupport
378378
del cpmsLine
379379

380-
if self._smsReadSupported and (self.smsReceivedCallbackFunc or self.smsStatusReportCallbackFunc):
380+
if self._smsReadSupported and (self.smsReceivedCallback or self.smsStatusReportCallback):
381381
try:
382382
self.write('AT+CNMI=' + self.AT_CNMI) # Set message notifications
383383
except CommandError:
@@ -1067,16 +1067,16 @@ def processStoredSms(self, unreadOnly=False):
10671067
:param unreadOnly: If True, only process unread SMS messages
10681068
:type unreadOnly: boolean
10691069
"""
1070-
if self.smsReceivedCallbackFunc:
1070+
if self.smsReceivedCallback:
10711071
states = [Sms.STATUS_RECEIVED_UNREAD]
10721072
if not unreadOnly:
10731073
states.insert(0, Sms.STATUS_RECEIVED_READ)
10741074
for msgStatus in states:
10751075
messages = self.listStoredSms(status=msgStatus, delete=True)
10761076
for sms in messages:
1077-
self.smsReceivedCallbackFunc(sms)
1077+
self.smsReceivedCallback(sms)
10781078
else:
1079-
raise ValueError('GsmModem.smsReceivedCallbackFunc not set')
1079+
raise ValueError('GsmModem.smsReceivedCallback not set')
10801080

10811081
def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False):
10821082
""" Returns SMS messages currently stored on the device/SIM card.
@@ -1283,9 +1283,7 @@ def _handleIncomingCall(self, lines):
12831283
callId = len(self.activeCalls) + 1;
12841284
call = IncomingCall(self, callerNumber, ton, callerName, callId, callType)
12851285
self.activeCalls[callId] = call
1286-
1287-
if self.incomingCallCallbackFunc:
1288-
self.incomingCallCallbackFunc(call)
1286+
self.incomingCallCallback(call)
12891287

12901288
def _handleCallInitiated(self, regexMatch, callId=None, callType=1):
12911289
""" Handler for "outgoing call initiated" event notification line """
@@ -1346,16 +1344,16 @@ def _handleCallRejected(self, regexMatch, callId=None):
13461344
def _handleSmsReceived(self, notificationLine):
13471345
""" Handler for "new SMS" unsolicited notification line """
13481346
self.log.debug('SMS message received')
1349-
if self.smsReceivedCallbackFunc is not None:
1347+
if self.smsReceivedCallback is not None:
13501348
cmtiMatch = self.CMTI_REGEX.match(notificationLine)
13511349
if cmtiMatch:
13521350
msgMemory = cmtiMatch.group(1)
13531351
msgIndex = cmtiMatch.group(2)
13541352
sms = self.readStoredSms(msgIndex, msgMemory)
13551353
try:
1356-
self.smsReceivedCallbackFunc(sms)
1354+
self.smsReceivedCallback(sms)
13571355
except Exception:
1358-
self.log.error('error in smsReceivedCallbackFunc', exc_info=True)
1356+
self.log.error('error in smsReceivedCallback', exc_info=True)
13591357
else:
13601358
self.deleteStoredSms(msgIndex)
13611359

@@ -1374,12 +1372,12 @@ def _handleSmsStatusReport(self, notificationLine):
13741372
if self._smsStatusReportEvent:
13751373
# A sendSms() call is waiting for this response - notify waiting thread
13761374
self._smsStatusReportEvent.set()
1377-
elif self.smsStatusReportCallbackFunc:
1375+
elif self.smsStatusReportCallback:
13781376
# Nothing is waiting for this report directly - use callback
13791377
try:
1380-
self.smsStatusReportCallbackFunc(report)
1378+
self.smsStatusReportCallback(report)
13811379
except Exception:
1382-
self.log.error('error in smsStatusReportCallbackFunc', exc_info=True)
1380+
self.log.error('error in smsStatusReportCallback', exc_info=True)
13831381

13841382
def _handleSmsStatusReportTe(self, length, notificationLine):
13851383
""" Handler for TE SMS status reports """
@@ -1402,10 +1400,9 @@ def _handleSmsStatusReportTe(self, length, notificationLine):
14021400
else:
14031401
# Nothing is waiting for this report directly - use callback
14041402
try:
1405-
self.smsStatusReportCallbackFunc(report)
1403+
self.smsStatusReportCallback(report)
14061404
except Exception:
1407-
self.log.error('error in smsStatusReportCallbackFunc', exc_info=True)
1408-
1405+
self.log.error('error in smsStatusReportCallback', exc_info=True)
14091406

14101407
def readStoredSms(self, index, memory=None):
14111408
""" Reads and returns the SMS message at the specified index
@@ -1543,6 +1540,10 @@ def _parseCusdResponse(self, lines):
15431540
message = cusdMatches[0].group(2)
15441541
return Ussd(self, sessionActive, message)
15451542

1543+
def _placeHolderCallback(self, *args):
1544+
""" Does nothing """
1545+
self.log.debug('called with args: {0}'.format(args))
1546+
15461547
def _pollCallStatus(self, expectedState, callId=None, timeout=None):
15471548
""" Poll the status of outgoing calls.
15481549
This is used for modems that do not have a known set of call status update notifications.

gsmmodem/serial_comms.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def __init__(self, port, baudrate=115200, notifyCallbackFunc=None, fatalErrorCal
3939
# Reentrant lock for managing concurrent write access to the underlying serial port
4040
self._txLock = threading.RLock()
4141

42-
self.notifyCallbackFunc = notifyCallbackFunc
43-
self.fatalErrorCallbackFunc = fatalErrorCallbackFunc
42+
self.notifyCallback = notifyCallbackFunc or self._placeholderCallback
43+
self.fatalErrorCallback = fatalErrorCallbackFunc or self._placeholderCallback
4444

4545
self.com_args = args
4646
self.com_kwargs = kwargs
@@ -78,10 +78,12 @@ def _handleLineRead(self, line, checkForResponseTerm=True):
7878
# No more chars on the way for this notification - notify higher-level callback
7979
#print 'notification:', self._notification
8080
self.log.debug('notification: %s', self._notification)
81-
if self.notifyCallbackFunc:
82-
self.notifyCallbackFunc(self._notification)
81+
self.notifyCallback(self._notification)
8382
self._notification = []
8483

84+
def _placeholderCallback(self, *args, **kwargs):
85+
""" Placeholder callback function (does nothing) """
86+
8587
def _readLoop(self):
8688
""" Read thread main loop
8789
@@ -117,8 +119,7 @@ def _readLoop(self):
117119
except Exception: #pragma: no cover
118120
pass
119121
# Notify the fatal error handler
120-
if self.fatalErrorCallbackFunc:
121-
self.fatalErrorCallbackFunc(e)
122+
self.fatalErrorCallback(e)
122123

123124
def write(self, data, waitForResponse=True, timeout=5, expectedResponseTermSeq=None):
124125
data = data.encode()

0 commit comments

Comments
 (0)