手動機アーム、mbed基盤のspiをおくるだけのプログラムです(9/4)

Dependencies:   SPI_master_arm_shudouki mbed

Fork of SPI_master_arm_shudouki2 by F^3 RC 2班

Committer:
yoka06
Date:
Mon Sep 04 13:02:43 2017 +0000
Revision:
2:faa028d2f2f8
Parent:
0:76d1c7f13415
?

Who changed what in which revision?

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