Add missing option for getting tdma signal strength
[project/uqmi.git] / commands-nas.c
1 #include "qmi-message.h"
2
3 static struct qmi_nas_set_system_selection_preference_request sel_req;
4
5 #define cmd_nas_do_set_system_selection_cb no_cb
6 static enum qmi_cmd_result
7 cmd_nas_do_set_system_selection_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
8 {
9         qmi_set_nas_set_system_selection_preference_request(msg, &sel_req);
10         return QMI_CMD_REQUEST;
11 }
12
13 static enum qmi_cmd_result
14 do_sel_network(void)
15 {
16         static bool use_sel_req = false;
17
18         if (!use_sel_req) {
19                 use_sel_req = true;
20                 uqmi_add_command(NULL, __UQMI_COMMAND_nas_do_set_system_selection);
21         }
22
23         return QMI_CMD_DONE;
24 }
25
26 #define cmd_nas_set_network_modes_cb no_cb
27 static enum qmi_cmd_result
28 cmd_nas_set_network_modes_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
29 {
30         static const struct {
31                 const char *name;
32                 QmiNasRatModePreference val;
33         } modes[] = {
34                 { "cdma", QMI_NAS_RAT_MODE_PREFERENCE_CDMA_1X | QMI_NAS_RAT_MODE_PREFERENCE_CDMA_1XEVDO },
35                 { "td-scdma", QMI_NAS_RAT_MODE_PREFERENCE_TD_SCDMA },
36                 { "gsm", QMI_NAS_RAT_MODE_PREFERENCE_GSM },
37                 { "umts", QMI_NAS_RAT_MODE_PREFERENCE_UMTS },
38                 { "lte", QMI_NAS_RAT_MODE_PREFERENCE_LTE },
39         };
40         QmiNasRatModePreference val = 0;
41         char *word;
42         int i;
43
44         for (word = strtok(arg, ",");
45              word;
46              word = strtok(NULL, ",")) {
47                 bool found = false;
48
49                 for (i = 0; i < ARRAY_SIZE(modes); i++) {
50                         if (strcmp(word, modes[i].name) != 0 &&
51                                 strcmp(word, "all") != 0)
52                                 continue;
53
54                         val |= modes[i].val;
55                         found = true;
56                 }
57
58                 if (!found) {
59                         uqmi_add_error("Invalid network mode");
60                         return QMI_CMD_EXIT;
61                 }
62         }
63
64         qmi_set(&sel_req, mode_preference, val);
65         return do_sel_network();
66 }
67
68 #define cmd_nas_set_network_preference_cb no_cb
69 static enum qmi_cmd_result
70 cmd_nas_set_network_preference_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
71 {
72         QmiNasGsmWcdmaAcquisitionOrderPreference pref = QMI_NAS_GSM_WCDMA_ACQUISITION_ORDER_PREFERENCE_AUTOMATIC;
73
74         if (!strcmp(arg, "gsm"))
75                 pref = QMI_NAS_GSM_WCDMA_ACQUISITION_ORDER_PREFERENCE_GSM;
76         else if (!strcmp(arg, "wcdma"))
77                 pref = QMI_NAS_GSM_WCDMA_ACQUISITION_ORDER_PREFERENCE_WCDMA;
78
79         qmi_set(&sel_req, gsm_wcdma_acquisition_order_preference, pref);
80         return do_sel_network();
81 }
82
83 #define cmd_nas_set_roaming_cb no_cb
84 static enum qmi_cmd_result
85 cmd_nas_set_roaming_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
86 {
87         QmiNasRoamingPreference pref;
88
89         if (!strcmp(arg, "any"))
90                 pref = QMI_NAS_ROAMING_PREFERENCE_ANY;
91         else if (!strcmp(arg, "only"))
92                 pref = QMI_NAS_ROAMING_PREFERENCE_NOT_OFF;
93         else if (!strcmp(arg, "off"))
94                 pref = QMI_NAS_ROAMING_PREFERENCE_OFF;
95         else
96                 return uqmi_add_error("Invalid argument");
97
98         qmi_set(&sel_req, roaming_preference, pref);
99         return do_sel_network();
100 }
101
102 #define cmd_nas_initiate_network_register_cb no_cb
103 static enum qmi_cmd_result
104 cmd_nas_initiate_network_register_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
105 {
106         static struct qmi_nas_initiate_network_register_request register_req = {
107                 QMI_INIT(action, QMI_NAS_NETWORK_REGISTER_TYPE_AUTOMATIC)
108         };
109
110         qmi_set_nas_initiate_network_register_request(msg, &register_req);
111         return QMI_CMD_REQUEST;
112 }
113
114 static void
115 cmd_nas_get_signal_info_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
116 {
117         struct qmi_nas_get_signal_info_response res;
118         void *c;
119
120         qmi_parse_nas_get_signal_info_response(msg, &res);
121
122         c = blobmsg_open_table(&status, NULL);
123         if (res.set.cdma_signal_strength) {
124                 blobmsg_add_string(&status, "type", "cdma");
125                 blobmsg_add_u32(&status, "rssi", (int32_t) res.data.cdma_signal_strength.rssi);
126                 blobmsg_add_u32(&status, "ecio", (int32_t) res.data.cdma_signal_strength.ecio);
127         }
128
129         if (res.set.hdr_signal_strength) {
130                 blobmsg_add_string(&status, "type", "hdr");
131                 blobmsg_add_u32(&status, "rssi", (int32_t) res.data.hdr_signal_strength.rssi);
132                 blobmsg_add_u32(&status, "ecio", (int32_t) res.data.hdr_signal_strength.ecio);
133                 blobmsg_add_u32(&status, "io", res.data.hdr_signal_strength.io);
134         }
135
136         if (res.set.gsm_signal_strength) {
137                 blobmsg_add_string(&status, "type", "gsm");
138                 blobmsg_add_u32(&status, "signal", (int32_t) res.data.gsm_signal_strength);
139         }
140
141         if (res.set.wcdma_signal_strength) {
142                 blobmsg_add_string(&status, "type", "wcdma");
143                 blobmsg_add_u32(&status, "rssi", (int32_t) res.data.wcdma_signal_strength.rssi);
144                 blobmsg_add_u32(&status, "ecio", (int32_t) res.data.wcdma_signal_strength.ecio);
145         }
146
147         if (res.set.lte_signal_strength) {
148                 blobmsg_add_string(&status, "type", "lte");
149                 blobmsg_add_u32(&status, "rssi", (int32_t) res.data.lte_signal_strength.rssi);
150                 blobmsg_add_u32(&status, "rsrq", (int32_t) res.data.lte_signal_strength.rsrq);
151                 blobmsg_add_u32(&status, "rsrp", (int32_t) res.data.lte_signal_strength.rsrp);
152                 blobmsg_add_u32(&status, "snr", (int32_t) res.data.lte_signal_strength.snr);
153         }
154
155         if (res.set.tdma_signal_strength) {
156                 blobmsg_add_string(&status, "type", "tdma");
157                 blobmsg_add_u32(&status, "signal", (int32_t) res.data.tdma_signal_strength);
158         }
159
160         blobmsg_close_table(&status, c);
161 }
162
163 static enum qmi_cmd_result
164 cmd_nas_get_signal_info_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
165 {
166         qmi_set_nas_get_signal_info_request(msg);
167         return QMI_CMD_REQUEST;
168 }
169
170 static void
171 cmd_nas_get_serving_system_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
172 {
173         struct qmi_nas_get_serving_system_response res;
174         static const char *reg_states[] = {
175                 [QMI_NAS_REGISTRATION_STATE_NOT_REGISTERED] = "not_registered",
176                 [QMI_NAS_REGISTRATION_STATE_REGISTERED] = "registered",
177                 [QMI_NAS_REGISTRATION_STATE_NOT_REGISTERED_SEARCHING] = "searching",
178                 [QMI_NAS_REGISTRATION_STATE_REGISTRATION_DENIED] = "registering_denied",
179                 [QMI_NAS_REGISTRATION_STATE_UNKNOWN] = "unknown",
180         };
181         void *c;
182
183         qmi_parse_nas_get_serving_system_response(msg, &res);
184
185         c = blobmsg_open_table(&status, NULL);
186         if (res.set.serving_system) {
187                 int state = res.data.serving_system.registration_state;
188
189                 if (state > QMI_NAS_REGISTRATION_STATE_UNKNOWN)
190                         state = QMI_NAS_REGISTRATION_STATE_UNKNOWN;
191
192                 blobmsg_add_string(&status, "registration", reg_states[state]);
193         }
194         if (res.set.current_plmn) {
195                 blobmsg_add_u32(&status, "plmn_mcc", res.data.current_plmn.mcc);
196                 blobmsg_add_u32(&status, "plmn_mnc", res.data.current_plmn.mnc);
197                 if (res.data.current_plmn.description)
198                         blobmsg_add_string(&status, "plmn_description", res.data.current_plmn.description);
199         }
200
201         if (res.set.roaming_indicator)
202                 blobmsg_add_u8(&status, "roaming", !res.data.roaming_indicator);
203
204         blobmsg_close_table(&status, c);
205 }
206
207 static enum qmi_cmd_result
208 cmd_nas_get_serving_system_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
209 {
210         qmi_set_nas_get_serving_system_request(msg);
211         return QMI_CMD_REQUEST;
212 }
213
214 static void
215 cmd_nas_network_scan_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
216 {
217         static struct qmi_nas_network_scan_response res;
218         const char *network_status[] = {
219                 "current_serving",
220                 "available",
221                 "home",
222                 "roaming",
223                 "forbidden",
224                 "not_forbidden",
225                 "preferred",
226                 "not_preferred",
227         };
228         const char *radio[] = {
229                 [QMI_NAS_RADIO_INTERFACE_NONE] = "none",
230                 [QMI_NAS_RADIO_INTERFACE_CDMA_1X] = "cdma-1x",
231                 [QMI_NAS_RADIO_INTERFACE_CDMA_1XEVDO] = "cdma-1x_evdo",
232                 [QMI_NAS_RADIO_INTERFACE_AMPS] = "amps",
233                 [QMI_NAS_RADIO_INTERFACE_GSM] = "gsm",
234                 [QMI_NAS_RADIO_INTERFACE_UMTS] = "umts",
235                 [QMI_NAS_RADIO_INTERFACE_LTE] = "lte",
236                 [QMI_NAS_RADIO_INTERFACE_TD_SCDMA] = "td-scdma",
237         };
238         void *t, *c, *info, *stat;
239         int i, j;
240
241         qmi_parse_nas_network_scan_response(msg, &res);
242
243         t = blobmsg_open_table(&status, NULL);
244
245         c = blobmsg_open_array(&status, "network_info");
246         for (i = 0; i < res.data.network_information_n; i++) {
247                 info = blobmsg_open_table(&status, NULL);
248                 blobmsg_add_u32(&status, "mcc", res.data.network_information[i].mcc);
249                 blobmsg_add_u32(&status, "mnc", res.data.network_information[i].mnc);
250                 if (res.data.network_information[i].description)
251                         blobmsg_add_string(&status, "description", res.data.network_information[i].description);
252                 stat = blobmsg_open_array(&status, "status");
253                 for (j = 0; j < ARRAY_SIZE(network_status); j++) {
254                         if (!(res.data.network_information[i].network_status & (1 << j)))
255                                 continue;
256
257                         blobmsg_add_string(&status, NULL, network_status[j]);
258                 }
259                 blobmsg_close_array(&status, stat);
260                 blobmsg_close_table(&status, info);
261         }
262         blobmsg_close_array(&status, c);
263
264         c = blobmsg_open_array(&status, "radio_access_technology");
265         for (i = 0; i < res.data.radio_access_technology_n; i++) {
266                 const char *r = "unknown";
267                 int r_i = res.data.radio_access_technology[i].radio_interface;
268
269                 info = blobmsg_open_table(&status, NULL);
270                 blobmsg_add_u32(&status, "mcc", res.data.radio_access_technology[i].mcc);
271                 blobmsg_add_u32(&status, "mnc", res.data.radio_access_technology[i].mnc);
272                 if (r_i >= 0 && r_i < ARRAY_SIZE(radio))
273                         r = radio[r_i];
274
275                 blobmsg_add_string(&status, "radio", r);
276                 blobmsg_close_table(&status, info);
277         }
278         blobmsg_close_array(&status, c);
279
280         blobmsg_close_table(&status, t);
281 }
282
283 static enum qmi_cmd_result
284 cmd_nas_network_scan_prepare(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, char *arg)
285 {
286         struct qmi_nas_network_scan_request sreq = {};
287
288         qmi_set_nas_network_scan_request(msg, &sreq);
289         return QMI_CMD_REQUEST;
290 }