Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / sys / dvb / camswclient.c
1 /*
2  * camswclient.c - GStreamer softcam client
3  * Copyright (C) 2007 Alessandro Decina
4  * 
5  * Authors:
6  *   Alessandro Decina <alessandro@nnva.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <gst/gst.h>
32
33 #include "camswclient.h"
34 #include "cam.h"
35 #include "camutils.h"
36
37 #define GST_CAT_DEFAULT cam_debug_cat
38 #define UNIX_PATH_MAX 108
39
40 CamSwClient *
41 cam_sw_client_new (void)
42 {
43   CamSwClient *client = g_new0 (CamSwClient, 1);
44
45   client->state = CAM_SW_CLIENT_STATE_CLOSED;
46
47   return client;
48 }
49
50 static void
51 reset_state (CamSwClient * client)
52 {
53   if (client->sock)
54     close (client->sock);
55
56   if (client->sock_path)
57     g_free (client->sock_path);
58 }
59
60 void
61 cam_sw_client_free (CamSwClient * client)
62 {
63   g_return_if_fail (client != NULL);
64
65   if (client->state != CAM_SW_CLIENT_STATE_CLOSED)
66     GST_WARNING ("client not in CLOSED state when free'd");
67
68   reset_state (client);
69   g_free (client);
70 }
71
72 gboolean
73 cam_sw_client_open (CamSwClient * client, const char *sock_path)
74 {
75   struct sockaddr_un addr;
76   int ret;
77
78   g_return_val_if_fail (client != NULL, FALSE);
79   g_return_val_if_fail (client->state == CAM_SW_CLIENT_STATE_CLOSED, FALSE);
80   g_return_val_if_fail (sock_path != NULL, FALSE);
81
82   addr.sun_family = AF_UNIX;
83   strncpy (addr.sun_path, sock_path, sizeof (addr.sun_path));
84
85   GST_INFO ("connecting to softcam socket: %s", sock_path);
86   client->sock = socket (PF_UNIX, SOCK_STREAM, 0);
87   ret =
88       connect (client->sock, (struct sockaddr *) &addr,
89       sizeof (struct sockaddr_un));
90   if (ret != 0) {
91     GST_ERROR ("error opening softcam socket %s, error: %s",
92         sock_path, strerror (errno));
93
94     return FALSE;
95   }
96
97   client->sock_path = g_strdup (sock_path);
98   client->state = CAM_SW_CLIENT_STATE_OPEN;
99
100   return TRUE;
101 }
102
103 void
104 cam_sw_client_close (CamSwClient * client)
105 {
106   g_return_if_fail (client != NULL);
107   g_return_if_fail (client->state == CAM_SW_CLIENT_STATE_OPEN);
108
109   reset_state (client);
110   client->state = CAM_SW_CLIENT_STATE_CLOSED;
111 }
112
113 static void
114 send_ca_pmt (CamSwClient * client, GstStructure * pmt,
115     guint8 list_management, guint8 cmd_id)
116 {
117   guint8 *buffer;
118   guint buffer_size;
119   guint8 *ca_pmt;
120   guint ca_pmt_size;
121   guint length_field_len;
122   guint header_len;
123
124   ca_pmt = cam_build_ca_pmt (pmt, list_management, cmd_id, &ca_pmt_size);
125
126   length_field_len = cam_calc_length_field_size (ca_pmt_size);
127   header_len = 3 + length_field_len;
128   buffer_size = header_len + ca_pmt_size;
129
130   buffer = g_malloc0 (buffer_size);
131   memcpy (buffer + header_len, ca_pmt, ca_pmt_size);
132
133   /* ca_pmt resource_id */
134   buffer[0] = 0x9F;
135   buffer[1] = 0x80;
136   buffer[2] = 0x32;
137
138   cam_write_length_field (&buffer[3], ca_pmt_size);
139
140   if (write (client->sock, buffer, buffer_size) == -1) {
141     GST_WARNING ("write failed when sending pmt with errno: %d", errno);
142   }
143
144   g_free (ca_pmt);
145   g_free (buffer);
146 }
147
148 void
149 cam_sw_client_set_pmt (CamSwClient * client, GstStructure * pmt)
150 {
151   g_return_if_fail (client != NULL);
152   g_return_if_fail (pmt != NULL);
153
154   return send_ca_pmt (client, pmt, 0x03 /* only */ ,
155       0x01 /* ok_descrambling */ );
156 }
157
158 void
159 cam_sw_client_update_pmt (CamSwClient * client, GstStructure * pmt)
160 {
161   g_return_if_fail (client != NULL);
162   g_return_if_fail (pmt != NULL);
163
164   return send_ca_pmt (client, pmt, 0x05 /* update */ ,
165       0x01 /* ok_descrambling */ );
166 }