My fork of X_NUCLEO_IDB0XA1
Fork of X_NUCLEO_IDB0XA1 by
source/utils/Payload.cpp@236:2a73f3a97452, 2016-06-09 (annotated)
- Committer:
- Andrea Palmieri
- Date:
- Thu Jun 09 17:16:36 2016 +0200
- Revision:
- 236:2a73f3a97452
- Parent:
- 132:51056160fa4a
Fix issue on ADV payload de-allocation
Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>
Who changed what in which revision?
User | Revision | Line number | New 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 | |
Wolfgang Betz |
132:51056160fa4a | 17 | #include <Payload.h> |
Wolfgang Betz |
132:51056160fa4a | 18 | |
Wolfgang Betz |
132:51056160fa4a | 19 | Payload::Payload() { |
Wolfgang Betz |
132:51056160fa4a | 20 | stringLength = 0; |
Wolfgang Betz |
132:51056160fa4a | 21 | payloadUnitCount = 0; |
Wolfgang Betz |
132:51056160fa4a | 22 | payload = NULL; |
Wolfgang Betz |
132:51056160fa4a | 23 | } |
Wolfgang Betz |
132:51056160fa4a | 24 | |
Wolfgang Betz |
132:51056160fa4a | 25 | Payload::Payload(const uint8_t *tokenString, uint8_t string_ength) { |
Wolfgang Betz |
132:51056160fa4a | 26 | // initialize private data members |
Wolfgang Betz |
132:51056160fa4a | 27 | stringLength = string_ength; |
Wolfgang Betz |
132:51056160fa4a | 28 | payloadUnitCount = 0; |
Wolfgang Betz |
132:51056160fa4a | 29 | payload = NULL; |
Wolfgang Betz |
132:51056160fa4a | 30 | |
Wolfgang Betz |
132:51056160fa4a | 31 | int index = 0; |
Wolfgang Betz |
132:51056160fa4a | 32 | while( index!=stringLength) { |
Wolfgang Betz |
132:51056160fa4a | 33 | int len=tokenString[index]; |
Wolfgang Betz |
132:51056160fa4a | 34 | index=index+1+len; |
Wolfgang Betz |
132:51056160fa4a | 35 | payloadUnitCount++; |
Wolfgang Betz |
132:51056160fa4a | 36 | } |
Wolfgang Betz |
132:51056160fa4a | 37 | |
Wolfgang Betz |
132:51056160fa4a | 38 | UnitPayload *obj = new UnitPayload[payloadUnitCount]; |
Wolfgang Betz |
132:51056160fa4a | 39 | int i=0; |
Wolfgang Betz |
132:51056160fa4a | 40 | int c=0; |
Wolfgang Betz |
132:51056160fa4a | 41 | int j,k; |
Wolfgang Betz |
132:51056160fa4a | 42 | |
Wolfgang Betz |
132:51056160fa4a | 43 | while(i<payloadUnitCount) |
Wolfgang Betz |
132:51056160fa4a | 44 | { |
Wolfgang Betz |
132:51056160fa4a | 45 | obj[i].length=tokenString[c]; |
Wolfgang Betz |
132:51056160fa4a | 46 | obj[i].id=tokenString[c+1]; |
Wolfgang Betz |
132:51056160fa4a | 47 | |
Wolfgang Betz |
132:51056160fa4a | 48 | obj[i].data = new uint8_t[obj[i].length]; |
Wolfgang Betz |
132:51056160fa4a | 49 | for(j=c+2,k=0;(j<(c+obj[i].length+1))&&(k<obj[i].length-1);j++,k++) |
Wolfgang Betz |
132:51056160fa4a | 50 | { |
Wolfgang Betz |
132:51056160fa4a | 51 | obj[i].data[k]=tokenString[j]; |
Wolfgang Betz |
132:51056160fa4a | 52 | |
Wolfgang Betz |
132:51056160fa4a | 53 | } |
Wolfgang Betz |
132:51056160fa4a | 54 | |
Wolfgang Betz |
132:51056160fa4a | 55 | c=c+obj[i].length+1; |
Wolfgang Betz |
132:51056160fa4a | 56 | i++; |
Wolfgang Betz |
132:51056160fa4a | 57 | |
Wolfgang Betz |
132:51056160fa4a | 58 | } |
Wolfgang Betz |
132:51056160fa4a | 59 | payload = obj; |
Wolfgang Betz |
132:51056160fa4a | 60 | } |
Wolfgang Betz |
132:51056160fa4a | 61 | |
Wolfgang Betz |
132:51056160fa4a | 62 | uint8_t Payload::getPayloadUnitCount() { |
Wolfgang Betz |
132:51056160fa4a | 63 | return payloadUnitCount; |
Wolfgang Betz |
132:51056160fa4a | 64 | } |
Wolfgang Betz |
132:51056160fa4a | 65 | |
Wolfgang Betz |
132:51056160fa4a | 66 | uint8_t Payload::getIDAtIndex(int index) { |
Wolfgang Betz |
132:51056160fa4a | 67 | return payload[index].get_id(); |
Wolfgang Betz |
132:51056160fa4a | 68 | } |
Wolfgang Betz |
132:51056160fa4a | 69 | |
Wolfgang Betz |
132:51056160fa4a | 70 | uint8_t Payload::getLengthAtIndex(int index) { |
Wolfgang Betz |
132:51056160fa4a | 71 | return payload[index].get_length(); |
Wolfgang Betz |
132:51056160fa4a | 72 | } |
Wolfgang Betz |
132:51056160fa4a | 73 | |
Wolfgang Betz |
132:51056160fa4a | 74 | uint8_t* Payload::getDataAtIndex(int index) { |
Wolfgang Betz |
132:51056160fa4a | 75 | return payload[index].get_data(); |
Wolfgang Betz |
132:51056160fa4a | 76 | } |
Wolfgang Betz |
132:51056160fa4a | 77 | |
Wolfgang Betz |
132:51056160fa4a | 78 | int8_t Payload::getInt8AtIndex(int index) { |
Wolfgang Betz |
132:51056160fa4a | 79 | uint8_t* str = payload[index].get_data(); |
Wolfgang Betz |
132:51056160fa4a | 80 | int8_t value = (int8_t)str[0]; |
Wolfgang Betz |
132:51056160fa4a | 81 | return value; |
Wolfgang Betz |
132:51056160fa4a | 82 | } |
Wolfgang Betz |
132:51056160fa4a | 83 | |
Wolfgang Betz |
132:51056160fa4a | 84 | uint16_t Payload::getUint16AtIndex(int index) { |
Wolfgang Betz |
132:51056160fa4a | 85 | uint16_t* str = (uint16_t*)payload[index].get_data(); |
Wolfgang Betz |
132:51056160fa4a | 86 | uint16_t value = str[0]; |
Wolfgang Betz |
132:51056160fa4a | 87 | return value; |
Wolfgang Betz |
132:51056160fa4a | 88 | } |
Wolfgang Betz |
132:51056160fa4a | 89 | |
Wolfgang Betz |
132:51056160fa4a | 90 | uint8_t* Payload::getSerializedAdDataAtIndex(int index) { |
Wolfgang Betz |
132:51056160fa4a | 91 | uint8_t length = payload[index].get_length(); |
Wolfgang Betz |
132:51056160fa4a | 92 | uint8_t* data = payload[index].get_data(); |
Wolfgang Betz |
132:51056160fa4a | 93 | uint8_t id = payload[index].get_id(); |
Wolfgang Betz |
132:51056160fa4a | 94 | uint8_t *serializedAdData = new uint8_t[length]; |
Wolfgang Betz |
132:51056160fa4a | 95 | |
Wolfgang Betz |
132:51056160fa4a | 96 | serializedAdData[0] = id; |
Wolfgang Betz |
132:51056160fa4a | 97 | for(int i=0; i<length-1; i++) { |
Wolfgang Betz |
132:51056160fa4a | 98 | serializedAdData[i+1] = data[i]; |
Wolfgang Betz |
132:51056160fa4a | 99 | } |
Wolfgang Betz |
132:51056160fa4a | 100 | return serializedAdData; |
Andrea Palmieri |
236:2a73f3a97452 | 101 | } |
Andrea Palmieri |
236:2a73f3a97452 | 102 | |
Andrea Palmieri |
236:2a73f3a97452 | 103 | Payload::~Payload() { |
Andrea Palmieri |
236:2a73f3a97452 | 104 | int i = 0; |
Andrea Palmieri |
236:2a73f3a97452 | 105 | |
Andrea Palmieri |
236:2a73f3a97452 | 106 | if(payload) { |
Andrea Palmieri |
236:2a73f3a97452 | 107 | while(i<payloadUnitCount) { |
Andrea Palmieri |
236:2a73f3a97452 | 108 | if(payload->data) { |
Andrea Palmieri |
236:2a73f3a97452 | 109 | delete[] payload->data; |
Andrea Palmieri |
236:2a73f3a97452 | 110 | payload->data = NULL; |
Andrea Palmieri |
236:2a73f3a97452 | 111 | } |
Andrea Palmieri |
236:2a73f3a97452 | 112 | } |
Andrea Palmieri |
236:2a73f3a97452 | 113 | delete[] payload; |
Andrea Palmieri |
236:2a73f3a97452 | 114 | payload = NULL; |
Andrea Palmieri |
236:2a73f3a97452 | 115 | } |
Andrea Palmieri |
236:2a73f3a97452 | 116 | |
Andrea Palmieri |
236:2a73f3a97452 | 117 | } |
Andrea Palmieri |
236:2a73f3a97452 | 118 |