ble

Dependencies:   HC_SR04_Ultrasonic_Library Servo mbed

Fork of FIP_REV1 by Robotique FIP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Payload.cpp Source File

Payload.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <Payload.h>   
00018    
00019    Payload::Payload() {
00020         stringLength = 0;
00021         payloadUnitCount = 0;
00022         payload = NULL;
00023     }
00024     
00025     Payload::Payload(const uint8_t *tokenString, uint8_t string_ength) {
00026         // initialize private data members
00027         stringLength = string_ength;
00028         payloadUnitCount = 0;
00029         payload = NULL;
00030         
00031         int index = 0;
00032         while( index!=stringLength) {
00033             int len=tokenString[index];
00034             index=index+1+len;
00035             payloadUnitCount++;               
00036         }
00037     
00038         UnitPayload *obj = new UnitPayload[payloadUnitCount];
00039         int i=0;
00040         int c=0;
00041         int j,k;
00042 
00043         while(i<payloadUnitCount)
00044         {   
00045             obj[i].length=tokenString[c];
00046             obj[i].id=tokenString[c+1];
00047 
00048             obj[i].data = new uint8_t[obj[i].length];
00049             for(j=c+2,k=0;(j<(c+obj[i].length+1))&&(k<obj[i].length-1);j++,k++)
00050             {
00051                 obj[i].data[k]=tokenString[j];
00052         
00053             }           
00054             
00055             c=c+obj[i].length+1;
00056             i++;
00057 
00058         }
00059         payload = obj;
00060     }
00061 
00062 uint8_t Payload::getPayloadUnitCount() {
00063     return payloadUnitCount;
00064 }
00065 
00066 uint8_t Payload::getIDAtIndex(int index) {
00067     return payload[index].get_id();
00068 }
00069 
00070 uint8_t Payload::getLengthAtIndex(int index) {
00071     return payload[index].get_length();
00072 }
00073 
00074 uint8_t* Payload::getDataAtIndex(int index) {
00075     return payload[index].get_data();
00076 }
00077 
00078 int8_t Payload::getInt8AtIndex(int index) {
00079     uint8_t* str = payload[index].get_data();
00080     int8_t value = (int8_t)str[0];
00081     return value;
00082 }
00083 
00084 uint16_t Payload::getUint16AtIndex(int index) {
00085     uint16_t* str = (uint16_t*)payload[index].get_data();
00086     uint16_t value = str[0];
00087     return value;    
00088     }
00089     
00090 uint8_t* Payload::getSerializedAdDataAtIndex(int index) {
00091     uint8_t length = payload[index].get_length();
00092     uint8_t* data = payload[index].get_data();
00093     uint8_t id = payload[index].get_id();
00094     uint8_t *serializedAdData = new uint8_t[length];
00095     
00096     serializedAdData[0] = id;
00097     for(int i=0; i<length-1; i++) {
00098             serializedAdData[i+1] = data[i];
00099         }
00100     return serializedAdData;
00101     }