From f7ee8f01ffd7fe04c32cd94775054d242174f59d Mon Sep 17 00:00:00 2001
From: Paul Fertser <fercerpav@gmail.com>
Date: Mon, 2 Feb 2009 17:10:31 +0300
Subject: [PATCH] Preliminary support for SMS message delivery reports

I couldn't find enough reliable information for that type of PDU, therefore
support is incomplete for now.
---
 framework/subsystems/ogsmd/device.py               |    5 ++++
 framework/subsystems/ogsmd/gsm/sms.py              |   21 +++++++++++++++----
 .../ogsmd/modems/abstract/unsolicited.py           |    8 +++++++
 3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/framework/subsystems/ogsmd/device.py b/framework/subsystems/ogsmd/device.py
index 914fd7d..3a0e898 100644
--- a/framework/subsystems/ogsmd/device.py
+++ b/framework/subsystems/ogsmd/device.py
@@ -374,6 +374,11 @@ class Device( resource.Resource ):
     def IncomingMessage( self, address, text, features ):
         logger.info( "incoming message (unbuffered) from %s", address )
 
+    @resource.queuedsignal
+    @dbus.service.signal( DBUS_INTERFACE_SMS, "ss" )
+    def IncomingMessageReceipt( self, number, text ):
+        logger.info( "incoming message delivery report from %s", number )
+
     #
     # dbus org.freesmartphone.GSM.Network
     #
diff --git a/framework/subsystems/ogsmd/gsm/sms.py b/framework/subsystems/ogsmd/gsm/sms.py
index 43053b9..58a96a9 100644
--- a/framework/subsystems/ogsmd/gsm/sms.py
+++ b/framework/subsystems/ogsmd/gsm/sms.py
@@ -100,7 +100,7 @@ class SMS(object):
         sms = cls( smstype )
         offset = 0
 
-        if sms.type == "sms-deliver" or sms.type == "sms-submit":
+        if sms.type == "sms-deliver" or sms.type == "sms-submit" or sms.type == "sms-status-report":
             # SCA - Service Center address
             sca_len = bytes[offset]
             offset += 1
@@ -124,22 +124,31 @@ class SMS(object):
             sms.pdu_mms = sms.pdu_rd
         elif sms.type == "sms-submit-report":
             sms.pdu_udhi = pdu_type & 0x04 != 0
+        elif sms.type == "sms-status-report":
+            sms.pdu_udhi = pdu_type & 0x40 != 0
+            sms.pdu_srr = pdu_type & 0x20 != 0
+            sms.pdu_mms = pdu_type & 0x04 != 0
 
         offset += 1
-        if sms.type == "sms-submit":
+        if sms.type == "sms-submit" or sms.type == "sms-status-report":
             # MR - Message Reference
             sms.mr = bytes[offset]
             offset += 1
 
         # OA/DA - Originating or Destination Address
         # WARNING, the length is coded in digits of the number, not in octets occupied!
-        if sms.type == "sms-submit" or sms.type == "sms-deliver":
+        if sms.type == "sms-submit" or sms.type == "sms-deliver" or sms.type == "sms-status-report":
             oa_len = 1 + (bytes[offset] + 1) / 2
             offset += 1
             sms.oa = PDUAddress.decode( bytes[offset:offset+oa_len] )
             sms.da = sms.oa
 
             offset += oa_len
+
+            if sms.type == "sms-status-report":
+                # Skip SCTS, DT (discharge time), ST, PI FIXME
+                offset += 4
+
             # PID - Protocol identifier
             sms.pid = bytes[offset]
 
@@ -185,7 +194,9 @@ class SMS(object):
 
                 offset += 7
 
-        if sms.type == "sms-submit-report" and not sms.pdu_udli:
+        if ( sms.type == "sms-submit-report" and not sms.pdu_udli ) \
+             or sms.type == "sms-status-report":
+            # FIXME can sms-status-report have userdata?
             return sms
 
         # UD - User Data
@@ -198,7 +209,7 @@ class SMS(object):
         self.type = type
         self.sca = False
         self.pdu_udhi = False
-        self.pdu_srr = False
+        self.pdu_srr = True # Request delivery report
         self.pdu_sri = False
         self.pdu_rp = False
         self.pdu_vpf = 0
diff --git a/framework/subsystems/ogsmd/modems/abstract/unsolicited.py b/framework/subsystems/ogsmd/modems/abstract/unsolicited.py
index 2bd8f6d..715e6f3 100644
--- a/framework/subsystems/ogsmd/modems/abstract/unsolicited.py
+++ b/framework/subsystems/ogsmd/modems/abstract/unsolicited.py
@@ -148,6 +148,14 @@ class AbstractUnsolicitedResponseDelegate( object ):
         sms = ogsmd.gsm.sms.SMS.decode( pdu, "sms-deliver" )
         self._object.IncomingMessage( str(sms.oa), sms.ud, sms.featureMap )
 
+    # +CDS: <PDU size>\r\n<PDU>
+    def plusCDS( self, righthandside, pdu ):
+        """
+        Incoming delivery report
+        """
+	sms = ogsmd.gsm.sms.SMS.decode( pdu, "sms-status-report" )
+        self._object.IncomingMessageReceipt( str(sms.oa), "delivered" )
+
     # +CMTI: "SM",7
     def plusCMTI( self, righthandside ):
         """
-- 
1.5.6.5


