~cyphermox/ubuntu/natty/ofono/release-0.41

« back to all changes in this revision

Viewing changes to drivers/isimodem/sms.c

  • Committer: Mathieu Trudel-Lapierre
  • Date: 2011-02-11 02:17:20 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: mathieu-tl@ubuntu.com-20110211021720-cvxc3erw1keomunj
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
#include <glib.h>
35
35
 
 
36
#include <gisi/message.h>
36
37
#include <gisi/client.h>
37
38
#include <gisi/iter.h>
38
39
 
46
47
#include "sms.h"
47
48
#include "debug.h"
48
49
 
 
50
/* This is a straightforward copy of the EF_smsp structure */
 
51
struct sim_efsmsp{
 
52
        uint8_t absent;
 
53
        uint8_t tp_pid;
 
54
        uint8_t tp_dcs;
 
55
        uint8_t tp_vp;
 
56
        uint8_t dst[12];
 
57
        uint8_t sca[12];
 
58
        uint8_t alphalen;
 
59
        uint8_t filler[3];
 
60
        uint16_t alpha[17];
 
61
};
 
62
 
 
63
/* Sub-block used by PN_SMS */
 
64
struct sms_params {
 
65
        uint8_t location;
 
66
        uint8_t absent;
 
67
        uint8_t tp_pid;
 
68
        uint8_t tp_dcs;
 
69
        uint8_t dst[12];
 
70
        uint8_t sca[12];
 
71
        uint8_t tp_vp;
 
72
        uint8_t alphalen;
 
73
        uint8_t filler[2];
 
74
        uint16_t alpha[17];
 
75
};
 
76
 
 
77
struct sms_report {
 
78
        uint8_t type;
 
79
        uint8_t cause;
 
80
        uint8_t ref;
 
81
};
 
82
 
 
83
struct sms_status {
 
84
        uint8_t status;
 
85
        uint8_t ref;
 
86
        uint8_t route;
 
87
        uint8_t cseg;   /* Current segment */
 
88
        uint8_t tseg;   /* Total segments */
 
89
};
 
90
 
 
91
struct sms_addr {
 
92
        uint8_t type;
 
93
        uint8_t len;
 
94
        uint8_t *data;
 
95
};
 
96
 
 
97
struct sms_common {
 
98
        uint8_t len;
 
99
        uint8_t *data;
 
100
};
 
101
 
49
102
struct sms_data {
50
103
        GIsiClient *client;
51
104
        GIsiClient *sim;
52
 
        /* This is a straightforward copy of the EF_smsp structure */
53
 
        struct sim_parameters {
54
 
                uint8_t      absent;
55
 
                uint8_t      tp_pid;
56
 
                uint8_t      tp_dcs;
57
 
                uint8_t      tp_vp;
58
 
                uint8_t      dst[12];
59
 
                uint8_t      sca[12];
60
 
                uint8_t      alphalen;
61
 
                uint8_t      filler[3];
62
 
                uint16_t     alpha[17];
63
 
        } params;
 
105
        struct sim_efsmsp params;
64
106
};
65
107
 
66
 
static gboolean sca_query_resp_cb(GIsiClient *client,
67
 
                                        const void *restrict data, size_t len,
68
 
                                        uint16_t object, void *opaque)
69
 
{
70
 
        const uint8_t *msg = data;
71
 
        struct isi_cb_data *cbd = opaque;
 
108
static gboolean check_sim_status(const GIsiMessage *msg, uint8_t msgid,
 
109
                                        uint8_t service)
 
110
{
 
111
        uint8_t type;
 
112
        uint8_t cause;
 
113
 
 
114
        if (g_isi_msg_error(msg) < 0) {
 
115
                DBG("Error: %s", strerror(-g_isi_msg_error(msg)));
 
116
                return FALSE;
 
117
        }
 
118
 
 
119
        if (g_isi_msg_id(msg) != msgid) {
 
120
                DBG("Unexpected msg: %s",
 
121
                        sms_message_id_name(g_isi_msg_id(msg)));
 
122
                return FALSE;
 
123
        }
 
124
 
 
125
        if (!g_isi_msg_data_get_byte(msg, 0, &type))
 
126
                return FALSE;
 
127
 
 
128
        if (type != service) {
 
129
                DBG("Unexpected service type: 0x%02X", type);
 
130
                return FALSE;
 
131
        }
 
132
 
 
133
        if (!g_isi_msg_data_get_byte(msg, 1, &cause))
 
134
                return FALSE;
 
135
 
 
136
        if (cause != SIM_SERV_OK) {
 
137
                DBG("Request failed: %s", sim_isi_cause_name(cause));
 
138
                return FALSE;
 
139
        }
 
140
 
 
141
        return TRUE;
 
142
}
 
143
 
 
144
static gboolean check_sms_status(const GIsiMessage *msg, uint8_t msgid)
 
145
{
 
146
        uint8_t cause;
 
147
 
 
148
        if (g_isi_msg_error(msg) < 0) {
 
149
                DBG("Error: %s", strerror(-g_isi_msg_error(msg)));
 
150
                return FALSE;
 
151
        }
 
152
 
 
153
        if (g_isi_msg_id(msg) != msgid) {
 
154
                DBG("Unexpected msg: %s",
 
155
                        sms_message_id_name(g_isi_msg_id(msg)));
 
156
                return FALSE;
 
157
        }
 
158
 
 
159
        if (!g_isi_msg_data_get_byte(msg, 0, &cause)) {
 
160
                DBG("Unable to parse cause");
 
161
                return FALSE;
 
162
        }
 
163
 
 
164
        if (cause == SMS_OK)
 
165
                return TRUE;
 
166
 
 
167
        if (cause == SMS_ERR_PP_RESERVED) {
 
168
                DBG("Request failed: 0x%02"PRIx8" (%s).\n\n  Unable to "
 
169
                        "bootstrap SMS routing.\n  It appears some other "
 
170
                        "component is already\n  registered as the SMS "
 
171
                        "routing endpoint.\n  As a consequence, "
 
172
                        "only sending SMSs is going to work.\n\n",
 
173
                        cause, sms_isi_cause_name(cause));
 
174
                return TRUE;
 
175
        }
 
176
 
 
177
        DBG("Request failed: %s", sms_isi_cause_name(cause));
 
178
        return FALSE;
 
179
}
 
180
 
 
181
static void sca_query_resp_cb(const GIsiMessage *msg, void *data)
 
182
{
 
183
        struct isi_cb_data *cbd = data;
72
184
        struct ofono_sms *sms = cbd->user;
73
185
        struct sms_data *sd = ofono_sms_get_data(sms);
74
186
        ofono_sms_sca_query_cb_t cb = cbd->cb;
75
187
 
76
188
        struct ofono_phone_number sca;
 
189
        struct sms_params *info;
 
190
        size_t len = sizeof(struct sms_params);
77
191
        uint8_t bcd_len;
78
192
 
79
 
        /* Nicely aligned. */
80
 
        struct {
81
 
                uint8_t      status;
82
 
                uint8_t      absent;
83
 
                uint8_t      tp_pid;
84
 
                uint8_t      tp_dcs;
85
 
                uint8_t      dst[12];
86
 
                uint8_t      sca[12];
87
 
                uint8_t      tp_vp;
88
 
                uint8_t      alphalen;
89
 
                uint8_t      filler[2];
90
 
                uint16_t     alpha[17];
91
 
        } params;
92
 
 
93
 
        if (!msg) {
94
 
                DBG("ISI client error: %d", g_isi_client_error(client));
95
 
                goto error;
96
 
        }
97
 
 
98
 
        if (len < 31 || msg[0] != SIM_SMS_RESP || msg[1] != READ_PARAMETER)
99
 
                return FALSE;
100
 
 
101
 
        if (msg[3] != SIM_SERV_OK)
102
 
                goto error;
103
 
 
104
 
        memset(&params, 0, sizeof(params));
105
 
        if (len  > 3 + sizeof(params))
106
 
                len = 3 + sizeof(params);
107
 
        memcpy(&params, msg + 3, len - 3);
108
 
 
109
 
        if (params.alphalen > 17)
110
 
                params.alphalen = 17;
111
 
        else if (params.alphalen < 1)
112
 
                params.alphalen = 1;
113
 
        params.alpha[params.alphalen - 1] = '\0';
114
 
 
115
 
        sd->params.absent = params.absent;
116
 
        sd->params.tp_pid = params.tp_pid;
117
 
        sd->params.tp_dcs = params.tp_dcs;
118
 
        sd->params.tp_vp = params.tp_vp;
119
 
        memcpy(sd->params.dst, params.dst, sizeof(sd->params.dst));
120
 
        memcpy(sd->params.sca, params.sca, sizeof(sd->params.sca));
121
 
        sd->params.alphalen = params.alphalen;
122
 
        memcpy(sd->params.alpha, params.alpha, sizeof(sd->params.alpha));
 
193
        if (!check_sim_status(msg, SIM_SMS_RESP, READ_PARAMETER))
 
194
                goto error;
 
195
 
 
196
        if (!g_isi_msg_data_get_struct(msg, 2, (const void **) &info, len))
 
197
                goto error;
 
198
 
 
199
        if (info->alphalen > 17)
 
200
                info->alphalen = 17;
 
201
        else if (info->alphalen < 1)
 
202
                info->alphalen = 1;
 
203
 
 
204
        info->alpha[info->alphalen - 1] = '\0';
 
205
 
 
206
        sd->params.absent = info->absent;
 
207
        sd->params.tp_pid = info->tp_pid;
 
208
        sd->params.tp_dcs = info->tp_dcs;
 
209
        sd->params.tp_vp = info->tp_vp;
 
210
 
 
211
        memcpy(sd->params.dst, info->dst, sizeof(sd->params.dst));
 
212
        memcpy(sd->params.sca, info->sca, sizeof(sd->params.sca));
 
213
 
 
214
        sd->params.alphalen = info->alphalen;
 
215
        memcpy(sd->params.alpha, info->alpha, sizeof(sd->params.alpha));
123
216
 
124
217
        /*
125
 
         * Bitmask indicating absense of parameters --
 
218
         * Bitmask indicating absence of parameters --
126
219
         * If second bit is set it indicates that the SCA is absent
127
220
         */
128
 
        if (params.absent & 0x2)
129
 
                goto error;
130
 
 
131
 
        bcd_len = params.sca[0];
132
 
 
133
 
        if (bcd_len <= 1 || bcd_len > 12)
134
 
                goto error;
135
 
 
136
 
        extract_bcd_number(params.sca + 2, bcd_len - 1, sca.number);
137
 
        sca.type = 0x80 | params.sca[1];
 
221
        if (info->absent & 0x2)
 
222
                goto error;
 
223
 
 
224
        bcd_len = info->sca[0];
 
225
 
 
226
        if (bcd_len == 0 || bcd_len > 12)
 
227
                goto error;
 
228
 
 
229
        extract_bcd_number(info->sca + 2, bcd_len - 1, sca.number);
 
230
        sca.type = 0x80 | info->sca[1];
138
231
 
139
232
        CALLBACK_WITH_SUCCESS(cb, &sca, cbd->data);
140
 
        return TRUE;
 
233
        return;
141
234
 
142
235
error:
143
236
        CALLBACK_WITH_FAILURE(cb, NULL, cbd->data);
144
 
        return TRUE;
145
237
}
146
238
 
147
239
static void isi_sca_query(struct ofono_sms *sms,
150
242
        struct sms_data *sd = ofono_sms_get_data(sms);
151
243
        struct isi_cb_data *cbd = isi_cb_data_new(sms, cb, data);
152
244
 
153
 
        uint8_t msg[] = {
 
245
        const uint8_t msg[] = {
154
246
                SIM_SMS_REQ,
155
247
                READ_PARAMETER,
156
248
                1,      /* Location, default is 1 */
157
249
        };
158
250
 
159
 
        if (!cbd || !sd)
 
251
        if (cbd == NULL || sd == NULL || sd->sim == NULL)
160
252
                goto error;
161
253
 
162
 
        if (g_isi_send(sd->sim, msg, sizeof(msg), SIM_TIMEOUT,
163
 
                        sca_query_resp_cb, cbd, g_free))
 
254
        if (g_isi_client_send(sd->sim, msg, sizeof(msg),
 
255
                                sca_query_resp_cb, cbd, g_free))
164
256
                return;
165
257
 
166
258
error:
168
260
        g_free(cbd);
169
261
}
170
262
 
171
 
static gboolean sca_set_resp_cb(GIsiClient *client,
172
 
                                const void *restrict data, size_t len,
173
 
                                uint16_t object, void *opaque)
 
263
static void sca_set_resp_cb(const GIsiMessage *msg, void *data)
174
264
{
175
 
        const uint8_t *msg = data;
176
 
        struct isi_cb_data *cbd = opaque;
 
265
        struct isi_cb_data *cbd = data;
177
266
        ofono_sms_sca_set_cb_t cb = cbd->cb;
178
267
 
179
 
        if (!msg) {
180
 
                DBG("ISI client error: %d", g_isi_client_error(client));
181
 
                goto error;
 
268
        if (!check_sim_status(msg, SIM_SMS_RESP, UPDATE_PARAMETER)) {
 
269
                CALLBACK_WITH_FAILURE(cb, cbd->data);
 
270
                return;
182
271
        }
183
272
 
184
 
        if (len < 3 || msg[0] != SIM_SMS_RESP || msg[1] != UPDATE_PARAMETER)
185
 
                return FALSE;
186
 
 
187
 
        if (msg[2] != SIM_SERV_OK)
188
 
                goto error;
189
 
 
190
273
        CALLBACK_WITH_SUCCESS(cb, cbd->data);
191
 
        return TRUE;
192
 
 
193
 
error:
194
 
        CALLBACK_WITH_FAILURE(cb, cbd->data);
195
 
        return TRUE;
196
274
}
197
275
 
198
276
static void isi_sca_set(struct ofono_sms *sms,
209
287
                1,      /* Location, default is 1 */
210
288
        };
211
289
 
212
 
        struct iovec iov[] = {
 
290
        struct iovec iov[2] = {
213
291
                { msg, sizeof(msg) },
214
292
                { &sd->params, sizeof(sd->params) },
215
293
        };
216
294
 
217
 
        if (!cbd || !sd)
 
295
        if (cbd == NULL || sd == NULL)
218
296
                goto error;
219
297
 
220
298
        bcd = sd->params.sca;
224
302
        bcd[0] = 1 + (strlen(sca->number) + 1) / 2;
225
303
        bcd[1] = sca->type & 0xFF;
226
304
 
227
 
        if (g_isi_vsend(sd->sim, iov, G_N_ELEMENTS(iov), SIM_TIMEOUT,
228
 
                        sca_set_resp_cb, cbd, g_free))
 
305
        if (g_isi_client_vsend(sd->sim, iov, 2, sca_set_resp_cb, cbd, g_free))
229
306
                return;
230
307
 
231
308
error:
233
310
        g_free(cbd);
234
311
}
235
312
 
236
 
static gboolean submit_resp_cb(GIsiClient *client,
237
 
                                const void *restrict data, size_t len,
238
 
                                uint16_t object, void *opaque)
 
313
static void submit_resp_cb(const GIsiMessage *msg, void *data)
239
314
{
240
 
        const uint8_t *msg = data;
241
 
        struct isi_cb_data *cbd = opaque;
 
315
        struct isi_cb_data *cbd = data;
242
316
        ofono_sms_submit_cb_t cb = cbd->cb;
243
 
 
244
 
        int mr = -1;
 
317
        struct sms_report *report;
 
318
        size_t len = sizeof(struct sms_report);
245
319
        GIsiSubBlockIter iter;
246
320
 
247
 
        if (!msg) {
248
 
                DBG("ISI client error: %d", g_isi_client_error(client));
249
 
                goto error;
250
 
        }
251
 
 
252
 
        if (len < 3 || msg[0] != SMS_MESSAGE_SEND_RESP)
253
 
                return FALSE;
254
 
 
255
 
        for (g_isi_sb_iter_init(&iter, msg, len, 3);
256
 
                g_isi_sb_iter_is_valid(&iter);
257
 
                g_isi_sb_iter_next(&iter)) {
258
 
 
259
 
                uint8_t type;
260
 
                uint8_t cause;
261
 
                uint8_t ref;
262
 
 
263
 
                switch (g_isi_sb_iter_get_id(&iter)) {
264
 
 
265
 
                case SMS_GSM_REPORT:
266
 
 
267
 
                        if (!g_isi_sb_iter_get_byte(&iter, &type, 2)
268
 
                                || !g_isi_sb_iter_get_byte(&iter, &cause, 3)
269
 
                                || !g_isi_sb_iter_get_byte(&iter, &ref, 4))
270
 
                                goto error;
271
 
 
272
 
                        if (cause != 0) {
273
 
                                DBG("Submit error: 0x%"PRIx8" (type 0x%"PRIx8")",
274
 
                                        cause, type);
275
 
                                goto error;
276
 
                        }
277
 
 
278
 
                        DBG("cause=0x%"PRIx8", type 0x%"PRIx8", mr=0x%"PRIx8,
279
 
                                        cause, type, ref);
280
 
 
281
 
                        mr = (int)ref;
282
 
                        break;
283
 
 
284
 
                default:
285
 
                        DBG("skipped sub-block: %s (%zu bytes)",
286
 
                                sms_subblock_name(g_isi_sb_iter_get_id(&iter)),
287
 
                                g_isi_sb_iter_get_len(&iter));
288
 
 
289
 
                }
290
 
        }
291
 
 
292
 
        if (mr == -1)
293
 
                goto error;
294
 
 
295
 
        CALLBACK_WITH_SUCCESS(cb, mr, cbd->data);
296
 
        return TRUE;
 
321
        if (!check_sms_status(msg, SMS_MESSAGE_SEND_RESP))
 
322
                goto error;
 
323
 
 
324
        for (g_isi_sb_iter_init(&iter, msg, 2);
 
325
                        g_isi_sb_iter_is_valid(&iter);
 
326
                        g_isi_sb_iter_next(&iter)) {
 
327
 
 
328
                if (g_isi_sb_iter_get_id(&iter) != SMS_GSM_REPORT)
 
329
                        continue;
 
330
 
 
331
                if (!g_isi_sb_iter_get_struct(&iter, (void **) &report, len, 2))
 
332
                        goto error;
 
333
 
 
334
                if (report->cause != SMS_OK)
 
335
                        goto error;
 
336
 
 
337
                CALLBACK_WITH_SUCCESS(cb, report->ref, cbd->data);
 
338
                return;
 
339
        }
297
340
 
298
341
error:
299
342
        CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
300
 
        return TRUE;
301
343
}
302
344
 
303
345
static void isi_submit(struct ofono_sms *sms, unsigned char *pdu,
350
392
                { sca_sb, sca_sb_len },
351
393
        };
352
394
 
353
 
        if (!cbd || !sd)
 
395
        if (cbd == NULL || sd == NULL)
354
396
                goto error;
355
397
 
356
398
        if (use_sca) {
358
400
                memcpy(sca_sb + 4, pdu, sca_sb[3]);
359
401
        }
360
402
 
361
 
        /* Modem seems to time out SMS_MESSAGE_SEND_REQ in 5 seconds */
362
 
        /* Wait normal timeout plus the modem timeout */
363
 
        if (g_isi_vsend(sd->client, iov, G_N_ELEMENTS(iov), SMS_TIMEOUT + 5,
364
 
                        submit_resp_cb, cbd, g_free))
 
403
        /*
 
404
         * Modem seems to time out SMS_MESSAGE_SEND_REQ in 5 seconds.
 
405
         * Wait normal timeout plus the modem timeout.
 
406
         */
 
407
        if (g_isi_client_vsend_with_timeout(sd->client, iov, 4,
 
408
                                SMS_TIMEOUT + 5,
 
409
                                submit_resp_cb, cbd, g_free))
365
410
                return;
366
411
 
367
412
error:
369
414
        g_free(cbd);
370
415
}
371
416
 
372
 
static void send_status_ind_cb(GIsiClient *client,
373
 
                                const void *restrict data, size_t len,
374
 
                                uint16_t object, void *opaque)
375
 
{
376
 
        const uint8_t *msg = data;
377
 
 
378
 
        if (!msg || len < 6 || msg[0] != SMS_MESSAGE_SEND_STATUS_IND)
379
 
                return;
380
 
 
381
 
        DBG("status=0x%"PRIx8", mr=0x%"PRIx8", route=0x%"PRIx8
 
417
static void isi_bearer_query(struct ofono_sms *sms,
 
418
                                ofono_sms_bearer_query_cb_t cb, void *data)
 
419
{
 
420
        DBG("Not implemented");
 
421
        CALLBACK_WITH_FAILURE(cb, -1, data);
 
422
}
 
423
 
 
424
static void isi_bearer_set(struct ofono_sms *sms, int bearer,
 
425
                                ofono_sms_bearer_set_cb_t cb, void *data)
 
426
{
 
427
        DBG("Not implemented");
 
428
        CALLBACK_WITH_FAILURE(cb, data);
 
429
}
 
430
 
 
431
static void send_status_ind_cb(const GIsiMessage *msg, void *data)
 
432
{
 
433
        struct sms_status *info;
 
434
        size_t len = sizeof(struct sms_status);
 
435
 
 
436
        DBG("");
 
437
 
 
438
        if (g_isi_msg_id(msg) != SMS_MESSAGE_SEND_STATUS_IND)
 
439
                return;
 
440
 
 
441
        if (!g_isi_msg_data_get_struct(msg, 0, (const void **) &info, len))
 
442
                return;
 
443
 
 
444
        DBG("status=0x%"PRIx8", ref=0x%"PRIx8", route=0x%"PRIx8
382
445
                ", cseg=0x%"PRIx8", tseg=0x%"PRIx8,
383
 
                msg[1], msg[2], msg[3], msg[4], msg[5]);
 
446
                info->status, info->ref, info->route, info->cseg,
 
447
                info->tseg);
384
448
 
385
449
        DBG("TODO: Status notification");
386
450
}
387
451
 
388
 
static gboolean report_resp_cb(GIsiClient *client,
389
 
                                const void *restrict data, size_t len,
390
 
                                uint16_t object, void *opaque)
 
452
static void report_resp_cb(const GIsiMessage *msg, void *data)
391
453
{
392
 
        const uint8_t *msg = data;
393
 
 
394
 
        if (!msg) {
395
 
                DBG("ISI client error: %d", g_isi_client_error(client));
396
 
                return TRUE;
397
 
        }
398
 
 
399
 
        if (len < 3 || msg[0] != SMS_GSM_RECEIVED_PP_REPORT_RESP)
400
 
                return FALSE;
401
 
 
402
 
        DBG("Report resp cause=0x%"PRIx8, msg[1]);
403
 
 
404
 
        return TRUE;
 
454
        uint8_t cause;
 
455
 
 
456
        if (g_isi_msg_error(msg) < 0)
 
457
                return;
 
458
 
 
459
        if (g_isi_msg_id(msg) != SMS_GSM_RECEIVED_PP_REPORT_RESP)
 
460
                return;
 
461
 
 
462
        if (!g_isi_msg_data_get_byte(msg, 0, &cause))
 
463
                return;
 
464
 
 
465
        DBG("Report resp cause=0x%"PRIx8, cause);
405
466
}
406
467
 
407
468
static gboolean send_deliver_report(GIsiClient *client, gboolean success)
422
483
                0, 0, 0,        /* Filler */
423
484
                0,              /* Sub blocks */
424
485
        };
425
 
 
426
 
        return g_isi_send(client, msg, sizeof(msg), SMS_TIMEOUT,
427
 
                                report_resp_cb, NULL, NULL) != NULL;
428
 
}
429
 
 
430
 
static void routing_ntf_cb(GIsiClient *client,
431
 
                                const void *restrict data, size_t len,
432
 
                                uint16_t object, void *opaque)
433
 
{
434
 
        const uint8_t *msg = data;
435
 
        struct ofono_sms *sms = opaque;
 
486
        size_t len = sizeof(msg);
 
487
 
 
488
        return g_isi_client_send(client, msg, len, report_resp_cb, NULL, NULL);
 
489
}
 
490
 
 
491
static gboolean parse_sms_address(GIsiSubBlockIter *iter, struct sms_addr *add)
 
492
{
 
493
        add->data = NULL;
 
494
 
 
495
        if (!g_isi_sb_iter_get_byte(iter, &add->type, 2))
 
496
                return FALSE;
 
497
 
 
498
        if (!g_isi_sb_iter_get_byte(iter, &add->len, 3))
 
499
                return FALSE;
 
500
 
 
501
        if (add->len == 0)
 
502
                return FALSE;
 
503
 
 
504
        if (!g_isi_sb_iter_get_struct(iter, (void **) &add->data, add->len, 4))
 
505
                return FALSE;
 
506
 
 
507
        return TRUE;
 
508
}
 
509
 
 
510
static gboolean parse_sms_tpdu(GIsiSubBlockIter *iter, struct sms_common *com)
 
511
{
 
512
        com->data = NULL;
 
513
 
 
514
        if (!g_isi_sb_iter_get_byte(iter, &com->len, 2))
 
515
                return FALSE;
 
516
 
 
517
        if (com->len == 0)
 
518
                return FALSE;
 
519
 
 
520
        if (!g_isi_sb_iter_get_struct(iter, (void **) &com->data, com->len, 4))
 
521
                return FALSE;
 
522
 
 
523
        return TRUE;
 
524
}
 
525
 
 
526
static gboolean parse_gsm_tpdu(GIsiSubBlockIter *parent, struct sms_addr *add,
 
527
                                struct sms_common *com)
 
528
{
436
529
        GIsiSubBlockIter iter;
437
530
 
438
 
        uint8_t *sca = NULL;
439
 
        uint8_t sca_len = 0;
440
 
        uint8_t *tpdu = NULL;
441
 
        uint8_t tpdu_len = 0;
442
 
 
443
 
        unsigned char pdu[176];
444
 
 
445
 
        if (!msg || len < 7 || msg[0] != SMS_PP_ROUTING_NTF
446
 
                || msg[3] != SMS_GSM_TPDU)
447
 
                return;
448
 
 
449
 
        for (g_isi_sb_iter_init(&iter, msg, len, 7);
450
 
                g_isi_sb_iter_is_valid(&iter);
451
 
                g_isi_sb_iter_next(&iter)) {
 
531
        for (g_isi_sb_subiter_init(parent, &iter, 2);
 
532
                        g_isi_sb_iter_is_valid(&iter);
 
533
                        g_isi_sb_iter_next(&iter)) {
452
534
 
453
535
                switch (g_isi_sb_iter_get_id(&iter)) {
454
 
 
455
 
                uint8_t type;
456
 
                void *data;
457
 
                uint8_t data_len;
458
 
 
459
536
                case SMS_ADDRESS:
460
537
 
461
 
                        if (!g_isi_sb_iter_get_byte(&iter, &type, 2)
462
 
                                || !g_isi_sb_iter_get_byte(&iter, &data_len, 3)
463
 
                                || !g_isi_sb_iter_get_data(&iter, &data, 4)
464
 
                                || type != SMS_GSM_0411_ADDRESS)
465
 
                                break;
466
 
 
467
 
                        sca = data;
468
 
                        sca_len = data_len;
 
538
                        if (!parse_sms_address(&iter, add))
 
539
                                return FALSE;
 
540
 
 
541
                        if (add->type != SMS_GSM_0411_ADDRESS)
 
542
                                return FALSE;
 
543
 
469
544
                        break;
470
545
 
471
546
                case SMS_COMMON_DATA:
472
547
 
473
 
                        if (!g_isi_sb_iter_get_byte(&iter, &data_len, 2)
474
 
                                || !g_isi_sb_iter_get_data(&iter, &data, 4))
475
 
                                break;
 
548
                        if (!parse_sms_tpdu(&iter, com))
 
549
                                return FALSE;
476
550
 
477
 
                        tpdu = data;
478
 
                        tpdu_len = data_len;
479
551
                        break;
480
 
 
481
 
                default:
482
 
                        DBG("skipped sub-block: %s (%zu bytes)",
483
 
                                sms_subblock_name(g_isi_sb_iter_get_id(&iter)),
484
 
                                g_isi_sb_iter_get_len(&iter));
485
552
                }
486
553
        }
487
554
 
488
 
        if (!tpdu || !sca || tpdu_len + sca_len > sizeof(pdu))
489
 
                return;
490
 
 
491
 
        memcpy(pdu, sca, sca_len);
492
 
        memcpy(pdu + sca_len, tpdu, tpdu_len);
493
 
 
494
 
        ofono_sms_deliver_notify(sms, pdu, tpdu_len + sca_len, tpdu_len);
495
 
 
496
 
        /* FIXME: We should not ack the DELIVER unless it has been
 
555
        return TRUE;
 
556
}
 
557
 
 
558
static void routing_ntf_cb(const GIsiMessage *msg, void *data)
 
559
{
 
560
        struct ofono_sms *sms = data;
 
561
        struct sms_data *sd = ofono_sms_get_data(sms);
 
562
        struct sms_common tpdu;
 
563
        struct sms_addr addr;
 
564
        GIsiSubBlockIter iter;
 
565
 
 
566
        uint8_t pdu[176];
 
567
 
 
568
        if (g_isi_msg_id(msg) != SMS_PP_ROUTING_NTF)
 
569
                return;
 
570
 
 
571
        for (g_isi_sb_iter_init(&iter, msg, 2);
 
572
                        g_isi_sb_iter_is_valid(&iter);
 
573
                        g_isi_sb_iter_next(&iter)) {
 
574
 
 
575
                if (g_isi_sb_iter_get_id(&iter) != SMS_GSM_TPDU)
 
576
                        continue;
 
577
 
 
578
                if (!parse_gsm_tpdu(&iter, &addr, &tpdu))
 
579
                        return;
 
580
        }
 
581
 
 
582
        if (tpdu.data == NULL || addr.data == NULL ||
 
583
                        tpdu.len + addr.len > sizeof(pdu))
 
584
                return;
 
585
 
 
586
        memcpy(pdu, addr.data, addr.len);
 
587
        memcpy(pdu + addr.len, tpdu.data, tpdu.len);
 
588
 
 
589
        ofono_sms_deliver_notify(sms, pdu, tpdu.len + addr.len, tpdu.len);
 
590
 
 
591
        /*
 
592
         * FIXME: We should not ack the DELIVER unless it has been
497
593
         * reliably stored, i.e., written to disk. Currently, there is
498
594
         * no such indication from core, so we just blindly trust that
499
 
         * it did The Right Thing here. */
500
 
        send_deliver_report(client, TRUE);
 
595
         * it did The Right Thing here.
 
596
         */
 
597
        send_deliver_report(sd->client, TRUE);
501
598
}
502
599
 
503
 
static gboolean routing_resp_cb(GIsiClient *client,
504
 
                                const void *restrict data, size_t len,
505
 
                                uint16_t object, void *opaque)
 
600
static void routing_resp_cb(const GIsiMessage *msg, void *data)
506
601
{
507
 
        const unsigned char *msg = data;
508
 
        struct ofono_sms *sms = opaque;
509
 
 
510
 
        if (!msg) {
511
 
                DBG("ISI client error: %d", g_isi_client_error(client));
512
 
                goto error;
513
 
        }
514
 
 
515
 
        if (len < 3 || msg[0] != SMS_PP_ROUTING_RESP)
516
 
                goto error;
517
 
 
518
 
        if (msg[1] != SMS_OK) {
519
 
 
520
 
                if (msg[1] == SMS_ERR_PP_RESERVED) {
521
 
                        DBG("Request failed: 0x%02"PRIx8" (%s).\n\n  "
522
 
                                "Unable to bootstrap SMS routing.\n  "
523
 
                                "It appears some other component is "
524
 
                                "already\n  registered as the SMS "
525
 
                                "routing endpoint.\n  As a consequence, "
526
 
                                "receiving SMSs is NOT going to work.\n  "
527
 
                                "On the other hand, sending might work.\n\n",
528
 
                                msg[1], sms_isi_cause_name(msg[1]));
529
 
                        ofono_sms_register(sms);
530
 
                }
531
 
                return TRUE;
532
 
        }
533
 
 
534
 
        g_isi_subscribe(client, SMS_PP_ROUTING_NTF, routing_ntf_cb, sms);
 
602
        struct ofono_sms *sms = data;
 
603
        struct sms_data *sd = ofono_sms_get_data(sms);
 
604
 
 
605
        if (!check_sms_status(msg, SMS_PP_ROUTING_RESP))
 
606
                return;
 
607
 
 
608
        g_isi_client_ntf_subscribe(sd->client, SMS_PP_ROUTING_NTF,
 
609
                                        routing_ntf_cb, sms);
535
610
 
536
611
        ofono_sms_register(sms);
537
 
        return TRUE;
538
 
 
539
 
error:
540
 
        DBG("Unable to bootstrap SMS routing.");
541
 
        return TRUE;
 
612
}
 
613
 
 
614
static void sim_reachable_cb(const GIsiMessage *msg, void *data)
 
615
{
 
616
        struct ofono_sms *sms = data;
 
617
        struct sms_data *sd = ofono_sms_get_data(sms);
 
618
 
 
619
        const uint8_t req[] = {
 
620
                SMS_PP_ROUTING_REQ,
 
621
                SMS_ROUTING_SET,
 
622
                0x01,  /* Sub-block count */
 
623
                SMS_GSM_ROUTING,
 
624
                0x08,  /* Sub-block length */
 
625
                SMS_GSM_TPDU_ROUTING,
 
626
                SMS_GSM_MT_ALL_TYPE,
 
627
                0x00, 0x00, 0x00,  /* Filler */
 
628
                0x00  /* Sub-sub-block count */
 
629
        };
 
630
        size_t len = sizeof(req);
 
631
 
 
632
        if (g_isi_msg_error(msg) < 0) {
 
633
                DBG("unable to find SIM resource");
 
634
                g_isi_client_destroy(sd->sim);
 
635
                sd->sim = NULL;
 
636
        }
 
637
 
 
638
        g_isi_client_ind_subscribe(sd->client, SMS_MESSAGE_SEND_STATUS_IND,
 
639
                                        send_status_ind_cb, sms);
 
640
        g_isi_client_send(sd->client, req, len, routing_resp_cb, sms, NULL);
 
641
}
 
642
 
 
643
static void sms_reachable_cb(const GIsiMessage *msg, void *data)
 
644
{
 
645
        struct ofono_sms *sms = data;
 
646
        struct sms_data *sd = ofono_sms_get_data(sms);
 
647
 
 
648
        if (g_isi_msg_error(msg) < 0) {
 
649
                DBG("unable to find SMS resource");
 
650
                return;
 
651
        }
 
652
 
 
653
        ISI_VERSION_DBG(msg);
 
654
 
 
655
        g_isi_client_verify(sd->sim, sim_reachable_cb, sms, NULL);
542
656
}
543
657
 
544
658
static int isi_sms_probe(struct ofono_sms *sms, unsigned int vendor,
545
659
                                void *user)
546
660
{
547
 
        GIsiModem *idx = user;
548
 
        struct sms_data *data = g_try_new0(struct sms_data, 1);
549
 
        const char *debug;
550
 
 
551
 
        const unsigned char msg[] = {
552
 
                SMS_PP_ROUTING_REQ,
553
 
                SMS_ROUTING_SET,
554
 
                0x01,  /* Sub-block count */
555
 
                SMS_GSM_ROUTING,
556
 
                0x08,  /* Sub-block length */
557
 
                SMS_GSM_TPDU_ROUTING,
558
 
                SMS_GSM_MT_ALL_TYPE,
559
 
                0x00, 0x00, 0x00,  /* Filler */
560
 
                0x00  /* Sub-sub-block count */
561
 
        };
562
 
 
563
 
        if (!data)
564
 
                return -ENOMEM;
565
 
 
566
 
        data->params.absent = 0xff;
567
 
        data->params.alphalen = 1; /* Includes final UCS2-coded NUL */
568
 
 
569
 
        data->client = g_isi_client_create(idx, PN_SMS);
570
 
        if (!data->client)
571
 
                return -ENOMEM;
572
 
 
573
 
        data->sim = g_isi_client_create(idx, PN_SIM);
574
 
        if (!data->sim) {
575
 
                g_isi_client_destroy(data->client);
576
 
                return -ENOMEM;
577
 
        }
578
 
 
579
 
        ofono_sms_set_data(sms, data);
580
 
 
581
 
        debug = getenv("OFONO_ISI_DEBUG");
582
 
        if (debug && (strcmp(debug, "all") == 0 || strcmp(debug, "sms") == 0)) {
583
 
                g_isi_client_set_debug(data->client, sms_debug, NULL);
584
 
                g_isi_client_set_debug(data->sim, sim_debug, NULL);
585
 
        }
586
 
 
587
 
        g_isi_subscribe(data->client, SMS_MESSAGE_SEND_STATUS_IND,
588
 
                        send_status_ind_cb, sms);
589
 
        if (!g_isi_send(data->client, msg, sizeof(msg), SMS_TIMEOUT,
590
 
                        routing_resp_cb, sms, NULL))
591
 
                DBG("Failed to set SMS routing.");
 
661
        GIsiModem *modem = user;
 
662
        struct sms_data *sd = g_try_new0(struct sms_data, 1);
 
663
 
 
664
        if (sd == NULL)
 
665
                return -ENOMEM;
 
666
 
 
667
        sd->params.absent = 0xFF;
 
668
        sd->params.alphalen = 1; /* Includes final UCS2-coded NUL */
 
669
 
 
670
        sd->client = g_isi_client_create(modem, PN_SMS);
 
671
        if (sd->client == NULL)
 
672
                goto nomem;
 
673
 
 
674
        sd->sim = g_isi_client_create(modem, PN_SIM);
 
675
        if (sd->sim == NULL)
 
676
                goto nomem;
 
677
 
 
678
        g_isi_client_set_timeout(sd->client, SMS_TIMEOUT);
 
679
        g_isi_client_set_timeout(sd->sim, SIM_TIMEOUT);
 
680
 
 
681
        ofono_sms_set_data(sms, sd);
 
682
 
 
683
        g_isi_client_verify(sd->client, sms_reachable_cb, sms, NULL);
592
684
 
593
685
        return 0;
 
686
 
 
687
nomem:
 
688
        g_isi_client_destroy(sd->client);
 
689
        g_free(sd);
 
690
        return -ENOMEM;
594
691
}
595
692
 
596
693
static void isi_sms_remove(struct ofono_sms *sms)
597
694
{
598
 
        struct sms_data *data = ofono_sms_get_data(sms);
 
695
        struct sms_data *sd = ofono_sms_get_data(sms);
599
696
 
600
 
        const unsigned char msg[] = {
 
697
        const uint8_t msg[] = {
601
698
                SMS_PP_ROUTING_REQ,
602
699
                SMS_ROUTING_RELEASE,
603
700
                0x01,  /* Sub-block count */
609
706
                0x00  /* Sub-sub-block count */
610
707
        };
611
708
 
612
 
        if (!data)
613
 
                return;
614
 
 
615
709
        ofono_sms_set_data(sms, NULL);
616
710
 
 
711
        if (sd == NULL)
 
712
                return;
 
713
 
617
714
        /*
618
715
         * Send a promiscuous routing release, so as not to
619
716
         * hog resources unnecessarily after being removed
620
717
         */
621
 
        g_isi_send(data->client, msg, sizeof(msg),
622
 
                        SMS_TIMEOUT, NULL, NULL, NULL);
623
 
        g_isi_client_destroy(data->client);
624
 
        g_isi_client_destroy(data->sim);
625
 
        g_free(data);
 
718
        g_isi_client_send(sd->client, msg, sizeof(msg), NULL, NULL, NULL);
 
719
        g_isi_client_destroy(sd->client);
 
720
        g_isi_client_destroy(sd->sim);
 
721
        g_free(sd);
626
722
}
627
723
 
628
724
static struct ofono_sms_driver driver = {
631
727
        .remove                 = isi_sms_remove,
632
728
        .sca_query              = isi_sca_query,
633
729
        .sca_set                = isi_sca_set,
634
 
        .submit                 = isi_submit
 
730
        .submit                 = isi_submit,
 
731
        .bearer_query           = isi_bearer_query,
 
732
        .bearer_set             = isi_bearer_set,
635
733
};
636
734
 
637
 
void isi_sms_init()
 
735
void isi_sms_init(void)
638
736
{
639
737
        ofono_sms_driver_register(&driver);
640
738
}
641
739
 
642
 
void isi_sms_exit()
 
740
void isi_sms_exit(void)
643
741
{
644
742
        ofono_sms_driver_unregister(&driver);
645
743
}