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
Socket.cpp@0:003889bc474f, 2013-12-07 (annotated)
- Committer:
- nobukuma
- Date:
- Sat Dec 07 14:19:00 2013 +0000
- Revision:
- 0:003889bc474f
http://mbed.org/users/networker/code/myBlueUSB/ rev13??rev12??????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nobukuma | 0:003889bc474f | 1 | /* |
nobukuma | 0:003889bc474f | 2 | Copyright (c) 2010 Peter Barrett |
nobukuma | 0:003889bc474f | 3 | |
nobukuma | 0:003889bc474f | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
nobukuma | 0:003889bc474f | 5 | of this software and associated documentation files (the "Software"), to deal |
nobukuma | 0:003889bc474f | 6 | in the Software without restriction, including without limitation the rights |
nobukuma | 0:003889bc474f | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
nobukuma | 0:003889bc474f | 8 | copies of the Software, and to permit persons to whom the Software is |
nobukuma | 0:003889bc474f | 9 | furnished to do so, subject to the following conditions: |
nobukuma | 0:003889bc474f | 10 | |
nobukuma | 0:003889bc474f | 11 | The above copyright notice and this permission notice shall be included in |
nobukuma | 0:003889bc474f | 12 | all copies or substantial portions of the Software. |
nobukuma | 0:003889bc474f | 13 | |
nobukuma | 0:003889bc474f | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
nobukuma | 0:003889bc474f | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
nobukuma | 0:003889bc474f | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
nobukuma | 0:003889bc474f | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
nobukuma | 0:003889bc474f | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
nobukuma | 0:003889bc474f | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
nobukuma | 0:003889bc474f | 20 | THE SOFTWARE. |
nobukuma | 0:003889bc474f | 21 | */ |
nobukuma | 0:003889bc474f | 22 | |
nobukuma | 0:003889bc474f | 23 | #include <stdio.h> |
nobukuma | 0:003889bc474f | 24 | #include <stdlib.h> |
nobukuma | 0:003889bc474f | 25 | #include <stdio.h> |
nobukuma | 0:003889bc474f | 26 | #include <string.h> |
nobukuma | 0:003889bc474f | 27 | |
nobukuma | 0:003889bc474f | 28 | #include "Utils.h" |
nobukuma | 0:003889bc474f | 29 | #include "Socket.h" |
nobukuma | 0:003889bc474f | 30 | |
nobukuma | 0:003889bc474f | 31 | #define MAX_SOCKET_HANDLERS 4 |
nobukuma | 0:003889bc474f | 32 | #define MAX_SOCKETS 16 |
nobukuma | 0:003889bc474f | 33 | |
nobukuma | 0:003889bc474f | 34 | class SocketInternalPad |
nobukuma | 0:003889bc474f | 35 | { |
nobukuma | 0:003889bc474f | 36 | public: |
nobukuma | 0:003889bc474f | 37 | SocketInternal si; |
nobukuma | 0:003889bc474f | 38 | u8 pad[8]; |
nobukuma | 0:003889bc474f | 39 | }; |
nobukuma | 0:003889bc474f | 40 | |
nobukuma | 0:003889bc474f | 41 | class SocketManager |
nobukuma | 0:003889bc474f | 42 | { |
nobukuma | 0:003889bc474f | 43 | SocketHandler* _handlers[MAX_SOCKET_HANDLERS]; |
nobukuma | 0:003889bc474f | 44 | SocketInternalPad _sockets[MAX_SOCKETS]; |
nobukuma | 0:003889bc474f | 45 | |
nobukuma | 0:003889bc474f | 46 | public: |
nobukuma | 0:003889bc474f | 47 | SocketManager() |
nobukuma | 0:003889bc474f | 48 | { |
nobukuma | 0:003889bc474f | 49 | memset(_handlers,0,sizeof(_handlers)); |
nobukuma | 0:003889bc474f | 50 | memset(_sockets,0,sizeof(_sockets)); |
nobukuma | 0:003889bc474f | 51 | } |
nobukuma | 0:003889bc474f | 52 | |
nobukuma | 0:003889bc474f | 53 | SocketHandler* GetHandler(int type) |
nobukuma | 0:003889bc474f | 54 | { |
nobukuma | 0:003889bc474f | 55 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
nobukuma | 0:003889bc474f | 56 | return 0; |
nobukuma | 0:003889bc474f | 57 | return _handlers[type - 1]; |
nobukuma | 0:003889bc474f | 58 | } |
nobukuma | 0:003889bc474f | 59 | |
nobukuma | 0:003889bc474f | 60 | SocketInternal* GetInternal(int s) |
nobukuma | 0:003889bc474f | 61 | { |
nobukuma | 0:003889bc474f | 62 | if (s < 1 || s > MAX_SOCKETS) |
nobukuma | 0:003889bc474f | 63 | return 0; |
nobukuma | 0:003889bc474f | 64 | return &_sockets[s - 1].si; |
nobukuma | 0:003889bc474f | 65 | } |
nobukuma | 0:003889bc474f | 66 | |
nobukuma | 0:003889bc474f | 67 | int RegisterSocketHandler(int type, SocketHandler* handler) |
nobukuma | 0:003889bc474f | 68 | { |
nobukuma | 0:003889bc474f | 69 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
nobukuma | 0:003889bc474f | 70 | return ERR_SOCKET_TYPE_NOT_FOUND; |
nobukuma | 0:003889bc474f | 71 | _handlers[type - 1] = handler; |
nobukuma | 0:003889bc474f | 72 | return 0; |
nobukuma | 0:003889bc474f | 73 | } |
nobukuma | 0:003889bc474f | 74 | |
nobukuma | 0:003889bc474f | 75 | int Create(int type, SocketCallback callback, void* userData) |
nobukuma | 0:003889bc474f | 76 | { |
nobukuma | 0:003889bc474f | 77 | SocketHandler* h = GetHandler(type); |
nobukuma | 0:003889bc474f | 78 | if (!h) |
nobukuma | 0:003889bc474f | 79 | return ERR_SOCKET_TYPE_NOT_FOUND; |
nobukuma | 0:003889bc474f | 80 | |
nobukuma | 0:003889bc474f | 81 | for (int i = 0; i < MAX_SOCKETS; i++) |
nobukuma | 0:003889bc474f | 82 | { |
nobukuma | 0:003889bc474f | 83 | SocketInternal* si = (SocketInternal*)(_sockets+i); |
nobukuma | 0:003889bc474f | 84 | if (si->ID == 0) |
nobukuma | 0:003889bc474f | 85 | { |
nobukuma | 0:003889bc474f | 86 | si->ID = i+1; |
nobukuma | 0:003889bc474f | 87 | si->Type = type; |
nobukuma | 0:003889bc474f | 88 | si->Callback = callback; |
nobukuma | 0:003889bc474f | 89 | si->userData = userData; |
nobukuma | 0:003889bc474f | 90 | printf("Creating socket %d for type %d, invoking 'Open' on %p (=%s)\n", si->ID, type, h, h->Name()); |
nobukuma | 0:003889bc474f | 91 | return h->Create(si); |
nobukuma | 0:003889bc474f | 92 | } |
nobukuma | 0:003889bc474f | 93 | } |
nobukuma | 0:003889bc474f | 94 | return ERR_SOCKET_NONE_LEFT; |
nobukuma | 0:003889bc474f | 95 | } |
nobukuma | 0:003889bc474f | 96 | |
nobukuma | 0:003889bc474f | 97 | int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
nobukuma | 0:003889bc474f | 98 | { |
nobukuma | 0:003889bc474f | 99 | SocketHandler* h = GetHandler(type); |
nobukuma | 0:003889bc474f | 100 | if (!h) |
nobukuma | 0:003889bc474f | 101 | return ERR_SOCKET_TYPE_NOT_FOUND; |
nobukuma | 0:003889bc474f | 102 | |
nobukuma | 0:003889bc474f | 103 | for (int i = 0; i < MAX_SOCKETS; i++) |
nobukuma | 0:003889bc474f | 104 | { |
nobukuma | 0:003889bc474f | 105 | SocketInternal* si = (SocketInternal*)(_sockets+i); |
nobukuma | 0:003889bc474f | 106 | if (si->ID == 0) |
nobukuma | 0:003889bc474f | 107 | { |
nobukuma | 0:003889bc474f | 108 | si->ID = i+1; |
nobukuma | 0:003889bc474f | 109 | si->Type = type; |
nobukuma | 0:003889bc474f | 110 | si->Callback = callback; |
nobukuma | 0:003889bc474f | 111 | si->userData = userData; |
nobukuma | 0:003889bc474f | 112 | printf("Opening socket %d for type %d, invoking 'Open' on %p (=%s)\n", si->ID, type, h, h->Name()); |
nobukuma | 0:003889bc474f | 113 | return h->Open(si,addr); |
nobukuma | 0:003889bc474f | 114 | } |
nobukuma | 0:003889bc474f | 115 | } |
nobukuma | 0:003889bc474f | 116 | return ERR_SOCKET_NONE_LEFT; |
nobukuma | 0:003889bc474f | 117 | } |
nobukuma | 0:003889bc474f | 118 | |
nobukuma | 0:003889bc474f | 119 | int Listen(int type, int channel, SocketCallback callback, void* userData) |
nobukuma | 0:003889bc474f | 120 | { |
nobukuma | 0:003889bc474f | 121 | SocketHandler* h = GetHandler(type); |
nobukuma | 0:003889bc474f | 122 | if (!h) |
nobukuma | 0:003889bc474f | 123 | return ERR_SOCKET_TYPE_NOT_FOUND; |
nobukuma | 0:003889bc474f | 124 | |
nobukuma | 0:003889bc474f | 125 | for (int i = 0; i < MAX_SOCKETS; i++) |
nobukuma | 0:003889bc474f | 126 | { |
nobukuma | 0:003889bc474f | 127 | SocketInternal* si = (SocketInternal*)(_sockets+i); |
nobukuma | 0:003889bc474f | 128 | if (si->ID == 0) |
nobukuma | 0:003889bc474f | 129 | { |
nobukuma | 0:003889bc474f | 130 | si->ID = i+1; |
nobukuma | 0:003889bc474f | 131 | si->Type = type; |
nobukuma | 0:003889bc474f | 132 | si->Callback = callback; |
nobukuma | 0:003889bc474f | 133 | si->userData = userData; |
nobukuma | 0:003889bc474f | 134 | printf("Passively opening socket %d for type %d, invoking 'Listen' on %p (=%s)\n", si->ID, type, h, h->Name()); |
nobukuma | 0:003889bc474f | 135 | int sn = h->Listen(si, channel); |
nobukuma | 0:003889bc474f | 136 | if (sn < 0) |
nobukuma | 0:003889bc474f | 137 | si->ID = 0;//free the socket when error |
nobukuma | 0:003889bc474f | 138 | return sn; |
nobukuma | 0:003889bc474f | 139 | } |
nobukuma | 0:003889bc474f | 140 | } |
nobukuma | 0:003889bc474f | 141 | return ERR_SOCKET_NONE_LEFT; |
nobukuma | 0:003889bc474f | 142 | } |
nobukuma | 0:003889bc474f | 143 | |
nobukuma | 0:003889bc474f | 144 | int Accept(int type, int scid, int rxid, SocketCallback callback, void* userData) |
nobukuma | 0:003889bc474f | 145 | { |
nobukuma | 0:003889bc474f | 146 | SocketHandler* h = GetHandler(type); |
nobukuma | 0:003889bc474f | 147 | if (!h) |
nobukuma | 0:003889bc474f | 148 | return ERR_SOCKET_TYPE_NOT_FOUND; |
nobukuma | 0:003889bc474f | 149 | |
nobukuma | 0:003889bc474f | 150 | for (int i = 0; i < MAX_SOCKETS; i++) |
nobukuma | 0:003889bc474f | 151 | { |
nobukuma | 0:003889bc474f | 152 | SocketInternal* si = (SocketInternal*)(_sockets+i); |
nobukuma | 0:003889bc474f | 153 | if (si->ID == 0) |
nobukuma | 0:003889bc474f | 154 | { |
nobukuma | 0:003889bc474f | 155 | si->ID = i+1; |
nobukuma | 0:003889bc474f | 156 | si->Type = type; |
nobukuma | 0:003889bc474f | 157 | si->Callback = callback; |
nobukuma | 0:003889bc474f | 158 | si->userData = userData; |
nobukuma | 0:003889bc474f | 159 | printf("Accepting socket %d for type %d, invoking 'Accept' on %p (=%s)\n", si->ID, type, h, h->Name()); |
nobukuma | 0:003889bc474f | 160 | return h->Accept(si, scid, rxid); |
nobukuma | 0:003889bc474f | 161 | } |
nobukuma | 0:003889bc474f | 162 | } |
nobukuma | 0:003889bc474f | 163 | return ERR_SOCKET_NONE_LEFT; |
nobukuma | 0:003889bc474f | 164 | } |
nobukuma | 0:003889bc474f | 165 | |
nobukuma | 0:003889bc474f | 166 | int Send(int socket, const u8* data, int len) |
nobukuma | 0:003889bc474f | 167 | { |
nobukuma | 0:003889bc474f | 168 | SocketInternal* si = GetInternal(socket); |
nobukuma | 0:003889bc474f | 169 | if (!si || si->ID != socket) |
nobukuma | 0:003889bc474f | 170 | return ERR_SOCKET_NOT_FOUND; |
nobukuma | 0:003889bc474f | 171 | // printf("sending %d bytes to socket %d (ID=%d)\n", len, socket, si->ID); |
nobukuma | 0:003889bc474f | 172 | return GetHandler(si->Type)->Send(si,data,len); |
nobukuma | 0:003889bc474f | 173 | } |
nobukuma | 0:003889bc474f | 174 | |
nobukuma | 0:003889bc474f | 175 | int SetOpt(int socket, int so, int* data, int len) |
nobukuma | 0:003889bc474f | 176 | { |
nobukuma | 0:003889bc474f | 177 | SocketInternal* si = GetInternal(socket); |
nobukuma | 0:003889bc474f | 178 | if (!si || si->ID != socket) |
nobukuma | 0:003889bc474f | 179 | return ERR_SOCKET_NOT_FOUND; |
nobukuma | 0:003889bc474f | 180 | return GetHandler(si->Type)->SetOpt(si, so, data,len); |
nobukuma | 0:003889bc474f | 181 | } |
nobukuma | 0:003889bc474f | 182 | |
nobukuma | 0:003889bc474f | 183 | int GetOpt(int socket, int so, int* data, int len) |
nobukuma | 0:003889bc474f | 184 | { |
nobukuma | 0:003889bc474f | 185 | SocketInternal* si = GetInternal(socket); |
nobukuma | 0:003889bc474f | 186 | if (!si || si->ID != socket) |
nobukuma | 0:003889bc474f | 187 | return ERR_SOCKET_NOT_FOUND; |
nobukuma | 0:003889bc474f | 188 | return GetHandler(si->Type)->GetOpt(si, so, data,len); |
nobukuma | 0:003889bc474f | 189 | } |
nobukuma | 0:003889bc474f | 190 | |
nobukuma | 0:003889bc474f | 191 | int Close(int socket) |
nobukuma | 0:003889bc474f | 192 | { |
nobukuma | 0:003889bc474f | 193 | SocketInternal* si = GetInternal(socket); |
nobukuma | 0:003889bc474f | 194 | if (!si || si->ID != socket){ |
nobukuma | 0:003889bc474f | 195 | printf("Close: socket %d not found\n", socket); |
nobukuma | 0:003889bc474f | 196 | return ERR_SOCKET_NOT_FOUND; |
nobukuma | 0:003889bc474f | 197 | } |
nobukuma | 0:003889bc474f | 198 | printf("Close: socket %d (type=%d)\n", socket, si->Type); |
nobukuma | 0:003889bc474f | 199 | si->SetState(SocketState_Closing); |
nobukuma | 0:003889bc474f | 200 | int retval = GetHandler(si->Type)->Close(si); |
nobukuma | 0:003889bc474f | 201 | //si->SetState(Socket_Closed); |
nobukuma | 0:003889bc474f | 202 | //si->ID = 0; |
nobukuma | 0:003889bc474f | 203 | return retval; |
nobukuma | 0:003889bc474f | 204 | } |
nobukuma | 0:003889bc474f | 205 | }; |
nobukuma | 0:003889bc474f | 206 | |
nobukuma | 0:003889bc474f | 207 | SocketManager gSocketManager; |
nobukuma | 0:003889bc474f | 208 | |
nobukuma | 0:003889bc474f | 209 | int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
nobukuma | 0:003889bc474f | 210 | { |
nobukuma | 0:003889bc474f | 211 | return gSocketManager.Open(type,addr,callback,userData); |
nobukuma | 0:003889bc474f | 212 | } |
nobukuma | 0:003889bc474f | 213 | |
nobukuma | 0:003889bc474f | 214 | int Socket_Listen(int type, int channel, SocketCallback callback, void* userData) // Open a socket for listening |
nobukuma | 0:003889bc474f | 215 | { |
nobukuma | 0:003889bc474f | 216 | return gSocketManager.Listen(type,channel,callback,userData); |
nobukuma | 0:003889bc474f | 217 | } |
nobukuma | 0:003889bc474f | 218 | |
nobukuma | 0:003889bc474f | 219 | int Socket_Accept(int type, int scid, int rxid, SocketCallback callback, void* userData) // Open a socket for an incoming connection |
nobukuma | 0:003889bc474f | 220 | { |
nobukuma | 0:003889bc474f | 221 | return gSocketManager.Accept(type,scid,rxid,callback,userData); |
nobukuma | 0:003889bc474f | 222 | } |
nobukuma | 0:003889bc474f | 223 | |
nobukuma | 0:003889bc474f | 224 | int Socket_Send(int socket, const u8* data, int len) |
nobukuma | 0:003889bc474f | 225 | { |
nobukuma | 0:003889bc474f | 226 | return gSocketManager.Send(socket,data,len); |
nobukuma | 0:003889bc474f | 227 | } |
nobukuma | 0:003889bc474f | 228 | |
nobukuma | 0:003889bc474f | 229 | int Socket_SetOpt(int socket, int so, int* data, int len) |
nobukuma | 0:003889bc474f | 230 | { |
nobukuma | 0:003889bc474f | 231 | return gSocketManager.SetOpt(socket, so, data,len); |
nobukuma | 0:003889bc474f | 232 | } |
nobukuma | 0:003889bc474f | 233 | |
nobukuma | 0:003889bc474f | 234 | int Socket_GetOpt(int socket, int so, int* data, int len) |
nobukuma | 0:003889bc474f | 235 | { |
nobukuma | 0:003889bc474f | 236 | return gSocketManager.GetOpt(socket, so, data,len); |
nobukuma | 0:003889bc474f | 237 | } |
nobukuma | 0:003889bc474f | 238 | |
nobukuma | 0:003889bc474f | 239 | int Socket_Close(int socket) |
nobukuma | 0:003889bc474f | 240 | { |
nobukuma | 0:003889bc474f | 241 | return gSocketManager.Close(socket); |
nobukuma | 0:003889bc474f | 242 | } |
nobukuma | 0:003889bc474f | 243 | |
nobukuma | 0:003889bc474f | 244 | int RegisterSocketHandler(int type, SocketHandler* handler) |
nobukuma | 0:003889bc474f | 245 | { |
nobukuma | 0:003889bc474f | 246 | return gSocketManager.RegisterSocketHandler(type,handler); |
nobukuma | 0:003889bc474f | 247 | } |
nobukuma | 0:003889bc474f | 248 | |
nobukuma | 0:003889bc474f | 249 | SocketInternal* GetSocketInternal(int socket) |
nobukuma | 0:003889bc474f | 250 | { |
nobukuma | 0:003889bc474f | 251 | return gSocketManager.GetInternal(socket); |
nobukuma | 0:003889bc474f | 252 | } |
nobukuma | 0:003889bc474f | 253 |