TEST

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Committer:
Emre.Eken@IST-LT-36262.maxim-ic.internal
Date:
Mon Jul 29 10:26:58 2019 +0300
Revision:
3:2fe2ff1ca0dc
Parent:
1:f60eafbf009a
Maxim copy right section is added.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 1 /*******************************************************************************
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 2 * Copyright (C) Maxim Integrated Products, Inc., All rights Reserved.
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 3 *
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 4 * This software is protected by copyright laws of the United States and
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 5 * of foreign countries. This material may also be protected by patent laws
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 6 * and technology transfer regulations of the United States and of foreign
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 7 * countries. This software is furnished under a license agreement and/or a
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 8 * nondisclosure agreement and may only be used or reproduced in accordance
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 9 * with the terms of those agreements. Dissemination of this information to
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 10 * any party or parties not specified in the license agreement and/or
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 11 * nondisclosure agreement is expressly prohibited.
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 12 *
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 13 * The above copyright notice and this permission notice shall be included
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 14 * in all copies or substantial portions of the Software.
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 15 *
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 19 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 20 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 22 * OTHER DEALINGS IN THE SOFTWARE.
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 23 *
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 24 * Except as contained in this notice, the name of Maxim Integrated
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 25 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 26 * Products, Inc. Branding Policy.
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 27 *
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 28 * The mere transfer of this software does not imply any licenses
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 29 * of trade secrets, proprietary technology, copyrights, patents,
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 30 * trademarks, maskwork rights, or any other form of intellectual
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 31 * property whatsoever. Maxim Integrated Products, Inc. retains all
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 32 * ownership rights.
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 33 *******************************************************************************
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 34 */
Emre.Eken@IST-LT-36262.maxim-ic.internal 3:2fe2ff1ca0dc 35
gmehmet 1:f60eafbf009a 36 #include "BLE_ICARUS.h"
gmehmet 1:f60eafbf009a 37
gmehmet 1:f60eafbf009a 38 #include "../../Utilities/mxm_assert.h"
gmehmet 1:f60eafbf009a 39 #include "queue.h"
gmehmet 1:f60eafbf009a 40 #include "Peripherals.h"
gmehmet 1:f60eafbf009a 41 #include "../../version.h"
gmehmet 1:f60eafbf009a 42
gmehmet 1:f60eafbf009a 43
gmehmet 1:f60eafbf009a 44 UUID customServiceUUID("00001523-1212-efde-1523-785feabcd123");
gmehmet 1:f60eafbf009a 45 UUID notifyCharUUID( "00001011-1212-efde-1523-785feabcd123");
gmehmet 1:f60eafbf009a 46 UUID configRWCharUUID("00001027-1212-efde-1523-785feabcd123");
gmehmet 1:f60eafbf009a 47
gmehmet 1:f60eafbf009a 48 const static char DEVICE_NAME[] = FIRMWARE_VERSION;
gmehmet 1:f60eafbf009a 49 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
gmehmet 1:f60eafbf009a 50
gmehmet 1:f60eafbf009a 51 // BLE defines
gmehmet 1:f60eafbf009a 52 #define BLE_TICKER_PERIOD 0.050 //Ticker period in order of seconds
gmehmet 1:f60eafbf009a 53 #define BLE_CONN_INT_PACKET 2 //Ticker period in order of seconds
gmehmet 1:f60eafbf009a 54 #define BLE_NOTIFY_CHAR_ARR_SIZE 20
gmehmet 1:f60eafbf009a 55 #define BLE_READWRITE_CHAR_ARR_SIZE 16
gmehmet 1:f60eafbf009a 56 #define MAX_BLE_QUEUE 128
gmehmet 1:f60eafbf009a 57 // end of BLE defines
gmehmet 1:f60eafbf009a 58
gmehmet 1:f60eafbf009a 59 #if defined(USE_BLE_TICKER_TO_CHECK_TRANSFER)
gmehmet 1:f60eafbf009a 60 Ticker TICKER_BLE;
gmehmet 1:f60eafbf009a 61 static volatile unsigned char BLE_CAN_TRANSFER = 0;
gmehmet 1:f60eafbf009a 62
gmehmet 1:f60eafbf009a 63 static void Ble_Can_Transfer_Toggle(){
gmehmet 1:f60eafbf009a 64 BLE_CAN_TRANSFER = true;
gmehmet 1:f60eafbf009a 65 }
gmehmet 1:f60eafbf009a 66
gmehmet 1:f60eafbf009a 67 static inline char Ble_Can_Transfer_Check(){
gmehmet 1:f60eafbf009a 68 return BLE_CAN_TRANSFER;
gmehmet 1:f60eafbf009a 69 }
gmehmet 1:f60eafbf009a 70
gmehmet 1:f60eafbf009a 71 static inline void Ble_Can_Transfer_Set(unsigned char en){
gmehmet 1:f60eafbf009a 72 BLE_CAN_TRANSFER = en;
gmehmet 1:f60eafbf009a 73 }
gmehmet 1:f60eafbf009a 74
gmehmet 1:f60eafbf009a 75 #endif
gmehmet 1:f60eafbf009a 76
gmehmet 1:f60eafbf009a 77 /* Set Up custom Characteristics */
gmehmet 1:f60eafbf009a 78 static uint8_t notifyValue[BLE_NOTIFY_CHAR_ARR_SIZE] = {0};
gmehmet 1:f60eafbf009a 79 GattCharacteristic notifyChar(notifyCharUUID, notifyValue, BLE_NOTIFY_CHAR_ARR_SIZE, BLE_NOTIFY_CHAR_ARR_SIZE, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
gmehmet 1:f60eafbf009a 80
gmehmet 1:f60eafbf009a 81 static uint8_t configValue[BLE_READWRITE_CHAR_ARR_SIZE] = {3,0,254,37};
gmehmet 1:f60eafbf009a 82 ReadWriteArrayGattCharacteristic<uint8_t, sizeof(configValue)> writeChar(configRWCharUUID, configValue);
gmehmet 1:f60eafbf009a 83
gmehmet 1:f60eafbf009a 84 /* Set up custom service */
gmehmet 1:f60eafbf009a 85 GattCharacteristic *characteristics[] = {&writeChar,&notifyChar};
gmehmet 1:f60eafbf009a 86 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
gmehmet 1:f60eafbf009a 87
gmehmet 1:f60eafbf009a 88 // Temporary Fixes to be removed
gmehmet 1:f60eafbf009a 89 volatile BLE_State bleState = BLE_STARTING;
gmehmet 1:f60eafbf009a 90
gmehmet 1:f60eafbf009a 91 // end of Temporary Fixes to be removed
gmehmet 1:f60eafbf009a 92
gmehmet 1:f60eafbf009a 93 // sc...
gmehmet 1:f60eafbf009a 94 struct queue_t BLEQUEUE;
gmehmet 1:f60eafbf009a 95 static uint8_t BLEOutBuffer[BLE_NOTIFY_CHAR_ARR_SIZE * MAX_BLE_QUEUE];
gmehmet 1:f60eafbf009a 96
gmehmet 1:f60eafbf009a 97 static DSInterface *BLE_DS_INTERFACE;
gmehmet 1:f60eafbf009a 98
gmehmet 1:f60eafbf009a 99
gmehmet 1:f60eafbf009a 100 /*
gmehmet 1:f60eafbf009a 101 * Handle writes to writeCharacteristic
gmehmet 1:f60eafbf009a 102 */
gmehmet 1:f60eafbf009a 103 void writeCharCallback(const GattWriteCallbackParams *params)
gmehmet 1:f60eafbf009a 104 {
gmehmet 1:f60eafbf009a 105 uint8_t data[BLE_READWRITE_CHAR_ARR_SIZE] = {0};
gmehmet 1:f60eafbf009a 106 /* Check to see what characteristic was written, by handle */
gmehmet 1:f60eafbf009a 107 printf("writeCharCallback %p\r\n", Thread::gettid());
gmehmet 1:f60eafbf009a 108 if(params->handle == writeChar.getValueHandle()) {
gmehmet 1:f60eafbf009a 109 printf("Data received: length = %d, data = 0x",params->len);
gmehmet 1:f60eafbf009a 110 for(int x=0; x < params->len; x++) {
gmehmet 1:f60eafbf009a 111 if ((BLE_DS_INTERFACE != NULL) && (params->data[x] != 0)) {
gmehmet 1:f60eafbf009a 112 BLE_DS_INTERFACE->build_command((char)params->data[x]);
gmehmet 1:f60eafbf009a 113 }
gmehmet 1:f60eafbf009a 114 printf("%x-", params->data[x]);
gmehmet 1:f60eafbf009a 115 }
gmehmet 1:f60eafbf009a 116 printf("\n\r");
gmehmet 1:f60eafbf009a 117 }
gmehmet 1:f60eafbf009a 118 /* Update the notifyChar with the value of writeChar */
gmehmet 1:f60eafbf009a 119 BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(writeChar.getValueHandle(), data, BLE_READWRITE_CHAR_ARR_SIZE);
gmehmet 1:f60eafbf009a 120 }
gmehmet 1:f60eafbf009a 121
gmehmet 1:f60eafbf009a 122 /**
gmehmet 1:f60eafbf009a 123 * This function is called when the ble initialization process has failed
gmehmet 1:f60eafbf009a 124 */
gmehmet 1:f60eafbf009a 125 void onBleInitError(BLE &ble, ble_error_t error)
gmehmet 1:f60eafbf009a 126 {
gmehmet 1:f60eafbf009a 127 printf("errro %d\r\n", __LINE__);
gmehmet 1:f60eafbf009a 128 /* Avoid compiler warnings */
gmehmet 1:f60eafbf009a 129 (void) ble;
gmehmet 1:f60eafbf009a 130 (void) error;
gmehmet 1:f60eafbf009a 131 /* Initialization error handling should go here */
gmehmet 1:f60eafbf009a 132 }
gmehmet 1:f60eafbf009a 133
gmehmet 1:f60eafbf009a 134 /* Restart Advertising on disconnection*/
gmehmet 1:f60eafbf009a 135 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
gmehmet 1:f60eafbf009a 136 {
gmehmet 1:f60eafbf009a 137 pr_debug("disconnectionCallback %p\r\n", Thread::gettid());
gmehmet 1:f60eafbf009a 138 #if defined(USE_BLE_TICKER_TO_CHECK_TRANSFER)
gmehmet 1:f60eafbf009a 139 TICKER_BLE.detach();
gmehmet 1:f60eafbf009a 140 pr_debug("detached disconnectionCallback\r\n");
gmehmet 1:f60eafbf009a 141 Ble_Can_Transfer_Set(false);
gmehmet 1:f60eafbf009a 142 #endif
gmehmet 1:f60eafbf009a 143 bleState = BLE_DISCONNECTED;
gmehmet 1:f60eafbf009a 144 BLE::Instance().gap().startAdvertising();
gmehmet 1:f60eafbf009a 145 BLE_DS_INTERFACE->ds_set_ble_status(false);
gmehmet 1:f60eafbf009a 146 queue_reset(&BLEQUEUE);
gmehmet 1:f60eafbf009a 147 }
gmehmet 1:f60eafbf009a 148
gmehmet 1:f60eafbf009a 149 /* Connection */
gmehmet 1:f60eafbf009a 150 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
gmehmet 1:f60eafbf009a 151 {
gmehmet 1:f60eafbf009a 152 pr_err("connectionCallback %p\r\n", Thread::gettid());
gmehmet 1:f60eafbf009a 153
gmehmet 1:f60eafbf009a 154 Gap::ConnectionParams_t newParams = {
gmehmet 1:f60eafbf009a 155 .minConnectionInterval = 6, /**< Minimum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
gmehmet 1:f60eafbf009a 156 .maxConnectionInterval = 9, /**< Maximum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
gmehmet 1:f60eafbf009a 157 .slaveLatency = 0, /**< Slave Latency in number of connection events, see BLE_GAP_CP_LIMITS.*/
gmehmet 1:f60eafbf009a 158 .connectionSupervisionTimeout = 600 /**< Connection Supervision Timeout in 10 ms units, see BLE_GAP_CP_LIMITS.*/
gmehmet 1:f60eafbf009a 159 };
gmehmet 1:f60eafbf009a 160
gmehmet 1:f60eafbf009a 161 BLE::Instance().gap().updateConnectionParams(params->handle, &newParams);
gmehmet 1:f60eafbf009a 162 BLE::Instance().gap().stopAdvertising();
gmehmet 1:f60eafbf009a 163 BLE_DS_INTERFACE->ds_set_ble_status(true);
gmehmet 1:f60eafbf009a 164 #if defined(USE_BLE_TICKER_TO_CHECK_TRANSFER)
gmehmet 1:f60eafbf009a 165 TICKER_BLE.attach(&Ble_Can_Transfer_Toggle, BLE_TICKER_PERIOD);
gmehmet 1:f60eafbf009a 166 pr_debug("Attached connectionCallback\r\n");
gmehmet 1:f60eafbf009a 167 #endif
gmehmet 1:f60eafbf009a 168 //m.sensor_enable(1);
gmehmet 1:f60eafbf009a 169 bleState = BLE_CONNECTED;
gmehmet 1:f60eafbf009a 170 }
gmehmet 1:f60eafbf009a 171
gmehmet 1:f60eafbf009a 172 /**
gmehmet 1:f60eafbf009a 173 * Callback triggered when the ble initialization process has finished
gmehmet 1:f60eafbf009a 174 */
gmehmet 1:f60eafbf009a 175 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
gmehmet 1:f60eafbf009a 176 {
gmehmet 1:f60eafbf009a 177 int ret;
gmehmet 1:f60eafbf009a 178 BLE& ble = params->ble;
gmehmet 1:f60eafbf009a 179 ble_error_t error = params->error;
gmehmet 1:f60eafbf009a 180
gmehmet 1:f60eafbf009a 181 if (error != BLE_ERROR_NONE) {
gmehmet 1:f60eafbf009a 182 printf("errro %d\r\n", __LINE__);
gmehmet 1:f60eafbf009a 183 /* In case of error, forward the error handling to onBleInitError */
gmehmet 1:f60eafbf009a 184 onBleInitError(ble, error);
gmehmet 1:f60eafbf009a 185 printf("errro %d\r\n", __LINE__);
gmehmet 1:f60eafbf009a 186 return;
gmehmet 1:f60eafbf009a 187 }
gmehmet 1:f60eafbf009a 188 /* Ensure that it is the default instance of BLE */
gmehmet 1:f60eafbf009a 189 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
gmehmet 1:f60eafbf009a 190 printf("errro %d\r\n", __LINE__);
gmehmet 1:f60eafbf009a 191 return;
gmehmet 1:f60eafbf009a 192 }
gmehmet 1:f60eafbf009a 193 ble.gap().onDisconnection(disconnectionCallback);
gmehmet 1:f60eafbf009a 194 ble.gap().onConnection(connectionCallback);
gmehmet 1:f60eafbf009a 195 ble.gattServer().onDataWritten(writeCharCallback);
gmehmet 1:f60eafbf009a 196 /* Setup advertising */
gmehmet 1:f60eafbf009a 197 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
gmehmet 1:f60eafbf009a 198 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
gmehmet 1:f60eafbf009a 199 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
gmehmet 1:f60eafbf009a 200 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
gmehmet 1:f60eafbf009a 201 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG);
gmehmet 1:f60eafbf009a 202 ble.gap().setAdvertisingInterval(100); // 100ms.
gmehmet 1:f60eafbf009a 203 /* Add our custom service */
gmehmet 1:f60eafbf009a 204 ble.gattServer().addService(customService);
gmehmet 1:f60eafbf009a 205 printf("bleInitComplete\n");
gmehmet 1:f60eafbf009a 206 ble.gap().startAdvertising();
gmehmet 1:f60eafbf009a 207
gmehmet 1:f60eafbf009a 208 ret = queue_init(&BLEQUEUE, BLEOutBuffer, BLE_NOTIFY_CHAR_ARR_SIZE, BLE_NOTIFY_CHAR_ARR_SIZE * MAX_BLE_QUEUE);
gmehmet 1:f60eafbf009a 209 if(ret != 0)
gmehmet 1:f60eafbf009a 210 printf("queue_init has failed\r\n");
gmehmet 1:f60eafbf009a 211 }
gmehmet 1:f60eafbf009a 212
gmehmet 1:f60eafbf009a 213 int BLE_Icarus_TransferData(uint8_t data_transfer[20]){
gmehmet 1:f60eafbf009a 214 int ret;
gmehmet 1:f60eafbf009a 215 ret = BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(notifyChar.getValueHandle(), data_transfer, 20);
gmehmet 1:f60eafbf009a 216 return ret;
gmehmet 1:f60eafbf009a 217 }
gmehmet 1:f60eafbf009a 218
gmehmet 1:f60eafbf009a 219 int BLE_Icarus_TransferDataFromQueue(){
gmehmet 1:f60eafbf009a 220 int ret;
gmehmet 1:f60eafbf009a 221 uint8_t data_transfer[20];
gmehmet 1:f60eafbf009a 222 unsigned char i;
gmehmet 1:f60eafbf009a 223
gmehmet 1:f60eafbf009a 224 if (BLEQUEUE.num_item >= 1) {
gmehmet 1:f60eafbf009a 225 #if defined(USE_BLE_TICKER_TO_CHECK_TRANSFER)
gmehmet 1:f60eafbf009a 226 if(!Ble_Can_Transfer_Check())
gmehmet 1:f60eafbf009a 227 return 0;
gmehmet 1:f60eafbf009a 228 #endif
gmehmet 1:f60eafbf009a 229 for(i = 0; i < BLE_CONN_INT_PACKET; ++i){
gmehmet 1:f60eafbf009a 230 ret = dequeue(&BLEQUEUE, data_transfer);
gmehmet 1:f60eafbf009a 231 if(ret < 0)
gmehmet 1:f60eafbf009a 232 break;
gmehmet 1:f60eafbf009a 233 pr_debug("dequeued data for tx, %d remain\r\n", BLEQUEUE.num_item);
gmehmet 1:f60eafbf009a 234 BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(notifyChar.getValueHandle(), data_transfer, 20);
gmehmet 1:f60eafbf009a 235 }
gmehmet 1:f60eafbf009a 236 #if defined(USE_BLE_TICKER_TO_CHECK_TRANSFER)
gmehmet 1:f60eafbf009a 237 TICKER_BLE.attach(&Ble_Can_Transfer_Toggle, BLE_TICKER_PERIOD);
gmehmet 1:f60eafbf009a 238 Ble_Can_Transfer_Set(false);
gmehmet 1:f60eafbf009a 239 #endif
gmehmet 1:f60eafbf009a 240 }
gmehmet 1:f60eafbf009a 241
gmehmet 1:f60eafbf009a 242 return 0;
gmehmet 1:f60eafbf009a 243 }
gmehmet 1:f60eafbf009a 244
gmehmet 1:f60eafbf009a 245
gmehmet 1:f60eafbf009a 246 //TODO: check that function for memory safety (no overflow should occur)
gmehmet 1:f60eafbf009a 247 int BLE_Icarus_AddtoQueue(uint8_t *data_transfer, int32_t buf_size, int32_t data_size) {
gmehmet 1:f60eafbf009a 248 int ret = 0;
gmehmet 1:f60eafbf009a 249 //printf("size is: %d\r\n", size);
gmehmet 1:f60eafbf009a 250 // TODO: Append a known character to the byte array in case size is
gmehmet 1:f60eafbf009a 251 // less than 20 bytes
gmehmet 1:f60eafbf009a 252 while ((data_size % BLE_NOTIFY_CHAR_ARR_SIZE) && data_size < buf_size)
gmehmet 1:f60eafbf009a 253 data_transfer[data_size++] = 0;
gmehmet 1:f60eafbf009a 254 mxm_assert_msg(!(data_size % 20), "BLE packet size must be multiple of 20 bytes");
gmehmet 1:f60eafbf009a 255
gmehmet 1:f60eafbf009a 256 while(data_size > 0){
gmehmet 1:f60eafbf009a 257 ret = enqueue(&BLEQUEUE, data_transfer);
gmehmet 1:f60eafbf009a 258 data_size -= BLE_NOTIFY_CHAR_ARR_SIZE;
gmehmet 1:f60eafbf009a 259 data_transfer += BLE_NOTIFY_CHAR_ARR_SIZE;
gmehmet 1:f60eafbf009a 260 }
gmehmet 1:f60eafbf009a 261
gmehmet 1:f60eafbf009a 262 if(ret != 0)
gmehmet 1:f60eafbf009a 263 printf("BLE_Icarus_AddtoQueue has failed\r\n");
gmehmet 1:f60eafbf009a 264
gmehmet 1:f60eafbf009a 265 return ret;
gmehmet 1:f60eafbf009a 266 }
gmehmet 1:f60eafbf009a 267
gmehmet 1:f60eafbf009a 268
gmehmet 1:f60eafbf009a 269 int BLE_Icarus_SetDSInterface(DSInterface *comm_obj) {
gmehmet 1:f60eafbf009a 270 BLE_DS_INTERFACE = comm_obj;
gmehmet 1:f60eafbf009a 271 return 0;
gmehmet 1:f60eafbf009a 272 }
gmehmet 1:f60eafbf009a 273
gmehmet 1:f60eafbf009a 274 bool BLE_Icarus_Interface_Exists()
gmehmet 1:f60eafbf009a 275 {
gmehmet 1:f60eafbf009a 276 return (bleState == BLE_CONNECTED);
gmehmet 1:f60eafbf009a 277 }
gmehmet 1:f60eafbf009a 278
gmehmet 1:f60eafbf009a 279 int BLE_ICARUS_Get_Mac_Address(char MacAdress[6]){
gmehmet 1:f60eafbf009a 280 /* Print out device MAC address to the console*/
gmehmet 1:f60eafbf009a 281 Gap::AddressType_t addr_type;
gmehmet 1:f60eafbf009a 282 Gap::Address_t address;
gmehmet 1:f60eafbf009a 283 if(BLE::Instance().gap().getAddress(&addr_type, address) != 0)
gmehmet 1:f60eafbf009a 284 return -1;
gmehmet 1:f60eafbf009a 285 for (int i = 5; i >= 0; i--){
gmehmet 1:f60eafbf009a 286 MacAdress[5-i] = address[i];
gmehmet 1:f60eafbf009a 287 }
gmehmet 1:f60eafbf009a 288 pr_info("BLE_ADV_NAME:%s", DEVICE_NAME);
gmehmet 1:f60eafbf009a 289 return 0;
gmehmet 1:f60eafbf009a 290 }