def _parsePDU(self, header, pdu): try: if not header.startswith("+CMGL: "): return None, None index, stat, alpha, length = header[7:].split(',') # parse PDU smsc_len = int(pdu[0:2], 16) # smsc_number_type = int(pdu[2:4],16) # if smsc_number_type != 0x81 and smsc_number_type != 0x91: return (-2, hex(smsc_number_type)) offset = smsc_len * 2 + 3 sender, offset = self._parse_sender(pdu, offset) if pdu[offset:offset + 4] != '0000': return None, None offset += 4 timestamp, offset = self._parse_timestamp(pdu, offset) message, offset = self._parse_message(pdu, offset) return SMS(sender, timestamp, message), index except ValueError as e: self.logger.error(repr(e)) return None, None
def _parse_encoded_sms_response(self, encoded_response): msg = '+CMT' serial_response = self._filter_return_values_from_at_response( msg, encoded_response) response_list = serial_response.split(',') # Handle sender index = response_list[0].find(':') sender = response_list[0][index:].strip(': \"').strip('\"') message_timestamp_blob = response_list[-1].split('\r\n') # Handle timestamp timestamp_list = message_timestamp_blob[0].split(':') tzquarter = timestamp_list[2][2:][:-1] timestamp_list[2] = timestamp_list[2][:2] # Handle message message = message_timestamp_blob[-1] # Handle date date_list = response_list[-2].split('/') date_list[0] = date_list[0].strip('\"') timestamp_obj = Timestamp(year=date_list[0], month=date_list[1], day=date_list[2], hour=timestamp_list[0], minute=timestamp_list[1], second=timestamp_list[2], tzquarter=tzquarter) sms_obj = SMS(sender=sender, timestamp=timestamp_obj, message=message) return sms_obj
def _parsePDU(self, header, pdu): try: if not header.startswith("+CMGL: "): return None, None index, stat, alpha, length = header[7:].split(',') #parse PDU smsc_len = int(pdu[0:2], 16) # smsc_number_type = int(pdu[2:4],16) # if smsc_number_type != 0x81 and smsc_number_type != 0x91: return (-2, hex(smsc_number_type)) offset = smsc_len * 2 + 3 sms_deliver = int(pdu[offset], 16) if sms_deliver & 0x03 != 0: return None offset += 1 sender_len = int(pdu[offset:offset + 2], 16) offset += 2 sender_number_type = int(pdu[offset:offset + 2], 16) offset += 2 sender_read = sender_len if sender_read & 1 != 0: sender_read += 1 sender_raw = pdu[offset:offset + sender_read] if sender_number_type & 0x50 == 0x50: #GSM-7 sender = self._convert7to8bit(sender_raw, sender_len * 4 / 7) else: sender = ''.join([ sender_raw[x:x + 2][::-1] for x in range(0, len(sender_raw), 2) ]) if sender_read & 1 != 0: sender = sender[:-1] offset += sender_read if pdu[offset:offset + 4] != '0000': return -4 offset += 4 ts_raw = pdu[offset:offset + 14] ts = ''.join( [ts_raw[x:x + 2][::-1] for x in range(0, len(ts_raw), 2)]) dt = datetime.datetime.strptime(ts[:-2], '%y%m%d%H%M%S') tz_byte = int(ts[-2:], 16) tz_bcd = ((tz_byte & 0x70) >> 4) * 10 + (tz_byte & 0x0F) delta = datetime.timedelta(minutes=15 * tz_bcd) #adjust to UTC from Service Center timestamp if (tz_byte & 0x80) == 0x80: dt += delta else: dt -= delta offset += 14 msg_len = int(pdu[offset:offset + 2], 16) offset += 2 message = self._convert7to8bit(pdu[offset:], msg_len) return SMS(sender, dt, message), index except ValueError as e: self.logger.error(repr(e)) return None, None