Re: [RFC] Device: device_get_binding() returns NULL if device fails initialization


Davidoaia, Bogdan M <bogdan.m.davidoaia@...>
 

Hi Daniel,

I didn't catch this at first, but the patch causes a problem for the
Grove LCD driver (not sure if this problem is also present for other
drivers).

The way it was written, the Grove LCD driver doesn't use the driver_api
pointer at all (there is no LCD API). As such, it is NULL by default and
the call to device_get_binding will return NULL even though the device
was correctly initialized.

Bogdan

On Ma, 2016-04-05 at 16:36 -0700, Daniel Leung wrote:
Problem Statement:
Currently, there is no way to know if a driver fails initialization.

Problem Description:
Zephyr currently does not provide a way to check programmatically
whether a device can be used. The device_get_binding() always
returns a valid device struct (if such device driver instance
exists). This causes a bit of headache when debugging apps as
the developer assumes the devices are ready to be used.

Solution:
The solution was actually proposed in [1] a while ago. The idea
is to piggy-back onto driver_api pointer. If the pointer is NULL,
the device driver has failed in its own initialization function.
The driver_api is set to NULL (or in other words, never set to
the actual API struct). When device_get_binding() sess that
the driver_api is NULL for a particular device struct, it returns
NULL to the caller, and thus preventing its use in the app.
Since this is a binary state, instead of creating another variable
inside device struct, this uses driver_api to avoid unnecessarily
enlarging the ROM size. Since this is a simple change, a patch
has been created in [2].

[1] https://lists.zephyrproject.org/archives/list/devel(a)lists.zephyrproject.org/message/MZB5PYBSRHV3NIEHJYXYQVLTPFIIHPB3/
[2] https://gerrit.zephyrproject.org/r/1261


----------
Daniel Leung

Join devel@lists.zephyrproject.org to automatically receive all group messages.