Preliminary main mbed library for nexpaq development
libraries/tests/mbed/rpc/main.cpp@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 2 | #include "test_env.h" |
nexpaq | 0:6c56fb4bc5f0 | 3 | #include "mbed_rpc.h" |
nexpaq | 0:6c56fb4bc5f0 | 4 | |
nexpaq | 0:6c56fb4bc5f0 | 5 | void foo(Arguments *args, Reply *reply) { |
nexpaq | 0:6c56fb4bc5f0 | 6 | reply->putData<float>(args->getArg<float>() * 3.3); |
nexpaq | 0:6c56fb4bc5f0 | 7 | } |
nexpaq | 0:6c56fb4bc5f0 | 8 | |
nexpaq | 0:6c56fb4bc5f0 | 9 | bool rpc_test(const char *input, const char *expected) { |
nexpaq | 0:6c56fb4bc5f0 | 10 | char outbuf[RPC_MAX_STRING] = {0}; |
nexpaq | 0:6c56fb4bc5f0 | 11 | bool result = RPC::call(input, outbuf); |
nexpaq | 0:6c56fb4bc5f0 | 12 | printf("RPC: %s -> ", input); |
nexpaq | 0:6c56fb4bc5f0 | 13 | |
nexpaq | 0:6c56fb4bc5f0 | 14 | if (result == false) { |
nexpaq | 0:6c56fb4bc5f0 | 15 | printf("Procedure call ... [FAIL]\r\n"); |
nexpaq | 0:6c56fb4bc5f0 | 16 | } else if (strncmp(outbuf, expected, RPC_MAX_STRING) != 0) { |
nexpaq | 0:6c56fb4bc5f0 | 17 | printf("'%s' != '%s' ... [FAIL]\r\n", outbuf, expected); |
nexpaq | 0:6c56fb4bc5f0 | 18 | result = false; |
nexpaq | 0:6c56fb4bc5f0 | 19 | } else { |
nexpaq | 0:6c56fb4bc5f0 | 20 | printf("'%s' ... [OK]\r\n", outbuf); |
nexpaq | 0:6c56fb4bc5f0 | 21 | } |
nexpaq | 0:6c56fb4bc5f0 | 22 | return result; |
nexpaq | 0:6c56fb4bc5f0 | 23 | } |
nexpaq | 0:6c56fb4bc5f0 | 24 | |
nexpaq | 0:6c56fb4bc5f0 | 25 | #define RPC_TEST(INPUT,EXPECTED) result = result && rpc_test(INPUT,EXPECTED); if (result == false) { notify_completion(result); exit(1); } |
nexpaq | 0:6c56fb4bc5f0 | 26 | |
nexpaq | 0:6c56fb4bc5f0 | 27 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 28 | float f = 0; |
nexpaq | 0:6c56fb4bc5f0 | 29 | bool result = true; |
nexpaq | 0:6c56fb4bc5f0 | 30 | // Variable |
nexpaq | 0:6c56fb4bc5f0 | 31 | RPCVariable<float> rpc_f(&f, "f"); |
nexpaq | 0:6c56fb4bc5f0 | 32 | RPC_TEST("/f/write 1", ""); |
nexpaq | 0:6c56fb4bc5f0 | 33 | RPC_TEST("/f/read", "1"); |
nexpaq | 0:6c56fb4bc5f0 | 34 | |
nexpaq | 0:6c56fb4bc5f0 | 35 | // Function |
nexpaq | 0:6c56fb4bc5f0 | 36 | RPCFunction rpc_foo(&foo, "foo"); |
nexpaq | 0:6c56fb4bc5f0 | 37 | #if defined(TOOLCHAIN_ARM_MICRO) |
nexpaq | 0:6c56fb4bc5f0 | 38 | RPC_TEST("/foo/run 1", "3.299999952316284"); |
nexpaq | 0:6c56fb4bc5f0 | 39 | #else |
nexpaq | 0:6c56fb4bc5f0 | 40 | RPC_TEST("/foo/run 1", "3.2999999523162842"); |
nexpaq | 0:6c56fb4bc5f0 | 41 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 42 | |
nexpaq | 0:6c56fb4bc5f0 | 43 | // Class |
nexpaq | 0:6c56fb4bc5f0 | 44 | RPC::add_rpc_class<RpcDigitalOut>(); |
nexpaq | 0:6c56fb4bc5f0 | 45 | RPC_TEST("/DigitalOut/new LED2 led2", "led2"); |
nexpaq | 0:6c56fb4bc5f0 | 46 | RPC_TEST("/led2/write 1", ""); |
nexpaq | 0:6c56fb4bc5f0 | 47 | RPC_TEST("/led2/read", "1"); |
nexpaq | 0:6c56fb4bc5f0 | 48 | |
nexpaq | 0:6c56fb4bc5f0 | 49 | // Instance |
nexpaq | 0:6c56fb4bc5f0 | 50 | RpcDigitalOut rpc_led(LED1, "led1"); |
nexpaq | 0:6c56fb4bc5f0 | 51 | RPC_TEST("/led1/write 1", ""); |
nexpaq | 0:6c56fb4bc5f0 | 52 | RPC_TEST("/led1/read", "1"); |
nexpaq | 0:6c56fb4bc5f0 | 53 | |
nexpaq | 0:6c56fb4bc5f0 | 54 | // Introspection |
nexpaq | 0:6c56fb4bc5f0 | 55 | RPC_TEST("/", "led1 led2 foo f DigitalOut RPC"); |
nexpaq | 0:6c56fb4bc5f0 | 56 | RPC_TEST("/f", "read write delete"); |
nexpaq | 0:6c56fb4bc5f0 | 57 | RPC_TEST("/foo", "run delete"); |
nexpaq | 0:6c56fb4bc5f0 | 58 | RPC_TEST("/DigitalOut", "new"); |
nexpaq | 0:6c56fb4bc5f0 | 59 | RPC_TEST("/led1", "write read delete"); |
nexpaq | 0:6c56fb4bc5f0 | 60 | |
nexpaq | 0:6c56fb4bc5f0 | 61 | // Delete instance |
nexpaq | 0:6c56fb4bc5f0 | 62 | RPC_TEST("/led2/delete", ""); |
nexpaq | 0:6c56fb4bc5f0 | 63 | RPC_TEST("/", "led1 foo f DigitalOut RPC"); |
nexpaq | 0:6c56fb4bc5f0 | 64 | |
nexpaq | 0:6c56fb4bc5f0 | 65 | notify_completion(result); |
nexpaq | 0:6c56fb4bc5f0 | 66 | return 0; |
nexpaq | 0:6c56fb4bc5f0 | 67 | } |