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