かいださん

Dependencies:   mbed

Fork of 2016_Spring_kaida by 新部内対抗5班

Committer:
abcdefgh
Date:
Fri Jun 10 17:29:55 2016 +0000
Revision:
18:cab2885dc8fe
Parent:
15:5e973e6c058c
2016/06/10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hirokimineshita 0:736c76a75def 1 /*
hirokimineshita 0:736c76a75def 2 Copyright (c) 2010 Peter Barrett
hirokimineshita 0:736c76a75def 3
hirokimineshita 0:736c76a75def 4 Permission is hereby granted, free of charge, to any person obtaining a copy
hirokimineshita 0:736c76a75def 5 of this software and associated documentation files (the "Software"), to deal
hirokimineshita 0:736c76a75def 6 in the Software without restriction, including without limitation the rights
hirokimineshita 0:736c76a75def 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hirokimineshita 0:736c76a75def 8 copies of the Software, and to permit persons to whom the Software is
hirokimineshita 0:736c76a75def 9 furnished to do so, subject to the following conditions:
hirokimineshita 0:736c76a75def 10
hirokimineshita 0:736c76a75def 11 The above copyright notice and this permission notice shall be included in
hirokimineshita 0:736c76a75def 12 all copies or substantial portions of the Software.
hirokimineshita 0:736c76a75def 13
hirokimineshita 0:736c76a75def 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hirokimineshita 0:736c76a75def 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hirokimineshita 0:736c76a75def 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hirokimineshita 0:736c76a75def 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hirokimineshita 0:736c76a75def 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hirokimineshita 0:736c76a75def 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hirokimineshita 0:736c76a75def 20 THE SOFTWARE.
hirokimineshita 0:736c76a75def 21 */
hirokimineshita 0:736c76a75def 22
abcdefgh 15:5e973e6c058c 23 /*
hirokimineshita 0:736c76a75def 24 Tue Apr 26 2011 Bart Janssens: added a socket listener
hirokimineshita 0:736c76a75def 25 */
hirokimineshita 0:736c76a75def 26
hirokimineshita 0:736c76a75def 27 #include <stdio.h>
hirokimineshita 0:736c76a75def 28 #include <stdlib.h>
hirokimineshita 0:736c76a75def 29 #include <stdio.h>
hirokimineshita 0:736c76a75def 30 #include <string.h>
hirokimineshita 0:736c76a75def 31
hirokimineshita 0:736c76a75def 32 #include "Utils.h"
hirokimineshita 0:736c76a75def 33 #include "Socket.h"
hirokimineshita 0:736c76a75def 34
hirokimineshita 0:736c76a75def 35 #define MAX_SOCKET_HANDLERS 3
hirokimineshita 0:736c76a75def 36 #define MAX_SOCKETS 16
hirokimineshita 0:736c76a75def 37 #define MAX_LISTEN 8
hirokimineshita 0:736c76a75def 38
hirokimineshita 0:736c76a75def 39 class SocketInternalPad
hirokimineshita 0:736c76a75def 40 {
abcdefgh 15:5e973e6c058c 41 public:
hirokimineshita 0:736c76a75def 42 SocketInternal si;
hirokimineshita 0:736c76a75def 43 u8 pad[8];
hirokimineshita 0:736c76a75def 44 };
hirokimineshita 0:736c76a75def 45
hirokimineshita 0:736c76a75def 46
abcdefgh 15:5e973e6c058c 47
hirokimineshita 0:736c76a75def 48
hirokimineshita 0:736c76a75def 49 class SocketManager
hirokimineshita 0:736c76a75def 50 {
hirokimineshita 0:736c76a75def 51 SocketHandler* _handlers[MAX_SOCKET_HANDLERS];
hirokimineshita 0:736c76a75def 52 SocketInternalPad _sockets[MAX_SOCKETS];
hirokimineshita 0:736c76a75def 53 SocketListener _listeners[MAX_LISTEN];
hirokimineshita 0:736c76a75def 54
abcdefgh 15:5e973e6c058c 55 public:
abcdefgh 15:5e973e6c058c 56 SocketManager() {
hirokimineshita 0:736c76a75def 57 memset(_handlers,0,sizeof(_handlers));
hirokimineshita 0:736c76a75def 58 memset(_sockets,0,sizeof(_sockets));
hirokimineshita 0:736c76a75def 59 memset(_listeners,0,sizeof(_listeners));
hirokimineshita 0:736c76a75def 60 }
hirokimineshita 0:736c76a75def 61
abcdefgh 15:5e973e6c058c 62 SocketHandler* GetHandler(int type) {
hirokimineshita 0:736c76a75def 63 if (type < 1 || type > MAX_SOCKET_HANDLERS)
hirokimineshita 0:736c76a75def 64 return 0;
hirokimineshita 0:736c76a75def 65 return _handlers[type - 1];
hirokimineshita 0:736c76a75def 66 }
hirokimineshita 0:736c76a75def 67
abcdefgh 15:5e973e6c058c 68 SocketInternal* GetInternal(int s) {
hirokimineshita 0:736c76a75def 69 if (s < 1 || s > MAX_SOCKETS)
hirokimineshita 0:736c76a75def 70 return 0;
hirokimineshita 0:736c76a75def 71 return &_sockets[s - 1].si;
hirokimineshita 0:736c76a75def 72 }
hirokimineshita 0:736c76a75def 73
abcdefgh 15:5e973e6c058c 74 int RegisterSocketHandler(int type, SocketHandler* handler) {
abcdefgh 15:5e973e6c058c 75 if (type < 1 || type > MAX_SOCKET_HANDLERS)
hirokimineshita 0:736c76a75def 76 return ERR_SOCKET_TYPE_NOT_FOUND;
hirokimineshita 0:736c76a75def 77 _handlers[type - 1] = handler;
hirokimineshita 0:736c76a75def 78 return 0;
hirokimineshita 0:736c76a75def 79 }
hirokimineshita 0:736c76a75def 80
abcdefgh 15:5e973e6c058c 81 int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) {
hirokimineshita 0:736c76a75def 82 SocketHandler* h = GetHandler(type);
hirokimineshita 0:736c76a75def 83 if (!h)
hirokimineshita 0:736c76a75def 84 return ERR_SOCKET_TYPE_NOT_FOUND;
abcdefgh 15:5e973e6c058c 85
abcdefgh 15:5e973e6c058c 86 for (int i = 0; i < MAX_SOCKETS; i++) {
hirokimineshita 0:736c76a75def 87 SocketInternal* si = (SocketInternal*)(_sockets+i);
abcdefgh 15:5e973e6c058c 88 if (si->ID == 0) {
hirokimineshita 0:736c76a75def 89 //printf("Call to Socket Manager Open \r\n");
hirokimineshita 0:736c76a75def 90 si->ID = i+1;
hirokimineshita 0:736c76a75def 91 si->Type = type;
hirokimineshita 0:736c76a75def 92 si->Callback = callback;
hirokimineshita 0:736c76a75def 93 si->userData = userData;
hirokimineshita 0:736c76a75def 94 return h->Open(si,addr);
hirokimineshita 0:736c76a75def 95 }
hirokimineshita 0:736c76a75def 96 }
hirokimineshita 0:736c76a75def 97 return ERR_SOCKET_NONE_LEFT;
hirokimineshita 0:736c76a75def 98 }
abcdefgh 15:5e973e6c058c 99
abcdefgh 15:5e973e6c058c 100 SocketInternal* Create(int type, SocketAddrHdr* addr, int port) {
hirokimineshita 0:736c76a75def 101 SocketInternal* si;
hirokimineshita 0:736c76a75def 102 SocketListener* li;
hirokimineshita 0:736c76a75def 103 SocketHandler* h = GetHandler(type);
hirokimineshita 0:736c76a75def 104 if (!h)
hirokimineshita 0:736c76a75def 105 return 0;
abcdefgh 15:5e973e6c058c 106
abcdefgh 15:5e973e6c058c 107 for (int i = 0; i < MAX_SOCKETS; i++) {
hirokimineshita 0:736c76a75def 108 si = (SocketInternal*)(_sockets+i);
abcdefgh 15:5e973e6c058c 109 if (si->ID == 0) {
hirokimineshita 0:736c76a75def 110 si->ID = i+1;
hirokimineshita 0:736c76a75def 111 si->State = SocketState_Listen;
hirokimineshita 0:736c76a75def 112 si->Type = type;
hirokimineshita 0:736c76a75def 113 si->port = port;
abcdefgh 15:5e973e6c058c 114 for (int i = 0; i < MAX_LISTEN; i++) {
hirokimineshita 0:736c76a75def 115 li = (SocketListener*)(_listeners+i);
hirokimineshita 0:736c76a75def 116 if (( li->Type == si->Type )&& (li->port == si->port)) {
hirokimineshita 0:736c76a75def 117 si->Callback = li->Callback;
hirokimineshita 0:736c76a75def 118 si->userData = li->userData;
hirokimineshita 0:736c76a75def 119 h->Create(si,addr);
hirokimineshita 0:736c76a75def 120 return si;
hirokimineshita 0:736c76a75def 121 }
hirokimineshita 0:736c76a75def 122
hirokimineshita 0:736c76a75def 123 }
abcdefgh 15:5e973e6c058c 124 }
hirokimineshita 0:736c76a75def 125 }
hirokimineshita 0:736c76a75def 126
hirokimineshita 0:736c76a75def 127 }
abcdefgh 15:5e973e6c058c 128
abcdefgh 15:5e973e6c058c 129
abcdefgh 15:5e973e6c058c 130 int Listen(int type, int port, SocketCallback callback,void* userData) {
hirokimineshita 0:736c76a75def 131 SocketListener* li;
hirokimineshita 0:736c76a75def 132 SocketHandler* h = GetHandler(type);
hirokimineshita 0:736c76a75def 133 if (!h) return ERR_SOCKET_TYPE_NOT_FOUND;
abcdefgh 15:5e973e6c058c 134
hirokimineshita 0:736c76a75def 135 //printf("Call to Socket Manager Listen \r\n");
abcdefgh 15:5e973e6c058c 136 for (int i = 0; i < MAX_LISTEN; i++) {
hirokimineshita 0:736c76a75def 137 li = (SocketListener*)(_listeners+i);
hirokimineshita 0:736c76a75def 138 if (( li->Type == type )&& (li->port == port)) {
abcdefgh 15:5e973e6c058c 139 //printf("Port %d is already in use\r\n",port);
abcdefgh 15:5e973e6c058c 140 return ERR_SOCKET_IN_USE; //in use
hirokimineshita 0:736c76a75def 141 }
hirokimineshita 0:736c76a75def 142 }
abcdefgh 15:5e973e6c058c 143
abcdefgh 15:5e973e6c058c 144 for (int i = 0; i < MAX_LISTEN; i++) {
hirokimineshita 0:736c76a75def 145 li = (SocketListener*)(_listeners+i);
hirokimineshita 0:736c76a75def 146 if (( li->Type == 0 )&& (li->port == 0)) {
hirokimineshita 0:736c76a75def 147 li->ID = i+1;
hirokimineshita 0:736c76a75def 148 li->Type = type;
hirokimineshita 0:736c76a75def 149 li->port = port;
hirokimineshita 0:736c76a75def 150 li->Callback = callback;
hirokimineshita 0:736c76a75def 151 li->userData = userData;
hirokimineshita 0:736c76a75def 152 //printf("Listening on port %d \r\n",port);
hirokimineshita 0:736c76a75def 153 return 0;
hirokimineshita 0:736c76a75def 154 }
hirokimineshita 0:736c76a75def 155 }
hirokimineshita 0:736c76a75def 156 //printf("Max listen ports reached\r\n",port);
abcdefgh 15:5e973e6c058c 157 return ERR_SOCKET_NONE_LEFT;
hirokimineshita 0:736c76a75def 158 }
abcdefgh 15:5e973e6c058c 159
abcdefgh 15:5e973e6c058c 160 int InUse(int type, int port) {
hirokimineshita 0:736c76a75def 161 SocketListener* li;
hirokimineshita 0:736c76a75def 162 SocketHandler* h = GetHandler(type);
hirokimineshita 0:736c76a75def 163 if (!h) return ERR_SOCKET_TYPE_NOT_FOUND;
abcdefgh 15:5e973e6c058c 164 for (int i = 0; i < MAX_LISTEN; i++) {
hirokimineshita 0:736c76a75def 165 li = (SocketListener*)(_listeners+i);
hirokimineshita 0:736c76a75def 166 if (( li->Type == type )&& (li->port == port)) {
hirokimineshita 0:736c76a75def 167
hirokimineshita 0:736c76a75def 168 //printf("Listen check on port %d OK\r\n",port);
hirokimineshita 0:736c76a75def 169 return 0;
hirokimineshita 0:736c76a75def 170 }
hirokimineshita 0:736c76a75def 171 }
hirokimineshita 0:736c76a75def 172 //printf("Listen check on port %d NOK\r\n",port);
abcdefgh 15:5e973e6c058c 173 return ERR_SOCKET_NONE_LEFT;
abcdefgh 15:5e973e6c058c 174 }
hirokimineshita 0:736c76a75def 175
abcdefgh 15:5e973e6c058c 176
abcdefgh 15:5e973e6c058c 177 int Accept(int socket, SocketCallback callback, void* userData) {
hirokimineshita 0:736c76a75def 178 SocketInternal* si = GetInternal(socket);
hirokimineshita 0:736c76a75def 179 if (!si || si->ID != socket)
hirokimineshita 0:736c76a75def 180 return ERR_SOCKET_NOT_FOUND;
abcdefgh 15:5e973e6c058c 181
hirokimineshita 0:736c76a75def 182 si->Callback = callback;
hirokimineshita 0:736c76a75def 183 si->userData = userData;
abcdefgh 15:5e973e6c058c 184
abcdefgh 15:5e973e6c058c 185 //printf("Call to Socket Manager Accept \r\n");
hirokimineshita 0:736c76a75def 186 return 0;
abcdefgh 15:5e973e6c058c 187
hirokimineshita 0:736c76a75def 188 }
hirokimineshita 0:736c76a75def 189
abcdefgh 15:5e973e6c058c 190 int Send(int socket, const u8* data, int len) {
abcdefgh 15:5e973e6c058c 191 //printf("Call to Socket Manager Send \r\n");
hirokimineshita 0:736c76a75def 192 SocketInternal* si = GetInternal(socket);
abcdefgh 15:5e973e6c058c 193 //printf("socket = %d si->ID = %d si->Type = %d \r\n", socket, si->ID, si->Type);
abcdefgh 15:5e973e6c058c 194 if (!si || si->ID != socket) {
hirokimineshita 0:736c76a75def 195 //printf("send: socket not found \r\n");
hirokimineshita 0:736c76a75def 196 return ERR_SOCKET_NOT_FOUND;
hirokimineshita 0:736c76a75def 197 }
hirokimineshita 0:736c76a75def 198 //printf("Calling l2cap send \r\n");
abcdefgh 15:5e973e6c058c 199
hirokimineshita 0:736c76a75def 200 SocketHandler* h = GetHandler(si->Type);
hirokimineshita 0:736c76a75def 201 if (!h) {
abcdefgh 15:5e973e6c058c 202 //printf("Send: no socket type found \r\n");
abcdefgh 15:5e973e6c058c 203 return ERR_SOCKET_TYPE_NOT_FOUND;
hirokimineshita 0:736c76a75def 204 }
hirokimineshita 0:736c76a75def 205 return h->Send(si,data,len);
abcdefgh 15:5e973e6c058c 206
hirokimineshita 0:736c76a75def 207 }
hirokimineshita 0:736c76a75def 208
abcdefgh 15:5e973e6c058c 209 int Close(int socket) {
hirokimineshita 0:736c76a75def 210 SocketInternal* si = GetInternal(socket);
hirokimineshita 0:736c76a75def 211 if (!si || si->ID != socket)
hirokimineshita 0:736c76a75def 212 return ERR_SOCKET_NOT_FOUND;
hirokimineshita 0:736c76a75def 213 si->ID = 0;
hirokimineshita 0:736c76a75def 214 return GetHandler(si->Type)->Close(si);
hirokimineshita 0:736c76a75def 215 }
hirokimineshita 0:736c76a75def 216 };
hirokimineshita 0:736c76a75def 217
hirokimineshita 0:736c76a75def 218 SocketManager gSocketManager;
hirokimineshita 0:736c76a75def 219
hirokimineshita 0:736c76a75def 220 int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
hirokimineshita 0:736c76a75def 221 {
hirokimineshita 0:736c76a75def 222 //printf("Call to Socket Open \r\n");
hirokimineshita 0:736c76a75def 223 return gSocketManager.Open(type,addr,callback,userData);
hirokimineshita 0:736c76a75def 224 }
hirokimineshita 0:736c76a75def 225
hirokimineshita 0:736c76a75def 226 SocketInternal* Socket_Create(int type, SocketAddrHdr* addr, int port)
hirokimineshita 0:736c76a75def 227 {
hirokimineshita 0:736c76a75def 228 return gSocketManager.Create(type, addr, port);
hirokimineshita 0:736c76a75def 229 }
hirokimineshita 0:736c76a75def 230
hirokimineshita 0:736c76a75def 231 int Socket_Send(int socket, const u8* data, int len)
hirokimineshita 0:736c76a75def 232 {
hirokimineshita 0:736c76a75def 233 //printf("Call to Socket_Send \r\n");
hirokimineshita 0:736c76a75def 234 return gSocketManager.Send(socket,data,len);
hirokimineshita 0:736c76a75def 235 }
hirokimineshita 0:736c76a75def 236
hirokimineshita 0:736c76a75def 237 int Socket_Close(int socket)
hirokimineshita 0:736c76a75def 238 {
hirokimineshita 0:736c76a75def 239 return gSocketManager.Close(socket);
hirokimineshita 0:736c76a75def 240 }
hirokimineshita 0:736c76a75def 241
hirokimineshita 0:736c76a75def 242 int Socket_Listen(int type, int port,SocketCallback callback, void* userData)
hirokimineshita 0:736c76a75def 243 {
hirokimineshita 0:736c76a75def 244 //printf("Call to Socket_Listen \r\n");
hirokimineshita 0:736c76a75def 245 return gSocketManager.Listen(type, port,callback,userData);
hirokimineshita 0:736c76a75def 246 }
hirokimineshita 0:736c76a75def 247
hirokimineshita 0:736c76a75def 248 int Socket_InUse(int type, int port)
hirokimineshita 0:736c76a75def 249 {
hirokimineshita 0:736c76a75def 250 //printf("Call to Socket_InUse \r\n");
hirokimineshita 0:736c76a75def 251 return gSocketManager.InUse(type, port);
hirokimineshita 0:736c76a75def 252 }
hirokimineshita 0:736c76a75def 253
hirokimineshita 0:736c76a75def 254 int Socket_Accept(int socket, SocketCallback callback, void* userData)
hirokimineshita 0:736c76a75def 255 {
hirokimineshita 0:736c76a75def 256 //printf("Call to Socket_Accept \r\n");
hirokimineshita 0:736c76a75def 257 return gSocketManager.Accept(socket, callback, userData);
hirokimineshita 0:736c76a75def 258 }
hirokimineshita 0:736c76a75def 259
hirokimineshita 0:736c76a75def 260 int RegisterSocketHandler(int type, SocketHandler* handler)
hirokimineshita 0:736c76a75def 261 {
hirokimineshita 0:736c76a75def 262 return gSocketManager.RegisterSocketHandler(type,handler);
hirokimineshita 0:736c76a75def 263 }
hirokimineshita 0:736c76a75def 264
hirokimineshita 0:736c76a75def 265 SocketInternal* GetSocketInternal(int socket)
hirokimineshita 0:736c76a75def 266 {
hirokimineshita 0:736c76a75def 267 return gSocketManager.GetInternal(socket);
hirokimineshita 0:736c76a75def 268 }
hirokimineshita 0:736c76a75def 269