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