0

我正在尝试更改 iBeacon 的广告,在主要和次要字段中发送时间戳。在示例代码中,Major 和 Minor 是 uint8_t,因此为了获取日期,例如我使用以下代码:

uint8_t getConcatTimestamp(){
   time_t rawtime;
   struct tm * timeinfo;
     char *bufferyear;
     char *buffermon;
     char *bufferday;
     char *bufferhour;
     char *buffermin;
     uint8_t segundos=0x10;
   time ( &rawtime );
   timeinfo = localtime ( &rawtime );
   bufferyear = (char *) malloc( sizeof(char) *4);
     buffermon = (char *) malloc( sizeof(char) *4);
     bufferday = (char *) malloc( sizeof(char) *4);
     bufferhour = (char *) malloc( sizeof(char) *4);
     buffermin = (char *) malloc( sizeof(char) *4);

   strftime (bufferyear,4,"%y",timeinfo);
     strftime (buffermon,4,"%m",timeinfo);
   strftime (bufferday,4,"%d",timeinfo);
   strftime (bufferhour,4,"%H",timeinfo);
   strftime (buffermin,4,"%M",timeinfo);
     segundos=(uint8_t)(timeinfo->tm_sec);
    //segundos=0x10;
 //return buffer;
    return(segundos);

}

尽管使用 segundos 作为 0x10 它不起作用,我不知道为什么。但是这个想法是使用例如 segundos=(uint8_t)(timeinfo->tm_sec); 因为我假装提取每个 16 位数据(0x00,2 个十六进制数字)以在主要和次要中使用它们。我尝试使用 char * 缓冲区使用 char 字符串,但我发现以后很难将数据从 char 传递到 uint8_t,也许更容易使用可以从以下提供的结构中获得的整数:

struct tm {
   int tm_sec;         /* seconds,  range 0 to 59          */
   int tm_min;         /* minutes, range 0 to 59           */
   int tm_hour;        /* hours, range 0 to 23             */
   int tm_mday;        /* day of the month, range 1 to 31  */
   int tm_mon;         /* month, range 0 to 11             */
   int tm_year;        /* The number of years since 1900   */
   int tm_wday;        /* day of the week, range 0 to 6    */
   int tm_yday;        /* day in the year, range 0 to 365  */
   int tm_isdst;       /* daylight saving time             */   
};

在主类中用 iBeacon 做广告的无限可能如下:

// Enter main loop.
    for (;;)
    {
            seconds=getConcatTimestamp();
                if(cont<2)
                {   

                    flash_db_t tmp;
                    tmp.data.major_value[0] = clbeacon_info[18] = 0x00;
        tmp.data.major_value[1] = clbeacon_info[19] = 0x01;
        tmp.data.minor_value[0] = clbeacon_info[20] = 0x00;
                    if(cont==0)
                    {
                //tmp.data.minor_value[1] = clbeacon_info[21] = aesstruct.key[0];
                        tmp.data.minor_value[1] = clbeacon_info[21] = seconds;
                    }
                    else
                    {
                    tmp.data.minor_value[1] = clbeacon_info[21] = aesstruct.key[1];
                    }
                    err_code = pstorage_clear(&pstorage_block_id, sizeof(flash_db_t));
        APP_ERROR_CHECK(err_code);

        err_code = pstorage_store(&pstorage_block_id, (uint8_t *)&tmp, sizeof(flash_db_t), 0);
        APP_ERROR_CHECK(err_code);

        err_code = pstorage_access_wait();
        APP_ERROR_CHECK(err_code);
                    advertising_init(beacon_mode_normal);
        s_leds_bit_mask |= BEACON_MODE_LED_MSK;
                    //Solo esto estaba dentro del for(;;)
        app_sched_execute();
        power_manage();







                    cont++;

                }
                else
                    cont=0;
            }
}

是的,我知道 Major 和 Minor 是 16 位的,但是由于构建了这个 iBeacon 固件,它使用了一个具有 2 个字段的 uint8_t 数组,两者都有 16 位。这实际上或多或少是我使用的数据结构:

typedef struct
{
    uint8_t  magic_byte;
    uint8_t  beacon_uuid[16];    
    uint8_t  major_value[2];
    uint8_t  minor_value[2];
    uint8_t  measured_rssi;
}flash_db_layout_t;

Si 这就是我需要数据 en uint8_t 的原因。我想发送 128 位,4 个完整的数据包,带有 Minor 和 Major(32*4=128bits 用 AES 加密)。但首先我需要发送时间戳才能进行测试。那么,是否有任何示例或片段可以知道如何将它们转换为字节?但我最终不需要 uint8_t 吗?因为当我上传固件时设备无法工作,我猜这一定是因为数据类型错误

4

0 回答 0