1#!/usr/bin/env python3
2#
3#   Copyright 2016 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16"""
17Utility functions for for Anritsu Signalling Tester.
18"""
19# yapf: disable
20
21OPERATION_COMPLETE = 1
22NO_ERROR = 0
23
24ANRITSU_ERROR_CODES = {
25    0: 'No errors occurred',
26    2: 'The specified file does not exist',
27    14: 'The buffer size is insufficient',
28    29: 'The save destination is a write-protected file.',
29    80: 'A file with the same name already exists.'
30        ' (If Overwrite is specified to 0.)',
31    87: 'The specified value is wrong.',
32    112: 'The disk space is insufficient.',
33    183: 'SmartStudio is already running.',
34    1060: 'The control software has not been started or has already terminated',
35    1067: 'SmartStudio, control software or SMS Centre could not start due to'
36          'a problem or problems resulting from OS or the MD8475A system.',
37    1229: 'Connecting to the server failed.',
38    1235: 'A request is suspended.',
39    1460: 'The operation is terminated due to the expiration of the'
40          ' timeout period.',
41    9999: 'A GPIB command error occurred.',
42    536870912: 'The license could not be confirmed.',
43    536870913: 'The specified file cannot be loaded by the SmartStudio.',
44    536870914: 'The specified process ID does not exist.',
45    536870915: 'The received data does not exist.',
46    536870916: 'Simulation is not running.',
47    536870917: 'Simulation is running.',
48    536870918: 'Test Case has never been executed.',
49    536870919: 'The resource cannot be obtained.',
50    536870920: 'A resource protocol error, such as download error or'
51               ' license error, occurred.',
52    536870921: 'The function call has been in invalid status.',
53    536870922: 'The current Simulation Model does not allow the operation.',
54    536870923: 'The Cell name to be set does not exist.',
55    536870924: 'The test is being executed.',
56    536870925: 'The current UE status does not correspond to the'
57               ' test parameters.',
58    536870926: 'There is no LOG information because the simulation'
59               ' has not been executed.',
60    536870927: 'Measure Export has already been executed.',
61    536870928: 'SmartStudio is not connected to the SMS Centre.',
62    536870929: 'SmartStudio failed to send an SMS message to the SMS Centre.',
63    536870930: 'SmartStudio has successfully sent an SMS message'
64               ' to the SMS Centre,but the SMS Centre judges it as an error.',
65    536870931: 'The processing that is unavailable with the current system'
66               ' status has been executed.',
67    536870932: 'The option could not be confirmed.',
68    536870933: 'Measure Export has been stopped.',
69    536870934: 'SmartStudio cannot load the specified file because the'
70               ' version is old.',
71    536870935: 'The data with the specified PDN number does not exist.',
72    536870936: 'The data with the specified Dedicated number does not exist.',
73    536870937: 'The PDN data cannot be added because the upper limit of the'
74               ' number of PDN data has been reached.',
75    536870938: 'The number of antennas, which cannot be set to the current'
76               ' Simulation Model,has been specified.',
77    536870939: 'Calibration of path loss failed.',
78    536870940: 'There is a parameter conflict.',
79    536870941: 'The DL Ref Power setting is out of the setting range'
80               ' at W-CDMA (Evolution).',
81    536870942: 'DC-HSDPA is not available for the current channel setting.',
82    536870943: 'The specified Packet Rate cannot be used by the current'
83               ' Simulation Model.',
84    536870944: 'The W-CDMA Cell parameter F-DPCH is set to Enable.',
85    536870945: 'Target is invalid.',
86    536870946: 'The PWS Centre detects an error.',
87    536870947: 'The Ec/Ior setting is invalid.',
88    536870948: 'The combination of Attach Type and TA Update Type is invalid.',
89    536870949: 'The license of the option has expired.',
90    536870950: 'The Ping command is being executed.',
91    536870951: 'The Ping command is not being executed.',
92    536870952: 'The current Test Case parameter setting is wrong.',
93    536870953: 'The specified IP address is the same as that of Default Gateway'
94               'specified by Simulation parameter.',
95    536870954: 'TFT IE conversion failed.',
96    536870955: 'Saving settings to the SmartStudio scenario failed.',
97    536875008: 'An error exists in the parameter configuration.'
98               '(This error applies only to the current version.)',
99    536936448: 'License verification failed.',
100    536936449: 'The IMS Services cannot load the specified file.',
101    536936462: 'Simulation is not performed and no log information exists.',
102    536936467: 'The executed process is inoperable in the current status'
103               ' of Visual User Agent.',
104    536936707: 'The specified Virtual Network is not running.',
105    536936709: 'The specified Virtual Network is running. '
106               'Any one of the Virtual Networks is running.',
107    536936727: 'The specified Virtual Network does not exist.',
108    536936729: 'When the Virtual Network already exists.',
109    554762241: 'The RF Measurement launcher cannot be accessed.',
110    554762242: 'License check of the RF Measurement failed.',
111    554762243: 'Function is called when RF Measurement cannot be set.',
112    554762244: 'RF Measurement has been already started.',
113    554762245: 'RF Measurement failed to start due to a problem resulting'
114               ' from OS or the MD8475A system.',
115    554762246: 'RF Measurement is not started or is already terminated.',
116    554762247: 'There is a version mismatch between RF Measurement and CAL.',
117    554827777: 'The specified value for RF Measurement is abnormal.',
118    554827778: 'GPIB command error has occurred in RF Measurement.',
119    554827779: 'Invalid file path was specified to RF Measurement.',
120    554827780: 'RF Measurement argument is NULL pointer.',
121    555810817: 'RF Measurement is now performing the measurement.',
122    555810818: 'RF Measurement is now not performing the measurement.',
123    555810819: 'RF Measurement is not measured yet. (There is no result '
124               'information since measurement is not performed.)',
125    555810820: 'An error has occurred when RF Measurement'
126               ' starts the measurement.',
127    555810821: 'Simulation has stopped when RF Measurement is '
128               'performing the measurement.',
129    555810822: 'An error has been retrieved from the Platform when '
130               'RF Measurement is performing the measurement.',
131    555810823: 'Measurement has been started in the system state where RF '
132               'Measurement is invalid.',
133    556859393: 'RF Measurement is now saving a file.',
134    556859394: 'There is insufficient disk space when saving'
135               'a Measure Result file of RF Measurement.',
136    556859395: 'An internal error has occurred or USB cable has been'
137               ' disconnected when saving a Measure Result'
138               ' file of RF Measurement.',
139    556859396: 'A write-protected file was specified as the save destination'
140               ' when saving a Measure Result file of RF Measurement.',
141    568328193: 'An internal error has occurred in RF Measurement.',
142    687865857: 'Calibration Measure DSP is now being measured.',
143    687865858: 'Calibration measurement failed.',
144    687865859: 'Calibration slot is empty or its system does not apply.',
145    687865860: 'Unexpected command is received from Calibration HWC.',
146    687865861: 'Failed to receive the Calibration measurement result.',
147    687865862: 'Failed to open the correction value file on the'
148               ' Calibration HDD.',
149    687865863: 'Failed to move the pointer on the Calibration correction'
150               ' value table.',
151    687865864: 'Failed to write the correction value to the Calibration'
152               ' correction value file on the Calibration HDD.',
153    687865865: 'Failed to load the correction value from the Calibration HDD.',
154    687865866: 'Failed to create a directory to which the correction value '
155               'file on the Calibration HDD is saved.',
156    687865867: 'Correction data has not been written in the'
157               ' Calibration-specified correction table.',
158    687865868: 'Data received from Calibration HWC does not exist.',
159    687865869: 'Data has not been written to the Flash ROM'
160               ' of Calibration BASE UNIT.',
161    687865870: 'Correction data has not been written to the'
162               ' Calibration-specified sector.',
163    687866111: 'An calibration error other than described above occurred.',
164}
165
166
167def _error_code_tostring(error_code):
168    ''' returns the description of the error from the error code
169    returned by anritsu MD8475A '''
170    try:
171        error_string = ANRITSU_ERROR_CODES[error_code]
172    except KeyError:
173        error_string = "Error : {} ".format(error_code)
174
175    return error_string
176
177
178class AnritsuUtils(object):
179    def gsm_encode(text):
180        '''To encode text string with GSM 7-bit alphabet for common symbols'''
181        table = {' ': '%20', '!': '%21', '\"': '%22', '#': '%23', '$': '%24',
182                 '/': '%2F', '%': '%25', '&': '%26', '\'': '%27', '(': '%28',
183                 ')': '%29', '*': '%2A', '+': '%2B', ',': '%2C', ':': '%3A',
184                 ';': '%3B', '<': '%3C', '=': '%3D', '>': '%3E', '?': '%3F',
185                 '@': '%40', '[': '%5B', ']': '%5D', '_': '%5F', 'é': '%C3%A9'}
186        coded_str = ""
187        for char in text:
188            if char in table:
189                coded_str += table[char]
190            else:
191                coded_str += char
192        return coded_str
193
194    def gsm_decode(text):
195        '''To decode text string with GSM 7-bit alphabet for common symbols'''
196        table = {'%20': ' ', '%21': '!', '%22': '\"', '%23': '#', '%24': '$',
197                 '%2F': '/', '%25': '%', '%26': '&', '%27': '\'', '%28': '(',
198                 '%29': ')', '%2A': '*', '%2B': '+', '%2C': ',', '%3A': ':',
199                 '%3B': ';', '%3C': '<', '%3D': '=', '%3E': '>', '%3F': '?',
200                 '%40': '@', '%5B': '[', '%5D': ']', '%5F': '_', '%C3%A9': 'é'}
201        coded_str = text
202        for char in table:
203            if char in text:
204                coded_str = coded_str.replace(char, table[char])
205        return coded_str
206
207    def cdma_encode(text):
208        '''To encode text string with GSM 7-bit alphabet for common symbols'''
209        table = {' ': '%20', '!': '%21', '\"': '%22', '#': '%23', '$': '%24',
210                 '/': '%2F', '%': '%25', '&': '%26', '\'': '%27', '(': '%28',
211                 ')': '%29', '*': '%2A', '+': '%2B', ',': '%2C', ':': '%3A',
212                 ';': '%3B', '<': '%3C', '=': '%3D', '>': '%3E', '?': '%3F',
213                 '@': '%40', '[': '%5B', ']': '%5D', '_': '%5F'}
214        coded_str = ""
215        for char in text:
216            if char in table:
217                coded_str += table[char]
218            else:
219                coded_str += char
220        return coded_str
221
222class AnritsuError(Exception):
223    '''Exception for errors related to Anritsu.'''
224    def __init__(self, error, command=None):
225        self._error_code = error
226        self._error_message = _error_code_tostring(self._error_code)
227        if command is not None:
228            self._error_message = "Command {} returned the error: '{}'".format(
229                                  command, self._error_message)
230
231    def __str__(self):
232        return self._error_message
233# yapf: enable
234