blob: 0c17a3d29e3ffc66156c8845e212a1bfad820b33 [file] [log] [blame]
Orhan Kupusogluec57af02021-05-12 12:38:17 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <string>
18#include <cstring>
19#include <iostream>
20#include <fstream>
21#include <sstream>
22#include <cstdio>
23#include <cstdlib>
24#include <cmath>
25#include <iomanip>
26#include <stdexcept>
27
28#include "trx_eeprom_reader.h"
29
30// g++ -std=c++11 -DRXTX_POWER_EXE_MODE trx_eeprom_reader.cc -o rssi
31
32TrxEepromReader::TrxEepromReader(device_type dev_type, const power_type read_type, const int port)
33 : _read_type{read_type},
34 _port{port} {
35
36 if (dev_type == DEVICE_EITHER) {
37 std::string device = TrxEepromReader::get_board_name();
38 if (device == "ASGvOLT64") {
39 dev_type = TrxEepromReader::DEVICE_GPON;
40 } else if (device == "ASXvOLT16") {
41 dev_type = TrxEepromReader::DEVICE_XGSPON;
42 } else { // improbable
43 std::cerr << "ERROR - unknown device: '" << device << "'\n";
44 dev_type = TrxEepromReader::DEVICE_XGSPON;
45 }
46 }
47
48 switch (dev_type) {
49 case DEVICE_GPON:
50 _dev_name = "ASGvOLT64";
51 _buf_size = 600;
52 _read_offset = 360,
53 _read_num_bytes = 2;
54 _port_addr = 50;
55 _name_eeprom = "eeprom";
56 _bus_index = _gpon_bus_index;
57 _max_ports = sizeof(_gpon_bus_index) / sizeof(_gpon_bus_index[0]);
58 break;
59
60 case DEVICE_XGSPON:
61 _dev_name = "ASXvOLT16";
62 _buf_size = 256;
63 _read_offset = 104,
64 _read_num_bytes = 2;
65 _port_addr = 50;
66 _name_eeprom = "sfp_eeprom";
67 _bus_index = _xgspon_bus_index;
68 _max_ports = sizeof(_xgspon_bus_index) / sizeof(_xgspon_bus_index[0]);
69 break;
70 }
71
72 // trick: convert the first string specifier to integer specifier
73 sprintf(_node_format, _master_format, "%d", _port_addr, _name_eeprom.c_str());
74}
75
76std::pair<std::string, bool> TrxEepromReader::read_txt_file(const std::string& file_name) {
77 std::ifstream in_file(file_name);
78
79 if (!in_file.is_open()) {
80 std::cerr << "error while opening file '" << file_name << "'\n";
81 return {"", false};
82 }
83
84 std::stringstream buffer;
85 buffer << in_file.rdbuf();
86
87 return {buffer.str(), in_file.good()};
88}
89
90std::string TrxEepromReader::get_board_name() {
91 const std::string board_path = "/sys/devices/virtual/dmi/id/board_name";
92 auto res = TrxEepromReader::read_txt_file(board_path);
93
94 // read failure is improbable
95 if (!res.second) {
96 std::cerr << "ERROR - file " << board_path << "cannot be opened\n";
97 }
98
99 if (res.first.find_last_of("\n") != std::string::npos) {
100 res.first.pop_back();
101 }
102
103 return res.first;
104}
105
106int TrxEepromReader::read_binary_file(char* buffer) {
107 std::ifstream is;
108 int len;
109
110 if (buffer == NULL || _buf_size < 0) {
111 return -1;
112 }
113
114#ifdef TEST_MODE
115 is.open("./eeprom.bin", std::ios_base::in | std::ios_base::binary);
116#else
117 is.open(_node_path, std::ios_base::in | std::ios_base::binary);
118#endif
119
120 if (!is.is_open()) {
121 return -2;
122 }
123
124 is.read(buffer, _buf_size);
125 len = is.gcount();
126 is.close();
127
128 if (len > _buf_size || len < (_read_offset + _read_num_bytes)) {
129 return -4;
130 }
131
132 return len;
133}
134
135bool TrxEepromReader::is_valid_port() const {
136 return (_port >= 0 && (size_t)_port < _max_ports);
137}
138
139void TrxEepromReader::set_port_path() {
140 sprintf(_node_path, _node_format, _bus_index[_port]);
141}
142
143unsigned long TrxEepromReader::get_value_from_pointer_u(unsigned char *ptr, int size) {
144 unsigned long sum = 0;
145 unsigned char i;
146
147 if (size > 4) {
148 return (sum);
149 }
150
151 for (i = 0; i < size; ++i) {
152 sum = sum * 256 + (*(ptr + i));
153 }
154
155 return (sum);
156}
157
158double TrxEepromReader::raw_rx_to_mw(int raw) {
159 return raw * 0.0001;
160}
161
162double TrxEepromReader::raw_tx_to_mw(int raw) {
163 return raw * 0.0002;
164}
165
166double TrxEepromReader::mw_to_dbm(double mw) {
167 return 10 * log10(mw);
168}
169
170std::pair<std::pair<int, int>, bool> TrxEepromReader::read_power_raw() {
171 if (is_valid_port()) {
172 set_port_path();
173
174 char* eeprom_data = new (std::nothrow) char[_buf_size];
175
176 if (!eeprom_data) {
177 std::cerr << "ERROR - memory allocation for eeprom_data failed\n";
178 return {{0, 0}, false};
179 }
180
181 int ret_file = read_binary_file(eeprom_data);
182
183 if (ret_file < 0) {
184 std::cerr << "ERROR - eeprom_data file cannot be opened\n";
185 return {{0, 0}, false};
186 }
187
188 // le = Little Endian, be = Big Endian
189 unsigned char rx_power_le[_read_num_bytes] = {0};
190 unsigned char tx_power_le[_read_num_bytes] = {0};
191 unsigned long rx_power_be = 0;
192 unsigned long tx_power_be = 0;
193
194 if (_read_type == RX_POWER || _read_type == RX_AND_TX_POWER) {
195 memcpy(&rx_power_le, eeprom_data + _read_offset, _read_num_bytes);
196 rx_power_be = get_value_from_pointer_u(rx_power_le, _read_num_bytes);
197 }
198
199 if (_read_type == TX_POWER || _read_type == RX_AND_TX_POWER) {
200 memcpy(&tx_power_le, eeprom_data + _read_offset - _read_num_bytes, _read_num_bytes);
201 tx_power_be = get_value_from_pointer_u(tx_power_le, _read_num_bytes);
202 }
203
204 if (_read_type == RX_POWER) {
205 tx_power_be = 0;
206 }
207
208 if (_read_type == TX_POWER) {
209 rx_power_be = 0;
210 }
211
212 delete[] eeprom_data;
213
214 return {{(int)rx_power_be, (int)tx_power_be}, true};
215 } else {
216 std::cerr << "ERROR - invalid port: " << _port << '\n';
217 return {{0, 0}, false};
218 }
219}
220
221std::pair<std::pair<double, double>, bool> TrxEepromReader::read_power_mean_dbm() {
222 auto power_raw = read_power_raw();
223 if (power_raw.second) {
224 return {{mw_to_dbm(raw_rx_to_mw(power_raw.first.first)), mw_to_dbm(raw_tx_to_mw(power_raw.first.second))}, true};
225 } else {
226 return {{0.0, 0.0}, false};
227 }
228}
229
230std::string TrxEepromReader::dump_data() {
231 std::ostringstream dump;
232
233 dump << "\tdevice: " << _dev_name << '\n'
234 << "\tbuffer size: " << _buf_size << '\n'
235 << "\tread offset: " << _read_offset << '\n'
236 << "\tnum bytes: " << _read_num_bytes << '\n'
237 << "\tmax ports: " << _max_ports << '\n'
238 << "\tport: " << _port << '\n';
239
240 //dump << std::showpos;
241 dump << std::fixed;
242 dump << std::setprecision(3);
243
244 auto rxtx_power_raw = this->read_power_raw();
245
246 dump << "\tnode path: " << _node_path << '\n';
247
248 if (rxtx_power_raw.second) {
249 if (_read_type == RX_POWER || _read_type == RX_AND_TX_POWER) {
250 dump << "\tRx power - raw: (hex) " << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << rxtx_power_raw.first.first
251 << ", (dec) " << std::dec << std::setfill(' ') << std::setw(5) << rxtx_power_raw.first.first
252 << " | " << std::dec << std::fixed << std::setprecision(5) << std::setw(8) << raw_rx_to_mw(rxtx_power_raw.first.first) << " mW, "
253 << std::dec << std::setw(9) << mw_to_dbm(raw_rx_to_mw(rxtx_power_raw.first.first)) << " dBm\n";
254 }
255 if (_read_type == TX_POWER || _read_type == RX_AND_TX_POWER) {
256 dump << "\tTx power - raw: (hex) " << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << rxtx_power_raw.first.second
257 << ", (dec) " << std::dec << std::setfill(' ') << std::setw(5) << rxtx_power_raw.first.second
258 << " | " << std::dec << std::fixed << std::setprecision(5) << std::setw(8) << raw_tx_to_mw(rxtx_power_raw.first.second) << " mW, "
259 << std::dec << std::setw(9) << mw_to_dbm(raw_tx_to_mw(rxtx_power_raw.first.second)) << " dBm\n";
260 }
261 }
262
263 return dump.str();
264}
265
266int TrxEepromReader::get_buf_size() const {
267 return _buf_size;
268}
269
270int TrxEepromReader::get_read_offset() const {
271 return _read_offset;
272}
273
274int TrxEepromReader::get_read_num_bytes() const {
275 return _read_num_bytes;
276}
277
278int TrxEepromReader::get_max_ports() const {
279 return _max_ports;
280}
281
282const char* TrxEepromReader::get_node_path() const {
283 return _node_path;
284}
285
286#ifdef RXTX_POWER_EXE_MODE
287int main(int argc, char *argv[]) {
288 int port = 0;
289
290 if (argc > 1) {
291 std::string help{argv[1]};
292
293 // trim leading '-'s
294 do {
295 if (help.find_first_of("-") == std::string::npos) {
296 break;
297 } else {
298 help.erase(0, 1);
299 }
300 } while (true);
301
302 if (help == "h" || help == "help") {
303 std::cout << "usage:\n\t" << argv[0] << " " << port << '\n';
304 std::cout << "\t#1 port no\n";
305 return 0;
306 }
307
308 try {
309 port = std::stoi(argv[1]);
310 } catch(std::invalid_argument eia) {
311 std::cerr << "ERROR - invalid argument exception: '" << argv[1] << "'\n";
312 return 1;
313 } catch(std::out_of_range eoor) {
314 std::cerr << "ERROR - out of range exception: '" << argv[1] << "'\n";
315 return 1;
316 }
317 }
318
319 TrxEepromReader trx_eeprom_reader{TrxEepromReader::DEVICE_EITHER, TrxEepromReader::RX_AND_TX_POWER, port};
320
321 std::cout << trx_eeprom_reader.dump_data() << '\n';
322
323 return 0;
324}
325#endif