summaryrefslogtreecommitdiff
path: root/sys/dvb/camapplication.c
blob: cbc479acbcd9c761e802086f02a36a1ae9fb7ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * camapplication.c - GStreamer CAM (EN50221) Application Layer
 * Copyright (C) 2007 Alessandro Decina
 * 
 * Authors:
 *   Alessandro Decina <alessandro@nnva.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "camapplication.h"

#define GST_CAT_DEFAULT cam_debug_cat

static CamReturn open_session_request_cb (CamSL * sl,
    CamSLSession * session, CamSLResourceStatus * status);
static CamReturn session_opened_cb (CamSL * sl, CamSLSession * session);
static CamReturn session_closed_cb (CamSL * sl, CamSLSession * session);
static CamReturn session_data_cb (CamSL * sl,
    CamSLSession * session, guint8 * data, guint length);

static guint
resource_id_hash (gconstpointer key)
{
  guint resource_id = GPOINTER_TO_UINT (key);

  if (!CAM_AL_RESOURCE_ID_IS_PUBLIC (resource_id)) {
    /* private identifier, leave it as is */
    return resource_id;
  }

  /* public identifier, mask out the version number */
  return resource_id >> 6;
}

CamAL *
cam_al_new (CamSL * sl)
{
  CamAL *al = g_new0 (CamAL, 1);

  al->sl = sl;
  al->applications = g_hash_table_new (resource_id_hash, g_direct_equal);

  sl->user_data = al;
  sl->open_session_request = open_session_request_cb;
  sl->session_opened = session_opened_cb;
  sl->session_closed = session_closed_cb;
  sl->session_data = session_data_cb;

  return al;
}

void
cam_al_destroy (CamAL * al)
{
  g_hash_table_destroy (al->applications);
  g_free (al);
}

gboolean
cam_al_install (CamAL * al, CamALApplication * application)
{
  if (g_hash_table_lookup (al->applications,
          GINT_TO_POINTER (application->resource_id)) != NULL)
    return FALSE;

  application->al = al;

  g_hash_table_insert (al->applications,
      GINT_TO_POINTER (application->resource_id), application);

  return TRUE;
}

gboolean
cam_al_uninstall (CamAL * al, CamALApplication * application)
{
  gboolean ret;

  ret = g_hash_table_remove (al->applications,
      GINT_TO_POINTER (application->resource_id));

  return ret;
}

CamALApplication *
cam_al_get (CamAL * al, guint resource_id)
{
  return CAM_AL_APPLICATION (g_hash_table_lookup (al->applications,
          GINT_TO_POINTER (resource_id)));
}

void
_cam_al_application_init (CamALApplication * application)
{
  application->sessions = NULL;
}

void
_cam_al_application_destroy (CamALApplication * application)
{
  g_list_free (application->sessions);
}

static void
foreach_get_key (gpointer key, gpointer value, gpointer user_data)
{
  GList **lst = (GList **) user_data;

  *lst = g_list_append (*lst, key);
}

GList *
cam_al_get_resource_ids (CamAL * al)
{
  GList *resource_ids = NULL;

  g_hash_table_foreach (al->applications, foreach_get_key, &resource_ids);

  return resource_ids;
}

void
cam_al_calc_buffer_size (CamAL * al, guint body_length,
    guint * buffer_size, guint * offset)
{
  guint apdu_header_length;
  guint8 length_field_len;

  /* get the length of the lenght_field() */
  length_field_len = cam_calc_length_field_size (body_length);

  /* sum the APDU header */
  apdu_header_length = 3 + length_field_len;

  /* chain up to the session layer to get the size of the buffer that can
   * contain the whole APDU */
  cam_sl_calc_buffer_size (al->sl, apdu_header_length + body_length,
      buffer_size, offset);

  /* add the APDU header to the SPDU offset */
  *offset += apdu_header_length;
}

CamReturn
cam_al_application_write (CamALApplication * application,
    CamSLSession * session, guint tag, guint8 * buffer, guint buffer_size,
    guint body_length)
{
  guint length_field_len;
  guint apdu_header_length;
  guint8 *apdu;

  length_field_len = cam_calc_length_field_size (body_length);
  apdu_header_length = 3 + length_field_len;
  apdu = (buffer + buffer_size) - body_length - apdu_header_length;
  apdu[0] = tag >> 16;
  apdu[1] = (tag >> 8) & 0xFF;
  apdu[2] = tag & 0xFF;

  cam_write_length_field (&apdu[3], body_length);

  return cam_sl_session_write (session, buffer, buffer_size,
      apdu_header_length + body_length);
}

static CamReturn
open_session_request_cb (CamSL * sl, CamSLSession * session,
    CamSLResourceStatus * status)
{
  CamAL *al = CAM_AL (sl->user_data);
  CamALApplication *application;
  guint resource_id = session->resource_id;
  CamReturn ret;

  application = g_hash_table_lookup (al->applications,
      GINT_TO_POINTER (resource_id));
  if (application == NULL) {
    *status = CAM_SL_RESOURCE_STATUS_NOT_FOUND;

    return CAM_RETURN_OK;
  }

  if (CAM_AL_RESOURCE_ID_VERSION (application->resource_id)
      < CAM_AL_RESOURCE_ID_VERSION (resource_id)) {
    *status = CAM_SL_RESOURCE_STATUS_INVALID_VERSION;

    return CAM_RETURN_OK;
  }

  ret = application->session_request (application, session, status);
  if (CAM_FAILED (ret)) {
    *status = CAM_SL_RESOURCE_STATUS_NOT_FOUND;

    return ret;
  }

  if (*status == CAM_SL_RESOURCE_STATUS_OPEN) {
    session->user_data = application;
    application->sessions = g_list_append (application->sessions, session);
  }

  return CAM_RETURN_OK;
}

static CamReturn
session_opened_cb (CamSL * sl, CamSLSession * session)
{
  CamALApplication *application;

  application = CAM_AL_APPLICATION (session->user_data);
  if (application == NULL) {
    GST_ERROR ("session is established but has no application");
    return CAM_RETURN_APPLICATION_ERROR;
  }

  return application->open (application, session);
}

static CamReturn
session_closed_cb (CamSL * sl, CamSLSession * session)
{
  CamALApplication *application;
  CamReturn ret;
  GList *walk;

  application = CAM_AL_APPLICATION (session->user_data);
  if (application == NULL) {
    GST_ERROR ("session is established but has no application");
    return CAM_RETURN_APPLICATION_ERROR;
  }

  ret = application->close (application, session);
  for (walk = application->sessions; walk; walk = g_list_next (walk)) {
    CamSLSession *s = CAM_SL_SESSION (walk->data);

    if (s->session_nb == session->session_nb) {
      application->sessions = g_list_delete_link (application->sessions, walk);
      break;
    }
  }

  return ret;
}

static CamReturn
session_data_cb (CamSL * sl, CamSLSession * session, guint8 * data, guint size)
{
  CamALApplication *application;
  guint tag = 0;
  guint8 length_field_len;
  guint length;
  guint i;

  application = CAM_AL_APPLICATION (session->user_data);
  if (application == NULL) {
    GST_ERROR ("session is established but has no application");
    return CAM_RETURN_APPLICATION_ERROR;
  }

  if (size < 4) {
    GST_ERROR ("invalid APDU length %d", size);
    return CAM_RETURN_APPLICATION_ERROR;
  }

  for (i = 0; i < 3; ++i)
    tag = (tag << 8) | data[i];

  length_field_len = cam_read_length_field (&data[3], &length);

  if (length != size - 4) {
    GST_ERROR ("unexpected APDU length %d expected %d", length, size);

    return CAM_RETURN_APPLICATION_ERROR;
  }

  return application->data (application, session,
      tag, data + 3 + length_field_len, length);
}