Based on myBlueUSB and rosserial_mbed

Dependencies:   mbed myUSBHost AvailableMemory myBlueUSB

Committer:
OTL
Date:
Sat Sep 17 14:24:13 2011 +0000
Revision:
1:18139954944b
Parent:
0:7684b95768c7
remove m3pi and main

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OTL 0:7684b95768c7 1 #ifndef SDP_H
OTL 0:7684b95768c7 2 #define SDP_H
OTL 0:7684b95768c7 3 #include "AvailableMemory.h"
OTL 0:7684b95768c7 4 #include "sdp_data.h"
OTL 0:7684b95768c7 5 #include <map>
OTL 0:7684b95768c7 6 #define OFFSET 8
OTL 0:7684b95768c7 7
OTL 0:7684b95768c7 8 class SDPManager;
OTL 0:7684b95768c7 9 extern SDPManager SDP;
OTL 0:7684b95768c7 10 typedef map<unsigned short, sdp_data*> serv_rec;
OTL 0:7684b95768c7 11
OTL 0:7684b95768c7 12 void attribHandler(serv_rec *r);
OTL 0:7684b95768c7 13 unsigned parseUUID(const u8* data, int len, unsigned &p);
OTL 0:7684b95768c7 14 unsigned length(const unsigned char *el, unsigned &p);
OTL 0:7684b95768c7 15 unsigned getval(const unsigned char *p, int n) ;
OTL 0:7684b95768c7 16 void errorhandler(unsigned err);//default error handler
OTL 0:7684b95768c7 17
OTL 0:7684b95768c7 18
OTL 0:7684b95768c7 19 class SDPHandler: public SocketHandler {
OTL 0:7684b95768c7 20 // int _l2cap;
OTL 0:7684b95768c7 21 int sdp_socket;
OTL 0:7684b95768c7 22 unsigned char l2cap_buf[100+OFFSET];
OTL 0:7684b95768c7 23 unsigned char* buf;
OTL 0:7684b95768c7 24 unsigned txid;
OTL 0:7684b95768c7 25 unsigned char contState[17];//maximum size, in practive it is 3
OTL 0:7684b95768c7 26 unsigned char *contBuf;
OTL 0:7684b95768c7 27 unsigned byteCount;
OTL 0:7684b95768c7 28 int _state;
OTL 0:7684b95768c7 29 sdp_data *tree;//root of the entire service tree
OTL 0:7684b95768c7 30 map<unsigned, serv_rec*> services;//the set of supported services <handle, service>
OTL 0:7684b95768c7 31 map<unsigned, serv_rec*>::iterator index;
OTL 0:7684b95768c7 32 //server properties
OTL 0:7684b95768c7 33 // static map<unsigned, serv_rec*> server;
OTL 0:7684b95768c7 34 // static int serverSock;
OTL 0:7684b95768c7 35 public:
OTL 0:7684b95768c7 36 SDPHandler();
OTL 0:7684b95768c7 37 void Clear();
OTL 0:7684b95768c7 38 virtual int Open(SocketInternal* sock, SocketAddrHdr* addr);
OTL 0:7684b95768c7 39 // virtual int Accept(SocketInternal *sock, int scid, int rxid); //called indirectly from BTDevice::Control
OTL 0:7684b95768c7 40 virtual int Send(SocketInternal* sock, const u8* data, int len);
OTL 0:7684b95768c7 41 virtual int Close(SocketInternal* sock);
OTL 0:7684b95768c7 42 virtual char* Name() {
OTL 0:7684b95768c7 43 return "SDPHandler SocketHandler";
OTL 0:7684b95768c7 44 }
OTL 0:7684b95768c7 45 void OnSdpRsp(const u8* data, int len);
OTL 0:7684b95768c7 46 static void OnSdpRsp(int socket, SocketState state, const u8* data, int len, void* userData);
OTL 0:7684b95768c7 47
OTL 0:7684b95768c7 48 //this function is called when the SDP sockets receives data (see HCICallback in TestShell),
OTL 0:7684b95768c7 49 //currently does not happen because not forwarded from OnSdpRsp, can be used to handle multiple connections
OTL 0:7684b95768c7 50 static void OnSockCallback(int socket, SocketState state, const u8* data, int len, void* userData) ;
OTL 0:7684b95768c7 51 //The SDP server is stateless hence can be static
OTL 0:7684b95768c7 52 // static void SDPServer(int socket, SocketState state, const u8* data, int len, void* userData) ;
OTL 0:7684b95768c7 53
OTL 0:7684b95768c7 54 void (*ErrorResponse)(unsigned) ;
OTL 0:7684b95768c7 55 void (*ServiceSearchResponse)() ;
OTL 0:7684b95768c7 56 void (*ServiceAttributeResponse)(serv_rec*) ;
OTL 0:7684b95768c7 57 void (*ServiceSearchAttributeResponse)() ;
OTL 0:7684b95768c7 58 int ServiceSearchRequest(sdp_data *sp, unsigned count, unsigned cs=0);
OTL 0:7684b95768c7 59 int ServiceAttributeRequest(unsigned handle, unsigned count, sdp_data* al, unsigned cs=0) ;
OTL 0:7684b95768c7 60 int ServiceSearchAttributeRequest(sdp_data *sp, unsigned count, sdp_data* al, unsigned cs=0);
OTL 0:7684b95768c7 61 //server
OTL 0:7684b95768c7 62 // static int ServiceSearchAttributeReply(unsigned rxid, sdp_data* al, unsigned count, unsigned cs=0);
OTL 0:7684b95768c7 63 private:
OTL 0:7684b95768c7 64 // static unsigned length(const unsigned char *el, unsigned &p);
OTL 0:7684b95768c7 65 // static unsigned getval(const unsigned char *p, int n) ;
OTL 0:7684b95768c7 66 // static unsigned parseUUID(const u8* data, int len, unsigned &p);
OTL 0:7684b95768c7 67 unsigned parse (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
OTL 0:7684b95768c7 68 unsigned parseLight (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
OTL 0:7684b95768c7 69 int parseRsp(const unsigned char*rsp, int len) ;
OTL 0:7684b95768c7 70 void append(const unsigned char*rsp, int len) ;
OTL 0:7684b95768c7 71 void freeBuf();
OTL 0:7684b95768c7 72 };
OTL 0:7684b95768c7 73 /*
OTL 0:7684b95768c7 74 class SDPClient: public SDPHandler {
OTL 0:7684b95768c7 75 };
OTL 0:7684b95768c7 76
OTL 0:7684b95768c7 77 class SDPServer: public SDPHandler {
OTL 0:7684b95768c7 78 };
OTL 0:7684b95768c7 79 */
OTL 0:7684b95768c7 80 class SDPManager: public SocketHandler {
OTL 0:7684b95768c7 81 map<int, SDPHandler*> handlers;
OTL 0:7684b95768c7 82 //server properties
OTL 0:7684b95768c7 83 // SDPHandler *Server;
OTL 0:7684b95768c7 84 static map<unsigned, serv_rec*> server;
OTL 0:7684b95768c7 85 static int serverSock;
OTL 0:7684b95768c7 86 bool once;
OTL 0:7684b95768c7 87 public:
OTL 0:7684b95768c7 88 SDPManager() {
OTL 0:7684b95768c7 89 once = true;
OTL 0:7684b95768c7 90 }
OTL 0:7684b95768c7 91 virtual int Open(SocketInternal* sock, SocketAddrHdr* addr) {
OTL 0:7684b95768c7 92 printf("SDPManager::Open(sock (ID=%d, type=%d), addr): should not be called\n", sock->ID, sock->Type);
OTL 0:7684b95768c7 93 return sock->ID;//((SDPHandler*)sock->userData)->Open(sock, addr);
OTL 0:7684b95768c7 94 }
OTL 0:7684b95768c7 95 int Open(SocketAddrHdr* addr) {
OTL 0:7684b95768c7 96 L2CAPAddr* ad = (L2CAPAddr*)addr;
OTL 0:7684b95768c7 97 ad->psm = L2CAP_PSM_SDP;//open the l2cap channel
OTL 0:7684b95768c7 98 SDPHandler *h = new SDPHandler;
OTL 0:7684b95768c7 99 int s = Socket_Open(SOCKET_L2CAP, addr, &SDPHandler::OnSdpRsp, h);
OTL 0:7684b95768c7 100 handlers[s] = h;
OTL 0:7684b95768c7 101 return s;
OTL 0:7684b95768c7 102 }
OTL 0:7684b95768c7 103 virtual int Accept(SocketInternal *sock, int scid, int rxid) { //called indirectly from BTDevice::Control
OTL 0:7684b95768c7 104 if (once) {
OTL 0:7684b95768c7 105 once = false;
OTL 0:7684b95768c7 106 buildServer();//build the DB on the first connection
OTL 0:7684b95768c7 107 }
OTL 0:7684b95768c7 108 //sock is registered as an SDP sock but we use it as an L2CAP sock
OTL 0:7684b95768c7 109 //type=SDP
OTL 0:7684b95768c7 110 //userData = BTDevice
OTL 0:7684b95768c7 111 //Internal = L2CAPSocket
OTL 0:7684b95768c7 112 BTDevice *l2cap = (BTDevice*)sock->userData;
OTL 0:7684b95768c7 113 //sock->dcid = scid
OTL 0:7684b95768c7 114 //sock->scid = something based on sock->ID
OTL 0:7684b95768c7 115 serverSock = sock->ID;
OTL 0:7684b95768c7 116 printf("Invoking accept on %p (%s) for sock %d and scid=%d\n", l2cap, l2cap->Name(), sock->ID, scid);
OTL 0:7684b95768c7 117 return l2cap->Accept(sock, scid, rxid);
OTL 0:7684b95768c7 118 }
OTL 0:7684b95768c7 119 virtual int Send(SocketInternal* sock, const u8* data, int len) {//called by the server
OTL 0:7684b95768c7 120 BTDevice *l2cap = (BTDevice*)sock->userData;
OTL 0:7684b95768c7 121 return l2cap->Send(sock, data, len);
OTL 0:7684b95768c7 122 }
OTL 0:7684b95768c7 123 virtual int Close(SocketInternal* sock) {
OTL 0:7684b95768c7 124 printf("SDPManager::Close() closing socket %d\n", sock->ID);
OTL 0:7684b95768c7 125 SDPHandler *h = handlers[sock->ID];
OTL 0:7684b95768c7 126 int retval = h->Close(sock);
OTL 0:7684b95768c7 127 delete h;
OTL 0:7684b95768c7 128 handlers[sock->ID] = 0;
OTL 0:7684b95768c7 129 return retval;
OTL 0:7684b95768c7 130 }
OTL 0:7684b95768c7 131 void Destroy(int s) {
OTL 0:7684b95768c7 132 printf("Deleting handler for socket %d\n", s);
OTL 0:7684b95768c7 133 delete handlers[s];
OTL 0:7684b95768c7 134 handlers[s] = 0;
OTL 0:7684b95768c7 135 }
OTL 0:7684b95768c7 136 virtual char* Name() {
OTL 0:7684b95768c7 137 return "SDPManager SocketHandler";
OTL 0:7684b95768c7 138 }
OTL 0:7684b95768c7 139 //void OnSdpRsp(const u8* data, int len);
OTL 0:7684b95768c7 140 static void OnSdpRsp(int socket, SocketState state, const u8* data, int len, void* userData) {
OTL 0:7684b95768c7 141 printf("SDPManager::OnSdpRsp(socket %d, state %d, len %d)\n", socket, state, len);
OTL 0:7684b95768c7 142 }
OTL 0:7684b95768c7 143 //The SDP server is (almost) stateless hence can be static
OTL 0:7684b95768c7 144 static void SDPServer(int socket, SocketState state, const u8* data, int len, void* userData) ;
OTL 0:7684b95768c7 145 static void match(bool elig[], unsigned uuid);
OTL 0:7684b95768c7 146 static bool isInList(unsigned short id, const unsigned char* list, int end);
OTL 0:7684b95768c7 147 static void addToReply(sdp_data *svc, serv_rec *list, const unsigned char* att, int end);
OTL 0:7684b95768c7 148 static int findUUID(unsigned h, unsigned uuid);
OTL 0:7684b95768c7 149 void buildServer();
OTL 0:7684b95768c7 150 static int ServiceSearchReply(unsigned rxid, unsigned *handles, unsigned count, unsigned cs=0);
OTL 0:7684b95768c7 151 static int ServiceAttributeReply(unsigned rxid, sdp_data* al, unsigned count, unsigned cs=0);
OTL 0:7684b95768c7 152 static int ServiceSearchAttributeReply(unsigned rxid, sdp_data* al, unsigned count, unsigned cs=0);
OTL 0:7684b95768c7 153 /*
OTL 0:7684b95768c7 154 //this function is called when the SDP sockets receives data (see HCICallback in TestShell),
OTL 0:7684b95768c7 155 //currently does not happen because not forwarded from OnSdpRsp, can be used to handle multiple connections
OTL 0:7684b95768c7 156 static void OnSockCallback(int socket, SocketState state, const u8* data, int len, void* userData) ;
OTL 0:7684b95768c7 157
OTL 0:7684b95768c7 158 static void errorhandler(unsigned err);
OTL 0:7684b95768c7 159
OTL 0:7684b95768c7 160 void (*ErrorResponse)(unsigned) ;
OTL 0:7684b95768c7 161 void (*ServiceSearchResponse)() ;
OTL 0:7684b95768c7 162 void (*ServiceAttributeResponse)(serv_rec*) ;
OTL 0:7684b95768c7 163 void (*ServiceSearchAttributeResponse)() ;
OTL 0:7684b95768c7 164 int ServiceSearchRequest(sdp_data *sp, unsigned count, unsigned cs=0);
OTL 0:7684b95768c7 165 int ServiceAttributeRequest(unsigned handle, unsigned count, sdp_data* al, unsigned cs=0) ;
OTL 0:7684b95768c7 166 int ServiceSearchAttributeRequest(sdp_data *sp, unsigned count, sdp_data* al, unsigned cs=0);
OTL 0:7684b95768c7 167 //server
OTL 0:7684b95768c7 168 private:
OTL 0:7684b95768c7 169 static unsigned length(const unsigned char *el, unsigned &p);
OTL 0:7684b95768c7 170 static unsigned getval(const unsigned char *p, int n) ;
OTL 0:7684b95768c7 171 static unsigned parseUUID(const u8* data, int len, unsigned &p);
OTL 0:7684b95768c7 172 static void addAttrib(unsigned h, unsigned short id, sdp_data *attrib);
OTL 0:7684b95768c7 173 static void addIndex(unsigned h, unsigned uuid);
OTL 0:7684b95768c7 174 static int findUUID(unsigned h, unsigned uuid);
OTL 0:7684b95768c7 175 static void match(bool elig[], unsigned uuid);
OTL 0:7684b95768c7 176 static bool isInList(unsigned short id, const unsigned char* list, int end);
OTL 0:7684b95768c7 177 void buildServer();
OTL 0:7684b95768c7 178 unsigned parse (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
OTL 0:7684b95768c7 179 unsigned parseLight (const unsigned char *el, unsigned count, sdp_data* &result, serv_rec* &record) ;
OTL 0:7684b95768c7 180 int parseRsp(const unsigned char*rsp, int len) ;
OTL 0:7684b95768c7 181 void append(const unsigned char*rsp, int len) ;
OTL 0:7684b95768c7 182 void freeBuf();
OTL 0:7684b95768c7 183 */
OTL 0:7684b95768c7 184 };
OTL 0:7684b95768c7 185
OTL 0:7684b95768c7 186 #endif