mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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