diagnostic.c (4796B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2021 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <stdio.h> 28 29 #include <ell/ell.h> 30 31 #include "client/diagnostic.h" 32 #include "client/display.h" 33 34 typedef bool (*display_dict_custom_func_t)(struct l_dbus_message_iter *variant, 35 const char *key, const char *margin, 36 int name_column_width, int value_column_width); 37 38 /* 39 * Maps dictionary keys to types/units. 'type' should be a valid DBus type, or 40 * zero for displaying in a custom fashion. When the display needs to be 41 * customized 'units' should point to a custom display function of the form 42 * display_dict_custom_func_t which should display the entire value as well 43 * as any units required. 44 */ 45 struct diagnostic_dict_mapping { 46 const char *key; 47 char type; 48 const char *units; 49 display_dict_custom_func_t custom; 50 }; 51 52 static const struct diagnostic_dict_mapping *find_mapping(const char *key, 53 const struct diagnostic_dict_mapping *mapping) 54 { 55 int idx = 0; 56 57 while (mapping[idx].key) { 58 if (!strcmp(mapping[idx].key, key)) 59 return &mapping[idx]; 60 61 idx++; 62 } 63 64 return NULL; 65 } 66 67 static bool display_bitrate_100kbps(struct l_dbus_message_iter *variant, 68 const char *key, const char *margin, 69 int name_column_width, int value_column_width) 70 { 71 uint32_t rate; 72 char str[50]; 73 74 if (!l_dbus_message_iter_get_variant(variant, "u", &rate)) 75 return false; 76 77 sprintf(str, "%u Kbit/s", rate * 100); 78 display_table_row(margin, 3, 8, "", name_column_width, key, value_column_width, str); 79 80 return true; 81 } 82 83 static const struct diagnostic_dict_mapping diagnostic_mapping[] = { 84 { "Address", 's' }, 85 { "ConnectedBss", 's' }, 86 { "RxMode", 's' }, 87 { "TxMode", 's' }, 88 { "RxBitrate", 0, NULL, display_bitrate_100kbps }, 89 { "TxBitrate", 0, NULL, display_bitrate_100kbps }, 90 { "ExpectedThroughput", 'u', "Kbit/s" }, 91 { "RSSI", 'n', "dBm" }, 92 { "AverageRSSI", 'n', "dBm" }, 93 { "RxMCS", 'y' }, 94 { "TxMCS", 'y' }, 95 { "Frequency", 'u' }, 96 { "Channel", 'q' }, 97 { "Security", 's' }, 98 { "InactiveTime", 'u', "ms" }, 99 { "ConnectedTime", 'u', "s" }, 100 { NULL } 101 }; 102 103 void diagnostic_display(struct l_dbus_message_iter *dict, 104 const char *margin, int name_column_width, 105 int value_column_width) 106 { 107 struct l_dbus_message_iter variant; 108 const char *key; 109 const struct diagnostic_dict_mapping *map; 110 char display_text[160]; 111 112 while (l_dbus_message_iter_next_entry(dict, &key, &variant)) { 113 const char *s_value; 114 uint32_t u_value; 115 uint16_t q_value; 116 int16_t n_value; 117 uint8_t y_value; 118 int bytes; 119 120 map = find_mapping(key, diagnostic_mapping); 121 if (!map) 122 continue; 123 124 switch (map->type) { 125 case 0: 126 if (!map->custom) 127 continue; 128 129 if (!map->custom(&variant, key, margin, name_column_width, 130 value_column_width)) 131 goto parse_error; 132 133 /* custom should handle any units, so continue */ 134 continue; 135 136 case 's': 137 if (!l_dbus_message_iter_get_variant(&variant, "s", 138 &s_value)) 139 goto parse_error; 140 141 bytes = sprintf(display_text, "%s", s_value); 142 break; 143 144 case 'u': 145 if (!l_dbus_message_iter_get_variant(&variant, "u", 146 &u_value)) 147 goto parse_error; 148 149 bytes = sprintf(display_text, "%u", u_value); 150 break; 151 152 case 'q': 153 if (!l_dbus_message_iter_get_variant(&variant, "q", 154 &q_value)) 155 goto parse_error; 156 157 bytes = sprintf(display_text, "%u", q_value); 158 break; 159 160 case 'n': 161 if (!l_dbus_message_iter_get_variant(&variant, "n", 162 &n_value)) 163 goto parse_error; 164 165 bytes = sprintf(display_text, "%i", n_value); 166 break; 167 168 case 'y': 169 if (!l_dbus_message_iter_get_variant(&variant, "y", 170 &y_value)) 171 goto parse_error; 172 173 bytes = sprintf(display_text, "%u", y_value); 174 break; 175 176 default: 177 display("type %c not handled\n", map->type); 178 continue; 179 } 180 181 if (map->units) 182 sprintf(display_text + bytes, " %s", map->units); 183 184 display_table_row(margin, 3, 8, "", name_column_width, 185 key, value_column_width, display_text); 186 } 187 188 return; 189 190 parse_error: 191 display_error("Error parsing diagnostics"); 192 }