-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipc.py
309 lines (241 loc) · 7.81 KB
/
ipc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
ipc.py: make XML IPC to ondd via its control socket
Copyright 2014-2015, Outernet Inc.
Some rights reserved.
This software is free software licensed under the terms of GPLv3. See COPYING
file that comes with the source code, or http://www.gnu.org/licenses/gpl.txt.
"""
from __future__ import unicode_literals
import os
import socket
import logging
import xml.etree.ElementTree as ET
from contextlib import contextmanager
from bottle import request
from bottle_utils.html import yesno
OUT_ENCODING = 'utf8'
IN_ENCODING = 'utf8'
KU_BAND = 'k'
C_BAND = 'c'
UNIVERSAL = 'u'
C_OFF = 5150 # Frequency offset for C band
NA_KU_OFF = 10750 # Frequency offset for North America Ku
UN_LO_OFF = 9750 # Low band offset
UN_HI_OFF = 10600 # High band offset
UN_HI_SW = 11700 # Transponder frequency at which we switch to high band
ONDD_BAD_RESPONSE_CODE = '400'
ONDD_SOCKET_TIMEOUT = 20.0
def connect(path):
sock = socket.socket(socket.AF_UNIX)
sock.settimeout(ONDD_SOCKET_TIMEOUT)
sock.connect(path)
return sock
@contextmanager
def open_socket():
sock = connect(request.app.config['ondd.socket'])
try:
yield sock
finally:
# Permanently close this socket
sock.shutdown(socket.SHUT_RDWR)
sock.close()
def ping():
""" Check if ondd endpoint is active."""
try:
with open_socket():
return True
except (socket.error, socket.timeout):
return False
def read(sock, buffsize=2048):
""" Read the data from a socket until exhausted or NULL byte
:param sock: socket object
:param buffsize: size of the buffer in bytes (2048 by default)
"""
idata = data = sock.recv(buffsize)
while idata and b'\0' not in idata:
idata = sock.recv(buffsize)
data += idata
return data[:-1].decode(IN_ENCODING)
def parse(data):
""" Parse incoming XML into Etree object
:param data: XML string
:returns: root node object
"""
return ET.fromstring(data.encode('utf8'))
def send(payload):
""" Connect to socket configured in the config file
According to ONDD API, payload must be terminated by NULL byte. If the
supplied payload isn't terminated by NULL byte, one will automatically be
appended to the end.
:param payload: the XML payload to send down the pipe
:returns: response data
"""
if not payload[-1] == '\0':
payload = payload.encode(OUT_ENCODING) + '\0'
try:
with open_socket() as sock:
logging.debug('ONDD: sending payload: %s', payload)
sock.send(payload)
data = read(sock)
logging.debug('ONDD: received data: %s', data)
except (socket.error, socket.timeout):
return None
else:
return parse(data)
def xml_get_path(path):
""" Return XML for getting a path
:param path: path of the get request
"""
return '<get uri="%s" />' % path
def xml_put_path(path, subtree=''):
""" Return XML for putting a path
:param path: path
:param subtree: XML fragment for the PUT request
"""
return '<put uri="%s">%s</put>' % (path, subtree)
def kw2xml(**kwargs):
""" Convert any keyword parameters to XML
This function does not guarantee the order of the tags.
Example::
>>> kw2xml(foo='bar', bar='baz', baz=1)
'<foo>bar</foo><bar>baz</bar><baz>1</baz>'
"""
xml = ''
for k, v in kwargs.items():
xml += '<%(key)s>%(val)s</%(key)s>' % dict(key=k, val=v)
return xml
def v2pol(volts):
if volts == '13':
return 'v'
elif volts == '18':
return 'h'
return '0'
def get_status():
""" Get ONDD status """
payload = xml_get_path('/status')
root = send(payload)
if root is None:
return {
'has_lock': False,
'signal': 0,
'snr': 0.0,
'streams': []
}
tuner = root.find('tuner')
streams = root.find('streams')
return {
'has_lock': tuner.find('lock').text == 'yes',
'signal': int(tuner.find('signal').text),
'snr': float(tuner.find('snr').text),
'streams': [
{'id': s.find('ident').text,
'bitrate': int(s.find('bitrate').text)}
for s in streams]
}
def get_file_list():
""" Get ONDD file download list """
payload = xml_get_path('/signaling/')
try:
root = send(payload)
except ET.ParseError:
logging.error('ONDD: Could not parse XML data')
return []
if root is None:
return []
out = []
streams = root.find('streams')
for s in streams:
files = s.find('files')
for f in files:
out.append({
'path': f.find('path').text,
'size': int(f.find('size').text)
})
return out
def parse_transfer(transfer):
path = transfer.find('path').text or ''
block_count = int(transfer.find('block_count').text)
block_received = int(transfer.find('block_received').text)
complete = transfer.find('complete').text == 'yes'
if complete:
percentage = 100
else:
percentage = block_received * 100 / (block_count or 1)
return dict(path=path,
filename=os.path.basename(path),
hash=transfer.find('hash').text,
block_count=block_count,
block_received=block_received,
percentage=percentage,
complete=complete)
def get_transfers():
""" Get information about the file ONDD is currently processing """
payload = xml_get_path('/transfers')
try:
root = send(payload)
except ET.ParseError:
logging.error('ONDD: Could not parse XML data')
return []
if root is None:
return []
return [parse_transfer(transfer) for stream in root.find('streams')
for transfer in stream.find('transfers')]
def freq_conv(freq, lnb_type):
""" Converts transponder frequency to L-band frequency
The conversion formula requires the LNB type. The type can be either:
`KU_BAND` or `'k'` for North America Ku band LNB
`C_BAND` or `'c'` for C band LNB
`UNIVERSAL` or `'u'` for Universal LNB.
Example:
>>> freq_conv(11471, 'u')
1721
"""
if lnb_type == KU_BAND:
# NA Ku band LNB
return freq - NA_KU_OFF
if lnb_type == C_BAND:
# C band LNB
return abs(freq - C_OFF)
# Universal
if freq > UN_HI_SW:
return freq - UN_HI_OFF
return freq - UN_LO_OFF
def needs_tone(freq, lnb_type):
""" Whether LNB needs a 22KHz tone
Always returns ``True`` for C band and North America Ku band LNBs.
"""
if lnb_type in (KU_BAND, C_BAND):
return False
return freq > UN_HI_SW
def get_settings():
""" Get ONDD tuner settings """
payload = xml_get_path('/settings')
root = send(payload)
if root is None:
return {
'frequency': 0,
'delivery': '',
'modulation': '',
'polarization': '',
'tone': False,
'azimuth': 0
}
tuner = root.find('tuner')
return {
'frequency': int(tuner.find('frequency').text),
'delivery': tuner.find('delivery').text,
'modulation': tuner.find('modulation').text,
'polarization': v2pol(tuner.find('voltage').text),
'tone': tuner.find('tone').text == 'yes',
'azimuth': int(tuner.find('azimuth').text or 0),
}
def set_settings(frequency, symbolrate, delivery='dvb-s', modulation='qpsk',
tone=True, voltage=13, azimuth=0):
tone = yesno(tone)
payload = xml_put_path('/settings', kw2xml(**locals()))
resp = send(payload)
if resp is None:
return ONDD_BAD_RESPONSE_CODE
resp_code = resp.get('code')
logging.debug('ONDD: received response code %s', resp_code)
return resp_code