Skip to content

Commit 225d1db

Browse files
committed
added test application task
1 parent 099fe3b commit 225d1db

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

docs/tock_workshop/index.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,69 @@ pub unsafe fn main() {
245245
// ...
246246
}
247247
```
248+
249+
### Writing an application
250+
251+
To test the capsule we just created, we will need an application. This app will check if the driver is present in the kernel configuration, then it will issue a *"print"* command to the driver every second.
252+
253+
The simplest way to do this is to create a new entry in the `examples` directory, then import the `Makefile` from the `blink` example. We will also need to create an API for issuing commands to the capsule. This is usually done in a separate `.c` file, and further exposed in a header file to be used by application developers, but for the task at hand, defining them in `main.c` will work.
254+
255+
```c title="examples/ws-mock-test/main.c"
256+
#include <libtock/tock.h>
257+
258+
#define MOCK_DRIVER_NUM 0x9000A
259+
#define MOCK_EXIST 0x0
260+
#define MOCK_PRINT 0x1
261+
262+
int check_mock_driver_exists(void);
263+
int mock_print(void);
264+
265+
int check_mock_driver_exists(void) {
266+
syscall_return_t ret = command(MOCK_DRIVER_NUM, MOCK_EXIST, 0, 0);
267+
return tock_command_return_novalue_to_returncode(ret);
268+
}
269+
270+
int mock_print(void) {
271+
syscall_return_t ret = command(MOCK_DRIVER_NUM, MOCK_PRINT, 0, 0);
272+
return tock_command_return_novalue_to_returncode(ret);
273+
}
274+
275+
int main(void) {
276+
}
277+
```
278+
279+
:::note `tock.h` API
280+
The `syscall_return_t` type is the representation of the `CommandReturn` type seen in kernel. Tock supports returning either a success or error response to system calls, along with a payload of up to two `u32`s in size. The developer must be aware of the return type used by a respective command of a driver to correctly decode the response message. In our case, both commands used return no payload so the `tock_command_return_novalue_to_returncode`.
281+
:::
282+
283+
Now, we need to complete the implementation of the main function.
284+
285+
```c title="example/ws-mock-test/main.c"
286+
#include <libtock/tock.h>
287+
288+
#include <libtock-sync/services/alarm.h>
289+
290+
// Headers for `printf`
291+
#include <stdio.h>
292+
#include <stdlib.h>
293+
#include <string.h>
294+
#include <unistd.h>
295+
296+
// ...
297+
298+
int main(void) {
299+
printf("Mock capsule test\n");
300+
301+
int err = check_mock_driver_exists();
302+
if (err < 0) {
303+
printf("Mock capsule missing");
304+
return err;
305+
}
306+
307+
while (true) {
308+
mock_print();
309+
// This delay uses an underlying alarm in the kernel.
310+
libtocksync_alarm_delay_ms(1000);
311+
}
312+
}
313+
```

0 commit comments

Comments
 (0)