|
1 | 1 | #include "mbed.h"
|
| 2 | +#include "test_env.h" |
2 | 3 | #include "mbed_rpc.h"
|
3 | 4 |
|
| 5 | +char outbuf[RPC_MAX_STRING] = {0}; |
4 | 6 | float f = 0;
|
5 | 7 |
|
6 | 8 | void foo(Arguments *args, Reply *reply) {
|
7 | 9 | reply->putData<float>(args->getArg<float>() * 3.3);
|
8 | 10 | }
|
9 | 11 |
|
10 |
| -char outbuf[RPC_MAX_STRING]; |
11 |
| -void test(const char *input, const char *expected) { |
12 |
| - printf("> %s\n", input); |
| 12 | +bool rpc_test(const char *input, const char *expected) { |
| 13 | + printf("RPC: %s -> ", input); |
13 | 14 | bool result = RPC::call(input, outbuf);
|
14 |
| - |
15 |
| - if (!result) { |
16 |
| - printf("[ERROR] The remote procedure call failed\n"); |
17 |
| - } else if (strcmp(outbuf, expected) != 0) { |
18 |
| - printf("[ERROR] \"%s\" != \"%s\"\n", outbuf, expected); |
| 15 | + |
| 16 | + if (result == false) { |
| 17 | + printf("Procedure call ... [FAIL]\r\n"); |
| 18 | + } else if (strncmp(outbuf, expected, RPC_MAX_STRING) != 0) { |
| 19 | + printf("'%s' != '%s' ... [FAIL]\r\n", outbuf, expected); |
| 20 | + result = false; |
19 | 21 | } else {
|
20 |
| - printf("{%s}\n", outbuf); |
| 22 | + printf("'%s' ... [OK]\r\n", outbuf); |
21 | 23 | }
|
| 24 | + return result; |
22 | 25 | }
|
23 | 26 |
|
| 27 | +#define RPC_TEST(INPUT,EXPECTED) result = result && rpc_test(INPUT,EXPECTED); if (result == false) { notify_completion(result); exit(1); } |
| 28 | + |
24 | 29 | int main() {
|
| 30 | + bool result = true; |
25 | 31 | // Variable
|
26 | 32 | RPCVariable<float> rpc_f(&f, "f");
|
27 |
| - test("/f/write 1", ""); |
28 |
| - test("/f/read", "1"); |
29 |
| - |
| 33 | + RPC_TEST("/f/write 1", ""); |
| 34 | + RPC_TEST("/f/read", "1"); |
| 35 | + |
30 | 36 | // Function
|
31 | 37 | RPCFunction rpc_foo(&foo, "foo");
|
32 |
| - test("/foo/run 1", "3.2999999523162842"); |
33 |
| - |
| 38 | + RPC_TEST("/foo/run 1", "3.2999999523162842"); |
| 39 | + |
34 | 40 | // Class
|
35 | 41 | RPC::add_rpc_class<RpcDigitalOut>();
|
36 |
| - test("/DigitalOut/new LED2 led2", "led2"); |
37 |
| - test("/led2/write 1", ""); |
38 |
| - test("/led2/read", "1"); |
39 |
| - |
| 42 | + RPC_TEST("/DigitalOut/new LED2 led2", "led2"); |
| 43 | + RPC_TEST("/led2/write 1", ""); |
| 44 | + RPC_TEST("/led2/read", "1"); |
| 45 | + |
40 | 46 | // Instance
|
41 | 47 | RpcDigitalOut rpc_led(LED1, "led1");
|
42 |
| - test("/led1/write 1", ""); |
43 |
| - test("/led1/read", "1"); |
44 |
| - |
| 48 | + RPC_TEST("/led1/write 1", ""); |
| 49 | + RPC_TEST("/led1/read", "1"); |
| 50 | + |
45 | 51 | // Introspection
|
46 |
| - test("/", "led1 led2 foo f DigitalOut RPC"); |
47 |
| - test("/f", "read write delete"); |
48 |
| - test("/foo", "run delete"); |
49 |
| - test("/DigitalOut", "new"); |
50 |
| - test("/led1", "write read delete"); |
51 |
| - |
| 52 | + RPC_TEST("/", "led1 led2 foo f DigitalOut RPC"); |
| 53 | + RPC_TEST("/f", "read write delete"); |
| 54 | + RPC_TEST("/foo", "run delete"); |
| 55 | + RPC_TEST("/DigitalOut", "new"); |
| 56 | + RPC_TEST("/led1", "write read delete"); |
| 57 | + |
52 | 58 | // Delete instance
|
53 |
| - test("/led2/delete", ""); |
54 |
| - test("/", "led1 foo f DigitalOut RPC"); |
55 |
| - |
56 |
| - while (true) { |
57 |
| - wait(1); |
58 |
| - } |
| 59 | + RPC_TEST("/led2/delete", ""); |
| 60 | + RPC_TEST("/", "led1 foo f DigitalOut RPC"); |
| 61 | + |
| 62 | + notify_completion(result); |
| 63 | + return 0; |
59 | 64 | }
|
0 commit comments