BLE lib
Fork of Nucleo_BLE_BlueNRG by
utils/src/Payload.cpp@0:a948f5f3904c, 2014-12-19 (annotated)
- Committer:
- sjallouli
- Date:
- Fri Dec 19 18:56:07 2014 +0000
- Revision:
- 0:a948f5f3904c
BLE_BlueNRG for Nucleo board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:a948f5f3904c | 1 | /* mbed Microcontroller Library |
sjallouli | 0:a948f5f3904c | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:a948f5f3904c | 3 | * |
sjallouli | 0:a948f5f3904c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:a948f5f3904c | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:a948f5f3904c | 6 | * You may obtain a copy of the License at |
sjallouli | 0:a948f5f3904c | 7 | * |
sjallouli | 0:a948f5f3904c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:a948f5f3904c | 9 | * |
sjallouli | 0:a948f5f3904c | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:a948f5f3904c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:a948f5f3904c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:a948f5f3904c | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:a948f5f3904c | 14 | * limitations under the License. |
sjallouli | 0:a948f5f3904c | 15 | */ |
sjallouli | 0:a948f5f3904c | 16 | |
sjallouli | 0:a948f5f3904c | 17 | #include <Payload.h> |
sjallouli | 0:a948f5f3904c | 18 | |
sjallouli | 0:a948f5f3904c | 19 | Payload::Payload() { |
sjallouli | 0:a948f5f3904c | 20 | stringLength = 0; |
sjallouli | 0:a948f5f3904c | 21 | payloadUnitCount = 0; |
sjallouli | 0:a948f5f3904c | 22 | payload = NULL; |
sjallouli | 0:a948f5f3904c | 23 | } |
sjallouli | 0:a948f5f3904c | 24 | |
sjallouli | 0:a948f5f3904c | 25 | Payload::Payload(const uint8_t *tokenString, uint8_t string_ength) { |
sjallouli | 0:a948f5f3904c | 26 | // initialize private data members |
sjallouli | 0:a948f5f3904c | 27 | stringLength = string_ength; |
sjallouli | 0:a948f5f3904c | 28 | payloadUnitCount = 0; |
sjallouli | 0:a948f5f3904c | 29 | payload = NULL; |
sjallouli | 0:a948f5f3904c | 30 | |
sjallouli | 0:a948f5f3904c | 31 | int index = 0; |
sjallouli | 0:a948f5f3904c | 32 | while( index!=stringLength) { |
sjallouli | 0:a948f5f3904c | 33 | int len=tokenString[index]; |
sjallouli | 0:a948f5f3904c | 34 | index=index+1+len; |
sjallouli | 0:a948f5f3904c | 35 | payloadUnitCount++; |
sjallouli | 0:a948f5f3904c | 36 | } |
sjallouli | 0:a948f5f3904c | 37 | |
sjallouli | 0:a948f5f3904c | 38 | UnitPayload *obj = new UnitPayload[payloadUnitCount]; |
sjallouli | 0:a948f5f3904c | 39 | int i=0; |
sjallouli | 0:a948f5f3904c | 40 | int c=0; |
sjallouli | 0:a948f5f3904c | 41 | int j,k; |
sjallouli | 0:a948f5f3904c | 42 | |
sjallouli | 0:a948f5f3904c | 43 | while(i<payloadUnitCount) |
sjallouli | 0:a948f5f3904c | 44 | { |
sjallouli | 0:a948f5f3904c | 45 | obj[i].length=tokenString[c]; |
sjallouli | 0:a948f5f3904c | 46 | obj[i].id=tokenString[c+1]; |
sjallouli | 0:a948f5f3904c | 47 | |
sjallouli | 0:a948f5f3904c | 48 | obj[i].data = new uint8_t[obj[i].length]; |
sjallouli | 0:a948f5f3904c | 49 | for(j=c+2,k=0;(j<(c+obj[i].length+1))&&(k<obj[i].length-1);j++,k++) |
sjallouli | 0:a948f5f3904c | 50 | { |
sjallouli | 0:a948f5f3904c | 51 | obj[i].data[k]=tokenString[j]; |
sjallouli | 0:a948f5f3904c | 52 | |
sjallouli | 0:a948f5f3904c | 53 | } |
sjallouli | 0:a948f5f3904c | 54 | |
sjallouli | 0:a948f5f3904c | 55 | c=c+obj[i].length+1; |
sjallouli | 0:a948f5f3904c | 56 | i++; |
sjallouli | 0:a948f5f3904c | 57 | |
sjallouli | 0:a948f5f3904c | 58 | } |
sjallouli | 0:a948f5f3904c | 59 | payload = obj; |
sjallouli | 0:a948f5f3904c | 60 | } |
sjallouli | 0:a948f5f3904c | 61 | |
sjallouli | 0:a948f5f3904c | 62 | uint8_t Payload::getPayloadUnitCount() { |
sjallouli | 0:a948f5f3904c | 63 | return payloadUnitCount; |
sjallouli | 0:a948f5f3904c | 64 | } |
sjallouli | 0:a948f5f3904c | 65 | |
sjallouli | 0:a948f5f3904c | 66 | uint8_t Payload::getIDAtIndex(int index) { |
sjallouli | 0:a948f5f3904c | 67 | return payload[index].get_id(); |
sjallouli | 0:a948f5f3904c | 68 | } |
sjallouli | 0:a948f5f3904c | 69 | |
sjallouli | 0:a948f5f3904c | 70 | uint8_t Payload::getLengthAtIndex(int index) { |
sjallouli | 0:a948f5f3904c | 71 | return payload[index].get_length(); |
sjallouli | 0:a948f5f3904c | 72 | } |
sjallouli | 0:a948f5f3904c | 73 | |
sjallouli | 0:a948f5f3904c | 74 | uint8_t* Payload::getDataAtIndex(int index) { |
sjallouli | 0:a948f5f3904c | 75 | return payload[index].get_data(); |
sjallouli | 0:a948f5f3904c | 76 | } |
sjallouli | 0:a948f5f3904c | 77 | |
sjallouli | 0:a948f5f3904c | 78 | int8_t Payload::getInt8AtIndex(int index) { |
sjallouli | 0:a948f5f3904c | 79 | uint8_t* str = payload[index].get_data(); |
sjallouli | 0:a948f5f3904c | 80 | int8_t value = (int8_t)str[0]; |
sjallouli | 0:a948f5f3904c | 81 | return value; |
sjallouli | 0:a948f5f3904c | 82 | } |
sjallouli | 0:a948f5f3904c | 83 | |
sjallouli | 0:a948f5f3904c | 84 | uint16_t Payload::getUint16AtIndex(int index) { |
sjallouli | 0:a948f5f3904c | 85 | uint16_t* str = (uint16_t*)payload[index].get_data(); |
sjallouli | 0:a948f5f3904c | 86 | uint16_t value = str[0]; |
sjallouli | 0:a948f5f3904c | 87 | return value; |
sjallouli | 0:a948f5f3904c | 88 | } |
sjallouli | 0:a948f5f3904c | 89 | |
sjallouli | 0:a948f5f3904c | 90 | uint8_t* Payload::getSerializedAdDataAtIndex(int index) { |
sjallouli | 0:a948f5f3904c | 91 | uint8_t length = payload[index].get_length(); |
sjallouli | 0:a948f5f3904c | 92 | uint8_t* data = payload[index].get_data(); |
sjallouli | 0:a948f5f3904c | 93 | uint8_t id = payload[index].get_id(); |
sjallouli | 0:a948f5f3904c | 94 | uint8_t *serializedAdData = new uint8_t[length]; |
sjallouli | 0:a948f5f3904c | 95 | |
sjallouli | 0:a948f5f3904c | 96 | serializedAdData[0] = id; |
sjallouli | 0:a948f5f3904c | 97 | for(int i=0; i<length-1; i++) { |
sjallouli | 0:a948f5f3904c | 98 | serializedAdData[i+1] = data[i]; |
sjallouli | 0:a948f5f3904c | 99 | } |
sjallouli | 0:a948f5f3904c | 100 | return serializedAdData; |
sjallouli | 0:a948f5f3904c | 101 | } |