うおーるぼっとをWiiリモコンでコントロールする新しいプログラムです。 以前のものより、Wiiリモコンが早く繋がる様になりました。 It is a program which controls A with the Wii remote. ※ A Bluetooth dongle and a Wii remote control are needed.

Dependencies:   USBHost mbed FATFileSystem mbed-rtos

Committer:
jksoft
Date:
Mon Jun 10 16:01:50 2013 +0000
Revision:
0:fccb789424fc
1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:fccb789424fc 1 /*
jksoft 0:fccb789424fc 2 * Copyright (C) 2009-2012 by Matthias Ringwald
jksoft 0:fccb789424fc 3 *
jksoft 0:fccb789424fc 4 * Redistribution and use in source and binary forms, with or without
jksoft 0:fccb789424fc 5 * modification, are permitted provided that the following conditions
jksoft 0:fccb789424fc 6 * are met:
jksoft 0:fccb789424fc 7 *
jksoft 0:fccb789424fc 8 * 1. Redistributions of source code must retain the above copyright
jksoft 0:fccb789424fc 9 * notice, this list of conditions and the following disclaimer.
jksoft 0:fccb789424fc 10 * 2. Redistributions in binary form must reproduce the above copyright
jksoft 0:fccb789424fc 11 * notice, this list of conditions and the following disclaimer in the
jksoft 0:fccb789424fc 12 * documentation and/or other materials provided with the distribution.
jksoft 0:fccb789424fc 13 * 3. Neither the name of the copyright holders nor the names of
jksoft 0:fccb789424fc 14 * contributors may be used to endorse or promote products derived
jksoft 0:fccb789424fc 15 * from this software without specific prior written permission.
jksoft 0:fccb789424fc 16 * 4. Any redistribution, use, or modification is done solely for
jksoft 0:fccb789424fc 17 * personal benefit and not for any commercial purpose or for
jksoft 0:fccb789424fc 18 * monetary gain.
jksoft 0:fccb789424fc 19 *
jksoft 0:fccb789424fc 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
jksoft 0:fccb789424fc 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
jksoft 0:fccb789424fc 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
jksoft 0:fccb789424fc 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
jksoft 0:fccb789424fc 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
jksoft 0:fccb789424fc 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
jksoft 0:fccb789424fc 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
jksoft 0:fccb789424fc 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
jksoft 0:fccb789424fc 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
jksoft 0:fccb789424fc 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
jksoft 0:fccb789424fc 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
jksoft 0:fccb789424fc 31 * SUCH DAMAGE.
jksoft 0:fccb789424fc 32 *
jksoft 0:fccb789424fc 33 * Please inquire about commercial licensing options at btstack@ringwald.ch
jksoft 0:fccb789424fc 34 *
jksoft 0:fccb789424fc 35 */
jksoft 0:fccb789424fc 36
jksoft 0:fccb789424fc 37 #include <string.h>
jksoft 0:fccb789424fc 38 #include <stdlib.h>
jksoft 0:fccb789424fc 39
jksoft 0:fccb789424fc 40 #include "remote_device_db.h"
jksoft 0:fccb789424fc 41 #include "btstack_memory.h"
jksoft 0:fccb789424fc 42 #include "debug.h"
jksoft 0:fccb789424fc 43
jksoft 0:fccb789424fc 44 #include <btstack/utils.h>
jksoft 0:fccb789424fc 45 #include <btstack/linked_list.h>
jksoft 0:fccb789424fc 46
jksoft 0:fccb789424fc 47 // This lists should be only accessed by tests.
jksoft 0:fccb789424fc 48 linked_list_t db_mem_link_keys = NULL;
jksoft 0:fccb789424fc 49 linked_list_t db_mem_names = NULL;
jksoft 0:fccb789424fc 50 static linked_list_t db_mem_services = NULL;
jksoft 0:fccb789424fc 51
jksoft 0:fccb789424fc 52 // Device info
jksoft 0:fccb789424fc 53 static void db_open(void){
jksoft 0:fccb789424fc 54 }
jksoft 0:fccb789424fc 55
jksoft 0:fccb789424fc 56 static void db_close(void){
jksoft 0:fccb789424fc 57 }
jksoft 0:fccb789424fc 58
jksoft 0:fccb789424fc 59 static db_mem_device_t * get_item(linked_list_t list, bd_addr_t *bd_addr) {
jksoft 0:fccb789424fc 60 linked_item_t *it;
jksoft 0:fccb789424fc 61 for (it = (linked_item_t *) list; it ; it = it->next){
jksoft 0:fccb789424fc 62 db_mem_device_t * item = (db_mem_device_t *) it;
jksoft 0:fccb789424fc 63 if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) {
jksoft 0:fccb789424fc 64 return item;
jksoft 0:fccb789424fc 65 }
jksoft 0:fccb789424fc 66 }
jksoft 0:fccb789424fc 67 return NULL;
jksoft 0:fccb789424fc 68 }
jksoft 0:fccb789424fc 69
jksoft 0:fccb789424fc 70 static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) {
jksoft 0:fccb789424fc 71 db_mem_device_name_t * item = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
jksoft 0:fccb789424fc 72
jksoft 0:fccb789424fc 73 if (!item) return 0;
jksoft 0:fccb789424fc 74
jksoft 0:fccb789424fc 75 strncpy((char*)device_name, item->device_name, MAX_NAME_LEN);
jksoft 0:fccb789424fc 76
jksoft 0:fccb789424fc 77 linked_list_remove(&db_mem_names, (linked_item_t *) item);
jksoft 0:fccb789424fc 78 linked_list_add(&db_mem_names, (linked_item_t *) item);
jksoft 0:fccb789424fc 79
jksoft 0:fccb789424fc 80 return 1;
jksoft 0:fccb789424fc 81 }
jksoft 0:fccb789424fc 82
jksoft 0:fccb789424fc 83 static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) {
jksoft 0:fccb789424fc 84 db_mem_device_link_key_t * item = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
jksoft 0:fccb789424fc 85
jksoft 0:fccb789424fc 86 if (!item) return 0;
jksoft 0:fccb789424fc 87
jksoft 0:fccb789424fc 88 memcpy(link_key, item->link_key, LINK_KEY_LEN);
jksoft 0:fccb789424fc 89
jksoft 0:fccb789424fc 90 linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
jksoft 0:fccb789424fc 91 linked_list_add(&db_mem_link_keys, (linked_item_t *) item);
jksoft 0:fccb789424fc 92
jksoft 0:fccb789424fc 93 return 1;
jksoft 0:fccb789424fc 94 }
jksoft 0:fccb789424fc 95
jksoft 0:fccb789424fc 96 static void delete_link_key(bd_addr_t *bd_addr){
jksoft 0:fccb789424fc 97 db_mem_device_t * item = get_item(db_mem_link_keys, bd_addr);
jksoft 0:fccb789424fc 98
jksoft 0:fccb789424fc 99 if (!item) return;
jksoft 0:fccb789424fc 100
jksoft 0:fccb789424fc 101 linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
jksoft 0:fccb789424fc 102 btstack_memory_db_mem_device_link_key_free(item);
jksoft 0:fccb789424fc 103 }
jksoft 0:fccb789424fc 104
jksoft 0:fccb789424fc 105
jksoft 0:fccb789424fc 106 static void put_link_key(bd_addr_t *bd_addr, link_key_t *link_key){
jksoft 0:fccb789424fc 107 db_mem_device_link_key_t * existingRecord = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
jksoft 0:fccb789424fc 108
jksoft 0:fccb789424fc 109 if (existingRecord){
jksoft 0:fccb789424fc 110 memcpy(existingRecord->link_key, link_key, LINK_KEY_LEN);
jksoft 0:fccb789424fc 111 return;
jksoft 0:fccb789424fc 112 }
jksoft 0:fccb789424fc 113
jksoft 0:fccb789424fc 114 // Record not found, create new one for this device
jksoft 0:fccb789424fc 115 db_mem_device_link_key_t * newItem = (db_mem_device_link_key_t*) btstack_memory_db_mem_device_link_key_get();
jksoft 0:fccb789424fc 116 if (!newItem){
jksoft 0:fccb789424fc 117 newItem = (db_mem_device_link_key_t*)linked_list_get_last_item(&db_mem_link_keys);
jksoft 0:fccb789424fc 118 }
jksoft 0:fccb789424fc 119
jksoft 0:fccb789424fc 120 if (!newItem) return;
jksoft 0:fccb789424fc 121
jksoft 0:fccb789424fc 122 memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
jksoft 0:fccb789424fc 123 memcpy(newItem->link_key, link_key, LINK_KEY_LEN);
jksoft 0:fccb789424fc 124 linked_list_add(&db_mem_link_keys, (linked_item_t *) newItem);
jksoft 0:fccb789424fc 125 }
jksoft 0:fccb789424fc 126
jksoft 0:fccb789424fc 127 static void delete_name(bd_addr_t *bd_addr){
jksoft 0:fccb789424fc 128 db_mem_device_t * item = get_item(db_mem_names, bd_addr);
jksoft 0:fccb789424fc 129
jksoft 0:fccb789424fc 130 if (!item) return;
jksoft 0:fccb789424fc 131
jksoft 0:fccb789424fc 132 linked_list_remove(&db_mem_names, (linked_item_t *) item);
jksoft 0:fccb789424fc 133 btstack_memory_db_mem_device_name_free(item);
jksoft 0:fccb789424fc 134 }
jksoft 0:fccb789424fc 135
jksoft 0:fccb789424fc 136 static void put_name(bd_addr_t *bd_addr, device_name_t *device_name){
jksoft 0:fccb789424fc 137 db_mem_device_name_t * existingRecord = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
jksoft 0:fccb789424fc 138
jksoft 0:fccb789424fc 139 if (existingRecord){
jksoft 0:fccb789424fc 140 strncpy(existingRecord->device_name, (const char*) device_name, MAX_NAME_LEN);
jksoft 0:fccb789424fc 141 return;
jksoft 0:fccb789424fc 142 }
jksoft 0:fccb789424fc 143
jksoft 0:fccb789424fc 144 // Record not found, create a new one for this device
jksoft 0:fccb789424fc 145 db_mem_device_name_t * newItem = (db_mem_device_name_t *) btstack_memory_db_mem_device_name_get();
jksoft 0:fccb789424fc 146 if (!newItem) {
jksoft 0:fccb789424fc 147 newItem = (db_mem_device_name_t*)linked_list_get_last_item(&db_mem_names);
jksoft 0:fccb789424fc 148 };
jksoft 0:fccb789424fc 149
jksoft 0:fccb789424fc 150 if (!newItem) return;
jksoft 0:fccb789424fc 151
jksoft 0:fccb789424fc 152 memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
jksoft 0:fccb789424fc 153 strncpy(newItem->device_name, (const char*) device_name, MAX_NAME_LEN);
jksoft 0:fccb789424fc 154 linked_list_add(&db_mem_names, (linked_item_t *) newItem);
jksoft 0:fccb789424fc 155 }
jksoft 0:fccb789424fc 156
jksoft 0:fccb789424fc 157
jksoft 0:fccb789424fc 158 // MARK: PERSISTENT RFCOMM CHANNEL ALLOCATION
jksoft 0:fccb789424fc 159
jksoft 0:fccb789424fc 160 static uint8_t persistent_rfcomm_channel(char *serviceName){
jksoft 0:fccb789424fc 161 linked_item_t *it;
jksoft 0:fccb789424fc 162 db_mem_service_t * item;
jksoft 0:fccb789424fc 163 uint8_t max_channel = 1;
jksoft 0:fccb789424fc 164
jksoft 0:fccb789424fc 165 for (it = (linked_item_t *) db_mem_services; it ; it = it->next){
jksoft 0:fccb789424fc 166 item = (db_mem_service_t *) it;
jksoft 0:fccb789424fc 167 if (strncmp(item->service_name, serviceName, MAX_NAME_LEN) == 0) {
jksoft 0:fccb789424fc 168 // Match found
jksoft 0:fccb789424fc 169 return item->channel;
jksoft 0:fccb789424fc 170 }
jksoft 0:fccb789424fc 171
jksoft 0:fccb789424fc 172 // TODO prevent overflow
jksoft 0:fccb789424fc 173 if (item->channel >= max_channel) max_channel = item->channel + 1;
jksoft 0:fccb789424fc 174 }
jksoft 0:fccb789424fc 175
jksoft 0:fccb789424fc 176 // Allocate new persistant channel
jksoft 0:fccb789424fc 177 db_mem_service_t * newItem = (db_mem_service_t *) btstack_memory_db_mem_service_get();
jksoft 0:fccb789424fc 178
jksoft 0:fccb789424fc 179 if (!newItem) return 0;
jksoft 0:fccb789424fc 180
jksoft 0:fccb789424fc 181 strncpy(newItem->service_name, serviceName, MAX_NAME_LEN);
jksoft 0:fccb789424fc 182 newItem->channel = max_channel;
jksoft 0:fccb789424fc 183 linked_list_add(&db_mem_services, (linked_item_t *) newItem);
jksoft 0:fccb789424fc 184 return max_channel;
jksoft 0:fccb789424fc 185 }
jksoft 0:fccb789424fc 186
jksoft 0:fccb789424fc 187
jksoft 0:fccb789424fc 188 const remote_device_db_t remote_device_db_memory = {
jksoft 0:fccb789424fc 189 db_open,
jksoft 0:fccb789424fc 190 db_close,
jksoft 0:fccb789424fc 191 get_link_key,
jksoft 0:fccb789424fc 192 put_link_key,
jksoft 0:fccb789424fc 193 delete_link_key,
jksoft 0:fccb789424fc 194 get_name,
jksoft 0:fccb789424fc 195 put_name,
jksoft 0:fccb789424fc 196 delete_name,
jksoft 0:fccb789424fc 197 persistent_rfcomm_channel
jksoft 0:fccb789424fc 198 };