Library to allo USB PTP device to be hosted by the mbed platform
Dependents: class_project_main
PIMA15740/PIMAString.h@11:3b072cf16df8, 2013-10-07 (annotated)
- Committer:
- jakowisp
- Date:
- Mon Oct 07 04:45:55 2013 +0000
- Revision:
- 11:3b072cf16df8
- Parent:
- 3:1fcb46ab18df
Add code for Property description download.; Add code decoder logic.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jakowisp | 2:67753d738eb8 | 1 | /** |
jakowisp | 2:67753d738eb8 | 2 | * @file PIMAString.h |
jakowisp | 2:67753d738eb8 | 3 | * @brief PIMA String class definition |
jakowisp | 2:67753d738eb8 | 4 | * @author Dwayne Dilbeck |
jakowisp | 2:67753d738eb8 | 5 | * @date 8/23/2013 |
jakowisp | 2:67753d738eb8 | 6 | * |
jakowisp | 3:1fcb46ab18df | 7 | * mbed USBHostPTP Library(PIMA15740 String definition) |
jakowisp | 3:1fcb46ab18df | 8 | * @par Copyright: |
jakowisp | 3:1fcb46ab18df | 9 | * Copyright (c) 2013 Dwayne Dilbeck |
jakowisp | 3:1fcb46ab18df | 10 | * @par License: |
jakowisp | 3:1fcb46ab18df | 11 | * This software is distributed under the terms of the GNU Lesser General Public License |
jakowisp | 2:67753d738eb8 | 12 | */ |
jakowisp | 0:98cf19bcd828 | 13 | |
jakowisp | 2:67753d738eb8 | 14 | /** |
jakowisp | 2:67753d738eb8 | 15 | * Class PIMA String |
jakowisp | 2:67753d738eb8 | 16 | * |
jakowisp | 3:1fcb46ab18df | 17 | * The PIMA15740 standard defines a string as an unsigned 8bit length followed by a list |
jakowisp | 2:67753d738eb8 | 18 | * of 16bit unicode characters |
jakowisp | 2:67753d738eb8 | 19 | * |
jakowisp | 2:67753d738eb8 | 20 | */ |
jakowisp | 0:98cf19bcd828 | 21 | class PIMAString { |
jakowisp | 0:98cf19bcd828 | 22 | public: |
jakowisp | 0:98cf19bcd828 | 23 | |
jakowisp | 2:67753d738eb8 | 24 | /** |
jakowisp | 2:67753d738eb8 | 25 | * Constructor |
jakowisp | 3:1fcb46ab18df | 26 | * |
jakowisp | 2:67753d738eb8 | 27 | * By default the pointers are set to NULL and length set to ZERO. |
jakowisp | 2:67753d738eb8 | 28 | */ |
jakowisp | 0:98cf19bcd828 | 29 | PIMAString() { |
jakowisp | 0:98cf19bcd828 | 30 | length=0; |
jakowisp | 0:98cf19bcd828 | 31 | StringChars=NULL; |
jakowisp | 0:98cf19bcd828 | 32 | vals=NULL; |
jakowisp | 0:98cf19bcd828 | 33 | }; |
jakowisp | 0:98cf19bcd828 | 34 | |
jakowisp | 2:67753d738eb8 | 35 | /** |
jakowisp | 2:67753d738eb8 | 36 | * Desctructor |
jakowisp | 3:1fcb46ab18df | 37 | * |
jakowisp | 3:1fcb46ab18df | 38 | * Free the Memory allocated for character storage |
jakowisp | 2:67753d738eb8 | 39 | */ |
jakowisp | 0:98cf19bcd828 | 40 | ~PIMAString() { |
jakowisp | 0:98cf19bcd828 | 41 | if( StringChars !=NULL) |
jakowisp | 0:98cf19bcd828 | 42 | free(StringChars); |
jakowisp | 0:98cf19bcd828 | 43 | if(vals!=NULL) |
jakowisp | 0:98cf19bcd828 | 44 | free(vals); |
jakowisp | 0:98cf19bcd828 | 45 | }; |
jakowisp | 0:98cf19bcd828 | 46 | |
jakowisp | 2:67753d738eb8 | 47 | /** |
jakowisp | 3:1fcb46ab18df | 48 | * Create and fill character storage from a supplied buffer pointer |
jakowisp | 2:67753d738eb8 | 49 | * |
jakowisp | 2:67753d738eb8 | 50 | * @param currentPtr a unit8_t pointer to a buffer location where a PIMAString shoudl be read |
jakowisp | 2:67753d738eb8 | 51 | * |
jakowisp | 3:1fcb46ab18df | 52 | * @return The number of bytes used from the buffer to create the PIMA string. |
jakowisp | 2:67753d738eb8 | 53 | */ |
jakowisp | 0:98cf19bcd828 | 54 | int FillString(uint8_t *currentPtr) { |
jakowisp | 0:98cf19bcd828 | 55 | setLength(*currentPtr); |
jakowisp | 0:98cf19bcd828 | 56 | setStringChars((uint16_t *)(currentPtr+1)); |
jakowisp | 0:98cf19bcd828 | 57 | return 2*length +1; |
jakowisp | 0:98cf19bcd828 | 58 | }; |
jakowisp | 0:98cf19bcd828 | 59 | |
jakowisp | 2:67753d738eb8 | 60 | /** |
jakowisp | 2:67753d738eb8 | 61 | * Truncate the unicode characters to make a string for printf. |
jakowisp | 2:67753d738eb8 | 62 | * |
jakowisp | 2:67753d738eb8 | 63 | * @param none |
jakowisp | 2:67753d738eb8 | 64 | * @return pointer to an array of characters that is null terminated. |
jakowisp | 2:67753d738eb8 | 65 | */ |
jakowisp | 0:98cf19bcd828 | 66 | char * getString() { |
jakowisp | 0:98cf19bcd828 | 67 | if(vals!=NULL) |
jakowisp | 0:98cf19bcd828 | 68 | free(vals); |
jakowisp | 0:98cf19bcd828 | 69 | vals=(char *) malloc(sizeof(char)*(this->length+1)); |
jakowisp | 0:98cf19bcd828 | 70 | for(int i=0;i<this->length;i++) |
jakowisp | 0:98cf19bcd828 | 71 | vals[i]=StringChars[i]; //NOTE: Truncates the uint16_t value. |
jakowisp | 0:98cf19bcd828 | 72 | vals[length]='\0'; |
jakowisp | 0:98cf19bcd828 | 73 | return vals; |
jakowisp | 0:98cf19bcd828 | 74 | } |
jakowisp | 0:98cf19bcd828 | 75 | |
jakowisp | 2:67753d738eb8 | 76 | /** |
jakowisp | 2:67753d738eb8 | 77 | * Get the number of characters stored in the PIMA String |
jakowisp | 2:67753d738eb8 | 78 | * |
jakowisp | 2:67753d738eb8 | 79 | * @param none |
jakowisp | 2:67753d738eb8 | 80 | * @return uint8_t value that is the number of chracters notstored in the PIMA String |
jakowisp | 2:67753d738eb8 | 81 | */ |
jakowisp | 0:98cf19bcd828 | 82 | uint8_t getLength() { |
jakowisp | 0:98cf19bcd828 | 83 | return this->length; |
jakowisp | 0:98cf19bcd828 | 84 | } |
jakowisp | 0:98cf19bcd828 | 85 | |
jakowisp | 0:98cf19bcd828 | 86 | private: |
jakowisp | 2:67753d738eb8 | 87 | /** |
jakowisp | 2:67753d738eb8 | 88 | * Function to allocate unicode cahracter storage space and set the length value |
jakowisp | 2:67753d738eb8 | 89 | * |
jakowisp | 2:67753d738eb8 | 90 | * @param uint8_t The number of characters to create storage space for. |
jakowisp | 2:67753d738eb8 | 91 | * @return none |
jakowisp | 2:67753d738eb8 | 92 | */ |
jakowisp | 0:98cf19bcd828 | 93 | void setLength(uint8_t length) { |
jakowisp | 0:98cf19bcd828 | 94 | if(length > this->length) { |
jakowisp | 0:98cf19bcd828 | 95 | if( StringChars !=NULL) |
jakowisp | 0:98cf19bcd828 | 96 | free(StringChars); |
jakowisp | 0:98cf19bcd828 | 97 | StringChars = (uint16_t *) malloc(sizeof(uint16_t)*length); |
jakowisp | 0:98cf19bcd828 | 98 | } |
jakowisp | 0:98cf19bcd828 | 99 | this->length=length; |
jakowisp | 0:98cf19bcd828 | 100 | }; |
jakowisp | 0:98cf19bcd828 | 101 | |
jakowisp | 2:67753d738eb8 | 102 | /** |
jakowisp | 2:67753d738eb8 | 103 | * Function to read unicode charcaters form a uint8t buffer and store them. |
jakowisp | 2:67753d738eb8 | 104 | * @param pointer to a uint8_t buffer |
jakowisp | 2:67753d738eb8 | 105 | */ |
jakowisp | 0:98cf19bcd828 | 106 | void setStringChars(uint16_t *buffer){ |
jakowisp | 0:98cf19bcd828 | 107 | if(buffer!=NULL && StringChars !=NULL) |
jakowisp | 0:98cf19bcd828 | 108 | for(int i=0;i<this->length;i++) |
jakowisp | 0:98cf19bcd828 | 109 | StringChars[i]=buffer[i]; |
jakowisp | 0:98cf19bcd828 | 110 | }; |
jakowisp | 0:98cf19bcd828 | 111 | |
jakowisp | 2:67753d738eb8 | 112 | ///number of stored characters |
jakowisp | 0:98cf19bcd828 | 113 | uint8_t length; |
jakowisp | 2:67753d738eb8 | 114 | ///Pointer to the character storage |
jakowisp | 0:98cf19bcd828 | 115 | uint16_t *StringChars; |
jakowisp | 2:67753d738eb8 | 116 | ///Pointer to Character storge that are truncated unicode values. |
jakowisp | 0:98cf19bcd828 | 117 | char *vals; |
jakowisp | 0:98cf19bcd828 | 118 | }; |