Ad van der Weiden / sdp

Dependents:   mbed_TANK_Kinect ftusbClass

Revision:
2:d5a27b2d2e08
Parent:
1:70ee392bcfd4
Child:
3:e8d2ebb7392e
--- a/sdp.cpp	Wed Apr 06 18:09:20 2011 +0000
+++ b/sdp.cpp	Thu Apr 07 21:01:39 2011 +0000
@@ -23,12 +23,16 @@
 void SDPManager::OnSdpRsp(const u8* data, int len) {
     static sdp_data list(sdp_data::SEQUENCE); 
     static sdp_data all(0x0000ffffU,4);
+    static sdp_data serviceID(0U, 2);
+    static sdp_data name(0x100U, 2);
     static sdp_data root(sdp_data::UUID, BROWSEROOT);
     static sdp_data req(sdp_data::SEQUENCE);
     static bool once = true;
-    printf("_state=%d first=%d   ", _state, once);
+    //printf("_state=%d first=%d   ", _state, once);
     if (once) {
        list.add_element(&all);
+       //list.add_element(&serviceID);
+       //list.add_element(&name);
        req.add_element(&root);
        once = false;
     }
@@ -39,7 +43,7 @@
         case 0: //closed
             if (len==0) { //socket just opened
                 //'Open' cleared the services list
-                ServiceSearchRequest(&req, 1);
+                ServiceSearchRequest(&req, 10);
                 _state = 1; //wait for service handles
             }
             break;
@@ -51,7 +55,7 @@
                     index = services.begin();
                     if (index != services.end()) {
                         unsigned handle = (*index).first;
-                        printf("req.: handle %#X\n", handle);
+                        //printf("req.: handle %#X\n", handle);
                         ServiceAttributeRequest(handle, 100, &list);//0x1001D
                     } else 
                         printf(" - empty list - \n");//should not happen
@@ -67,7 +71,7 @@
                 if (data[0]==5) {
                     index++; //move to next service
                     if (index != services.end()) {
-                        printf("req.: handle %#X\n", (*index).first);
+                        //printf("req.: handle %#X\n", (*index).first);
                         ServiceAttributeRequest((*index).first, 100, &list);
                     } else {
                         printf(" - end of list - \n");
@@ -308,21 +312,21 @@
         case sdp_data::ALTERNATIVE:
             item = new sdp_data(sdp_data::ALTERNATIVE);
 skip: {//p points just after the length indicator, hence at the first item IN the sequence
-               //printf("SEQ%d{%p ", len, item);
+                //printf("SEQ%d{%p ", len, item);
                 int n = 0;
                 unsigned short key;
-                serv_rec *dummy = 0;
+                serv_rec *dummy = 0;//means: there is no service record to fill in for deeper levels
                 while (p < end) {
-                    sdp_data *elem = 0;
+                    sdp_data *elem = 0; //this becomes the tree with attribute values
                     p += parse(el + p, len-p, elem, dummy);//parse each element in the sequence, the second arg is as yet unused
-                    item->add_element(elem);
-                    if (record) {
+                    if (record) { //if at the level of attribute list, add elem to record as key/value pair
                         if (n & 1) { //value
                             record->insert(pair<unsigned short, sdp_data*>(key, elem));
                         } else //key
                             key = elem->asUnsigned();
                         n++;
-                    }
+                    } else //just add the elements to the value tree
+                        item->add_element(elem);
                 }
             }
             //printf("}\n");
@@ -356,10 +360,13 @@
     byteCount = 0;
 }
 
+
+//TODO: test case 7, add server support (cases 2, 4, 6)
+//3 cases: cont==0 && contBuf==0 -> use rsp; cont!=0 -> append; cont==0 && contBuf!=0 -> append and use contBuf
 int SDPManager::parseRsp(const unsigned char*rsp, int len) {
     unsigned tid = rsp[2] + ((unsigned)rsp[1]<<8);
     unsigned parlen = rsp[4] + ((unsigned)rsp[3]<<8);
-    printf("ParseRsp: tid=%04X, parlen=%d ", tid, parlen);
+    //printf("ParseRsp: tid=%04X, parlen=%d ", tid, parlen);
     unsigned cont = 0;
     switch (rsp[0]) {
         case 1: {//errorRsp
@@ -394,19 +401,26 @@
         }
         break;
         case 5: { //serviceattributeRsp
-            if (tree) delete tree;
             unsigned count = rsp[6] + ((unsigned)rsp[5]<<8);//bytes in this part of the attribute list
-            append(rsp+7, count);
+//            append(rsp+7, count);
             cont = rsp[7+count];
             memcpy(contState, &rsp[7+count], cont+1);//copy the continuation state
-            if (cont) break;
-            printf("count=%d parsing...\n", byteCount);
+            if (cont) {
+               append(rsp+7, count);
+               break;
+            }
+            //printf("count=%d parsing...\n", byteCount);
             serv_rec *serv = new serv_rec;
-            parse(contBuf, byteCount, tree, serv);
-            printf("...parsing done,  ");
-            unsigned key = (*serv)[0]->asUnsigned();//'0' is always the serviceID
-            printf("Key=%#X\n", key);
-            services[key] = serv;
+            if (contBuf){
+               append(rsp+7, count);
+               parse(contBuf, byteCount, tree, serv);
+            } else
+               parse(rsp+7, count, tree, serv);
+            //printf("...parsing done,  ");
+            //get the AttributeID, make sure attribId 0 is always included in the request
+            unsigned key = (*serv)[0]->asUnsigned();//AttributeID '0' always refers to the serviceID
+            //printf("Key=%#X\n", key); //key will be 0 when not requested
+            services[key] = serv; //Add the attribute list to the services
             freeBuf();
             if (ServiceAttributeResponse)
                 ServiceAttributeResponse(serv);
@@ -414,7 +428,6 @@
         break;
         //below is UNTESTED
         case 7: { //servicesearchattributeRsp
-            if (tree) delete tree;
             unsigned count = rsp[6] + ((unsigned)rsp[5]<<8);
             append(rsp+7, count);
             cont = rsp[7+count];