Modify README and add comments for some cryptic methods
parent
550a2b4d3d
commit
be53921802
15
README.md
15
README.md
|
@ -1,6 +1,6 @@
|
||||||
# Biju
|
# 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).
|
This is project is based on this [code snippet](http://dzone.com/snippets/send-and-receive-sms-text).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
@ -20,19 +20,22 @@ Or install it yourself as:
|
||||||
## Usage
|
## 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
|
# 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
|
puts sms
|
||||||
end
|
end
|
||||||
|
|
||||||
# method to send sms
|
# method to send sms
|
||||||
sms = Biju::Sms.new(:phone_number => '+3312345678', :message => 'hello world')
|
sms = Biju::Sms.new(phone_number: '+3312345678', message: 'hello world')
|
||||||
@modem.send(sms)
|
modem.send(sms)
|
||||||
|
|
||||||
@modem.close
|
modem.close
|
||||||
```
|
```
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
1. Write missing test for modem module.
|
1. Write missing test for modem module.
|
||||||
|
|
|
@ -26,6 +26,10 @@ module Biju
|
||||||
extended_error
|
extended_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
modem.close
|
||||||
|
end
|
||||||
|
|
||||||
def at_command(cmd = nil, *args, &block)
|
def at_command(cmd = nil, *args, &block)
|
||||||
command = ['AT', cmd].compact.join
|
command = ['AT', cmd].compact.join
|
||||||
command_args = args.compact.to_hayes
|
command_args = args.compact.to_hayes
|
||||||
|
|
|
@ -35,14 +35,22 @@ module Biju
|
||||||
|
|
||||||
string.scan(/../).map(&:hex).each_with_index do |octet, i|
|
string.scan(/../).map(&:hex).each_with_index do |octet, i|
|
||||||
index = i % 7
|
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
|
current = ((octet & (2 ** (7 - index) - 1)) << index) | next_char
|
||||||
|
|
||||||
res = add_char(res, current)
|
res = add_char(res, current)
|
||||||
current_length += 1
|
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
|
break if length > 0 && current_length >= length
|
||||||
|
|
||||||
|
# Get the relevant bits for the next character
|
||||||
next_char = octet >> (7 - index)
|
next_char = octet >> (7 - index)
|
||||||
|
# When index is 6, next_char contains a full septet
|
||||||
if index == 6
|
if index == 6
|
||||||
res = add_char(res, next_char)
|
res = add_char(res, next_char)
|
||||||
current_length += 1
|
current_length += 1
|
||||||
|
@ -58,6 +66,9 @@ module Biju
|
||||||
length = 0
|
length = 0
|
||||||
|
|
||||||
string.chars.each do |char|
|
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)
|
if get_septet(char)
|
||||||
res << get_septet(char).reverse
|
res << get_septet(char).reverse
|
||||||
length += 1
|
length += 1
|
||||||
|
@ -67,9 +78,11 @@ module Biju
|
||||||
length += 2
|
length += 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
# Add necessary bits to get a full octet
|
||||||
res << ('0' * (8 - (res.length % 8))) unless res.length % 8 == 0
|
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,
|
res.scan(/.{8}/).map { |octet| '%02x' % octet.reverse.to_i(2) }.join,
|
||||||
length: length,
|
length: length,
|
||||||
]
|
]
|
||||||
|
|
|
@ -21,6 +21,7 @@ module Biju
|
||||||
end
|
end
|
||||||
|
|
||||||
def length
|
def length
|
||||||
|
# If the last character is 0xF, remove this one from length
|
||||||
number.length - (number[-2, 2].hex >> 4 == 15 ? 1 : 0)
|
number.length - (number[-2, 2].hex >> 4 == 15 ? 1 : 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require 'date'
|
||||||
|
|
||||||
module Biju
|
module Biju
|
||||||
module PDU
|
module PDU
|
||||||
class Timestamp
|
class Timestamp
|
||||||
|
@ -8,12 +10,18 @@ module Biju
|
||||||
end
|
end
|
||||||
|
|
||||||
def timezone
|
def timezone
|
||||||
|
# The last 2 digits of the timestamp are for the timezone
|
||||||
timezone = timestamp[-2, 2].reverse.hex
|
timezone = timestamp[-2, 2].reverse.hex
|
||||||
|
|
||||||
|
# The MSB define the plus-minus sign. 0 for +, 1 for -
|
||||||
sign = (timezone >> 7 == 0 ? '+' : '-')
|
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)
|
tens_digit = ((timezone & 0b01110000) >> 4)
|
||||||
units_digit = (timezone & 0b00001111)
|
units_digit = (timezone & 0b00001111)
|
||||||
|
|
||||||
|
# Timezone is in quarters of an hour
|
||||||
sign << '%02d' % ((tens_digit * 10 + units_digit) / 4)
|
sign << '%02d' % ((tens_digit * 10 + units_digit) / 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue