helium / console-decoders

Payload decoder functions for console.
59 stars 62 forks source link

Payload for Digital Matter Yabby Edge is not decoded #43

Open raivisdejus opened 2 years ago

raivisdejus commented 2 years ago

A decoder for /Digital_Matter/Yabby_LoRaWAN/decoder.js does not work.

The payload received from the Yabby Edge devices is some Base64 encoded string. The current decoder function implementation in the console can't process it as the decoder functions expect the parameter to be in hexadecimal encoding.

This is a sample string we get from the device XR/1IU3kYg4ABcE= It is properly decoded by a decoder on Digital Matter site https://support.digitalmatter.com/support/solutions/articles/16000077653-decoding-the-yabby-lorawan-payload but does not work with the decoder in the repo, if it is saved in the Helium console as a decoder function.

Digital Matter decoder has this js function to get the value for bytes parameter used In the decoder.

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
      function getBase64(input)
      {
          var output = new Array();
          var chr1, chr2, chr3;
          var enc1, enc2, enc3, enc4;
          var i = 0;

          var orig_input = input;
          input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

          if (orig_input != input)
              return null;

          if (input.length % 4)
              return null;

          var j = 0;
          while (i < input.length)
          {
              enc1 = keyStr.indexOf(input.charAt(i++));
              enc2 = keyStr.indexOf(input.charAt(i++));
              enc3 = keyStr.indexOf(input.charAt(i++));
              enc4 = keyStr.indexOf(input.charAt(i++));

              chr1 = (enc1 << 2) | (enc2 >> 4);
              chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
              chr3 = ((enc3 & 3) << 6) | enc4;

              output[j++] = chr1;
              if (enc3 != 64)
                output[j++] = chr2;
              if (enc4 != 64)
                output[j++] = chr3;
          }

          return output;
      }

Is there a way to use this function to process the paylod value before it is sent to the decoder?