1 // *********************************************************************
    2 //   Custom DPA Handler code template                                  *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Template.c,v $
    7 // Version: $Revision: 1.69 $
    8 // Date:    $Date: 2022/02/25 09:41:25 $
    9 //
   10 // Revision history:
   11 //   2022/02/24  Release for DPA 4.17
   12 //   2021/08/20  Release for DPA 4.16
   13 //   2020/09/03  Release for DPA 4.15
   14 //   2020/02/27  Release for DPA 4.13
   15 //   2019/01/10  Release for DPA 4.00
   16 //   2017/08/14  Release for DPA 3.01
   17 //   2017/03/13  Release for DPA 3.00
   18 //   2015/08/05  Release for DPA 2.20
   19 //   2014/10/31  Release for DPA 2.10
   20 //   2014/04/30  Release for DPA 2.00
   21 //
   22 // *********************************************************************
   23 
   24 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   25 
   26 /*<
   27 This is a Custom DPA Handler with all available events prepared.
   28 Use the following define to use a Coordinator version instead of default Node version.
   29 `#define COORDINATOR_CUSTOM_HANDLER`
   30 >*/
   31 
   32 // Default IQRF include (modify the path according to your setup)
   33 #include "IQRF.h"
   34 
   35 // Default DPA header (modify the path according to your setup)
   36 #include "DPA.h"
   37 // Default Custom DPA Handler header (modify the path according to your setup)
   38 #include "DPAcustomHandler.h"
   39 
   40 // Uncomment the following includes if the respective component is needed
   41 // IQRF standards header (modify the path according to your setup)
   42 //#include "IQRFstandard.h"
   43 //#include "IQRF_HWPID.h"
   44 //#include "NFC.c"
   45 
   46 //############################################################################################
   47 
   48 // Place for global variables shared among CustomDpaHandler() and other function, otherwise local [static] variables are recommended
   49 // example: uns8 globalCounter;
   50 
   51 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   52 //############################################################################################
   53 // https://doc.iqrf.org/DpaTechGuide/pages/custom-dpa-handler.html
   54 bit CustomDpaHandler()
   55 //############################################################################################
   56 {
   57   // Handler presence mark
   58   clrwdt();
   59 
   60   // Place for local static variables used only within CustomDpaHandler() among more events
   61   // example: static bit interruptOccured;
   62 
   63   // Detect DPA event to handle (unused event handlers can be commented out or even deleted)
   64   switch ( GetDpaEvent() )
   65   {
   66 #ifdef DpaEvent_Interrupt
   67     // -------------------------------------------------
   68     case DpaEvent_Interrupt:
   69       // Do an extra quick background interrupt work
   70       // ! The time spent handling this event is critical.If there is no interrupt to handle return immediately otherwise keep the code as fast as possible.
   71       // ! Make sure the event is the 1st case in the main switch statement at the handler routine.This ensures that the event is handled as the 1st one.
   72       // ! It is desirable that this event is handled with immediate return even if it is not used by the custom handler because the Interrupt event is raised on every MCU interrupt and the “empty” return handler ensures the shortest possible interrupt routine response time.
   73       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   74       // ! Make sure race condition does not occur when accessing those variables at other places.
   75       // ! Make sure( inspect.lst file generated by C compiler ) compiler does not create any hidden temporary local variable( occurs when using division, multiplication or bit shifts ) at the event handler code.The name of such variable is usually Cnumbercnt.
   76       // ! Do not call any OS functions except setINDFx().
   77       // ! Do not use any OS variables especially for writing access.
   78       // ! All above rules apply also to any other function being called from the event handler code, although calling any function from Interrupt event is not recommended because of additional MCU stack usage.
   79       // https://doc.iqrf.org/DpaTechGuide/pages/EventInterrupt.html
   80       return Carry;
   81 #endif
   82       // -------------------------------------------------
   83     case DpaEvent_Idle:
   84       // Do a quick background work when RF packet is not received
   85       // https://doc.iqrf.org/DpaTechGuide/pages/idle.html
   86       break;
   87 
   88       // -------------------------------------------------
   89     case DpaEvent_Reset:
   90       // Called after module is reset
   91       // https://doc.iqrf.org/DpaTechGuide/pages/ResetEvent.html
   92 
   93       //goto DpaHandleReturnTRUE; // return TRUE only if you handle node bonding/unbonding
   94       break;
   95 
   96 #ifndef COORDINATOR_CUSTOM_HANDLER
   97       // -------------------------------------------------
   98     case DpaEvent_BondingButton:
   99       // Called to allow a bonding button customization
  100       // https://doc.iqrf.org/DpaTechGuide/pages/bondingbutton.html
  101 
  102       //goto DpaHandleReturnTRUE; // return TRUE to handle bonding button
  103       break;
  104 #endif
  105 
  106 #ifndef COORDINATOR_CUSTOM_HANDLER
  107       // -------------------------------------------------
  108     case DpaEvent_Indicate:
  109       // Called to allow a customization of the device indication
  110       // https://doc.iqrf.org/DpaTechGuide/pages/IndicateEvent.html
  111 
  112       //goto DpaHandleReturnTRUE; // return TRUE to skip default indication
  113       break;
  114 #endif
  115 
  116       // -------------------------------------------------
  117     case DpaEvent_Init:
  118       // Do a one time initialization before main loop starts
  119       // https://doc.iqrf.org/DpaTechGuide/pages/init.html
  120       break;
  121 
  122       // -------------------------------------------------
  123     case DpaEvent_ReceiveDpaRequest:
  124       // Called after DPA request was received
  125       // https://doc.iqrf.org/DpaTechGuide/pages/receivedparequest.html
  126 
  127       //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  128       break;
  129 
  130       // -------------------------------------------------
  131     case DpaEvent_BeforeSendingDpaResponse:
  132       // Called before sending DPA response back to originator of DPA response
  133       // https://doc.iqrf.org/DpaTechGuide/pages/beforesendingdparesponse.html
  134       break;
  135 
  136       // -------------------------------------------------
  137     case DpaEvent_Notification:
  138       // Called after DPA request was processed and after DPA response was sent
  139       // https://doc.iqrf.org/DpaTechGuide/pages/notification.html
  140       break;
  141 
  142       // -------------------------------------------------
  143     case DpaEvent_AfterRouting:
  144       // Called after Notification and after routing of the DPA response was finished
  145       // https://doc.iqrf.org/DpaTechGuide/pages/afterrouting.html
  146       break;
  147 
  148 #ifndef COORDINATOR_CUSTOM_HANDLER
  149       // -------------------------------------------------
  150     case DpaEvent_FrcValue:
  151       // Called to get FRC value
  152       // https://doc.iqrf.org/DpaTechGuide/pages/frcvalue.html
  153       break;
  154 #endif
  155 
  156 #ifndef COORDINATOR_CUSTOM_HANDLER
  157       // -------------------------------------------------
  158     case DpaEvent_FrcResponseTime:
  159       // Called to get FRC response time
  160       // https://doc.iqrf.org/DpaTechGuide/pages/frcresponsetime.html
  161       break;
  162 #endif
  163 
  164 #ifndef COORDINATOR_CUSTOM_HANDLER
  165       // -------------------------------------------------
  166     case DpaEvent_BeforeSleep:
  167       // Called before going to sleep
  168       // https://doc.iqrf.org/DpaTechGuide/pages/beforesleep.html
  169       break;
  170 #endif
  171 
  172 #ifndef COORDINATOR_CUSTOM_HANDLER
  173       // -------------------------------------------------
  174     case DpaEvent_AfterSleep:
  175       // Called after woken up after sleep
  176       // https://doc.iqrf.org/DpaTechGuide/pages/aftersleep.html
  177       break;
  178 #endif
  179 
  180       // -------------------------------------------------
  181     case DpaEvent_DisableInterrupts:
  182       // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
  183       // https://doc.iqrf.org/DpaTechGuide/pages/eventDisableInterrupts.html
  184       break;
  185 
  186 #ifdef COORDINATOR_CUSTOM_HANDLER
  187       // -------------------------------------------------
  188     case DpaEvent_ReceiveDpaResponse:
  189       // Called after DPA response was received at coordinator
  190       // https://doc.iqrf.org/DpaTechGuide/pages/receivedparesponse.html
  191 
  192       //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  193       break;
  194 #endif
  195 
  196 #ifdef COORDINATOR_CUSTOM_HANDLER
  197       // -------------------------------------------------
  198     case DpaEvent_IFaceReceive:
  199       // Called after DPA request from interface master was received at coordinator
  200       // https://doc.iqrf.org/DpaTechGuide/pages/ifacereceive.html
  201 
  202       //goto DpaHandleReturnTRUE; // return TRUE to skip default processing
  203       break;
  204 #endif
  205 
  206       // -------------------------------------------------
  207     case DpaEvent_PeerToPeer:
  208       // Called when peer-to-peer (non-networking) packet is received
  209       // https://doc.iqrf.org/DpaTechGuide/pages/peertopeer.html
  210       break;
  211 
  212       // -------------------------------------------------
  213     case DpaEvent_UserDpaValue:
  214       // Called when DPA is required to return User defined DPA value in the response
  215       // https://doc.iqrf.org/DpaTechGuide/pages/userdpavalue.html
  216       break;
  217 
  218 #ifndef COORDINATOR_CUSTOM_HANDLER
  219       // -------------------------------------------------
  220     case DpaEvent_VerifyLocalFrc:
  221       // Called to verify local FRC command
  222       // https://doc.iqrf.org/DpaTechGuide/pages/verifylocalfrc.html
  223 
  224       //goto DpaHandleReturnTRUE; // return TRUE allow FRC command
  225       break;
  226 #endif
  227 
  228       // -------------------------------------------------
  229     case DpaEvent_DpaRequest:
  230       // Called to interpret DPA request for peripherals
  231       // https://doc.iqrf.org/DpaTechGuide/pages/EventDpaRequest.html
  232       IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
  233       {
  234         // -------------------------------------------------
  235         // Peripheral enumeration
  236         // https://doc.iqrf.org/DpaTechGuide/pages/enumerate-peripherals.html
  237 
  238         _DpaMessage.EnumPeripheralsAnswer.UserPerNr |= 0; // ?
  239         // FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 ); // ?
  240         _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x000F; // ????
  241         _DpaMessage.EnumPeripheralsAnswer.HWPIDver |= 0; // ????
  242 
  243 DpaHandleReturnTRUE:
  244         return TRUE;
  245       }
  246       else
  247       {
  248       // -------------------------------------------------
  249       // Get information about peripheral
  250       // https://doc.iqrf.org/DpaTechGuide/pages/get-peripheral-info.html
  251 
  252       if ( _PNUM == PNUM_USER + 0 ) // ?
  253       {
  254         _DpaMessage.PeripheralInfoAnswer.PerT = 0; // PERIPHERAL_TYPE_?
  255         _DpaMessage.PeripheralInfoAnswer.PerTE = 0; // PERIPHERAL_TYPE_EXTENDED_?
  256         _DpaMessage.PeripheralInfoAnswer.Par1 = 0; // ?
  257         _DpaMessage.PeripheralInfoAnswer.Par2 = 0; // ?
  258         goto DpaHandleReturnTRUE;
  259       }
  260 
  261       break;
  262       }
  263 
  264       // -------------------------------------------------
  265       // Handle peripheral command
  266       // https://doc.iqrf.org/DpaTechGuide/pages/handle-peripheral-request.html
  267 
  268       if ( _PNUM == PNUM_USER + 0 ) // ?
  269       {
  270         if ( _PCMD == 0 ) // ????
  271         {
  272           goto DpaHandleReturnTRUE;
  273         }
  274       }
  275 
  276       break;
  277   }
  278 
  279 DpaHandleReturnFALSE:
  280   return FALSE;
  281 }
  282 
  283 //############################################################################################
  284 // Uncomment the following includes if the respective component is needed
  285 //#include "NFC.c"
  286 
  287 // Default Custom DPA Handler header; 2nd include implementing a Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup)
  288 #include "DPAcustomHandler.h"
  289 //############################################################################################