|
| 1 | +/**************************************************************************/ |
| 2 | +/*! |
| 3 | + @file bonding.cpp |
| 4 | + @author hathach (tinyusb.org) |
| 5 | +
|
| 6 | + @section LICENSE |
| 7 | +
|
| 8 | + Software License Agreement (BSD License) |
| 9 | +
|
| 10 | + Copyright (c) 2018, Adafruit Industries (adafruit.com) |
| 11 | + All rights reserved. |
| 12 | +
|
| 13 | + Redistribution and use in source and binary forms, with or without |
| 14 | + modification, are permitted provided that the following conditions are met: |
| 15 | + 1. Redistributions of source code must retain the above copyright |
| 16 | + notice, this list of conditions and the following disclaimer. |
| 17 | + 2. Redistributions in binary form must reproduce the above copyright |
| 18 | + notice, this list of conditions and the following disclaimer in the |
| 19 | + documentation and/or other materials provided with the distribution. |
| 20 | + 3. Neither the name of the copyright holders nor the |
| 21 | + names of its contributors may be used to endorse or promote products |
| 22 | + derived from this software without specific prior written permission. |
| 23 | +
|
| 24 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 25 | + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
| 28 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | +*/ |
| 35 | +/**************************************************************************/ |
| 36 | + |
| 37 | +#include <Arduino.h> |
| 38 | +#include <Nffs.h> |
| 39 | +#include "bonding.h" |
| 40 | +#include "bluefruit.h" |
| 41 | + |
| 42 | +#if CFG_DEBUG >= 2 |
| 43 | +//#define printBondDir() dbgPrintDir(CFG_BOND_NFFS_DIR) |
| 44 | +#define printBondDir() |
| 45 | +#else |
| 46 | +#define printBondDir() |
| 47 | +#endif |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +/*------------------------------------------------------------------*/ |
| 52 | +/* Saving Bond Data to Nffs in following layout |
| 53 | + * - _bond_data 80 bytes |
| 54 | + * - Name 32 bytes |
| 55 | + * - CCCD variable |
| 56 | + *------------------------------------------------------------------*/ |
| 57 | +static void bond_save_keys_dfr(uint16_t conn_hdl, bond_data_t* bdata) |
| 58 | +{ |
| 59 | + char filename[BOND_FILENAME_LEN]; |
| 60 | + sprintf(filename, BOND_FILENAME, bdata->own_enc.master_id.ediv); |
| 61 | + |
| 62 | + char devname[CFG_MAX_DEVNAME_LEN] = { 0 }; |
| 63 | + Bluefruit.Gap.getPeerName(conn_hdl, devname, CFG_MAX_DEVNAME_LEN); |
| 64 | + |
| 65 | + NffsFile file(filename, FS_ACCESS_WRITE); |
| 66 | + |
| 67 | + VERIFY( file.exists(), ); |
| 68 | + |
| 69 | + bool result = true; |
| 70 | + |
| 71 | + // write keys |
| 72 | + if ( !file.write((uint8_t*)bdata, sizeof(bond_data_t)) ) |
| 73 | + { |
| 74 | + result = false; |
| 75 | + } |
| 76 | + |
| 77 | + // write device name |
| 78 | + if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) ) |
| 79 | + { |
| 80 | + result = false; |
| 81 | + } |
| 82 | + |
| 83 | + file.close(); |
| 84 | + |
| 85 | + if (result) |
| 86 | + { |
| 87 | + LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename); |
| 88 | + }else |
| 89 | + { |
| 90 | + LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname); |
| 91 | + } |
| 92 | + |
| 93 | + printBondDir(); |
| 94 | +} |
| 95 | + |
| 96 | +void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata) |
| 97 | +{ |
| 98 | + uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_data_t) ); |
| 99 | + VERIFY(buf, ); |
| 100 | + |
| 101 | + memcpy(buf, bdata, sizeof(bond_data_t)); |
| 102 | + |
| 103 | + // queue to execute in Ada Callback thread |
| 104 | + ada_callback(buf, bond_save_keys_dfr, conn_hdl, buf); |
| 105 | +} |
| 106 | + |
| 107 | +void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv) |
| 108 | +{ |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +bool bond_load_keys(uint16_t ediv, bond_data_t* bdata) |
| 113 | +{ |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv) |
| 118 | +{ |
| 119 | + |
| 120 | +} |
0 commit comments