Improved RPC library with added bounds checking to stop memory corruption, removed memory leaks, Arduino pin names, better object discovery.
Fork of mbed-rpc by
I have added bounds checking in both Argument and Reply classes to stop existing buffer over-runs in both classes.
Added the ability to discover the name and class of an RPC object to allow better discovery of existing objects on a server. Removed a memory leak with externally created RPC objects. Ensured consistency so that externally created objects can't be deleted using the RPC interface and are correctly listed after RPC::clear.
Updated RPCVariable to use references instead of pointers to ensure that the compiler checks that there is always a valid backing variable. Also removed RPC new/delete functionality as this is not valid. Added a specialisation of RPCVariable to allow for strings to be read and written via RPC.
Added Arduino pin names to parse_pins, currently only for FRDM boards but same code should work for others just need to add them to the #ifdef.
parse_pins.cpp@10:7511acfaa62d, 2015-03-30 (annotated)
- Committer:
- rhourahane
- Date:
- Mon Mar 30 16:43:47 2015 +0000
- Revision:
- 10:7511acfaa62d
- Parent:
- 9:6ce5db613c77
Updates to improve robustness and remove memory leak. Added RCVariable specialisation for char buffer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:6919289a5946 | 1 | /* mbed Microcontroller Library |
emilmont | 1:6919289a5946 | 2 | * Copyright (c) 2006-2013 ARM Limited |
emilmont | 1:6919289a5946 | 3 | * |
emilmont | 1:6919289a5946 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
emilmont | 1:6919289a5946 | 5 | * you may not use this file except in compliance with the License. |
emilmont | 1:6919289a5946 | 6 | * You may obtain a copy of the License at |
emilmont | 1:6919289a5946 | 7 | * |
emilmont | 1:6919289a5946 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
emilmont | 1:6919289a5946 | 9 | * |
emilmont | 1:6919289a5946 | 10 | * Unless required by applicable law or agreed to in writing, software |
emilmont | 1:6919289a5946 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
emilmont | 1:6919289a5946 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
emilmont | 1:6919289a5946 | 13 | * See the License for the specific language governing permissions and |
emilmont | 1:6919289a5946 | 14 | * limitations under the License. |
emilmont | 1:6919289a5946 | 15 | */ |
emilmont | 1:6919289a5946 | 16 | #include "port_api.h" |
rhourahane | 9:6ce5db613c77 | 17 | #include <string.h> |
emilmont | 1:6919289a5946 | 18 | |
emilmont | 1:6919289a5946 | 19 | namespace mbed { |
emilmont | 1:6919289a5946 | 20 | |
emilmont | 1:6919289a5946 | 21 | PinName parse_pins(const char *str) { |
emilmont | 4:9f88f495e549 | 22 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC2368) |
emilmont | 1:6919289a5946 | 23 | static const PinName pin_names[] = {p5, p6, p7, p8, p9, p10, p11, p12, p13, p14 |
emilmont | 1:6919289a5946 | 24 | , p15, p16, p17, p18, p19, p20, p21, p22, p23 |
emilmont | 1:6919289a5946 | 25 | , p24, p25, p26, p27, p28, p29, p30}; |
bogdanm | 5:4490a0d9cb2a | 26 | #elif defined(TARGET_LPC1114) |
bogdanm | 5:4490a0d9cb2a | 27 | static const PinName pin_names[] = {dp1, dp2, dp4, dp5, dp6, dp9, dp10, dp11 |
bogdanm | 5:4490a0d9cb2a | 28 | , dp13, dp14, dp15, dp16, dp17, dp18, dp23 |
bogdanm | 5:4490a0d9cb2a | 29 | , dp24, dp25, dp26, dp27, dp28}; |
mbed_official | 7:2a26fd6a2b36 | 30 | #elif defined(TARGET_LPC4088) |
mbed_official | 8:fece2d5e8d96 | 31 | static const PinName pin_names[] = {NC, NC, NC, NC, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14 |
emilmont | 4:9f88f495e549 | 32 | , p15, p16, p17, p18, p19, p20, NC, NC, p23 |
emilmont | 4:9f88f495e549 | 33 | , p24, p25, p26, p27, p28, p29, p30, p31, p32 |
mbed_official | 8:fece2d5e8d96 | 34 | , p33, p34, NC, NC, p37, p38, p39, NC, NC, NC, NC, NC, NC, NC}; |
mbed_official | 8:fece2d5e8d96 | 35 | #elif defined(TARGET_LPC4088_DM) |
mbed_official | 8:fece2d5e8d96 | 36 | static const PinName pin_names[] = {p1, p2, p3, p4, NC, NC, p7, p8, p9, p10, p11, p12, p13, p14 |
mbed_official | 8:fece2d5e8d96 | 37 | , p15, p16, p17, p18, p19, p20, p21, p22, p23 |
mbed_official | 8:fece2d5e8d96 | 38 | , p24, p25, p26, NC, NC, p29, p30, NC, NC |
mbed_official | 8:fece2d5e8d96 | 39 | , NC, NC, NC, NC, NC, NC, NC, NC, p41, p42, p43, p44, p45, p46}; |
emilmont | 1:6919289a5946 | 40 | #endif |
emilmont | 1:6919289a5946 | 41 | |
mbed_official | 8:fece2d5e8d96 | 42 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC2368) || defined(TARGET_LPC812) || defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM) || defined(TARGET_LPC1114) |
emilmont | 1:6919289a5946 | 43 | if (str[0] == 'P') { // Pn_n |
emilmont | 1:6919289a5946 | 44 | uint32_t port = str[1] - '0'; |
emilmont | 1:6919289a5946 | 45 | uint32_t pin = str[3] - '0'; // Pn_n |
emilmont | 1:6919289a5946 | 46 | uint32_t pin2 = str[4] - '0'; // Pn_nn |
emilmont | 1:6919289a5946 | 47 | if (pin2 <= 9) { |
emilmont | 1:6919289a5946 | 48 | pin = pin * 10 + pin2; |
emilmont | 1:6919289a5946 | 49 | } |
emilmont | 1:6919289a5946 | 50 | return port_pin((PortName)port, pin); |
emilmont | 1:6919289a5946 | 51 | |
mbed_official | 7:2a26fd6a2b36 | 52 | #elif defined(TARGET_KL25Z) || defined(TARGET_KL05Z) || defined(TARGET_KL46Z) || defined(TARGET_K64F) |
mbed_official | 7:2a26fd6a2b36 | 53 | if (str[0] == 'P' && str[1] == 'T') { // PTxn |
mbed_official | 7:2a26fd6a2b36 | 54 | uint32_t port = str[2] - 'A'; |
mbed_official | 7:2a26fd6a2b36 | 55 | uint32_t pin = str[3] - '0'; // PTxn |
mbed_official | 7:2a26fd6a2b36 | 56 | uint32_t pin2 = str[4] - '0'; // PTxnn |
emilmont | 1:6919289a5946 | 57 | |
mbed_official | 7:2a26fd6a2b36 | 58 | if (pin2 <= 9) { |
mbed_official | 7:2a26fd6a2b36 | 59 | pin = pin * 10 + pin2; |
mbed_official | 7:2a26fd6a2b36 | 60 | } |
mbed_official | 7:2a26fd6a2b36 | 61 | return port_pin((PortName)port, pin); |
emilmont | 1:6919289a5946 | 62 | #endif |
emilmont | 1:6919289a5946 | 63 | |
mbed_official | 7:2a26fd6a2b36 | 64 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC2368) |
emilmont | 1:6919289a5946 | 65 | } else if (str[0] == 'p') { // pn |
emilmont | 1:6919289a5946 | 66 | uint32_t pin = str[1] - '0'; // pn |
emilmont | 1:6919289a5946 | 67 | uint32_t pin2 = str[2] - '0'; // pnn |
emilmont | 1:6919289a5946 | 68 | if (pin2 <= 9) { |
emilmont | 1:6919289a5946 | 69 | pin = pin * 10 + pin2; |
emilmont | 1:6919289a5946 | 70 | } |
emilmont | 1:6919289a5946 | 71 | if (pin < 5 || pin > 30) { |
emilmont | 1:6919289a5946 | 72 | return NC; |
emilmont | 1:6919289a5946 | 73 | } |
emilmont | 1:6919289a5946 | 74 | return pin_names[pin - 5]; |
mbed_official | 8:fece2d5e8d96 | 75 | #elif defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM) |
emilmont | 4:9f88f495e549 | 76 | } else if (str[0] == 'p') { // pn |
emilmont | 4:9f88f495e549 | 77 | uint32_t pin = str[1] - '0'; // pn |
emilmont | 4:9f88f495e549 | 78 | uint32_t pin2 = str[2] - '0'; // pnn |
emilmont | 4:9f88f495e549 | 79 | if (pin2 <= 9) { |
emilmont | 4:9f88f495e549 | 80 | pin = pin * 10 + pin2; |
emilmont | 4:9f88f495e549 | 81 | } |
mbed_official | 8:fece2d5e8d96 | 82 | if (pin < 1 || pin > 46) { |
emilmont | 4:9f88f495e549 | 83 | return NC; |
emilmont | 4:9f88f495e549 | 84 | } |
mbed_official | 8:fece2d5e8d96 | 85 | return pin_names[pin - 1]; |
emilmont | 1:6919289a5946 | 86 | #endif |
emilmont | 1:6919289a5946 | 87 | |
emilmont | 1:6919289a5946 | 88 | } else if (str[0] == 'L') { // LEDn |
emilmont | 1:6919289a5946 | 89 | switch (str[3]) { |
emilmont | 1:6919289a5946 | 90 | case '1' : return LED1; |
emilmont | 1:6919289a5946 | 91 | case '2' : return LED2; |
emilmont | 1:6919289a5946 | 92 | case '3' : return LED3; |
emilmont | 1:6919289a5946 | 93 | case '4' : return LED4; |
emilmont | 1:6919289a5946 | 94 | } |
emilmont | 1:6919289a5946 | 95 | |
emilmont | 1:6919289a5946 | 96 | } else if (str[0] == 'U') { // USB?X |
emilmont | 1:6919289a5946 | 97 | switch (str[3]) { |
emilmont | 1:6919289a5946 | 98 | case 'T' : return USBTX; |
emilmont | 1:6919289a5946 | 99 | case 'R' : return USBRX; |
emilmont | 1:6919289a5946 | 100 | } |
emilmont | 1:6919289a5946 | 101 | } |
emilmont | 1:6919289a5946 | 102 | |
rhourahane | 9:6ce5db613c77 | 103 | #if defined(TARGET_KL25Z) || defined(TARGET_KL05Z) || defined(TARGET_KL46Z) || defined(TARGET_K64F) |
rhourahane | 9:6ce5db613c77 | 104 | // Handle Ardiuno pin names. |
rhourahane | 9:6ce5db613c77 | 105 | else if ((str[0] == 'D') && ((str[1] >= '0') && (str[1] <= '9'))) { |
rhourahane | 9:6ce5db613c77 | 106 | static const PinName digital_pins[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14, D15 }; |
rhourahane | 9:6ce5db613c77 | 107 | int pin = str[1] - '0'; // pn |
rhourahane | 9:6ce5db613c77 | 108 | int pin2 = str[2] - '0'; // pnn |
rhourahane | 9:6ce5db613c77 | 109 | if ((pin2 >= 0) && (pin2 <= 9)) { |
rhourahane | 9:6ce5db613c77 | 110 | pin = pin * 10 + pin2; |
rhourahane | 9:6ce5db613c77 | 111 | } |
rhourahane | 9:6ce5db613c77 | 112 | if ((pin < 0) || (pin > 16)) { |
rhourahane | 9:6ce5db613c77 | 113 | return NC; |
rhourahane | 9:6ce5db613c77 | 114 | } |
rhourahane | 9:6ce5db613c77 | 115 | |
rhourahane | 9:6ce5db613c77 | 116 | return digital_pins[pin]; |
rhourahane | 9:6ce5db613c77 | 117 | } else if ((str[0] == 'A') && ((str[1] >= '0') && (str[1] <= '9'))) { |
rhourahane | 9:6ce5db613c77 | 118 | static const PinName analog_pins[] = { A0, A1, A2, A3, A4, A5 }; |
rhourahane | 9:6ce5db613c77 | 119 | int pin = str[1] - '0'; // pn |
rhourahane | 9:6ce5db613c77 | 120 | if ((pin < 0) || (pin > 6)) { |
rhourahane | 9:6ce5db613c77 | 121 | return NC; |
rhourahane | 9:6ce5db613c77 | 122 | } |
rhourahane | 9:6ce5db613c77 | 123 | |
rhourahane | 9:6ce5db613c77 | 124 | return analog_pins[pin]; |
rhourahane | 9:6ce5db613c77 | 125 | } |
rhourahane | 9:6ce5db613c77 | 126 | #endif |
rhourahane | 9:6ce5db613c77 | 127 | |
rhourahane | 9:6ce5db613c77 | 128 | #if defined(TARGET_K64F) |
rhourahane | 9:6ce5db613c77 | 129 | if (strcmp(str, "DAC0_OUT") == 0) { |
rhourahane | 9:6ce5db613c77 | 130 | return DAC0_OUT; |
rhourahane | 9:6ce5db613c77 | 131 | } |
rhourahane | 9:6ce5db613c77 | 132 | #endif |
rhourahane | 9:6ce5db613c77 | 133 | |
emilmont | 1:6919289a5946 | 134 | return NC; |
emilmont | 1:6919289a5946 | 135 | } |
emilmont | 1:6919289a5946 | 136 | |
emilmont | 1:6919289a5946 | 137 | } |