<h4>The Problem</h4>
<p>Currently sensors and actuators are only auto-initialized when SAUL is used. This is not only inconvenient by itself, but also makes using both SAUL and the native driver API impossible. (One use case: Debugging via the <code>saul</code> shell command while the application uses the native interface for accessing additional device-specific features. Another use case: Different "apps" running as separate threads on the same device, where one app is portable and using SAUL, while the other needs to access device-specific features not available through SAUL.)</p>
<h5>The Issue in Detail</h5>
<p>E.g. in the auto init code of the <code>dht</code> driver the device descriptors are allocated here:</p>
<p><a href="https://github.com/RIOT-OS/RIOT/blob/6b094a78a943bcbd74d21a7ce83840c1fdcdc0a6/sys/auto_init/saul/auto_init_dht.c#L35-L38">https://github.com/RIOT-OS/RIOT/blob/6b094a78a943bcbd74d21a7ce83840c1fdcdc0a6/sys/auto_init/saul/auto_init_dht.c#L35-L38</a></p>
<p>With them being <code>static</code>, it is impossible (without resorting to nasty hacks) to access the auto initialized descriptors. And having two device descriptors referring to the same device is something that will cause bad things to happen.</p>
<p>Even if we would remove the <code>static</code> qualifier from all device descriptor arrays, using <code>&dht_devs[0]</code> is kind of ugly.</p>
<h3>Proposed Solution</h3>
<p>A sensor <code>foo</code> should provide to types:</p>
<div class="highlight highlight-source-c"><pre><span class="pl-k">typedef</span> <span class="pl-k">struct</span> {
     <span class="pl-c1">foo_params_t</span> params;
     <span class="pl-k">int</span> state;
     ....
} <span class="pl-c1">foo_dev_t</span>;

<span class="pl-k">typedef</span> <span class="pl-c1">uint8_t</span> <span class="pl-c1">foo_t</span>;</pre></div>
<p>In the auto-init code we would have</p>
<div class="highlight highlight-source-c"><pre><span class="pl-c1">foo_dev_t</span> foo_devs[N_FOO_DEVS];</pre></div>
<p>So <code>foo_dev_t</code> would contain the data/configuration/state of a device, and <code>foo_t</code> would be the index in <code>foo_devs</code>. The auto-init code must initialize all available sensors regardless of whether SAUL is used. If SAUL is used, it must additionally register the initialized sensors.</p>
<p>The API of foo should look than like this:</p>
<div class="highlight highlight-source-c"><pre><span class="pl-k">int</span> <span class="pl-en">foo_read</span>(<span class="pl-c1">foo_t</span> dev, <span class="pl-c1">uint16_t</span> *dest);
....</pre></div>
<p>This would also be more consistent with other RIOT APIs, e.g. <code>gpio_t</code>, <code>spi_t</code>, <code>i2c_t</code> etc. are also "simple types" that one can pass as value, while e.g. <code>bmx280_t</code> is something you really want to pass as reference. Also a POSIX file descriptor is an <code>int</code>, the newly proposed device descriptor would be much better match to POSIX descriptors than a potentially large <code>struct</code>.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/RIOT-OS/RIOT/issues/11826?email_source=notifications&email_token=ABE7WYGYQR73JRT2SHWAUWTP622PRA5CNFSM4IASCEL2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6QUSLQ">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABE7WYEOQT7BXPVNQDDOMSLP622PRANCNFSM4IASCELQ">mute the thread</a>.<img src="https://github.com/notifications/beacon/ABE7WYFBFVCY7RUTAYUYBBLP622PRA5CNFSM4IASCEL2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6QUSLQ.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/RIOT-OS/RIOT/issues/11826?email_source=notifications\u0026email_token=ABE7WYGYQR73JRT2SHWAUWTP622PRA5CNFSM4IASCEL2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6QUSLQ",
"url": "https://github.com/RIOT-OS/RIOT/issues/11826?email_source=notifications\u0026email_token=ABE7WYGYQR73JRT2SHWAUWTP622PRA5CNFSM4IASCEL2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6QUSLQ",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>