Einfaches mbed RPC Beispiel

Dependencies:   EthernetInterface HttpServer mbed-rtos mbed

Fork of RPCHTTPServerSimple by th.iotkit2.ch

Remote Procedure Call (RPC - „Aufruf einer fernen Prozedur“) ist eine Technik zur Realisierung von Interprozesskommunikation. Sie ermöglicht den Aufruf von Funktionen in anderen Adressräumen. Im Normalfall werden die aufgerufenen Funktionen auf einem anderen Computer als das aufrufende Programm ausgeführt. Es existieren viele Implementierungen dieser Technik, in der Regel sind sie untereinander nicht kompatibel

Die mbed Plattform beinhaltet eine RPC Library, welche ausgesuchte mbed Objekte (DigitalPin, DigitalOut etc.) mittels Serieller Schnittstelle oder via HTTP Aufrufen, zur Verfügung stellt.

Die mbed RCP Klassen verwenden C++ Templates, siehe Zeilen mit <Argument> und den Namespace RPC, siehe Zeilen mit RPC::.

Client

Committer:
stefan1691
Date:
Wed Apr 08 12:31:41 2015 +0000
Revision:
11:43e28c85fd75
Korrektur AnalogIn laut . https://developer.mbed.org/questions/3897/AnalogIn-not-working-in-rpc/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stefan1691 11:43e28c85fd75 1 /* mbed Microcontroller Library
stefan1691 11:43e28c85fd75 2 * Copyright (c) 2006-2013 ARM Limited
stefan1691 11:43e28c85fd75 3 *
stefan1691 11:43e28c85fd75 4 * Licensed under the Apache License, Version 2.0 (the "License");
stefan1691 11:43e28c85fd75 5 * you may not use this file except in compliance with the License.
stefan1691 11:43e28c85fd75 6 * You may obtain a copy of the License at
stefan1691 11:43e28c85fd75 7 *
stefan1691 11:43e28c85fd75 8 * http://www.apache.org/licenses/LICENSE-2.0
stefan1691 11:43e28c85fd75 9 *
stefan1691 11:43e28c85fd75 10 * Unless required by applicable law or agreed to in writing, software
stefan1691 11:43e28c85fd75 11 * distributed under the License is distributed on an "AS IS" BASIS,
stefan1691 11:43e28c85fd75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
stefan1691 11:43e28c85fd75 13 * See the License for the specific language governing permissions and
stefan1691 11:43e28c85fd75 14 * limitations under the License.
stefan1691 11:43e28c85fd75 15 */
stefan1691 11:43e28c85fd75 16 #include "Arguments.h"
stefan1691 11:43e28c85fd75 17 #include "pinmap.h"
stefan1691 11:43e28c85fd75 18
stefan1691 11:43e28c85fd75 19 using namespace std;
stefan1691 11:43e28c85fd75 20
stefan1691 11:43e28c85fd75 21 namespace mbed {
stefan1691 11:43e28c85fd75 22
stefan1691 11:43e28c85fd75 23 Arguments::Arguments(const char* rqs) {
stefan1691 11:43e28c85fd75 24 obj_name = NULL;
stefan1691 11:43e28c85fd75 25 method_name = NULL;
stefan1691 11:43e28c85fd75 26 argc = 0;
stefan1691 11:43e28c85fd75 27
stefan1691 11:43e28c85fd75 28 // This copy can be removed if we can assume the request string is
stefan1691 11:43e28c85fd75 29 // persistent and writable for the duration of the call
stefan1691 11:43e28c85fd75 30 strcpy(request, rqs);
stefan1691 11:43e28c85fd75 31
stefan1691 11:43e28c85fd75 32 // Initial '/'
stefan1691 11:43e28c85fd75 33 char* p = request;
stefan1691 11:43e28c85fd75 34 if (*p != '/') return;
stefan1691 11:43e28c85fd75 35 p++;
stefan1691 11:43e28c85fd75 36
stefan1691 11:43e28c85fd75 37 // Object Name
stefan1691 11:43e28c85fd75 38 p = search_arg(&obj_name, p, '/');
stefan1691 11:43e28c85fd75 39 if (p == NULL) return;
stefan1691 11:43e28c85fd75 40
stefan1691 11:43e28c85fd75 41 // Method Name
stefan1691 11:43e28c85fd75 42 p = search_arg(&method_name, p, ' ');
stefan1691 11:43e28c85fd75 43 if (p == NULL) return;
stefan1691 11:43e28c85fd75 44
stefan1691 11:43e28c85fd75 45 // Arguments
stefan1691 11:43e28c85fd75 46 while (true) {
stefan1691 11:43e28c85fd75 47 argv[argc] = NULL;
stefan1691 11:43e28c85fd75 48 p = search_arg(&argv[argc], p, ' ');
stefan1691 11:43e28c85fd75 49 if (argv[argc] != NULL) argc++;
stefan1691 11:43e28c85fd75 50 if (p == NULL) break;
stefan1691 11:43e28c85fd75 51 }
stefan1691 11:43e28c85fd75 52
stefan1691 11:43e28c85fd75 53 index = -1;
stefan1691 11:43e28c85fd75 54 }
stefan1691 11:43e28c85fd75 55
stefan1691 11:43e28c85fd75 56 char* Arguments::search_arg(char **arg, char *p, char next_sep) {
stefan1691 11:43e28c85fd75 57 char *s = p;
stefan1691 11:43e28c85fd75 58 while (true) {
stefan1691 11:43e28c85fd75 59 if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break;
stefan1691 11:43e28c85fd75 60 p++;
stefan1691 11:43e28c85fd75 61 }
stefan1691 11:43e28c85fd75 62 if (p == s) return NULL;
stefan1691 11:43e28c85fd75 63 *arg = s;
stefan1691 11:43e28c85fd75 64 char separator = *p;
stefan1691 11:43e28c85fd75 65 *p = '\0';
stefan1691 11:43e28c85fd75 66 p++;
stefan1691 11:43e28c85fd75 67 return (separator == next_sep) ? (p) : (NULL);
stefan1691 11:43e28c85fd75 68 }
stefan1691 11:43e28c85fd75 69
stefan1691 11:43e28c85fd75 70 template<> PinName Arguments::getArg<PinName>(void) {
stefan1691 11:43e28c85fd75 71 index++;
stefan1691 11:43e28c85fd75 72 return parse_pins(argv[index]);
stefan1691 11:43e28c85fd75 73 }
stefan1691 11:43e28c85fd75 74
stefan1691 11:43e28c85fd75 75 template<> int Arguments::getArg<int>(void) {
stefan1691 11:43e28c85fd75 76 index++;
stefan1691 11:43e28c85fd75 77 char *pEnd;
stefan1691 11:43e28c85fd75 78 return strtol(argv[index], &pEnd, 10);
stefan1691 11:43e28c85fd75 79 }
stefan1691 11:43e28c85fd75 80
stefan1691 11:43e28c85fd75 81 template<> const char* Arguments::getArg<const char*>(void) {
stefan1691 11:43e28c85fd75 82 index++;
stefan1691 11:43e28c85fd75 83 return argv[index];
stefan1691 11:43e28c85fd75 84 }
stefan1691 11:43e28c85fd75 85
stefan1691 11:43e28c85fd75 86 template<> char Arguments::getArg<char>(void) {
stefan1691 11:43e28c85fd75 87 index++;
stefan1691 11:43e28c85fd75 88 return *argv[index];
stefan1691 11:43e28c85fd75 89 }
stefan1691 11:43e28c85fd75 90
stefan1691 11:43e28c85fd75 91 template<> double Arguments::getArg<double>(void) {
stefan1691 11:43e28c85fd75 92 index++;
stefan1691 11:43e28c85fd75 93 return atof(argv[index]);
stefan1691 11:43e28c85fd75 94 }
stefan1691 11:43e28c85fd75 95
stefan1691 11:43e28c85fd75 96 template<> float Arguments::getArg<float>(void) {
stefan1691 11:43e28c85fd75 97 index++;
stefan1691 11:43e28c85fd75 98 return atof(argv[index]);
stefan1691 11:43e28c85fd75 99 }
stefan1691 11:43e28c85fd75 100
stefan1691 11:43e28c85fd75 101 Reply::Reply(char* r) {
stefan1691 11:43e28c85fd75 102 first = true;
stefan1691 11:43e28c85fd75 103 *r = '\0';
stefan1691 11:43e28c85fd75 104 reply = r;
stefan1691 11:43e28c85fd75 105 }
stefan1691 11:43e28c85fd75 106
stefan1691 11:43e28c85fd75 107 void Reply::separator(void) {
stefan1691 11:43e28c85fd75 108 if (first) {
stefan1691 11:43e28c85fd75 109 first = false;
stefan1691 11:43e28c85fd75 110 } else {
stefan1691 11:43e28c85fd75 111 *reply = ' '; reply++;
stefan1691 11:43e28c85fd75 112 }
stefan1691 11:43e28c85fd75 113 }
stefan1691 11:43e28c85fd75 114
stefan1691 11:43e28c85fd75 115 template<> void Reply::putData<const char*>(const char* s) {
stefan1691 11:43e28c85fd75 116 separator();
stefan1691 11:43e28c85fd75 117 reply += sprintf(reply, "%s", s);
stefan1691 11:43e28c85fd75 118 }
stefan1691 11:43e28c85fd75 119
stefan1691 11:43e28c85fd75 120 template<> void Reply::putData<char*>(char* s) {
stefan1691 11:43e28c85fd75 121 separator();
stefan1691 11:43e28c85fd75 122 reply += sprintf(reply, "%s", s);
stefan1691 11:43e28c85fd75 123 }
stefan1691 11:43e28c85fd75 124
stefan1691 11:43e28c85fd75 125 template<> void Reply::putData<char>(char c) {
stefan1691 11:43e28c85fd75 126 separator();
stefan1691 11:43e28c85fd75 127 reply += sprintf(reply, "%c", c);
stefan1691 11:43e28c85fd75 128 }
stefan1691 11:43e28c85fd75 129
stefan1691 11:43e28c85fd75 130 template<> void Reply::putData<int>(int v) {
stefan1691 11:43e28c85fd75 131 separator();
stefan1691 11:43e28c85fd75 132 reply += sprintf(reply, "%d", v);
stefan1691 11:43e28c85fd75 133 }
stefan1691 11:43e28c85fd75 134
stefan1691 11:43e28c85fd75 135 template<> void Reply::putData<float>(float f) {
stefan1691 11:43e28c85fd75 136 separator();
stefan1691 11:43e28c85fd75 137 reply += sprintf(reply, "%.17g", f);
stefan1691 11:43e28c85fd75 138 }
stefan1691 11:43e28c85fd75 139
stefan1691 11:43e28c85fd75 140 // put by me to accomodate read_u16() of AnalogIn
stefan1691 11:43e28c85fd75 141 template<> void Reply::putData<unsigned short>(unsigned short uint16) {
stefan1691 11:43e28c85fd75 142 separator();
stefan1691 11:43e28c85fd75 143 reply += sprintf(reply, "%u", uint16);
stefan1691 11:43e28c85fd75 144 }
stefan1691 11:43e28c85fd75 145 } // namespace mbed