Pulse Oximeter (NONIN) communicates with mbed via Bluetooth dongle and sends Heart Rate and Oxygen Saturation via GPRS module

Dependencies:   C12832 GPS GSM mbed

Fork of myBlueUSB_localfix by Nobuaki Aoki

Committer:
samialshorman
Date:
Tue Apr 14 21:48:07 2015 +0000
Revision:
3:55a622e3dbb5
Parent:
0:003889bc474f
Nonin (Pulse Oximeter) connected to mbed lpc 1768 by Bluetooth dongle and sends SMS including Heart Rate and Oxygen saturation by GPRS module

Who changed what in which revision?

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