BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:52:03 2016 +0100
Branch:
3666c65cd67a49bd0e607ef237508b504fff77a1
Revision:
295:480f017cd04d
Parent:
280:fbee0e3444be
Sync with 3666c65cd67a49bd0e607ef237508b504fff77a1

2016-07-28 16:05:49+02:00: Andrea Palmieri
Update include

Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>

Who changed what in which revision?

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