4848
4949import serial
5050from serial .serialutil import SerialException
51+ from typing import Literal
5152
5253import commissioner
5354from GRLLibs .ThreadPacket .PlatformPackets import (
@@ -335,9 +336,10 @@ def __executeCommand(self, cmd, timeout=DEFAULT_COMMAND_TIMEOUT, ignore_exec=Fal
335336 if line is None :
336337 time .sleep (0.01 )
337338 continue
338- line = line . replace ( " \x1b [J" , "" )
339+
339340 # self.log("readline: %s", line)
340341 # skip empty lines
342+ line = line .replace ("\x1b [J" , "" )
341343 if line :
342344 response .append (line )
343345
@@ -347,6 +349,8 @@ def __executeCommand(self, cmd, timeout=DEFAULT_COMMAND_TIMEOUT, ignore_exec=Fal
347349 m = OpenThreadTHCI ._COMMAND_OUTPUT_ERROR_PATTERN .match (line )
348350 if m is not None :
349351 code , msg = m .groups ()
352+ if ignore_exec :
353+ return True
350354 raise CommandError (int (code ), msg )
351355 else :
352356 if ignore_exec :
@@ -964,7 +968,9 @@ def getMAC(self, bType=MacType.RandomMac):
964968 specific type of MAC address
965969 """
966970 # if power down happens, return extended address assigned previously
967- if self .isPowerDown :
971+ if self .isPowerDown and bType == MacType .EthMac :
972+ return self ._deviceGetEtherMac ()
973+ elif self .isPowerDown :
968974 macAddr64 = self .mac
969975 else :
970976 if bType == MacType .FactoryMac :
@@ -2234,9 +2240,9 @@ def diagnosticQuery(self, strDestinationAddr="", listTLV_ids=[], rloc16=0):
22342240
22352241 @API
22362242 def vendorInfo (self , name = "" , model = "" , version = "" ):
2237- vn = "vendor name %s" % name
2238- vm = "vendor model %s" % model
2239- vv = "vendor swversion %s" % version
2243+ vn = "vendor name %s" % name . replace ( " " , "" )
2244+ vm = "vendor model %s" % model . replace ( " " , "" )
2245+ vv = "vendor swversion %s" % version . replace ( " " , "" )
22402246 self .__executeCommand (vn )
22412247 self .__executeCommand (vm )
22422248 self .__executeCommand (vv )
@@ -3050,10 +3056,10 @@ def ValidateDeviceFirmware(self):
30503056 # elif self.DeviceCapability == OT12_CAPBS:
30513057 # return OT12_VERSION in self.UIStatusMsg
30523058 if self .DeviceCapability == OT13_CAPBS :
3053- return [
3054- True if OT13_VER in self .UIStatusMsg else False
3055- for OT13_VER in OT13_VERSION
3056- ]
3059+ OT13_FirmwareCheck = any (
3060+ OT13_VER in self .UIStatusMsg for OT13_VER in OT13_VERSION
3061+ )
3062+ return OT13_FirmwareCheck
30573063 else :
30583064 return False
30593065
@@ -3262,26 +3268,45 @@ def sendUdp(self, destination, port, payload="hello"):
32623268 return self .__executeCommand (cmd )[- 1 ] == "Done"
32633269
32643270 @API
3265- def send_udp (self , interface , destination , port , payload = "12ABcd" ):
3271+ def send_udp (
3272+ self ,
3273+ interface ,
3274+ destination ,
3275+ port ,
3276+ payload = "12ABcd" ,
3277+ return_result = False ,
3278+ spiff_udp_ip = "" ,
3279+ ):
32663280 """payload hexstring"""
32673281 assert payload is not None , "payload should not be none"
32683282 assert interface == 0 , "non-BR must send UDP to Thread interface"
3269- self .__udpOpen ()
3283+ self .__udpOpen (spiff_udp_ip , port )
32703284 time .sleep (0.5 )
3285+ cmd_ = "test tmforiginfilter disable"
3286+ self .__executeCommand (cmd_ )
32713287 cmd = "udp send %s %s -x %s" % (destination , port , payload )
3272- result = self .__executeCommand (cmd )[- 1 ] == "Done"
3273-
3288+ if not return_result :
3289+ result = self .__executeCommand (cmd )[- 1 ] == "Done"
3290+ else :
3291+ result = self .__executeCommand (cmd )[0 ]
3292+ _cmd = "test tmforiginfilter enable"
3293+ self .__executeCommand (_cmd )
32743294 return result
32753295
3276- def __udpOpen (self ):
3296+ def __udpOpen (self , spiff_udp_ip = "" , port = "" ):
3297+ if spiff_udp_ip != "" :
3298+ self .__isUdpOpened = False
3299+ self .__executeCommand ("udp close" )
32773300 if not self .__isUdpOpened :
32783301 cmd = "udp open"
32793302 self .__executeCommand (cmd )
32803303
32813304 # Bind to RLOC address and first dynamic port
32823305 rlocAddr = self .getRloc ()
3283-
3284- cmd = "udp bind %s 49152" % rlocAddr
3306+ if spiff_udp_ip == "" :
3307+ cmd = "udp bind %s 49152" % rlocAddr
3308+ else :
3309+ cmd = "udp connect %s %s" % (spiff_udp_ip , port )
32853310 self .__executeCommand (cmd )
32863311
32873312 self .__isUdpOpened = True
@@ -3404,6 +3429,10 @@ def mle_Discovery(self, channel):
34043429 cmd = "discover %s" % channel
34053430 return self .__executeCommand (cmd )
34063431
3432+ @API
3433+ def icmp_ping_req (self , addr ):
3434+ return self .__executeCommand ("ping %s 2" % addr )
3435+
34073436 ########################################################################################################################
34083437 # 1.3 THCI Commands
34093438 #
@@ -3736,7 +3765,9 @@ def get_own_omr_address(self, omr_prefix):
37363765
37373766 def srp_client_remove (self , instancename , servicename ):
37383767 cmd = "srp client service remove %s %s" % (instancename , servicename )
3768+ # cmd = 'srp client service remove service-test-1 _thread-test._udp'
37393769 self .__executeCommand (cmd )
3770+ # self.__executeCommand("netdata register")
37403771 time .sleep (3 )
37413772
37423773 def srpCallBackEnable (self ):
@@ -3758,10 +3789,6 @@ def host_clear(self):
37583789 cmd = "srp client host clear"
37593790 self .__executeCommand (cmd )
37603791
3761- def client_key_lease (self , keylease ):
3762- cmd = "srp client keyleaseinterval %s" % keylease
3763- self .__executeCommand (cmd )
3764-
37653792 def srpCallBackDisable (self ):
37663793 cmd = "srp client callback disable"
37673794 self .__executeCommand (cmd )
@@ -3872,17 +3899,17 @@ def srp_update(
38723899 srv ,
38733900 tcp_service_type ,
38743901 port ,
3875- weight ,
38763902 priority ,
3903+ weight ,
38773904 txt ,
38783905 )
38793906 else :
38803907 cmd = "srp client service add %s %s %s %s %s %s" % (
38813908 srv ,
38823909 service_type ,
38833910 port ,
3884- weight ,
38853911 priority ,
3912+ weight ,
38863913 txt ,
38873914 )
38883915 self .__executeCommand (cmd )
@@ -4019,17 +4046,17 @@ def srp_add(
40194046 srv ,
40204047 tcp_service_type ,
40214048 port ,
4022- weight ,
40234049 priority ,
4050+ weight ,
40244051 txt ,
40254052 )
40264053 else :
40274054 cmd = "srp client service add %s %s %s %s %s %s" % (
40284055 srv ,
40294056 service_type ,
40304057 port ,
4031- weight ,
40324058 priority ,
4059+ weight ,
40334060 txt ,
40344061 )
40354062 self .__executeCommand (cmd )
@@ -4045,6 +4072,41 @@ def srp_add(
40454072 cmd = "srp client start %s %s" % (dst_addr , port )
40464073 self .__executeCommand (cmd )
40474074
4075+ def srp_service_registration (
4076+ self ,
4077+ host_name ,
4078+ host_addr ,
4079+ lease_info = (),
4080+ dst_addr_and_port = (),
4081+ services = [],
4082+ service_key : Literal ["" , "enable" , "disable" ] = "" ,
4083+ name_compression : Literal ["" , "enable" , "disable" ] = "" ,
4084+ ):
4085+ self .__executeCommand ("srp client host clear" )
4086+ self .__executeCommand (f"srp client host name { host_name } " )
4087+ self .__executeCommand (f"srp client host address { host_addr } " )
4088+ self .__executeCommand (f"srp client leaseinterval { lease_info [0 ]} " )
4089+ self .__executeCommand (f"srp client keyleaseinterval { lease_info [1 ]} " )
4090+
4091+ if service_key != "" :
4092+ self .__executeCommand (f"srp client service key { service_key } " )
4093+
4094+ if name_compression != "" :
4095+ self .__executeCommand (f"dns compression { name_compression } " )
4096+
4097+ if dst_addr_and_port :
4098+ self .__executeCommand ("srp client stop" )
4099+ self .__executeCommand (f"dns config { dst_addr_and_port [0 ]} 53 6000 3 1" )
4100+ self .__executeCommand (
4101+ f"srp client start { dst_addr_and_port [0 ]} { dst_addr_and_port [1 ]} "
4102+ )
4103+ else :
4104+ self .__executeCommand ("srp client autostart enable" )
4105+
4106+ for instance_name , service_name , port , priority , weight , txt in services :
4107+ cmd = f"srp client service add { instance_name } { service_name } { port } { priority } { weight } { txt } "
4108+ self .__executeCommand (cmd )
4109+
40484110 def dns_query (self , addr = "" , service_type = "_thread-test._udp" ):
40494111 """Send unicast DNS query
40504112
@@ -4073,9 +4135,9 @@ def dns_resolve4(self, host_name):
40734135 cmd = "dns resolve4 %s.default.service.arpa" % host_name
40744136 self .__executeCommand (cmd )
40754137
4076- def dns_resolve_ip (self , host_name , server_ip = "" ):
4138+ def dns_resolve_ip (self , host_name , server_ip = "" , ignore_exec = False ):
40774139 cmd = "dns resolve %s %s" % (host_name , server_ip )
4078- self .__executeCommand (cmd )
4140+ self .__executeCommand (cmd , ignore_exec = ignore_exec )
40794141
40804142 def dns_resolve_ipv4 (self , host_name , server_ip = "" , ignore_exec = False ):
40814143 cmd = "dns resolve4 %s %s" % (host_name , server_ip )
@@ -4154,14 +4216,20 @@ def dns_tcp_query_instance(self, service_name, service_type):
41544216 self .__executeCommand (cmd )
41554217
41564218 def dns_query_instance (
4157- self , ip_addr , service_name , service_type = "_thread-test._udp"
4219+ self ,
4220+ service_name ,
4221+ service_type = "_thread-test._udp" ,
4222+ ip_addr = "" ,
4223+ ignore_exec = False ,
41584224 ):
4159- cmd = "dns service %s %s.default.service.arpa %s 53 6000 3 1 " % (
4225+ cmd = "dns service %s %s.default.service.arpa" % (
41604226 service_name ,
41614227 service_type ,
4162- ip_addr ,
41634228 )
4164- self .__executeCommand (cmd )
4229+ if ip_addr :
4230+ cmd += f" { ip_addr } 53 6000 3 1"
4231+ print (cmd )
4232+ self .__executeCommand (cmd , ignore_exec = ignore_exec )
41654233
41664234 def setMliid (self , sMlIid ):
41674235 self .__setMlIid (sMlIid )
@@ -4174,6 +4242,10 @@ def srp_host_remove(self):
41744242 cmd = "srp client host remove"
41754243 self .__executeCommand (cmd )
41764244
4245+ def client_service_clear (self , instancename , servicename ):
4246+ cmd = "srp client service clear %s %s" % (instancename , servicename )
4247+ self .__executeCommand (cmd )
4248+
41774249 def add_new_service (self ):
41784250 rloc = self .__executeCommand ("rloc16" )[0 ]
41794251 mleid = self .__executeCommand ("ipaddr mleid" )[0 ]
@@ -4250,9 +4322,49 @@ def stopDTLSSession(self):
42504322 # time.sleep(15)
42514323
42524324 @API
4253- def add_service (self , service , server ):
4254- self .__executeCommand ("service add 44970 %s %s" % (service , server ))
4255- self .__executeCommand ("netdata register" )
4325+ def add_service (self , service = "" , server = "" , service_data = False ):
4326+ if service_data :
4327+ self .__executeCommand ("service add 44970 %s%s" % (service , server ))
4328+ self .__executeCommand ("netdata register" )
4329+ else :
4330+ self .__executeCommand ("service add 44970 %s %s" % (service , server ))
4331+ self .__executeCommand ("netdata register" )
4332+
4333+ def tcp_http_request (self , addr = "" , port = 80 , hexa_http_request = "" ):
4334+ self .__executeCommand ("tcp init" )
4335+ self .__executeCommand ("tcp connect %s %s" % (addr , port ))
4336+ time .sleep (3 )
4337+ tcp_request = self .__executeCommand (
4338+ "tcp send -x %s" % hexa_http_request , ignore_exec = True
4339+ )
4340+ print (tcp_request )
4341+ time .sleep (3 )
4342+ self .__executeCommand ("tcp sendend" , ignore_exec = True )
4343+ self .__executeCommand ("tcp deinit" , ignore_exec = True )
4344+ return "Failed" if tcp_request is True else True
4345+
4346+ def nat64_prefix (self ):
4347+ netdata = self .__executeCommand ("netdata show" )
4348+ nat64 = ""
4349+ for addr in netdata :
4350+ if "/96" in addr :
4351+ nat64 = addr
4352+ break
4353+ return nat64
4354+
4355+ def pd_omr (self , prefix ):
4356+ pd = self .__executeCommand ("ipaddr" )
4357+ result = ""
4358+ for pd_omr in pd [:- 1 ]:
4359+ if ModuleHelper .GetFullIpv6Address (pd_omr ).startswith (prefix ):
4360+ result = pd_omr
4361+ break
4362+ else :
4363+ continue
4364+ if result == "" :
4365+ prefix = ModuleHelper .GetFullIpv6Address (prefix + "::" )
4366+ result = prefix
4367+ return result
42564368
42574369 ########################################################################################################################
42584370 # 1.3 THCI Commands
0 commit comments