mini-led-cube/doc/doxygen/html/opendevice_8c.html

368 lines
20 KiB
HTML
Raw Normal View History

2011-12-25 13:48:15 +01:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>CTHN.de - LEDCube - commandline client: opendevice.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Generated by Doxygen 1.7.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">CTHN.de - LEDCube - commandline client</div>
</td>
</tr>
</tbody>
</table>
</div>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
<li id="searchli">
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
<div id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File&#160;List</span></a></li>
<li><a href="globals.html"><span>File&#160;Members</span></a></li>
</ul>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#define-members">Defines</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">opendevice.c File Reference</div> </div>
</div>
<div class="contents">
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br/>
<code>#include &quot;<a class="el" href="opendevice_8h_source.html">opendevice.h</a>&quot;</code><br/>
</div><div class="textblock"><div class="dynheader">
Include dependency graph for opendevice.c:</div>
<div class="dyncontent">
<div class="center"><img src="opendevice_8c__incl.png" border="0" usemap="#opendevice_8c" alt=""/></div>
<map name="opendevice_8c" id="opendevice_8c">
<area shape="rect" id="node5" href="opendevice_8h.html" title="opendevice.h" alt="" coords="71,83,169,112"/></map>
</div>
</div><table class="memberdecls">
<tr><td colspan="2"><h2><a name="define-members"></a>
Defines</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="opendevice_8c.html#a5f48dcc6eee4e04499a8b2a5e8f56b91">MATCH_SUCCESS</a>&#160;&#160;&#160;1</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="opendevice_8c.html#ac8ad57fb87ae8e10684a7b910e92bba6">MATCH_FAILED</a>&#160;&#160;&#160;0</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="opendevice_8c.html#abd0788cdedf1a166ae08894f36e3b2e5">MATCH_ABORT</a>&#160;&#160;&#160;-1</td></tr>
<tr><td colspan="2"><h2><a name="func-members"></a>
Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="opendevice_8c.html#a75b5f0b18368de18125662f87524b5b0">usbGetStringAscii</a> (usb_dev_handle *dev, int index, char *buf, int buflen)</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="opendevice_8c.html#acea645564f1dfcd741ff20038d811ac5">usbOpenDevice</a> (usb_dev_handle **device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp)</td></tr>
</table>
<hr/><h2>Define Documentation</h2>
<a class="anchor" id="abd0788cdedf1a166ae08894f36e3b2e5"></a><!-- doxytag: member="opendevice.c::MATCH_ABORT" ref="abd0788cdedf1a166ae08894f36e3b2e5" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define MATCH_ABORT&#160;&#160;&#160;-1</td>
</tr>
</table>
</div>
<div class="memdoc">
</div>
</div>
<a class="anchor" id="ac8ad57fb87ae8e10684a7b910e92bba6"></a><!-- doxytag: member="opendevice.c::MATCH_FAILED" ref="ac8ad57fb87ae8e10684a7b910e92bba6" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define MATCH_FAILED&#160;&#160;&#160;0</td>
</tr>
</table>
</div>
<div class="memdoc">
</div>
</div>
<a class="anchor" id="a5f48dcc6eee4e04499a8b2a5e8f56b91"></a><!-- doxytag: member="opendevice.c::MATCH_SUCCESS" ref="a5f48dcc6eee4e04499a8b2a5e8f56b91" args="" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">#define MATCH_SUCCESS&#160;&#160;&#160;1</td>
</tr>
</table>
</div>
<div class="memdoc">
</div>
</div>
<hr/><h2>Function Documentation</h2>
<a class="anchor" id="a75b5f0b18368de18125662f87524b5b0"></a><!-- doxytag: member="opendevice.c::usbGetStringAscii" ref="a75b5f0b18368de18125662f87524b5b0" args="(usb_dev_handle *dev, int index, char *buf, int buflen)" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int usbGetStringAscii </td>
<td>(</td>
<td class="paramtype">usb_dev_handle *&#160;</td>
<td class="paramname"><em>dev</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>index</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>buf</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>buflen</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div>
<div class="memdoc">
<div class="fragment"><pre class="fragment">{
<span class="keywordtype">char</span> buffer[256];
<span class="keywordtype">int</span> rval, i;
<span class="keywordflow">if</span>((rval = usb_get_string_simple(dev, index, buf, buflen)) &gt;= 0) <span class="comment">/* use libusb version if it works */</span>
<span class="keywordflow">return</span> rval;
<span class="keywordflow">if</span>((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING &lt;&lt; 8) + index, 0x0409, buffer, <span class="keyword">sizeof</span>(buffer), 5000)) &lt; 0)
<span class="keywordflow">return</span> rval;
<span class="keywordflow">if</span>(buffer[1] != USB_DT_STRING){
*buf = 0;
<span class="keywordflow">return</span> 0;
}
<span class="keywordflow">if</span>((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)buffer[0] &lt; rval)
rval = (<span class="keywordtype">unsigned</span> char)buffer[0];
rval /= 2;
<span class="comment">/* lossy conversion to ISO Latin1: */</span>
<span class="keywordflow">for</span>(i=1;i&lt;rval;i++){
<span class="keywordflow">if</span>(i &gt; buflen) <span class="comment">/* destination buffer overflow */</span>
<span class="keywordflow">break</span>;
buf[i-1] = buffer[2 * i];
<span class="keywordflow">if</span>(buffer[2 * i + 1] != 0) <span class="comment">/* outside of ISO Latin1 range */</span>
buf[i-1] = <span class="charliteral">&#39;?&#39;</span>;
}
buf[i-1] = 0;
<span class="keywordflow">return</span> i-1;
}
</pre></div>
</div>
</div>
<a class="anchor" id="acea645564f1dfcd741ff20038d811ac5"></a><!-- doxytag: member="opendevice.c::usbOpenDevice" ref="acea645564f1dfcd741ff20038d811ac5" args="(usb_dev_handle **device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp)" -->
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int usbOpenDevice </td>
<td>(</td>
<td class="paramtype">usb_dev_handle **&#160;</td>
<td class="paramname"><em>device</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>vendorID</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>vendorNamePattern</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>productID</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>productNamePattern</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>serialNamePattern</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">FILE *&#160;</td>
<td class="paramname"><em>printMatchingDevicesFp</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">FILE *&#160;</td>
<td class="paramname"><em>warningsFp</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div>
<div class="memdoc">
<div class="fragment"><pre class="fragment">{
<span class="keyword">struct </span>usb_bus *bus;
<span class="keyword">struct </span>usb_device *dev;
usb_dev_handle *handle = NULL;
<span class="keywordtype">int</span> errorCode = <a class="code" href="opendevice_8h.html#aeffd7f45947b1e146f8dfc1c6d94704e">USBOPEN_ERR_NOTFOUND</a>;
usb_find_busses();
usb_find_devices();
<span class="keywordflow">for</span>(bus = usb_get_busses(); bus; bus = bus-&gt;next){
<span class="keywordflow">for</span>(dev = bus-&gt;devices; dev; dev = dev-&gt;next){ <span class="comment">/* iterate over all devices on all busses */</span>
<span class="keywordflow">if</span>((vendorID == 0 || dev-&gt;descriptor.idVendor == vendorID)
&amp;&amp; (productID == 0 || dev-&gt;descriptor.idProduct == productID)){
<span class="keywordtype">char</span> vendor[256], product[256], serial[256];
<span class="keywordtype">int</span> len;
handle = usb_open(dev); <span class="comment">/* we need to open the device in order to query strings */</span>
<span class="keywordflow">if</span>(!handle){
errorCode = <a class="code" href="opendevice_8h.html#a7740d6b3c1581a07523be3b3d5039c95">USBOPEN_ERR_ACCESS</a>;
<span class="keywordflow">if</span>(warningsFp != NULL)
fprintf(warningsFp, <span class="stringliteral">&quot;Warning: cannot open VID=0x%04x PID=0x%04x: %s\n&quot;</span>, dev-&gt;descriptor.idVendor, dev-&gt;descriptor.idProduct, usb_strerror());
<span class="keywordflow">continue</span>;
}
<span class="comment">/* now check whether the names match: */</span>
len = vendor[0] = 0;
<span class="keywordflow">if</span>(dev-&gt;descriptor.iManufacturer &gt; 0){
len = <a class="code" href="opendevice_8c.html#a75b5f0b18368de18125662f87524b5b0">usbGetStringAscii</a>(handle, dev-&gt;descriptor.iManufacturer, vendor, <span class="keyword">sizeof</span>(vendor));
}
<span class="keywordflow">if</span>(len &lt; 0){
errorCode = <a class="code" href="opendevice_8h.html#a7740d6b3c1581a07523be3b3d5039c95">USBOPEN_ERR_ACCESS</a>;
<span class="keywordflow">if</span>(warningsFp != NULL)
fprintf(warningsFp, <span class="stringliteral">&quot;Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n&quot;</span>, dev-&gt;descriptor.idVendor, dev-&gt;descriptor.idProduct, usb_strerror());
}<span class="keywordflow">else</span>{
errorCode = <a class="code" href="opendevice_8h.html#aeffd7f45947b1e146f8dfc1c6d94704e">USBOPEN_ERR_NOTFOUND</a>;
<span class="comment">/* printf(&quot;seen device from vendor -&gt;%s&lt;-\n&quot;, vendor); */</span>
<span class="keywordflow">if</span>(shellStyleMatch(vendor, vendorNamePattern)){
len = product[0] = 0;
<span class="keywordflow">if</span>(dev-&gt;descriptor.iProduct &gt; 0){
len = <a class="code" href="opendevice_8c.html#a75b5f0b18368de18125662f87524b5b0">usbGetStringAscii</a>(handle, dev-&gt;descriptor.iProduct, product, <span class="keyword">sizeof</span>(product));
}
<span class="keywordflow">if</span>(len &lt; 0){
errorCode = <a class="code" href="opendevice_8h.html#a7740d6b3c1581a07523be3b3d5039c95">USBOPEN_ERR_ACCESS</a>;
<span class="keywordflow">if</span>(warningsFp != NULL)
fprintf(warningsFp, <span class="stringliteral">&quot;Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n&quot;</span>, dev-&gt;descriptor.idVendor, dev-&gt;descriptor.idProduct, usb_strerror());
}<span class="keywordflow">else</span>{
errorCode = <a class="code" href="opendevice_8h.html#aeffd7f45947b1e146f8dfc1c6d94704e">USBOPEN_ERR_NOTFOUND</a>;
<span class="comment">/* printf(&quot;seen product -&gt;%s&lt;-\n&quot;, product); */</span>
<span class="keywordflow">if</span>(shellStyleMatch(product, productNamePattern)){
len = serial[0] = 0;
<span class="keywordflow">if</span>(dev-&gt;descriptor.iSerialNumber &gt; 0){
len = <a class="code" href="opendevice_8c.html#a75b5f0b18368de18125662f87524b5b0">usbGetStringAscii</a>(handle, dev-&gt;descriptor.iSerialNumber, serial, <span class="keyword">sizeof</span>(serial));
}
<span class="keywordflow">if</span>(len &lt; 0){
errorCode = <a class="code" href="opendevice_8h.html#a7740d6b3c1581a07523be3b3d5039c95">USBOPEN_ERR_ACCESS</a>;
<span class="keywordflow">if</span>(warningsFp != NULL)
fprintf(warningsFp, <span class="stringliteral">&quot;Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n&quot;</span>, dev-&gt;descriptor.idVendor, dev-&gt;descriptor.idProduct, usb_strerror());
}
<span class="keywordflow">if</span>(shellStyleMatch(serial, serialNamePattern)){
<span class="keywordflow">if</span>(printMatchingDevicesFp != NULL){
<span class="keywordflow">if</span>(serial[0] == 0){
fprintf(printMatchingDevicesFp, <span class="stringliteral">&quot;VID=0x%04x PID=0x%04x vendor=\&quot;%s\&quot; product=\&quot;%s\&quot;\n&quot;</span>, dev-&gt;descriptor.idVendor, dev-&gt;descriptor.idProduct, vendor, product);
}<span class="keywordflow">else</span>{
fprintf(printMatchingDevicesFp, <span class="stringliteral">&quot;VID=0x%04x PID=0x%04x vendor=\&quot;%s\&quot; product=\&quot;%s\&quot; serial=\&quot;%s\&quot;\n&quot;</span>, dev-&gt;descriptor.idVendor, dev-&gt;descriptor.idProduct, vendor, product, serial);
}
}<span class="keywordflow">else</span>{
<span class="keywordflow">break</span>;
}
}
}
}
}
}
usb_close(handle);
handle = NULL;
}
}
<span class="keywordflow">if</span>(handle) <span class="comment">/* we have found a deice */</span>
<span class="keywordflow">break</span>;
}
<span class="keywordflow">if</span>(handle != NULL){
errorCode = 0;
*device = handle;
}
<span class="keywordflow">if</span>(printMatchingDevicesFp != NULL) <span class="comment">/* never return an error for listing only */</span>
errorCode = 0;
<span class="keywordflow">return</span> errorCode;
}
</pre></div>
<p><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="opendevice_8c_acea645564f1dfcd741ff20038d811ac5_cgraph.png" border="0" usemap="#opendevice_8c_acea645564f1dfcd741ff20038d811ac5_cgraph" alt=""/></div>
<map name="opendevice_8c_acea645564f1dfcd741ff20038d811ac5_cgraph" id="opendevice_8c_acea645564f1dfcd741ff20038d811ac5_cgraph">
<area shape="rect" id="node3" href="opendevice_8c.html#a75b5f0b18368de18125662f87524b5b0" title="usbGetStringAscii" alt="" coords="169,5,295,35"/></map>
</div>
</p>
</div>
</div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Defines</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Sun Dec 25 2011 13:37:31 for CTHN.de - LEDCube - commandline client by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>