📄 devio.c
字号:
<HTML><HEAD><TITLE>/home/asysweb/public_html/cortex/examples/exmpl13/src/devio.c</TITLE></HEAD><BODY><pre><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/* */</font><font color="#6920ac">/* Copyright (c) 1997-2000 Australian Real Time Embedded Systems */</font><font color="#6920ac">/* */</font><font color="#6920ac">/* The authors hereby grant permission to use, copy, modify, distribute, */</font><font color="#6920ac">/* and license this software and its documentation for any purpose, */</font><font color="#6920ac">/* provided that existing copyright notices are retained in all copies */</font><font color="#6920ac">/* and that this notice and the following disclaimer are included */</font><font color="#6920ac">/* verbatim in any distributions. No written agreement, license, or */</font><font color="#6920ac">/* royalty fee is required for any of the authorized uses. */</font><font color="#6920ac">/* */</font><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/* */</font><font color="#6920ac">/* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS */</font><font color="#6920ac">/* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */</font><font color="#6920ac">/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE */</font><font color="#6920ac">/* ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY */</font><font color="#6920ac">/* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */</font><font color="#6920ac">/* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */</font><font color="#6920ac">/* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */</font><font color="#6920ac">/* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER */</font><font color="#6920ac">/* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR */</font><font color="#6920ac">/* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN */</font><font color="#6920ac">/* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */</font><font color="#6920ac">/* */</font><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/* REVISION HISTORY */</font><font color="#6920ac">/* */</font><font color="#6920ac">/* 18/02/00 Implementation for CORTEX environment */</font><font color="#6920ac">/* Vadim Azarovsky <vadima@artesys.com.au> */</font><font color="#6920ac">/*************************************************************************/</font><b><font color='DarkGreen'>#include</font></b> <a href="string.h.FIND-INC"><font color="blue">"string.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="stdio.h.FIND-INC"><font color="blue">"stdio.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="typedefs.h.FIND-INC"><font color="blue">"typedefs.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="avos.h.FIND-INC"><font color="blue">"avos.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="net.h.FIND-INC"><font color="blue">"net.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="netbuf.h.FIND-INC"><font color="blue">"netbuf.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="devio.h.FIND-INC"><font color="blue">"devio.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="debug.h.FIND-INC"><font color="blue">"debug.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="plt_defs.h.FIND-INC"><font color="blue">"plt_defs.h"</font></a><i>static</i> <i>char</i> FrameChar_g = <a href="PPP_FLAG.FIND-DEF">PPP_FLAG</a>; <font color="#6920ac">/* current frame character */</font><i>mutx_Mutex_t</i> *pLockIO_g = <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>;<i>mutx_Mutex_t</i> LockIO_g;<font color="#6920ac">/* Get name for file device ID. */</font><font size="+1"><i>const</i> <i>char</i> *<b><font color="azure1"><a name="nameForDevice">nameForDevice</a></font></b>(u_int fd){</font> <i>static</i> <i>char</i> name[20]; <a href="sprintf.FIND-FUNC">sprintf</a>(name, <font color="DarkGreen">"pppDev%d"</font>, fd); <b>return</b> name;}<font color="#6920ac">/* Put an nBuf packet to device. */</font><font size="+1">INT <b><font color="azure1"><a name="nPut">nPut</a></font></b>(u_int fid, NBuf *nb_a){</font> <i>sdrv_Desc_t</i> *pSerial; <i>crtx_Char_t</i> *pData; <i>crtx_Int_t</i> Len; <i>crtx_Int_t</i> Total = 0; NBuf *nb; <b>if</b> (fid != 0) <a href="syst_Fatal.FIND-FUNC">syst_Fatal</a>(<font color="DarkGreen">"Unknown network interface (#%d) is used\n"</font>, fid); pSerial = &pltf_SdrvDesc; <b>if</b> (pLockIO_g == <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>) { pLockIO_g = <a href="mutx_Create.FIND-FUNC">mutx_Create</a>(&LockIO_g, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>); } <b>if</b> (nb_a) {<font color="#6920ac">#if 0 /* DEBUG_SUPPORT > 0 */ trace(LOG_INFO, "nPut:fid=%d nb_a=%p dump:", fid, nb_a); nDumpChain(nb_a);#endif</font> <a href="mutx_Lock.FIND-FUNC">mutx_Lock</a>(pLockIO_g); nb = nb_a; <b>while</b> (nb) { <b>if</b> (nb->nextChain == nb) <a href="syst_Fatal.FIND-FUNC">syst_Fatal</a>(<font color="DarkGreen">"nPut: buffer %p is free in chain %p"</font>, nb, nb_a); <b>if</b> (nb->len > 0) { pData = <a href="nBUFTOPTR.FIND-FUNC">nBUFTOPTR</a>(nb, <i>crtx_Char_t</i> *); Len = <a href="sdrv_Write.FIND-FUNC">sdrv_Write</a>(pData, nb->len, pSerial); <b>if</b> (Len < 0) { <a href="nFreeChain.FIND-FUNC">nFreeChain</a>(nb_a); <a href="mutx_Unlock.FIND-FUNC">mutx_Unlock</a>(pLockIO_g); <b>return</b> -1; } Total += Len; } nb = nb->nextBuf; } <a href="mutx_Unlock.FIND-FUNC">mutx_Unlock</a>(pLockIO_g); <a href="nFreeChain.FIND-FUNC">nFreeChain</a>(nb_a); } <b>return</b> Total;}<font color="#6920ac">/* Get an nBuf packet from device. */</font><font size="+1">INT <b><font color="azure1"><a name="nGet">nGet</a></font></b>(u_int fid, NBuf **nb, u_int timeout){</font> <i>sdrv_Desc_t</i> *pSerial; <i>tick_Timeout_t</i> Timeout = timeout; <i>crtx_Int_t</i> Char; <i>char</i> ch; <i>int</i> cLen; <i>int</i> tLen = 0; <b>if</b> (fid != 0) <a href="syst_Fatal.FIND-FUNC">syst_Fatal</a>(<font color="DarkGreen">"Unknown network interface (#%d) is used\n"</font>, fid); pSerial = &pltf_SdrvDesc; *nb = <a href="NULL.FIND-DEF">NULL</a>; <font color="#6920ac">/* no data */</font> <font color="#6920ac">/* get first character */</font> <b>if</b> ((Char = <a href="sdrv_Getct.FIND-FUNC">sdrv_Getct</a>(pSerial, &Timeout)) == -1) <font color="#6920ac">/* timeout expired, no buffer returned */</font> <b>return</b> -1; <font color="#6920ac">/* allocate new buffer */</font> <a href="nGET.FIND-FUNC">nGET</a>(*nb); <b>if</b> (*nb == <a href="NULL.FIND-DEF">NULL</a>) <font color="#6920ac">/* buffer allocation failed */</font> <b>return</b> -2; ch = (<i>char</i>)Char; <a href="nAPPENDCHAR.FIND-FUNC">nAPPENDCHAR</a>(*nb, ch, cLen); tLen += cLen; <b>while</b> (!((Char == FrameChar_g) && (tLen > 1)) && <a href="nTRAILINGSPACE.FIND-FUNC">nTRAILINGSPACE</a>(*nb)>0) { <b>if</b> ((Char = <a href="sdrv_Getct.FIND-FUNC">sdrv_Getct</a>(pSerial, &Timeout)) == -1) <b>return</b> 0; ch = (<i>char</i>)Char; <a href="nAPPENDCHAR.FIND-FUNC">nAPPENDCHAR</a>(*nb, ch, cLen); tLen += cLen; }<font color="#6920ac">#if 0 /* DEBUG_SUPPORT > 0 */ trace(LOG_INFO, "nGet:fid=%d nb=%p dump:", fid, *nb); nDumpChain(*nb);#endif</font> <b>return</b> 0;}<font size="+1">INT <b><font color="azure1"><a name="ioctl">ioctl</a></font></b>(u_int fd, INT op, <i>void</i> *data){</font> <i>sdrv_Desc_t</i> *pSerial; <i>char</i> *pFrameChar; <b>if</b> (fd != 0) <a href="syst_Fatal.FIND-FUNC">syst_Fatal</a>(<font color="DarkGreen">"Unknown network interface (#%d) is used\n"</font>, fd); pSerial = &pltf_SdrvDesc; <font color="#6920ac">/* VA: may need support for FRAME character in sdrv-driver */</font> <b>switch</b>(op) { <b>case</b> <a href="SETFRAME.FIND-DEF">SETFRAME</a>: pFrameChar = (<i>char</i>*)data; FrameChar_g = *pFrameChar; <b>break</b>; <b>case</b> <a href="GETFRAME.FIND-DEF">GETFRAME</a>: pFrameChar = (<i>char</i>*)data; *pFrameChar = FrameChar_g; <b>break</b>; <b>case</b> <a href="ENAECHO.FIND-DEF">ENAECHO</a>: <a href="sdrv_Ioctl.FIND-FUNC">sdrv_Ioctl</a>(<a href="SDRV_IOCTL_ECHO_ON.FIND-DEF">SDRV_IOCTL_ECHO_ON</a>, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, pSerial); <b>break</b>; <b>case</b> <a href="DISECHO.FIND-DEF">DISECHO</a>: <a href="sdrv_Ioctl.FIND-FUNC">sdrv_Ioctl</a>(<a href="SDRV_IOCTL_ECHO_OFF.FIND-DEF">SDRV_IOCTL_ECHO_OFF</a>, <a href="CRTX_NULL.FIND-DEF">CRTX_NULL</a>, pSerial); <b>break</b>; <b>case</b> <a href="GETCONV.FIND-DEF">GETCONV</a>: <a href="sdrv_Ioctl.FIND-FUNC">sdrv_Ioctl</a>(<a href="SDRV_IOCTL_GET_CONV.FIND-DEF">SDRV_IOCTL_GET_CONV</a>, data, pSerial); <b>break</b>; <b>case</b> <a href="SETCONV.FIND-DEF">SETCONV</a>: <a href="sdrv_Ioctl.FIND-FUNC">sdrv_Ioctl</a>(<a href="SDRV_IOCTL_SET_CONV.FIND-DEF">SDRV_IOCTL_SET_CONV</a>, data, pSerial); <b>break</b>; <font color="blue"><b>default</b>:</font> <b>break</b>; } <b>return</b> 0;}</pre></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -