<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>ZVBI Library: examples/network.c</title> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.9 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="contents"> <h1>examples/network.c</h1>Network identification example.<p> <div class="fragment"><pre class="fragment"><span class="comment">/*</span> <span class="comment"> * libzvbi network identification example</span> <span class="comment"> *</span> <span class="comment"> * Copyright (C) 2006 Michael H. Schimek</span> <span class="comment"> *</span> <span class="comment"> * Redistribution and use in source and binary forms, with or without</span> <span class="comment"> * modification, are permitted provided that the following conditions</span> <span class="comment"> * are met:</span> <span class="comment"> * 1. Redistributions of source code must retain the above copyright</span> <span class="comment"> * notice, this list of conditions and the following disclaimer.</span> <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> <span class="comment"> * notice, this list of conditions and the following disclaimer in</span> <span class="comment"> * the documentation and/or other materials provided with the</span> <span class="comment"> * distribution.</span> <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span> <span class="comment"> * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span> <span class="comment"> * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span> <span class="comment"> * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span> <span class="comment"> * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span> <span class="comment"> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span> <span class="comment"> * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span> <span class="comment"> * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span> <span class="comment"> * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span> <span class="comment"> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span> <span class="comment"> * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span> <span class="comment"> */</span> <span class="comment">/* $Id: network.c,v 1.5 2008/02/19 00:51:51 mschimek Exp $ */</span> <span class="comment">/* This example shows how to identify a network from data transmitted</span> <span class="comment"> in XDS packets, Teletext packet 8/30 format 1 and 2, and VPS packets.</span> <span class="comment"></span> <span class="comment"> gcc -o network network.c `pkg-config zvbi-0.2 --cflags --libs` */</span> <span class="preprocessor">#undef NDEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#include <assert.h></span> <span class="preprocessor">#include <stdio.h></span> <span class="preprocessor">#include <stdlib.h></span> <span class="preprocessor">#include <string.h></span> <span class="preprocessor">#include <locale.h></span> <span class="preprocessor">#include <errno.h></span> <span class="preprocessor">#include <libzvbi.h></span> <span class="keyword">static</span> <a name="a0"></a><a class="code" href="group__Device.html#g0ae28639bd50247df2bcda9e79f700d4" title="Opaque device interface handle.">vbi_capture</a> * cap; <span class="keyword">static</span> vbi_decoder * dec; <span class="keyword">static</span> vbi_bool quit; <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> services; <span class="keyword">static</span> <span class="keywordtype">void</span> handler (<a name="_a1"></a><a class="code" href="structvbi__event.html" title="Event union.">vbi_event</a> * ev, <span class="keywordtype">void</span> * user_data) { <span class="keyword">const</span> <span class="keywordtype">char</span> *event_name; <span class="keyword">const</span> <span class="keywordtype">char</span> *network_name; <span class="keyword">const</span> <span class="keywordtype">char</span> *call_sign; <span class="keywordtype">char</span> *locale_network_name; user_data = user_data; <span class="comment">/* unused */</span> <span class="comment">/* VBI_EVENT_NETWORK_ID is always sent when the decoder</span> <span class="comment"> receives a CNI. VBI_EVENT_NETWORK only if it can</span> <span class="comment"> determine a network name. */</span> <span class="keywordflow">switch</span> (ev-><a name="a2"></a>type) { <span class="keywordflow">case</span> VBI_EVENT_NETWORK: event_name = <span class="stringliteral">"VBI_EVENT_NETWORK"</span>; quit = TRUE; <span class="keywordflow">break</span>; <span class="keywordflow">case</span> VBI_EVENT_NETWORK_ID: event_name = <span class="stringliteral">"VBI_EVENT_NETWORK_ID"</span>; <span class="keywordflow">break</span>; <span class="keywordflow">default</span>: assert (0); } network_name = <span class="stringliteral">"unknown"</span>; <span class="keywordflow">if</span> (0 != ev-><a name="a3"></a>ev.<a name="a4"></a>network.<a name="a5"></a><a class="code" href="structvbi__network.html#f9d608a153277b382fe51f7100d02724">name</a>[0]) network_name = ev->ev.network.<a class="code" href="structvbi__network.html#f9d608a153277b382fe51f7100d02724">name</a>; <span class="comment">/* The network name is an ISO-8859-1 string (the API is</span> <span class="comment"> quite old...) so we convert it to locale encoding,</span> <span class="comment"> nowadays usually UTF-8. */</span> locale_network_name = <a name="a6"></a><a class="code" href="group__Conv.html#gb360448d13ec1cd3c4a7a4babb07243f">vbi_strndup_iconv</a> (<a name="a7"></a><a class="code" href="group__Conv.html#g69e68f14e65c01622db558df3e6a11f6">vbi_locale_codeset</a> (), <span class="stringliteral">"ISO-8859-1"</span>, network_name, strlen (network_name), <span class="comment">/* repl_char */</span> <span class="charliteral">'?'</span>); <span class="comment">/* ASCII. */</span> call_sign = <span class="stringliteral">"unknown"</span>; <span class="keywordflow">if</span> (0 != ev->ev.network.<a name="a8"></a><a class="code" href="structvbi__network.html#f0c2eebb0061a42a4c19835d4daca487">call</a>[0]) call_sign = ev->ev.network.<a class="code" href="structvbi__network.html#f0c2eebb0061a42a4c19835d4daca487">call</a>; printf (<span class="stringliteral">"%s: receiving: \"%s\" call sign: \"%s\" "</span> <span class="stringliteral">"CNI VPS: 0x%x 8/30-1: 0x%x 8/30-2: 0x%x\n"</span>, event_name, (NULL == locale_network_name) ? <span class="stringliteral">"iconv-error"</span> : locale_network_name, call_sign, ev->ev.network.<a name="a9"></a><a class="code" href="structvbi__network.html#072907768d911e73c4d96e5be9414954">cni_vps</a>, ev->ev.network.<a name="a10"></a><a class="code" href="structvbi__network.html#e994f3cec515287f42aaedb7eab6b93c">cni_8301</a>, ev->ev.network.<a name="a11"></a><a class="code" href="structvbi__network.html#1ce63ca7c685b7563da4115a71a2afed">cni_8302</a>); free (locale_network_name); } <span class="keyword">static</span> <span class="keywordtype">void</span> mainloop (<span class="keywordtype">void</span>) { <span class="keyword">struct </span>timeval timeout; vbi_capture_buffer *sliced_buffer; <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n_frames; <span class="comment">/* Don't wait more than two seconds for the driver</span> <span class="comment"> to return data. */</span> timeout.tv_sec = 2; timeout.tv_usec = 0; <span class="comment">/* Should receive a CNI within two seconds,</span> <span class="comment"> call sign within ten seconds(?). */</span> <span class="keywordflow">if</span> (services & VBI_SLICED_CAPTION_525) n_frames = 11 * 30; <span class="keywordflow">else</span> n_frames = 3 * 25; <span class="keywordflow">for</span> (; n_frames > 0; --n_frames) { <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n_lines; <span class="keywordtype">int</span> r; r = <a name="a12"></a><a class="code" href="group__Device.html#g58dcf2387b685b7c1c09a729f07893c8">vbi_capture_pull</a> (cap, <span class="comment">/* raw_buffer */</span> NULL, &sliced_buffer, &timeout); <span class="keywordflow">switch</span> (r) { <span class="keywordflow">case</span> -1: fprintf (stderr, <span class="stringliteral">"VBI read error %d (%s)\n"</span>, errno, strerror (errno)); <span class="comment">/* Could be ignored, esp. EIO with some drivers. */</span> exit(EXIT_FAILURE); <span class="keywordflow">case</span> 0: fprintf (stderr, <span class="stringliteral">"VBI read timeout\n"</span>); exit(EXIT_FAILURE); <span class="keywordflow">case</span> 1: <span class="comment">/* success */</span> <span class="keywordflow">break</span>; <span class="keywordflow">default</span>: assert (0); } n_lines = sliced_buffer->size / <span class="keyword">sizeof</span> (<a name="_a13"></a><a class="code" href="structvbi__sliced.html" title="This structure holds one scan line of sliced vbi data.">vbi_sliced</a>); <a name="a14"></a><a class="code" href="group__Service.html#g2e2201e3b2bab9b7fb1bb66ade716772" title="Main function of the data service decoder.">vbi_decode</a> (dec, (<a class="code" href="structvbi__sliced.html" title="This structure holds one scan line of sliced vbi data.">vbi_sliced</a> *) sliced_buffer->data, n_lines, sliced_buffer->timestamp); <span class="keywordflow">if</span> (quit) <span class="keywordflow">return</span>; } printf (<span class="stringliteral">"No network ID received or network unknown.\n"</span>); } <span class="keywordtype">int</span> main (<span class="keywordtype">void</span>) { <span class="keywordtype">char</span> *errstr; vbi_bool success; setlocale (LC_ALL, <span class="stringliteral">""</span>); services = (VBI_SLICED_TELETEXT_B | VBI_SLICED_VPS | VBI_SLICED_CAPTION_525); cap = <a name="a15"></a><a class="code" href="group__Device.html#g85ba4554b337ecb053168bb88f713e5f">vbi_capture_v4l2_new</a> (<span class="stringliteral">"/dev/vbi"</span>, <span class="comment">/* buffers */</span> 5, &services, <span class="comment">/* strict */</span> 0, &errstr, <span class="comment">/* verbose */</span> FALSE); <span class="keywordflow">if</span> (NULL == cap) { fprintf (stderr, <span class="stringliteral">"Cannot capture VBI data with V4L2 interface:\n"</span> <span class="stringliteral">"%s\n"</span>, errstr); free (errstr); errstr = NULL; exit (EXIT_FAILURE); } dec = <a name="a16"></a><a class="code" href="group__Service.html#g3c09b7fa196326f51409a91e2857ba5e" title="Allocate a new data service decoder instance.">vbi_decoder_new</a> (); assert (NULL != dec); success = <a name="a17"></a><a class="code" href="group__Event.html#gcd13f9d8a2d47a4393541b7411fe426a">vbi_event_handler_add</a> (dec, (VBI_EVENT_NETWORK | VBI_EVENT_NETWORK_ID), handler, <span class="comment">/* user_data */</span> NULL); assert (success); mainloop (); <a name="a18"></a><a class="code" href="group__Service.html#gb3f52f0b71746c9081c053a33cad7bce" title="Delete a data service decoder instance.">vbi_decoder_delete</a> (dec); dec = NULL; <a name="a19"></a><a class="code" href="group__Device.html#gec518b62f39b83c80b06cc0a4affbee6">vbi_capture_delete</a> (cap); cap = NULL; exit (EXIT_SUCCESS); } </pre></div> </div> <hr size="1"><address style="text-align: right;"><small>Generated on Wed May 27 04:47:58 2009 for ZVBI Library by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address> </body> </html>