@@ -184,6 +184,45 @@ def start_service(cls):
184
184
print ("Unable to register ip, revert to ap mode" )
185
185
cls .start_as_ap ()
186
186
187
+ @classmethod
188
+ def get_hostapd_config_file (cls ):
189
+ adapter = cls .get_adapter_type ()
190
+ hostapd_type = cls .hostapds .get (adapter )
191
+ return "/etc/hostapd/" + hostapd_type + ".conf"
192
+
193
+ @classmethod
194
+ def set_unique_ssid (cls ):
195
+ """
196
+ See if ssid is already a unique id based on CPU serial number.
197
+ If not, set it in hostapd.conf
198
+ """
199
+ try :
200
+ inconfig = None
201
+ with open (cls .get_hostapd_config_file ()) as infile :
202
+ inconfig = infile .read ()
203
+ inconfig = inconfig .replace ("CHANGEMEATFIRSTRUN" , str (abs (hash (cls .get_serial ())))[0 :4 ])
204
+ with open (cls .get_hostapd_config_file (), "w" ) as outfile :
205
+ outfile .write (inconfig )
206
+ except :
207
+ print ("Unexpected error: " , sys .exc_info ()[0 ])
208
+ raise
209
+
210
+ @classmethod
211
+ def get_serial (cls ):
212
+ """
213
+ Extract serial from cpuinfo file
214
+ """
215
+ cpuserial = "0000000000000000"
216
+ try :
217
+ f = open ('/proc/cpuinfo' ,'r' )
218
+ for line in f :
219
+ if line [0 :6 ]== 'Serial' :
220
+ cpuserial = line [10 :26 ]
221
+ f .close ()
222
+ except :
223
+ cpuserial = "ERROR000000000"
224
+ return cpuserial
225
+
187
226
def main ():
188
227
w = WiFi ()
189
228
if len (sys .argv ) > 2 and sys .argv [1 ] == "updatecfg" :
@@ -199,6 +238,7 @@ def main():
199
238
WiFi .get_config ()['bot_name' ] = sys .argv [3 ]
200
239
WiFi .save_config ()
201
240
else :
241
+ w .set_unique_ssid ()
202
242
w .start_service ()
203
243
204
244
if __name__ == "__main__" :
0 commit comments