@@ -236,7 +236,7 @@ they should have the last argument be a callback function pointer.
236
236
returncode_t libtock_[name]_[desc](<arguments>, libtock_[name]_callback_[desc] cb);
237
237
```
238
238
239
- ### Example
239
+ ### Example:
240
240
241
241
242
242
For example, a library called "sensor" with a sensor read operation should look
@@ -277,23 +277,103 @@ returncode_t libtock_sensor_read(libtock_sensor_callback_reading cb) {
277
277
278
278
## Synchronous APIs
279
279
280
- Most system call interfaces will want to provide a synchronous API as well.
280
+ Most system call interfaces will want to provide a synchronous API as well. This
281
+ requires another file for syscalls.
282
+
283
+ All synchronous APIs MUST use the
284
+ [Yield-WaitFor](https://book.tockos.org/trd/trd104-syscalls.html#413-yield-waitfor)
285
+ (`yield_wait_for()`) variant of the yield syscall. This ensures predictable
286
+ behavior for `libtock-sync` users because Yield-WaitFor ensures that no other
287
+ application upcall will run until the synchronous API has finished.
288
+
289
+ ### Synchronous Syscall APIs
290
+
291
+ | Characteristic | Value |
292
+ |------------------|------------------------------------------------------|
293
+ | Location | `libtock-sync/[category]/syscalls` |
294
+ | Source File Name | `libtock-sync/[category]/syscalls/[name]_syscalls.c` |
295
+ | Header File Name | `libtock-sync/[category]/syscalls/[name]_syscalls.h` |
296
+
297
+ ### Synchronous Syscall Header File
298
+
299
+ The `[name]_syscalls.h` must be wrapped in `extern "C" { ... }` if the header
300
+ file is used in a C++ app.
301
+
302
+ #### Example:
303
+
304
+ ```c
305
+ #pragma once
306
+
307
+ #include <libtock/tock.h>
308
+ #include <libtock/[category]/syscalls/[name]_syscalls.h>
309
+
310
+ #ifdef __cplusplus
311
+ extern "C" {
312
+ #endif
313
+
314
+ // Signatures go here.
315
+
316
+ #ifdef __cplusplus
317
+ }
318
+ #endif
319
+ ```
320
+
321
+ ### Yield-WaitFor
322
+
323
+ Each supported upcall must have a matching Yield-WaitFor call. This function
324
+ calls ` yield_wait_for() ` and interprets the ` yield_waitfor_return_t ` return
325
+ value arguments.
326
+
327
+ The signature is:
328
+
329
+ ``` c
330
+ returncode_t libtocksync_[name]_yield_wait_for(<arguments>);
331
+ ```
332
+
333
+ If only one upcall is supported, the function name must be
334
+ ` libtocksync_[name]_yield_wait_for ` .
335
+
336
+ If more than one upcall is supported, the function names must start with
337
+ ` libtocksync_[name]_yield_wait_for_ ` followed by the same description of what
338
+ the upcall is used for from the ` libtock ` library.
339
+
340
+ The arguments must be appropriately named pointers to data types returned by the
341
+ upcall, except for any returned ReturnCode. The ReturnCode must be returned from
342
+ the function.
343
+
344
+ #### Example:
345
+
346
+ ``` c
347
+ returncode_t libtocksync_[name]_yield_wait_for(int * value) {
348
+ yield_waitfor_return_t ret;
349
+ ret = yield_wait_for(DRIVER_NUM_ [ NAME] , 0);
350
+ if (ret.data0 != RETURNCODE_SUCCESS) return ret.data0;
351
+
352
+ * value = (int) ret.data1;
353
+ return RETURNCODE_SUCCESS;
354
+ }
355
+ ```
356
+
357
+ ### Synchronous Operations
358
+
359
+ The asynchronous operations should have matching synchronous versions using
360
+ Yield-WaitFor internally.
281
361
282
362
| Characteristic | Value |
283
363
| ------------------| ------------------------------------|
284
364
| Location | ` libtock-sync/[category] ` |
285
365
| Source File Name | ` libtock-sync/[category]/[name].c ` |
286
366
| Header File Name | ` libtock-sync/[category]/[name].h ` |
287
367
288
- ### Header Files
368
+ ### Synchronous Operation Header Files
289
369
290
370
The libtock-sync ` [name].h ` header file must look like:
291
371
292
372
``` c
293
373
#pragma once
294
374
375
+ #include " syscalls/temperature_syscalls.h"
295
376
#include < libtock/tock.h>
296
- #include <libtock/[category]/[name].h>
297
377
298
378
#ifdef __cplusplus
299
379
extern "C" {
@@ -306,7 +386,7 @@ extern "C" {
306
386
#endif
307
387
```
308
388
309
- ### Synchronous APIs
389
+ ### Example:
310
390
311
391
For our sensor example:
312
392
@@ -337,17 +417,13 @@ operation:
337
417
338
418
``` c
339
419
returncode_t libtocksync_sensor_read (int* val) {
340
- int err;
341
- result.fired = false;
420
+ returncode_t err;
342
421
343
- err = libtock_sensor_read(sensor_cb );
422
+ err = libtock_sensor_command_read( );
344
423
if (err != RETURNCODE_SUCCESS) return err;
345
424
346
- // Wait for the callback.
347
- yield_for(&result.fired);
348
- if (result.result != RETURNCODE_SUCCESS) return result.result;
349
-
350
- * val = result.val;
351
- return RETURNCODE_SUCCESS;
425
+ // Wait for the operation to finish.
426
+ err = libtock_temperature_yield_wait_for(val);
427
+ return err;
352
428
}
353
429
```
0 commit comments