SDP client for myBlueUSB

Dependents:   mbed_TANK_Kinect ftusbClass

Revision:
0:7493bf6bb1b9
Child:
1:70ee392bcfd4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sdp.h	Mon Apr 04 16:45:20 2011 +0000
@@ -0,0 +1,89 @@
+#ifndef SDP_H
+#define SDP_H
+
+#include <map>
+#define OFFSET  8
+
+class SDPManager;
+extern SDPManager SDP;
+typedef map<unsigned short, sdp_data*> serv_rec;
+
+void attribHandler(serv_rec *r);
+
+//at the moment, SDP can handle only one connection at a time
+class SDPManager: public SocketHandler {
+    int _l2cap;
+    int sdp_socket; //at the moment the only socket
+    unsigned char l2cap_buf[100+OFFSET];
+    unsigned char* buf;
+    unsigned txid;
+    sdp_data *tree;//root of the entire service tree
+    map<unsigned, serv_rec*> services;//the set of supported services <handle, service>
+    map<unsigned, serv_rec*>::iterator index;
+public:
+    SDPManager(): _l2cap(0), txid(1), tree(0) {
+        ErrorResponse=errorhandler;
+        ServiceSearchResponse=0;
+        ServiceAttributeResponse=attribHandler;
+        ServiceSearchAttributeResponse=0;
+        buf = l2cap_buf+OFFSET;
+    }
+
+    //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;
+          }
+        //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) {
+            printf("Opening L2CAP channel failed\n");
+            return _l2cap;
+        }
+        printf("Successfully opened L2CAP channel for SDP on socket %d\n", _l2cap);
+        return sock->ID;
+    }
+
+    virtual int Send(SocketInternal* sock, const u8* data, int len) {
+        printf("SDPManager::Send should not be called directly\n");
+        return Socket_Send(_l2cap, data, len);
+    }
+
+    virtual int Close(SocketInternal* sock) {
+        printf("SDP socket %d and L2CAP socket %d closed\n", sock->ID, _l2cap);
+        return Socket_Close(_l2cap);
+    }
+
+    virtual char* Name() {
+        return "SDPManager SocketHandler";
+    }
+
+    //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) ;
+    //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) ;
+    static void errorhandler(unsigned err);
+
+    void (*ErrorResponse)(unsigned) ;
+    void (*ServiceSearchResponse)() ;
+    void (*ServiceAttributeResponse)(serv_rec*) ;
+    void (*ServiceSearchAttributeResponse)() ;
+    int ServiceSearchRequest(sdp_data *sp, unsigned count, unsigned cs=0);
+    int ServiceAttributeRequest(unsigned handle, unsigned count, sdp_data* al, unsigned cs=0) ;
+    int ServiceSearchAttributeRequest(sdp_data *sp, unsigned count, sdp_data* al, unsigned cs=0);
+private:
+    unsigned length(const unsigned char *el, unsigned &p);
+    unsigned getval(const unsigned char *p, int n) ;
+    unsigned parse (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
+    int parseRsp(const unsigned char*rsp, int len) ;
+};
+
+#endif
\ No newline at end of file