Sami Alshorman / Mbed 2 deprecated BluetoothNONIN

Dependencies:   C12832 GPS GSM mbed

Fork of myBlueUSB_localfix by Nobuaki Aoki

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sdp.h Source File

sdp.h

00001 #ifndef SDP_H
00002 #define SDP_H
00003 #include "AvailableMemory.h"
00004 #include "sdp_data.h"
00005 #include <map>
00006 #define OFFSET  8
00007 
00008 class SDPManager;
00009 extern SDPManager SDP;
00010 typedef map<unsigned short, sdp_data*> serv_rec;
00011 
00012 void attribHandler(serv_rec *r);
00013 unsigned parseUUID(const u8* data, int len, unsigned &p);
00014 unsigned length(const unsigned char *el, unsigned &p);
00015 unsigned getval(const unsigned char *p, int n) ;
00016 void errorhandler(unsigned err);//default error handler
00017 
00018 
00019 class SDPHandler: public SocketHandler {
00020 //    int _l2cap;
00021     int sdp_socket;
00022     unsigned char l2cap_buf[100+OFFSET];
00023     unsigned char* buf;
00024     unsigned txid;
00025     unsigned char contState[17];//maximum size, in practive it is 3
00026     unsigned char *contBuf;
00027     unsigned byteCount;
00028     int _state;
00029     sdp_data *tree;//root of the entire service tree
00030     map<unsigned, serv_rec*> services;//the set of supported services <handle, service>
00031     map<unsigned, serv_rec*>::iterator index;
00032 //server properties
00033 //    static map<unsigned, serv_rec*> server;
00034 //    static int serverSock;
00035 public:
00036     SDPHandler();
00037     void Clear();
00038     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr);
00039 //    virtual int Accept(SocketInternal *sock, int scid, int rxid); //called indirectly from BTDevice::Control
00040     virtual int Send(SocketInternal* sock, const u8* data, int len);
00041     virtual int Close(SocketInternal* sock);
00042     virtual char* Name() {
00043         return "SDPHandler SocketHandler";
00044     }
00045     void OnSdpRsp(const u8* data, int len);
00046     static void OnSdpRsp(int socket, SocketState state, const u8* data, int len, void* userData);
00047 
00048     //this function is called when the SDP sockets receives data (see HCICallback in TestShell),
00049     //currently does not happen because not forwarded from OnSdpRsp, can be used to handle multiple connections
00050     static void OnSockCallback(int socket, SocketState state, const u8* data, int len, void* userData) ;
00051     //The SDP server is stateless hence can be static
00052 //    static void SDPServer(int socket, SocketState state, const u8* data, int len, void* userData) ;
00053 
00054     void (*ErrorResponse)(unsigned) ;
00055     void (*ServiceSearchResponse)() ;
00056     void (*ServiceAttributeResponse)(serv_rec*) ;
00057     void (*ServiceSearchAttributeResponse)() ;
00058     int ServiceSearchRequest(sdp_data *sp, unsigned count, unsigned cs=0);
00059     int ServiceAttributeRequest(unsigned handle, unsigned count, sdp_data* al, unsigned cs=0) ;
00060     int ServiceSearchAttributeRequest(sdp_data *sp, unsigned count, sdp_data* al, unsigned cs=0);
00061 //server
00062 //    static int ServiceSearchAttributeReply(unsigned rxid, sdp_data* al, unsigned count, unsigned cs=0);
00063 private:
00064 //    static unsigned length(const unsigned char *el, unsigned &p);
00065 //    static unsigned getval(const unsigned char *p, int n) ;
00066 //    static unsigned parseUUID(const u8* data, int len, unsigned &p);
00067     unsigned parse (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
00068     unsigned parseLight (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
00069     int parseRsp(const unsigned char*rsp, int len) ;
00070     void append(const unsigned char*rsp, int len) ;
00071     void freeBuf();
00072 };
00073 /*
00074 class SDPClient: public SDPHandler {
00075 };
00076 
00077 class SDPServer: public SDPHandler {
00078 };
00079 */
00080 class SDPManager: public SocketHandler {
00081     map<int, SDPHandler*> handlers;
00082 //server properties
00083 //    SDPHandler *Server;
00084     static map<unsigned, serv_rec*> server;
00085     static int serverSock;
00086     bool once;
00087 public:
00088     SDPManager() {
00089         once = true;
00090     }
00091     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr) {
00092         printf("SDPManager::Open(sock (ID=%d, type=%d), addr): should not be called\n", sock->ID, sock->Type);
00093         return sock->ID;//((SDPHandler*)sock->userData)->Open(sock, addr);
00094     }
00095     int Open(SocketAddrHdr* addr) {
00096         L2CAPAddr* ad = (L2CAPAddr*)addr;
00097         ad->psm = L2CAP_PSM_SDP;//open the l2cap channel
00098         SDPHandler *h = new SDPHandler;
00099         int s = Socket_Open(SOCKET_L2CAP, addr, &SDPHandler::OnSdpRsp, h);
00100         handlers[s] = h;
00101         return s;
00102     }
00103     virtual int Accept(SocketInternal *sock, int scid, int rxid) { //called indirectly from BTDevice::Control
00104         if (once) {
00105             once = false;
00106             buildServer();//build the DB on the first connection
00107         }
00108         //sock is registered as an SDP sock but we use it as an L2CAP sock
00109         //type=SDP
00110         //userData = BTDevice
00111         //Internal = L2CAPSocket
00112         BTDevice *l2cap = (BTDevice*)sock->userData;
00113         //sock->dcid = scid
00114         //sock->scid = something based on sock->ID
00115         serverSock = sock->ID;
00116         printf("Invoking accept on %p (%s) for sock %d and scid=%d\n", l2cap, l2cap->Name(), sock->ID, scid);
00117         return l2cap->Accept(sock, scid, rxid);
00118     }
00119     virtual int Send(SocketInternal* sock, const u8* data, int len) {//called by the server
00120         BTDevice *l2cap = (BTDevice*)sock->userData;
00121         return l2cap->Send(sock, data, len);
00122     }
00123     virtual int Close(SocketInternal* sock) {
00124         printf("SDPManager::Close() closing  socket %d\n", sock->ID);
00125         SDPHandler *h = handlers[sock->ID];
00126         int retval = h->Close(sock);
00127         delete h;
00128         handlers[sock->ID] = 0;
00129         return retval;
00130     }
00131     void Destroy(int s) {
00132         printf("Deleting handler for socket %d\n", s);
00133         delete handlers[s];
00134         handlers[s] = 0;
00135     }
00136     virtual char* Name() {
00137         return "SDPManager SocketHandler";
00138     }
00139     //void OnSdpRsp(const u8* data, int len);
00140     static void OnSdpRsp(int socket, SocketState state, const u8* data, int len, void* userData) {
00141         printf("SDPManager::OnSdpRsp(socket %d, state %d, len %d)\n", socket, state, len);
00142     }
00143     //The SDP server is (almost) stateless hence can be static
00144     static void SDPServer(int socket, SocketState state, const u8* data, int len, void* userData) ;
00145     static void match(bool elig[], unsigned uuid);
00146     static bool isInList(unsigned short id, const unsigned char* list, int end);
00147     static void addToReply(sdp_data *svc, serv_rec *list, const unsigned char* att, int end);
00148     static int findUUID(unsigned h, unsigned uuid);
00149     void buildServer();
00150     static int ServiceSearchReply(unsigned rxid, unsigned *handles, unsigned count, unsigned cs=0);
00151     static int ServiceAttributeReply(unsigned rxid, sdp_data* al, unsigned count, unsigned cs=0);
00152     static int ServiceSearchAttributeReply(unsigned rxid, sdp_data* al, unsigned count, unsigned cs=0);
00153     /*
00154         //this function is called when the SDP sockets receives data (see HCICallback in TestShell),
00155         //currently does not happen because not forwarded from OnSdpRsp, can be used to handle multiple connections
00156         static void OnSockCallback(int socket, SocketState state, const u8* data, int len, void* userData) ;
00157 
00158         static void errorhandler(unsigned err);
00159 
00160         void (*ErrorResponse)(unsigned) ;
00161         void (*ServiceSearchResponse)() ;
00162         void (*ServiceAttributeResponse)(serv_rec*) ;
00163         void (*ServiceSearchAttributeResponse)() ;
00164         int ServiceSearchRequest(sdp_data *sp, unsigned count, unsigned cs=0);
00165         int ServiceAttributeRequest(unsigned handle, unsigned count, sdp_data* al, unsigned cs=0) ;
00166         int ServiceSearchAttributeRequest(sdp_data *sp, unsigned count, sdp_data* al, unsigned cs=0);
00167     //server
00168     private:
00169         static unsigned length(const unsigned char *el, unsigned &p);
00170         static unsigned getval(const unsigned char *p, int n) ;
00171         static unsigned parseUUID(const u8* data, int len, unsigned &p);
00172         static void addAttrib(unsigned h, unsigned short id, sdp_data *attrib);
00173         static void addIndex(unsigned h, unsigned uuid);
00174         static int findUUID(unsigned h, unsigned uuid);
00175         static void match(bool elig[], unsigned uuid);
00176         static bool isInList(unsigned short id, const unsigned char* list, int end);
00177         void buildServer();
00178         unsigned parse (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
00179         unsigned parseLight (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
00180         int parseRsp(const unsigned char*rsp, int len) ;
00181         void append(const unsigned char*rsp, int len) ;
00182         void freeBuf();
00183     */
00184 };
00185 
00186 #endif