Ad van der Weiden / sdp

Dependents:   mbed_TANK_Kinect ftusbClass

Revision:
2:d5a27b2d2e08
Parent:
1:70ee392bcfd4
Child:
3:e8d2ebb7392e
--- a/sdp.h	Wed Apr 06 18:09:20 2011 +0000
+++ b/sdp.h	Thu Apr 07 21:01:39 2011 +0000
@@ -10,6 +10,8 @@
 
 void attribHandler(serv_rec *r);
 
+//TODO: add support for multiple connections
+//TODO: add server support, now only client
 //at the moment, SDP can handle only one connection at a time
 class SDPManager: public SocketHandler {
     int _l2cap;
@@ -37,18 +39,24 @@
         _state = 0;
     }
 
+    void Clear() {
+        for (index = services.begin(); index != services.end(); index++) {//for all services
+            for (serv_rec::iterator it = index->second->begin(); it != index->second->end(); it++)//for all attributes
+                delete it->second; //delete the attribute value tree
+            delete (*index).second; //delete the attribute list
+        }
+        services.clear();//and empty the services list
+    }
+
     //Called as: Socket_Open(SOCKET_SDP, addr, callback, userdata(this))
     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr) {
         L2CAPAddr* ad = (L2CAPAddr*)addr;
         if (_l2cap) {
-          printf("This SDP supports only one connection at a time\n");
-          return 0;
+            printf("This SDP supports only one connection at a time\n");
+            return 0;
         }
         //BD_ADDR* a = &ad->bdaddr;
         sdp_socket = sock->ID;
-        for (index = services.begin(); index != services.end(); index++)
-           delete (*index).second;
-        services.clear();
         ad->psm = L2CAP_PSM_SDP;//open the l2cap channel
         _l2cap = Socket_Open(SOCKET_L2CAP, addr, OnSdpRsp, this);//this is the socket between SDP and the L2CAP layer
         if (_l2cap <= 0) {
@@ -65,8 +73,11 @@
     }
 
     virtual int Close(SocketInternal* sock) {
-        printf("SDP socket %d and L2CAP socket %d closed\n", sock->ID, _l2cap);
-        return Socket_Close(_l2cap);
+        Clear();
+        printf("SDP socket %d and L2CAP socket %d closed, freemem=%d\n", sock->ID, _l2cap, AvailableMemory(1));
+        int retval = Socket_Close(_l2cap);//could also keep it open for the next connection
+        _l2cap = 0;
+        return retval;
     }
 
     virtual char* Name() {
@@ -77,15 +88,15 @@
     //this function is called when the L2CAP layer receives SDP packets (see SDPManager::Open), userdata is the sdpmanager instance
     static void OnSdpRsp(int socket, SocketState state, const u8* data, int len, void* userData) {
         printf("\x1B[%dm", 35);
-        printf("OnSdpRsp(socket=%d, state=%d, len=%d)\n", socket, state, len);
-//        printf("OnSdpRsp(socket=%d, state=%d, len=%d, freemem=%d)\n", socket, state, len, AvailableMemory(1));
+//        printf("OnSdpRsp(socket=%d, state=%d, len=%d)\n", socket, state, len);
+        printf("OnSdpRsp(socket=%d, state=%d, len=%d, freemem=%d\n", socket, state, len, AvailableMemory(1));
         SDPManager *self = (SDPManager*)userData;
-        if (state == SocketState_Open){
+        if (state == SocketState_Open) {
             self->OnSdpRsp(data, len);
         }
         printf("\x1B[%dm", 0);
     }
-    
+
     //this function is called when the SDP sockets receives data (see HCICallback in TestShell),
     //currently does not happen because not forwarded from OnSdpRsp, can be used to handle multiple connections
     static void OnSockCallback(int socket, SocketState state, const u8* data, int len, void* userData) ;