AMB21 bt_mesh device Sensor_server Model

我正在使用 Ameba-D 的 Standard SDK 中的 bt_mesh device 範例。我想在此範例中實現 Sensor_server Model,傳送感測器數據到手機APP。但範例中沒有直接創建 sensor data 的部分。目前嘗試在 sensor_server_app.c 要初始化model時使用 sensor_server_set_db 和修改 sensor_setup_server_data,但是都沒有成功。不確定改動的方向有沒有錯,想請教若要達到上述的功能我要如何實現?

uint8_t temperature_value_1 = 32;
...
static int32_t sensor_server_data(const mesh_model_info_p pmodel_info, uint32_t type, void *pargs)

{
    (void)pmodel_info;

    switch (type)

    {

    case SENSOR_SERVER_GET:

    {

        sensor_server_get_t *p_get_data = NULL;

        p_get_data = (sensor_server_get_t *)pargs;

  

        if (p_get_data)

        {

            if(p_get_data->property_id == 0x56)

            {

                sensor_p.property_id = 0x56;

                sensor_p.raw_data = &temperature_value_1;

            }

            else

            {

                sensor_p.property_id = p_get_data->property_id;

                p_get_data->raw_data = sensor_p.raw_data;

            }

        }

        printf("sensor server receive: property id: %d , raw data %d\r\n",

               p_get_data->property_id,
               p_get_data->raw_data);

    }

    break;
 ...
}

...

void sensor_server_models_init(void)

{
    sensor_db_t sensors_data;
    sensors_data.descriptor.property_id = 0x56;
    sensors_data.sensor_raw_data_len = sizeof(uint8_t);

    printf("\r\ngeneric_sensor_server_model_init\r\n");

    sensor_server_model.model_data_cb = sensor_server_data;

    sensor_server_reg(0, &sensor_server_model);

    sensor_server_set_db(&sensor_server_model, &sensors_data, 1);

...
}

我目前將程式做以下修改成功在手機上讀到數值,但是現在出現新的問題是當從手機設定 sensor server 的 publication 時 amb21會發生錯誤,目前還在研究發生錯誤的原因。

/*sensor_server_app.c*/
uint8_t temperature_value_1 = 50; // 溫度值 property id  0x56 解析度 0.5 , 50 = 25 ℃
...
static int32_t sensor_server_data(const mesh_model_info_p pmodel_info, uint32_t type, void *pargs)
{
  ...
  switch (p_get_data->property_id)
            {
            case 0x56:  
                p_get_data->raw_data = &temperature_value_1;
                break;
            default:
                p_get_data->raw_data = sensor_p.raw_data;
                break;
            }
...
}
void sensor_server_models_init(void)
{
// 初始化 sensor_db_t 結構
    sensor_db_t *pinfo = (sensor_db_t *)malloc(sizeof(sensor_db_t) * 3);
    if (pinfo == NULL)
    {
        return;
    }
    pinfo[0].descriptor.property_id = 0x56;
    pinfo[0].sensor_raw_data_len = sizeof(temperature_value_1);
   // sensor_server_model.pargs = pinfo;
    sensor_server_model.model_data_cb = sensor_server_data;
    sensor_server_reg(0, &sensor_server_model);
    sensor_server_set_db(&sensor_server_model, pinfo, 1);
...
}

錯誤訊息如下:

Hard Fault Patch (Secure)

Security Fault: 
Secure State: 1
Stacked: 
R0 = 0x00000000
R1 = 0x48012000
R2 = 0x0000000d
R3 = 0xb2b3f211
R12 = 0xeffb081c
LR = 0x0e019ba5
PC = 0x0e019a40
PSR = 0x21000000
Current: 
EXC_RETURN = 0xffffffa9
MSP = 0x1004f7f0
PSP = 0x1004f7f0
xPSR = 0xa0000007
CFSR  = 0x00000000
HFSR  = 0x00000000
DFSR  = 0x00000000
MMFAR = 0x00000000
BFAR  = 0x00000000
AFSR  = 0x00000000
PriMask = 0x00000000
SVC priority: 0x00
PendSVC priority: 0x00
Systick priority: 0x00
MSP_NS = 0x10004fd8
PSP_NS = 0x1004f7f0
CFSR_NS  = 0x00000000
HFSR_NS  = 0x00000000
DFSR_NS  = 0x00000000
MMFAR_NS = 0x00000000
BFAR_NS  = 0x00000000
AFSR_NS  = 0x00000000
SVC priority NS: 0x00

PendSVC priority NS: 0xe0

Systick priority NS: 0xe0

我找到原因是出在缺少cadence 的初始化,現在已經可以正常作動。

void sensor_server_models_init(void)
{

    //初始化 sensor_db_t 結構
    sensor_db_t *pinfo = (sensor_db_t *)malloc(sizeof(sensor_db_t));
    if (pinfo == NULL)
    {
        printf("Memory allocation for sensor_db_t failed!\n");
        return -1;
    }
    pinfo->descriptor.property_id = 0x56;
    pinfo->sensor_raw_data_len = sizeof(temperature_value_1);
     //動態分配 sensor_cadence_t 結構
    pinfo->cadence = (sensor_cadence_t *)malloc(sizeof(sensor_cadence_t));
    if (pinfo->cadence == NULL)
    {
        printf("Memory allocation for sensor_cadence_t failed!\n");
        free(pinfo); // 释放 pinfo 结构
        return -1;
    }
    *pinfo->cadence = (sensor_cadence_t){0, 0, 0, NULL, NULL, 5, NULL, NULL};
...
}
1 Like