Skip to main content

发送大约20字节的特点in pxp reporter DA14681

1 year ago

发送大约20字节的特点in pxp reporter DA14681

Posted bymahmed10610 points 1 reply
0 upvotes

Hi dialog

I am working on custom board based in DA14681 and i am using pxp reporter as a base project and sending data over the BLE by making a custom service notifiable. So far everything is working great. Now i want to send more than 1 bytes, e.g i want to send 10 or 20 bytes on that characteristics but i am unable to do it.

Here is the service code but it is working for single byte of data transfer. How i can modify it to send 20 bytes or is there any example code that can help me in this regard. Thanks

typedef struct {
ble_service_t svc;

// handles
uint16_t templ_val_h;
uint16_t templ_ccc_h;
} mmtn_service_t;

static void handle_read_req(ble_service_t *svc, const ble_evt_gatts_read_req_t *evt)
{
mmtn_service_t *mmtn = (mmtn_service_t *) svc;

if (evt->handle == mmtn->templ_ccc_h)
{
uint16_t ccc = 0x0000;

ble_storage_get_u16(evt->conn_idx, mmtn->templ_ccc_h, &ccc);

// we're little-endian, ok to write directly from uint16_t
ble_gatts_read_cfm(evt->conn_idx, evt->handle, ATT_ERROR_OK, sizeof(ccc), &ccc);
} else
{
int8_t temp_level = 0;

/* Default alert level - 'No Alert' */
//pm_stay_alive();
temp_level = get_thermistor_temperature(TEMP_IN_C,0); // 0 -> TH4

//test_temp_counter++;
ble_gatts_read_cfm(evt->conn_idx, evt->handle, ATT_ERROR_OK, sizeof(temp_level ), &temp_level );
//pm_resume_sleep();
}
}

ble_service_t *mmtn_init(const ble_service_config_t *config, const mm_tempsense_info_t *info)
{
uint16_t num_descr;
uint16_t num_attr;
uint16_t cpf_h = 0;
mmtn_service_t *mmtn;
att_uuid_t uuid;
int8_t level = 0;

mmtn = OS_MALLOC(sizeof(*mmtn));
memset(mmtn, 0, sizeof(*mmtn));

mmtn->svc.connected_evt = handle_connected_evt;
mmtn->svc.read_req = handle_read_req;
mmtn->svc.write_req = handle_write_req;

// Content Presentation Format descriptor present if 'info' is set
num_descr = (info ? 2 : 1);
num_attr = ble_service_get_num_attr(config, 1, num_descr);

ble_uuid_from_string(UUID_MMTN, &uuid);
ble_gatts_add_service(&uuid, GATT_SERVICE_PRIMARY, num_attr);

ble_service_config_add_includes(config);

ble_uuid_from_string(UUID_MMTN_TEMPRATURE_LEVEL, &uuid);
ble_gatts_add_characteristic(&uuid, GATT_PROP_READ | GATT_PROP_NOTIFY,
ble_service_config_elevate_perm(ATT_PERM_READ, config),
1, 0, NULL, &mmtn->templ_val_h);

ble_uuid_create16(UUID_GATT_CLIENT_CHAR_CONFIGURATION, &uuid);
ble_gatts_add_descriptor(&uuid, ATT_PERM_RW, 1, 0, &mmtn->templ_ccc_h);

if (info) {
ble_uuid_create16(UUID_GATT_CHAR_PRESENTATION_FORMAT, &uuid);
ble_gatts_add_descriptor(&uuid,
ble_service_config_elevate_perm(ATT_PERM_READ, config),
7, 0, &cpf_h);
}

ble_gatts_register_service(&mmtn->svc.start_h, &mmtn->templ_val_h, &mmtn->templ_ccc_h, &cpf_h, 0);

/* Set initial value for battery level so we always have proper characteristic value set. */
ble_gatts_set_value(mmtn->templ_val_h, sizeof(

ble_service_t *mmtn_init(const ble_service_config_t *config, const mm_tempsense_info_t *info)
{
uint16_t num_descr;
uint16_t num_attr;
uint16_t cpf_h = 0;
mmtn_service_t *mmtn;
att_uuid_t uuid;
int8_t level = 0;

mmtn = OS_MALLOC(sizeof(*mmtn));
memset(mmtn, 0, sizeof(*mmtn));

mmtn->svc.connected_evt = handle_connected_evt;
mmtn->svc.read_req = handle_read_req;
mmtn->svc.write_req = handle_write_req;

// Content Presentation Format descriptor present if 'info' is set
num_descr = (info ? 2 : 1);
num_attr = ble_service_get_num_attr(config, 1, num_descr);

ble_uuid_from_string(UUID_MMTN, &uuid);
ble_gatts_add_service(&uuid, GATT_SERVICE_PRIMARY, num_attr);

ble_service_config_add_includes(config);

ble_uuid_from_string(UUID_MMTN_TEMPRATURE_LEVEL, &uuid);
ble_gatts_add_characteristic(&uuid, GATT_PROP_READ | GATT_PROP_NOTIFY,
ble_service_config_elevate_perm(ATT_PERM_READ, config),
1, 0, NULL, &mmtn->templ_val_h);

ble_uuid_create16(UUID_GATT_CLIENT_CHAR_CONFIGURATION, &uuid);
ble_gatts_add_descriptor(&uuid, ATT_PERM_RW, 1, 0, &mmtn->templ_ccc_h);

if (info) {
ble_uuid_create16(UUID_GATT_CHAR_PRESENTATION_FORMAT, &uuid);
ble_gatts_add_descriptor(&uuid,
ble_service_config_elevate_perm(ATT_PERM_READ, config),
7, 0, &cpf_h);
}

ble_gatts_register_service(&mmtn->svc.start_h, &mmtn->templ_val_h, &mmtn->templ_ccc_h, &cpf_h, 0);

/* Set initial value for battery level so we always have proper characteristic value set. */
ble_gatts_set_value(mmtn->templ_val_h, sizeof(temp_level ), &temp_level );

if (info) {
uint8_t cpf_val[7];
uint8_t *p = cpf_val;

put_u8_inc(&p, 0x04); // Format=unsigned 8-bit integer
put_u8_inc(&p, 0x00); // Exponent=0
put_u16_inc(&p, 0x27AD); // Unit=percentage
put_u8_inc(&p, info->namespace);
put_u16_inc(&p, info->descriptor);

// Content Presentation Format descriptor has static value
ble_gatts_set_value(cpf_h, sizeof(cpf_val), cpf_val);
}

mmtn->svc.end_h = mmtn->svc.start_h + num_attr;

return &mmtn->svc;
}

), &array);

if (info) {
uint8_t cpf_val[7];
uint8_t *p = cpf_val;

put_u8_inc(&p, 0x04); // Format=unsigned 8-bit integer
put_u8_inc(&p, 0x00); // Exponent=0
put_u16_inc(&p, 0x27AD); // Unit=percentage
put_u8_inc(&p, info->namespace);
put_u16_inc(&p, info->descriptor);

// Content Presentation Format descriptor has static value
ble_gatts_set_value(cpf_h, sizeof(cpf_val), cpf_val);
}

mmtn->svc.end_h = mmtn->svc.start_h + num_attr;

return &mmtn->svc;
}

static void notify_level(ble_service_t *svc, uint16_t conn_idx, int8_t level)
{
mmtn_service_t *mmtn = (mmtn_service_t *) svc;
uint16_t ccc = 0x0000;

ble_storage_get_u16(conn_idx, mmtn->templ_ccc_h, &ccc);

if (!(ccc & GATT_CCC_NOTIFICATIONS)) {
return;
}

ble_gatts_send_event(conn_idx, mmtn->templ_val_h, GATT_EVENT_NOTIFICATION, sizeof(level), &level);
}

accepted answer!

1 year ago

PM_Dialog

Hi mahmed106,

Thanks for your inputs. I got through your provided code snippet and I found that the maximum length of characteristic value is 1 byte. The max_len argument of your ble_gatts_add_characteristic() is set to 1, which means that maximum length is 1 byte. Can you please change it?

Thanks, PM_Dialog