/* * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. * * @author: Abhay Agarwal * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include #include #include #include #include #include #include #include "uat-menu.h" #include "uat-common.h" extern ua_service_h g_service_h; /**< Service handle */ extern ua_monitor_h g_ua_mon_h; /**< Monitor handle */ extern ua_user_h g_user_h; /**< Monitor handle */ extern ua_device_h g_device_h; /**< Device handle */ extern char g_service_str[MENU_DATA_SIZE + 1]; /**< service name */ extern char g_user_account_str[MENU_DATA_SIZE + 1]; /**< user account */ extern char g_user_name_str[MENU_DATA_SIZE + 1]; /**< user name */ extern char g_device_type[MENU_DATA_SIZE + 1]; /**< Sensor type of the selected device */ extern char g_device_type_str[MENU_DATA_SIZE + 1]; /**< Readable sensor type string */ static char g_os_type[MENU_DATA_SIZE + 1] = {"2"}; /**< OS type of the selected device */ extern char g_os_type_str[MENU_DATA_SIZE + 1]; /**< Readable OS type string */ extern char g_device_id_str[MENU_DATA_SIZE + 1]; /**< Mobile ID of the selected device */ extern char g_mac_addr_str[MENU_DATA_SIZE + 1]; /**< MAC of the selected device */ static char g_ipv4_address_str[MENU_DATA_SIZE + 1] = {0,}; /**< IPv4 of the selected device */ static char g_loop_str[MENU_DATA_SIZE + 1] = {0,}; /**< Number of loops*/ static char g_presence_type[MENU_DATA_SIZE + 1] = "2"; /**< Selected PRESENCE type */ static char g_absence_type[MENU_DATA_SIZE + 1] = "2"; /**< Selected ABSENCE type */ #define BROADCAST_MAC_ADDR "ff:ff:ff:ff:ff:ff" static int __uat_scenario_service_startup() { int ret = UA_ERROR_NONE; char *svc_name = NULL; int detection_cycle = 60; /* create service handle */ ret = _uat_service_create(); retv_if(ret != UA_ERROR_NONE, ret); /* set service name */ svc_name = g_strndup("ua.scenerio.service1", MENU_DATA_SIZE); check_if(NULL == g_service_h); ret = ua_service_set_name(g_service_h, svc_name); retv_if(ret != UA_ERROR_NONE, ret); /* Add service */ check_if(NULL == g_service_h); ret = ua_service_add(g_service_h); msg(" - ua_service_add() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); retv_if(!(ret == UA_ERROR_NONE || ret == UA_ERROR_ALREADY_DONE || UA_ERROR_ALREADY_REGISTERED), RET_SUCCESS); /* service added successfully - update global name */ if (ret != UA_ERROR_NONE) { ua_service_destroy(g_service_h); g_service_h = NULL; ret = ua_get_service_by_name(svc_name, &g_service_h); } if (UA_ERROR_NONE == ret) { memset(g_service_str, 0, MENU_DATA_SIZE + 1); memcpy(g_service_str, svc_name, MENU_DATA_SIZE); } g_free(svc_name); ret = ua_set_detection_cycle(g_service_h, detection_cycle); msg(" - ua_set_detection_cycle() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return ret; } static int __uat_scenario_user_startup() { int ret = UA_ERROR_NONE; char *user_account = NULL; char *user_name = NULL; /* create user handle */ ret = _uat_user_create(); retv_if(ret != UA_ERROR_NONE, ret); /* set user account */ user_account = g_strndup("ua.scenerio.account1", MENU_DATA_SIZE); check_if(NULL == g_user_h); ret = ua_user_set_account(g_user_h, user_account); retv_if(ret != UA_ERROR_NONE, ret); /* set user name */ user_name = g_strndup("ua.scenerio.user1", MENU_DATA_SIZE); check_if(NULL == g_user_h); ret = ua_user_set_name(g_user_h, user_name); retv_if(ret != UA_ERROR_NONE, ret); /* Add user */ check_if(NULL == g_user_h); ret = ua_user_add(g_user_h); msg(" - ua_user_add() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); retv_if(!(ret == UA_ERROR_NONE || ret == UA_ERROR_ALREADY_DONE || UA_ERROR_ALREADY_REGISTERED), RET_SUCCESS); /* user added successfully - update global name */ if (ret != UA_ERROR_NONE) { ua_user_destroy(g_user_h); g_user_h = NULL; ret = ua_service_get_user_by_account(g_service_h, user_account, &g_user_h); msg(" - ua_service_get_user_by_account() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } if (UA_ERROR_NONE == ret) { memset(g_user_account_str, 0, MENU_DATA_SIZE + 1); memcpy(g_user_account_str, user_account, MENU_DATA_SIZE); memset(g_user_name_str, 0, MENU_DATA_SIZE + 1); memcpy(g_user_name_str, user_name, MENU_DATA_SIZE); } g_free(user_account); g_free(user_name); return ret; } static int __uat_scenario_monitor_startup() { int ret = UA_ERROR_NONE; ret = _uat_monitor_create(); retv_if(ret != UA_ERROR_NONE, ret); ret = _uat_monitor_set_sensor_status_cb(); check_if(ret != UA_ERROR_NONE); return ret; } static int run_ua_scenario_startup(MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg(" ### uat_scenario_startup ###"); /* Initialize ua */ ret = ua_initialize(); msg(" - ua_initialize() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); retv_if(!(ret == UA_ERROR_NONE || ret == UA_ERROR_ALREADY_DONE), RET_SUCCESS); /* service startup */ ret = __uat_scenario_service_startup(); retv_if(!(ret == UA_ERROR_NONE || ret == UA_ERROR_ALREADY_DONE || UA_ERROR_ALREADY_REGISTERED), RET_SUCCESS); /* user startup */ ret = __uat_scenario_user_startup(); retv_if(!(ret == UA_ERROR_NONE || ret == UA_ERROR_ALREADY_DONE || UA_ERROR_ALREADY_REGISTERED), RET_SUCCESS); /* monitor startup */ ret = __uat_scenario_monitor_startup(); retv_if(ret != UA_ERROR_NONE, RET_SUCCESS); return RET_SUCCESS; } static int __uat_scenario_monitor_cleanup() { int ret; ret = ua_monitor_unset_sensor_status_changed_cb(g_ua_mon_h); check_if(ret != UA_ERROR_NONE); if (g_ua_mon_h) ret = ua_monitor_destroy(g_ua_mon_h); check_if(ret != UA_ERROR_NONE); return UA_ERROR_NONE; } static int __uat_scenario_service_cleanup() { int ret = UA_ERROR_NONE; /* Remove service */ check_if(NULL == g_service_h); ret = ua_service_remove(g_service_h); retv_if(ret != UA_ERROR_NONE, ret); return ret; } static int __uat_scenario_user_cleanup() { int ret = UA_ERROR_NONE; /* Remove user */ check_if(NULL == g_user_h); ret = ua_user_remove(g_user_h); retv_if(ret != UA_ERROR_NONE, ret); uat_clear_user_list(); return ret; } static int run_ua_scenario_cleanup(MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg(" ### uat_scenario_cleanup ###"); /* monitor cleanup */ ret = __uat_scenario_monitor_cleanup(); msg(" - ua_scenario_monitor_cleanup() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); /* user cleanup */ ret = __uat_scenario_user_cleanup(); msg(" - ua_scenario_user_cleanup() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); /* service cleanup */ ret = __uat_scenario_service_cleanup(); msg(" - ua_scenario_service_cleanup() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); /* deinitialize ua */ ret = ua_deinitialize(); msg(" - ua_deinitialize() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static void __device_added_cb(int result, ua_device_h handle, void *user_data) { int ret; if (UA_ERROR_NONE != result) msgr("__device_added_cb() result: [0x%X] [%s]", result, uat_get_error_str(result)); else msgb("__device_added_cb() result: [0x%X] [%s]", result, uat_get_error_str(result)); _print_device_info(handle); if (UA_ERROR_NONE != result) return; /* Add device to service */ ret = ua_service_add_device(g_service_h, handle); msg(" - ua_service_add_device() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); ua_mac_type_e device_type = UA_MAC_TYPE_INVALID; ret = ua_device_get_mac_type(handle, &device_type); } static int __create_add_device(int device_type, int ostype, char *device_id_str, char *mac_addr_str, char *ipv4_addr_str, void *cb) { int ret = UA_ERROR_NONE; msg("__create_add_device"); ua_device_h device_h = NULL; ret = ua_device_create(device_type, mac_addr_str, device_id_str, &device_h); msg(" - ua_device_create() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); ret = ua_device_set_os_info(device_h, ostype); msg(" - ua_device_set_os_info() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); if (device_type == UA_MAC_TYPE_WIFI) { if (ipv4_addr_str) { ret = ua_device_set_wifi_ipv4_address(device_h, ipv4_addr_str); msg(" - ua_device_set_wifi_ipv4_address() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } } ret = ua_user_add_device(g_user_h, device_h, cb, NULL); msg(" - ua_user_add_device() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return ret; } static int run_ua_scenario_add_device(MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; ua_os_type_e ostype = UA_OS_TYPE_NOT_DEFINED; if (strlen(g_os_type)) ostype = (unsigned char)strtol(g_os_type, NULL, 10); if (ostype > UA_OS_TYPE_IOS) ostype = UA_OS_TYPE_IOS; else if (ostype < UA_OS_TYPE_NOT_DEFINED) ostype = UA_OS_TYPE_TIZEN; /* Add wifi device */ ret = __create_add_device(UA_MAC_TYPE_WIFI, ostype, g_device_id_str, g_mac_addr_str, g_ipv4_address_str, __device_added_cb); msg(" - Wi-Fi device add ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); /* Add ble device */ ret = __create_add_device(UA_MAC_TYPE_BLE, ostype, g_device_id_str, /*g_mac_addr_str*/g_device_id_str, g_ipv4_address_str, __device_added_cb); msg(" - BLE device add () ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_scenario_foreach(MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; /* Get service list */ uat_clear_service_list(); ret = ua_foreach_service(_foreach_added_service_cb, NULL); msg(" - ua_foreach_service() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); /* Get user list */ uat_clear_user_list(); ret = ua_foreach_users(_foreach_registered_user_cb, NULL); msg(" - ua_foreach_users() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); /* Get devices list */ uat_clear_device_list(); ret = ua_foreach_devices(_foreach_added_device_cb, NULL); msg(" - ua_foreach_devices() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_monitor_start_detection( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; ua_detection_mode_e detection_mode = UA_DETECT_MODE_INVALID; msg("run_ua_monitor_start_detection"); check_if(NULL == g_ua_mon_h); if (strlen(g_absence_type)) detection_mode = (unsigned char)strtol((g_absence_type), NULL, 10); ret = ua_monitor_start_absence_detection(g_ua_mon_h, (g_service_str[0] == '\0' ? NULL : g_service_h), detection_mode, _sensor_absence_detected_cb, g_ua_mon_h); msg(" - ua_monitor_start_absence_detection() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); if (strlen(g_presence_type)) detection_mode = (unsigned char)strtol(g_presence_type, NULL, 10); ret = ua_monitor_start_presence_detection(g_ua_mon_h, (g_service_str[0] == '\0' ? NULL : g_service_h), detection_mode, _sensor_presence_detected_cb, g_ua_mon_h); msg(" - ua_monitor_start_presence_detection() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_monitor_stop_detection( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("run_ua_monitor_stop_detection,"); check_if(NULL == g_ua_mon_h); ret = ua_monitor_stop_absence_detection(g_ua_mon_h); msg(" - ua_monitor_stop_absence_detection() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); ret = ua_monitor_stop_presence_detection(g_ua_mon_h); msg(" - ua_monitor_stop_presence_detection() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } /* loop test cases */ static int run_ua_scenario_loop_init_deinit( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; int loop = 10; if (strlen(g_loop_str)) loop = (unsigned char)strtol(g_loop_str, NULL, 10); if (loop > MENU_DATA_SIZE) loop = MENU_DATA_SIZE; for (int i = 0; i < loop; i++) { msg(" init deinit loop [%d] of [%d]", i + 1, loop); /* Initialize ua */ ret = ua_initialize(); msg(" - ua_initialize() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); usleep(100); /* Deinitialize ua */ ret = ua_deinitialize(); msg(" - ua_deinitialize() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } return RET_SUCCESS; } static int run_ua_scenario_loop_startup_cleanup( MManager *mm, struct menu_data *menu) { int loop = 10; if (strlen(g_loop_str)) loop = (unsigned char)strtol(g_loop_str, NULL, 10); if (loop > MENU_DATA_SIZE) loop = MENU_DATA_SIZE; for (int i = 0; i < loop; i++) { msg(" init deinit loop [%d] of [%d]", i + 1, loop); /* Startup ua */ run_ua_scenario_startup(NULL, NULL); usleep(100); /* Deinitialize ua */ run_ua_scenario_cleanup(NULL, NULL); } return RET_SUCCESS; } /* test case submenu */ static struct menu_data menu_detection[] = { { "1", "ABSENCE detection type (1:ALL 2:ANY)", NULL, NULL, g_absence_type }, { "2", "PRESENCE detection type (1:ALL 2:ANY)", NULL, NULL, g_presence_type }, { "3", "start", NULL, run_ua_monitor_start_detection, NULL }, { "4", "stop", NULL, run_ua_monitor_stop_detection, NULL }, { NULL, NULL, }, }; static struct menu_data menu_ua_scenario_add_device[] = { { "1", "OS type (1:Tizen 2:Andorid 3:iOS)", NULL, NULL, g_os_type }, { "2", "device_id", NULL, NULL, g_device_id_str }, { "3", "MAC", NULL, NULL, g_mac_addr_str }, { "4", "IPv4 address", NULL, NULL, g_ipv4_address_str }, { "5", "run", NULL, run_ua_scenario_add_device, NULL }, { NULL, NULL, }, }; /* loop test case submenu */ static struct menu_data menu_ua_scenario_loop_startup_cleanup[] = { { "1", "Number of loops", NULL, NULL, g_loop_str }, { "2", "run", NULL, run_ua_scenario_loop_startup_cleanup, NULL }, { NULL, NULL, }, }; static struct menu_data menu_ua_scenario_loop_init_deinit[] = { { "1", "Number of loops", NULL, NULL, g_loop_str }, { "2", "run", NULL, run_ua_scenario_loop_init_deinit, NULL }, { NULL, NULL, }, }; /* scenario test case menu */ struct menu_data menu_ua_scenarios[] = { { "1", "ua_startup", NULL, run_ua_scenario_startup, NULL }, { "2", "ua_cleanup", NULL, run_ua_scenario_cleanup, NULL }, { "3", "ua_add_device", menu_ua_scenario_add_device, NULL, NULL}, { "4", "ua_foreach", NULL, run_ua_scenario_foreach, NULL }, { "11", "start/stop absence/presence detection", menu_detection, NULL, NULL }, { "21", "ua_loop_init_deinit", menu_ua_scenario_loop_init_deinit, NULL, g_loop_str}, { "22", "ua_loop_startup_cleanup", menu_ua_scenario_loop_startup_cleanup, NULL, g_loop_str}, { NULL, NULL, }, };