local fix version of myBlueUSB (http://mbed.org/users/networker/code/myBlueUSB/). - merge deleted files which are required to compile. - enable echo back of received data via RFCOMM.
Dependencies: AvailableMemory FatFileSystem mbed myUSBHost
sdp/sdp_data.cpp@0:003889bc474f, 2013-12-07 (annotated)
- Committer:
- nobukuma
- Date:
- Sat Dec 07 14:19:00 2013 +0000
- Revision:
- 0:003889bc474f
http://mbed.org/users/networker/code/myBlueUSB/ rev13??rev12??????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nobukuma | 0:003889bc474f | 1 | #include "mbed.h" |
nobukuma | 0:003889bc474f | 2 | #include "sdp_data.h" |
nobukuma | 0:003889bc474f | 3 | #include "Utils.h" |
nobukuma | 0:003889bc474f | 4 | |
nobukuma | 0:003889bc474f | 5 | char sdp_data::ret[12]; |
nobukuma | 0:003889bc474f | 6 | |
nobukuma | 0:003889bc474f | 7 | unsigned sdp_data::asUnsigned() { |
nobukuma | 0:003889bc474f | 8 | switch (type) { |
nobukuma | 0:003889bc474f | 9 | case NULL_: |
nobukuma | 0:003889bc474f | 10 | return 0; |
nobukuma | 0:003889bc474f | 11 | case UNSIGNED: |
nobukuma | 0:003889bc474f | 12 | case SIGNED: |
nobukuma | 0:003889bc474f | 13 | case BOOL: |
nobukuma | 0:003889bc474f | 14 | return data; |
nobukuma | 0:003889bc474f | 15 | case UUID: |
nobukuma | 0:003889bc474f | 16 | #ifdef LONGUUID |
nobukuma | 0:003889bc474f | 17 | return uuid[6] + uuid[7]<<16; |
nobukuma | 0:003889bc474f | 18 | #else |
nobukuma | 0:003889bc474f | 19 | return data; |
nobukuma | 0:003889bc474f | 20 | #endif |
nobukuma | 0:003889bc474f | 21 | default: |
nobukuma | 0:003889bc474f | 22 | return 0; |
nobukuma | 0:003889bc474f | 23 | } |
nobukuma | 0:003889bc474f | 24 | } |
nobukuma | 0:003889bc474f | 25 | |
nobukuma | 0:003889bc474f | 26 | const char* sdp_data::asString(bool alt) { |
nobukuma | 0:003889bc474f | 27 | char sep = ','; |
nobukuma | 0:003889bc474f | 28 | switch (type) { |
nobukuma | 0:003889bc474f | 29 | case NULL_: |
nobukuma | 0:003889bc474f | 30 | return "NULL"; |
nobukuma | 0:003889bc474f | 31 | case UNSIGNED: |
nobukuma | 0:003889bc474f | 32 | if (alt) sprintf(ret, "0x%0*X", size*2, data); |
nobukuma | 0:003889bc474f | 33 | else sprintf(ret, "%u", data); |
nobukuma | 0:003889bc474f | 34 | return ret; |
nobukuma | 0:003889bc474f | 35 | case SIGNED: |
nobukuma | 0:003889bc474f | 36 | sprintf(ret, "%d", data); |
nobukuma | 0:003889bc474f | 37 | return ret; |
nobukuma | 0:003889bc474f | 38 | case BOOL: |
nobukuma | 0:003889bc474f | 39 | return data ? "TRUE" : "FALSE"; |
nobukuma | 0:003889bc474f | 40 | case STRING: |
nobukuma | 0:003889bc474f | 41 | case URL: |
nobukuma | 0:003889bc474f | 42 | return str; |
nobukuma | 0:003889bc474f | 43 | case ALTERNATIVE: |
nobukuma | 0:003889bc474f | 44 | sep = '|'; |
nobukuma | 0:003889bc474f | 45 | case SEQUENCE: { |
nobukuma | 0:003889bc474f | 46 | if (longstr) delete[] longstr; |
nobukuma | 0:003889bc474f | 47 | int n = sprintf(ret, "SEQ %d { ", size) + 1; |
nobukuma | 0:003889bc474f | 48 | longstr = new char[n]; |
nobukuma | 0:003889bc474f | 49 | strcpy(longstr, ret); |
nobukuma | 0:003889bc474f | 50 | for (int i = 0; i < sequence.size(); i++) { |
nobukuma | 0:003889bc474f | 51 | const char *s = sequence[i]->asString(alt); |
nobukuma | 0:003889bc474f | 52 | n = strlen(longstr) + strlen(s) + 2; |
nobukuma | 0:003889bc474f | 53 | char *t = new char[n]; |
nobukuma | 0:003889bc474f | 54 | strcpy(t, longstr); |
nobukuma | 0:003889bc474f | 55 | strcat(t, s); |
nobukuma | 0:003889bc474f | 56 | t[n-2] = sep; |
nobukuma | 0:003889bc474f | 57 | t[n-1]='\0'; |
nobukuma | 0:003889bc474f | 58 | //printf("[%s]+[%s]+%c=[%s]\n", longstr, s, sep, t); |
nobukuma | 0:003889bc474f | 59 | delete[] longstr; |
nobukuma | 0:003889bc474f | 60 | longstr = t; |
nobukuma | 0:003889bc474f | 61 | } |
nobukuma | 0:003889bc474f | 62 | longstr[n-2] = '}'; |
nobukuma | 0:003889bc474f | 63 | } |
nobukuma | 0:003889bc474f | 64 | return longstr; |
nobukuma | 0:003889bc474f | 65 | case UUID: |
nobukuma | 0:003889bc474f | 66 | #ifdef LONGUUID |
nobukuma | 0:003889bc474f | 67 | switch (size) { |
nobukuma | 0:003889bc474f | 68 | case 2: |
nobukuma | 0:003889bc474f | 69 | sprintf(ret, "0x%04X", uuid[6]); |
nobukuma | 0:003889bc474f | 70 | return ret; |
nobukuma | 0:003889bc474f | 71 | case 4: |
nobukuma | 0:003889bc474f | 72 | sprintf(ret, "0x%04X%04X", uuid[7],uuid[6]); |
nobukuma | 0:003889bc474f | 73 | return ret; |
nobukuma | 0:003889bc474f | 74 | case 16: |
nobukuma | 0:003889bc474f | 75 | longstr = new char[35]; |
nobukuma | 0:003889bc474f | 76 | sprintf(longstr, "%04X%04X-%04X-%04X-%04X-%04X%04X%04X", uuid[7],uuid[6],uuid[5],uuid[4],uuid[3],uuid[2],uuid[1],uuid[0]); |
nobukuma | 0:003889bc474f | 77 | return longstr; |
nobukuma | 0:003889bc474f | 78 | } |
nobukuma | 0:003889bc474f | 79 | #else |
nobukuma | 0:003889bc474f | 80 | switch (size) { |
nobukuma | 0:003889bc474f | 81 | case 2: |
nobukuma | 0:003889bc474f | 82 | sprintf(ret, "0x%04X", data & 0xffff); |
nobukuma | 0:003889bc474f | 83 | return ret; |
nobukuma | 0:003889bc474f | 84 | case 4: |
nobukuma | 0:003889bc474f | 85 | sprintf(ret, "0x%08X", data); |
nobukuma | 0:003889bc474f | 86 | return ret; |
nobukuma | 0:003889bc474f | 87 | case 16: |
nobukuma | 0:003889bc474f | 88 | longstr = new char[35]; |
nobukuma | 0:003889bc474f | 89 | sprintf(longstr, "%08X-%04X-%04X-%04X-%04X%04X%04X", data,base_uuid[5],base_uuid[4],base_uuid[3],base_uuid[2],base_uuid[1],base_uuid[0]); |
nobukuma | 0:003889bc474f | 90 | return longstr; |
nobukuma | 0:003889bc474f | 91 | } |
nobukuma | 0:003889bc474f | 92 | #endif |
nobukuma | 0:003889bc474f | 93 | } |
nobukuma | 0:003889bc474f | 94 | return "Unsupported"; |
nobukuma | 0:003889bc474f | 95 | } |
nobukuma | 0:003889bc474f | 96 | |
nobukuma | 0:003889bc474f | 97 | unsigned sdp_data::Size() { |
nobukuma | 0:003889bc474f | 98 | if (size==0 && type==SEQUENCE) |
nobukuma | 0:003889bc474f | 99 | return 2; |
nobukuma | 0:003889bc474f | 100 | if (size<3 || size==4 || size==8 || size==16) |
nobukuma | 0:003889bc474f | 101 | return size+1;//include descriptor |
nobukuma | 0:003889bc474f | 102 | if (size < 256) return size+2; //1 extra byte |
nobukuma | 0:003889bc474f | 103 | if (size < 65536) return size+3; //2 extra bytes |
nobukuma | 0:003889bc474f | 104 | return size+5; //4 extra bytes |
nobukuma | 0:003889bc474f | 105 | } |
nobukuma | 0:003889bc474f | 106 | |
nobukuma | 0:003889bc474f | 107 | unsigned sdp_data::sizedesc(unsigned char *buf) { |
nobukuma | 0:003889bc474f | 108 | int desc, extra=0; |
nobukuma | 0:003889bc474f | 109 | switch (size) { |
nobukuma | 0:003889bc474f | 110 | case 0: |
nobukuma | 0:003889bc474f | 111 | /* should be: |
nobukuma | 0:003889bc474f | 112 | if (type != NULL_) { |
nobukuma | 0:003889bc474f | 113 | desc = 5; |
nobukuma | 0:003889bc474f | 114 | extra = 1; |
nobukuma | 0:003889bc474f | 115 | buf[1] = 0; |
nobukuma | 0:003889bc474f | 116 | } |
nobukuma | 0:003889bc474f | 117 | */ |
nobukuma | 0:003889bc474f | 118 | case 1: |
nobukuma | 0:003889bc474f | 119 | desc = 0; |
nobukuma | 0:003889bc474f | 120 | break; |
nobukuma | 0:003889bc474f | 121 | case 2: |
nobukuma | 0:003889bc474f | 122 | desc = 1; |
nobukuma | 0:003889bc474f | 123 | break; |
nobukuma | 0:003889bc474f | 124 | case 4: |
nobukuma | 0:003889bc474f | 125 | desc = 2; |
nobukuma | 0:003889bc474f | 126 | break; |
nobukuma | 0:003889bc474f | 127 | case 8: |
nobukuma | 0:003889bc474f | 128 | desc = 3; |
nobukuma | 0:003889bc474f | 129 | break; |
nobukuma | 0:003889bc474f | 130 | case 16: |
nobukuma | 0:003889bc474f | 131 | desc = 4; |
nobukuma | 0:003889bc474f | 132 | break; |
nobukuma | 0:003889bc474f | 133 | default: |
nobukuma | 0:003889bc474f | 134 | if (size < 256) { |
nobukuma | 0:003889bc474f | 135 | desc = 5; |
nobukuma | 0:003889bc474f | 136 | extra = 1; |
nobukuma | 0:003889bc474f | 137 | buf[1] = size; |
nobukuma | 0:003889bc474f | 138 | } else if (size < 65536) { |
nobukuma | 0:003889bc474f | 139 | desc = 6; |
nobukuma | 0:003889bc474f | 140 | extra = 2; |
nobukuma | 0:003889bc474f | 141 | *(unsigned short*)&buf[1] = size; |
nobukuma | 0:003889bc474f | 142 | } else { |
nobukuma | 0:003889bc474f | 143 | desc = 7; |
nobukuma | 0:003889bc474f | 144 | extra = 4; |
nobukuma | 0:003889bc474f | 145 | *(unsigned*)&buf[1] = size; |
nobukuma | 0:003889bc474f | 146 | } |
nobukuma | 0:003889bc474f | 147 | } |
nobukuma | 0:003889bc474f | 148 | buf[0] |= desc; |
nobukuma | 0:003889bc474f | 149 | return extra+1; |
nobukuma | 0:003889bc474f | 150 | } |
nobukuma | 0:003889bc474f | 151 | |
nobukuma | 0:003889bc474f | 152 | void sdp_data::revcpy(unsigned char*d, const unsigned char*s, int n) { |
nobukuma | 0:003889bc474f | 153 | for (int i = 0; i < n; i++) |
nobukuma | 0:003889bc474f | 154 | d[i] = s[n-i-1]; |
nobukuma | 0:003889bc474f | 155 | } |
nobukuma | 0:003889bc474f | 156 | |
nobukuma | 0:003889bc474f | 157 | unsigned sdp_data::build(unsigned char *buf, unsigned max) {//max is ignored |
nobukuma | 0:003889bc474f | 158 | int p = 0; |
nobukuma | 0:003889bc474f | 159 | buf[p] = type<<3; |
nobukuma | 0:003889bc474f | 160 | switch (type) { |
nobukuma | 0:003889bc474f | 161 | case NULL_: |
nobukuma | 0:003889bc474f | 162 | p++; |
nobukuma | 0:003889bc474f | 163 | break; |
nobukuma | 0:003889bc474f | 164 | case UNSIGNED: |
nobukuma | 0:003889bc474f | 165 | case SIGNED: |
nobukuma | 0:003889bc474f | 166 | case BOOL: |
nobukuma | 0:003889bc474f | 167 | p += sizedesc(buf+p); |
nobukuma | 0:003889bc474f | 168 | revcpy(buf+p, (unsigned char*)&data, size); |
nobukuma | 0:003889bc474f | 169 | break; |
nobukuma | 0:003889bc474f | 170 | case UUID: |
nobukuma | 0:003889bc474f | 171 | p += sizedesc(buf+p); |
nobukuma | 0:003889bc474f | 172 | #ifdef LONGUUID |
nobukuma | 0:003889bc474f | 173 | switch (size) { |
nobukuma | 0:003889bc474f | 174 | case 2: |
nobukuma | 0:003889bc474f | 175 | case 4: |
nobukuma | 0:003889bc474f | 176 | revcpy(buf+p, (unsigned char*)&uuid[6], size); |
nobukuma | 0:003889bc474f | 177 | break; |
nobukuma | 0:003889bc474f | 178 | case 16: |
nobukuma | 0:003889bc474f | 179 | revcpy(buf+p, (unsigned char*)uuid, size); |
nobukuma | 0:003889bc474f | 180 | break; |
nobukuma | 0:003889bc474f | 181 | } |
nobukuma | 0:003889bc474f | 182 | #else |
nobukuma | 0:003889bc474f | 183 | switch (size) { |
nobukuma | 0:003889bc474f | 184 | case 2: |
nobukuma | 0:003889bc474f | 185 | case 4: |
nobukuma | 0:003889bc474f | 186 | revcpy(buf+p, (unsigned char*)&data, size); |
nobukuma | 0:003889bc474f | 187 | break; |
nobukuma | 0:003889bc474f | 188 | case 16: |
nobukuma | 0:003889bc474f | 189 | revcpy(buf+p, (unsigned char*)&data, 4); |
nobukuma | 0:003889bc474f | 190 | revcpy(buf+p+4, base_uuid, 12); |
nobukuma | 0:003889bc474f | 191 | break; |
nobukuma | 0:003889bc474f | 192 | } |
nobukuma | 0:003889bc474f | 193 | #endif |
nobukuma | 0:003889bc474f | 194 | break; |
nobukuma | 0:003889bc474f | 195 | case STRING: |
nobukuma | 0:003889bc474f | 196 | case URL: |
nobukuma | 0:003889bc474f | 197 | p += sizedesc(buf+p); |
nobukuma | 0:003889bc474f | 198 | memcpy(buf+p, str, size); |
nobukuma | 0:003889bc474f | 199 | break; |
nobukuma | 0:003889bc474f | 200 | case SEQUENCE: |
nobukuma | 0:003889bc474f | 201 | case ALTERNATIVE: { |
nobukuma | 0:003889bc474f | 202 | if (sequence.size()==0) {//hack: should be solved in sizedesc |
nobukuma | 0:003889bc474f | 203 | buf[p++] |= 5; |
nobukuma | 0:003889bc474f | 204 | buf[p++] = 0; |
nobukuma | 0:003889bc474f | 205 | break; |
nobukuma | 0:003889bc474f | 206 | } |
nobukuma | 0:003889bc474f | 207 | int n = 0; |
nobukuma | 0:003889bc474f | 208 | p += sizedesc(buf+p); |
nobukuma | 0:003889bc474f | 209 | for (int i = 0; i < sequence.size(); i++) |
nobukuma | 0:003889bc474f | 210 | n += sequence.at(i)->build(buf+p+n, max-p); |
nobukuma | 0:003889bc474f | 211 | } |
nobukuma | 0:003889bc474f | 212 | break; |
nobukuma | 0:003889bc474f | 213 | } |
nobukuma | 0:003889bc474f | 214 | p += size; |
nobukuma | 0:003889bc474f | 215 | // printfBytes("Build:", buf, p); |
nobukuma | 0:003889bc474f | 216 | return p; |
nobukuma | 0:003889bc474f | 217 | } |
nobukuma | 0:003889bc474f | 218 | |
nobukuma | 0:003889bc474f | 219 | bool sdp_data::findUUID(unsigned uuid) { |
nobukuma | 0:003889bc474f | 220 | if (type == UUID) |
nobukuma | 0:003889bc474f | 221 | return asUnsigned()==uuid; |
nobukuma | 0:003889bc474f | 222 | if (type==SEQUENCE || type==ALTERNATIVE) { |
nobukuma | 0:003889bc474f | 223 | for (int i = 0; i < sequence.size(); i++) { |
nobukuma | 0:003889bc474f | 224 | if (sequence[i]->findUUID(uuid)) |
nobukuma | 0:003889bc474f | 225 | return true; |
nobukuma | 0:003889bc474f | 226 | } |
nobukuma | 0:003889bc474f | 227 | } |
nobukuma | 0:003889bc474f | 228 | return false; |
nobukuma | 0:003889bc474f | 229 | } |