1 /*
2     en50221 encoder An implementation for libdvb
3     an implementation for the en50221 session layer
4 
5     Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
6     Copyright (C) 2005 Julian Scheel (julian@jusst.de)
7     Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
8 
9     This library is free software; you can redistribute it and/or modify
10     it under the terms of the GNU Lesser General Public License as
11     published by the Free Software Foundation; either version 2.1 of
12     the License, or (at your option) any later version.
13 
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU Lesser General Public License for more details.
18 
19     You should have received a copy of the GNU Lesser General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22 */
23 
24 
25 #ifndef __EN50221_SESSION_H__
26 #define __EN50221_SESSION_H__
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <libdvben50221/en50221_transport.h>
35 
36 #define S_SCALLBACK_REASON_CAMCONNECTING  0x00	// CAM originated session connecting to resource (check for availability)
37 #define S_SCALLBACK_REASON_CAMCONNECTED   0x01	// CAM originated session connection established succesfully
38 #define S_SCALLBACK_REASON_CAMCONNECTFAIL 0x02	// CAM originated session connection failed
39 #define S_SCALLBACK_REASON_CONNECTED      0x03	// Host originated session ACKed by CAM.
40 #define S_SCALLBACK_REASON_CONNECTFAIL    0x04	// Host originated session NACKed by CAM.
41 #define S_SCALLBACK_REASON_CLOSE          0x05	// Session closed
42 #define S_SCALLBACK_REASON_TC_CONNECT     0x06	// A host originated transport connection has been established.
43 #define S_SCALLBACK_REASON_TC_CAMCONNECT  0x07	// A CAM originated transport connection has been established.
44 
45 
46 /**
47  * Opaque type representing a session layer.
48  */
49 struct en50221_session_layer;
50 
51 /**
52  * Type definition for resource callback function - called by session layer when data
53  * arrives for a particular resource.
54  *
55  * @param arg Private argument.
56  * @param slot_id Slot id concerned.
57  * @param session_number Session number.
58  * @param resource_id Resource id.
59  * @param data The data.
60  * @param data_length Length of data in bytes.
61  * @return 0 on success, or -1 on failure.
62  */
63 typedef int (*en50221_sl_resource_callback) (void *arg,
64 					     uint8_t slot_id,
65 					     uint16_t session_number,
66 					     uint32_t resource_id,
67 					     uint8_t * data,
68 					     uint32_t data_length);
69 
70 /**
71  * Type definition for resource lookup callback function - used by the session layer to
72  * look up requested resources.
73  *
74  * @param arg Private argument.
75  * @param slot_id Slot id the request came from.
76  * @param requested_resource_id Resource id requested.
77  * @param callback_out Output parameter for pointer to resource callback function.
78  * @param arg_out Output parameter for arg to pass to resource callback.
79  * @param resource_id_out Set this to the resource_id connected to (e.g. may differ from resource_id due to versions).
80  * @return 0 on success,
81  * -1 if the resource was not found,
82  * -2 if it exists, but had a lower version, or
83  * -3 if it exists, but was unavailable.
84  */
85 typedef int (*en50221_sl_lookup_callback) (void *arg,
86 					   uint8_t slot_id,
87 					   uint32_t requested_resource_id,
88 					   en50221_sl_resource_callback * callback_out,
89 					   void **arg_out,
90 					   uint32_t *resource_id_out);
91 
92 
93 /**
94  * Type definition for session callback function - used to inform top level code when a CAM
95  * modifies a session to a resource.
96  *
97  * @param arg Private argument.
98  * @param reason One of the S_CCALLBACK_REASON_* values above.
99  * @param slot_id Slot id concerned.
100  * @param session_number Session number.
101  * @param resource_id Resource id.
102  * @return 0 on sucess, or -1 on error.
103  */
104 typedef int (*en50221_sl_session_callback) (void *arg, int reason,
105 					    uint8_t slot_id,
106 					    uint16_t session_number,
107 					    uint32_t resource_id);
108 
109 /**
110  * Construct a new instance of the session layer.
111  *
112  * @param tl The en50221_transport_layer instance to use.
113  * @param max_sessions Maximum number of sessions supported.
114  * @return The en50221_session_layer instance, or NULL on error.
115  */
116 extern struct en50221_session_layer *en50221_sl_create(struct en50221_transport_layer *tl,
117 						       uint32_t max_sessions);
118 
119 /**
120  * Destroy an instance of the session layer.
121  *
122  * @param tl The en50221_session_layer instance.
123  */
124 extern void en50221_sl_destroy(struct en50221_session_layer *sl);
125 
126 /**
127  * Gets the last error.
128  *
129  * @param tl The en50221_session_layer instance.
130  * @return One of the EN50221ERR_* values.
131  */
132 extern int en50221_sl_get_error(struct en50221_session_layer *tl);
133 
134 /**
135  * Register the callback for resource lookup.
136  *
137  * @param sl The en50221_session_layer instance.
138  * @param callback The callback. Set to NULL to remove the callback completely.
139  * @param arg Private data passed as arg0 of the callback.
140  */
141 extern void en50221_sl_register_lookup_callback(struct en50221_session_layer *sl,
142 						en50221_sl_lookup_callback callback,
143 						void *arg);
144 
145 /**
146  * Register the callback for informing about session from a cam.
147  *
148  * @param sl The en50221_session_layer instance.
149  * @param callback The callback. Set to NULL to remove the callback completely.
150  * @param arg Private data passed as arg0 of the callback.
151  */
152 extern void en50221_sl_register_session_callback(struct en50221_session_layer *sl,
153 						 en50221_sl_session_callback callback,
154 						 void *arg);
155 
156 /**
157  * Create a new session to a module in a slot.
158  *
159  * @param sl The en50221_session_layer instance.
160  * @param slot The slot to connect to.
161  * @param resource_id The resource_id to connect to.
162  * @param callback The callback for received data.
163  * @param arg Argument to pass to the callback.
164  * @return The new session_number, or -1 on error.
165  */
166 extern int en50221_sl_create_session(struct en50221_session_layer *sl, int slot_id,
167 				     uint8_t connection_id,
168 				     uint32_t resource_id,
169 				     en50221_sl_resource_callback callback,
170 				     void *arg);
171 
172 /**
173  * Destroy a session.
174  *
175  * @param sl The en50221_session_layer instance.
176  * @param session_number The session to destroy.
177  * @return 0 on success, or -1 on error.
178  */
179 extern int en50221_sl_destroy_session(struct en50221_session_layer *sl,
180 				      uint16_t session_number);
181 
182 /**
183  * this function is used to take a data-block, pack into
184  * into a SPDU (SESSION_NUMBER) and send it to the transport layer
185  *
186  * @param sl The en50221_session_layer instance to use.
187  * @param session_number Session number concerned.
188  * @param data Data to send.
189  * @param data_length Length of data in bytes.
190  * @return 0 on success, or -1 on error.
191  */
192 extern int en50221_sl_send_data(struct en50221_session_layer *sl,
193 				uint16_t session_number,
194 				uint8_t * data,
195 				uint16_t data_length);
196 
197 /**
198  * this function is used to take a data-block, pack into
199  * into a SPDU (SESSION_NUMBER) and send it to the transport layer
200  *
201  * @param sl The en50221_session_layer instance to use.
202  * @param session_number Session number concerned.
203  * @param vector IOVEC to send.
204  * @param iov_count Number of elements in io vector.
205  * @return 0 on success, or -1 on error.
206  */
207 extern int en50221_sl_send_datav(struct en50221_session_layer *sl,
208 				 uint16_t session_number,
209 				 struct iovec *vector,
210 				 int iov_count);
211 
212 /**
213  * this is used to send a message to all sessions, linked
214  * to resource res
215  *
216  * @param tl The en50221_session_layer instance to use.
217  * @param slot_id Set to -1 to send to any slot. Other values will send to only that slot.
218  * @param resource_id Resource id concerned.
219  * @param data Data to send.
220  * @param data_length Length of data in bytes.
221  * @return 0 on success, or -1 on error.
222  */
223 extern int en50221_sl_broadcast_data(struct en50221_session_layer *sl,
224 				     int slot_id,
225 				     uint32_t resource_id,
226 				     uint8_t * data,
227 				     uint16_t data_length);
228 
229 #ifdef __cplusplus
230 }
231 #endif
232 #endif
233