takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ip_test_parsers.py Source File

ip_test_parsers.py

00001 """
00002 Copyright 2018 ARM Limited
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006 
00007     http://www.apache.org/licenses/LICENSE-2.0
00008 
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License.
00014 """
00015 import re
00016 import time
00017 from collections import OrderedDict
00018 from datetime import datetime
00019 
00020 from icetea_lib.Plugin.PluginBase import PluginBase
00021 
00022 
00023 class IpTestParsers(PluginBase):
00024     # constructor
00025     def __init__(self):
00026         super(IpTestParsers, self).__init__()
00027 
00028     def get_parsers(self):
00029         return {
00030             'ifconfig': self.__ifconfigParser,
00031             'ifup': self.__ifconfigParser,
00032             'ethup': self.__ifconfigParser,
00033             'dut1': self.__ifconfigParser,
00034             'dut2': self.__ifconfigParser,
00035             'dut3': self.__ifconfigParser,
00036             'dut4': self.__ifconfigParser,
00037             'dut5': self.__ifconfigParser,
00038             'dut6': self.__ifconfigParser,
00039             'socket': self.__mbedossocketParser,
00040             'ticker': self.__ticker_parser
00041         }
00042 
00043     # socket command for mbedos
00044     def __mbedossocketParser(self, response):
00045         results = {'socket_id': None,
00046                    'data_type': None,
00047                    'data': "",
00048                    'printed_bytes': None,
00049                    'sent_bytes': None,
00050                    'received_bytes': None,
00051                    'address': None,
00052                    'port': None,
00053                    'loop_id': None
00054                    }
00055         respLines = response.lines
00056         part = None
00057         for line in respLines:
00058             ret = PluginBase.find_one(line, ".*sid: ([0-9]+)")
00059             if ret is not False:
00060                 results['socket_id'] = ret
00061 
00062             ret = PluginBase.find_one(line, ".*(hex|string) data, printing .* bytes:")
00063             if ret is not False:
00064                 results['data_type'] = ret
00065 
00066             ret = PluginBase.find_one(line, ".*data, printing (.*) bytes:")
00067             if ret is not False:
00068                 part = "data"
00069 
00070             ret = PluginBase.find_one(line, "^Printed (.*) bytes$")
00071             if ret is not False:
00072                 results['printed_bytes'] = int(ret)
00073                 part = None
00074 
00075             if part == "data":
00076                 ret = PluginBase.find_one(line, "^\d{4}:  (.*)$")
00077                 if ret is not False:
00078                     results['data'] = results['data'] + ret
00079 
00080             ret = PluginBase.find_one(line, ".*sent: ([0-9]+) bytes")
00081             if ret is not False:
00082                 results['sent_bytes'] = int(ret)
00083 
00084             ret = PluginBase.find_one(line, ".*received: ([0-9]+) bytes")
00085             if ret is not False:
00086                 results['received_bytes'] = int(ret)
00087 
00088             ret = PluginBase.find_one(line, ".*address: ([0-9a-fxA-F:.]+)")
00089             if ret is not False:
00090                 results['address'] = ret
00091 
00092             ret = PluginBase.find_one(line, ".*port: ([0-9]+)")
00093             if ret is not False:
00094                 results['port'] = ret
00095 
00096             ret = PluginBase.find_one(line, ".*lid: ([0-9]+)")
00097             if ret is not False:
00098                 results['loop_id'] = ret
00099 
00100         return results
00101 
00102     # response parser for ifup
00103     def __ifconfigParser(self, response):
00104         results = {}
00105         lines = response.traces
00106         part = None
00107 
00108         results['address'] = {
00109             'll': '',
00110             'globals': [],
00111             'ipv4': None,
00112             'ipv6': []
00113         }
00114 
00115         for line in lines:
00116             # print "ifcfgparser: %s" % line
00117             match = re.search('IPv6 if addr', line)
00118             if match:
00119                 part = "address"
00120             match = re.search('IPv4 if addr', line)
00121             if match:
00122                 part = "address"
00123             match = re.search('MAC-48\:[\W]{1,}(.*)', line)
00124             if match:
00125                 mac48 = match.group(1)
00126                 # Validate the MAC-48 syntax as well
00127                 match = re.search("([0-9a-fA-F]{2}:??){5}([0-9a-fA-F]{2})", mac48)
00128                 if match:
00129                     results['MAC'] = mac48
00130 
00131             if part == "address":
00132                 match = re.search('.*\[(\d)\]:\W([abcdefg\d\:]{5,})', line)
00133                 if match:
00134                     addr = match.group(2)
00135                     if re.search('^fe80\:\:', addr):
00136                         results['address']['ll'] = addr
00137                     else:
00138                         results['address']["globals"].append(addr)
00139 
00140                 match = re.search('\[(\d)\]:\W([a-fA-F\d\:]{5,})', line)
00141                 if match:
00142                     results['address']['ipv6'].append(match.group(2))
00143 
00144                 match = re.search('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$', line)
00145                 if match:
00146                     results['address']['ipv4'] = match.group(1)
00147 
00148         return results
00149 
00150     def __ticker_parser(self, response):
00151         results = {}
00152         respLines = response.lines
00153         for line in respLines:
00154             ret = PluginBase.find_one(line, 'Ticker id: ([0-9]+)')
00155             if ret is not False:
00156                 results['ticker_id'] = ret
00157         return results