Skip to content

Commit 3c94582

Browse files
committed
Hologram Python SDK v0.5.27 release
1 parent 4c864bb commit 3c94582

File tree

7 files changed

+68
-45
lines changed

7 files changed

+68
-45
lines changed

Hologram/Cloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from Network import NetworkManager
1515
from Authentication import *
1616

17-
__version__ = '0.5.26'
17+
__version__ = '0.5.27'
1818

1919
class Cloud(object):
2020

Hologram/HologramCloud.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def sendMessage(self, message, topics = None, timeout = 5):
104104
nonce=nonce)
105105
modem_response = self.network.get_sim_otp_response(command)
106106
self.authentication.generate_sim_otp_token(modem_response)
107+
elif self.authenticationType == 'totp':
108+
self.authentication.credentials['device_id'] = self.network.iccid
109+
self.authentication.credentials['private_key'] = self.network.imsi
107110

108111
output = self.authentication.buildPayloadString(message,
109112
topics=topics,

Hologram/Network/Modem/ModemMode/PPP.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
#
1010
import subprocess
1111
import sys
12+
import time
1213
from pppd import PPPConnection
1314
from IPPP import IPPP
1415
from Exceptions.HologramError import PPPError
1516

1617
DEFAULT_PPP_TIMEOUT = 200
18+
DEFAULT_PPP_INTERFACE = 'ppp0'
19+
MAX_PPP_INTERFACE_UP_RETRIES = 10
20+
MAX_REROUTE_PACKET_RETRIES = 15
1721

1822
class PPP(IPPP):
1923

@@ -49,14 +53,38 @@ def connect(self, timeout=DEFAULT_PPP_TIMEOUT):
4953

5054
result = self._ppp.connect(timeout=timeout)
5155

52-
if result == True:
56+
if result == True and self.__is_ppp_interface_up():
5357
self.__reroute_packets()
54-
return result
58+
return True
59+
else:
60+
return False
5561

5662
def disconnect(self):
5763
self.__shut_down_existing_ppp_session()
5864
return self._ppp.disconnect()
5965

66+
# EFFECTS: Blocks to make sure ppp interface is up.
67+
def __is_ppp_interface_up(self):
68+
count = 0
69+
while count <= MAX_PPP_INTERFACE_UP_RETRIES:
70+
try:
71+
out_list = subprocess.check_output(['ip', 'address', 'show', 'dev',
72+
DEFAULT_PPP_INTERFACE],
73+
stderr=subprocess.STDOUT)
74+
75+
# Check if ready to break out of loop when ppp0 is found.
76+
if 'does not exist' in out_list:
77+
time.sleep(1)
78+
count += 1
79+
else:
80+
break
81+
except subprocess.CalledProcessError as e:
82+
pass
83+
84+
if count <= MAX_PPP_INTERFACE_UP_RETRIES:
85+
return True
86+
return False
87+
6088
# EFFECTS: Makes sure that there are no existing PPP instances on the same
6189
# device interface.
6290
def __enforce_no_existing_ppp_session(self):
@@ -116,10 +144,31 @@ def __split_PID_from_process(self, process):
116144
return None
117145

118146
def __reroute_packets(self):
119-
self.logger.info('Rerouting packets to ppp0 interface')
120-
subprocess.call('ip route add 10.176.0.0/16 dev ppp0', shell=True)
121-
subprocess.call('ip route add 10.254.0.0/16 dev ppp0', shell=True)
122-
subprocess.call('ip route add default dev ppp0', shell=True)
147+
self.logger.info('Rerouting packets to %s interface', DEFAULT_PPP_INTERFACE)
148+
149+
count = 0
150+
# Make sure that we still have ppp interface before adding the routes.
151+
while count <= MAX_REROUTE_PACKET_RETRIES:
152+
try:
153+
out_list = subprocess.check_output(['ip', 'route', 'add',
154+
'10.176.0.0/16', 'dev',
155+
DEFAULT_PPP_INTERFACE],
156+
stderr=subprocess.STDOUT)
157+
158+
# Check if ready to break out of loop when ppp0 is found.
159+
if 'Network is down' in out_list:
160+
time.sleep(1)
161+
count += 1
162+
else:
163+
break
164+
except Exception as e:
165+
pass
166+
167+
if count > MAX_REROUTE_PACKET_RETRIES:
168+
return
169+
170+
subprocess.call('ip route add 10.254.0.0/16 dev %s' % DEFAULT_PPP_INTERFACE, shell=True)
171+
subprocess.call('ip route add default dev %s' % DEFAULT_PPP_INTERFACE, shell=True)
123172

124173
@property
125174
def localIPAddress(self):

examples/example-hologram-cloud-totp.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,12 @@
2121
print ''
2222
print 'Testing Hologram Cloud class...'
2323
print ''
24-
print '* Note: You can obtain device keys from the Devices page'
25-
print '* at https://dashboard.hologram.io'
26-
print ''
27-
28-
device_id = raw_input('What is your device id? ')
29-
private_key = raw_input('What is your private key? ')
30-
31-
credentials = {'device_id': device_id, 'private_key': private_key}
32-
33-
hologram = HologramCloud(credentials, enable_inbound=False, authentication_type='totp')
3424

35-
print 'Hologram SDK version:'
36-
print hologram.version
25+
hologram = HologramCloud(dict(), enable_inbound=False, network='cellular', authentication_type='totp')
3726

38-
print ''
39-
print 'Cloud type: ' + str(hologram)
40-
print ''
41-
print 'Network type: ' + hologram.network_type
42-
print ''
27+
result = hologram.network.connect()
28+
if result == False:
29+
print 'Failed to connect to cell network'
4330

4431
recv = hologram.sendMessage('YESYESYES!',
4532
topics = ['YES'],
@@ -51,3 +38,5 @@
5138
print ''
5239
print 'Testing complete.'
5340
print ''
41+
42+
hologram.network.disconnect()

scripts/hologram_send.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ def parse_hologram_send_args(parser):
3333
parser.add_argument('-v', '--verbose', action='store_true', required=False)
3434
parser.add_argument('--host', required=False, help=argparse.SUPPRESS)
3535
parser.add_argument('-p', '--port', required=False, help=argparse.SUPPRESS)
36-
parser.add_argument('--iccid', nargs='?', help=argparse.SUPPRESS)
37-
parser.add_argument('--imsi', nargs='?', help=argparse.SUPPRESS)
3836
parser.add_argument('--authtype', default='totp', nargs='?',
3937
help='The authentication type used if HologramCloud is in use')
4038

@@ -72,26 +70,10 @@ def parse_sms_args(parser):
7270
# EFFECTS: Parses and sends the Hologram message using TOTP Authentication
7371
def sendTOTP(args, data, is_sms=False):
7472

75-
if not args['iccid'] and ('device_id' in data):
76-
args['iccid'] = data['device_id']
77-
78-
if not args['imsi'] and ('private_key' in data):
79-
args['imsi'] = data['private_key']
80-
81-
credentials = {'device_id': args['iccid'], 'private_key': args['imsi']}
82-
hologram = HologramCloud(credentials, enable_inbound=False,
73+
hologram = HologramCloud(dict(), enable_inbound=False,
8374
authentication_type='totp',
8475
network='cellular')
8576

86-
modem = ''
87-
# Load the ICCID and IMSI values if modem is physically attached to machine
88-
if hologram.network.isModemAttached():
89-
hologram.credentials = {'device_id': hologram.network.iccid,
90-
'private_key': hologram.network.imsi}
91-
92-
if (hologram.credentials['device_id'] is None) or (hologram.credentials['private_key'] is None):
93-
raise HologramError('Device id or private key not specified or cannot be pulled from modem. Please specify them or rerun the program with a provided device key')
94-
9577
result = hologram.network.connect()
9678
if result == False:
9779
raise HologramError('Failed to connect to cell network')

tests/MessageMode/test_Cloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ def test_invalid_send_sms(self):
4040
def test_sdk_version(self):
4141
cloud = Cloud(None, send_host = '127.0.0.1', send_port = 9999)
4242

43-
assert cloud.version == '0.5.26'
43+
assert cloud.version == '0.5.27'

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.26
1+
0.5.27

0 commit comments

Comments
 (0)