모종의 음모/HDLC2022. 8. 29. 14:42

 

Flag field가 아닌 Data에 0x7E가 있는지 확인

0x7E -< 0x7D, 0x5E를 삽입

[링크 : https://blog.daum.net/trts1004/12109121]

 

232와 같은 비동기 전송일 경우 bit stuff를 적용하지 않는다고..?

Synchronous framing
Because a flag sequence consists of six consecutive 1-bits, other data is coded to ensure that it never contains more than five 1-bits in a row. This is done by bit stuffing: any time that five consecutive 1-bits appear in the transmitted data, the data is paused and a 0-bit is transmitted.


Asynchronous framing
When using asynchronous serial communication such as standard RS-232 serial ports, synchronous-style bit stuffing is inappropriate for several reasons:

Bit stuffing is not needed to ensure an adequate number of transitions, as start and stop bits provide that,
Because the data is NRZ encoded for transmission, rather than NRZI encoded, the encoded waveform is different,
RS-232 sends bits in groups of 8, making adding single bits very awkward, and
For the same reason, it is only necessary to specially code flag bytes; it is not necessary to worry about the bit pattern straddling multiple bytes.
Instead asynchronous framing uses "control-octet transparency", also called "byte stuffing" or "octet stuffing". The frame boundary octet is 01111110, (0x7E in hexadecimal notation). A "control escape octet", has the value 0x7D (bit sequence '10111110', as RS-232 transmits least-significant bit first). If either of these two octets appears in the transmitted data, an escape octet is sent, followed by the original data octet with bit 5 inverted. For example, the byte 0x7E would be transmitted as 0x7D 0x5E ("10111110 01111010"). Other reserved octet values (such as XON or XOFF) can be escaped in the same way if necessary.

The "abort sequence" 0x7D 0x7E ends a packet with an incomplete byte-stuff sequence, forcing the receiver to detect an error. This can be used to abort packet transmission with no chance the partial packet will be interpreted as valid by the receiver.

[링크 : https://en.wikipedia.org/wiki/High-Level_Data_Link_Control]

Posted by 구차니
모종의 음모/HDLC2022. 8. 29. 14:28

잘 작동하는지 모름, 걍 밑바닥에서 부터 작성함

 

$ cat hdlc_nrzi_enc.c
#include <stdio.h>

void main()
{
        unsigned char data[10] =                {0x7E, 0x11, 0x22, 0x33, 0x1E, 0x0A, 0x7E, 0x00, 0x00, 0x00};
        unsigned char lsbmsb[10] =              {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        unsigned char bitstuff[10] =    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        unsigned char nrzi[10] =                {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

        int i = 0;
        int b = 0;

        // msb to lsb, data order change
        printf("bitwise LSB convert ----------------------------------------\n");
        for(i = 0; i < 10; i++)
        {
                unsigned char tempval = 0;
                unsigned char orival = 0;
                orival = data[i];
                for(b = 0; b < 8; b++)
                {
                        tempval |= ((orival & 0x01) << (7 - b));
                        orival = orival >> 1;
                }
                lsbmsb[i] = tempval;
                printf("%02X -> %02X\n",data[i],lsbmsb[i]);
        }

        // zrzi
        printf("NRZ-I ----------------------------------------\n");
        unsigned char last = 0;
        unsigned char out = 0;
        for(i = 0; i < 80; i++)
        {
                last = (lsbmsb[i / 8] >> (7 - (i % 8))) & 0x01;
//              printf("i:%d l:%d ", i, last);
                if(last == 0)
                        out=(out==0?1:0);
//              printf("o:%d\n", out);
                nrzi[i / 8] |= out << (7 - (i % 8));

                if(i % 8 == 7)
                        printf("%02X -> %02X\n", lsbmsb[i/8],nrzi[i/8]);
        }
}
$ ./enc
bitwise LSB convert ----------------------------------------
7E -> 7E
11 -> 88
22 -> 44
33 -> CC
1E -> 78
0A -> 50
7E -> 7E
00 -> 00
00 -> 00
00 -> 00
NRZ-I ----------------------------------------
7E -> FE
88 -> 5A
44 -> D2
CC -> 22
78 -> FA
50 -> CA
7E -> FE
00 -> AA
00 -> AA
00 -> AA

 

 

$ cat hdlc_nrzi_dec.c
#include <stdio.h>

void main()
{
        unsigned char data[10] =                {0xFE, 0x5A, 0xD2, 0x22, 0xFA, 0xCA, 0xFE, 0x00, 0x00, 0x00};
        unsigned char lsbmsb[10] =              {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        unsigned char bitstuff[10] =    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        unsigned char nrzi[10] =                {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

        int i = 0;
        int b = 0;

        // zrzi
        printf("NRZ-I ----------------------------------------\n");
        unsigned char last = 0;
        unsigned char curr = 0;
        unsigned char out = 0;
        for(i = 0; i < 80; i++)
        {
                curr = (data[i / 8] >> (7 - (i % 8))) & 0x01;
//              printf("i:%d l:%d c:%d ", i, last, curr);
                if(last != curr)
                        out = 0;
                else out = 1 ;
                last = curr;
//              printf("o:%d\n", out);
                nrzi[i / 8] |= out << (7 - (i % 8));

                if(i % 8 == 7)
                        printf("%02X -> %02X\n", data[i/8],nrzi[i/8]);
        }

        // msb to lsb, data order change
        printf("bitwise LSB convert ----------------------------------------\n");
        for(i = 0; i < 10; i++)
        {
                unsigned char tempval = 0;
                unsigned char orival = 0;
                orival = nrzi[i];
                for(b = 0; b < 8; b++)
                {
                        tempval |= ((orival & 0x01) << (7 - b));
                        orival = orival >> 1;
                }
                lsbmsb[i] = tempval;
                printf("%02X -> %02X\n",nrzi[i],lsbmsb[i]);
        }

}
$ ./dec
NRZ-I ----------------------------------------
FE -> 7E
5A -> 88
D2 -> 44
22 -> CC
FA -> 78
CA -> 50
FE -> 7E
00 -> FF
00 -> FF
00 -> FF
bitwise LSB convert ----------------------------------------
7E -> 7E
88 -> 11
44 -> 22
CC -> 33
78 -> 1E
50 -> 0A
7E -> 7E
FF -> FF
FF -> FF
FF -> FF

'모종의 음모 > HDLC' 카테고리의 다른 글

HDLC bit stuff  (0) 2022.08.29
hdlc 프로토콜 - bit stuffing, endian?  (0) 2022.08.26
HDLC FCS ccitt-1 16bit crc  (0) 2021.12.06
HDLC 프로토콜(High level Data Link Control)  (0) 2021.07.27
Posted by 구차니
모종의 음모/HDLC2022. 8. 26. 15:47

아.. 이런 변수가 하나 더 있었던 건가..

111111 가 전송되려면 1111101 이런식으로 전송되어야 한다.

(그런데.. 진짜 데이터가 111110 이면 어떡해야 하지? 어짜피 5개가 되었으니 1111100이 되는건가?)

Because a flag sequence consists of six consecutive 1-bits, other data is coded to ensure that it never contains more than five 1-bits in a row. This is done by bit stuffing: any time that five consecutive 1-bits appear in the transmitted data, the data is paused and a 0-bit is transmitted.

HDLC transmits bytes of data with the least significant bit first (not to be confused with little-endian order, which refers to byte ordering within a multi-byte field).

[링크 : https://en.wikipedia.org/wiki/High-Level_Data_Link_Control]

 

The NRZI coding scheme transmits a 0 bit as a signal transition, and a 1 bit as no change. In this case, bit stuffing is most easily described as the insertion of a 0 bit after a long run of 1 bits.

[링크 : https://en.wikipedia.org/wiki/Bit_stuffing]

 

[링크 : https://copycode.tistory.com/79]

[링크 : https://itwiki.kr/w/HDLC]

[링크 : https://goodgid.github.io/What-is-HDLC/]

 

+

 

 

0,1 눈대중으로 변환해서 연속된 0이나 1에 대한 오류가 존재하지만

전반적으로 비트가 뒤집힌 형태로 전송됨을 확인함. (바이트 내에서 LSB가 MSB에 존재함)

7E 11 22 33 1E 0A 7F
01111110 00010001 00100010 00110011 00011110 00001010 01111111 (raw)
01111110 10110100 10010110 10001000 10111110 10100110 01111111 (nrzi)

11111110 01011010 11010010 00100010 11111010 11001010 11111110 (수신데이터)
01111111 01011010 01001011 01000100 01011111 01010011 01111111 (수신데이터 바이트 단위 순서 변경)

 

'모종의 음모 > HDLC' 카테고리의 다른 글

HDLC bit stuff  (0) 2022.08.29
hdlc nrzi encoder / decoder (bit stuff 제외)  (0) 2022.08.29
HDLC FCS ccitt-1 16bit crc  (0) 2021.12.06
HDLC 프로토콜(High level Data Link Control)  (0) 2021.07.27
Posted by 구차니
모종의 음모/HDLC2021. 12. 6. 18:22
Posted by 구차니
모종의 음모/HDLC2021. 7. 27. 16:57

'모종의 음모 > HDLC' 카테고리의 다른 글

HDLC bit stuff  (0) 2022.08.29
hdlc nrzi encoder / decoder (bit stuff 제외)  (0) 2022.08.29
hdlc 프로토콜 - bit stuffing, endian?  (0) 2022.08.26
HDLC FCS ccitt-1 16bit crc  (0) 2021.12.06
Posted by 구차니