From de58833f9fb5f997c52a6e7b8bb6330e25160336 Mon Sep 17 00:00:00 2001 From: Jihoon Jung Date: Fri, 3 Feb 2023 10:59:15 +0900 Subject: [PATCH] Add testsuites for testmode in nfc_unit_test Change-Id: Ib23689ff4fa770893c95413a2265af5e9d495246 Signed-off-by: Jihoon Jung --- include/net_nfc_client_test.h | 4 + include/nfc_internal.h | 4 + src/net_nfc_client_test.c | 69 ++++++++++++++ src/nfc_manager.c | 36 +++++++ test/nfc_unit_test.c | 216 +++++++++++++++++++++--------------------- 5 files changed, 223 insertions(+), 106 deletions(-) diff --git a/include/net_nfc_client_test.h b/include/net_nfc_client_test.h index 3ec6e4a..89f698c 100755 --- a/include/net_nfc_client_test.h +++ b/include/net_nfc_client_test.h @@ -42,6 +42,10 @@ typedef void (*net_nfc_client_test_ese_test_completed)(net_nfc_error_e result, void *user_data); +net_nfc_error_e net_nfc_client_test_test_mode_on_sync(void); + +net_nfc_error_e net_nfc_client_test_test_mode_off_sync(void); + net_nfc_error_e net_nfc_client_test_sim_test( net_nfc_client_test_sim_test_completed callback, void *user_data); diff --git a/include/nfc_internal.h b/include/nfc_internal.h index 6ceb15e..206efe6 100755 --- a/include/nfc_internal.h +++ b/include/nfc_internal.h @@ -581,6 +581,10 @@ int nfc_se_foreach_registered_handlers(nfc_card_emulation_category_type_e catego int nfc_manager_configure_discovery(int mode); +int nfc_manager_test_mode_on(); + +int nfc_manager_test_mode_off(); + #ifdef __cplusplus } #endif diff --git a/src/net_nfc_client_test.c b/src/net_nfc_client_test.c index 83e5b73..3730663 100755 --- a/src/net_nfc_client_test.c +++ b/src/net_nfc_client_test.c @@ -512,6 +512,75 @@ net_nfc_error_e net_nfc_client_test_set_ee_data_sync(int mode, } NET_NFC_EXPORT_API +net_nfc_error_e net_nfc_client_test_test_mode_on_sync(void) +{ + net_nfc_error_e out_result = NET_NFC_OK; + GError *error = NULL; + + DEBUG_CLIENT_MSG("NFC Test Mode ON SYNC!!!!"); + + if (test_proxy == NULL) { + if (net_nfc_client_test_init() != NET_NFC_OK) { + DEBUG_ERR_MSG("test_proxy fail"); + return NET_NFC_NOT_INITIALIZED; + } + } + + /* prevent executing daemon when nfc is off */ + if (net_nfc_client_manager_is_activated() == false) + return NET_NFC_NOT_ACTIVATED; + + if (net_nfc_gdbus_test_call_test_mode_on_sync(test_proxy, + (gint *)&out_result, + NULL, + &error) == FALSE) { + if (error != NULL) { + DEBUG_ERR_MSG("can not call Test Mode On: %s", error->message); + g_error_free(error); + } + + out_result = NET_NFC_IPC_FAIL; + } + + return out_result; +} + +NET_NFC_EXPORT_API +net_nfc_error_e net_nfc_client_test_test_mode_off_sync(void) +{ + net_nfc_error_e out_result = NET_NFC_OK; + GError *error = NULL; + + DEBUG_CLIENT_MSG("NFC Test Mode Off SYNC!!!!"); + + if (test_proxy == NULL) { + if (net_nfc_client_test_init() != NET_NFC_OK) { + DEBUG_ERR_MSG("test_proxy fail"); + return NET_NFC_NOT_INITIALIZED; + } + } + + /* prevent executing daemon when nfc is off */ + if (net_nfc_client_manager_is_activated() == false) + return NET_NFC_NOT_ACTIVATED; + + if (net_nfc_gdbus_test_call_test_mode_off_sync(test_proxy, + (gint *)&out_result, + NULL, + &error) == FALSE) { + if (error != NULL) { + DEBUG_ERR_MSG("can not call Test Mode Off: %s", error->message); + g_error_free(error); + } + + out_result = NET_NFC_IPC_FAIL; + } + + return out_result; +} + + +NET_NFC_EXPORT_API net_nfc_error_e net_nfc_client_test_ese_test( net_nfc_client_test_ese_test_completed callback, void *user_data) diff --git a/src/nfc_manager.c b/src/nfc_manager.c index 8a94049..cdc480e 100644 --- a/src/nfc_manager.c +++ b/src/nfc_manager.c @@ -114,6 +114,42 @@ int nfc_manager_configure_discovery(int mode) /* LCOV_EXCL_STOP */ } +int nfc_manager_test_mode_on(void) +{ + int ret; + + LOG_BEGIN(); + + CHECK_SUPPORTED(NFC_FEATURE); + + /* LCOV_EXCL_START */ + CHECK_INIT(); + CHECK_ACTIVATED(); + + ret = net_nfc_client_test_test_mode_on_sync(); + + return nfc_common_convert_error_code(__func__, ret); + /* LCOV_EXCL_STOP */ +} + +int nfc_manager_test_mode_off(void) +{ + int ret; + + LOG_BEGIN(); + + CHECK_SUPPORTED(NFC_FEATURE); + + /* LCOV_EXCL_START */ + CHECK_INIT(); + CHECK_ACTIVATED(); + + ret = net_nfc_client_test_test_mode_off_sync(); + + return nfc_common_convert_error_code(__func__, ret); + /* LCOV_EXCL_STOP */ +} + bool nfc_manager_is_activated(void) { int ret; diff --git a/test/nfc_unit_test.c b/test/nfc_unit_test.c index fbfef69..a0f188f 100755 --- a/test/nfc_unit_test.c +++ b/test/nfc_unit_test.c @@ -8,12 +8,13 @@ #include #define BUFFER_LEN 10 -#define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args) -#define TC_PRT(format, args...) PRT(format"\n", ##args) +#define PRT(format, args...) printf("%s:%d() " format, __FUNCTION__, __LINE__, ##args) +#define TC_PRT(format, args...) PRT(format "\n", ##args) GMainLoop *main_loop = NULL; -typedef struct { +typedef struct +{ const char *tc_name; int tc_code; } tc_table_t; @@ -24,21 +25,23 @@ tc_table_t tc_table[] = { {"NFC Configure discovery (CE only) Test", 2}, {"NFC Configure discovery (Tag only) Test", 3}, {"NFC Tag NDEF Read", 4}, - {"NFC Tag NDEF Write", 5}, - {"NFC Deinitialize Test", 6}, - {"NFC Configure discovery (ALL enable) Test", 7}, - {"NFC Configure discovery (ALL disable) Test", 8}, + {"NFC Test Mode ON", 5}, + {"NFC Test Mode OFF", 6}, + {"NFC Deinitialize Test", 7}, + {"NFC Configure discovery (ALL enable) Test", 8}, + {"NFC Configure discovery (ALL disable) Test", 9}, /* -----------*/ - {"Finish" , 0x00ff}, - {NULL , 0x0000}, + {"Finish", 0x00ff}, + {NULL, 0x0000}, }; void tc_usage_print(void) { int i = 0; - while (tc_table[i].tc_name) { + while (tc_table[i].tc_name) + { if (tc_table[i].tc_code != 0x00ff) TC_PRT("Key %d : usage %s", tc_table[i].tc_code, tc_table[i].tc_name); else @@ -50,21 +53,25 @@ void tc_usage_print(void) void __tag_read_completed(nfc_error_e result, nfc_ndef_message_h message, void *user_data) { - if (result == NFC_ERROR_NONE) { + if (result == NFC_ERROR_NONE) + { int count = 0; nfc_ndef_message_get_record_count(message, &count); TC_PRT("tag read successful"); TC_PRT("record count : %d", count); - if (count > 0) { + if (count > 0) + { nfc_ndef_record_h record = NULL; int ret = nfc_ndef_message_get_record(message, 0, &record); - if (ret != NFC_ERROR_NONE) { + if (ret != NFC_ERROR_NONE) + { TC_PRT("fail to get record from ndef message: %d", ret); return; } - if (record != NULL) { + if (record != NULL) + { char *lang_code = NULL; int id_size = 0; int type_size = 0; @@ -80,8 +87,9 @@ void __tag_read_completed(nfc_error_e result, nfc_ndef_message_h message, void * nfc_ndef_record_get_type(record, &type, &type_size); TC_PRT("3. type : %s, type_size : %d", type, type_size); - if (strcmp((const char *)type, "T") == 0) { - char* text = NULL; + if (strcmp((const char *)type, "T") == 0) + { + char *text = NULL; nfc_ndef_record_get_text(record, &text); TC_PRT("3. This record is Text record."); @@ -91,7 +99,9 @@ void __tag_read_completed(nfc_error_e result, nfc_ndef_message_h message, void * TC_PRT("----* record 0 information end *----"); } } - } else { + } + else + { TC_PRT("tag read error : %d", result); } } @@ -106,7 +116,8 @@ void __tag_write_completed(nfc_error_e result, void *user_data) void __tag_discovered(nfc_discovered_type_e type, nfc_tag_h tag, void *user_data) { - if (type == NFC_DISCOVERED_TYPE_ATTACHED) { + if (type == NFC_DISCOVERED_TYPE_ATTACHED) + { nfc_tag_type_e tag_type = NFC_UNKNOWN_TARGET; bool is_support_ndef = false; @@ -117,13 +128,14 @@ void __tag_discovered(nfc_discovered_type_e type, nfc_tag_h tag, void *user_data nfc_tag_is_support_ndef(tag, &is_support_ndef); TC_PRT("NDEF is supported? : %d", is_support_ndef); - - } else if (type == NFC_DISCOVERED_TYPE_DETACHED) { + } + else if (type == NFC_DISCOVERED_TYPE_DETACHED) + { TC_PRT("tag detached!!!"); } } -char* nfc_util_itoa(char* fmt, ...) +char *nfc_util_itoa(char *fmt, ...) { char str[128]; va_list arguments; @@ -139,7 +151,7 @@ nfc_ndef_message_h __create_ndef_message() nfc_ndef_message_h msg = NULL; nfc_ndef_record_h ndef_name_record = NULL; unsigned int seed = time(NULL); - char* time_str = nfc_util_itoa("The random text : %d", rand_r(&seed)); + char *time_str = nfc_util_itoa("The random text : %d", rand_r(&seed)); nfc_ndef_message_create(&msg); nfc_ndef_record_create_text(&ndef_name_record, time_str, "en-US", NFC_ENCODE_UTF_8); @@ -154,99 +166,92 @@ int test_input_callback(void *data) int ret = 0; long test_id = (long)data; - switch (test_id) { + switch (test_id) + { case 0x00ff: TC_PRT("Finished"); g_main_loop_quit(main_loop); break; case 1: - { - TC_PRT("nfc initialize start"); - ret = nfc_manager_initialize(); - if (ret == NFC_ERROR_NONE) - TC_PRT("NFC Initialize successful"); - else - TC_PRT("NFC Error occur : %d", ret); - - nfc_manager_set_tag_discovered_cb(__tag_discovered, NULL); - TC_PRT("nfc initialize end"); - } - break; + { + TC_PRT("nfc initialize start"); + ret = nfc_manager_initialize(); + if (ret == NFC_ERROR_NONE) + TC_PRT("NFC Initialize successful"); + else + TC_PRT("NFC Error occur : %d", ret); + + nfc_manager_set_tag_discovered_cb(__tag_discovered, NULL); + TC_PRT("nfc initialize end"); + } + break; case 2: - { - TC_PRT("nfc configure discovery : CE only start"); - ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_CONFIG_LISTENING); - TC_PRT("nfc configure discovery : CE only end : %d", ret); - } - break; + { + TC_PRT("nfc configure discovery : CE only start"); + ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_CONFIG_LISTENING); + TC_PRT("nfc configure discovery : CE only end : %d", ret); + } + break; case 3: - { - TC_PRT("nfc configure discovery : Tag only start"); - ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_CONFIG_POLLING); - TC_PRT("nfc configure discovery : Tag only end : %d", ret); - } - break; + { + TC_PRT("nfc configure discovery : Tag only start"); + ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_CONFIG_POLLING); + TC_PRT("nfc configure discovery : Tag only end : %d", ret); + } + break; case 4: - { - TC_PRT("Tag read start"); - nfc_tag_h current_tag = NULL; - nfc_manager_get_connected_tag(¤t_tag); + { + TC_PRT("Tag read start"); + nfc_tag_h current_tag = NULL; + nfc_manager_get_connected_tag(¤t_tag); - if (current_tag != NULL) - ret = nfc_tag_read_ndef(current_tag, __tag_read_completed, NULL); - else - TC_PRT("Tag Not Connected"); + if (current_tag != NULL) + ret = nfc_tag_read_ndef(current_tag, __tag_read_completed, NULL); + else + TC_PRT("Tag Not Connected"); - TC_PRT("Tag read end"); - } - break; + TC_PRT("Tag read end"); + } + break; case 5: - { - TC_PRT("Tag write start"); - - nfc_tag_h current_tag = NULL; - nfc_manager_get_connected_tag(¤t_tag); - nfc_ndef_message_h msg = __create_ndef_message(); - - if (msg != NULL) { - if (current_tag != NULL) - ret = nfc_tag_write_ndef(current_tag, msg, __tag_write_completed, NULL); - else - TC_PRT("Tag Not Connected."); - } else { - TC_PRT("NDEF Message create is failed."); - } - - TC_PRT("Tag write end"); - } - break; + { + TC_PRT("Test Mode ON"); + nfc_manager_test_mode_on(); + } + break; case 6: - { - TC_PRT("nfc deinitialize start"); + { + TC_PRT("Test Mode OFF"); + nfc_manager_test_mode_off(); + } + break; + case 7: + { + TC_PRT("nfc deinitialize start"); - ret = nfc_manager_deinitialize(); - if (ret == NFC_ERROR_NONE) - TC_PRT("NFC Deinitialize successful"); - else - TC_PRT("NFC Error occur : %d", ret); + ret = nfc_manager_deinitialize(); + if (ret == NFC_ERROR_NONE) + TC_PRT("NFC Deinitialize successful"); + else + TC_PRT("NFC Error occur : %d", ret); - TC_PRT("nfc deinitialize end"); - } - break; - case 7: - { - TC_PRT("nfc configure discovery : All enable start"); - ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_RESUME); - TC_PRT("nfc configure discovery : All enable end : %d", ret); - } - break; + TC_PRT("nfc deinitialize end"); + } + break; case 8: - { - TC_PRT("nfc configure discovery : All disable start"); - ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_STOP); - TC_PRT("nfc configure discovery : All enable end : %d", ret); - } - break; + { + TC_PRT("nfc configure discovery : All enable start"); + ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_RESUME); + TC_PRT("nfc configure discovery : All enable end : %d", ret); + } + break; + case 9: + { + TC_PRT("nfc configure discovery : All disable start"); + ret = nfc_manager_configure_discovery(NFC_DISCOVERY_MODE_STOP); + TC_PRT("nfc configure discovery : All enable end : %d", ret); + } + break; default: tc_usage_print(); break; @@ -256,10 +261,10 @@ int test_input_callback(void *data) } static gboolean key_event_cb(GIOChannel *chan, - GIOCondition cond, - gpointer data) + GIOCondition cond, + gpointer data) { - char buf[BUFFER_LEN] = { 0 }; + char buf[BUFFER_LEN] = {0}; gsize len = 0; long test_id; @@ -277,8 +282,7 @@ static gboolean key_event_cb(GIOChannel *chan, return TRUE; } - -int main(int argc, char ** argv) +int main(int argc, char **argv) { GIOChannel *key_io; @@ -290,7 +294,7 @@ int main(int argc, char ** argv) g_io_channel_set_flags(key_io, G_IO_FLAG_NONBLOCK, NULL); g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, - key_event_cb, NULL); + key_event_cb, NULL); g_io_channel_unref(key_io); -- 2.7.4