RUI3 (RAK Unified Interface 3) - RAK4631
RAKLorawan::enckey

Public Member Functions

bool get (uint8_t *buff, uint32_t len)
 
bool set (uint8_t *buff, uint32_t len)
 

Detailed Description

Description
This api will encrypt the date being sent and received

Member Function Documentation

◆ get()

bool get ( uint8_t *  buff,
uint32_t  len 
)
Description
This api allows to get the key of P2P mode encryption
Syntax
api.lorawan.enckey.get(buff, len)
Parameters
buffthe buffer to store encryption key
lenthe length of encryption key(must be 16 bytes)
Returns
bool
Return values
TRUEfor getting encryption key success
FALSEfor getting encryption key failure
Example
  long startTime;
  char * node_encrypt_key = "aaaabbbbccccdddd";
  uint8_t encrypt_buff[16];
  
  void setup()
  {
      Serial.begin(115200);
      startTime = millis();
    
      Serial.println("P2P Start");
      
      Serial.printf("Set Node device work mode %s\r\n", api.lorawan.nwm.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode frequency %s\r\n", api.lorawan.pfreq.set(868000000) ? "Success" : "Fail");
      Serial.printf("Set P2P mode spreading factor %s\r\n", api.lorawan.psf.set(12) ? "Success" : "Fail");
      Serial.printf("Set P2P mode bandwidth %s\r\n", api.lorawan.pbw.set(125) ? "Success" : "Fail");
      Serial.printf("Set P2P mode code rate %s\r\n", api.lorawan.pcr.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode preamble length %s\r\n", api.lorawan.ppl.set(8) ? "Success" : "Fail");
      Serial.printf("Set P2P mode tx power %s\r\n", api.lorawan.ptp.set(22) ? "Success" : "Fail");
  Serial.printf("Set P2P mode encryption status %s\r\n", api.lorawan.encry.set(1) ? "Success" : "Fail");
  Serial.printf("Set P2P mode encryption Key %s\r\n\r\n", api.lorawan.enckey.set((uint8_t *)node_encrypt_key, 16) ? "Success" : "Fail");
  
      Serial.printf("P2P encryption status = %s\r\n", api.lorawan.encry.get() ? "Enbale" : "Disable");

  api.lorawan.enckey.get(encrypt_buff, 16);
  Serial.printf("P2P encryption Key = 0x");
      for (int i = 0 ; i < 16 ; i++) {
          Serial.printf("%02X", encrypt_buff[i]);
      }
  Serial.println("");
    
      randomSeed(millis());
  }

  void loop()
  {
      uint8_t payload[] = "payload";

      int rxDelay = random(3000, 5000);
    
      // Receive P2P data every 10 seconds
      if(millis() - startTime >= 10*1000) {
        Serial.printf("P2P Rx start for %d millisSconds\r\n", rxDelay);
        startTime = millis();
        Serial.printf("P2P set Rx mode %s\r\n",api.lorawan.precv(rxDelay) ? "Success" : "Fail");
        delay(rxDelay);
      } else {
      
        Serial.printf("P2P send %s\r\n", api.lorawan.psend(sizeof(payload), payload)? "Success" : "Fail");
        delay(1000);
      }

  }

◆ set()

bool set ( uint8_t *  buff,
uint32_t  len 
)
Description
This api allows to set the key of P2P mode encryption
Syntax
api.lorawan.enckey.set(buff, len)
Parameters
buffthe buffer to set encryption key
lenthe length of encryption key(must be 16 bytes)
Returns
bool
Return values
TRUEfor setting encryption key success
FALSEfor setting encryption failure
Example
  long startTime;
  char * node_encrypt_key = "aaaabbbbccccdddd";
  uint8_t encrypt_buff[16];
  
  void setup()
  {
      Serial.begin(115200);
      startTime = millis();
    
      Serial.println("P2P Start");
      
      Serial.printf("Set Node device work mode %s\r\n", api.lorawan.nwm.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode frequency %s\r\n", api.lorawan.pfreq.set(868000000) ? "Success" : "Fail");
      Serial.printf("Set P2P mode spreading factor %s\r\n", api.lorawan.psf.set(12) ? "Success" : "Fail");
      Serial.printf("Set P2P mode bandwidth %s\r\n", api.lorawan.pbw.set(125) ? "Success" : "Fail");
      Serial.printf("Set P2P mode code rate %s\r\n", api.lorawan.pcr.set(0) ? "Success" : "Fail");
      Serial.printf("Set P2P mode preamble length %s\r\n", api.lorawan.ppl.set(8) ? "Success" : "Fail");
      Serial.printf("Set P2P mode tx power %s\r\n", api.lorawan.ptp.set(22) ? "Success" : "Fail");
  Serial.printf("Set P2P mode encryption status %s\r\n", api.lorawan.encry.set(1) ? "Success" : "Fail");
  Serial.printf("Set P2P mode encryption Key %s\r\n\r\n", api.lorawan.enckey.set((uint8_t *)node_encrypt_key, 16) ? "Success" : "Fail");
  
      Serial.printf("P2P encryption status = %s\r\n", api.lorawan.encry.get() ? "Enbale" : "Disable");

  api.lorawan.enckey.get(encrypt_buff, 16);
  Serial.printf("P2P encryption Key = 0x");
      for (int i = 0 ; i < 16 ; i++) {
          Serial.printf("%02X", encrypt_buff[i]);
      }
  Serial.println("");
    
      randomSeed(millis());
  }

  void loop()
  {
      uint8_t payload[] = "payload";

      int rxDelay = random(3000, 5000);
    
      // Receive P2P data every 10 seconds
      if(millis() - startTime >= 10*1000) {
        Serial.printf("P2P Rx start for %d millisSconds\r\n", rxDelay);
        startTime = millis();
        Serial.printf("P2P set Rx mode %s\r\n",api.lorawan.precv(rxDelay) ? "Success" : "Fail");
        delay(rxDelay);
      } else {
      
        Serial.printf("P2P send %s\r\n", api.lorawan.psend(sizeof(payload), payload)? "Success" : "Fail");
        delay(1000);
      }

  }