HW layer for the Nucleo board, it only work with old BLE_API

Dependents:   Hello_BLE F446RE-BLE

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
hemddabral
Date:
Tue Oct 07 10:13:02 2014 +0000
Revision:
58:027c65a54097
Parent:
18:6d5f4c638f6a
Child:
60:b0b2d639c0f5
code cleanup and indentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hemddabral 12:a5d7db4a1e49 1 /* mbed Microcontroller Library
hemddabral 58:027c65a54097 2 * Copyright (c) 2006-2013 ARM Limited
hemddabral 58:027c65a54097 3 *
hemddabral 58:027c65a54097 4 * Licensed under the Apache License, Version 2.0 (the "License");
hemddabral 58:027c65a54097 5 * you may not use this file except in compliance with the License.
hemddabral 58:027c65a54097 6 * You may obtain a copy of the License at
hemddabral 58:027c65a54097 7 *
hemddabral 58:027c65a54097 8 * http://www.apache.org/licenses/LICENSE-2.0
hemddabral 58:027c65a54097 9 *
hemddabral 58:027c65a54097 10 * Unless required by applicable law or agreed to in writing, software
hemddabral 58:027c65a54097 11 * distributed under the License is distributed on an "AS IS" BASIS,
hemddabral 58:027c65a54097 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
hemddabral 58:027c65a54097 13 * See the License for the specific language governing permissions and
hemddabral 58:027c65a54097 14 * limitations under the License.
hemddabral 58:027c65a54097 15 */
hemddabral 58:027c65a54097 16
hemddabral 12:a5d7db4a1e49 17 #include "mbed.h"
hemddabral 12:a5d7db4a1e49 18
hemddabral 12:a5d7db4a1e49 19 #ifndef __PAYLOAD_H__
hemddabral 12:a5d7db4a1e49 20 #define __PAYLOAD_H__
hemddabral 12:a5d7db4a1e49 21
hemddabral 12:a5d7db4a1e49 22 class UnitPayload
hemddabral 12:a5d7db4a1e49 23 {
hemddabral 58:027c65a54097 24 public:
hemddabral 12:a5d7db4a1e49 25 uint8_t length;
hemddabral 18:6d5f4c638f6a 26 uint8_t id;
hemddabral 12:a5d7db4a1e49 27 uint8_t *data;
hemddabral 18:6d5f4c638f6a 28 uint8_t *idptr;
hemddabral 18:6d5f4c638f6a 29
hemddabral 18:6d5f4c638f6a 30
hemddabral 58:027c65a54097 31
hemddabral 12:a5d7db4a1e49 32 void set_length(uint8_t l) {
hemddabral 12:a5d7db4a1e49 33 length=l;
hemddabral 12:a5d7db4a1e49 34 }
hemddabral 12:a5d7db4a1e49 35
hemddabral 12:a5d7db4a1e49 36 void set_id(uint8_t i) {
hemddabral 12:a5d7db4a1e49 37 id=i;
hemddabral 12:a5d7db4a1e49 38 }
hemddabral 12:a5d7db4a1e49 39
hemddabral 12:a5d7db4a1e49 40 void set_data(uint8_t* data1) {
hemddabral 12:a5d7db4a1e49 41 for(int j=0;j<length;j++)
hemddabral 12:a5d7db4a1e49 42 {
hemddabral 12:a5d7db4a1e49 43 data[j]=data1[j];
hemddabral 12:a5d7db4a1e49 44 }
hemddabral 12:a5d7db4a1e49 45 }
hemddabral 12:a5d7db4a1e49 46
hemddabral 12:a5d7db4a1e49 47 uint8_t get_length() {
hemddabral 12:a5d7db4a1e49 48 return length;
hemddabral 12:a5d7db4a1e49 49 }
hemddabral 12:a5d7db4a1e49 50
hemddabral 12:a5d7db4a1e49 51 uint8_t get_id() {
hemddabral 12:a5d7db4a1e49 52 return id;
hemddabral 12:a5d7db4a1e49 53 }
hemddabral 12:a5d7db4a1e49 54
hemddabral 12:a5d7db4a1e49 55 uint8_t* get_data() {
hemddabral 12:a5d7db4a1e49 56 return data;
hemddabral 12:a5d7db4a1e49 57 }
hemddabral 12:a5d7db4a1e49 58
hemddabral 12:a5d7db4a1e49 59 };
hemddabral 12:a5d7db4a1e49 60
hemddabral 12:a5d7db4a1e49 61 class Payload {
hemddabral 12:a5d7db4a1e49 62 UnitPayload *payload;
hemddabral 12:a5d7db4a1e49 63 int stringLength;
hemddabral 12:a5d7db4a1e49 64 int payloadUnitCount;
hemddabral 12:a5d7db4a1e49 65
hemddabral 12:a5d7db4a1e49 66 public:
hemddabral 58:027c65a54097 67 Payload(const uint8_t *tokenString, uint8_t string_ength);
hemddabral 58:027c65a54097 68 Payload::Payload();
hemddabral 58:027c65a54097 69 uint8_t Payload::getPayloadUnitCount();
hemddabral 58:027c65a54097 70
hemddabral 12:a5d7db4a1e49 71 uint8_t Payload::getIDAtIndex(int index);
hemddabral 12:a5d7db4a1e49 72 uint8_t Payload::getLengthAtIndex(int index);
hemddabral 12:a5d7db4a1e49 73 uint8_t* Payload::getDataAtIndex(int index);
hemddabral 14:baa7a1464517 74 int8_t getInt8AtIndex(int index);
hemddabral 18:6d5f4c638f6a 75 uint16_t getUint16AtIndex(int index);
hemddabral 18:6d5f4c638f6a 76 uint8_t* getSerializedAdDataAtIndex(int index);
hemddabral 12:a5d7db4a1e49 77 };
hemddabral 12:a5d7db4a1e49 78
hemddabral 18:6d5f4c638f6a 79
hemddabral 18:6d5f4c638f6a 80 class PayloadUnit {
hemddabral 58:027c65a54097 81 private:
hemddabral 58:027c65a54097 82 uint8_t* lenPtr;
hemddabral 58:027c65a54097 83 uint8_t* adTypePtr;
hemddabral 58:027c65a54097 84 uint8_t* dataPtr;
hemddabral 58:027c65a54097 85
hemddabral 58:027c65a54097 86 public:
hemddabral 58:027c65a54097 87 PayloadUnit() {
hemddabral 58:027c65a54097 88 lenPtr = NULL;
hemddabral 58:027c65a54097 89 adTypePtr = NULL;
hemddabral 58:027c65a54097 90 dataPtr = NULL;
hemddabral 58:027c65a54097 91 }
hemddabral 58:027c65a54097 92
hemddabral 58:027c65a54097 93 PayloadUnit(uint8_t *len, uint8_t *adType, uint8_t* data) {
hemddabral 58:027c65a54097 94 lenPtr = len;
hemddabral 58:027c65a54097 95 adTypePtr = adType;
hemddabral 58:027c65a54097 96 dataPtr = data;
hemddabral 58:027c65a54097 97 }
hemddabral 58:027c65a54097 98
hemddabral 58:027c65a54097 99 void setLenPtr(uint8_t *len) {
hemddabral 58:027c65a54097 100 lenPtr = len;
hemddabral 58:027c65a54097 101 }
hemddabral 58:027c65a54097 102
hemddabral 58:027c65a54097 103 void setAdTypePtr(uint8_t *adType) {
hemddabral 58:027c65a54097 104 adTypePtr = adType;
hemddabral 58:027c65a54097 105 }
hemddabral 58:027c65a54097 106
hemddabral 58:027c65a54097 107 void setDataPtr(uint8_t *data) {
hemddabral 58:027c65a54097 108 dataPtr = data;
hemddabral 58:027c65a54097 109 }
hemddabral 18:6d5f4c638f6a 110
hemddabral 58:027c65a54097 111 uint8_t* getLenPtr() {
hemddabral 58:027c65a54097 112 return lenPtr;
hemddabral 58:027c65a54097 113 }
hemddabral 58:027c65a54097 114
hemddabral 58:027c65a54097 115 uint8_t* getAdTypePtr() {
hemddabral 58:027c65a54097 116 return adTypePtr;
hemddabral 58:027c65a54097 117 }
hemddabral 58:027c65a54097 118
hemddabral 58:027c65a54097 119 uint8_t* getDataPtr() {
hemddabral 58:027c65a54097 120 return dataPtr;
hemddabral 58:027c65a54097 121 }
hemddabral 58:027c65a54097 122
hemddabral 58:027c65a54097 123 void printDataAsHex() {
hemddabral 58:027c65a54097 124 int i = 0;
hemddabral 58:027c65a54097 125 printf("AdData=");
hemddabral 58:027c65a54097 126 for(i=0; i<*lenPtr-1; i++) {
hemddabral 58:027c65a54097 127 printf("0x%x ", dataPtr[i]);
hemddabral 58:027c65a54097 128 }
hemddabral 58:027c65a54097 129 printf("\n");
hemddabral 58:027c65a54097 130 }
hemddabral 58:027c65a54097 131
hemddabral 58:027c65a54097 132 void printDataAsString() {
hemddabral 58:027c65a54097 133 int i = 0;
hemddabral 58:027c65a54097 134 printf("AdData=");
hemddabral 58:027c65a54097 135 for(i=0; i<*lenPtr; i++) {
hemddabral 58:027c65a54097 136 printf("%c", dataPtr[i]);
hemddabral 58:027c65a54097 137 }
hemddabral 58:027c65a54097 138 printf("\n");
hemddabral 58:027c65a54097 139 }
hemddabral 58:027c65a54097 140
hemddabral 58:027c65a54097 141 };
hemddabral 58:027c65a54097 142
hemddabral 18:6d5f4c638f6a 143 class PayloadPtr {
hemddabral 18:6d5f4c638f6a 144 private:
hemddabral 18:6d5f4c638f6a 145 PayloadUnit *unit;
hemddabral 18:6d5f4c638f6a 146 int payloadUnitCount;
hemddabral 18:6d5f4c638f6a 147 public:
hemddabral 18:6d5f4c638f6a 148 PayloadPtr::PayloadPtr(const uint8_t *tokenString, uint8_t string_ength) {
hemddabral 18:6d5f4c638f6a 149 // initialize private data members
hemddabral 18:6d5f4c638f6a 150 int stringLength = string_ength;
hemddabral 18:6d5f4c638f6a 151 payloadUnitCount = 0;
hemddabral 18:6d5f4c638f6a 152
hemddabral 18:6d5f4c638f6a 153 int index = 0;
hemddabral 18:6d5f4c638f6a 154 while(index!=stringLength) {
hemddabral 18:6d5f4c638f6a 155 int len=tokenString[index];
hemddabral 18:6d5f4c638f6a 156 index=index+1+len;
hemddabral 18:6d5f4c638f6a 157 payloadUnitCount++;
hemddabral 18:6d5f4c638f6a 158 }
hemddabral 18:6d5f4c638f6a 159
hemddabral 18:6d5f4c638f6a 160 // allocate memory to unit
hemddabral 18:6d5f4c638f6a 161 unit = new PayloadUnit[payloadUnitCount];
hemddabral 18:6d5f4c638f6a 162 int i = 0;
hemddabral 18:6d5f4c638f6a 163 int nextUnitOffset = 0;
hemddabral 18:6d5f4c638f6a 164
hemddabral 18:6d5f4c638f6a 165 while(i<payloadUnitCount) {
hemddabral 18:6d5f4c638f6a 166 unit[i].setLenPtr((uint8_t *)tokenString+nextUnitOffset);
hemddabral 18:6d5f4c638f6a 167 unit[i].setAdTypePtr((uint8_t *)tokenString+nextUnitOffset+1);
hemddabral 18:6d5f4c638f6a 168 unit[i].setDataPtr((uint8_t *)tokenString+nextUnitOffset+2);
hemddabral 58:027c65a54097 169
hemddabral 18:6d5f4c638f6a 170 nextUnitOffset += *unit[i].getLenPtr()+1;
hemddabral 18:6d5f4c638f6a 171 i++;
hemddabral 18:6d5f4c638f6a 172
hemddabral 18:6d5f4c638f6a 173 }
hemddabral 18:6d5f4c638f6a 174 }
hemddabral 18:6d5f4c638f6a 175
hemddabral 18:6d5f4c638f6a 176 PayloadUnit getUnitAtIndex(int index) {
hemddabral 18:6d5f4c638f6a 177 return unit[index];
hemddabral 58:027c65a54097 178 }
hemddabral 18:6d5f4c638f6a 179
hemddabral 18:6d5f4c638f6a 180 int getPayloadUnitCount() { return payloadUnitCount; }
hemddabral 58:027c65a54097 181
hemddabral 18:6d5f4c638f6a 182
hemddabral 18:6d5f4c638f6a 183 };
hemddabral 18:6d5f4c638f6a 184
hemddabral 12:a5d7db4a1e49 185 #endif // __PAYLOAD_H__