1 // *********************************************************************
    2 //   Custom DPA Handler code example - Peer-to-peer receiver           *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Peer-to-Peer.c,v $
    7 // Version: $Revision: 1.23 $
    8 // Date:    $Date: 2021/04/26 15:13:50 $
    9 //
   10 // Revision history:
   11 //   2019/01/10  Release for DPA 4.00
   12 //   2017/03/13  Release for DPA 3.00
   13 //   2015/08/05  Release for DPA 2.20
   14 //
   15 // *********************************************************************
   16 
   17 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   18 
   19 // Default IQRF include (modify the path according to your setup)
   20 #include "IQRF.h"
   21 
   22 // Uncomment to compile Custom DPA Handler for Coordinator
   23 //#define COORDINATOR_CUSTOM_HANDLER
   24 
   25 // Default DPA header (modify the path according to your setup)
   26 #include "DPA.h"
   27 // Default Custom DPA Handler header (modify the path according to your setup)
   28 #include "DPAcustomHandler.h"
   29 
   30 // This example receives peer-to-peer packets sent from Peer-to-Peer-Transmitter.c example using build-in DPA support of peer-to-peer packets (event DpaEvent_PeerToPeer).
   31 // The example also shows fully user programmed implementation of peer-to-peer packet at the different channel (event DpaEvent_Idle). This approach requires more code but avoids radio jamming of the main network traffic.
   32 // !!! It is highly recommended to use additional security techniques (e.g. encryption, rolling code, checksum, CRC) against packet sniffing, spoofing and eavesdropping. 
   33 // !!!  It is also recommended to use listen-before-talk technique to minimize the risk of RF collision that might cause the main network RF traffic to fail.
   34 
   35 void HandlerPeer2PeerPacket();
   36 
   37 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   38 //############################################################################################
   39 bit CustomDpaHandler()
   40 //############################################################################################
   41 {
   42   // Handler presence mark
   43   clrwdt();
   44 
   45   // Detect DPA event to handle
   46   switch ( GetDpaEvent() )
   47   {
   48 #ifdef  DpaEvent_Interrupt
   49     // -------------------------------------------------
   50     case DpaEvent_Interrupt:
   51       // Do an extra quick background interrupt work
   52       // ! 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.
   53       // ! 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.
   54       // ! 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.
   55       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   56       // ! Make sure race condition does not occur when accessing those variables at other places.
   57       // ! 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.
   58       // ! Do not call any OS functions except setINDFx().
   59       // ! Do not use any OS variables especially for writing access.
   60       // ! 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.
   61 
   62       return Carry;
   63 #endif
   64       // -------------------------------------------------
   65     case DpaEvent_PeerToPeer:
   66       // Called when peer-to-peer (non-networking) packet is received
   67 
   68       // Handle peer-to-peer packet received by the build-in DPA support. Those packets are sent at the same channel the main network work at.
   69       HandlerPeer2PeerPacket();
   70       break;
   71 
   72       // -------------------------------------------------
   73     case DpaEvent_Idle:
   74     {
   75       // Do a quick background work when RF packet is not received
   76 
   77       // Try to receive peer-to-peer packets at the secondary main network channel. This avoids RF jamming of the main network but it is not 100% reliable as 2 channels must be scanned.
   78 
   79       // Save state of the filtering.
   80       bit saveFiltering = _filterCurrentNetwork;
   81       // Packet not received yet
   82       bit handlePacket = FALSE;
   83       // Change the channel
   84       uns8 saveRFchannel = RFchannel;
   85       setRFchannel( DpaApiReadConfigByte( CFGIND_CHANNEL_B ) );
   86       // Disable filtering to allow receiving of peer-to-peer packets
   87       setNetworkFilteringOff();
   88 
   89       // Try to receive packet
   90       if ( checkRF( DpaApiReadConfigByte( CFGIND_RXFILTER ) ) && RFRXpacket() )
   91         // Some packet received!
   92         handlePacket = TRUE;
   93 
   94       // Before handling the packet restore RF settings (there might be needed for the correct DPA command, that is stored in the packet, execution)
   95       // Restore channel
   96       setRFchannel( saveRFchannel );
   97       // Restore network filtering
   98       if ( saveFiltering )
   99         setNetworkFilteringOn();
  100 
  101       // If packet was received, handle it
  102       if ( handlePacket )
  103         HandlerPeer2PeerPacket();
  104 
  105       break;
  106     }
  107   }
  108 
  109   return FALSE;
  110 }
  111 
  112 //############################################################################################
  113 void HandlerPeer2PeerPacket()
  114 //############################################################################################
  115 {
  116   // Peer-to-peer "DPA" packet?
  117 #pragma warning Remove this branch
  118   if ( _DPAF )
  119   {
  120     // Is my local address matched?
  121     if ( _DpaParams == ntwADDR )
  122     {
  123       // Just execute the DPA request
  124       DpaApiLocalRequest();
  125       // Make sure LED is visible at LP mode
  126       waitMS( 20 );
  127     }
  128   }
  129   else
  130   {
  131     // Pure peer-to-peer packet
  132 
  133     // Is my peer-to-peer packet (check length and content)
  134     if ( DLEN == 2 && bufferRF[0] == 'P' && bufferRF[1] == ntwADDR )
  135     {
  136       // Pulse red LED
  137       pulseLEDR();
  138       // Make sure LED is visible at LP mode
  139       waitMS( 20 );
  140     }
  141   }
  142 }
  143 //############################################################################################
  144 // Default Custom DPA Handler header; 2nd include to implement Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup) 
  145 #include "DPAcustomHandler.h"
  146 //############################################################################################