Based on myBlueUSB and rosserial_mbed

Dependencies:   mbed myUSBHost AvailableMemory myBlueUSB

Committer:
OTL
Date:
Sat Sep 17 14:21:35 2011 +0000
Revision:
0:7684b95768c7

        

Who changed what in which revision?

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