ltq-vdsl-app: prepare for multiple mei ioctls
[openwrt/staging/chunkeey.git] / package / network / config / ltq-vdsl-app / src / src / dsl_cpe_ubus.c
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2020 Andre Heider <a.heider@gmail.com>
4 */
5
6 #include <sys/ioctl.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <fcntl.h>
10 #include <libubus.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "dsl_cpe_control.h"
17 #include <drv_dsl_cpe_api_ioctl.h>
18 #ifdef INCLUDE_DSL_CPE_API_VRX
19 #include <drv_mei_cpe_interface.h>
20 #endif
21
22 #define U16(v1, v2) ( \
23 ((uint16_t)(v1) << 8) | \
24 ((uint16_t)(v2)))
25
26 #define U32(v1, v2, v3, v4) ( \
27 ((uint32_t)(v1) << 24) | \
28 ((uint32_t)(v2) << 16) | \
29 ((uint32_t)(v3) << 8) | \
30 ((uint32_t)(v4)))
31
32 #define STR_CASE(id, text) \
33 case id: \
34 str = text; \
35 break;
36
37 #define STR_CASE_MAP(id, text, number) \
38 case id: \
39 str = text; \
40 map = number; \
41 break;
42
43 #define IOCTL(type, request) \
44 type out; \
45 memset(&out, 0, sizeof(type)); \
46 if (ioctl(fd, request, &out)) \
47 return;
48
49 #define IOCTL_DIR(type, request, dir) \
50 type out; \
51 memset(&out, 0, sizeof(type)); \
52 out.nDirection = dir; \
53 if (ioctl(fd, request, &out)) \
54 return;
55
56 #define IOCTL_DIR_DELT(type, request, dir, delt) \
57 type out; \
58 memset(&out, 0, sizeof(type)); \
59 out.nDirection = dir; \
60 out.nDeltDataType = delt; \
61 if (ioctl(fd, request, &out)) \
62 return;
63
64 typedef enum {
65 ANNEX_UNKNOWN = 0,
66 ANNEX_A,
67 ANNEX_B,
68 ANNEX_C,
69 ANNEX_I,
70 ANNEX_J,
71 ANNEX_L,
72 ANNEX_M,
73 } annex_t;
74
75 typedef enum {
76 STD_UNKNOWN = 0,
77 STD_T1_413,
78 STD_TS_101_388,
79 STD_G_992_1,
80 STD_G_992_2,
81 STD_G_992_3,
82 STD_G_992_4,
83 STD_G_992_5,
84 STD_G_993_1,
85 STD_G_993_2,
86 } standard_t;
87
88 typedef enum {
89 VECTOR_UNKNOWN = 0,
90 VECTOR_OFF,
91 VECTOR_ON_DS,
92 VECTOR_ON_DS_US,
93 } vector_t;
94
95 typedef enum {
96 PROFILE_UNKNOWN = 0,
97 PROFILE_8A,
98 PROFILE_8B,
99 PROFILE_8C,
100 PROFILE_8D,
101 PROFILE_12A,
102 PROFILE_12B,
103 PROFILE_17A,
104 PROFILE_30A,
105 PROFILE_35B,
106 } profile_t;
107
108 /* These values are exported via ubus and backwards compability
109 * needs to be kept!
110 */
111 enum {
112 LSTATE_MAP_UNKNOWN = -1,
113 LSTATE_MAP_NOT_INITIALIZED,
114 LSTATE_MAP_EXCEPTION,
115 LSTATE_MAP_IDLE,
116 LSTATE_MAP_SILENT,
117 LSTATE_MAP_HANDSHAKE,
118 LSTATE_MAP_FULL_INIT,
119 LSTATE_MAP_SHOWTIME_NO_SYNC,
120 LSTATE_MAP_SHOWTIME_TC_SYNC,
121 LSTATE_MAP_RESYNC,
122 };
123
124 /* These values are exported via ubus and backwards compability
125 * needs to be kept!
126 */
127 enum {
128 PSTATE_MAP_UNKNOWN = -2,
129 PSTATE_MAP_NA,
130 PSTATE_MAP_L0,
131 PSTATE_MAP_L1,
132 PSTATE_MAP_L2,
133 PSTATE_MAP_L3,
134 };
135
136 static DSL_CPE_ThreadCtrl_t thread;
137 static struct ubus_context *ctx;
138 static struct blob_buf b;
139
140 static inline void m_double(const char *id, double value) {
141 blobmsg_add_double(&b, id, value);
142 }
143
144 static inline void m_bool(const char *id, bool value) {
145 blobmsg_add_u8(&b, id, value);
146 }
147
148 static inline void m_u32(const char *id, uint32_t value) {
149 blobmsg_add_u32(&b, id, value);
150 }
151
152 static inline void m_str(const char *id, const char *value) {
153 blobmsg_add_string(&b, id, value);
154 }
155
156 static inline void m_db(const char *id, int value) {
157 m_double(id, (double)value / 10);
158 }
159
160 static inline void m_array(const char *id, const uint8_t *value, uint8_t len) {
161 void *c = blobmsg_open_array(&b, id);
162
163 for (uint8_t i = 0; i < len; ++i)
164 blobmsg_add_u16(&b, "", value[i]);
165
166 blobmsg_close_array(&b, c);
167 }
168
169 static void m_vendor(const char *id, const uint8_t *value) {
170 // ITU-T T.35: U.S.
171 if (U16(value[0], value[1]) != 0xb500)
172 return;
173
174 const char *str = NULL;
175 switch (U32(value[2], value[3], value[4], value[5])) {
176 STR_CASE(0x414C4342, "Alcatel")
177 STR_CASE(0x414E4456, "Analog Devices")
178 STR_CASE(0x4244434D, "Broadcom")
179 STR_CASE(0x43454E54, "Centillium")
180 STR_CASE(0x4753504E, "Globespan")
181 STR_CASE(0x494B4E53, "Ikanos")
182 STR_CASE(0x4946544E, "Infineon")
183 STR_CASE(0x54535443, "Texas Instruments")
184 STR_CASE(0x544D4D42, "Thomson MultiMedia Broadband")
185 STR_CASE(0x5443544E, "Trend Chip Technologies")
186 STR_CASE(0x53544D49, "ST Micro")
187 };
188
189 if (!str)
190 return;
191
192 if ((value[6] == 0) && (value[7] == 0)) {
193 m_str(id, str);
194 return;
195 }
196
197 char buf[64];
198 sprintf(buf, "%s %d.%d", str, value[6], value[7]);
199 m_str(id, buf);
200
201 return;
202 }
203
204 annex_t get_annex(const uint8_t *xtse) {
205 if ((xtse[0] & XTSE_1_01_A_T1_413) ||
206 (xtse[0] & XTSE_1_03_A_1_NO) ||
207 (xtse[0] & XTSE_1_04_A_1_O) ||
208 (xtse[1] & XTSE_2_01_A_2_NO) ||
209 (xtse[2] & XTSE_3_03_A_3_NO) ||
210 (xtse[2] & XTSE_3_04_A_3_O) ||
211 (xtse[3] & XTSE_4_01_A_4_NO) ||
212 (xtse[3] & XTSE_4_02_A_4_O) ||
213 (xtse[5] & XTSE_6_01_A_5_NO) ||
214 (xtse[5] & XTSE_6_02_A_5_O) ||
215 (xtse[7] & XTSE_8_01_A))
216 return ANNEX_A;
217
218 if ((xtse[0] & XTSE_1_05_B_1_NO) ||
219 (xtse[0] & XTSE_1_06_B_1_O) ||
220 (xtse[1] & XTSE_2_02_B_2_O) ||
221 (xtse[2] & XTSE_3_05_B_3_NO) ||
222 (xtse[2] & XTSE_3_06_B_3_O) ||
223 (xtse[5] & XTSE_6_03_B_5_NO) ||
224 (xtse[5] & XTSE_6_04_B_5_O) ||
225 (xtse[7] & XTSE_8_02_B))
226 return ANNEX_B;
227
228 if ((xtse[0] & XTSE_1_02_C_TS_101388) ||
229 (xtse[0] & XTSE_1_07_C_1_NO) ||
230 (xtse[0] & XTSE_1_08_C_1_O) ||
231 (xtse[1] & XTSE_2_03_C_2_NO) ||
232 (xtse[1] & XTSE_2_04_C_2_O) ||
233 (xtse[7] & XTSE_8_03_C))
234 return ANNEX_C;
235
236 if ((xtse[3] & XTSE_4_05_I_3_NO) ||
237 (xtse[3] & XTSE_4_06_I_3_O) ||
238 (xtse[4] & XTSE_5_01_I_4_NO) ||
239 (xtse[4] & XTSE_5_02_I_4_O) ||
240 (xtse[5] & XTSE_6_07_I_5_NO) ||
241 (xtse[5] & XTSE_6_08_I_5_O))
242 return ANNEX_I;
243
244 if ((xtse[3] & XTSE_4_07_J_3_NO) ||
245 (xtse[3] & XTSE_4_08_J_3_O) ||
246 (xtse[6] & XTSE_7_01_J_5_NO) ||
247 (xtse[6] & XTSE_7_02_J_5_O))
248 return ANNEX_J;
249
250 if ((xtse[4] & XTSE_5_03_L_3_NO) ||
251 (xtse[4] & XTSE_5_04_L_3_NO) ||
252 (xtse[4] & XTSE_5_05_L_3_O) ||
253 (xtse[4] & XTSE_5_06_L_3_O))
254 return ANNEX_L;
255
256 if ((xtse[4] & XTSE_5_07_M_3_NO) ||
257 (xtse[4] & XTSE_5_08_M_3_O) ||
258 (xtse[6] & XTSE_7_03_M_5_NO) ||
259 (xtse[6] & XTSE_7_04_M_5_O))
260 return ANNEX_M;
261
262 return ANNEX_UNKNOWN;
263 }
264
265 static standard_t get_standard(const uint8_t *xtse) {
266 if (xtse[0] & XTSE_1_01_A_T1_413)
267 return STD_T1_413;
268
269 if (xtse[0] & XTSE_1_02_C_TS_101388)
270 return STD_TS_101_388;
271
272 if ((xtse[0] & XTSE_1_03_A_1_NO) ||
273 (xtse[0] & XTSE_1_04_A_1_O) ||
274 (xtse[0] & XTSE_1_05_B_1_NO) ||
275 (xtse[0] & XTSE_1_06_B_1_O) ||
276 (xtse[0] & XTSE_1_07_C_1_NO) ||
277 (xtse[0] & XTSE_1_08_C_1_O))
278 return STD_G_992_1;
279
280 if ((xtse[1] & XTSE_2_01_A_2_NO) ||
281 (xtse[1] & XTSE_2_02_B_2_O) ||
282 (xtse[1] & XTSE_2_03_C_2_NO) ||
283 (xtse[1] & XTSE_2_04_C_2_O))
284 return STD_G_992_2;
285
286 if ((xtse[2] & XTSE_3_03_A_3_NO) ||
287 (xtse[2] & XTSE_3_04_A_3_O) ||
288 (xtse[2] & XTSE_3_05_B_3_NO) ||
289 (xtse[2] & XTSE_3_06_B_3_O) ||
290 (xtse[3] & XTSE_4_05_I_3_NO) ||
291 (xtse[3] & XTSE_4_06_I_3_O) ||
292 (xtse[3] & XTSE_4_07_J_3_NO) ||
293 (xtse[3] & XTSE_4_08_J_3_O) ||
294 (xtse[4] & XTSE_5_03_L_3_NO) ||
295 (xtse[4] & XTSE_5_04_L_3_NO) ||
296 (xtse[4] & XTSE_5_05_L_3_O) ||
297 (xtse[4] & XTSE_5_06_L_3_O) ||
298 (xtse[4] & XTSE_5_07_M_3_NO) ||
299 (xtse[4] & XTSE_5_08_M_3_O))
300 return STD_G_992_3;
301
302 if ((xtse[3] & XTSE_4_01_A_4_NO) ||
303 (xtse[3] & XTSE_4_02_A_4_O) ||
304 (xtse[4] & XTSE_5_01_I_4_NO) ||
305 (xtse[4] & XTSE_5_02_I_4_O))
306 return STD_G_992_4;
307
308 if ((xtse[5] & XTSE_6_01_A_5_NO) ||
309 (xtse[5] & XTSE_6_02_A_5_O) ||
310 (xtse[5] & XTSE_6_03_B_5_NO) ||
311 (xtse[5] & XTSE_6_04_B_5_O) ||
312 (xtse[5] & XTSE_6_07_I_5_NO) ||
313 (xtse[5] & XTSE_6_08_I_5_O) ||
314 (xtse[6] & XTSE_7_01_J_5_NO) ||
315 (xtse[6] & XTSE_7_02_J_5_O) ||
316 (xtse[6] & XTSE_7_03_M_5_NO) ||
317 (xtse[6] & XTSE_7_04_M_5_O))
318 return STD_G_992_5;
319
320 if (xtse[7] & XTSE_8_08)
321 return STD_G_993_1;
322
323 if ((xtse[7] & XTSE_8_01_A) ||
324 (xtse[7] & XTSE_8_02_B) ||
325 (xtse[7] & XTSE_8_03_C))
326 return STD_G_993_2;
327
328 return STD_UNKNOWN;
329 }
330
331 static void version_information(int fd) {
332 IOCTL(DSL_VersionInformation_t, DSL_FIO_VERSION_INFORMATION_GET)
333
334 m_str("api_version", out.data.DSL_DriverVersionApi);
335 m_str("firmware_version", out.data.DSL_ChipSetFWVersion);
336 m_str("chipset", out.data.DSL_ChipSetType);
337 m_str("driver_version", out.data.DSL_DriverVersionMeiBsp);
338 }
339
340 static void line_state(int fd) {
341 IOCTL(DSL_LineState_t, DSL_FIO_LINE_STATE_GET)
342
343 int map = LSTATE_MAP_UNKNOWN;
344 const char *str;
345 switch (out.data.nLineState) {
346 STR_CASE_MAP(DSL_LINESTATE_NOT_INITIALIZED, "Not initialized", LSTATE_MAP_NOT_INITIALIZED)
347 STR_CASE_MAP(DSL_LINESTATE_EXCEPTION, "Exception", LSTATE_MAP_EXCEPTION)
348 STR_CASE(DSL_LINESTATE_NOT_UPDATED, "Not updated")
349 STR_CASE(DSL_LINESTATE_IDLE_REQUEST, "Idle request")
350 STR_CASE_MAP(DSL_LINESTATE_IDLE, "Idle", LSTATE_MAP_IDLE)
351 STR_CASE(DSL_LINESTATE_SILENT_REQUEST, "Silent request")
352 STR_CASE_MAP(DSL_LINESTATE_SILENT, "Silent", LSTATE_MAP_SILENT)
353 STR_CASE_MAP(DSL_LINESTATE_HANDSHAKE, "Handshake", LSTATE_MAP_HANDSHAKE)
354 STR_CASE(DSL_LINESTATE_BONDING_CLR, "Bonding CLR")
355 STR_CASE_MAP(DSL_LINESTATE_FULL_INIT, "Full init", LSTATE_MAP_FULL_INIT)
356 STR_CASE(DSL_LINESTATE_SHORT_INIT_ENTRY, "Short init entry")
357 STR_CASE(DSL_LINESTATE_DISCOVERY, "Discovery")
358 STR_CASE(DSL_LINESTATE_TRAINING, "Training")
359 STR_CASE(DSL_LINESTATE_ANALYSIS, "Analysis")
360 STR_CASE(DSL_LINESTATE_EXCHANGE, "Exchange")
361 STR_CASE_MAP(DSL_LINESTATE_SHOWTIME_NO_SYNC, "Showtime without TC-Layer sync", LSTATE_MAP_SHOWTIME_NO_SYNC)
362 STR_CASE_MAP(DSL_LINESTATE_SHOWTIME_TC_SYNC, "Showtime with TC-Layer sync", LSTATE_MAP_SHOWTIME_TC_SYNC)
363 STR_CASE(DSL_LINESTATE_FASTRETRAIN, "Fastretrain")
364 STR_CASE(DSL_LINESTATE_LOWPOWER_L2, "Lowpower L2")
365 STR_CASE(DSL_LINESTATE_LOOPDIAGNOSTIC_ACTIVE, "Loopdiagnostic active")
366 STR_CASE(DSL_LINESTATE_LOOPDIAGNOSTIC_DATA_EXCHANGE, "Loopdiagnostic data exchange")
367 STR_CASE(DSL_LINESTATE_LOOPDIAGNOSTIC_DATA_REQUEST, "Loopdiagnostic data request")
368 STR_CASE(DSL_LINESTATE_LOOPDIAGNOSTIC_COMPLETE, "Loopdiagnostic complete")
369 STR_CASE_MAP(DSL_LINESTATE_RESYNC, "Resync", LSTATE_MAP_RESYNC)
370 STR_CASE(DSL_LINESTATE_TEST, "Test")
371 STR_CASE(DSL_LINESTATE_TEST_LOOP, "Test loop")
372 STR_CASE(DSL_LINESTATE_TEST_REVERB, "Test reverb")
373 STR_CASE(DSL_LINESTATE_TEST_MEDLEY, "Test medley")
374 STR_CASE(DSL_LINESTATE_TEST_SHOWTIME_LOCK, "Showtime lock")
375 STR_CASE(DSL_LINESTATE_TEST_QUIET, "Quiet")
376 STR_CASE(DSL_LINESTATE_LOWPOWER_L3, "Lowpower L3")
377 #ifndef INCLUDE_DSL_CPE_API_DANUBE
378 STR_CASE(DSL_LINESTATE_DISABLED, "Disabled")
379 STR_CASE(DSL_LINESTATE_T1413, "T1413")
380 STR_CASE(DSL_LINESTATE_ORDERLY_SHUTDOWN_REQUEST, "Orderly shutdown request")
381 STR_CASE(DSL_LINESTATE_ORDERLY_SHUTDOWN, "Orderly shutdown")
382 STR_CASE(DSL_LINESTATE_TEST_FILTERDETECTION_ACTIVE, "Test filterdetection active")
383 STR_CASE(DSL_LINESTATE_TEST_FILTERDETECTION_COMPLETE, "Test filterdetection complete")
384 #endif
385 default:
386 str = NULL;
387 break;
388 };
389
390 if (str)
391 m_str("state", str);
392
393 if (map != LSTATE_MAP_UNKNOWN )
394 m_u32("state_num", map);
395
396 m_bool("up", out.data.nLineState == DSL_LINESTATE_SHOWTIME_TC_SYNC);
397 }
398
399 static void pm_channel_counters_showtime(int fd) {
400 IOCTL_DIR(DSL_PM_ChannelCounters_t, DSL_FIO_PM_CHANNEL_COUNTERS_SHOWTIME_GET, DSL_NEAR_END);
401
402 m_u32("uptime", out.interval.nElapsedTime);
403 }
404
405 static void g997_line_inventory(int fd) {
406 IOCTL_DIR(DSL_G997_LineInventory_t, DSL_FIO_G997_LINE_INVENTORY_GET, DSL_DOWNSTREAM)
407
408 m_array("vendor_id", out.data.G994VendorID, DSL_G997_LI_MAXLEN_VENDOR_ID);
409 m_vendor("vendor", out.data.G994VendorID);
410 m_array("system_vendor_id", out.data.SystemVendorID, DSL_G997_LI_MAXLEN_VENDOR_ID);
411 m_vendor("system_vendor", out.data.SystemVendorID);
412 m_array("version", out.data.VersionNumber, DSL_G997_LI_MAXLEN_VERSION);
413 m_array("serial", out.data.SerialNumber, DSL_G997_LI_MAXLEN_SERIAL);
414 }
415
416 static void g997_power_management_status(int fd) {
417 IOCTL(DSL_G997_PowerManagementStatus_t, DSL_FIO_G997_POWER_MANAGEMENT_STATUS_GET)
418
419 int map = PSTATE_MAP_UNKNOWN;
420 const char *str;
421 switch (out.data.nPowerManagementStatus) {
422 STR_CASE_MAP(DSL_G997_PMS_NA, "Power management state is not available", PSTATE_MAP_NA)
423 STR_CASE_MAP(DSL_G997_PMS_L0, "L0 - Synchronized", PSTATE_MAP_L0)
424 STR_CASE_MAP(DSL_G997_PMS_L1, "L1 - Power Down Data transmission (G.992.2)", PSTATE_MAP_L1)
425 STR_CASE_MAP(DSL_G997_PMS_L2, "L2 - Power Down Data transmission (G.992.3 and G.992.4)", PSTATE_MAP_L2)
426 STR_CASE_MAP(DSL_G997_PMS_L3, "L3 - No power", PSTATE_MAP_L3)
427 default:
428 str = NULL;
429 break;
430 };
431
432 if (str)
433 m_str("power_state", str);
434
435 if (map != PSTATE_MAP_UNKNOWN)
436 m_u32("power_state_num", map);
437 }
438
439 static void g997_xtu_system_enabling(int fd, standard_t *standard) {
440 IOCTL(DSL_G997_XTUSystemEnabling_t, DSL_FIO_G997_XTU_SYSTEM_ENABLING_STATUS_GET)
441
442 m_array("xtse", out.data.XTSE, DSL_G997_NUM_XTSE_OCTETS);
443
444 const char *str;
445 switch (get_annex(out.data.XTSE)) {
446 STR_CASE(ANNEX_A, "A")
447 STR_CASE(ANNEX_B, "B")
448 STR_CASE(ANNEX_C, "C")
449 STR_CASE(ANNEX_I, "I")
450 STR_CASE(ANNEX_J, "J")
451 STR_CASE(ANNEX_L, "L")
452 STR_CASE(ANNEX_M, "M")
453 default:
454 str = NULL;
455 break;
456 };
457 if (str)
458 m_str("annex", str);
459
460 *standard = get_standard(out.data.XTSE);
461
462 switch (*standard) {
463 STR_CASE(STD_T1_413, "T1.413")
464 STR_CASE(STD_TS_101_388, "TS 101 388")
465 STR_CASE(STD_G_992_1, "G.992.1")
466 STR_CASE(STD_G_992_2, "G.992.2")
467 STR_CASE(STD_G_992_3, "G.992.3")
468 STR_CASE(STD_G_992_4, "G.992.4")
469 STR_CASE(STD_G_992_5, "G.992.5")
470 STR_CASE(STD_G_993_1, "G.993.1")
471 STR_CASE(STD_G_993_2, "G.993.2")
472 default:
473 str = NULL;
474 break;
475 }
476 if (str)
477 m_str("standard", str);
478 }
479
480 static void get_vector_status(int fd, vector_t *status) {
481 *status = VECTOR_UNKNOWN;
482
483 #ifdef INCLUDE_DSL_CPE_API_VRX
484 if (fd < 0)
485 return;
486
487 IOCTL(IOCTL_MEI_dsmStatus_t, FIO_MEI_DSM_STATUS_GET);
488
489 switch (out.eVectorStatus) {
490 case e_MEI_VECTOR_STAT_OFF:
491 *status = VECTOR_OFF;
492 break;
493 case e_MEI_VECTOR_STAT_ON_DS:
494 *status = VECTOR_ON_DS;
495 break;
496 case e_MEI_VECTOR_STAT_ON_DS_US:
497 *status = VECTOR_ON_DS_US;
498 break;
499 default:
500 break;
501 };
502 #endif
503 }
504
505 static void band_plan_status(int fd, profile_t *profile) {
506 #if (INCLUDE_DSL_CPE_API_VDSL_SUPPORT == 1)
507 IOCTL(DSL_BandPlanStatus_t, DSL_FIO_BAND_PLAN_STATUS_GET)
508
509 switch (out.data.nProfile) {
510 case DSL_PROFILE_8A:
511 *profile = PROFILE_8A;
512 break;
513 case DSL_PROFILE_8B:
514 *profile = PROFILE_8B;
515 break;
516 case DSL_PROFILE_8C:
517 *profile = PROFILE_8C;
518 break;
519 case DSL_PROFILE_8D:
520 *profile = PROFILE_8D;
521 break;
522 case DSL_PROFILE_12A:
523 *profile = PROFILE_12A;
524 break;
525 case DSL_PROFILE_12B:
526 *profile = PROFILE_12B;
527 break;
528 case DSL_PROFILE_17A:
529 *profile = PROFILE_17A;
530 break;
531 case DSL_PROFILE_30A:
532 *profile = PROFILE_30A;
533 break;
534 case DSL_PROFILE_35B:
535 *profile = PROFILE_35B;
536 break;
537 default:
538 *profile = PROFILE_UNKNOWN;
539 break;
540 };
541
542 const char *str;
543 switch (*profile) {
544 STR_CASE(PROFILE_8A, "8a")
545 STR_CASE(PROFILE_8B, "8b")
546 STR_CASE(PROFILE_8C, "8c")
547 STR_CASE(PROFILE_8D, "8d")
548 STR_CASE(PROFILE_12A, "12a")
549 STR_CASE(PROFILE_12B, "12b")
550 STR_CASE(PROFILE_17A, "17a")
551 STR_CASE(PROFILE_30A, "30a")
552 STR_CASE(PROFILE_35B, "35b")
553 default:
554 str = NULL;
555 break;
556 };
557 if (str)
558 m_str("profile", str);
559 #endif
560 }
561
562 static void line_feature_config(int fd, DSL_AccessDir_t direction) {
563 IOCTL_DIR(DSL_LineFeature_t, DSL_FIO_LINE_FEATURE_STATUS_GET, direction)
564
565 m_bool("trellis", out.data.bTrellisEnable);
566 m_bool("bitswap", out.data.bBitswapEnable);
567 m_bool("retx", out.data.bReTxEnable);
568 m_bool("virtual_noise", out.data.bVirtualNoiseSupport);
569 }
570
571 static void g997_channel_status(int fd, DSL_AccessDir_t direction) {
572 IOCTL_DIR(DSL_G997_ChannelStatus_t, DSL_FIO_G997_CHANNEL_STATUS_GET, direction);
573
574 m_u32("interleave_delay", out.data.ActualInterleaveDelay * 10);
575 #ifndef INCLUDE_DSL_CPE_API_DANUBE
576 // prefer ACTNDR, see comments in drv_dsl_cpe_api_g997.h
577 m_u32("data_rate", out.data.ActualNetDataRate);
578 #else
579 m_u32("data_rate", out.data.ActualDataRate);
580 #endif
581 }
582
583 static void g997_line_status(int fd, DSL_AccessDir_t direction) {
584 IOCTL_DIR_DELT(DSL_G997_LineStatus_t, DSL_FIO_G997_LINE_STATUS_GET, direction, DSL_DELT_DATA_SHOWTIME);
585
586 m_db("latn", out.data.LATN);
587 m_db("satn", out.data.SATN);
588 m_db("snr", out.data.SNR);
589 m_db("actps", out.data.ACTPS);
590 m_db("actatp", out.data.ACTATP);
591 m_u32("attndr", out.data.ATTNDR);
592 }
593
594 static void pm_line_sec_counters_total(int fd, DSL_XTUDir_t direction) {
595 IOCTL_DIR(DSL_PM_LineSecCountersTotal_t, DSL_FIO_PM_LINE_SEC_COUNTERS_TOTAL_GET, direction)
596
597 m_u32("es", out.data.nES);
598 m_u32("ses", out.data.nSES);
599 m_u32("loss", out.data.nLOSS);
600 m_u32("uas", out.data.nUAS);
601 m_u32("lofs", out.data.nLOFS);
602 #ifndef INCLUDE_DSL_CPE_API_DANUBE
603 m_u32("fecs", out.data.nFECS);
604 #endif
605 }
606
607 static void pm_data_path_counters_total(int fd, DSL_XTUDir_t direction) {
608 IOCTL_DIR(DSL_PM_DataPathCountersTotal_t, DSL_FIO_PM_DATA_PATH_COUNTERS_TOTAL_GET, direction);
609
610 m_u32("hec", out.data.nHEC);
611 m_u32("ibe", out.data.nIBE);
612 m_u32("crc_p", out.data.nCRC_P);
613 m_u32("crcp_p", out.data.nCRCP_P);
614 m_u32("cv_p", out.data.nCV_P);
615 m_u32("cvp_p", out.data.nCVP_P);
616 }
617
618 static void retx_statistics(int fd, DSL_XTUDir_t direction) {
619 #ifdef INCLUDE_DSL_CPE_PM_RETX_COUNTERS
620 #ifdef INCLUDE_DSL_CPE_PM_RETX_THRESHOLDS
621 IOCTL_DIR(DSL_ReTxStatistics_t, DSL_FIO_RETX_STATISTICS_GET, direction);
622
623 m_u32("rx_corrupted", out.data.nRxCorruptedTotal);
624 m_u32("rx_uncorrected_protected", out.data.nRxUncorrectedProtected);
625 m_u32("rx_retransmitted", out.data.nRxRetransmitted);
626 m_u32("rx_corrected", out.data.nRxCorrected);
627 m_u32("tx_retransmitted", out.data.nTxRetransmitted);
628 #endif
629 #endif
630 }
631
632 static void describe_mode(standard_t standard, profile_t profile, vector_t vector) {
633 char buf[128];
634
635 switch (standard) {
636 case STD_T1_413:
637 strcpy(buf, "T1.413");
638 break;
639 case STD_TS_101_388:
640 strcpy(buf, "TS 101 388");
641 break;
642 case STD_G_992_1:
643 strcpy(buf, "G.992.1 (ADSL)");
644 break;
645 case STD_G_992_2:
646 strcpy(buf, "G.992.2 (ADSL lite)");
647 break;
648 case STD_G_992_3:
649 strcpy(buf, "G.992.3 (ADSL2)");
650 break;
651 case STD_G_992_4:
652 strcpy(buf, "G.992.4 (ADSL2 lite)");
653 break;
654 case STD_G_992_5:
655 strcpy(buf, "G.992.5 (ADSL2+)");
656 break;
657 case STD_G_993_1:
658 strcpy(buf, "G.993.1 (VDSL)");
659 break;
660 case STD_G_993_2:
661 strcpy(buf, "G.993.2 (VDSL2");
662
663 switch (profile) {
664 case PROFILE_8A:
665 strcat(buf, ", Profile 8a");
666 break;
667 case PROFILE_8B:
668 strcat(buf, ", Profile 8b");
669 break;
670 case PROFILE_8C:
671 strcat(buf, ", Profile 8c");
672 break;
673 case PROFILE_8D:
674 strcat(buf, ", Profile 8d");
675 break;
676 case PROFILE_12A:
677 strcat(buf, ", Profile 12a");
678 break;
679 case PROFILE_12B:
680 strcat(buf, ", Profile 12b");
681 break;
682 case PROFILE_17A:
683 strcat(buf, ", Profile 17a");
684 break;
685 case PROFILE_30A:
686 strcat(buf, ", Profile 30a");
687 break;
688 case PROFILE_35B:
689 strcat(buf, ", Profile 35b");
690 break;
691 default:
692 break;
693 };
694
695 switch (vector) {
696 case VECTOR_ON_DS:
697 strcat(buf, ", with downstream vectoring");
698 break;
699 case VECTOR_ON_DS_US:
700 strcat(buf, ", with down- and upstream vectoring");
701 break;
702 default:
703 break;
704 };
705
706 strcat(buf, ")");
707 break;
708 default:
709 return;
710 };
711
712 m_str("mode", buf);
713 }
714
715 static int metrics(struct ubus_context *ctx, struct ubus_object *obj,
716 struct ubus_request_data *req, const char *method,
717 struct blob_attr *msg)
718 {
719 int fd, fd_mei;
720 void *c, *c2;
721 standard_t standard = STD_UNKNOWN;
722 profile_t profile = PROFILE_UNKNOWN;
723 vector_t vector = VECTOR_UNKNOWN;
724
725 #ifndef INCLUDE_DSL_CPE_API_DANUBE
726 fd = open(DSL_CPE_DEVICE_NAME "0", O_RDWR, 0644);
727 #else
728 fd = open(DSL_CPE_DEVICE_NAME, O_RDWR, 0644);
729 #endif
730 if (fd < 0)
731 return UBUS_STATUS_UNKNOWN_ERROR;
732
733 #ifdef INCLUDE_DSL_CPE_API_VRX
734 fd_mei = open(DSL_CPE_DSL_LOW_DEV "/0", O_RDWR, 0644);
735 #else
736 fd_mei = -1;
737 #endif
738
739 blob_buf_init(&b, 0);
740
741 version_information(fd);
742 line_state(fd);
743 pm_channel_counters_showtime(fd);
744
745 c = blobmsg_open_table(&b, "atu_c");
746 g997_line_inventory(fd);
747 blobmsg_close_table(&b, c);
748
749 g997_power_management_status(fd);
750 g997_xtu_system_enabling(fd, &standard);
751
752 if (standard == STD_G_993_2) {
753 band_plan_status(fd, &profile);
754 get_vector_status(fd_mei, &vector);
755 }
756
757 describe_mode(standard, profile, vector);
758
759 c = blobmsg_open_table(&b, "upstream");
760 switch (vector) {
761 case VECTOR_OFF:
762 m_bool("vector", false);
763 break;
764 case VECTOR_ON_DS_US:
765 m_bool("vector", true);
766 break;
767 default:
768 break;
769 };
770 line_feature_config(fd, DSL_UPSTREAM);
771 g997_channel_status(fd, DSL_UPSTREAM);
772 g997_line_status(fd, DSL_UPSTREAM);
773 blobmsg_close_table(&b, c);
774
775 c = blobmsg_open_table(&b, "downstream");
776 switch (vector) {
777 case VECTOR_OFF:
778 m_bool("vector", false);
779 break;
780 case VECTOR_ON_DS:
781 case VECTOR_ON_DS_US:
782 m_bool("vector", true);
783 break;
784 default:
785 break;
786 };
787 line_feature_config(fd, DSL_DOWNSTREAM);
788 g997_channel_status(fd, DSL_DOWNSTREAM);
789 g997_line_status(fd, DSL_DOWNSTREAM);
790 blobmsg_close_table(&b, c);
791
792 c = blobmsg_open_table(&b, "errors");
793 c2 = blobmsg_open_table(&b, "near");
794 pm_line_sec_counters_total(fd, DSL_NEAR_END);
795 pm_data_path_counters_total(fd, DSL_NEAR_END);
796 retx_statistics(fd, DSL_NEAR_END);
797 blobmsg_close_table(&b, c2);
798
799 c2 = blobmsg_open_table(&b, "far");
800 pm_line_sec_counters_total(fd, DSL_FAR_END);
801 pm_data_path_counters_total(fd, DSL_FAR_END);
802 retx_statistics(fd, DSL_FAR_END);
803 blobmsg_close_table(&b, c2);
804 blobmsg_close_table(&b, c);
805
806 ubus_send_reply(ctx, req, b.head);
807
808 if (fd_mei >= 0)
809 close(fd_mei);
810 close(fd);
811
812 return 0;
813 }
814
815 static const struct ubus_method dsl_methods[] = {
816 UBUS_METHOD_NOARG("metrics", metrics),
817 };
818
819 static struct ubus_object_type dsl_object_type =
820 UBUS_OBJECT_TYPE("dsl", dsl_methods);
821
822 static struct ubus_object dsl_object = {
823 .name = "dsl",
824 .type = &dsl_object_type,
825 .methods = dsl_methods,
826 .n_methods = ARRAY_SIZE(dsl_methods),
827 };
828
829 static DSL_int_t ubus_main(DSL_CPE_Thread_Params_t *params) {
830 uloop_run();
831 return 0;
832 }
833
834 void ubus_init() {
835 uloop_init();
836
837 ctx = ubus_connect(NULL);
838 if (!ctx)
839 return;
840
841 if (ubus_add_object(ctx, &dsl_object)) {
842 ubus_free(ctx);
843 ctx = NULL;
844 return;
845 }
846
847 ubus_add_uloop(ctx);
848
849 DSL_CPE_ThreadInit(&thread, "ubus", ubus_main, DSL_CPE_PIPE_STACK_SIZE, DSL_CPE_PIPE_PRIORITY, 0, 0);
850 }
851
852 void ubus_deinit() {
853 if (!ctx)
854 return;
855
856 ubus_free(ctx);
857 uloop_done();
858
859 DSL_CPE_ThreadShutdown(&thread, 1000);
860 }