2018-03-03 08:15:19 +01:00
|
|
|
/*
|
|
|
|
This file is part of libcapwap.
|
|
|
|
|
|
|
|
libcapwap is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "cw.h"
|
2022-07-31 17:15:32 +02:00
|
|
|
#include "val.h"
|
2018-03-03 08:15:19 +01:00
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static cw_Val_t *get(cw_Val_t * data, const uint8_t * src, int len)
|
2018-03-03 08:15:19 +01:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
data->type = &cw_type_byte;
|
|
|
|
data->val.byte = cw_get_byte(src);
|
2018-03-05 20:39:15 +01:00
|
|
|
return data;
|
2018-03-03 08:15:19 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int put(const cw_Val_t *data, uint8_t * dst)
|
2018-03-03 08:15:19 +01:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
return cw_put_byte(dst, data->val.byte);
|
2018-03-03 08:15:19 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static const char * get_guardstr(int val, const cw_ValValRange_t * valrange)
|
2018-04-25 10:43:27 +02:00
|
|
|
{
|
|
|
|
while(valrange->name!=NULL){
|
|
|
|
if(val>=valrange->min && val<=valrange->max)
|
|
|
|
return valrange->name;
|
|
|
|
valrange++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int to_str(const cw_Val_t *data, char *dst, int max_len)
|
2018-03-05 20:39:15 +01:00
|
|
|
{
|
2018-04-25 10:43:27 +02:00
|
|
|
if (data->valguard!=NULL){
|
|
|
|
const char * name;
|
|
|
|
name = get_guardstr(data->val.byte,data->valguard);
|
|
|
|
if (name != NULL){
|
|
|
|
return sprintf(dst,"%s",name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if (max_len<3){
|
|
|
|
return 0;
|
|
|
|
}*/
|
2018-03-11 00:56:41 +01:00
|
|
|
return sprintf(dst, "%d", data->val.byte);
|
2018-04-25 10:43:27 +02:00
|
|
|
|
2018-03-05 20:39:15 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int get_guardval(const char *str, const cw_ValValRange_t * valrange)
|
2018-04-26 06:50:32 +02:00
|
|
|
{
|
|
|
|
while(valrange->name!=NULL){
|
|
|
|
if(strcmp(str,valrange->name)==0)
|
|
|
|
return valrange->min;
|
|
|
|
valrange++;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static cw_Val_t *from_str(cw_Val_t * data, const char *src)
|
2018-03-06 03:08:14 +01:00
|
|
|
{
|
2018-03-15 20:07:17 +01:00
|
|
|
data->type = &cw_type_byte;
|
2018-04-26 06:50:32 +02:00
|
|
|
if (data->valguard != NULL){
|
|
|
|
int rc;
|
|
|
|
rc = get_guardval(src,data->valguard);
|
|
|
|
if (rc != -1){
|
|
|
|
data->val.byte = rc;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
data->val.byte = atoi(src);
|
2018-03-06 03:08:14 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int len (cw_Val_t * data)
|
2018-03-15 20:07:17 +01:00
|
|
|
{
|
2018-04-25 10:43:27 +02:00
|
|
|
return sizeof(data->val.byte);
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static void * data(cw_Val_t * data)
|
2018-04-25 10:43:27 +02:00
|
|
|
{
|
|
|
|
return &data->val.byte;
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static const char * get_type_name(cw_Val_t *data)
|
2018-04-25 10:43:27 +02:00
|
|
|
{
|
|
|
|
if (data->valguard != NULL){
|
|
|
|
return CW_TYPE_STR->name;
|
|
|
|
}
|
|
|
|
return CW_TYPE_BYTE->name;
|
2018-03-15 20:07:17 +01:00
|
|
|
}
|
2018-03-06 03:08:14 +01:00
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int cast(cw_Val_t * data)
|
2018-04-26 06:50:32 +02:00
|
|
|
{
|
|
|
|
if (strcmp(data->type->name,CW_TYPE_BYTE->name)==0)
|
|
|
|
return 1;
|
|
|
|
if (strcmp(data->type->name,CW_TYPE_STR->name)==0){
|
|
|
|
char *src = data->val.ptr;
|
|
|
|
CW_TYPE_BYTE->from_str(data,src);
|
|
|
|
free(src);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-05 20:39:15 +01:00
|
|
|
const struct cw_Type cw_type_byte = {
|
2018-03-07 10:30:40 +01:00
|
|
|
"Byte", /* name */
|
2018-03-11 00:56:41 +01:00
|
|
|
NULL, /* del */
|
2018-03-07 10:30:40 +01:00
|
|
|
put, /* put */
|
|
|
|
get, /* get */
|
|
|
|
to_str, /* to_str */
|
2018-03-15 20:07:17 +01:00
|
|
|
from_str, /* from_str */
|
2018-04-25 10:43:27 +02:00
|
|
|
len, /* len */
|
|
|
|
data, /* data */
|
2018-04-26 06:50:32 +02:00
|
|
|
get_type_name, /* get_type_name */
|
|
|
|
cast
|
2018-03-05 20:39:15 +01:00
|
|
|
};
|