うおーるぼっと用プログラム Wiiリモコンからのダイレクト操作モードのみ BlueUSBをベースに使用しています。

Dependencies:   BD6211F mbed SimpleFilter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.cpp Source File

Socket.cpp

00001 /*
00002 Copyright (c) 2010 Peter Barrett
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 
00028 #include "Utils.h"
00029 #include "Socket.h"
00030 
00031 #define MAX_SOCKET_HANDLERS 3
00032 #define MAX_SOCKETS 16
00033 
00034 class SocketInternalPad
00035 {
00036     public:
00037     SocketInternal si;
00038     u8  pad[8];
00039 };
00040 
00041 class SocketManager
00042 {
00043     SocketHandler*  _handlers[MAX_SOCKET_HANDLERS];
00044     SocketInternalPad  _sockets[MAX_SOCKETS];
00045 
00046     public:
00047     SocketManager()
00048     {
00049         memset(_handlers,0,sizeof(_handlers));
00050         memset(_sockets,0,sizeof(_sockets));
00051     }
00052 
00053     SocketHandler* GetHandler(int type)
00054     {
00055         if (type < 1 || type > MAX_SOCKET_HANDLERS)
00056             return 0;
00057         return _handlers[type - 1];
00058     }
00059 
00060     SocketInternal* GetInternal(int s)
00061     {
00062         if (s < 1 || s > MAX_SOCKETS)
00063             return 0;
00064         return &_sockets[s - 1].si;
00065     }
00066 
00067     int RegisterSocketHandler(int type, SocketHandler* handler)
00068     {
00069        if (type < 1 || type > MAX_SOCKET_HANDLERS)
00070             return ERR_SOCKET_TYPE_NOT_FOUND;
00071         _handlers[type - 1] = handler;
00072         return 0;
00073     }
00074 
00075     int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
00076     {
00077         SocketHandler* h = GetHandler(type);
00078         if (!h)
00079             return ERR_SOCKET_TYPE_NOT_FOUND;
00080         
00081         for (int i = 0; i < MAX_SOCKETS; i++)
00082         {
00083             SocketInternal* si = (SocketInternal*)(_sockets+i);
00084             if (si->ID == 0)
00085             {
00086                 si->ID = i+1;
00087                 si->Type = type;
00088                 si->Callback = callback;
00089                 si->userData = userData;
00090                 return h->Open(si,addr);
00091             }
00092         }
00093         return ERR_SOCKET_NONE_LEFT;
00094     }
00095 
00096     int Send(int socket, const u8* data, int len)
00097     {
00098         SocketInternal* si = GetInternal(socket);
00099         if (!si || si->ID != socket)
00100             return ERR_SOCKET_NOT_FOUND;
00101         return GetHandler(si->Type)->Send(si,data,len);
00102     }
00103 
00104     int Close(int socket)
00105     {
00106         SocketInternal* si = GetInternal(socket);
00107         if (!si || si->ID != socket)
00108             return ERR_SOCKET_NOT_FOUND;
00109         si->ID = 0;
00110         return GetHandler(si->Type)->Close(si);
00111     }
00112 };
00113 
00114 SocketManager gSocketManager;
00115 
00116 int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
00117 {
00118     return gSocketManager.Open(type,addr,callback,userData);
00119 }
00120 
00121 int Socket_Send(int socket, const u8* data, int len)
00122 {
00123     return gSocketManager.Send(socket,data,len);
00124 }
00125 
00126 int Socket_Close(int socket)
00127 {
00128     return gSocketManager.Close(socket);
00129 }
00130 
00131 int RegisterSocketHandler(int type, SocketHandler* handler)
00132 {
00133     return gSocketManager.RegisterSocketHandler(type,handler);
00134 }
00135 
00136 SocketInternal* GetSocketInternal(int socket)
00137 {
00138     return gSocketManager.GetInternal(socket);
00139 }
00140