rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* MediaTek Inc. (C) 2016. All rights reserved. |
| 2 | * |
| 3 | * Copyright Statement: |
| 4 | * This software/firmware and related documentation ("MediaTek Software") are |
| 5 | * protected under relevant copyright laws. The information contained herein is |
| 6 | * confidential and proprietary to MediaTek Inc. and/or its licensors. Without |
| 7 | * the prior written permission of MediaTek inc. and/or its licensors, any |
| 8 | * reproduction, modification, use or disclosure of MediaTek Software, and |
| 9 | * information contained herein, in whole or in part, shall be strictly |
| 10 | * prohibited. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Description: |
| 15 | * Implement Param related APIs |
| 16 | */ |
| 17 | |
| 18 | #include "AudioParamParserPriv.h" |
| 19 | #include <errno.h> |
| 20 | |
| 21 | Param *paramCreate(const char *name, ParamInfo *paramInfo, const char *paramValue) { |
| 22 | |
| 23 | Param *param = NULL; |
| 24 | if (!paramInfo) { |
| 25 | ERR_LOG("The paramInfo is NULL, cannot create %s param!\n", name); |
| 26 | return NULL; |
| 27 | } |
| 28 | |
| 29 | param = (Param *)malloc(sizeof(Param)); |
| 30 | param->data = NULL; |
| 31 | param->name = strdup(name); |
| 32 | param->paramInfo = paramInfo; |
| 33 | paramSetupDataInfoByStr(param, paramValue); |
| 34 | |
| 35 | return param; |
| 36 | } |
| 37 | |
| 38 | EXPORT void paramRelease(Param *param) { |
| 39 | if (param) { |
| 40 | if (param->name) { |
| 41 | free(param->name); |
| 42 | } |
| 43 | if (param->data) { |
| 44 | free(param->data); |
| 45 | } |
| 46 | free(param); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | EXPORT Param *paramHashClone(Param *paramHash) { |
| 51 | Param *param = NULL, *item, *newItem; |
| 52 | size_t i; |
| 53 | if (!paramHash) { |
| 54 | ERR_LOG("paramHash is NULL\n"); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | for (item = paramHash; item != NULL; item = item->hh.next) { |
| 59 | newItem = malloc(sizeof(Param)); |
| 60 | newItem->arraySize = item->arraySize; |
| 61 | newItem->paramInfo = item->paramInfo; |
| 62 | newItem->paramUnit = item->paramUnit; |
| 63 | newItem->name = strdup(item->name); |
| 64 | |
| 65 | switch (item->paramInfo->dataType) { |
| 66 | case TYPE_STR: |
| 67 | newItem->data = strdup(item->data); |
| 68 | break; |
| 69 | case TYPE_INT: |
| 70 | newItem->data = malloc(sizeof(int)); |
| 71 | *(int *)newItem->data = *(int *)item->data; |
| 72 | break; |
| 73 | case TYPE_UINT: |
| 74 | newItem->data = malloc(sizeof(unsigned int)); |
| 75 | *(unsigned int *)newItem->data = *(unsigned int *)item->data; |
| 76 | break; |
| 77 | case TYPE_FLOAT: |
| 78 | newItem->data = malloc(sizeof(float)); |
| 79 | *(float *)newItem->data = *(float *)item->data; |
| 80 | break; |
| 81 | case TYPE_BYTE_ARRAY: |
| 82 | if (item->arraySize > 0) { |
| 83 | newItem->data = malloc(sizeof(char) * item->arraySize); |
| 84 | if (NULL == newItem->data) { |
| 85 | ERR_LOG("allocate working buf fail"); |
| 86 | return NULL; |
| 87 | } |
| 88 | } |
| 89 | for (i = 0; i < item->arraySize; i++) { |
| 90 | ((char *)newItem->data)[i] = ((char *)item->data)[i]; |
| 91 | } |
| 92 | break; |
| 93 | case TYPE_UBYTE_ARRAY: |
| 94 | if (item->arraySize > 0) { |
| 95 | newItem->data = malloc(sizeof(unsigned char) * item->arraySize); |
| 96 | if (NULL == newItem->data) { |
| 97 | ERR_LOG("allocate working buf fail"); |
| 98 | return NULL; |
| 99 | } |
| 100 | } |
| 101 | for (i = 0; i < item->arraySize; i++) { |
| 102 | ((unsigned char *)newItem->data)[i] = ((unsigned char *)item->data)[i]; |
| 103 | } |
| 104 | break; |
| 105 | case TYPE_SHORT_ARRAY: |
| 106 | if (item->arraySize > 0) { |
| 107 | newItem->data = malloc(sizeof(short) * item->arraySize); |
| 108 | if (NULL == newItem->data) { |
| 109 | ERR_LOG("allocate working buf fail"); |
| 110 | return NULL; |
| 111 | } |
| 112 | } |
| 113 | for (i = 0; i < item->arraySize; i++) { |
| 114 | ((short *)newItem->data)[i] = ((short *)item->data)[i]; |
| 115 | } |
| 116 | break; |
| 117 | case TYPE_USHORT_ARRAY: |
| 118 | if (item->arraySize > 0) { |
| 119 | newItem->data = malloc(sizeof(unsigned short) * item->arraySize); |
| 120 | if (NULL == newItem->data) { |
| 121 | ERR_LOG("allocate working buf fail"); |
| 122 | return NULL; |
| 123 | } |
| 124 | } |
| 125 | for (i = 0; i < item->arraySize; i++) { |
| 126 | ((unsigned short *)newItem->data)[i] = ((unsigned short *)item->data)[i]; |
| 127 | } |
| 128 | break; |
| 129 | case TYPE_INT_ARRAY: |
| 130 | if (item->arraySize > 0) { |
| 131 | newItem->data = malloc(sizeof(int) * item->arraySize); |
| 132 | if (NULL == newItem->data) { |
| 133 | ERR_LOG("allocate working buf fail"); |
| 134 | return NULL; |
| 135 | } |
| 136 | } |
| 137 | for (i = 0; i < item->arraySize; i++) { |
| 138 | ((int *)newItem->data)[i] = ((int *)item->data)[i]; |
| 139 | } |
| 140 | break; |
| 141 | case TYPE_UINT_ARRAY: |
| 142 | if (item->arraySize > 0) { |
| 143 | newItem->data = malloc(sizeof(unsigned int) * item->arraySize); |
| 144 | if (NULL == newItem->data) { |
| 145 | ERR_LOG("allocate working buf fail"); |
| 146 | return NULL; |
| 147 | } |
| 148 | } |
| 149 | for (i = 0; i < item->arraySize; i++) { |
| 150 | ((unsigned int *)newItem->data)[i] = ((unsigned int *)item->data)[i]; |
| 151 | } |
| 152 | break; |
| 153 | case TYPE_DOUBLE_ARRAY: |
| 154 | if (item->arraySize > 0) { |
| 155 | newItem->data = malloc(sizeof(double) * item->arraySize); |
| 156 | if (NULL == newItem->data) { |
| 157 | ERR_LOG("allocate working buf fail"); |
| 158 | return NULL; |
| 159 | } |
| 160 | } |
| 161 | for (i = 0; i < item->arraySize; i++) { |
| 162 | ((double *)newItem->data)[i] = ((double *)item->data)[i]; |
| 163 | } |
| 164 | break; |
| 165 | case TYPE_FIELD: |
| 166 | case TYPE_UNKNOWN: |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | if (newItem->name != NULL) { |
| 171 | HASH_ADD_KEYPTR(hh, param, newItem->name, strlen(newItem->name), newItem); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return param; |
| 176 | } |
| 177 | |
| 178 | EXPORT DATA_TYPE paramDataTypeToEnum(const char *dataType) { |
| 179 | if (!dataType) { |
| 180 | ERR_LOG("dataType is NULL\n"); |
| 181 | return TYPE_UNKNOWN; |
| 182 | } |
| 183 | |
| 184 | if (!strncmp(DATA_TYPE_STR_STRING, dataType, strlen(dataType) + 1)) { |
| 185 | return TYPE_STR; |
| 186 | } else if (!strncmp(DATA_TYPE_INT_STRING, dataType, strlen(dataType) + 1)) { |
| 187 | return TYPE_INT; |
| 188 | } else if (!strncmp(DATA_TYPE_UINT_STRING, dataType, strlen(dataType) + 1)) { |
| 189 | return TYPE_UINT; |
| 190 | } else if (!strncmp(DATA_TYPE_FLOAT_STRING, dataType, strlen(dataType) + 1)) { |
| 191 | return TYPE_FLOAT; |
| 192 | } else if (!strncmp(DATA_TYPE_BYTE_ARRAY_STRING, dataType, strlen(dataType) + 1)) { |
| 193 | return TYPE_BYTE_ARRAY; |
| 194 | } else if (!strncmp(DATA_TYPE_UBYTE_ARRAY_STRING, dataType, strlen(dataType) + 1)) { |
| 195 | return TYPE_UBYTE_ARRAY; |
| 196 | } else if (!strncmp(DATA_TYPE_SHORT_ARRAY_STRING, dataType, strlen(dataType) + 1)) { |
| 197 | return TYPE_SHORT_ARRAY; |
| 198 | } else if (!strncmp(DATA_TYPE_USHORT_ARRAY_STRING, dataType, strlen(dataType) + 1)) { |
| 199 | return TYPE_USHORT_ARRAY; |
| 200 | } else if (!strncmp(DATA_TYPE_INT_ARRAY_STRING, dataType, strlen(dataType) + 1)) { |
| 201 | return TYPE_INT_ARRAY; |
| 202 | } else if (!strncmp(DATA_TYPE_UINT_ARRAY_STRING, dataType, strlen(dataType) + 1)) { |
| 203 | return TYPE_UINT_ARRAY; |
| 204 | } else if (!strncmp(DATA_TYPE_DOUBLE_ARRAY_STRING, dataType, strlen(dataType) + 1)) { |
| 205 | return TYPE_DOUBLE_ARRAY; |
| 206 | } else { |
| 207 | return TYPE_UNKNOWN; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | EXPORT const char *paramDataTypeToStr(DATA_TYPE dataType) { |
| 212 | switch (dataType) { |
| 213 | case TYPE_STR: |
| 214 | return DATA_TYPE_STR_STRING; |
| 215 | case TYPE_INT: |
| 216 | return DATA_TYPE_INT_STRING; |
| 217 | case TYPE_UINT: |
| 218 | return DATA_TYPE_UINT_STRING; |
| 219 | case TYPE_FLOAT: |
| 220 | return DATA_TYPE_FLOAT_STRING; |
| 221 | case TYPE_BYTE_ARRAY: |
| 222 | return DATA_TYPE_BYTE_ARRAY_STRING; |
| 223 | case TYPE_UBYTE_ARRAY: |
| 224 | return DATA_TYPE_UBYTE_ARRAY_STRING; |
| 225 | case TYPE_SHORT_ARRAY: |
| 226 | return DATA_TYPE_SHORT_ARRAY_STRING; |
| 227 | case TYPE_USHORT_ARRAY: |
| 228 | return DATA_TYPE_USHORT_ARRAY_STRING; |
| 229 | case TYPE_INT_ARRAY: |
| 230 | return DATA_TYPE_INT_ARRAY_STRING; |
| 231 | case TYPE_UINT_ARRAY: |
| 232 | return DATA_TYPE_UINT_ARRAY_STRING; |
| 233 | case TYPE_DOUBLE_ARRAY: |
| 234 | return DATA_TYPE_DOUBLE_ARRAY_STRING; |
| 235 | case TYPE_UNKNOWN: |
| 236 | return DATA_TYPE_UNKNOWN_STRING; |
| 237 | case TYPE_FIELD: |
| 238 | return DATA_TYPE_FIELD_STRING; |
| 239 | } |
| 240 | return DATA_TYPE_UNKNOWN_STRING; |
| 241 | } |
| 242 | |
| 243 | EXPORT APP_STATUS paramSetupDataInfoByStr(Param *param, const char *str) { |
| 244 | return utilConvDataStringToNative(param->paramInfo->dataType, str, ¶m->data, ¶m->arraySize); |
| 245 | } |
| 246 | |
| 247 | EXPORT size_t paramGetArraySizeFromString(const char *str) { |
| 248 | size_t numOfSeperator = 0; |
| 249 | |
| 250 | if (!str) { |
| 251 | ERR_LOG("str is NULL!\n"); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | while ((str = strstr(str, ARRAY_SEPERATOR)) != NULL) { |
| 256 | numOfSeperator++; |
| 257 | str++; |
| 258 | } |
| 259 | |
| 260 | return numOfSeperator + 1; |
| 261 | } |
| 262 | |
| 263 | EXPORT size_t paramGetNumOfBytes(Param* param) |
| 264 | { |
| 265 | uint16_t numOfBytes = 0; |
| 266 | switch (param->paramInfo->dataType) { |
| 267 | case TYPE_BYTE_ARRAY: |
| 268 | numOfBytes = sizeof(char) * param->arraySize; |
| 269 | break; |
| 270 | case TYPE_UBYTE_ARRAY: |
| 271 | numOfBytes = sizeof(unsigned char) * param->arraySize; |
| 272 | break; |
| 273 | case TYPE_SHORT_ARRAY: |
| 274 | numOfBytes = sizeof(short) * param->arraySize; |
| 275 | break; |
| 276 | case TYPE_USHORT_ARRAY: |
| 277 | numOfBytes = sizeof(unsigned short) * param->arraySize; |
| 278 | break; |
| 279 | case TYPE_INT_ARRAY: |
| 280 | numOfBytes = sizeof(int) * param->arraySize; |
| 281 | break; |
| 282 | case TYPE_UINT_ARRAY: |
| 283 | numOfBytes = sizeof(unsigned int) * param->arraySize; |
| 284 | break; |
| 285 | case TYPE_DOUBLE_ARRAY: |
| 286 | numOfBytes = sizeof(double) * param->arraySize; |
| 287 | break; |
| 288 | case TYPE_STR: |
| 289 | numOfBytes = strlen(param->data) + 1; |
| 290 | break; |
| 291 | case TYPE_INT: |
| 292 | numOfBytes = sizeof(int); |
| 293 | break; |
| 294 | case TYPE_UINT: |
| 295 | numOfBytes = sizeof(unsigned int); |
| 296 | break; |
| 297 | case TYPE_FLOAT: |
| 298 | numOfBytes = sizeof(float); |
| 299 | break; |
| 300 | default: |
| 301 | ALOGE("%s(), Not an available param(%s) dataType(%d)", __FUNCTION__, param->paramInfo->name, param->paramInfo->dataType); |
| 302 | break; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | ALOGV("%s(), param name = %s, type = %d, arraySize = %d, bytes = %d", __FUNCTION__, param->paramInfo->name, param->paramInfo->dataType, param->arraySize, numOfBytes); |
| 307 | return numOfBytes; |
| 308 | } |
| 309 | |
| 310 | EXPORT APP_STATUS paramGetFieldVal(Param *param, FieldInfo *fieldInfo, unsigned int *val) { |
| 311 | unsigned char uchar; |
| 312 | unsigned short ushort; |
| 313 | unsigned int uint; |
| 314 | |
| 315 | if (!param) { |
| 316 | WARN_LOG("param is NULL\n"); |
| 317 | return APP_ERROR; |
| 318 | } |
| 319 | |
| 320 | if (!fieldInfo) { |
| 321 | WARN_LOG("param is NULL\n"); |
| 322 | return APP_ERROR; |
| 323 | } |
| 324 | |
| 325 | if (fieldInfo->arrayIndex >= param->arraySize) { |
| 326 | WARN_LOG("Param's array size is less than field index. (param=%s, field index="APP_SIZE_T_FT" >= array size="APP_SIZE_T_FT")\n", param->name, fieldInfo->arrayIndex, param->arraySize); |
| 327 | return APP_ERROR; |
| 328 | } |
| 329 | |
| 330 | if (fieldInfo->startBit < 0) { |
| 331 | WARN_LOG("The startBit < 0 (param=%s, startBit=%d)\n", param->name, fieldInfo->startBit); |
| 332 | return APP_ERROR; |
| 333 | } |
| 334 | |
| 335 | if (fieldInfo->endBit >= 32) { |
| 336 | WARN_LOG("The startBit >= 32 (param=%s, endBit=%d)\n", param->name, fieldInfo->endBit); |
| 337 | return APP_ERROR; |
| 338 | } |
| 339 | |
| 340 | switch (param->paramInfo->dataType) { |
| 341 | case TYPE_BYTE_ARRAY: |
| 342 | case TYPE_UBYTE_ARRAY: |
| 343 | if (fieldInfo->startBit > fieldInfo->endBit || fieldInfo->endBit >= 8) { |
| 344 | WARN_LOG("Field's bit information is not match param type. (param=%s, start bit=%d, end bit=%d)\n", param->name, fieldInfo->startBit, fieldInfo->endBit); |
| 345 | return APP_ERROR; |
| 346 | } |
| 347 | uchar = ((unsigned char *)param->data)[fieldInfo->arrayIndex]; |
| 348 | *val = ((uchar >> (fieldInfo->startBit)) & ((1 << (fieldInfo->endBit - fieldInfo->startBit + 1)) - 1)); |
| 349 | return APP_NO_ERROR; |
| 350 | case TYPE_SHORT_ARRAY: |
| 351 | case TYPE_USHORT_ARRAY: |
| 352 | if (fieldInfo->startBit > fieldInfo->endBit || fieldInfo->endBit >= 16) { |
| 353 | WARN_LOG("Field's bit information is not match param type. (param=%s, start bit=%d, end bit=%d)\n", param->name, fieldInfo->startBit, fieldInfo->endBit); |
| 354 | return APP_ERROR; |
| 355 | } |
| 356 | ushort = ((unsigned short *)param->data)[fieldInfo->arrayIndex]; |
| 357 | *val = ((ushort >> (fieldInfo->startBit)) & ((1 << (fieldInfo->endBit - fieldInfo->startBit + 1)) - 1)); |
| 358 | return APP_NO_ERROR; |
| 359 | case TYPE_INT_ARRAY: |
| 360 | case TYPE_UINT_ARRAY: |
| 361 | if (fieldInfo->startBit > fieldInfo->endBit || fieldInfo->endBit >= 32) { |
| 362 | WARN_LOG("Field's bit information is not match param type. (param=%s, start bit=%d, end bit=%d)\n", param->name, fieldInfo->startBit, fieldInfo->endBit); |
| 363 | return APP_ERROR; |
| 364 | } |
| 365 | uint = ((unsigned int *)param->data)[fieldInfo->arrayIndex]; |
| 366 | *val = ((uint >> (fieldInfo->startBit)) & ((1 << (fieldInfo->endBit - fieldInfo->startBit + 1)) - 1)); |
| 367 | return APP_NO_ERROR; |
| 368 | default: |
| 369 | WARN_LOG("Param didn't support field info (param=%s, type=%s)\n", param->name, paramDataTypeToStr(param->paramInfo->dataType)); |
| 370 | return APP_ERROR; |
| 371 | } |
| 372 | |
| 373 | return APP_ERROR; |
| 374 | } |
| 375 | |
| 376 | EXPORT char *paramNewDataStr(Param *param) { |
| 377 | return utilConvDataToString(param->paramInfo->dataType, param->data, param->arraySize, 1); |
| 378 | } |
| 379 | |
| 380 | EXPORT char *paramNewDataStrWithMode(Param *param, int uArrayHexMode) { |
| 381 | return utilConvDataToString(param->paramInfo->dataType, param->data, param->arraySize, uArrayHexMode); |
| 382 | } |
| 383 | |
| 384 | EXPORT APP_STATUS paramSetupDataInfoByVal(Param *param, void *data, int arraySize) { |
| 385 | int i; |
| 386 | |
| 387 | if (!param) { |
| 388 | ERR_LOG("Param is NULL\n"); |
| 389 | return APP_ERROR; |
| 390 | } |
| 391 | |
| 392 | switch (param->paramInfo->dataType) { |
| 393 | case TYPE_BYTE_ARRAY: |
| 394 | if (arraySize > 0) { |
| 395 | param->data = malloc(sizeof(char) * arraySize); |
| 396 | if (NULL == param->data) { |
| 397 | ERR_LOG("allocate working buf fail"); |
| 398 | return APP_ERROR; |
| 399 | } |
| 400 | } |
| 401 | for (i = 0; i < arraySize; i++) { |
| 402 | ((char *)param->data)[i] = ((char *)data)[i]; |
| 403 | } |
| 404 | param->arraySize = arraySize; |
| 405 | break; |
| 406 | case TYPE_UBYTE_ARRAY: |
| 407 | if (arraySize > 0) { |
| 408 | param->data = malloc(sizeof(unsigned char) * arraySize); |
| 409 | if (NULL == param->data) { |
| 410 | ERR_LOG("allocate working buf fail"); |
| 411 | return APP_ERROR; |
| 412 | } |
| 413 | } |
| 414 | for (i = 0; i < arraySize; i++) { |
| 415 | ((unsigned char *)param->data)[i] = ((unsigned char *)data)[i]; |
| 416 | } |
| 417 | param->arraySize = arraySize; |
| 418 | break; |
| 419 | case TYPE_SHORT_ARRAY: |
| 420 | if (arraySize > 0) { |
| 421 | param->data = malloc(sizeof(short) * arraySize); |
| 422 | if (NULL == param->data) { |
| 423 | ERR_LOG("allocate working buf fail"); |
| 424 | return APP_ERROR; |
| 425 | } |
| 426 | } |
| 427 | for (i = 0; i < arraySize; i++) { |
| 428 | ((short *)param->data)[i] = ((short *)data)[i]; |
| 429 | } |
| 430 | param->arraySize = arraySize; |
| 431 | break; |
| 432 | case TYPE_USHORT_ARRAY: |
| 433 | if (arraySize > 0) { |
| 434 | param->data = malloc(sizeof(unsigned short) * arraySize); |
| 435 | if (NULL == param->data) { |
| 436 | ERR_LOG("allocate working buf fail"); |
| 437 | return APP_ERROR; |
| 438 | } |
| 439 | } |
| 440 | for (i = 0; i < arraySize; i++) { |
| 441 | ((unsigned short *)param->data)[i] = ((unsigned short *)data)[i]; |
| 442 | } |
| 443 | param->arraySize = arraySize; |
| 444 | break; |
| 445 | case TYPE_INT_ARRAY: |
| 446 | if (arraySize > 0) { |
| 447 | param->data = malloc(sizeof(int) * arraySize); |
| 448 | if (NULL == param->data) { |
| 449 | ERR_LOG("allocate working buf fail"); |
| 450 | return APP_ERROR; |
| 451 | } |
| 452 | } |
| 453 | for (i = 0; i < arraySize; i++) { |
| 454 | ((int *)param->data)[i] = ((int *)data)[i]; |
| 455 | } |
| 456 | param->arraySize = arraySize; |
| 457 | break; |
| 458 | case TYPE_UINT_ARRAY: |
| 459 | if (arraySize > 0) { |
| 460 | param->data = malloc(sizeof(unsigned int) * arraySize); |
| 461 | if (NULL == param->data) { |
| 462 | ERR_LOG("allocate working buf fail"); |
| 463 | return APP_ERROR; |
| 464 | } |
| 465 | } |
| 466 | for (i = 0; i < arraySize; i++) { |
| 467 | ((unsigned int *)param->data)[i] = ((unsigned int *)data)[i]; |
| 468 | } |
| 469 | param->arraySize = arraySize; |
| 470 | break; |
| 471 | case TYPE_DOUBLE_ARRAY: |
| 472 | if (arraySize > 0) { |
| 473 | param->data = malloc(sizeof(double) * arraySize); |
| 474 | if (NULL == param->data) { |
| 475 | ERR_LOG("allocate working buf fail"); |
| 476 | return APP_ERROR; |
| 477 | } |
| 478 | } |
| 479 | for (i = 0; i < arraySize; i++) { |
| 480 | ((double *)param->data)[i] = ((double *)data)[i]; |
| 481 | } |
| 482 | param->arraySize = arraySize; |
| 483 | break; |
| 484 | case TYPE_STR: |
| 485 | param->data = strdup((const char *)data); |
| 486 | break; |
| 487 | case TYPE_INT: |
| 488 | param->data = malloc(sizeof(int)); |
| 489 | *(int *)param->data = *(int *)data; |
| 490 | break; |
| 491 | case TYPE_UINT: |
| 492 | param->data = malloc(sizeof(unsigned int)); |
| 493 | *(unsigned int *)param->data = *(unsigned int *)data; |
| 494 | break; |
| 495 | case TYPE_FLOAT: |
| 496 | param->data = malloc(sizeof(float)); |
| 497 | *(float *)param->data = *(float *)data; |
| 498 | break; |
| 499 | default: |
| 500 | return APP_ERROR; |
| 501 | } |
| 502 | |
| 503 | return APP_NO_ERROR; |
| 504 | } |
| 505 | |
| 506 | EXPORT APP_STATUS paramSetFieldVal(Param *param, FieldInfo *fieldInfo, unsigned int val) { |
| 507 | unsigned long mask; |
| 508 | |
| 509 | if (!param) { |
| 510 | ERR_LOG("param is NULL\n"); |
| 511 | return APP_ERROR; |
| 512 | } |
| 513 | |
| 514 | if (!fieldInfo) { |
| 515 | ERR_LOG("param is NULL\n"); |
| 516 | return APP_ERROR; |
| 517 | } |
| 518 | |
| 519 | if (fieldInfo->arrayIndex >= param->arraySize) { |
| 520 | ERR_LOG("Param's array size is less than field index. (param=%s, field index="APP_SIZE_T_FT" >= array size="APP_SIZE_T_FT")\n", param->name, fieldInfo->arrayIndex, param->arraySize); |
| 521 | return APP_ERROR; |
| 522 | } |
| 523 | |
| 524 | if (fieldInfo->startBit < 0) { |
| 525 | ERR_LOG("The startBit < 0 (param=%s, startBit=%d)\n", param->name, fieldInfo->startBit); |
| 526 | return APP_ERROR; |
| 527 | } |
| 528 | |
| 529 | if (fieldInfo->endBit >= 32) { |
| 530 | ERR_LOG("The startBit >= 32 (param=%s, endBit=%d)\n", param->name, fieldInfo->endBit); |
| 531 | return APP_ERROR; |
| 532 | } |
| 533 | |
| 534 | if (fieldInfo->endBit == 31 && fieldInfo->startBit == 0) { |
| 535 | mask = 0xFFFFFFFF; |
| 536 | } else { |
| 537 | mask = ((1 << (fieldInfo->endBit - fieldInfo->startBit + 1)) - 1); |
| 538 | } |
| 539 | |
| 540 | switch (param->paramInfo->dataType) { |
| 541 | case TYPE_BYTE_ARRAY: |
| 542 | case TYPE_UBYTE_ARRAY: |
| 543 | if (fieldInfo->startBit > fieldInfo->endBit || fieldInfo->endBit >= 8) { |
| 544 | ERR_LOG("Field's bit information is not match param type. (param=%s, start bit=%d, end bit=%d)\n", param->name, fieldInfo->startBit, fieldInfo->endBit); |
| 545 | return APP_ERROR; |
| 546 | } |
| 547 | |
| 548 | ((unsigned char *)param->data)[fieldInfo->arrayIndex] = |
| 549 | ((((unsigned char *)param->data)[fieldInfo->arrayIndex] & ~(mask << fieldInfo->startBit)) | (unsigned char)(val & mask) << fieldInfo->startBit); |
| 550 | return APP_NO_ERROR; |
| 551 | case TYPE_SHORT_ARRAY: |
| 552 | case TYPE_USHORT_ARRAY: |
| 553 | if (fieldInfo->startBit > fieldInfo->endBit || fieldInfo->endBit >= 16) { |
| 554 | ERR_LOG("Field's bit information is not match param type. (param=%s, start bit=%d, end bit=%d)\n", param->name, fieldInfo->startBit, fieldInfo->endBit); |
| 555 | return APP_ERROR; |
| 556 | } |
| 557 | |
| 558 | ((unsigned short *)param->data)[fieldInfo->arrayIndex] = |
| 559 | ((((unsigned short *)param->data)[fieldInfo->arrayIndex] & ~(mask << fieldInfo->startBit)) | (unsigned short)(val & mask) << fieldInfo->startBit); |
| 560 | return APP_NO_ERROR; |
| 561 | case TYPE_INT_ARRAY: |
| 562 | case TYPE_UINT_ARRAY: |
| 563 | if (fieldInfo->startBit > fieldInfo->endBit || fieldInfo->endBit >= 32) { |
| 564 | ERR_LOG("Field's bit information is not match param type. (param=%s, start bit=%d, end bit=%d)\n", param->name, fieldInfo->startBit, fieldInfo->endBit); |
| 565 | return APP_ERROR; |
| 566 | } |
| 567 | |
| 568 | ((unsigned int *)param->data)[fieldInfo->arrayIndex] = |
| 569 | ((((unsigned int *)param->data)[fieldInfo->arrayIndex] & ~(mask << fieldInfo->startBit)) | (val & mask) << fieldInfo->startBit); |
| 570 | return APP_NO_ERROR; |
| 571 | default: |
| 572 | ERR_LOG("Param is not array type, cannot query field value (param=%s, type=%s)\n", param->name, paramDataTypeToStr(param->paramInfo->dataType)); |
| 573 | return APP_ERROR; |
| 574 | } |
| 575 | |
| 576 | return APP_ERROR; |
| 577 | } |
| 578 | |