MMEx with SPI Slave to allow legacy devices to communicate with modern media such as USB, SD cards, the internet and all of the mbed\'s other interfaces
Dependencies: NetServices MSCUsbHost mbed TMP102 SDFileSystem
rfuncs.cpp@0:67a55a82ce06, 2011-02-27 (annotated)
- Committer:
- DeMein
- Date:
- Sun Feb 27 18:54:40 2011 +0000
- Revision:
- 0:67a55a82ce06
Version as submitted to the NXP Design Challenge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DeMein | 0:67a55a82ce06 | 1 | /* MMEx for MBED - RPC Command processing |
DeMein | 0:67a55a82ce06 | 2 | * Copyright (c) 2011 MK |
DeMein | 0:67a55a82ce06 | 3 | * |
DeMein | 0:67a55a82ce06 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
DeMein | 0:67a55a82ce06 | 5 | * of this software and associated documentation files (the "Software"), to deal |
DeMein | 0:67a55a82ce06 | 6 | * in the Software without restriction, including without limitation the rights |
DeMein | 0:67a55a82ce06 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
DeMein | 0:67a55a82ce06 | 8 | * copies of the Software, and to permit persons to whom the Software is |
DeMein | 0:67a55a82ce06 | 9 | * furnished to do so, subject to the following conditions: |
DeMein | 0:67a55a82ce06 | 10 | * |
DeMein | 0:67a55a82ce06 | 11 | * The above copyright notice and this permission notice shall be included in |
DeMein | 0:67a55a82ce06 | 12 | * all copies or substantial portions of the Software. |
DeMein | 0:67a55a82ce06 | 13 | * |
DeMein | 0:67a55a82ce06 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
DeMein | 0:67a55a82ce06 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
DeMein | 0:67a55a82ce06 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
DeMein | 0:67a55a82ce06 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
DeMein | 0:67a55a82ce06 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
DeMein | 0:67a55a82ce06 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
DeMein | 0:67a55a82ce06 | 20 | * THE SOFTWARE. |
DeMein | 0:67a55a82ce06 | 21 | */ |
DeMein | 0:67a55a82ce06 | 22 | |
DeMein | 0:67a55a82ce06 | 23 | /** |
DeMein | 0:67a55a82ce06 | 24 | \file rfuncs.cpp |
DeMein | 0:67a55a82ce06 | 25 | \brief Commands starting with R or / for processing RPC |
DeMein | 0:67a55a82ce06 | 26 | */ |
DeMein | 0:67a55a82ce06 | 27 | |
DeMein | 0:67a55a82ce06 | 28 | #include "rfuncs.h" |
DeMein | 0:67a55a82ce06 | 29 | |
DeMein | 0:67a55a82ce06 | 30 | /** main entry for parsing R- or /-commands |
DeMein | 0:67a55a82ce06 | 31 | * |
DeMein | 0:67a55a82ce06 | 32 | */ |
DeMein | 0:67a55a82ce06 | 33 | void parse_R() { |
DeMein | 0:67a55a82ce06 | 34 | DBG_msg("parse_R", inbuf); |
DeMein | 0:67a55a82ce06 | 35 | err_num = noerror; |
DeMein | 0:67a55a82ce06 | 36 | |
DeMein | 0:67a55a82ce06 | 37 | if (inbuf[1] != NULL) { |
DeMein | 0:67a55a82ce06 | 38 | switch (inbuf[1]) { |
DeMein | 0:67a55a82ce06 | 39 | case rslash : do_rslash(); |
DeMein | 0:67a55a82ce06 | 40 | break; |
DeMein | 0:67a55a82ce06 | 41 | case rresult : do_rresult(); |
DeMein | 0:67a55a82ce06 | 42 | break; |
DeMein | 0:67a55a82ce06 | 43 | case rxeq : do_rxeq(); |
DeMein | 0:67a55a82ce06 | 44 | break; |
DeMein | 0:67a55a82ce06 | 45 | default : do_rdefault(); // command not recognized |
DeMein | 0:67a55a82ce06 | 46 | break; |
DeMein | 0:67a55a82ce06 | 47 | } |
DeMein | 0:67a55a82ce06 | 48 | } else { do_rdefault(); } |
DeMein | 0:67a55a82ce06 | 49 | } |
DeMein | 0:67a55a82ce06 | 50 | |
DeMein | 0:67a55a82ce06 | 51 | /** Define the value of the PC command string |
DeMein | 0:67a55a82ce06 | 52 | * |
DeMein | 0:67a55a82ce06 | 53 | * syntax: R/[string] define new RPX command (same as //) |
DeMein | 0:67a55a82ce06 | 54 | * \n or: R/>+[string] appends to existing RPC string |
DeMein | 0:67a55a82ce06 | 55 | * |
DeMein | 0:67a55a82ce06 | 56 | */ |
DeMein | 0:67a55a82ce06 | 57 | void do_rslash() { |
DeMein | 0:67a55a82ce06 | 58 | DBG_msg("do_rslash", inbuf); |
DeMein | 0:67a55a82ce06 | 59 | |
DeMein | 0:67a55a82ce06 | 60 | while (inbuf[2] == c_space) remchar(inbuf, 2, 1); // remove leading space |
DeMein | 0:67a55a82ce06 | 61 | remchar(inbuf, 0, 1); // remove the R of 1st slash |
DeMein | 0:67a55a82ce06 | 62 | |
DeMein | 0:67a55a82ce06 | 63 | // now retrieve string from the command line, i starts at 3 |
DeMein | 0:67a55a82ce06 | 64 | |
DeMein | 0:67a55a82ce06 | 65 | // if first chars are not ">+" the string will be new |
DeMein | 0:67a55a82ce06 | 66 | if ((inbuf[3] == c_escape) && (inbuf[4] == c_append)) { |
DeMein | 0:67a55a82ce06 | 67 | // string is append, >+ from inbuf |
DeMein | 0:67a55a82ce06 | 68 | remchar(inbuf, 1, 2); |
DeMein | 0:67a55a82ce06 | 69 | DBG_msg("append RPC cmd", inbuf); |
DeMein | 0:67a55a82ce06 | 70 | param[par_R_].append(inbuf); |
DeMein | 0:67a55a82ce06 | 71 | } else { |
DeMein | 0:67a55a82ce06 | 72 | DBG_msg("new RPC cmd ", inbuf); |
DeMein | 0:67a55a82ce06 | 73 | param[par_R_].assign(inbuf); // no append, assign string |
DeMein | 0:67a55a82ce06 | 74 | } |
DeMein | 0:67a55a82ce06 | 75 | } |
DeMein | 0:67a55a82ce06 | 76 | |
DeMein | 0:67a55a82ce06 | 77 | /** Return the result of the previous RPC operation |
DeMein | 0:67a55a82ce06 | 78 | * |
DeMein | 0:67a55a82ce06 | 79 | * syntax: RR or /R |
DeMein | 0:67a55a82ce06 | 80 | */ |
DeMein | 0:67a55a82ce06 | 81 | void do_rresult() { |
DeMein | 0:67a55a82ce06 | 82 | char * cstr; |
DeMein | 0:67a55a82ce06 | 83 | |
DeMein | 0:67a55a82ce06 | 84 | DBG_msg("do_rresult", inbuf); |
DeMein | 0:67a55a82ce06 | 85 | cstr = new char [param[par_S_].size() + 1]; |
DeMein | 0:67a55a82ce06 | 86 | strcpy(cstr, param[par_S_].c_str()); |
DeMein | 0:67a55a82ce06 | 87 | DBG_msg("RPC result", cstr); |
DeMein | 0:67a55a82ce06 | 88 | upstring(cstr); |
DeMein | 0:67a55a82ce06 | 89 | mldl.tx_string(cstr); |
DeMein | 0:67a55a82ce06 | 90 | delete[] cstr; |
DeMein | 0:67a55a82ce06 | 91 | } |
DeMein | 0:67a55a82ce06 | 92 | |
DeMein | 0:67a55a82ce06 | 93 | /** Execute the current RPC string |
DeMein | 0:67a55a82ce06 | 94 | * |
DeMein | 0:67a55a82ce06 | 95 | * syntax: RX or /X |
DeMein | 0:67a55a82ce06 | 96 | */ |
DeMein | 0:67a55a82ce06 | 97 | void do_rxeq() { |
DeMein | 0:67a55a82ce06 | 98 | char outbuf[256]; |
DeMein | 0:67a55a82ce06 | 99 | char * cstr; |
DeMein | 0:67a55a82ce06 | 100 | |
DeMein | 0:67a55a82ce06 | 101 | DBG_msg("do_rxeq", inbuf); |
DeMein | 0:67a55a82ce06 | 102 | |
DeMein | 0:67a55a82ce06 | 103 | cstr = new char [param[par_R_].size() + 1]; |
DeMein | 0:67a55a82ce06 | 104 | strcpy(cstr, param[par_R_].c_str()); |
DeMein | 0:67a55a82ce06 | 105 | DBG_msg("RPC execute", cstr); |
DeMein | 0:67a55a82ce06 | 106 | |
DeMein | 0:67a55a82ce06 | 107 | rpc(cstr, outbuf); // do the RPC |
DeMein | 0:67a55a82ce06 | 108 | delete[] cstr; |
DeMein | 0:67a55a82ce06 | 109 | |
DeMein | 0:67a55a82ce06 | 110 | DBG_msg("RPC result", outbuf); |
DeMein | 0:67a55a82ce06 | 111 | param[par_S_].assign(outbuf); // store result |
DeMein | 0:67a55a82ce06 | 112 | } |
DeMein | 0:67a55a82ce06 | 113 | |
DeMein | 0:67a55a82ce06 | 114 | /** send error message, command not recognized |
DeMein | 0:67a55a82ce06 | 115 | * |
DeMein | 0:67a55a82ce06 | 116 | */ |
DeMein | 0:67a55a82ce06 | 117 | void do_rdefault() { |
DeMein | 0:67a55a82ce06 | 118 | DBG_msg("do_rdefault", inbuf); |
DeMein | 0:67a55a82ce06 | 119 | send_error(err_notrecognized); |
DeMein | 0:67a55a82ce06 | 120 | } |