1 /*! \file tx_pkt_example.py
6 """tx_pkt_example.py: OpEN API Packet Transmit example"""
36 def print_sanity_results(result, test, msg, feat) :
37 """Print overall comparison results"""
39 if result == OpEN.OPEN_E_UNAVAIL:
40 print "Sanity test skipped."
41 elif result == OpEN.OPEN_E_EXISTS:
42 print "Sanity Skipped (already exists) - %s - %s." % (msg, feat)
43 elif result == OpEN.OPEN_E_NONE
and test ==
True:
44 print "Sanity Success - %s - %s." % (msg, feat)
46 print "Sanity Failure - %s - %s." % (msg, feat)
48 def print_bad_result(result, msg) :
49 """Print some general error messages if the result is bad"""
51 if result == OpEN.OPEN_E_UNAVAIL:
52 print "Feature not supported - %s (err %d)." % (msg, result)
53 elif result == OpEN.OPEN_E_NOT_FOUND:
54 print "Test Skipped (not found) - ", msg
55 elif result != OpEN.OPEN_E_NONE:
56 print "Test Failure - %s (err %d)." % (msg, result)
59 """Packet transmission """
61 def __init__(self, client) :
62 self.m_client = client
64 def test_tx_pkt(self, intf) :
65 """Send a packet using Unicast transmission"""
67 intf_max_p = OpEN.new_uint32_tp()
69 result = OpEN.openapiMaxInterfaceCountGet(self.m_client, intf_max_p)
70 if result == OpEN.OPEN_E_NONE:
71 max_intf = OpEN.uint32_tp_value(intf_max_p)
72 if intf < 1
or intf > max_intf:
73 print "Invalid interface. The available range is 1 to %d." % max_intf
74 OpEN.delete_uint32_tp(intf_max_p)
77 OpEN.delete_uint32_tp(intf_max_p)
81 pkt_data =
"\x00\x01\x02\x03\x04\x05"
82 pkt_data +=
"\x06\x07\x08\x09\x0a\x0b"
83 pkt_data +=
"\xcc" * (PKT_LEN-len(pkt_data))
86 pkt_string = open_.getByteBuffer(PKT_LEN, pkt_data)
87 except OpENBufferSizeError:
88 print(
"test_tx_pkt: getByteBuffer raised OpENBufferSizeError")
91 print(
"test_tx_pkt: getByteBuffer raised TypeError")
93 pkt_buff = OpEN.open_buffdesc()
94 pkt_buff.pstart = pkt_string
95 pkt_buff.size = PKT_LEN
97 print "Send Packet..."
98 result = OpEN.openapiExtPktTransmit(self.m_client, pkt_buff, OpEN.OPEN_EXT_PKT_TX_L2RAW_UNICAST, intf, 0, 0)
99 print_bad_result(result,
'openapiExtPktTransmit')
100 print_sanity_results(result, (1 == 1),
'openapiExtPktTransmit',
'ifnum %d' % intf)
101 OpEN.delete_uint32_tp(intf_max_p)
104 """Demonstrate OpEN usage for Packet API"""
110 print 'Invalid interface, <ifnum> must be numeric.'
113 print 'tx_pkt_example.py <ifnum>'
116 ret = open_.connect(
"tx_pkt_example")
117 if ret == OpEN.OPEN_E_NONE :
118 open_.getNetworkOSVersion()
119 client = open_.get_client()
120 example = TxPktExample(client)
121 example.test_tx_pkt(intf)
124 print "Unable to connect"
126 if __name__ ==
'__main__':