Advertisement
Guest User

Untitled

a guest
Oct 9th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <osmocom/core/bits.h>
  4. #include <osmocom/core/crc32gen.h>
  5.  
  6. /* x24 + x23 + x21 + x20 + x19 + x17 + x16 + x15 + x13 + x8 + x7 + x5 + x4 + x2 + 1 */
  7. static const struct osmo_crc32gen_code fcs = {
  8.         .bits = 24,
  9.         .poly = 0xbba1b5,
  10.         .init = 0xffffff,
  11.         .remainder = 0xffffff,
  12. };
  13.  
  14. static int
  15. rlp_process_frame(pbit_t *frame)
  16. {
  17.         ubit_t fbits[240];
  18.         int crc;
  19.  
  20.         /* Decode to bits */
  21.         osmo_pbit2ubit_ext(fbits, 0, frame, 0, 240, 1);
  22.  
  23.         /* Check CRC */
  24.         crc = osmo_crc32gen_check_bits(&fcs, fbits, 216, fbits+216);
  25.         if (crc)
  26.                 return -1;
  27.  
  28.         /* Frame type */
  29.         if (!memcmp(&fbits[3], "\x01\x01\x01\x01\x01\x01", 6))
  30.         {
  31.                 const char *m_names[32] = {
  32.                         [ 0] = "UI",
  33.                         [ 3] = "DM",
  34.                         [ 7] = "SABM",
  35.                         [ 8] = "DISC",
  36.                         [12] = "UA",
  37.                         [15] = "NULL",
  38.                         [17] = "REMAP",
  39.                         [23] = "XID",
  40.                         [29] = "TEST",
  41.                 };
  42.                 int M;
  43.  
  44.                 M = (frame[1] & 0x7c) >> 2;
  45.  
  46.                 printf(" U  : %s\n",
  47.                         m_names[M]
  48.                 );
  49.  
  50.                 if (M == 23) {
  51.                         int i;
  52.                         for (i=0; i<30; i++)
  53.                                 printf("%02x", frame[i]);
  54.                         printf("\n");
  55.                 }
  56.  
  57.         }
  58.         else if (!memcmp(&fbits[3], "\x00\x01\x01\x01\x01\x01", 6))
  59.         {
  60.                 const char *s_names[4] = { "RR", "RNR", "REJ", "SREJ" };
  61.                 int nr;
  62.  
  63.                 nr = (frame[1] >> 5) | ((frame[2] & 0x3f) << 3);
  64.  
  65.                 printf(" S  : %4s - N(R) = %3d\n",
  66.                         s_names[(frame[0] & 0x06) >> 1],
  67.                         nr
  68.                 );
  69.         }
  70.         else
  71.         {
  72.                 const char *s_names[4] = { "RR", "RNR", "REJ", "SREJ" };
  73.                 int ns, nr;
  74.  
  75.                 ns = frame[0] | ((frame[1] & 1) << 8);
  76.                 nr = (frame[1] >> 5) | ((frame[2] & 0x3f) << 3);
  77.                 printf("I+S : %4s - N(R) = %3d  N(S) = %3d\n",
  78.                         s_names[(frame[0] & 0x06) >> 1],
  79.                         nr, ns
  80.                 );
  81.  
  82.                 {
  83.                         int i;
  84.                         printf("data: ");
  85.                         for (i=3; i<27; i++)
  86.                                 printf("%02x", frame[i]);
  87.                         printf("\n");
  88.                 }
  89.         }
  90.  
  91.         return 0;
  92.  
  93. }
  94.  
  95. int main(int argc, char *argv[])
  96. {
  97.         FILE *fh;
  98.         pbit_t frame[30];
  99.  
  100.         fh = fopen(argv[1], "rb");
  101.         if (!fh)
  102.                 return -1;
  103.  
  104.         while (!feof(fh)) {
  105.                 fread(frame, 30, 1, fh);
  106.                 rlp_process_frame(frame);
  107.         }
  108.  
  109.         fclose(fh);
  110.  
  111.         return 0;
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement