From: Youngjae Shin Date: Fri, 14 Aug 2020 06:42:13 +0000 (+0900) Subject: increase code quality X-Git-Tag: submit/tizen_6.0_hotfix/20201103.114801 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ae09b9b895a693322af3e2964898972425e13089;p=platform%2Fadaptation%2Femulator%2Fnfc-plugin-emul.git increase code quality separate big function add storage class specifier(static) Change-Id: I680e8069fa9ce5ac25846a54cfa1e0559f10917a --- diff --git a/src/oem_emul.c b/src/oem_emul.c index b5acb0a..cd29c58 100644 --- a/src/oem_emul.c +++ b/src/oem_emul.c @@ -181,7 +181,7 @@ static llcp_event_listener_cb g_emul_controller_llcp_cb; static hce_apdu_listener_cb g_emul_controller_hce_cb; /* for emulator management */ -pthread_t gEmulThread; +static pthread_t gEmulThread; /* for stack management */ static net_nfc_target_handle_s *current_working_handle = NULL; @@ -191,12 +191,12 @@ static bool g_p2p_attached = 0; static int current_connection_id = 1; /* for llcp functionality */ -socket_info_s socket_info_array[LLCP_NB_SOCKET_MAX] = { {0, } }; +static socket_info_s socket_info_array[LLCP_NB_SOCKET_MAX] = {{0}}; -snep_msg_s *Snep_Server_msg; -data_s *llcp_server_data = NULL; +static snep_msg_s *Snep_Server_msg; +static data_s *llcp_server_data = NULL; -pthread_cond_t cond = PTHREAD_COND_INITIALIZER; +static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; /* Memory utils */ @@ -230,11 +230,9 @@ static void __nfc_emul_util_alloc_mem(void **mem, int size, char *filename, unsi if (*mem != NULL) LOGE("FILE: %s, LINE:%d, WARNING: Pointer is already allocated or it was not initialized with NULL", filename, line); - *mem = malloc(size); + *mem = calloc(1, size); - if (*mem != NULL) - memset(*mem, 0x0, size); - else + if (NULL == *mem) LOGE("FILE: %s, LINE:%d, Allocation is failed", filename, line); } @@ -446,6 +444,168 @@ static net_nfc_record_tnf_e _net_nfc_get_tnf_type(int name_format) } +static inline int _create_records_well_known(emulMsg_data_s *record, char *record_payload, ndef_record_h *new_record) +{ + if (!strncmp((char *)record->typeName.buffer, "U", 1)) { + DEBUG_MSG("URI Type "); + + data_s payload_data = {NULL, 0}; + + if (record->payload.buffer != NULL) { + payload_data.length = strlen(record_payload) + 1; + + _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length); + if (payload_data.buffer == NULL) { + DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); + return -1; + } + + payload_data.buffer[0] = NET_NFC_SCHEMA_FULL_URI; /* first byte of payload is protocol scheme */ + memcpy(payload_data.buffer + 1, record->payload.buffer, payload_data.length - 1); + } + + if (net_nfc_util_create_record(record->tnf, &record->typeName, &record->id, &payload_data, (ndef_record_s**)new_record) != NET_NFC_OK) { + DEBUG_ERR_MSG("net_nfc_util_create_record is failed"); + return -1; + } + + if (payload_data.buffer != NULL) + _nfc_emul_util_free_mem(payload_data.buffer); + } else if (!strncmp((char *)record->typeName.buffer, "T", 1)) { + DEBUG_MSG("TEXT Type "); + + data_s payload_data = {NULL, 0}; + int offset = 0; + int controll_byte; + + if (record->payload.buffer != NULL) { + payload_data.length = strlen((char *)record_payload) + strlen("en-US") + 1; + + _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length); + if (payload_data.buffer == NULL) { + DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); + return -1; + } + + controll_byte = strlen("en-US") & 0x3F; + + payload_data.buffer[0] = controll_byte; + + offset = 1; + memcpy(payload_data.buffer + offset, "en-US", strlen("en-US")); + + offset = offset + strlen("en-US"); + memcpy(payload_data.buffer + offset, record->payload.buffer, strlen(record_payload)); + } + + if (net_nfc_util_create_record(record->tnf, &record->typeName, &record->id, &payload_data, (ndef_record_s**)new_record) != NET_NFC_OK) { + DEBUG_ERR_MSG("net_nfc_util_create_record is failed"); + return -1; + } + + if (payload_data.buffer != NULL) + _nfc_emul_util_free_mem(payload_data.buffer); + } else { + DEBUG_ERR_MSG("NET_NFC_RECORD_WELL_KNOWN_TYPE >> typeName is wrong"); + return -1; + } + return 0; +} + +static inline int _create_records_mime(emulMsg_data_s *record, char *record_payload, ndef_record_h *new_record) +{ + FILE *file = NULL; + data_s filePayload = {NULL, 0}; + + /* open file : size limit? 10k? */ + file = fopen(record_payload, "r"); + if (file != NULL) { + long int file_len = 0, read_count = 0, read_total = 0; + uint8_t *file_data = NULL; + + fseek(file, 0, SEEK_END); + file_len = ftell(file); + fseek(file, 0, SEEK_SET); + + _nfc_emul_util_alloc_mem(file_data, file_len); + + if (file_data == NULL) { + DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); + fclose(file); + return -1; + } + + /* create payload */ + do { + read_count = fread(file_data + read_total, 1, file_len - read_total, file); + read_total += read_count; + } while (read_count != 0 && read_total < file_len); + + fclose(file); + + DEBUG_MSG("fread(%s) success, size %ld", record->payload.buffer, file_len); + + filePayload.length = file_len; + _nfc_emul_util_alloc_mem(filePayload.buffer, filePayload.length); + + if (filePayload.buffer == NULL) { + DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); + _nfc_emul_util_free_mem(file_data); + return -1; + } + memcpy(filePayload.buffer, file_data, filePayload.length); + + _nfc_emul_util_free_mem(file_data); + + /* set id */ + if (record->id.buffer == NULL) { + char *file_name = NULL; + + file_name = strrchr(record_payload, '/'); + if (file_name == NULL) + file_name = (char *)record_payload; + else + file_name++; + + record->id.length = strlen(file_name); + _nfc_emul_util_alloc_mem(record->id.buffer, record->id.length + 1); + + if (record->id.buffer == NULL) { + DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); + _nfc_emul_util_free_mem(filePayload.buffer); + return -1; + } + memcpy(record->id.buffer, file_name, sizeof(record->id.buffer) - 1); + } + } else { + DEBUG_MSG("file open error"); + return -1; + } + + /* create record */ + int ret = net_nfc_util_create_record(record->tnf, &record->typeName, &record->id, + &filePayload, (ndef_record_s **)new_record); + _nfc_emul_util_free_mem(filePayload.buffer); + if (ret != NET_NFC_OK) { + DEBUG_MSG("net_nfc_create_record failed[%d]", ret); + return -1; + } else { + return 0; + } +} + +static void _create_records_free(emulMsg_data_s *record) +{ + if (record->typeName.buffer) + _nfc_emul_util_free_mem(record->typeName.buffer); + + if (record->id.buffer) + _nfc_emul_util_free_mem(record->id.buffer); + + if (record->payload.buffer) + _nfc_emul_util_free_mem(record->payload.buffer); +} + static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_message_s **ndef_message, int record_count) { DEBUG_EMUL_BEGIN(); @@ -464,7 +624,6 @@ static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_messag char *record_payload; emulMsg_data_s record = {0}; - data_s filePayload = {NULL, 0}; /* parse string */ if (index == 0) @@ -491,7 +650,7 @@ static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_messag if (record.typeName.buffer == NULL) { DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - goto ERROR; + return create_record_count; } memcpy(record.typeName.buffer, type_name, sizeof(record.typeName.buffer) - 1); @@ -502,7 +661,8 @@ static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_messag if (record.id.buffer == NULL) { DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - goto ERROR; + _nfc_emul_util_free_mem(record.typeName.buffer); + return create_record_count; } memcpy(record.id.buffer, record_id, sizeof(record.id.buffer) - 1); @@ -513,7 +673,9 @@ static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_messag if (record.payload.buffer == NULL) { DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - goto ERROR; + _nfc_emul_util_free_mem(record.typeName.buffer); + _nfc_emul_util_free_mem(record.id.buffer); + return create_record_count; } memcpy(record.payload.buffer, record_payload, sizeof(record.payload.buffer) - 1); @@ -530,187 +692,61 @@ static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_messag ndef_record_h new_record = NULL; net_nfc_error_e result = NET_NFC_OK; - if (record.tnf == NET_NFC_RECORD_EMPTY) { - if ((result = net_nfc_util_create_record(NET_NFC_RECORD_EMPTY, &record.typeName, &record.id, &record.payload, (ndef_record_s **) &new_record)) != NET_NFC_OK) { + switch (record.tnf) { + case NET_NFC_RECORD_EMPTY: + result = net_nfc_util_create_record(NET_NFC_RECORD_EMPTY, &record.typeName, + &record.id, &record.payload, (ndef_record_s **)&new_record); + if (NET_NFC_OK != result) { DEBUG_MSG("net_nfc_create_record failed[%d]", result); - goto ERROR;; + _create_records_free(&record); + return create_record_count; } - } else if (record.tnf == NET_NFC_RECORD_UNKNOWN) { - if ((result = net_nfc_util_create_record(NET_NFC_RECORD_UNKNOWN, &record.typeName, &record.id, &record.payload, (ndef_record_s **) &new_record)) != NET_NFC_OK) { + break; + case NET_NFC_RECORD_UNKNOWN: + result = net_nfc_util_create_record(NET_NFC_RECORD_UNKNOWN, &record.typeName, + &record.id, &record.payload, (ndef_record_s **)&new_record); + if (NET_NFC_OK != result) { DEBUG_MSG("net_nfc_create_record failed[%d]", result); - goto ERROR;; - } - } else if (record.tnf == NET_NFC_RECORD_WELL_KNOWN_TYPE) { - if (!strncmp((char *)record.typeName.buffer, "U", 1)) { - DEBUG_MSG("URI Type "); - - data_s payload_data = { NULL, 0 }; - - if (record.payload.buffer != NULL) { - payload_data.length = strlen((char *)record_payload) + 1; - - _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length); - if (payload_data.buffer == NULL) { - DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - goto ERROR; - } - - payload_data.buffer[0] = NET_NFC_SCHEMA_FULL_URI; /* first byte of payload is protocol scheme */ - memcpy(payload_data.buffer + 1, record.payload.buffer, payload_data.length - 1); - } - - if (net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &payload_data, (ndef_record_s**) &new_record) != NET_NFC_OK) { - DEBUG_ERR_MSG("net_nfc_util_create_record is failed"); - goto ERROR; - } - - if (payload_data.buffer != NULL) - _nfc_emul_util_free_mem(payload_data.buffer); - } else if (!strncmp((char *)record.typeName.buffer, "T", 1)) { - DEBUG_MSG("TEXT Type "); - - data_s payload_data = { NULL, 0 }; - int offset = 0; - int controll_byte; - - if (record.payload.buffer != NULL) { - payload_data.length = strlen((char *)record_payload) + strlen("en-US") + 1; - - _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length); - if (payload_data.buffer == NULL) { - DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - goto ERROR; - } - - controll_byte = strlen("en-US") & 0x3F; - - payload_data.buffer[0] = controll_byte; - - offset = 1; - memcpy(payload_data.buffer + offset, "en-US", strlen("en-US")); - - offset = offset + strlen("en-US"); - memcpy(payload_data.buffer + offset, record.payload.buffer, strlen(record_payload)); - } - - if (net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &payload_data, (ndef_record_s**) &new_record) != NET_NFC_OK) { - DEBUG_ERR_MSG("net_nfc_util_create_record is failed"); - goto ERROR; - } - - if (payload_data.buffer != NULL) - _nfc_emul_util_free_mem(payload_data.buffer); - } else { - DEBUG_ERR_MSG("NET_NFC_RECORD_WELL_KNOWN_TYPE >> typeName is wrong"); - goto ERROR; + _create_records_free(&record); + return create_record_count; } - } else if ((record.tnf == NET_NFC_RECORD_MIME_TYPE)) { - FILE *file = NULL; - - /* open file : size limit? 10k? */ - file = fopen(record_payload, "r"); - if (file != NULL) { - long int file_len = 0, read_count = 0, read_total = 0; - uint8_t *file_data = NULL; - - fseek(file, 0, SEEK_END); - file_len = ftell(file); - fseek(file, 0, SEEK_SET); - - _nfc_emul_util_alloc_mem(file_data, file_len); - - if (file_data == NULL) { - DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - fclose(file); - goto ERROR; - } - - /* create payload */ - do { - read_count = fread(file_data + read_total, 1, file_len - read_total, file); - read_total += read_count; - } while (read_count != 0 && read_total < file_len); - - fclose(file); - - DEBUG_MSG("fread(%s) success, size %ld", record.payload.buffer, file_len); - - filePayload.length = file_len; - _nfc_emul_util_alloc_mem(filePayload.buffer, filePayload.length); - - if (filePayload.buffer == NULL) { - DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - _nfc_emul_util_free_mem(file_data); - goto ERROR; - } - memcpy(filePayload.buffer, file_data, filePayload.length); - - _nfc_emul_util_free_mem(file_data); - - /* set id */ - if (record.id.buffer == NULL) { - char *file_name = NULL; - - file_name = strrchr(record_payload, '/'); - if (file_name == NULL) - file_name = (char *) record_payload; - else - file_name++; - - record.id.length = strlen(file_name); - _nfc_emul_util_alloc_mem(record.id.buffer, record.id.length + 1); - - if (record.id.buffer == NULL) { - DEBUG_MSG("_nfc_emul_util_alloc_mem failed"); - _nfc_emul_util_free_mem(filePayload.buffer); - goto ERROR; - } - memcpy(record.id.buffer, file_name, sizeof(record.id.buffer) - 1); - } - } else { - DEBUG_MSG("file open error"); - goto ERROR;; + break; + case NET_NFC_RECORD_WELL_KNOWN_TYPE: + if (_create_records_well_known(&record, record_payload, &new_record) < 0) { + _create_records_free(&record); + return create_record_count; } - - /* create record */ - if ((result = net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &filePayload, (ndef_record_s **) &new_record)) != NET_NFC_OK) { - DEBUG_MSG("net_nfc_create_record failed[%d]", result); - goto ERROR;; + break; + case NET_NFC_RECORD_MIME_TYPE: + if (_create_records_mime(&record, record_payload, &new_record) < 0) { + _create_records_free(&record); + return create_record_count; } - } else { + break; + default: /* NET_NFC_RECORD_URI or NET_NFC_RECORD_EXTERNAL_RTD */ - if ((result = net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &record.payload, (ndef_record_s **) &new_record)) != NET_NFC_OK) { + result = net_nfc_util_create_record(record.tnf, &record.typeName, + &record.id, &record.payload, (ndef_record_s **)&new_record); + if (NET_NFC_OK != result) { DEBUG_MSG("net_nfc_create_record failed[%d]", result); - goto ERROR;; + _create_records_free(&record); + return create_record_count; } + break; } /* append record to ndef msg */ - if ((result = net_nfc_util_append_record((ndef_message_s*) *ndef_message, (ndef_record_s *) new_record)) != NET_NFC_OK) { + if ((result = net_nfc_util_append_record((ndef_message_s*)*ndef_message, (ndef_record_s *)new_record)) != NET_NFC_OK) { DEBUG_MSG("net_nfc_util_append_record failed[%d]", result); - goto ERROR;; + _create_records_free(&record); + return create_record_count; } create_record_count++; DEBUG_MSG("Create Record Sucess. Create Record Count[%d]", create_record_count); - continue; -ERROR: - - /* free data */ - if (record.typeName.buffer != NULL) - _nfc_emul_util_free_mem(record.typeName.buffer); - - if (record.id.buffer != NULL) - _nfc_emul_util_free_mem(record.id.buffer); - - if (record.payload.buffer != NULL) - _nfc_emul_util_free_mem(record.payload.buffer); - - if (filePayload.buffer != NULL) - _nfc_emul_util_free_mem(filePayload.buffer); + _create_records_free(&record); DEBUG_MSG("Create Record Loop End"); - break; } DEBUG_EMUL_END();