Hi Jacob,
On Tue, Feb 21, 2017 at 10:34:53AM -0700, Jacob Rosenthal wrote:
> Im trying to build a DIS service with identification strings like bsp and
> app name from config/id package with conf_get_value
>
> I think Im using the api correctly?
>
> char *val;
> int rc;
> uint16_t uuid16;
> char tmp_buf[32 + 1]; ///hwid is only one that needs some tmp buffer
>
> if(ctxt->op != BLE_GATT_ACCESS_OP_READ_CHR)
> {
> return BLE_ATT_ERR_UNLIKELY;
> }
> uuid16 = ble_uuid_u16(ctxt->chr->uuid);
> assert(uuid16 != 0);
>
> switch (uuid16) {
> case BLE_SVC_DIS_CHR_SYS_ID_UUID16:
> val = conf_get_value("id/hwid", tmp_buf, sizeof(tmp_buf));
> console_printf("hwid %s\n", val);
> rc = os_mbuf_append(ctxt->om, val, strlen(val));
> return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
>
> But I cant seem to get anything other than a empty string at runtime...
[...]
This is indeed a bummer. The problem is that the conf_get_value()
function expects the setting name to be a mutable string. String
literals (e.g., "id/hwid") are constant, and cause the setting lookup to
fail. The reason the name needs to be mutable is that the lookup
routine calls strtok_r on the string, which replaces instances of '/'
with \0.
Anyway, the immediate fix is to put the setting name in a char array,
rather than pass a string literal to conf_get_value(). For example:
char conf_name[] = "id/hwid";
val = conf_get_value(conf_name, tmp_buf, sizeof(tmp_buf));
In my view, this aspect of the conf API isn't particularly obvious. We
should either change the API such that it works with constant strings or
clearly document this requirement.
Chris