C语言 LoRaWAN的碎片数据块传输实施存在问题

blpfk2vs  于 2023-02-21  发布在  其他
关注(0)|答案(1)|浏览(117)

我正在阅读LoRaWAN Fragmented Data Block Transport文档,我试图理解matrix_line函数背后的逻辑和文档中的示例(第23页及更多)。
为此,我将函数从Matlab(第31页)移植到C:

static bool is_power2(uint32_t Number)
{
    return (Number != 0) && ((Number & (Number - 1)) == 0);
}

static uint32_t prbs23(uint32_t x)
{
    uint32_t b0;
    uint32_t b1;

    b0 = (x & 0x00000001);
    b1 = (x & 0x00000020) >> 5;

    return (x >> 1) + ((b0 ^ b1) << 22);
}

static void matrix_line(uint8_t* p_Buf, int N, int M)
{
    uint32_t m;
    uint32_t x;
    uint32_t nb_coeff;
    uint32_t r;

    if(N <= M)
    {
        p_Buf[N] = 1;

        return;
    }

    m = 0;
    if(is_power2(M))
    {
        m = 1;
    }

    x = 1 + (1001 * N);

    nb_coeff = 0;
    while(nb_coeff < (M/2))
    {
        r = (1 << 16);

        while(r >= M)
        {
            x = prbs23(x);
            r = x % (M + m);
        }

        if(p_Buf[r + 1] == 0)
        {
            p_Buf[r + 1] = 1;
            nb_coeff += 1;
        }
    }
}

void main(void)
{
    uint8_t Buf[26];
    uint8_t M;
    uint8_t N;

    N = 26;
    M = 52;

    for(uint32_t i = 0; i < M; i++)
    {
        memset(Buf, 0, sizeof(Buf));

        matrix_line(Buf, i, N);

        for(uint32_t j = 0; j < N; j++)
        {
            printf("%u", Buf[j]);
        }

        printf("\n\r");
    }

    return 0;
}

我尝试使用示例规范N = 26M = 52从文档中获取图像

但我的输出如下所示:

01000000000000000000000000
00100000000000000000000000
00010000000000000000000000
00001000000000000000000000
00000100000000000000000000
00000010000000000000000000
00000001000000000000000000
00000000100000000000000000
00000000010000000000000000
00000000001000000000000000
00000000000100000000000000
00000000000010000000000000
00000000000001000000000000
00000000000000100000000000
00000000000000010000000000
00000000000000001000000000
00000000000000000100000000
00000000000000000010000000
00000000000000000001000000
00000000000000000000100000
00000000000000000000010000
00000000000000000000001000
00000000000000000000000100
00000000000000000000000010
00000000000000000000000001
00000000000000000000000000 <---
01010001101100110100011011
01111100010011010000011011
00000000110011001111011111
00101100001111001001010111
01111100100100100001001111
01110110001100000001011111
01010101110001101100011001
01101101010100101001010110
00111100110011100100010101
00010100011110011001110011
00001111000101001111110001
00110110001101001101001110
00010001000101011101111101
01101111011101001000100010
01010100111100110111010000
00001001111100110111011000
00110111000111011101000100
01000110111101110000100101
01101000100110101110011010
01010010101110011000011011
00110010111000000111110110
01011110100111011000010010
01111001101110100001100010
00001101011111011001010010
00000100111010011111010101
01001100100111101000110101

从标记行开始的所有行都不正确。空行是从哪里来的?为什么空行之后的每一行都向右滚动一行,即:

01010001101100110100011011

应该是

10100011011001101000110110
k10s72fa

k10s72fa1#

我凭直觉将代码更改为:

uint8_t Buf[1 + 26];
    uint8_t M = 52;
    uint8_t N = 26;

    for(uint32_t i = 1; i <= M; i++) // 1 <= i <= M
    {
        memset(Buf, 0, sizeof(Buf));

        matrix_line(Buf, i, N);

        for(uint32_t j = 1; j <= N; j++) // 1 <= j <= N
        {
            printf("%u", Buf[j]);
        }

        printf("\n");
    }

这似乎产生了预期的结果。
这是一个小问题,但是函数将无符号值作为有符号值接收......这应该得到解决。

相关问题