⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node_8cpp-source.html

📁 美国COPLEY驱动器,程序开发工具之一.
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="l00524"></a>00524 <span class="comment">       wait for a node guarding message from the host before indicating</span><a name="l00525"></a>00525 <span class="comment">       a local error.  The nodes timeout (life time) is guardTime * lifeFactor.</span><a name="l00526"></a>00526 <span class="comment">       This parameter must be between 0 and 255.  If it's zero, then life</span><a name="l00527"></a>00527 <span class="comment">       guarding on the node is disabled.</span><a name="l00528"></a>00528 <span class="comment">       </span><a name="l00529"></a>00529 <span class="comment">@return A pointer to an error object, or NULL on success</span><a name="l00530"></a>00530 <span class="comment">*/</span><a name="l00531"></a>00531 <span class="comment">/***************************************************************************/</span><a name="l00532"></a><a class="code" href="classNode.html#a5">00532</a> <span class="keyword">const</span> <a class="code" href="classError.html">Error</a> *<a class="code" href="classNode.html#a5">Node::StartNodeGuard</a>( <a class="code" href="CML__Utils_8h.html#a8">uint16</a> guardTime, <a class="code" href="CML__Utils_8h.html#a2">byte</a> lifeFactor )<a name="l00533"></a>00533 {<a name="l00534"></a>00534    <span class="comment">// If guard time is zero, return an error.</span><a name="l00535"></a>00535    <span class="comment">// Note that the lifeFactor can be zero, this disables the life</span><a name="l00536"></a>00536    <span class="comment">// guarding inside the node.</span><a name="l00537"></a>00537    <span class="keywordflow">if</span>( !guardTime )<a name="l00538"></a>00538       <span class="keywordflow">return</span> &amp;<a class="code" href="classCanOpenError.html#s1">CanOpenError::BadParam</a>;<a name="l00539"></a>00539 <a name="l00540"></a>00540    <span class="comment">// Program the life time factor</span><a name="l00541"></a>00541    <span class="keyword">const</span> <a class="code" href="classError.html">Error</a> *err;<a name="l00542"></a>00542    err = <a class="code" href="classNode.html#o0">sdo</a>.<a class="code" href="classSDO.html#a12">Dnld8</a>( 0x100D, 0, lifeFactor );<a name="l00543"></a>00543 <a name="l00544"></a>00544    <span class="comment">// Program the guard time</span><a name="l00545"></a>00545    <span class="keywordflow">if</span>( !err ) err = <a class="code" href="classNode.html#o0">sdo</a>.<a class="code" href="classSDO.html#a10">Dnld16</a>( 0x100C, 0, guardTime );<a name="l00546"></a>00546 <a name="l00547"></a>00547    <span class="comment">// Try to set the heartbeat time to zero.</span><a name="l00548"></a>00548    <span class="comment">// This may fail (since the heartbeat may not be</span><a name="l00549"></a>00549    <span class="comment">// supported).</span><a name="l00550"></a>00550    <span class="keywordflow">if</span>( !err ) <a class="code" href="classNode.html#o0">sdo</a>.<a class="code" href="classSDO.html#a10">Dnld16</a>( 0x1017, 0, (<a class="code" href="CML__Utils_8h.html#a8">uint16</a>)0 );<a name="l00551"></a>00551 <a name="l00552"></a>00552    <span class="keywordflow">if</span>( !err )<a name="l00553"></a>00553    {<a name="l00554"></a>00554       mutex.<a class="code" href="classMutex.html#a2">Lock</a>();<a name="l00555"></a>00555       guardType    = <a class="code" href="CML__Node_8h.html#a10a8">GUARDTYPE_NODEGUARD</a>;<a name="l00556"></a>00556       guardTimeout = guardTime;<a name="l00557"></a>00557       guardToggle  = -1;<a name="l00558"></a>00558       guardEvents.<a class="code" href="classEventMap.html#a6">setBits</a>( <a class="code" href="Node_8cpp.html#a1">GUARD_EVENT_CHANGE</a> );<a name="l00559"></a>00559       mutex.<a class="code" href="classMutex.html#a3">Unlock</a>();<a name="l00560"></a>00560    }<a name="l00561"></a>00561 <a name="l00562"></a>00562    <span class="keywordflow">return</span> err;<a name="l00563"></a>00563 }<a name="l00564"></a>00564 <a name="l00565"></a>00565 <span class="comment">/***************************************************************************/</span><span class="comment"></span><a name="l00566"></a>00566 <span class="comment">/**</span><a name="l00567"></a>00567 <span class="comment">Wait for the node state to change.  This function is used after sending out</span><a name="l00568"></a>00568 <span class="comment">a network management message intended to change the node's state.  It sends</span><a name="l00569"></a>00569 <span class="comment">a remote transmit request to the node and waits for the guard message to</span><a name="l00570"></a>00570 <span class="comment">be returned.</span><a name="l00571"></a>00571 <span class="comment"></span><a name="l00572"></a>00572 <span class="comment">This process allows us to track the node's state locally without fear of </span><a name="l00573"></a>00573 <span class="comment">race conditions that would arise if we simply assumed that sending out the</span><a name="l00574"></a>00574 <span class="comment">NMT message instantly caused the state to change to the new value.</span><a name="l00575"></a>00575 <span class="comment"></span><a name="l00576"></a>00576 <span class="comment">@param newState The state that we expect the node to change to.</span><a name="l00577"></a>00577 <span class="comment">@param request If true (default), send a CAN message requesting state info.</span><a name="l00578"></a>00578 <span class="comment">               If false no request is sent.</span><a name="l00579"></a>00579 <span class="comment">@return A pointer to an error object, or NULL on success</span><a name="l00580"></a>00580 <span class="comment">*/</span><a name="l00581"></a>00581 <span class="comment">/***************************************************************************/</span><a name="l00582"></a>00582 <span class="keyword">const</span> <a class="code" href="classError.html">Error</a> *Node::WaitStateChange( <a class="code" href="CML__Node_8h.html#a9">NodeState</a> newState, <span class="keywordtype">bool</span> request )<a name="l00583"></a>00583 {<a name="l00584"></a>00584    <a class="code" href="structCanFrame.html">CanFrame</a> frame;<a name="l00585"></a>00585    frame.<a class="code" href="structCanFrame.html#o1">id</a> = nodeID + 0x700;<a name="l00586"></a>00586    frame.<a class="code" href="structCanFrame.html#o0">type</a> = <a class="code" href="CML__Can_8h.html#a3a1">CAN_FRAME_REMOTE</a>;<a name="l00587"></a>00587    frame.<a class="code" href="structCanFrame.html#o2">length</a> = 1;<a name="l00588"></a>00588 <a name="l00589"></a>00589    <a class="code" href="classEventAny.html">EventAny</a> e = <a class="code" href="Node_8cpp.html#a2">GUARD_EVENT_DSRDSTATE</a>;<a name="l00590"></a>00590 <a name="l00591"></a>00591    guardEvents.<a class="code" href="classEventMap.html#a7">clrBits</a>( <a class="code" href="Node_8cpp.html#a2">GUARD_EVENT_DSRDSTATE</a> );<a name="l00592"></a>00592 <a name="l00593"></a>00593    desired = newState;<a name="l00594"></a>00594 <a name="l00595"></a>00595    <span class="keywordflow">if</span>( <a class="code" href="classNode.html#p0">stateChangeDelay</a> )<a name="l00596"></a>00596       <a class="code" href="classThread.html#e0">sleep</a>( <a class="code" href="classNode.html#p0">stateChangeDelay</a> );<a name="l00597"></a>00597 <a name="l00598"></a>00598    <span class="keywordflow">if</span>( request ) <a class="code" href="classReceiver.html#p0">co</a>-&gt;<a class="code" href="classCanOpen.html#a5">Xmit</a>( frame );<a name="l00599"></a>00599 <a name="l00600"></a>00600    <span class="keywordflow">return</span> e.<a class="code" href="classEvent.html#a7">Wait</a>( guardEvents, <a class="code" href="classNode.html#o0">sdo</a>.<a class="code" href="classSDO.html#a28">GetTimeout</a>() );<a name="l00601"></a>00601 }<a name="l00602"></a>00602 <a name="l00603"></a>00603 <span class="comment">/***************************************************************************/</span><span class="comment"></span><a name="l00604"></a>00604 <span class="comment">/**</span><a name="l00605"></a>00605 <span class="comment">This method is called when a new node guarding / heartbeat message is received</span><a name="l00606"></a>00606 <span class="comment">from the node.</span><a name="l00607"></a>00607 <span class="comment">*/</span><a name="l00608"></a>00608 <span class="comment">/***************************************************************************/</span><a name="l00609"></a><a class="code" href="classNode.html#a39">00609</a> <span class="keywordtype">int</span> <a class="code" href="classNode.html#a39">Node::NewFrame</a>( <a class="code" href="structCanFrame.html">CanFrame</a> &amp;frame )<a name="l00610"></a>00610 {<a name="l00611"></a>00611    <span class="keywordflow">if</span>( frame.<a class="code" href="structCanFrame.html#o0">type</a> != <a class="code" href="CML__Can_8h.html#a3a0">CAN_FRAME_DATA</a> )<a name="l00612"></a>00612       <span class="keywordflow">return</span> 1;<a name="l00613"></a>00613 <a name="l00614"></a>00614    mutex.<a class="code" href="classMutex.html#a2">Lock</a>();<a name="l00615"></a>00615 <a name="l00616"></a>00616    <span class="comment">// Save the old node state</span><a name="l00617"></a>00617    <a class="code" href="CML__Node_8h.html#a9">NodeState</a> oldState = state;<a name="l00618"></a>00618 <a name="l00619"></a>00619    <span class="comment">// Check for a valid toggle bit.  The toggle bit is only</span><a name="l00620"></a>00620    <span class="comment">// used when running in node guarding mode. </span><a name="l00621"></a>00621    <span class="keywordtype">int</span> toggleErr = guardToggle ^ (frame.<a class="code" href="structCanFrame.html#o3">data</a>[0] &amp; 0x80);<a name="l00622"></a>00622 <a name="l00623"></a>00623    <span class="comment">// If the guardToggle variable is -1, then ignore toggle</span><a name="l00624"></a>00624    <span class="comment">// errors.  This is used in the modes where the toggle bit</span><a name="l00625"></a>00625    <span class="comment">// is not defined (heartbeat and no guarding).  It's also</span><a name="l00626"></a>00626    <span class="comment">// used for the first node guarding message since I can't be</span><a name="l00627"></a>00627    <span class="comment">// sure what the node's state is when I start and therefore</span><a name="l00628"></a>00628    <span class="comment">// don't know if the toggle bit should be set.</span><a name="l00629"></a>00629    <span class="keywordflow">if</span>( guardToggle &lt; 0 )<a name="l00630"></a>00630       toggleErr = 0;<a name="l00631"></a>00631 <a name="l00632"></a>00632    <span class="keywordflow">if</span>( !toggleErr )<a name="l00633"></a>00633    {<a name="l00634"></a>00634       <span class="keywordflow">switch</span>( frame.<a class="code" href="structCanFrame.html#o3">data</a>[0] &amp; 0x7f )<a name="l00635"></a>00635       {<a name="l00636"></a>00636     <span class="keywordflow">case</span> 0:   state = <a class="code" href="CML__Node_8h.html#a9a4">NODESTATE_PRE_OP</a>;      <span class="keywordflow">break</span>;<a name="l00637"></a>00637     <span class="keywordflow">case</span> 4:   state = <a class="code" href="CML__Node_8h.html#a9a3">NODESTATE_STOPPED</a>;     <span class="keywordflow">break</span>;<a name="l00638"></a>00638     <span class="keywordflow">case</span> 5:   state = <a class="code" href="CML__Node_8h.html#a9a5">NODESTATE_OPERATIONAL</a>; <span class="keywordflow">break</span>;<a name="l00639"></a>00639     <span class="keywordflow">case</span> 127: state = <a class="code" href="CML__Node_8h.html#a9a4">NODESTATE_PRE_OP</a>;      <span class="keywordflow">break</span>;<a name="l00640"></a>00640     <span class="keywordflow">default</span>:  state = <a class="code" href="CML__Node_8h.html#a9a1">NODESTATE_UNKNOWN</a>;     <span class="keywordflow">break</span>;<a name="l00641"></a>00641       }<a name="l00642"></a>00642 <a name="l00643"></a>00643       <a class="code" href="CML__Utils_8h.html#a10">uint32</a> bits = <a class="code" href="Node_8cpp.html#a0">GUARD_EVENT_MSG_RVCD</a>;<a name="l00644"></a>00644 <a name="l00645"></a>00645       <span class="comment">// If a thread is waiting for this state, wake it.</span><a name="l00646"></a>00646       <span class="keywordflow">if</span>( state == desired )<a name="l00647"></a>00647       {<a name="l00648"></a>00648     bits |= <a class="code" href="Node_8cpp.html#a2">GUARD_EVENT_DSRDSTATE</a>;<a name="l00649"></a>00649     desired = <a class="code" href="CML__Node_8h.html#a9a0">NODESTATE_INVALID</a>;<a name="l00650"></a>00650       }<a name="l00651"></a>00651 <a name="l00652"></a>00652       <span class="comment">// Notify the guarding task that a message was received.</span><a name="l00653"></a>00653       guardEvents.<a class="code" href="classEventMap.html#a6">setBits</a>( bits );<a name="l00654"></a>00654    }<a name="l00655"></a>00655    <span class="keywordflow">else</span><a name="l00656"></a>00656       <a class="code" h

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -