KAMUI USB HOST MIDI-CV Example based on Peter Barrett's BlueUSB

Dependencies:   TextLCD mbed

Committer:
radiojunkbox
Date:
Fri May 11 15:31:59 2012 +0000
Revision:
0:3b4e3e2ec6a5
Rev. 0.1 alfa

Who changed what in which revision?

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