/
iot2000setup.py
956 lines (810 loc) · 28.3 KB
/
iot2000setup.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
#!/usr/bin/env python3
from snack import *
import time
from subprocess import call
from subprocess import Popen, PIPE
from subprocess import check_output
import subprocess
import os
import os.path
import stat
import mraa
import sys
import time
from shutil import copyfile
sys.settrace
class ansicolors:
clear = '\033[2J'
blue = '\033[1;34m'
reset = '\033[0m'
interfacesConfig = """# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
# The loopback interface
auto lo
iface lo inet loopback
"""
dhcpTemplate = """auto [interfaceName]
iface [interfaceName] inet dhcp
"""
staticTemplate = """auto [interfaceName]
iface [interfaceName] inet static
address [ip]
netmask 255.255.255.0
"""
wirelessDhcpTemplate = """allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant.conf
"""
wirelessStaticTemplate = """allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
address [ip]
netmask 255.255.255.0
wpa-conf /etc/wpa_supplicant.conf
"""
wpaFileTemplate = '''ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
update_config=1
network={
key_mgmt=[type]
ssid="[ssid]"
psk="[passwd]"
}'''
repoFileTemplate = '''src/gz all http://[host]/ipk/all
src/gz i586-nlp-32 http://[host]/ipk/i586-nlp-32
src/gz i586-nlp-32-intel-common http://[host]/ipk/i586-nlp-32-intel-common
src/gz iot2000 http://[host]/ipk/iot2000
'''
# Ofono Context Header
apnHeaderStr = " AccessPointName = "
userHeaderStr = " Username = "
pwdHeaderStr = " Password = "
class TopMenu:
def __init__(self):
# Use dmidecode to determine device type (IOT2040/IOT2000)
task = subprocess.Popen("/usr/sbin/dmidecode -t 11 | awk 'NR==8' | cut -f 2 -d :", stdout=subprocess.PIPE, shell=True)
device = task.stdout.read().decode().lstrip().rstrip()
self.deviceIsIot2020 = False
if (device == "IOT2020"):
self.deviceIsIot2020 = True
def show(self):
self.gscreen = SnackScreen()
menuItems = [
("OS Settings", OsSettings(self)),
("Networking", Networking(self)),
("Software", SoftwareSettings(self))]
menuItems.append(("Peripherals", Peripherals(self)))
title = "IOT2000 Setup"
action, selection = ListboxChoiceWindow(
self.gscreen,
title, "",
menuItems,
[('Quit', 'quit', 'ESC')])
if(action == 'quit'):
self.gscreen.finish()
exit()
selection.show()
class OsSettings:
def __init__(self, topmenu):
self.currentHostname = subprocess.check_output("hostname").decode()
self.topmenu = topmenu
self.finish = False
def show(self):
while(True and not self.finish):
action, selection = ListboxChoiceWindow(
self.topmenu.gscreen,
"OS Settings", "",
[("Change Hostname", self.ChangeHostname),
("Change Password", self.ChangePassword)],
[('Back', 'back', 'ESC')])
if(action == 'back'):
return
selection()
def ChangeHostname(self):
ret = EntryWindow(
self.topmenu.gscreen,
"Change Host Name",
"",
[("Host Name:", self.currentHostname)],
1,
70, 50,
[('OK'), ('Cancel', 'cancel', 'ESC')],
None)
if (ret[0] == "ok"):
subprocess.Popen(["hostname", ret[1][0].rstrip()], stdout=open(os.devnull, 'wb'))
with open("/etc/hostname", "w") as textfile:
textfile.write(ret[1][0].rstrip())
self.finish = True
def ChangePassword(self):
self.topmenu.gscreen.finish()
print(ansicolors.clear) # Clear console
subprocess.call("passwd")
self.finish = True
def readFileLines(fileName, startLine):
fileReading = open(fileName, 'r')
return "".join(fileReading.readlines()[startLine:])
class Networking:
def __init__(self, topmenu):
self.topmenu = topmenu
self.finish = False
self.eth0HWPresent = True
self.eth1HWPresent = not topmenu.deviceIsIot2020
self.wifiHWPresent = os.path.isdir("/sys/class/net/wlan0")
self.networkInterfaces = {'eth0': '', 'eth1': '', 'wlan0': ''}
self.cellularInterface = []
self.cellularSimInfo = {}
def show(self):
self.reconfigWifi = False
self.interfaceFileChange = False
self.newSettingsFile = interfacesConfig
# Get Get current configurations of each interface
self.getCurrentInterfaces(self.networkInterfaces)
self.cellularInterface = iot2000Connman.getCellularTechnology()
self.cellularSimInfo = iot2000Connman.getOfonoSimManager()
# Show check box
bb = ButtonBar(self.topmenu.gscreen, (("Ok", "ok"), ("Cancel", "cancel", "ESC")))
ct = CheckboxTree(height=7, scroll=1, width=40)
self.showCheckbox(ct)
gridFormText = "Cross [*] to select active interfaces"
g = GridForm(self.topmenu.gscreen, gridFormText, 1, 4)
g.add(ct, 0, 1)
g.add(bb, 0, 3, growx=1)
result = g.runOnce()
if (bb.buttonPressed(result) != "ok"):
return;
# Get user selection
selectedOptions = ct.getSelection()
eth0Enabled = "eth0" in selectedOptions
eth1Enabled = "eth1" in selectedOptions
wifiEnabled = "Wifi" in selectedOptions
cellularEnabled = "Cellular" in selectedOptions
# Show sub-config
if eth0Enabled:
proceed = self.showEthConfig("eth0")
if eth1Enabled:
proceed = self.showEthConfig("eth1")
if wifiEnabled:
proceed = self.showWifiConfig("wlan0")
if cellularEnabled:
proceed = self.showCellularConfig()
if not proceed:
return
# Show new config
wpa_conf = readFileLines("/etc/wpa_supplicant.conf", 4)
self.cellularInterface = iot2000Connman.getCellularTechnology()
infoText = "Interface config:" + "\n" \
+ self.newSettingsFile \
+ "wlan0 config: " + "\n" \
+ "".join(wpa_conf) + "\n" \
+ "cellular: " + str(cellularEnabled) + "\n" + "\n".join(self.cellularInterface)
ret = ButtonChoiceWindow(
self.topmenu.gscreen,
"Press OK to enable New Config",
infoText,
[("OK", "ok"), ("Cancel", "cancel", "ESC")],
50, 50,
None)
if (ret != "ok"):
return
# down unselected interfaces
if not eth0Enabled:
subprocess.call("/sbin/ifdown eth0", shell=True, stderr=open(os.devnull, 'wb'))
if not eth1Enabled:
subprocess.call("/sbin/ifdown eth1", shell=True, stderr=open(os.devnull, 'wb'))
if not wifiEnabled:
subprocess.call("/sbin/ifdown wlan0", shell=True, stderr=open(os.devnull, 'wb'))
if not cellularEnabled:
iot2000Connman.disconnectCellular()
# Write interface file
fileName = "/etc/network/interfaces"
backupFileName = "/etc/network/interfaces.bak"
copyfile(fileName, backupFileName)
interfacesFile = open(fileName, 'w')
interfacesFile.write(self.newSettingsFile)
interfacesFile.close()
# restartNetwork?
newTempInterface = {'eth0': '', 'eth1': '', 'wlan0': ''}
self.getCurrentInterfaces(newTempInterface)
for key in self.networkInterfaces.keys():
if newTempInterface[key] != self.networkInterfaces[key]:
self.interfaceFileChange = True
if self.interfaceFileChange or self.reconfigWifi:
self.restartNetwork()
def showEthConfig(self, interface):
title = "Configure " + interface
settings = [(interface, self.networkInterfaces[interface])]
# ret is tuple type
ret = EntryWindow(
self.topmenu.gscreen,
title,
"Specify IP addresses for network interfaces, enter 'dhcp' to obtain address by DHCP.",
settings,
1,
70, 50,
['OK', ('Cancel', 'cancel', 'ESC')],
None)
if (ret[0] == "cancel"):
return False
# Update eth config
self.updateNewInterfaceConfig(interface, ret[1][0].rstrip(), settings[0][1])
return True
def showWifiConfig(self, interface):
title = "Configure " + interface
settings = [(interface, self.networkInterfaces[interface]), ("Type:", "WPA-PSK"), ("SSID:", ""), ("Key:", "")]
ret = EntryWindow(
self.topmenu.gscreen,
title,
"Specify IP addresses for network interfaces, enter 'dhcp' to obtain address by DHCP.",
settings,
1,
70, 50,
['OK', ('Cancel', 'cancel', 'ESC')],
None)
if (ret[0] == "cancel"):
return False
# Update wifi interace
self.updateNewInterfaceConfig(interface, ret[1][0].rstrip(), settings[0][1])
# Update wpa config
for i in range(1, len(settings)):
if (ret[1][i] != settings[i][1]):
self.reconfigWifi = True
break
wpaConfig = wpaFileTemplate.replace("[type]", ret[1][1].rstrip()).replace("[ssid]", ret[1][2].rstrip()).replace(
"[passwd]", ret[1][3].rstrip())
fileName = "/etc/wpa_supplicant.conf"
backupFileName = "/etc/wpa_supplicant.conf.bak"
copyfile(fileName, backupFileName)
wpaFile = open(fileName, 'w')
wpaFile.write(wpaConfig)
wpaFile.close()
return True
def showCellularConfig(self):
settings = []
reconfigAPN = False
apnDict = iot2000Connman.getOfonoAPN()
settings.append(("APN:", apnDict[apnHeaderStr]))
settings.append(("Username:", apnDict[userHeaderStr]))
settings.append(("Password:", apnDict[pwdHeaderStr]))
pinStr= self.cellularSimInfo['PinRequired']
if pinStr == "puk":
settings.append(("Puk:", ""))
reconfigPin = True
elif pinStr == "pin":
settings.append(("Pin:", ""))
reconfigPin = True
elif pinStr == "none":
reconfigPin = False
title = "Configure Cellular"
infoText = "SIM Setting for modem " + self.cellularSimInfo['ModemPath'] \
+ "\n(leave empty when in doubt)"
ret = EntryWindow(
self.topmenu.gscreen,
title,
infoText,
settings,
1,
70, 50,
['OK', ('Cancel', 'cancel', 'ESC')],
None)
if (ret[0] == "cancel"):
return False
# Update APN config?
for i in range(len(apnDict)):
if (ret[1][i] != settings[i][1]):
reconfigAPN = True
break
# Flow: disconnect---set params---connect
if reconfigAPN or reconfigPin:
iot2000Connman.disconnectCellular()
if reconfigPin:
iot2000Connman.configPin(ret[1][3].rstrip(), pinStr, self.cellularSimInfo['ModemPath'])
time.sleep(1)
self.cellularInterface = iot2000Connman.getCellularTechnology()
iot2000Connman.connectCellular(ret[1][0].rstrip(), ret[1][1].rstrip(), ret[1][2].rstrip())
return True
def getInterfaceConfig(self, interface):
lines = [line.rstrip('\n') for line in open('/etc/network/interfaces')]
searchString = "auto " + interface
for lineNumber in range(0, len(lines) - 1):
if lines[lineNumber].startswith('#'):
continue
if (searchString in lines[lineNumber]):
splitLine = lines[lineNumber].split()
while (splitLine[0] != "iface"):
lineNumber += 1
splitLine = lines[lineNumber].split()
mode = splitLine[3]
if (mode == "dhcp"):
return "dhcp"
if (mode == "static"):
while (splitLine[0] != "address"):
lineNumber += 1
splitLine = lines[lineNumber].split()
return splitLine[1]
lineNumber += 1
return ""
def getConfigString(self, interface, config):
if ("wlan0" == interface):
if ("dhcp" == config):
return wirelessDhcpTemplate.replace("[interfaceName]", interface)
else:
return wirelessStaticTemplate.replace("[interfaceName]", interface).replace("[ip]", config)
if ("dhcp" == config):
return dhcpTemplate.replace("[interfaceName]", interface)
else:
return staticTemplate.replace("[interfaceName]", interface).replace("[ip]", config)
def restartNetwork(self):
self.topmenu.gscreen.finish()
print(ansicolors.clear) # Clear console
print(ansicolors.reset)
subprocess.call("/etc/init.d/networking restart", shell=True)
if (self.wifiHWPresent):
subprocess.call("/sbin/ifdown wlan0", shell=True)
subprocess.call("/sbin/ifup wlan0", shell=True)
self.finish = True
def getCurrentInterfaces(self, networkDict):
for key, value in networkDict.items():
networkDict[key] = self.getInterfaceConfig(key)
def showCheckbox(self, ct):
eth0CrossBox = self.eth0HWPresent and len(self.networkInterfaces['eth0'])
eth1CrossBox = self.eth1HWPresent and len(self.networkInterfaces['eth1'])
wifiCrossBox = self.wifiHWPresent and len(self.networkInterfaces['wlan0'])
cellularCrossBox = False
if len(self.cellularInterface):
if " Connected = True" in self.cellularInterface:
cellularCrossBox = True
# Show checkbox in case that hardware is present
if self.eth0HWPresent:
ct.append("eth0", selected=eth0CrossBox)
if self.eth1HWPresent:
ct.append("eth1", selected=eth1CrossBox)
if self.wifiHWPresent:
ct.append("Wifi", selected=wifiCrossBox)
if 'ModemPath' in self.cellularSimInfo:
if len(self.cellularSimInfo['ModemPath']) > 0:
ct.append("Cellular", selected=cellularCrossBox)
def updateNewInterfaceConfig(self, interface, newConfig, oldConfig):
# Invalid newConfig if it is blank
if len(newConfig) > 0:
self.newSettingsFile += self.getConfigString(interface, newConfig)
elif len(oldConfig) > 0:
self.newSettingsFile += self.getConfigString(interface, oldConfig)
class SoftwareSettings:
def __init__(self, topmenu):
self.topmenu = topmenu
self.finish = False
def show(self):
while(True and not self.finish):
action, selection = ListboxChoiceWindow(
self.topmenu.gscreen,
"Software", "",
[("Set Package Repository", self.setRepo),("Manage Packages", self.changePackages),("Manage Autostart Options", self.changeAutostart)],
[('Back', 'back', 'ESC')])
if(action == 'back'):
return
selection()
def setRepo(self):
ret = EntryWindow(
self.topmenu.gscreen,
"Please enter a valid address for a opkg package repository (IP or host name only).",
"",
[("Host Address:", "")],
1,
70, 50,
['OK', ('Cancel', 'cancel', 'ESC')],
None)
if(ret[0] != "ok"):
return
opkgConfig = repoFileTemplate.replace("[host]", ret[1][0].rstrip())
with open("/etc/opkg/iot2000.conf", "w") as textfile:
textfile.write(opkgConfig)
self.topmenu.gscreen.finish()
print(ansicolors.clear) # Clear console
print(ansicolors.reset)
finish = True
def changePackages(self):
### Edit here ###
# Contains all potential candidates for removal
packageList = ["galileo-target", "nodejs", "tcf-agent"]
###
bb = ButtonBar(self.topmenu.gscreen, (("Ok", "ok"), ("Cancel", "cancel")))
ct = CheckboxTree(height = 10, scroll = 1,width=40)
# Iterate through list of removal candidates and check if they are
# actually installed.
task = subprocess.Popen("/usr/bin/opkg list-installed", stdout=subprocess.PIPE, shell=True)
installedPackages = task.stdout.read().decode()
numberOfRemovablePackages = 0
for package in packageList:
if (package in installedPackages):
ct.append(package)
numberOfRemovablePackages += 1
g = GridForm(self.topmenu.gscreen, "Select Packages to Remove", 1, 4)
l = Label("Use 'Space' to select the packages you want to remove.")
g.add(l, 0, 0, growy=1, growx=1, padding=(1,1,1,1))
g.add(ct, 0, 1)
g.add(bb, 0, 3, growx = 1)
result = g.runOnce()
removeList = ''
if (bb.buttonPressed(result) != "ok" or numberOfRemovablePackages < 1):
return;
# Build list of selected packages
selectedPackages = ct.getSelection()
for package in selectedPackages:
removeList = removeList + package + '* '
ret = ButtonChoiceWindow(
self.topmenu.gscreen,
"Remove Packages",
"Are you sure you want to remove the following packages: \n\n" + removeList,
buttons=[("OK", "ok"), ("Cancel", "cancel", "ESC")],
width=40)
if (ret != "ok"):
return
removeList = "/usr/bin/opkg --force-removal-of-dependent-packages remove " + removeList
self.topmenu.gscreen.finish()
print(ansicolors.clear)
print(ansicolors.reset)
subprocess.call(removeList, shell=True)
self.finish = True
def changeAutostart(self):
nodeRedEnabled = self.serviceEnabled("node-red")
sshEnabled = self.serviceEnabled("sshd")
mosquittoEnabled = self.serviceEnabled("mosquitto")
galileoEnabled = self.serviceEnabled("galileod")
tcfEnabled = self.serviceEnabled("tcf-agent")
bb = ButtonBar(self.topmenu.gscreen, (("Ok", "ok"), ("Cancel", "cancel", "ESC")))
ct = CheckboxTree(height = 7, scroll = 1,width=40)
ct.append("Node-RED", "node-red", selected=nodeRedEnabled)
ct.append("SSH Server", "ssh", selected=sshEnabled)
ct.append("Mosquitto Broker", "mosquitto", selected=mosquittoEnabled)
ct.append("Galileo Arduino Runtime", "galileo", selected=galileoEnabled)
ct.append("TCF Debugger Agent", "tcf", selected=tcfEnabled)
g = GridForm(self.topmenu.gscreen, "Autostart Services", 1, 4)
g.add(ct, 0, 1)
g.add(bb, 0, 2, growx = 1)
result = g.runOnce()
if (bb.buttonPressed(result) != "ok"):
return;
selectedOptions = ct.getSelection()
nodeRedEnabledNew = "node-red" in selectedOptions
sshEnabledNew = "ssh" in selectedOptions
mosquittoEnabledNew = "mosquitto" in selectedOptions
galileoEnabledNew = "galileo" in selectedOptions
tcfEnabledNew = "tcf" in selectedOptions
if (nodeRedEnabled != nodeRedEnabledNew):
self.update_rc("node-red", nodeRedEnabledNew)
if (sshEnabled != sshEnabledNew):
self.update_rc("sshd", sshEnabledNew)
if (mosquittoEnabled != mosquittoEnabledNew):
self.update_rc("mosquitto", mosquittoEnabledNew)
if (galileoEnabled != galileoEnabledNew):
self.update_rc("galileod", galileoEnabledNew)
if (tcfEnabled != tcfEnabledNew):
self.update_rc("tcf-agent", tcfEnabledNew)
def update_rc(self, service, enable):
with open(os.devnull, 'wb') as devnull:
if (enable):
subprocess.call("update-rc.d %s defaults" % service, shell=True, stdout=devnull)
subprocess.call("/etc/init.d/%s start" % service, shell=True, stdout=devnull)
else:
subprocess.call("/etc/init.d/%s stop" % service, shell=True, stdout=devnull)
subprocess.call("update-rc.d -f %s remove" % service, shell=True, stdout=devnull, stderr=devnull)
def serviceEnabled(self, service):
task = subprocess.Popen("/etc/init.d/%s status" % service, stdout=subprocess.PIPE, shell=True)
taskReturn = task.stdout.read().decode().lstrip().rstrip()
return "running" in taskReturn
class Peripherals:
def __init__(self, topmenu):
self.topmenu = topmenu
self.finish = False
self.comPortsPresent = os.path.isdir("/dev/ttyS2")
def show(self):
menuItems = [("Show I/O Configuration", self.showIoConfiguration), ("Enable I2C on Pins A4 & A5", self.enableI2c), ("Enable SPI on Pins D10-D13", self.enableSpi), ("Enable UART0 on Pins D0 & D1", self.enableUart0)]
if (not self.topmenu.deviceIsIot2020):
menuItems.insert(0, ("Configure External COM Ports", self.configureComPorts))
while(True and not self.finish):
action, selection = ListboxChoiceWindow(
self.topmenu.gscreen,
"Peripherals", "",
menuItems,
[('Back', 'back', 'ESC')])
if(action == 'back'):
return
selection()
def enableUart0(self):
u=mraa.Uart(0)
self.showIoConfiguration()
def enableSpi(self):
s=mraa.Spi(0)
self.showIoConfiguration()
def enableI2c(self):
x = mraa.I2c(0)
self.showIoConfiguration()
def showIoConfiguration(self):
s = " Pin | Function\n ------------+------------\n"
for i in range(0,20):
s = s + " IO" + "{:<10}".format(str(i))
if (i == 0):
if (self.getPinValue(45) == 1):
s = s + "| UART 0 RX"
else:
s = s + "| GPIO"
elif (i == 1):
if (self.getPinValue(45) == 1):
s = s + "| UART 0 TX"
else:
s = s + "| GPIO"
if (i == 2):
if (self.getPinValue(77) == 1):
s = s + "| UART 1 RX"
else:
s = s + "| GPIO"
elif (i == 3):
if (self.getPinValue(76) == 1):
s = s + "| UART 1 TX"
else:
if (self.getPinValue(64) == 1):
s = s + "| PWM"
else:
s = s + "| GPIO"
if (i == 4):
s = s + "| GPIO"
elif (i == 5):
if (self.getPinValue(66) == 1):
s = s + "| PWM"
else:
s = s + "| GPIO"
if (i == 6):
if (self.getPinValue(68) == 1):
s = s + "| PWM"
else:
s = s + "| GPIO"
elif (i == 7):
s = s + "| GPIO"
elif (i == 8):
s = s + "| GPIO"
elif (i == 9):
s = s + "| GPIO"
elif (i == 10):
if (self.getPinValue(74) == 1):
s = s + "| PWM"
if (self.getPinValue(44) == 1):
s = s + "| SPI SS"
else:
s = s + "| GPIO"
elif (i == 11):
if (self.getPinValue(44) == 1):
s = s + "| SPI MOSI"
else:
s = s + "| GPIO"
elif (i == 12):
if (self.getPinValue(46) == 1):
s = s + "| SPI MISO"
else:
s = s + "| GPIO"
elif (i == 13):
if (self.getPinValue(46) == 1):
s = s + "| SPI SCK"
else:
s = s + "| GPIO"
elif (i == 14):
s = s + "| GPIO/ADC0"
elif (i == 15):
s = s + "| GPIO/ADC1"
elif (i == 16):
s = s + "| GPIO/ADC2"
elif (i == 17):
s = s + "| GPIO/ADC3"
elif (i == 18):
if (self.getPinValue(60) == 1):
if (self.getPinValue(78) == 1):
s = s + "| GPIO"
else:
s = s + "| ADC4"
else:
s = s + "| I2C SDA"
elif (i == 19):
if (self.getPinValue(60) == 1):
if (self.getPinValue(79) == 1):
s = s + "| GPIO"
else:
s = s + "| ADC5"
else:
s = s + "| I2C SCL"
s += "\n"
rv = ButtonChoiceWindow(
self.topmenu.gscreen,
"I/O Configuration Overview",
s,
buttons=["Back"],
width=40)
def exportPin(self, pin):
subprocess.call('echo -n "' + str(pin) + '" > /sys/class/gpio/export', shell=True, stderr=open(os.devnull, 'wb'), stdout=open(os.devnull, 'wb'))
def pinDirection(self, pin, direction):
if (direction == "in" or direction == "out"):
subprocess.call('echo -n "' + str(direction) + '" > /sys/class/gpio/gpio' + str(pin) + '/direction', shell=True, stderr=open(os.devnull, 'wb'), stdout=open(os.devnull, 'wb'))
def getPinValue(self, pin):
self.exportPin(pin)
return int(check_output(["cat", '/sys/class/gpio/gpio' + str(pin) +'/value']))
def configureComPorts(self):
portAction, portSelection = ListboxChoiceWindow(
self.topmenu.gscreen,
"Configure Serial Mode", "Select the serial port you want to configure and press 'Enter'.",
["X30", "X31"],
[('Cancel', 'cancel', 'ESC')])
if (portSelection == 0):
portName = "X30"
else:
portName = "X31"
currentMode = self.getSerialModeForPort(portName)
if (portAction == "cancel"):
return
modes = ["RS232", "RS485", "RS422"]
modeAction, modeSelection = ListboxChoiceWindow(
self.topmenu.gscreen,
"Configure Serial Mode", "Select a mode.",
modes,
[('Cancel', 'cancel', 'ESC')], default=currentMode)
if (modeAction == "cancel"):
return
persistentReturn = ButtonChoiceWindow(
self.topmenu.gscreen,
"Configure Serial Mode",
"Do you want to make your changes persistent? (Mode setting will be kept after reboot.) ",
buttons=[("Yes", "yes"), ("No", "no", "ESC")],
width=40)
switchTool = '/usr/bin/switchserialmode'
if (portSelection == 0):
switchDeviceArg = '/dev/ttyS2'
else:
switchDeviceArg = '/dev/ttyS3'
switchModeArg = modes[modeSelection].lower()
switchCommand = switchTool + " " + switchDeviceArg + " " + switchModeArg
subprocess.Popen([switchTool, switchDeviceArg, switchModeArg], stdout=open(os.devnull, 'wb'))
if (persistentReturn == "yes"):
fileName = "set_serial_mode_" + portName + ".sh"
filePath = "/etc/init.d/" + fileName
initFile = open(filePath, 'w')
initFile.write("#!/bin/sh\n" + switchCommand)
initFile.close()
st = os.stat(filePath)
os.chmod(filePath, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
subprocess.call("update-rc.d " + fileName + " defaults", shell=True, stdout=open(os.devnull, 'wb'))
def getSerialModeForPort(self, port):
fileName = "/etc/init.d/set_serial_mode_" + port + ".sh"
if (os.path.isfile(fileName)):
lines = [line.rstrip('\n') for line in open(fileName)]
selectedMode = lines[1].split()[2]
if selectedMode == "rs232":
return 0
elif selectedMode == "rs485":
return 1
elif selectedMode == "rs422":
return 2
return 0
class connmanManager:
def __init__(self):
self.ethSupport = True
self.wifiSupport = True
self.cellularSupport = True
self.bluetoothSupport = False
self.connmanTechnologies = []
self.connmanServices = []
self.connmanSettings = ""
self.connmanConfigs = ""
self.cellularTechnology = []
self.cellularContext = ""
self.cellularCarrier = ""
def isOfonoRunning(self):
cellularOfono = False
task = subprocess.Popen("ps aux | grep -v grep | grep -i ofonod", stdout=subprocess.PIPE, shell=True)
if len(task.stdout.read().decode()) > 0:
cellularOfono = True
return cellularOfono
def getOfonoSimManager(self):
ofonoSimManager = {'Present': False, 'PinRequired': "none", 'LockedPins': False}
task = subprocess.Popen("/usr/lib/ofono/test/list-modems", stdout=subprocess.PIPE, shell=True)
ofonoModemInfo = task.stdout.read().decode()
if len(ofonoModemInfo) > 0:
if ofonoModemInfo.find("Present = 1") >= 0:
ofonoSimManager['Present'] = True
if ofonoModemInfo.find("LockedPins = pin") >= 0:
ofonoSimManager['LockedPins'] = True
if ofonoModemInfo.find("PinRequired = pin") >= 0:
ofonoSimManager['PinRequired'] = "pin"
elif ofonoModemInfo.find("PinRequired = puk") >= 0:
ofonoSimManager['PinRequired'] = "puk"
# Add dynamic ModemPath
modemPathStart = ofonoModemInfo.index('[')
modemPathEnd = ofonoModemInfo.index(']')
modemPathStr = ofonoModemInfo[modemPathStart+2:modemPathEnd-1]
ofonoSimManager['ModemPath'] = modemPathStr
return ofonoSimManager
def getOfonoAPN(self):
apnDict = {apnHeaderStr:"", userHeaderStr:"", pwdHeaderStr:""}
task = subprocess.Popen("/usr/lib/ofono/test/list-contexts", stdout=subprocess.PIPE, shell=True)
ofonoContextList = task.stdout.read().decode().split('\n')
for key in apnDict.keys():
for i in range(len(ofonoContextList)):
if ofonoContextList[i].find(key) >= 0:
apnDict[key] = ofonoContextList[i][len(key):]
break
return apnDict
def getConnmanTechnologies(self):
self.connmanTechnologies = []
self.cellularTechnology = []
if self.isOfonoRunning():
task = subprocess.Popen("/usr/bin/connmanctl technologies", stdout=subprocess.PIPE, shell=True)
self.connmanTechnologies = task.stdout.read().decode().split('\n')
for i in range(len(self.connmanTechnologies)):
if self.connmanTechnologies[i].find('cellular') >= 0:
self.cellularTechnology = self.connmanTechnologies[i: i + 6]
break
def getCellularTechnology(self):
self.getConnmanTechnologies()
return self.cellularTechnology
def getCellularContext(self):
self.cellularContext = ""
self.cellularCarrier = ""
task = subprocess.Popen("/usr/bin/connmanctl services", stdout=subprocess.PIPE, shell=True)
self.connmanServices = task.stdout.read().decode().split()
for i in range(len(self.connmanServices)): # find the first cellular context
if self.connmanServices[i].find('cellular') >= 0:
self.cellularContext = self.connmanServices[i]
self.cellularCarrier = self.connmanServices[i - 1]
break
def getConnmanSettings(self):
file = open("/var/lib/connman/settings")
self.connmanSettings = file.read()
file.close()
def connectCellular(self, apn, user, pwd):
if not self.isOfonoRunning():
print("ofono is not running")
else:
if len(self.cellularTechnology) > 0:
# if modem need poweron
if " Powered = False" in self.cellularTechnology:
self.powerOnCellular()
# connect Cellular
self.connectCellularContext()
# real connected?
if " Connected = False" in self.getCellularTechnology():
self.initCellular(apn, user, pwd)
else:
print("--No cellular technology!!!---")
def initCellular(self, apn, user, pwd):
print("The Cellular need initialization")
internetContext = apn+" "+user+" "+pwd
subprocess.call("/usr/lib/ofono/test/create-internet-context " + internetContext, shell=True,
stderr=open(os.devnull, 'wb'))
subprocess.call("/usr/lib/ofono/test/online-modem", shell=True, stderr=open(os.devnull, 'wb'))
time.sleep(3)
# connect Cellular
self.connectCellularContext()
# real connected?
if " Connected = False" in self.getCellularTechnology():
print("Cellular connection is failed")
def connectCellularContext(self):
# get cellular service and context
self.getCellularContext()
# Connect cellularlo
if len(self.cellularContext) > 0:
subprocess.call("/usr/bin/connmanctl connect " + self.cellularContext, shell=True, stderr=open(os.devnull, 'wb'))
def powerOnCellular(self):
subprocess.call("/usr/bin/connmanctl enable cellular", shell=True, stderr=open(os.devnull, 'wb'))
print("Cellular is in poweron, need to wait")
time.sleep(5)
def disconnectCellular(self):
# get cellular service
self.getCellularContext()
subprocess.call("/usr/bin/connmanctl disconnect " + self.cellularContext, shell=True,
stderr=open(os.devnull, 'wb'))
def configPin(self, pinCode, pinStr, path):
pinArgs = path + " " + pinStr + " " + pinCode
subprocess.call("/usr/lib/ofono/test/enter-pin "+pinArgs, shell=True,
stderr=open(os.devnull, 'wb'))
subprocess.call("/usr/lib/ofono/test/unlock-pin "+pinArgs, shell=True,
stderr=open(os.devnull, 'wb'))
iot2000Connman = connmanManager()
mainwindow = TopMenu()
while(True):
mainwindow.show()