1 /****************************************************************************
3 (c) SYSTEC electronic GmbH, D-07973 Greiz, August-Bebel-Str. 29
4 www.systec-electronic.com
8 Description: source file for NMT-CN-Userspace-Module
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
16 1. Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
23 3. Neither the name of SYSTEC electronic GmbH nor the names of its
24 contributors may be used to endorse or promote products derived
25 from this software without prior written permission. For written
26 permission, please contact info@systec-electronic.com.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
43 If a provision of this License is or becomes illegal, invalid or
44 unenforceable in any jurisdiction, that shall not affect:
45 1. the validity or enforceability in that jurisdiction of any other
46 provision of this License; or
47 2. the validity or enforceability in other jurisdictions of that or
48 any other provision of this License.
50 -------------------------------------------------------------------------
52 $RCSfile: EplNmtCnu.c,v $
56 $Revision: 1.6 $ $Date: 2008/10/17 15:32:32 $
63 -------------------------------------------------------------------------
67 2006/06/09 k.t.: start of the implementation
69 ****************************************************************************/
72 #include "user/EplNmtCnu.h"
73 #include "user/EplDlluCal.h"
75 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_NMT_CN)) != 0)
77 /***************************************************************************/
80 /* G L O B A L D E F I N I T I O N S */
83 /***************************************************************************/
85 //---------------------------------------------------------------------------
87 //---------------------------------------------------------------------------
89 //---------------------------------------------------------------------------
91 //---------------------------------------------------------------------------
94 unsigned int m_uiNodeId;
95 tEplNmtuCheckEventCallback m_pfnCheckEventCb;
99 //---------------------------------------------------------------------------
100 // modul globale vars
101 //---------------------------------------------------------------------------
103 static tEplNmtCnuInstance EplNmtCnuInstance_g;
105 //---------------------------------------------------------------------------
106 // local function prototypes
107 //---------------------------------------------------------------------------
109 static tEplNmtCommand EplNmtCnuGetNmtCommand(tEplFrameInfo * pFrameInfo_p);
111 static BOOL EplNmtCnuNodeIdList(u8 * pbNmtCommandDate_p);
113 static tEplKernel EplNmtCnuCommandCb(tEplFrameInfo *pFrameInfo_p);
115 //=========================================================================//
117 // P U B L I C F U N C T I O N S //
119 //=========================================================================//
121 //---------------------------------------------------------------------------
123 // Function: EplNmtCnuInit
125 // Description: init the first instance of the module
129 // Parameters: uiNodeId_p = NodeId of the local node
132 // Returns: tEplKernel = errorcode
137 //---------------------------------------------------------------------------
138 tEplKernel EplNmtCnuInit(unsigned int uiNodeId_p)
142 Ret = EplNmtCnuAddInstance(uiNodeId_p);
147 //---------------------------------------------------------------------------
149 // Function: EplNmtCnuAddInstance
151 // Description: init the add new instance of the module
155 // Parameters: uiNodeId_p = NodeId of the local node
158 // Returns: tEplKernel = errorcode
163 //---------------------------------------------------------------------------
164 tEplKernel EplNmtCnuAddInstance(unsigned int uiNodeId_p)
168 Ret = kEplSuccessful;
170 // reset instance structure
171 EPL_MEMSET(&EplNmtCnuInstance_g, 0, sizeof(EplNmtCnuInstance_g));
174 EplNmtCnuInstance_g.m_uiNodeId = uiNodeId_p;
176 // register callback-function for NMT-commands
177 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_DLLU)) != 0)
178 Ret = EplDlluCalRegAsndService(kEplDllAsndNmtCommand,
180 kEplDllAsndFilterLocal);
187 //---------------------------------------------------------------------------
189 // Function: EplNmtCnuDelInstance
191 // Description: delte instance of the module
198 // Returns: tEplKernel = errorcode
203 //---------------------------------------------------------------------------
204 tEplKernel EplNmtCnuDelInstance(void)
208 Ret = kEplSuccessful;
210 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_DLLU)) != 0)
211 // deregister callback function from DLL
212 Ret = EplDlluCalRegAsndService(kEplDllAsndNmtCommand,
213 NULL, kEplDllAsndFilterNone);
219 //---------------------------------------------------------------------------
221 // Function: EplNmtCnuSendNmtRequest
223 // Description: Send an NMT-Request to the MN
227 // Parameters: uiNodeId_p = NodeId of the local node
228 // NmtCommand_p = requested NMT-Command
231 // Returns: tEplKernel = errorcode
236 //---------------------------------------------------------------------------
237 tEplKernel EplNmtCnuSendNmtRequest(unsigned int uiNodeId_p,
238 tEplNmtCommand NmtCommand_p)
241 tEplFrameInfo NmtRequestFrameInfo;
242 tEplFrame NmtRequestFrame;
244 Ret = kEplSuccessful;
247 EPL_MEMSET(&NmtRequestFrame.m_be_abDstMac[0], 0x00, sizeof(NmtRequestFrame.m_be_abDstMac)); // set by DLL
248 EPL_MEMSET(&NmtRequestFrame.m_be_abSrcMac[0], 0x00, sizeof(NmtRequestFrame.m_be_abSrcMac)); // set by DLL
249 AmiSetWordToBe(&NmtRequestFrame.m_be_wEtherType,
250 EPL_C_DLL_ETHERTYPE_EPL);
251 AmiSetByteToLe(&NmtRequestFrame.m_le_bDstNodeId, (u8) EPL_C_ADR_MN_DEF_NODE_ID); // node id of the MN
252 AmiSetByteToLe(&NmtRequestFrame.m_le_bMessageType,
253 (u8) kEplMsgTypeAsnd);
254 AmiSetByteToLe(&NmtRequestFrame.m_Data.m_Asnd.m_le_bServiceId,
255 (u8) kEplDllAsndNmtRequest);
256 AmiSetByteToLe(&NmtRequestFrame.m_Data.m_Asnd.m_Payload.
257 m_NmtRequestService.m_le_bNmtCommandId,
259 AmiSetByteToLe(&NmtRequestFrame.m_Data.m_Asnd.m_Payload.m_NmtRequestService.m_le_bTargetNodeId, (u8) uiNodeId_p); // target for the nmt command
260 EPL_MEMSET(&NmtRequestFrame.m_Data.m_Asnd.m_Payload.m_NmtRequestService.
261 m_le_abNmtCommandData[0], 0x00,
262 sizeof(NmtRequestFrame.m_Data.m_Asnd.m_Payload.
263 m_NmtRequestService.m_le_abNmtCommandData));
265 // build info-structure
266 NmtRequestFrameInfo.m_NetTime.m_dwNanoSec = 0;
267 NmtRequestFrameInfo.m_NetTime.m_dwSec = 0;
268 NmtRequestFrameInfo.m_pFrame = &NmtRequestFrame;
269 NmtRequestFrameInfo.m_uiFrameSize = EPL_C_DLL_MINSIZE_NMTREQ; // sizeof(NmtRequestFrame);
272 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_DLLU)) != 0)
273 Ret = EplDlluCalAsyncSend(&NmtRequestFrameInfo, // pointer to frameinfo
274 kEplDllAsyncReqPrioNmt); // priority
280 //---------------------------------------------------------------------------
282 // Function: EplNmtCnuRegisterStateChangeCb
284 // Description: register Callback-function go get informed about a
285 // NMT-Change-State-Event
289 // Parameters: pfnEplNmtStateChangeCb_p = functionpointer
292 // Returns: tEplKernel = errorcode
297 //---------------------------------------------------------------------------
299 tEplKernel EplNmtCnuRegisterCheckEventCb(tEplNmtuCheckEventCallback pfnEplNmtCheckEventCb_p)
303 Ret = kEplSuccessful;
305 // save callback-function in modul global var
306 EplNmtCnuInstance_g.m_pfnCheckEventCb = pfnEplNmtCheckEventCb_p;
312 //=========================================================================//
314 // P R I V A T E F U N C T I O N S //
316 //=========================================================================//
318 //---------------------------------------------------------------------------
320 // Function: EplNmtCnuCommandCb
322 // Description: callback funktion for NMT-Commands
326 // Parameters: pFrameInfo_p = Frame with the NMT-Commando
329 // Returns: tEplKernel = errorcode
334 //---------------------------------------------------------------------------
335 static tEplKernel EplNmtCnuCommandCb(tEplFrameInfo *pFrameInfo_p)
337 tEplKernel Ret = kEplSuccessful;
338 tEplNmtCommand NmtCommand;
340 tEplNmtEvent NmtEvent = kEplNmtEventNoEvent;
342 if (pFrameInfo_p == NULL) {
343 Ret = kEplNmtInvalidFramePointer;
347 NmtCommand = EplNmtCnuGetNmtCommand(pFrameInfo_p);
350 switch (NmtCommand) {
352 //------------------------------------------------------------------------
353 // plain NMT state commands
354 case kEplNmtCmdStartNode:
355 { // send NMT-Event to state maschine kEplNmtEventStartNode
356 NmtEvent = kEplNmtEventStartNode;
360 case kEplNmtCmdStopNode:
361 { // send NMT-Event to state maschine kEplNmtEventStopNode
362 NmtEvent = kEplNmtEventStopNode;
366 case kEplNmtCmdEnterPreOperational2:
367 { // send NMT-Event to state maschine kEplNmtEventEnterPreOperational2
368 NmtEvent = kEplNmtEventEnterPreOperational2;
372 case kEplNmtCmdEnableReadyToOperate:
373 { // send NMT-Event to state maschine kEplNmtEventEnableReadyToOperate
374 NmtEvent = kEplNmtEventEnableReadyToOperate;
378 case kEplNmtCmdResetNode:
379 { // send NMT-Event to state maschine kEplNmtEventResetNode
380 NmtEvent = kEplNmtEventResetNode;
384 case kEplNmtCmdResetCommunication:
385 { // send NMT-Event to state maschine kEplNmtEventResetCom
386 NmtEvent = kEplNmtEventResetCom;
390 case kEplNmtCmdResetConfiguration:
391 { // send NMT-Event to state maschine kEplNmtEventResetConfig
392 NmtEvent = kEplNmtEventResetConfig;
396 case kEplNmtCmdSwReset:
397 { // send NMT-Event to state maschine kEplNmtEventSwReset
398 NmtEvent = kEplNmtEventSwReset;
402 //------------------------------------------------------------------------
403 // extended NMT state commands
405 case kEplNmtCmdStartNodeEx:
407 // check if own nodeid is in EPL node list
409 EplNmtCnuNodeIdList(&
410 (pFrameInfo_p->m_pFrame->m_Data.
413 m_le_abNmtCommandData[0]));
414 if (fNodeIdInList != FALSE) { // own nodeid in list
415 // send event to process command
416 NmtEvent = kEplNmtEventStartNode;
421 case kEplNmtCmdStopNodeEx:
422 { // check if own nodeid is in EPL node list
424 EplNmtCnuNodeIdList(&pFrameInfo_p->m_pFrame->m_Data.
427 m_le_abNmtCommandData[0]);
428 if (fNodeIdInList != FALSE) { // own nodeid in list
429 // send event to process command
430 NmtEvent = kEplNmtEventStopNode;
435 case kEplNmtCmdEnterPreOperational2Ex:
436 { // check if own nodeid is in EPL node list
438 EplNmtCnuNodeIdList(&pFrameInfo_p->m_pFrame->m_Data.
441 m_le_abNmtCommandData[0]);
442 if (fNodeIdInList != FALSE) { // own nodeid in list
443 // send event to process command
444 NmtEvent = kEplNmtEventEnterPreOperational2;
449 case kEplNmtCmdEnableReadyToOperateEx:
450 { // check if own nodeid is in EPL node list
452 EplNmtCnuNodeIdList(&pFrameInfo_p->m_pFrame->m_Data.
455 m_le_abNmtCommandData[0]);
456 if (fNodeIdInList != FALSE) { // own nodeid in list
457 // send event to process command
458 NmtEvent = kEplNmtEventEnableReadyToOperate;
463 case kEplNmtCmdResetNodeEx:
464 { // check if own nodeid is in EPL node list
466 EplNmtCnuNodeIdList(&pFrameInfo_p->m_pFrame->m_Data.
469 m_le_abNmtCommandData[0]);
470 if (fNodeIdInList != FALSE) { // own nodeid in list
471 // send event to process command
472 NmtEvent = kEplNmtEventResetNode;
477 case kEplNmtCmdResetCommunicationEx:
478 { // check if own nodeid is in EPL node list
480 EplNmtCnuNodeIdList(&pFrameInfo_p->m_pFrame->m_Data.
483 m_le_abNmtCommandData[0]);
484 if (fNodeIdInList != FALSE) { // own nodeid in list
485 // send event to process command
486 NmtEvent = kEplNmtEventResetCom;
491 case kEplNmtCmdResetConfigurationEx:
492 { // check if own nodeid is in EPL node list
494 EplNmtCnuNodeIdList(&pFrameInfo_p->m_pFrame->m_Data.
497 m_le_abNmtCommandData[0]);
498 if (fNodeIdInList != FALSE) { // own nodeid in list
499 // send event to process command
500 NmtEvent = kEplNmtEventResetConfig;
505 case kEplNmtCmdSwResetEx:
506 { // check if own nodeid is in EPL node list
508 EplNmtCnuNodeIdList(&pFrameInfo_p->m_pFrame->m_Data.
511 m_le_abNmtCommandData[0]);
512 if (fNodeIdInList != FALSE) { // own nodeid in list
513 // send event to process command
514 NmtEvent = kEplNmtEventSwReset;
519 //------------------------------------------------------------------------
520 // NMT managing commands
522 // TODO: add functions to process managing command (optional)
524 case kEplNmtCmdNetHostNameSet:
529 case kEplNmtCmdFlushArpEntry:
534 //------------------------------------------------------------------------
537 // TODO: forward event with infos to the application (optional)
539 case kEplNmtCmdPublishConfiguredCN:
544 case kEplNmtCmdPublishActiveCN:
549 case kEplNmtCmdPublishPreOperational1:
554 case kEplNmtCmdPublishPreOperational2:
559 case kEplNmtCmdPublishReadyToOperate:
564 case kEplNmtCmdPublishOperational:
569 case kEplNmtCmdPublishStopped:
574 case kEplNmtCmdPublishEmergencyNew:
579 case kEplNmtCmdPublishTime:
584 //-----------------------------------------------------------------------
586 // -> requested command not supported by MN
587 case kEplNmtCmdInvalidService:
590 // TODO: errorevent to application
594 //------------------------------------------------------------------------
598 Ret = kEplNmtUnknownCommand;
602 } // end of switch(NmtCommand)
604 if (NmtEvent != kEplNmtEventNoEvent) {
605 if (EplNmtCnuInstance_g.m_pfnCheckEventCb != NULL) {
606 Ret = EplNmtCnuInstance_g.m_pfnCheckEventCb(NmtEvent);
607 if (Ret == kEplReject) {
608 Ret = kEplSuccessful;
610 } else if (Ret != kEplSuccessful) {
614 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_NMTU)) != 0)
615 Ret = EplNmtuNmtEvent(NmtEvent);
624 //---------------------------------------------------------------------------
626 // Function: EplNmtCnuGetNmtCommand()
628 // Description: returns the NMT-Command from the frame
632 // Parameters: pFrameInfo_p = pointer to the Frame
633 // with the NMT-Command
636 // Returns: tEplNmtCommand = NMT-Command
641 //---------------------------------------------------------------------------
642 static tEplNmtCommand EplNmtCnuGetNmtCommand(tEplFrameInfo * pFrameInfo_p)
644 tEplNmtCommand NmtCommand;
645 tEplNmtCommandService *pNmtCommandService;
648 &pFrameInfo_p->m_pFrame->m_Data.m_Asnd.m_Payload.
652 (tEplNmtCommand) AmiGetByteFromLe(&pNmtCommandService->
658 //---------------------------------------------------------------------------
660 // Function: EplNmtCnuNodeIdList()
662 // Description: check if the own nodeid is set in EPL Node List
666 // Parameters: pbNmtCommandDate_p = pointer to the data of the NMT Command
669 // Returns: BOOL = TRUE if nodeid is set in EPL Node List
670 // FALSE if nodeid not set in EPL Node List
675 //---------------------------------------------------------------------------
676 static BOOL EplNmtCnuNodeIdList(u8 * pbNmtCommandDate_p)
679 unsigned int uiByteOffset;
683 // get byte-offset of the own nodeid in NodeIdList
685 uiByteOffset = (unsigned int)(EplNmtCnuInstance_g.m_uiNodeId >> 3);
687 bBitOffset = (u8) EplNmtCnuInstance_g.m_uiNodeId % 8;
689 bNodeListByte = AmiGetByteFromLe(&pbNmtCommandDate_p[uiByteOffset]);
690 if ((bNodeListByte & bBitOffset) == 0) {
691 fNodeIdInList = FALSE;
693 fNodeIdInList = TRUE;
696 return fNodeIdInList;
699 #endif // #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_NMT_CN)) != 0)