Commit 65dadfb3 authored by Melissa's avatar Melissa Committed by Melissa Elkadi

Merge branch 'episys/master-nsa-nfapi-errors' into 'episys/master-nsa'

nfapi.c: Add on_error()

See merge request aburger/openairinterface5g!96
parent a1dee602
/* /*
* Copyright (c) 2001-2016, Cisco Systems, Inc. * Copyright (c) 2001-2016, Cisco Systems, Inc.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* Redistributions of source code must retain the above copyright * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* Redistributions in binary form must reproduce the above * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following * copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided * disclaimer in the documentation and/or other materials provided
* with the distribution. * with the distribution.
* *
* Neither the name of the Cisco Systems, Inc. nor the names of its * Neither the name of the Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived * contributors may be used to endorse or promote products derived
* from this software without specific prior written permission. * from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <signal.h> #include <signal.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sched.h> #include <sched.h>
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <pthread.h> #include <pthread.h>
#include <execinfo.h>
#include <nfapi_interface.h>
#include <nfapi.h> #include <nfapi_interface.h>
#include <debug.h> #include <nfapi.h>
#include <debug.h>
// Fundamental routines // What to do when an error happens (e.g., a push or pull fails)
static inline void on_error()
uint8_t push8(uint8_t in, uint8_t **out, uint8_t *end) { {
uint8_t *pOut = *out; // show the call stack
int fd = STDERR_FILENO;
if((end - pOut) >= 1) { static const char msg[] = "---stack trace---\n";
pOut[0] = in; __attribute__((unused)) int r =
(*out)+=1; write(fd, msg, sizeof(msg) - 1);
return 1; void *buffer[100];
} else { int nptrs = backtrace(buffer, sizeof(buffer) / sizeof(buffer[0]));
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); backtrace_symbols_fd(buffer, nptrs, fd);
return 0;
} //abort();
} }
uint8_t pushs8(int8_t in, uint8_t **out, uint8_t *end) { // Fundamental routines
uint8_t *pOut = *out;
uint8_t push8(uint8_t in, uint8_t **out, uint8_t *end) {
if((end - pOut) >= 1) { uint8_t *pOut = *out;
pOut[0] = in;
(*out)+=1; if((end - pOut) >= 1) {
return 1; pOut[0] = in;
} else { (*out)+=1;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); return 1;
return 0; } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
} on_error();
return 0;
uint8_t push16(uint16_t in, uint8_t **out, uint8_t *end) { }
uint8_t *pOut = *out; }
if((end - pOut) >= 2) { uint8_t pushs8(int8_t in, uint8_t **out, uint8_t *end) {
pOut[0] = (in & 0xFF00) >> 8; uint8_t *pOut = *out;
pOut[1] = (in & 0xFF);
(*out)+=2; if((end - pOut) >= 1) {
return 2; pOut[0] = in;
} else { (*out)+=1;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); return 1;
return 0; } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
} on_error();
return 0;
uint8_t pushs16(int16_t in, uint8_t **out, uint8_t *end) { }
uint8_t *pOut = *out; }
if((end - pOut) >= 2) { uint8_t push16(uint16_t in, uint8_t **out, uint8_t *end) {
pOut[0] = (in & 0xFF00) >> 8; uint8_t *pOut = *out;
pOut[1] = (in & 0xFF);
(*out)+=2; if((end - pOut) >= 2) {
return 2; pOut[0] = (in & 0xFF00) >> 8;
} else { pOut[1] = (in & 0xFF);
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); (*out)+=2;
return 0; return 2;
} } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
on_error();
uint8_t push32(uint32_t in, uint8_t **out, uint8_t *end) { return 0;
uint8_t *pOut = *out; }
}
if((end - pOut) >= 4) {
pOut[0] = (in & 0xFF000000) >> 24; uint8_t pushs16(int16_t in, uint8_t **out, uint8_t *end) {
pOut[1] = (in & 0xFF0000) >> 16; uint8_t *pOut = *out;
pOut[2] = (in & 0xFF00) >> 8;
pOut[3] = (in & 0xFF); if((end - pOut) >= 2) {
(*out)+=4; pOut[0] = (in & 0xFF00) >> 8;
return 4; pOut[1] = (in & 0xFF);
} else { (*out)+=2;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); return 2;
return 0; } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
} on_error();
return 0;
uint8_t pushs32(int32_t in, uint8_t **out, uint8_t *end) { }
uint8_t *pOut = *out; }
if((end - pOut) >= 4) { uint8_t push32(uint32_t in, uint8_t **out, uint8_t *end) {
pOut[0] = (in & 0xFF000000) >> 24; uint8_t *pOut = *out;
pOut[1] = (in & 0xFF0000) >> 16;
pOut[2] = (in & 0xFF00) >> 8; if((end - pOut) >= 4) {
pOut[3] = (in & 0xFF); pOut[0] = (in & 0xFF000000) >> 24;
(*out)+=4; pOut[1] = (in & 0xFF0000) >> 16;
return 4; pOut[2] = (in & 0xFF00) >> 8;
} else { pOut[3] = (in & 0xFF);
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); (*out)+=4;
return 0; return 4;
} } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
on_error();
uint8_t pull8(uint8_t **in, uint8_t *out, uint8_t *end) { return 0;
uint8_t *pIn = *in; }
}
if((end - pIn) >= 1 ) {
*out = *pIn; uint8_t pushs32(int32_t in, uint8_t **out, uint8_t *end) {
(*in)+=1; uint8_t *pOut = *out;
return 1;
} else { if((end - pOut) >= 4) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); pOut[0] = (in & 0xFF000000) >> 24;
return 0; pOut[1] = (in & 0xFF0000) >> 16;
} pOut[2] = (in & 0xFF00) >> 8;
} pOut[3] = (in & 0xFF);
(*out)+=4;
uint8_t pulls8(uint8_t **in, int8_t *out, uint8_t *end) { return 4;
uint8_t *pIn = *in; } else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
if((end - pIn) >= 1 ) { on_error();
*out = *pIn; return 0;
(*in)+=1; }
return 1; }
} else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); uint8_t pull8(uint8_t **in, uint8_t *out, uint8_t *end) {
return 0; uint8_t *pIn = *in;
}
} if((end - pIn) >= 1 ) {
*out = *pIn;
uint8_t pull16(uint8_t **in, uint16_t *out, uint8_t *end) { (*in)+=1;
uint8_t *pIn = *in; return 1;
} else {
if((end - pIn) >=2 ) { NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
*out = ((pIn[0]) << 8) | pIn[1]; on_error();
(*in)+=2; return 0;
return 2; }
} else { }
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
return 0; uint8_t pulls8(uint8_t **in, int8_t *out, uint8_t *end) {
} uint8_t *pIn = *in;
}
if((end - pIn) >= 1 ) {
uint8_t pulls16(uint8_t **in, int16_t *out, uint8_t *end) { *out = *pIn;
uint8_t *pIn = *in; (*in)+=1;
return 1;
if((end - pIn) >=2 ) { } else {
*out = ((pIn[0]) << 8) | pIn[1]; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
(*in)+=2; on_error();
return 2; return 0;
} else { }
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); }
return 0;
} uint8_t pull16(uint8_t **in, uint16_t *out, uint8_t *end) {
} uint8_t *pIn = *in;
uint8_t pull32(uint8_t **in, uint32_t *out, uint8_t *end) { if((end - pIn) >=2 ) {
uint8_t *pIn = *in; *out = ((pIn[0]) << 8) | pIn[1];
(*in)+=2;
if((end - pIn) >=4 ) { return 2;
*out = (pIn[0] << 24) | (pIn[1] << 16) | (pIn[2] << 8) | pIn[3]; } else {
(*in)+=4; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
return 4; on_error();
} else { return 0;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); }
return 0; }
}
} uint8_t pulls16(uint8_t **in, int16_t *out, uint8_t *end) {
uint8_t *pIn = *in;
uint8_t pulls32(uint8_t **in, int32_t *out, uint8_t *end) {
uint8_t *pIn = *in; if((end - pIn) >=2 ) {
*out = ((pIn[0]) << 8) | pIn[1];
if((end - pIn) >=4 ) { (*in)+=2;
*out = (pIn[0] << 24) | (pIn[1] << 16) | (pIn[2] << 8) | pIn[3]; return 2;
(*in)+=4; } else {
return 4; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
} else { on_error();
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); return 0;
return 0; }
} }
}
uint8_t pull32(uint8_t **in, uint32_t *out, uint8_t *end) {
/* uint8_t *pIn = *in;
inline void pusharray16(uint8_t **, uint16_t, uint32_t len)
{ if((end - pIn) >=4 ) {
} *out = (pIn[0] << 24) | (pIn[1] << 16) | (pIn[2] << 8) | pIn[3];
*/ (*in)+=4;
return 4;
uint32_t pullarray16(uint8_t **in, uint16_t out[], uint32_t max_len, uint32_t len, uint8_t *end) { } else {
if(len == 0) NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
return 1; on_error();
return 0;
if(len > max_len) { }
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); }
return 0;
} uint8_t pulls32(uint8_t **in, int32_t *out, uint8_t *end) {
uint8_t *pIn = *in;
if((end - (*in)) >= sizeof(uint16_t) * len) {
uint32_t idx; if((end - pIn) >=4 ) {
*out = (pIn[0] << 24) | (pIn[1] << 16) | (pIn[2] << 8) | pIn[3];
for(idx = 0; idx < len; ++idx) { (*in)+=4;
if(!pull16(in, &out[idx], end)) return 4;
return 0; } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
on_error();
return sizeof(uint16_t) * len; return 0;
} else { }
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); }
return 0;
} /*
} inline void pusharray16(uint8_t **, uint16_t, uint32_t len)
{
uint32_t pullarrays16(uint8_t **in, int16_t out[], uint32_t max_len, uint32_t len, uint8_t *end) { }
if(len == 0) */
return 1;
uint32_t pullarray16(uint8_t **in, uint16_t out[], uint32_t max_len, uint32_t len, uint8_t *end) {
if(len > max_len) { if(len == 0)
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); return 1;
return 0;
} if(len > max_len) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
if((end - (*in)) >= sizeof(uint16_t) * len) { on_error();
uint32_t idx; return 0;
}
for(idx = 0; idx < len; ++idx) {
if(!pulls16(in, &out[idx], end)) if((end - (*in)) >= sizeof(uint16_t) * len) {
return 0; uint32_t idx;
}
for(idx = 0; idx < len; ++idx) {
return sizeof(uint16_t) * len; if(!pull16(in, &out[idx], end))
} else { return 0;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); }
return 0;
} return sizeof(uint16_t) * len;
} } else {
uint32_t pusharray16(uint16_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) { NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
if(len == 0) on_error();
return 1; return 0;
}
if(len > max_len) { }
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
return 0; uint32_t pullarrays16(uint8_t **in, int16_t out[], uint32_t max_len, uint32_t len, uint8_t *end) {
} if(len == 0)
return 1;
if((end - (*out)) >= sizeof(uint16_t) * len) {
uint32_t idx; if(len > max_len) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
for(idx = 0; idx < len; ++idx) { on_error();
if(!push16(in[idx], out, end)) return 0;
return 0; }
}
if((end - (*in)) >= sizeof(uint16_t) * len) {
return sizeof(uint16_t) * len; uint32_t idx;
} else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); for(idx = 0; idx < len; ++idx) {
return 0; if(!pulls16(in, &out[idx], end))
} return 0;
} }
uint32_t pusharrays16(int16_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) {
if(len == 0) return sizeof(uint16_t) * len;
return 1; } else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
if(len > max_len) { on_error();
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); return 0;
return 0; }
} }
uint32_t pusharray16(uint16_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) {
if((end - (*out)) >= sizeof(uint16_t) * len) { if(len == 0)
uint32_t idx; return 1;
for(idx = 0; idx < len; ++idx) { if(len > max_len) {
pushs16(in[idx], out, end); NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
} on_error();
return 0;
return sizeof(uint16_t) * len; }
} else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); if((end - (*out)) >= sizeof(uint16_t) * len) {
return 0; uint32_t idx;
}
} for(idx = 0; idx < len; ++idx) {
uint32_t pullarray32(uint8_t **in, uint32_t out[], uint32_t max_len, uint32_t len, uint8_t *end) { if(!push16(in[idx], out, end))
if(len == 0) return 0;
return 1; }
if(len > max_len) { return sizeof(uint16_t) * len;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); } else {
return 0; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
} on_error();
return 0;
if((end - (*in)) >= sizeof(uint32_t) * len) { }
uint32_t idx; }
uint32_t pusharrays16(int16_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) {
for(idx = 0; idx < len; ++idx) { if(len == 0)
if(!pull32(in, &out[idx], end)) return 1;
return 0;
} if(len > max_len) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
return sizeof(uint32_t) * len; on_error();
} else { return 0;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); }
return 0;
} if((end - (*out)) >= sizeof(uint16_t) * len) {
} uint32_t idx;
uint32_t pullarrays32(uint8_t **in, int32_t out[], uint32_t max_len, uint32_t len, uint8_t *end) { for(idx = 0; idx < len; ++idx) {
if(len == 0) if (!pushs16(in[idx], out, end))
return 1; return 0;
}
if(len > max_len) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); return sizeof(uint16_t) * len;
return 0; } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
on_error();
if((end - (*in)) >= sizeof(uint32_t) * len) { return 0;
uint32_t idx; }
}
for(idx = 0; idx < len; ++idx) { uint32_t pullarray32(uint8_t **in, uint32_t out[], uint32_t max_len, uint32_t len, uint8_t *end) {
if(!pulls32(in, &out[idx], end)) if(len == 0)
return 0; return 1;
}
if(len > max_len) {
return sizeof(uint32_t) * len; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
} else { on_error();
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); return 0;
return 0; }
}
} if((end - (*in)) >= sizeof(uint32_t) * len) {
uint32_t pusharray32(uint32_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) { uint32_t idx;
if(len == 0)
return 1; for(idx = 0; idx < len; ++idx) {
if(!pull32(in, &out[idx], end))
if(len > max_len) { return 0;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); }
return 0;
} return sizeof(uint32_t) * len;
} else {
if((end - (*out)) >= sizeof(uint32_t) * len) { NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
uint32_t idx; on_error();
return 0;
for(idx = 0; idx < len; ++idx) { }
if(!push32(in[idx], out, end)) }
return 0;
} uint32_t pullarrays32(uint8_t **in, int32_t out[], uint32_t max_len, uint32_t len, uint8_t *end) {
if(len == 0)
return sizeof(uint32_t) * len; return 1;
} else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); if(len > max_len) {
return 0; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
} on_error();
} return 0;
uint32_t pusharrays32(int32_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) { }
if(len == 0)
return 1; if((end - (*in)) >= sizeof(uint32_t) * len) {
uint32_t idx;
if(len > max_len) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); for(idx = 0; idx < len; ++idx) {
return 0; if(!pulls32(in, &out[idx], end))
} return 0;
}
if((end - (*out)) >= sizeof(uint32_t) * len) {
uint32_t idx; return sizeof(uint32_t) * len;
} else {
for(idx = 0; idx < len; ++idx) { NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
pushs32(in[idx], out, end); on_error();
} return 0;
}
return sizeof(uint32_t) * len; }
} else { uint32_t pusharray32(uint32_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); if(len == 0)
return 0; return 1;
}
} if(len > max_len) {
uint32_t pullarray8(uint8_t **in, uint8_t out[], uint32_t max_len, uint32_t len, uint8_t *end) { NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
if(len == 0) on_error();
return 1; return 0;
}
if(len > max_len) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); if((end - (*out)) >= sizeof(uint32_t) * len) {
return 0; uint32_t idx;
}
for(idx = 0; idx < len; ++idx) {
if((end - (*in)) >= sizeof(uint8_t) * len) { if(!push32(in[idx], out, end))
memcpy(out, (*in), len); return 0;
(*in)+=len; }
return sizeof(uint8_t) * len;
} else { return sizeof(uint32_t) * len;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); } else {
return 0; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
} on_error();
} return 0;
}
uint32_t pusharray8(uint8_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) { }
if(len == 0) uint32_t pusharrays32(int32_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) {
return 1; if(len == 0)
return 1;
if(len > max_len) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len); if(len > max_len) {
return 0; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
} on_error();
return 0;
if((end - (*out)) >= sizeof(uint8_t) * len) { }
memcpy((*out), in, len);
(*out)+=len; if((end - (*out)) >= sizeof(uint32_t) * len) {
return sizeof(uint8_t) * len; uint32_t idx;
} else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__); for(idx = 0; idx < len; ++idx) {
return 0; if (!pushs32(in[idx], out, end))
} return 0;
} }
uint8_t packarray(void *array, uint16_t array_element_size, uint16_t max_count, uint16_t count, uint8_t **ppwritepackedmsg, uint8_t *end, pack_array_elem_fn fn) { return sizeof(uint32_t) * len;
if(count > max_count) { } else {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, count, max_count); NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
return 0; on_error();
} return 0;
}
uint16_t i = 0; }
uint32_t pullarray8(uint8_t **in, uint8_t out[], uint32_t max_len, uint32_t len, uint8_t *end) {
for(i = 0; i < count; ++i) { if(len == 0)
if((fn)(array, ppwritepackedmsg, end) == 0) return 1;
return 0;
if(len > max_len) {
array += array_element_size; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
} on_error();
return 0;
return 1; }
}
if((end - (*in)) >= sizeof(uint8_t) * len) {
uint8_t unpackarray(uint8_t **ppReadPackedMsg, void *array, uint16_t array_element_size, uint16_t max_count, uint16_t count, uint8_t *end, unpack_array_elem_fn fn) { memcpy(out, (*in), len);
if(count > max_count) { (*in)+=len;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, count, max_count); return sizeof(uint8_t) * len;
return 0; } else {
} NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
on_error();
uint16_t i = 0; return 0;
}
for(i = 0; i < count; ++i) { }
if((fn)(array, ppReadPackedMsg, end) == 0)
return 0; uint32_t pusharray8(uint8_t in[], uint32_t max_len, uint32_t len, uint8_t **out, uint8_t *end) {
if(len == 0)
array += array_element_size; return 1;
}
if(len > max_len) {
return 1; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, len, max_len);
} on_error();
return 0;
}
uint32_t pack_vendor_extension_tlv(nfapi_tl_t *ve, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p4_p5_codec_config_t *config) {
if(ve != 0 && config != 0) { if((end - (*out)) >= sizeof(uint8_t) * len) {
if(config->pack_vendor_extension_tlv) { memcpy((*out), in, len);
uint8_t *pStartOfTlv = *ppWritePackedMsg; (*out)+=len;
return sizeof(uint8_t) * len;
if(pack_tl(ve, ppWritePackedMsg, end) == 0) } else {
return 0; NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s no space in buffer\n", __FUNCTION__);
on_error();
uint8_t *pStartOfValue = *ppWritePackedMsg; return 0;
}
if((config->pack_vendor_extension_tlv)(ve, ppWritePackedMsg, end, config) == 0) }
return 0;
uint8_t packarray(void *array, uint16_t array_element_size, uint16_t max_count, uint16_t count, uint8_t **ppwritepackedmsg, uint8_t *end, pack_array_elem_fn fn) {
ve->length = (*ppWritePackedMsg) - pStartOfValue; if(count > max_count) {
pack_tl(ve, &pStartOfTlv, end); NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, count, max_count);
return 1; on_error();
} return 0;
} }
return 1; uint16_t i = 0;
}
for(i = 0; i < count; ++i) {
uint32_t unpack_vendor_extension_tlv(nfapi_tl_t *tl, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p4_p5_codec_config_t *config, nfapi_tl_t **ve_tlv) { if((fn)(array, ppwritepackedmsg, end) == 0)
if(ve_tlv != 0 && config != 0) { return 0;
if(config->unpack_vendor_extension_tlv) {
return (config->unpack_vendor_extension_tlv)(tl, ppReadPackedMsg, end, (void **)ve_tlv, config); array += array_element_size;
} }
}
return 1;
return 1; }
}
uint8_t unpackarray(uint8_t **ppReadPackedMsg, void *array, uint16_t array_element_size, uint16_t max_count, uint16_t count, uint8_t *end, unpack_array_elem_fn fn) {
uint32_t pack_p7_vendor_extension_tlv(nfapi_tl_t *ve, uint8_t **ppWritePackedMsg, uint8_t *end,nfapi_p7_codec_config_t *config) { if(count > max_count) {
if(ve != 0 && config != 0) { NFAPI_TRACE(NFAPI_TRACE_ERROR, "%s exceed array size (%d > %d)\n", __FUNCTION__, count, max_count);
if(config->pack_vendor_extension_tlv) { on_error();
uint8_t *pStartOfTlv = *ppWritePackedMsg; return 0;
}
if(pack_tl(ve, ppWritePackedMsg, end) == 0)
return 0; uint16_t i = 0;
uint8_t *pStartOfValue = *ppWritePackedMsg; for(i = 0; i < count; ++i) {
if((fn)(array, ppReadPackedMsg, end) == 0)
if((config->pack_vendor_extension_tlv)(ve, ppWritePackedMsg, end, config) == 0) return 0;
return 0;
array += array_element_size;
ve->length = (*ppWritePackedMsg) - pStartOfValue; }
pack_tl(ve, &pStartOfTlv, end);
return 1; return 1;
} }
}
return 1; uint32_t pack_vendor_extension_tlv(nfapi_tl_t *ve, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p4_p5_codec_config_t *config) {
} if(ve != 0 && config != 0) {
if(config->pack_vendor_extension_tlv) {
int unpack_p7_vendor_extension_tlv(nfapi_tl_t *tl, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config, nfapi_tl_t **ve_tlv) { uint8_t *pStartOfTlv = *ppWritePackedMsg;
if(ve_tlv != 0 && config != 0) {
if(config->unpack_vendor_extension_tlv) { if(pack_tl(ve, ppWritePackedMsg, end) == 0)
return (config->unpack_vendor_extension_tlv)(tl, ppReadPackedMsg, end, (void **)ve_tlv, config); return 0;
}
} uint8_t *pStartOfValue = *ppWritePackedMsg;
return 1; if((config->pack_vendor_extension_tlv)(ve, ppWritePackedMsg, end, config) == 0)
} return 0;
ve->length = (*ppWritePackedMsg) - pStartOfValue;
uint8_t pack_tl(nfapi_tl_t *tl, uint8_t **ppWritePackedMsg, uint8_t *end) { pack_tl(ve, &pStartOfTlv, end);
return (push16(tl->tag, ppWritePackedMsg, end) && return 1;
push16(tl->length, ppWritePackedMsg, end)); }
} }
uint8_t unpack_tl(uint8_t **ppReadPackedMsg, nfapi_tl_t *tl, uint8_t *end) { return 1;
return (pull16(ppReadPackedMsg, &tl->tag, end) && }
pull16(ppReadPackedMsg, &tl->length, end));
} uint32_t unpack_vendor_extension_tlv(nfapi_tl_t *tl, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p4_p5_codec_config_t *config, nfapi_tl_t **ve_tlv) {
if(ve_tlv != 0 && config != 0) {
int unpack_tlv_list(unpack_tlv_t unpack_fns[], uint16_t size, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p4_p5_codec_config_t *config, nfapi_tl_t **ve) { if(config->unpack_vendor_extension_tlv) {
nfapi_tl_t generic_tl; return (config->unpack_vendor_extension_tlv)(tl, ppReadPackedMsg, end, (void **)ve_tlv, config);
uint8_t numBadTags = 0; }
uint16_t idx = 0; }
while ((uint8_t *)(*ppReadPackedMsg) < end) { return 1;
// unpack the tl and process the values accordingly }
if(unpack_tl(ppReadPackedMsg, &generic_tl, end) == 0)
return 0; uint32_t pack_p7_vendor_extension_tlv(nfapi_tl_t *ve, uint8_t **ppWritePackedMsg, uint8_t *end,nfapi_p7_codec_config_t *config) {
if(ve != 0 && config != 0) {
uint8_t tagMatch = 0; if(config->pack_vendor_extension_tlv) {
uint8_t *pStartOfValue = *ppReadPackedMsg; uint8_t *pStartOfTlv = *ppWritePackedMsg;
for(idx = 0; idx < size; ++idx) { if(pack_tl(ve, ppWritePackedMsg, end) == 0)
if(unpack_fns[idx].tag == generic_tl.tag) { // match the extracted tag value with all the tags in unpack_fn list return 0;
tagMatch = 1;
nfapi_tl_t *tl = (nfapi_tl_t *)(unpack_fns[idx].tlv); uint8_t *pStartOfValue = *ppWritePackedMsg;
tl->tag = generic_tl.tag;
tl->length = generic_tl.length; if((config->pack_vendor_extension_tlv)(ve, ppWritePackedMsg, end, config) == 0)
int result = (*unpack_fns[idx].unpack_func)(tl, ppReadPackedMsg, end); return 0;
if(result == 0) { ve->length = (*ppWritePackedMsg) - pStartOfValue;
return 0; pack_tl(ve, &pStartOfTlv, end);
} return 1;
}
// check if the length was right; }
if(tl->length != (*ppReadPackedMsg - pStartOfValue)) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Warning tlv tag 0x%x length %d not equal to unpack %ld\n", tl->tag, tl->length, (*ppReadPackedMsg - pStartOfValue)); return 1;
} }
}
} int unpack_p7_vendor_extension_tlv(nfapi_tl_t *tl, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config, nfapi_tl_t **ve_tlv) {
if(ve_tlv != 0 && config != 0) {
if(tagMatch == 0) { if(config->unpack_vendor_extension_tlv) {
if(generic_tl.tag >= NFAPI_VENDOR_EXTENSION_MIN_TAG_VALUE && return (config->unpack_vendor_extension_tlv)(tl, ppReadPackedMsg, end, (void **)ve_tlv, config);
generic_tl.tag <= NFAPI_VENDOR_EXTENSION_MAX_TAG_VALUE) { }
int result = unpack_vendor_extension_tlv(&generic_tl, ppReadPackedMsg, end, config, ve); }
if(result == 0) { return 1;
// got tot the end. }
return 0;
} else if(result < 0) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown VE TAG value: 0x%04x\n", generic_tl.tag); uint8_t pack_tl(nfapi_tl_t *tl, uint8_t **ppWritePackedMsg, uint8_t *end) {
return (push16(tl->tag, ppWritePackedMsg, end) &&
if (++numBadTags > MAX_BAD_TAG) { push16(tl->length, ppWritePackedMsg, end));
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n"); }
return 0;
} uint8_t unpack_tl(uint8_t **ppReadPackedMsg, nfapi_tl_t *tl, uint8_t *end) {
return (pull16(ppReadPackedMsg, &tl->tag, end) &&
if((end - *ppReadPackedMsg) >= generic_tl.length) { pull16(ppReadPackedMsg, &tl->length, end));
// Advance past the unknown TLV }
(*ppReadPackedMsg) += generic_tl.length;
} else { int unpack_tlv_list(unpack_tlv_t unpack_fns[], uint16_t size, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p4_p5_codec_config_t *config, nfapi_tl_t **ve) {
// go to the end nfapi_tl_t generic_tl;
return 0; uint8_t numBadTags = 0;
} uint16_t idx = 0;
}
} else { while ((uint8_t *)(*ppReadPackedMsg) < end) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown TAG value: 0x%04x\n", generic_tl.tag); // unpack the tl and process the values accordingly
if(unpack_tl(ppReadPackedMsg, &generic_tl, end) == 0)
if (++numBadTags > MAX_BAD_TAG) { return 0;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n");
return 0; uint8_t tagMatch = 0;
} uint8_t *pStartOfValue = *ppReadPackedMsg;
if((end - *ppReadPackedMsg) >= generic_tl.length) { for(idx = 0; idx < size; ++idx) {
// Advance past the unknown TLV if(unpack_fns[idx].tag == generic_tl.tag) { // match the extracted tag value with all the tags in unpack_fn list
(*ppReadPackedMsg) += generic_tl.length; tagMatch = 1;
} else { nfapi_tl_t *tl = (nfapi_tl_t *)(unpack_fns[idx].tlv);
// go to the end tl->tag = generic_tl.tag;
return 0; tl->length = generic_tl.length;
} int result = (*unpack_fns[idx].unpack_func)(tl, ppReadPackedMsg, end);
}
} if(result == 0) {
} return 0;
}
return 1;
} // check if the length was right;
int unpack_p7_tlv_list(unpack_p7_tlv_t unpack_fns[], uint16_t size, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config, nfapi_tl_t **ve) { if(tl->length != (*ppReadPackedMsg - pStartOfValue)) {
nfapi_tl_t generic_tl; NFAPI_TRACE(NFAPI_TRACE_ERROR, "Warning tlv tag 0x%x length %d not equal to unpack %ld\n", tl->tag, tl->length, (*ppReadPackedMsg - pStartOfValue));
uint8_t numBadTags = 0; on_error();
uint16_t idx = 0; }
}
while ((uint8_t *)(*ppReadPackedMsg) < end) { }
// unpack the tl and process the values accordingly
if(unpack_tl(ppReadPackedMsg, &generic_tl, end) == 0) if(tagMatch == 0) {
return 0; if(generic_tl.tag >= NFAPI_VENDOR_EXTENSION_MIN_TAG_VALUE &&
generic_tl.tag <= NFAPI_VENDOR_EXTENSION_MAX_TAG_VALUE) {
uint8_t tagMatch = 0; int result = unpack_vendor_extension_tlv(&generic_tl, ppReadPackedMsg, end, config, ve);
uint8_t *pStartOfValue = *ppReadPackedMsg;
if(result == 0) {
for(idx = 0; idx < size; ++idx) { // got tot the end.
if(unpack_fns[idx].tag == generic_tl.tag) { return 0;
tagMatch = 1; } else if(result < 0) {
nfapi_tl_t *tl = (nfapi_tl_t *)(unpack_fns[idx].tlv); NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown VE TAG value: 0x%04x\n", generic_tl.tag);
tl->tag = generic_tl.tag; on_error();
tl->length = generic_tl.length;
int result = (*unpack_fns[idx].unpack_func)(tl, ppReadPackedMsg, end, config); if (++numBadTags > MAX_BAD_TAG) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n");
if(result == 0) { on_error();
return 0; return 0;
} }
// check if the length was right; if((end - *ppReadPackedMsg) >= generic_tl.length) {
if(tl->length != (*ppReadPackedMsg - pStartOfValue)) { // Advance past the unknown TLV
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Warning tlv tag 0x%x length %d not equal to unpack %ld\n", tl->tag, tl->length, (*ppReadPackedMsg - pStartOfValue)); (*ppReadPackedMsg) += generic_tl.length;
} } else {
} // go to the end
} return 0;
}
if(tagMatch == 0) { }
if(generic_tl.tag >= NFAPI_VENDOR_EXTENSION_MIN_TAG_VALUE && } else {
generic_tl.tag <= NFAPI_VENDOR_EXTENSION_MAX_TAG_VALUE) { NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown TAG value: 0x%04x\n", generic_tl.tag);
int result = unpack_p7_vendor_extension_tlv(&generic_tl, ppReadPackedMsg, end, config, ve); on_error();
if(result == 0) { if (++numBadTags > MAX_BAD_TAG) {
// got to end NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n");
return 0; on_error();
} else if(result < 0) { return 0;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown TAG value: 0x%04x\n", generic_tl.tag); }
if (++numBadTags > MAX_BAD_TAG) { if((end - *ppReadPackedMsg) >= generic_tl.length) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n"); // Advance past the unknown TLV
return -1; (*ppReadPackedMsg) += generic_tl.length;
} } else {
// go to the end
if((end - *ppReadPackedMsg) >= generic_tl.length) { return 0;
// Advance past the unknown TLV }
(*ppReadPackedMsg) += generic_tl.length; }
} else { }
// got ot the dn }
return 0;
} return 1;
} }
} else { int unpack_p7_tlv_list(unpack_p7_tlv_t unpack_fns[], uint16_t size, uint8_t **ppReadPackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config, nfapi_tl_t **ve) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown TAG value: 0x%04x\n", generic_tl.tag); nfapi_tl_t generic_tl;
uint8_t numBadTags = 0;
if (++numBadTags > MAX_BAD_TAG) { uint16_t idx = 0;
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n");
return -1; while ((uint8_t *)(*ppReadPackedMsg) < end) {
} // unpack the tl and process the values accordingly
if(unpack_tl(ppReadPackedMsg, &generic_tl, end) == 0)
if((end - *ppReadPackedMsg) >= generic_tl.length) { return 0;
// Advance past the unknown TLV
(*ppReadPackedMsg) += generic_tl.length; uint8_t tagMatch = 0;
} else { uint8_t *pStartOfValue = *ppReadPackedMsg;
// got ot the dn
return 0; for(idx = 0; idx < size; ++idx) {
} if(unpack_fns[idx].tag == generic_tl.tag) {
} tagMatch = 1;
} nfapi_tl_t *tl = (nfapi_tl_t *)(unpack_fns[idx].tlv);
} tl->tag = generic_tl.tag;
tl->length = generic_tl.length;
return 1; int result = (*unpack_fns[idx].unpack_func)(tl, ppReadPackedMsg, end, config);
}
if(result == 0) {
// This intermediate function deals with calculating the length of the value return 0;
// and writing into the tlv header. }
uint8_t pack_tlv(uint16_t tag, void *tlv, uint8_t **ppWritePackedMsg, uint8_t *end, pack_tlv_fn fn) {
nfapi_tl_t *tl = (nfapi_tl_t *)tlv; // check if the length was right;
if(tl->length != (*ppReadPackedMsg - pStartOfValue)) {
// If the tag is defined NFAPI_TRACE(NFAPI_TRACE_ERROR, "Warning tlv tag 0x%x length %d not equal to unpack %ld\n", tl->tag, tl->length, (*ppReadPackedMsg - pStartOfValue));
if(tl->tag == tag) { on_error();
uint8_t *pStartOfTlv = *ppWritePackedMsg; }
}
// write a dumy tlv header }
if(pack_tl(tl, ppWritePackedMsg, end) == 0)
return 0; if(tagMatch == 0) {
if(generic_tl.tag >= NFAPI_VENDOR_EXTENSION_MIN_TAG_VALUE &&
// Record the start of the value generic_tl.tag <= NFAPI_VENDOR_EXTENSION_MAX_TAG_VALUE) {
uint8_t *pStartOfValue = *ppWritePackedMsg; int result = unpack_p7_vendor_extension_tlv(&generic_tl, ppReadPackedMsg, end, config, ve);
// pack the tlv value if(result == 0) {
if(fn(tlv, ppWritePackedMsg, end) == 0) // got to end
return 0; return 0;
} else if(result < 0) {
// calculate the length of the value and rewrite the tl header NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown TAG value: 0x%04x\n", generic_tl.tag);
tl->length = (*ppWritePackedMsg) - pStartOfValue; on_error();
// rewrite the header with the correct length
pack_tl(tl, &pStartOfTlv, end); if (++numBadTags > MAX_BAD_TAG) {
} else { NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n");
if(tl->tag != 0) { on_error();
NFAPI_TRACE(NFAPI_TRACE_WARN, "Warning pack_tlv tag 0x%x does not match expected 0x%x\n", tl->tag, tag); return -1;
} else { }
//NFAPI_TRACE(NFAPI_TRACE_ERROR, "Warning pack_tlv tag 0x%x ZERO does not match expected 0x%x\n", tl->tag, tag);
} if((end - *ppReadPackedMsg) >= generic_tl.length) {
} // Advance past the unknown TLV
(*ppReadPackedMsg) += generic_tl.length;
return 1; } else {
} // got ot the dn
return 0;
const char *nfapi_error_code_to_str(nfapi_error_code_e value) { }
switch(value) { }
case NFAPI_MSG_OK: } else {
return "NFAPI_MSG_OK"; NFAPI_TRACE(NFAPI_TRACE_ERROR, "Unknown TAG value: 0x%04x\n", generic_tl.tag);
on_error();
case NFAPI_MSG_INVALID_STATE:
return "NFAPI_MSG_INVALID_STATE"; if (++numBadTags > MAX_BAD_TAG) {
NFAPI_TRACE(NFAPI_TRACE_ERROR, "Supplied message has had too many bad tags\n");
case NFAPI_MSG_INVALID_CONFIG: on_error();
return "NFAPI_MSG_INVALID_CONFIG"; return -1;
}
case NFAPI_SFN_OUT_OF_SYNC:
return "NFAPI_SFN_OUT_OF_SYNC"; if((end - *ppReadPackedMsg) >= generic_tl.length) {
// Advance past the unknown TLV
case NFAPI_MSG_SUBFRAME_ERR: (*ppReadPackedMsg) += generic_tl.length;
return "NFAPI_MSG_SUBFRAME_ERR"; } else {
// got ot the dn
case NFAPI_MSG_BCH_MISSING: return 0;
return "NFAPI_MSG_BCH_MISSING"; }
}
case NFAPI_MSG_INVALID_SFN: }
return "NFAPI_MSG_INVALID_SFN"; }
case NFAPI_MSG_HI_ERR: return 1;
return "NFAPI_MSG_HI_ERR"; }
case NFAPI_MSG_TX_ERR: // This intermediate function deals with calculating the length of the value
return "NFAPI_MSG_TX_ERR"; // and writing into the tlv header.
uint8_t pack_tlv(uint16_t tag, void *tlv, uint8_t **ppWritePackedMsg, uint8_t *end, pack_tlv_fn fn) {
default: nfapi_tl_t *tl = (nfapi_tl_t *)tlv;
return "UNKNOWN";
} // If the tag is defined
} if(tl->tag == tag) {
uint8_t *pStartOfTlv = *ppWritePackedMsg;
// write a dumy tlv header
if(pack_tl(tl, ppWritePackedMsg, end) == 0)
return 0;
// Record the start of the value
uint8_t *pStartOfValue = *ppWritePackedMsg;
// pack the tlv value
if(fn(tlv, ppWritePackedMsg, end) == 0)
return 0;
// calculate the length of the value and rewrite the tl header
tl->length = (*ppWritePackedMsg) - pStartOfValue;
// rewrite the header with the correct length
pack_tl(tl, &pStartOfTlv, end);
} else {
if(tl->tag != 0) {
NFAPI_TRACE(NFAPI_TRACE_WARN, "Warning pack_tlv tag 0x%x does not match expected 0x%x\n", tl->tag, tag);
} else {
//NFAPI_TRACE(NFAPI_TRACE_ERROR, "Warning pack_tlv tag 0x%x ZERO does not match expected 0x%x\n", tl->tag, tag);
}
}
return 1;
}
const char *nfapi_error_code_to_str(nfapi_error_code_e value) {
switch(value) {
case NFAPI_MSG_OK:
return "NFAPI_MSG_OK";
case NFAPI_MSG_INVALID_STATE:
return "NFAPI_MSG_INVALID_STATE";
case NFAPI_MSG_INVALID_CONFIG:
return "NFAPI_MSG_INVALID_CONFIG";
case NFAPI_SFN_OUT_OF_SYNC:
return "NFAPI_SFN_OUT_OF_SYNC";
case NFAPI_MSG_SUBFRAME_ERR:
return "NFAPI_MSG_SUBFRAME_ERR";
case NFAPI_MSG_BCH_MISSING:
return "NFAPI_MSG_BCH_MISSING";
case NFAPI_MSG_INVALID_SFN:
return "NFAPI_MSG_INVALID_SFN";
case NFAPI_MSG_HI_ERR:
return "NFAPI_MSG_HI_ERR";
case NFAPI_MSG_TX_ERR:
return "NFAPI_MSG_TX_ERR";
default:
return "UNKNOWN";
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment