📄 c-bkend2.html
字号:
<td><hr><div class="CalloutCell"><a name="89249"><b class="symbol_UC"><font face="Helvetica, sans-serif" size="-1" class="sans">CAUTION: </font></b></a>The target server drops the number of arguments returned by the <b class="symbol_lc">parseRoutine</b> routine. So, if an error occurs during the routine parsing phase, the routine should return 0, so the target server can resynchronize itself on the next command line argument.</div></td></tr><tr valign="top"><td></td><td><hr></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p callout></dl><h4 class="EntityTitle"><a name="88613"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 2-1: Flag-parsing Routine</font></a></h4><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="88650"> </a>Suppose you want to define a back end which can take a serial line speed argument. This argument will be coded with <b class="command">-speed</b> <i class="textVariable">value</i> on the command line. Your code includes declarations and <b class="routine"><i class="routine">mybkendFlagsGet</i></b><b>( )</b> as follows:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84637">static int mySpeed = 9600; /* mybackend default speed */ static FLAG_DESC mybkendFlags/* my flags descriptions */ { {"-speed", "-sp", flagInt, (void *) &mySpeed, "-sp[eed] myBkend Speed definition (default 9600)."}, {NULL, NULL, NULL, 0, NULL} /* End Of Record delimiter */ }; ... FLAG_DESC * mybkendFlagsGet (void) { return (FLAG_DESC *) mybkendFlags; }</a></b></pre></dl><dd><p class="Body"><a name="84651"> </a>Now, suppose you launch the target server with the following command line:</p></dl><dl class="margin"><dd><pre class="Code"><b><a name="84652">tgtsvr -B mybackend -speed 19200 targetName</a></b></pre></dl><dl class="margin"><dd><p class="Body"><a name="84653"> </a>When the target server loads <b class="keyword">mybkend</b>, it calls <b class="routine"><i class="routine">mybkendFlagsGet</i></b><b>( )</b>, appends the <b class="keyword">mybkendFlags</b> array to its own <b class="symbol_UC">FLAG_DESC</b> array, and parses the command line. When it parses the <b class="command">-speed</b> argument, it calls <b class="routine"><i class="routine">flagInt</i></b><b>( )</b> with the first argument set to 3, the second argument pointing to the array "<b class="symbol_lc">-speed 19200 targetName</b>", and the last argument set to <b class="symbol_lc">&mySpeed</b>.</p><dd><p class="Body"><a name="84654"> </a>This routine interprets the <b class="symbol_lc">19200</b> string as an integer and stores that value at the <b class="symbol_lc">&mySpeed </b>location. Thus, the back end speed is modified by the command line parameter.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84655">The Initialization Routine</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84656"> </a>When the target server attaches a back end, it calls the initialization routine of the named back end. The main purpose of this routine is to fill in the <b class="symbol_UC">TGT_OPS</b> structure defined in <i class="textVariable">installDir</i><b class="file">/host/include/tgtlib.h</b>, and the <b class="symbol_UC">BKEND_INFO</b> structure defined in <i class="textVariable">installDir</i><b class="file">/host/include/bkendlib.h</b>. It also performs whatever back-end initialization is appropriate.</p><dd><p class="Body"><a name="84657"> </a>The <b class="symbol_UC">TGT_OPS</b> structure, whose location is passed as an argument to the back-end initialization routine, is a collection of function pointers. The initialization routine fills this structure with pointers to functions that provide the back-end services. The implementation of the services is entirely back-end dependent. By virtue of the coupling, it is possible to support many back ends, each providing the same service in its own way. Once the <b class="symbol_UC">TGT_OPS </b>structure is filled in and returned, the target server is ready to start communicating with the target.</p><dd><p class="Body"><a name="84658"> </a>The <b class="symbol_UC">BKEND_INFO</b> structure, whose location is also passed as an argument to the back-end initialization routine, contains information about the newly loaded back end. This information consists of the back-end version and the asynchronous events notification. Once the <b class="symbol_UC">BKEND_INFO</b> structure is filled, the target server launches a thread which handles the asynchronous events according to the given method.</p><dd><p class="Body"><a name="84659"> </a>The name of the initialization routine is standardized so that the target server can find and attach the back end chosen by the user. It is composed of the back-end file name (without extension) plus the word <i class="term">Initialize</i>. For our example back end, called <b class="keyword">mybkend</b>, the initialization routine is <b class="routine"><i class="routine">mybkendInitialize</i></b><b>( )</b>, which is defined in <b class="file">mybkend.c</b>. This name indicates the DLL that the target server loads as well as the back-end initialization routine to call. The back-end name is passed to the target server by the <b class="command">-B</b> option when the target server is started.</p><dd><p class="Body"><a name="84661"> </a>The back-end initialization routine must match the following prototype:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84662">STATUS mybkendInitialize ( char * tgtName, /* network unique target name to connect */ TGT_OPS * pTgtOps /* vector of back end functions to fill */ BKEND_INFO * pBkendInfo /* Backend notification method */ )</a></b></pre></dl><dd><p class="Body"><a name="84672"> </a>The first parameter, <b class="symbol_lc">tgtName</b>, passed to the back-end initialization routine from the target server specifies the target name. The meaning of the parameter is completely back-end dependent. For example, the <b class="keyword">wdbrpc</b> back end uses the <b class="symbol_lc">tgtName</b> as the target's IP address while the <b class="keyword">wdbserial</b> back end doesn't use <b class="symbol_lc">tgtName</b>. The only restriction is that <b class="symbol_lc">tgtName</b> must be unique for each target server.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84674">The <b class="symbol_UC">TGT_OPS</b> Structure</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84676"> </a>The second parameter passed to the back-end initialization routine, <b class="symbol_lc">pTgtOps</b>, is a pointer to the <b class="symbol_UC">TGT_OPS</b> structure (defined in <i class="textVariable">installDir</i><b class="file">/host/include/tgtlib.h</b>), which declares the target server back-end interface. The initialization routine must fill this structure with the appropriate information and function pointers so that the target server understands what the back end can do. When the target server needs to perform a service, it calls the appropriate back-end function.</p></dl><dl class="margin"><dd><pre class="Code"><b><a name="84677">typedef struct tgt_ops /* target server back end operations */ { BOOL tgtConnected; /* TRUE if connected to Target */ TGT_LINK_DESC tgtLink; /* link descriptor */ u_int tgtSupInfo; /* additional information */ int tgtEventFd; /* not used. Use BKEND_INFO structure*/ /* back end routines pointers */ UINT32 (*tgtPingRtn) (void); UINT32 (*tgtConnectRtn) (WDB_TGT_INFO *); UINT32 (*tgtDisconnectRtn) (void); UINT32 (*tgtModeSetRtn) (UINT32 *); ... } TGT_OPS;</a></b></pre></dl><dl class="margin"><dd><p class="Body"><a name="84692"> </a>The <b class="symbol_lc">tgtConnected</b> field is a boolean variable that is TRUE if the target board is connected to the target server by the back end. This boolean must be set to FALSE by the initialization routine because the board is not yet connected. It is set to TRUE by the target server if the target-board connection succeeds.</p><dd><p class="Body"><a name="84693"> </a>The <b class="symbol_lc">tgtLink</b> field is a <b class="symbol_UC">TGT_LINK_DESC</b> structure also defined in <i class="textVariable">installDir</i><b class="file">/host/include/tgtlib.h</b>. This structure describes the communication link with the board and is composed of three fields: </p><dl class="margin"><dd><pre class="Code2"><b><a name="84694">typedef struct { char * name; /* target/host link name */ u_int type; /* target/host link type */ u_int speed; /* target/host link speed in bps */ } TGT_LINK_DESC;</a></b></pre></dl><dd><p class="Body"><a name="84700"> </a>The <b class="symbol_lc">tgtSupInfo</b> field is a target server spare field.</p><dd><p class="Body"><a name="84701"> </a>The <b class="symbol_lc">tgtEventFd</b> field obsolete. The file descriptor that the back end uses to wake up the target server when an asynchronous event arrives from the target system is now stored in the <b class="symbol_UC">BKEND_INFO</b> structure. The back end should set <b class="symbol_lc">tgtEventFd</b> to <b class="symbol_UC">NONE</b>.</p></dl></dl><dl class="margin"><dd><p class="table" callout><table border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td valign="top" width="40"><br><img border="0" alt="*" src="icons/caution.gif"></td><td><hr><div class="CalloutCell"><a name="89265"><b class="symbol_UC"><font face="Helvetica, sans-serif" size="-1" class="sans">CAUTION: </font></b></a>Tornado expects to receive event information in target byte order. Some emulators may automatically convert all data sent to the host to host byte order. The byte order conversion macros <b class="symbol_UC">FIX_16</b> and <b class="symbol_UC">FIX_32</b> manage this. They are located in <i class="textVariable">installDir</i><b class="file">/host/src/tgtsvr/backend/bedk/backend.h</b>.</div></td></tr><tr valign="top"><td></td><td><hr></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p callout><dl class="margin"><dd><p class="Body"><a name="84711"> </a>The <b class="symbol_lc">tgtPingRtn</b> field is the first of many back-end function pointers. Each function is a service provided by the back end. The complete description of each function is given in the online reference material under <b class="guiLabel"><font face="Helvetica, sans-serif" size="-1" class="sans">Tornado API Reference>Target Server Back End Interface</font></b>. (Note that the function names are similar to the pointer names, but are prefixed with <i class="term">bkend</i>. All function pointers of this structure must be filled in. If a back-end service is not provided, set the pointer to <b class="symbol_UC">NULL</b>.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84717">The<b class="symbol_UC"> BKEND_INFO</b> Structure</a></i></h4></font><dl class="margin"><dl class="margin">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -