/* * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. * * 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 #ifdef SUSPEND_RESUME_TEST #include #include #endif #include #include #include "uat-menu.h" #include "uat-common.h" extern ua_monitor_h g_ua_mon_h; /**< Monitor handle */ extern ua_service_h g_service_h; /**< Service handle */ extern char g_service_str[MENU_DATA_SIZE + 1]; /**< Service string */ static char cycle_time[MENU_DATA_SIZE + 1] = {"900", }; /**< cycle time */ static char window[MENU_DATA_SIZE + 1] = {"55", }; /**< Detection window */ 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 */ static char g_scan_time_multiplier[MENU_DATA_SIZE + 1] = {0,}; /**< 10ms * what number */ static char g_lpm_sensor[MENU_DATA_SIZE + 1] = "2"; /**< Sensor to set LPM */ static char g_lpm_onoff[MENU_DATA_SIZE + 1] = "1"; /**< LPM mode on/off */ static void __sensor_presence_detected_device(ua_device_h device_handle) { int ret; char *mac = NULL; ua_mac_type_e mac_type; ret = ua_device_get_mac_address(device_handle, &mac); if (UA_ERROR_NONE != ret) { msg(" - ua_device_get_mac_address() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } ret = ua_device_get_mac_type(device_handle, &mac_type); if (UA_ERROR_NONE != ret) { msg(" - ua_device_get_mac_type() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } msgb("[%s] Presence detected on [%s]", uat_convert_device_mac_type_to_txt(mac_type), mac); g_free(mac); } static void __sensor_detected_sensor_info(int type, ua_sensor_type_e sensor, ua_sensor_h sensor_handle) { int ret; unsigned long long timestamp; ret = ua_sensor_get_timestamp(sensor_handle, ×tamp); if (UA_ERROR_NONE != ret) { msg(" - ua_sensor_get_timestamp() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } if (UAT_PRESENCE == type) msgb("[%s] timestamp [%llu]", uat_get_sensor_bitmask_str(sensor), timestamp); else msgp("[%s] timestamp [%llu]", uat_get_sensor_bitmask_str(sensor), timestamp); } void _sensor_presence_detected_cb(int result, ua_monitor_h monitor, ua_sensor_type_e sensor, ua_service_h service_handle, ua_device_h device_handle, ua_sensor_h sensor_handle, void *user_data) { char *pbuf = uat_get_time(); msgb("\n[%s]", pbuf); free(pbuf); int ret; char *service_name = NULL; ret = ua_service_get_name(g_service_h, &service_name); if (UA_ERROR_NONE != ret) { msg(" - ua_service_get_name() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } msgb("Presence detected for service [%s]", service_name); if (UA_SENSOR_BLE == (UA_SENSOR_BLE & sensor)) { msgb("[%s] PRESENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_BLE), uat_get_error_str(result)); } if (UA_SENSOR_WIFI == (UA_SENSOR_WIFI & sensor)) { msgb("[%s] PRESENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_WIFI), uat_get_error_str(result)); } if (UA_SENSOR_LIGHT == (UA_SENSOR_LIGHT & sensor)) { msgb("[%s] PRESENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_LIGHT), uat_get_error_str(result)); } if (UA_SENSOR_MOTION == (UA_SENSOR_MOTION & sensor)) { msgb("[%s] PRESENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_MOTION), uat_get_error_str(result)); } /* For device information */ if (device_handle) __sensor_presence_detected_device(device_handle); /* For sensor information */ if (sensor_handle && (sensor & (UA_SENSOR_LIGHT | UA_SENSOR_MOTION))) __sensor_detected_sensor_info(UAT_PRESENCE, sensor, sensor_handle); } void _sensor_absence_detected_cb(int result, ua_monitor_h monitor, ua_service_h service_handle, ua_sensor_type_e sensor, ua_sensor_h sensor_handle, void *user_data) { char *pbuf = uat_get_time(); msgp("\n[%s]", pbuf); free(pbuf); int ret; char *service_name = NULL; ret = ua_service_get_name(g_service_h, &service_name); if (UA_ERROR_NONE != ret) { msg(" - ua_service_get_name() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } msgb("Absence detected for service [%s]", service_name); if (UA_SENSOR_BLE == (UA_SENSOR_BLE & sensor)) { msgp("[%s] ABSENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_BLE), uat_get_error_str(result)); } if (UA_SENSOR_WIFI == (UA_SENSOR_WIFI & sensor)) { msgp("[%s] ABSENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_WIFI), uat_get_error_str(result)); } if (UA_SENSOR_LIGHT == (UA_SENSOR_LIGHT & sensor)) { msgp("[%s] ABSENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_LIGHT), uat_get_error_str(result)); } if (UA_SENSOR_MOTION == (UA_SENSOR_MOTION & sensor)) { msgp("[%s] ABSENCE detected [%s]", uat_get_sensor_bitmask_str(UA_SENSOR_MOTION), uat_get_error_str(result)); } /* For sensor information */ if (sensor_handle && (sensor & (UA_SENSOR_LIGHT | UA_SENSOR_MOTION))) __sensor_detected_sensor_info(UAT_ABSENCE, sensor, sensor_handle); } void __ua_test_scan_completed_cb(ua_active_scan_type_e result, ua_monitor_h handle, ua_device_h device_handle, void *user_data) { int ret; if (result == UA_ACTIVE_SCAN_TYPE_DEVICE_FOUND && device_handle) { char *mac = NULL; ret = ua_device_get_mac_address(device_handle, &mac); if (UA_ERROR_NONE == ret) { msg(" Found Mac Address : [%s]", mac); g_free(mac); } msg(" - ua_test_scan_completed_cb ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } else msg("SCAN COMPLETED!!"); } static int run_ua_set_detection_cycle(MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; unsigned int cycle_value = 0; msg("ua_set_detection_cycle"); if (strlen(cycle_time)) cycle_value = (unsigned int)strtol(cycle_time, NULL, 10); ret = ua_get_service_by_name(g_service_str, &g_service_h); if (UA_ERROR_NONE != ret) { msgr(" - ua_get_service_by_name() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } ret = ua_set_detection_cycle(g_service_h, cycle_value); msg(" - ua_set_detection_cycle() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_get_detection_cycle( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; unsigned int cycle_value = 0; msg("ua_get_detection_cycle"); ret = ua_get_service_by_name(g_service_str, &g_service_h); if (UA_ERROR_NONE != ret) { msgr(" - ua_get_service_by_name() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } ret = ua_get_detection_cycle(g_service_h, &cycle_value); if (UA_ERROR_NONE == ret) snprintf(cycle_time, sizeof(cycle_time), "%u", cycle_value); msg(" - ua_get_detection_cycle() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); if (UA_ERROR_NONE == ret) msgb("Detection [cycle_time = %u]", cycle_value); return RET_SUCCESS; } static int run_ua_set_detection_window(MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; unsigned int window_value = 0; msg("ua_set_detection_window"); if (strlen(window)) window_value = (unsigned int)strtol(window, NULL, 10); ret = ua_set_detection_window(window_value); msg(" - ua_set_detection_window() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_get_detection_window( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; unsigned int window_value = 0; msg("ua_get_detection_window"); ret = ua_get_service_by_name(g_service_str, &g_service_h); if (UA_ERROR_NONE != ret) { msgr(" - ua_get_service_by_name() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); } ret = ua_get_detection_window(&window_value); msg(" - ua_get_detection_window() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); if (UA_ERROR_NONE == ret) msgb("Detection [window = %u]", window_value); return RET_SUCCESS; } static int run_ua_monitor_start_presence_detection( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("ua_monitor_start_presence_detection"); ua_detection_mode_e detection_mode = UA_DETECT_MODE_INVALID; check_if(NULL == g_ua_mon_h); 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_presence_detection( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("ua_monitor_stop_presence_detectio"); check_if(NULL == g_ua_mon_h); ret = ua_monitor_stop_presence_detection(g_ua_mon_h); msg(" - ua_monitor_stop_presence_detectio() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_monitor_start_absence_detection( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("ua_monitor_start_absence_detection"); check_if(NULL == g_ua_mon_h); ua_detection_mode_e detection_mode = UA_DETECT_MODE_INVALID; 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)); return RET_SUCCESS; } static int run_ua_monitor_stop_absence_detection( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("ua_monitor_stop_absence_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)); return RET_SUCCESS; } static int run_ua_monitor_start_scan( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; unsigned int scantimemultiplier = 0; msg("ua_monitor_start_scan"); check_if(NULL == g_ua_mon_h); if (strlen(g_scan_time_multiplier)) scantimemultiplier = (unsigned int)strtol(g_scan_time_multiplier, NULL, 10); ret = ua_monitor_start_scan(g_ua_mon_h, scantimemultiplier, __ua_test_scan_completed_cb, NULL); msg(" - ua_monitor_stop_absence_detection() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_monitor_stop_scan( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("ua_monitor_stop_scan,"); check_if(NULL == g_ua_mon_h); ret = ua_monitor_stop_scan(g_ua_mon_h); msg("ua_monitor_stop_scan,() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_monitor_start_absence_presence( 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_absence_presence"); 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_absence_presence( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("run_ua_monitor_stop_absence_presence,"); 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; } static int run_ua_enable_low_power_mode( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("ua_enable_low_power_mode"); ret = ua_enable_low_power_mode(); msg(" - ua_enable_low_power_mode() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_disable_low_power_mode( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("ua_disable_low_power_mode"); ret = ua_disable_low_power_mode(); msg(" - ua_disable_low_power_mode() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_ua_set_low_power_mode( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; ua_sensor_type_e sensor = UA_SENSOR_ALL; int onoff = 0; if (strlen(g_lpm_sensor)) sensor = (unsigned char)strtol((g_lpm_sensor), NULL, 10); if (strlen(g_lpm_onoff)) onoff = (unsigned char)strtol((g_lpm_onoff), NULL, 10); msg("ua_set_low_power_mode bitmask [%x]", sensor); ret = ua_set_low_power_mode(sensor, onoff == 1 ? true : false); msg(" - ua_set_low_power_mode() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } #ifdef SUSPEND_RESUME_TEST static int run_device_power_request_poweroff( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; int off_reason = POWER_OFF_REASON_ART_APP_TO_SUSPEND | POWER_OFF_TO_SUSPEND; msg("device_power_request_poweroff"); if (0 != device_power_request_poweroff(off_reason)) { msgr("Fail to request power off"); return false; } ret = device_power_set_wakeup_reason(POWER_WAKEUP_REASON_AMBIENT_READY); if (POWER_ERROR_NONE != ret) { msgr("Fail to set wakeup reason"); return RET_SUCCESS; } ret = device_power_set_state(POWER_STATE_STANDBY, 1); if (POWER_ERROR_NONE != ret) { msgr("Failed to request TV to STANDBY!"); return RET_SUCCESS; } msg(" - device_power_request_poweroff() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_device_power_lock_state( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("device_power_lock_state"); /* Power unlock */ ret = device_power_lock_state(POWER_STATE_STANDBY, 0); if (POWER_ERROR_NONE != ret) msgr("Fail to request lock power state"); msg(" - device_power_lock_state() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } static int run_device_power_unlock_state( MManager *mm, struct menu_data *menu) { int ret = UA_ERROR_NONE; msg("device_power_unlock_state"); /* Power unlock */ ret = device_power_unlock_state(POWER_STATE_STANDBY); if (POWER_ERROR_NONE != ret) msgr("Fail to request unlock power state"); msg(" - device_power_unlock_state() ret: [0x%X] [%s]", ret, uat_get_error_str(ret)); return RET_SUCCESS; } #endif /* SUSPEND_RESUME_TEST */ static struct menu_data menu_ua_set_detection_cycle[] = { { "1", "service", NULL, NULL, g_service_str }, { "2", "cycle_time", NULL, NULL, cycle_time }, { "3", "run", NULL, run_ua_set_detection_cycle, NULL }, { NULL, NULL, }, }; static struct menu_data menu_ua_get_detection_cycle[] = { { "1", "service", NULL, NULL, g_service_str }, { "2", "run", NULL, run_ua_get_detection_cycle, NULL }, { NULL, NULL, }, }; static struct menu_data menu_ua_set_detection_window[] = { { "1", "window", NULL, NULL, window }, { "2", "run", NULL, run_ua_set_detection_window, NULL }, { NULL, NULL, }, }; static struct menu_data menu_start_device_scan[] = { { "1", "Device scan time multiplier", NULL, NULL, g_scan_time_multiplier}, { "2", "run", NULL, run_ua_monitor_start_scan, NULL }, { NULL, NULL, }, }; static struct menu_data menu_ua_start_presence[] = { { "1", "type (1:ALL 2:ANY)", NULL, NULL, g_presence_type }, { "2", "service(empty for default, else selected service)", NULL, NULL, g_service_str}, { "3", "start", NULL, run_ua_monitor_start_presence_detection, NULL }, { "4", "stop", NULL, run_ua_monitor_stop_presence_detection, NULL }, { NULL, NULL, }, }; static struct menu_data menu_ua_start_absence[] = { { "1", "type (1:ALL 2:ANY)", NULL, NULL, g_absence_type }, { "2", "service(empty for default, else selected service)", NULL, NULL, g_service_str}, { "3", "start", NULL, run_ua_monitor_start_absence_detection, NULL }, { "4", "stop", NULL, run_ua_monitor_stop_absence_detection, NULL }, { NULL, NULL, }, }; static struct menu_data menu_start_absence_presence[] = { { "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_absence_presence, NULL }, { "4", "stop", NULL, run_ua_monitor_stop_absence_presence, NULL }, { NULL, NULL, }, }; static struct menu_data menu_set_low_power_mode[] = { { "1", "(1 BT 2:BLE 4:Wi-Fi 8:Motion 16:Light 32:Audio)", NULL, NULL, g_lpm_sensor }, { "2", "(0:OFF 1:ON)", NULL, NULL, g_lpm_onoff }, { "3", "ua_set_low_power_mode", NULL, run_ua_set_low_power_mode, NULL }, { NULL, NULL, }, }; struct menu_data menu_ua_detections[] = { { "1", "ua_set_detection_cycle", menu_ua_set_detection_cycle, NULL, NULL}, { "2", "ua_get_detection_cycle", menu_ua_get_detection_cycle, NULL, NULL}, { "3", "ua_set_detection_window", menu_ua_set_detection_window, NULL, NULL}, { "4", "ua_get_detection_window", NULL, run_ua_get_detection_window, NULL}, { "5", "ua_monitor_start/stop_presence_detection", menu_ua_start_presence, NULL, NULL }, { "6", "ua_monitor_start/stop_absence_detection", menu_ua_start_absence, NULL, NULL }, { "7", "start/stop absence/presence simulataneously", menu_start_absence_presence, NULL, NULL }, { "8", "ua_monitor_start_device_scan", menu_start_device_scan, NULL, NULL }, { "9", "ua_monitor_stop_scan", NULL, run_ua_monitor_stop_scan, NULL }, { "10", "ua_enable_low_power_mode", NULL, run_ua_enable_low_power_mode, NULL }, { "11", "ua_disable_low_power_mode", NULL, run_ua_disable_low_power_mode, NULL }, { "12", "ua_set_low_power_mode", menu_set_low_power_mode, NULL, NULL }, #ifdef SUSPEND_RESUME_TEST { "97", "request_power_off", NULL, run_device_power_request_poweroff, NULL }, { "98", "device_power_lock_state", NULL, run_device_power_lock_state, NULL }, { "99", "device_power_unlock_state", NULL, run_device_power_unlock_state, NULL }, #endif { NULL, NULL, }, };