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