From be53921802a821b23fa6d78c92282c232ae788d1 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Thu, 12 Sep 2013 10:35:13 +0200 Subject: [PATCH] Modify README and add comments for some cryptic methods --- README.md | 15 +++++++++------ lib/biju/hayes.rb | 4 ++++ lib/biju/pdu/encoding/gsm7bit.rb | 13 +++++++++++++ lib/biju/pdu/phone_number.rb | 1 + lib/biju/pdu/timestamp.rb | 10 +++++++++- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 488d628..4474b6a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Biju -[WIP] Biju is an easy way to mount a GSM modem to send, to receive and to delete messages through a ruby interface. +Biju is an easy way to mount a GSM modem to send, to receive and to delete messages through a ruby interface. This is project is based on this [code snippet](http://dzone.com/snippets/send-and-receive-sms-text). ## Installation @@ -20,19 +20,22 @@ Or install it yourself as: ## Usage ``` -@modem = Biju::Modem.new(:port => "/dev/tty.HUAWEIMobile-Modem") +modem = Biju::Hayes.new('/dev/tty.HUAWEIMobile-Modem', pin: '0000') # method to list all messages -@modem.messages.each do |sms| +# it can take the status in argument +# :unread, :read, :unsent, :sent, :all +modem.messages.each do |sms| puts sms end # method to send sms -sms = Biju::Sms.new(:phone_number => '+3312345678', :message => 'hello world') -@modem.send(sms) +sms = Biju::Sms.new(phone_number: '+3312345678', message: 'hello world') +modem.send(sms) -@modem.close +modem.close ``` + ## TODO 1. Write missing test for modem module. diff --git a/lib/biju/hayes.rb b/lib/biju/hayes.rb index 3b52111..2083a53 100644 --- a/lib/biju/hayes.rb +++ b/lib/biju/hayes.rb @@ -26,6 +26,10 @@ module Biju extended_error end + def close + modem.close + end + def at_command(cmd = nil, *args, &block) command = ['AT', cmd].compact.join command_args = args.compact.to_hayes diff --git a/lib/biju/pdu/encoding/gsm7bit.rb b/lib/biju/pdu/encoding/gsm7bit.rb index 854f950..758b063 100644 --- a/lib/biju/pdu/encoding/gsm7bit.rb +++ b/lib/biju/pdu/encoding/gsm7bit.rb @@ -35,14 +35,22 @@ module Biju string.scan(/../).map(&:hex).each_with_index do |octet, i| index = i % 7 + # Only keep the bits for the current character and + # add relevant bits from the previous octet + # to get the full septet and decode the current character current = ((octet & (2 ** (7 - index) - 1)) << index) | next_char res = add_char(res, current) current_length += 1 + # Break when the number of septet is reached + # to prevent to add a last @ when there is 7 septets. + # The last octet will have one more septet to ignore. break if length > 0 && current_length >= length + # Get the relevant bits for the next character next_char = octet >> (7 - index) + # When index is 6, next_char contains a full septet if index == 6 res = add_char(res, next_char) current_length += 1 @@ -58,6 +66,9 @@ module Biju length = 0 string.chars.each do |char| + # Look for the current character in basic character set and + # extension and concatenate the reversed septets to get + # full octets if get_septet(char) res << get_septet(char).reverse length += 1 @@ -67,9 +78,11 @@ module Biju length += 2 end end + # Add necessary bits to get a full octet res << ('0' * (8 - (res.length % 8))) unless res.length % 8 == 0 [ + # Group by octet, reverse them and print them in hex res.scan(/.{8}/).map { |octet| '%02x' % octet.reverse.to_i(2) }.join, length: length, ] diff --git a/lib/biju/pdu/phone_number.rb b/lib/biju/pdu/phone_number.rb index a52a67c..f8f5254 100644 --- a/lib/biju/pdu/phone_number.rb +++ b/lib/biju/pdu/phone_number.rb @@ -21,6 +21,7 @@ module Biju end def length + # If the last character is 0xF, remove this one from length number.length - (number[-2, 2].hex >> 4 == 15 ? 1 : 0) end end diff --git a/lib/biju/pdu/timestamp.rb b/lib/biju/pdu/timestamp.rb index 19b5b20..3c49256 100644 --- a/lib/biju/pdu/timestamp.rb +++ b/lib/biju/pdu/timestamp.rb @@ -1,3 +1,5 @@ +require 'date' + module Biju module PDU class Timestamp @@ -8,13 +10,19 @@ module Biju end def timezone + # The last 2 digits of the timestamp are for the timezone timezone = timestamp[-2, 2].reverse.hex + # The MSB define the plus-minus sign. 0 for +, 1 for - sign = (timezone >> 7 == 0 ? '+' : '-') + + # The following 3 bits represent tens digit + # and the last 4 bits are for the units digit tens_digit = ((timezone & 0b01110000) >> 4) units_digit = (timezone & 0b00001111) - sign << '%02d' % ((tens_digit * 10 + units_digit) / 4) + # Timezone is in quarters of an hour + sign << '%02d' % ((tens_digit * 10 + units_digit) / 4) end def to_datetime