![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This program uses code taken from another program called BlueUSB
Socket.cpp@0:31713f62f35b, 2011-12-10 (annotated)
- Committer:
- madcowswe
- Date:
- Sat Dec 10 18:45:31 2011 +0000
- Revision:
- 0:31713f62f35b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 0:31713f62f35b | 1 | /* |
madcowswe | 0:31713f62f35b | 2 | Copyright (c) 2010 Peter Barrett |
madcowswe | 0:31713f62f35b | 3 | |
madcowswe | 0:31713f62f35b | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
madcowswe | 0:31713f62f35b | 5 | of this software and associated documentation files (the "Software"), to deal |
madcowswe | 0:31713f62f35b | 6 | in the Software without restriction, including without limitation the rights |
madcowswe | 0:31713f62f35b | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
madcowswe | 0:31713f62f35b | 8 | copies of the Software, and to permit persons to whom the Software is |
madcowswe | 0:31713f62f35b | 9 | furnished to do so, subject to the following conditions: |
madcowswe | 0:31713f62f35b | 10 | |
madcowswe | 0:31713f62f35b | 11 | The above copyright notice and this permission notice shall be included in |
madcowswe | 0:31713f62f35b | 12 | all copies or substantial portions of the Software. |
madcowswe | 0:31713f62f35b | 13 | |
madcowswe | 0:31713f62f35b | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
madcowswe | 0:31713f62f35b | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
madcowswe | 0:31713f62f35b | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
madcowswe | 0:31713f62f35b | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
madcowswe | 0:31713f62f35b | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
madcowswe | 0:31713f62f35b | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
madcowswe | 0:31713f62f35b | 20 | THE SOFTWARE. |
madcowswe | 0:31713f62f35b | 21 | */ |
madcowswe | 0:31713f62f35b | 22 | |
madcowswe | 0:31713f62f35b | 23 | #include <stdio.h> |
madcowswe | 0:31713f62f35b | 24 | #include <stdlib.h> |
madcowswe | 0:31713f62f35b | 25 | #include <stdio.h> |
madcowswe | 0:31713f62f35b | 26 | #include <string.h> |
madcowswe | 0:31713f62f35b | 27 | |
madcowswe | 0:31713f62f35b | 28 | #include "Utils.h" |
madcowswe | 0:31713f62f35b | 29 | #include "Socket.h" |
madcowswe | 0:31713f62f35b | 30 | |
madcowswe | 0:31713f62f35b | 31 | #define MAX_SOCKET_HANDLERS 3 |
madcowswe | 0:31713f62f35b | 32 | #define MAX_SOCKETS 16 |
madcowswe | 0:31713f62f35b | 33 | |
madcowswe | 0:31713f62f35b | 34 | class SocketInternalPad |
madcowswe | 0:31713f62f35b | 35 | { |
madcowswe | 0:31713f62f35b | 36 | public: |
madcowswe | 0:31713f62f35b | 37 | SocketInternal si; |
madcowswe | 0:31713f62f35b | 38 | u8 pad[8]; |
madcowswe | 0:31713f62f35b | 39 | }; |
madcowswe | 0:31713f62f35b | 40 | |
madcowswe | 0:31713f62f35b | 41 | class SocketManager |
madcowswe | 0:31713f62f35b | 42 | { |
madcowswe | 0:31713f62f35b | 43 | SocketHandler* _handlers[MAX_SOCKET_HANDLERS]; |
madcowswe | 0:31713f62f35b | 44 | SocketInternalPad _sockets[MAX_SOCKETS]; |
madcowswe | 0:31713f62f35b | 45 | |
madcowswe | 0:31713f62f35b | 46 | public: |
madcowswe | 0:31713f62f35b | 47 | SocketManager() |
madcowswe | 0:31713f62f35b | 48 | { |
madcowswe | 0:31713f62f35b | 49 | memset(_handlers,0,sizeof(_handlers)); |
madcowswe | 0:31713f62f35b | 50 | memset(_sockets,0,sizeof(_sockets)); |
madcowswe | 0:31713f62f35b | 51 | } |
madcowswe | 0:31713f62f35b | 52 | |
madcowswe | 0:31713f62f35b | 53 | SocketHandler* GetHandler(int type) |
madcowswe | 0:31713f62f35b | 54 | { |
madcowswe | 0:31713f62f35b | 55 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
madcowswe | 0:31713f62f35b | 56 | return 0; |
madcowswe | 0:31713f62f35b | 57 | return _handlers[type - 1]; |
madcowswe | 0:31713f62f35b | 58 | } |
madcowswe | 0:31713f62f35b | 59 | |
madcowswe | 0:31713f62f35b | 60 | SocketInternal* GetInternal(int s) |
madcowswe | 0:31713f62f35b | 61 | { |
madcowswe | 0:31713f62f35b | 62 | if (s < 1 || s > MAX_SOCKETS) |
madcowswe | 0:31713f62f35b | 63 | return 0; |
madcowswe | 0:31713f62f35b | 64 | return &_sockets[s - 1].si; |
madcowswe | 0:31713f62f35b | 65 | } |
madcowswe | 0:31713f62f35b | 66 | |
madcowswe | 0:31713f62f35b | 67 | int RegisterSocketHandler(int type, SocketHandler* handler) |
madcowswe | 0:31713f62f35b | 68 | { |
madcowswe | 0:31713f62f35b | 69 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
madcowswe | 0:31713f62f35b | 70 | return ERR_SOCKET_TYPE_NOT_FOUND; |
madcowswe | 0:31713f62f35b | 71 | _handlers[type - 1] = handler; |
madcowswe | 0:31713f62f35b | 72 | return 0; |
madcowswe | 0:31713f62f35b | 73 | } |
madcowswe | 0:31713f62f35b | 74 | |
madcowswe | 0:31713f62f35b | 75 | int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
madcowswe | 0:31713f62f35b | 76 | { |
madcowswe | 0:31713f62f35b | 77 | SocketHandler* h = GetHandler(type); |
madcowswe | 0:31713f62f35b | 78 | if (!h) |
madcowswe | 0:31713f62f35b | 79 | return ERR_SOCKET_TYPE_NOT_FOUND; |
madcowswe | 0:31713f62f35b | 80 | |
madcowswe | 0:31713f62f35b | 81 | for (int i = 0; i < MAX_SOCKETS; i++) |
madcowswe | 0:31713f62f35b | 82 | { |
madcowswe | 0:31713f62f35b | 83 | SocketInternal* si = (SocketInternal*)(_sockets+i); |
madcowswe | 0:31713f62f35b | 84 | if (si->ID == 0) |
madcowswe | 0:31713f62f35b | 85 | { |
madcowswe | 0:31713f62f35b | 86 | si->ID = i+1; |
madcowswe | 0:31713f62f35b | 87 | si->Type = type; |
madcowswe | 0:31713f62f35b | 88 | si->Callback = callback; |
madcowswe | 0:31713f62f35b | 89 | si->userData = userData; |
madcowswe | 0:31713f62f35b | 90 | return h->Open(si,addr); |
madcowswe | 0:31713f62f35b | 91 | } |
madcowswe | 0:31713f62f35b | 92 | } |
madcowswe | 0:31713f62f35b | 93 | return ERR_SOCKET_NONE_LEFT; |
madcowswe | 0:31713f62f35b | 94 | } |
madcowswe | 0:31713f62f35b | 95 | |
madcowswe | 0:31713f62f35b | 96 | int Send(int socket, const u8* data, int len) |
madcowswe | 0:31713f62f35b | 97 | { |
madcowswe | 0:31713f62f35b | 98 | SocketInternal* si = GetInternal(socket); |
madcowswe | 0:31713f62f35b | 99 | if (!si || si->ID != socket) |
madcowswe | 0:31713f62f35b | 100 | return ERR_SOCKET_NOT_FOUND; |
madcowswe | 0:31713f62f35b | 101 | return GetHandler(si->Type)->Send(si,data,len); |
madcowswe | 0:31713f62f35b | 102 | } |
madcowswe | 0:31713f62f35b | 103 | |
madcowswe | 0:31713f62f35b | 104 | int Close(int socket) |
madcowswe | 0:31713f62f35b | 105 | { |
madcowswe | 0:31713f62f35b | 106 | SocketInternal* si = GetInternal(socket); |
madcowswe | 0:31713f62f35b | 107 | if (!si || si->ID != socket) |
madcowswe | 0:31713f62f35b | 108 | return ERR_SOCKET_NOT_FOUND; |
madcowswe | 0:31713f62f35b | 109 | si->ID = 0; |
madcowswe | 0:31713f62f35b | 110 | return GetHandler(si->Type)->Close(si); |
madcowswe | 0:31713f62f35b | 111 | } |
madcowswe | 0:31713f62f35b | 112 | }; |
madcowswe | 0:31713f62f35b | 113 | |
madcowswe | 0:31713f62f35b | 114 | SocketManager gSocketManager; |
madcowswe | 0:31713f62f35b | 115 | |
madcowswe | 0:31713f62f35b | 116 | int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
madcowswe | 0:31713f62f35b | 117 | { |
madcowswe | 0:31713f62f35b | 118 | return gSocketManager.Open(type,addr,callback,userData); |
madcowswe | 0:31713f62f35b | 119 | } |
madcowswe | 0:31713f62f35b | 120 | |
madcowswe | 0:31713f62f35b | 121 | int Socket_Send(int socket, const u8* data, int len) |
madcowswe | 0:31713f62f35b | 122 | { |
madcowswe | 0:31713f62f35b | 123 | return gSocketManager.Send(socket,data,len); |
madcowswe | 0:31713f62f35b | 124 | } |
madcowswe | 0:31713f62f35b | 125 | |
madcowswe | 0:31713f62f35b | 126 | int Socket_Close(int socket) |
madcowswe | 0:31713f62f35b | 127 | { |
madcowswe | 0:31713f62f35b | 128 | return gSocketManager.Close(socket); |
madcowswe | 0:31713f62f35b | 129 | } |
madcowswe | 0:31713f62f35b | 130 | |
madcowswe | 0:31713f62f35b | 131 | int RegisterSocketHandler(int type, SocketHandler* handler) |
madcowswe | 0:31713f62f35b | 132 | { |
madcowswe | 0:31713f62f35b | 133 | return gSocketManager.RegisterSocketHandler(type,handler); |
madcowswe | 0:31713f62f35b | 134 | } |
madcowswe | 0:31713f62f35b | 135 | |
madcowswe | 0:31713f62f35b | 136 | SocketInternal* GetSocketInternal(int socket) |
madcowswe | 0:31713f62f35b | 137 | { |
madcowswe | 0:31713f62f35b | 138 | return gSocketManager.GetInternal(socket); |
madcowswe | 0:31713f62f35b | 139 | } |
madcowswe | 0:31713f62f35b | 140 |