First Commit
[owSlave2.git] / tools / chowid.py
1 #!/usr/bin/python
2 # Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 # All rights reserved. 
4
5 # Redistribution and use in source and binary forms, with or without 
6 # modification, are permitted provided that the following conditions are 
7 # met: 
8
9 #  * Redistributions of source code must retain the above copyright 
10 #    notice, this list of conditions and the following disclaimer. 
11 #  * Redistributions in binary form must reproduce the above copyright 
12 #    notice, this list of conditions and the following disclaimer in the 
13 #    documentation and/or other materials provided with the 
14 #    distribution. 
15 #  * All advertising materials mentioning features or use of this 
16 #    software must display the following acknowledgement: This product 
17 #    includes software developed by tm3d.de and its contributors. 
18 #  * Neither the name of tm3d.de nor the names of its contributors may 
19 #    be used to endorse or promote products derived from this software 
20 #    without specific prior written permission. 
21
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
33
34
35
36
37 import time
38 import subprocess
39
40 def owcom(dev,send,rc):
41         res=[]
42         f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)
43         f.write("".join(map(chr, send)))
44         if (rc!=0):
45                 res=map(ord,f.read(rc))
46         f.close()
47         return res
48                 
49
50 def crc8(arr):
51         lscrc=0x0;
52         for v in arr:
53                 bit=1;
54                 while bit<256:
55                         if (v&bit)==bit:
56                                 lb=1
57                         else:
58                                 lb=0
59                         if (lscrc&1)!=lb:
60                                 lscrc=(lscrc>>1)^0x8c 
61                         else:
62                                 lscrc=(lscrc>>1)
63                         bit=bit*2
64         return lscrc
65         
66 def testnr(s):
67         for c in s.lower()[:]:
68                 if not((c) in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','-']):
69                         return False
70         return True
71
72 def addid(id,val):
73         for i in range(7):
74                 id[i+1]=id[i+1]+val
75                 if id[i+1]>254:
76                         id[i+1]=id[i+1]-254
77                         val=1
78                 else:
79                         return id
80         return id
81 #
82
83 l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)
84 dc=0
85 dl=[]
86 for g in  (l.split("\n")):
87         if len(g)>2:
88                 if testnr(g[0:2]):
89                         dl.append(g)
90                         dc=dc+1
91                         print dc,") ",g
92 if dc==0:
93         print "No 1-Wire Device found"
94         exit(0)
95 n=int(raw_input("No. of Device: "))
96 n=n-1
97 if (n>dc-1)or(n<0):
98                 exit(0)
99 print dl[n], "will be changed"
100 s=dl[n]
101 nr=[int(s[0:2],16),int(s[13:15],16),int(s[11:13],16),int(s[9:11],16),int(s[7:9],16),int(s[5:7],16),int(s[3:5],16)]
102 nr.append(crc8(nr))
103 onr=nr[:]
104 re=1
105 while re:
106         re=0
107         print "Number: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(nr)
108         print "1) Increment the old ID"
109         print "2) Add a value to the old ID (value < 255)"
110         print "3) Input 6 hex numbers like AA,B5,00,32,12,F1"
111         print "4) Exit"
112         cho=int(raw_input("Press the number of your choice: "))
113         if cho==1:
114                 nr=addid(nr,1)
115         elif cho==2:
116                 val=int(raw_input("Add value <255: "))
117                 nr=addid(nr,val)
118         elif cho==3:
119                 hn=raw_input("Input six numbers in hex (like AA,B5,00,32,12,F1): ")
120                 i=1
121                 for hnt in hn.split(","):
122                         nr[i]=int(hnt,16)
123                         i=i+1
124         elif cho==4:
125                 exit()
126         else:
127                 re=1
128 nr[7]=crc8(nr[:-1])
129 print "New ID: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(nr)
130 cho=raw_input("Set this ID? [Y/n): ")
131 if (cho!='Y'):
132         exit()
133
134
135 owcom(s,[0x75]+nr,0)
136 nnr=owcom(s,[0xA7],8)
137 print "New ID: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(nnr)
138 if nr!=nnr:
139         print "ERROR writing new ID\nMaybe it's no simulatet ow device from tm3d.de"
140         exit(0)
141 print "Old ID: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(onr)
142 owcom(s,[0x79,onr[1],onr[5],onr[6]],0)
143 print "Done.\nWait for a new Searchrom from Master\nSometimes the old nr keeps in the directory"