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