b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <dlfcn.h> |
| 5 | #include <stdint.h> |
| 6 | #include <string.h> |
| 7 | #include <pthread.h> |
| 8 | |
| 9 | //mbtk include |
| 10 | |
| 11 | typedef unsigned int uint32; |
| 12 | typedef unsigned char uint8; |
| 13 | typedef unsigned short uint16; |
| 14 | typedef void (*mbtk_info_callback_func)(const void* data, int data_len); |
| 15 | |
| 16 | typedef struct |
| 17 | { |
| 18 | int client_fd; |
| 19 | pthread_t read_thread_id; |
| 20 | int exit_fd[2]; |
| 21 | bool is_waitting; |
| 22 | pthread_cond_t cond; |
| 23 | pthread_mutex_t mutex; |
| 24 | |
| 25 | pthread_mutex_t send_mutex; |
| 26 | |
| 27 | // Temp response data. |
| 28 | uint16 info_err; |
| 29 | uint16 data_len; |
| 30 | void *data; |
| 31 | |
| 32 | //mbtk wyq for server_ready_status add start |
| 33 | char server_ready_status; |
| 34 | //mbtk wyq for server_ready_status add end |
| 35 | |
| 36 | mbtk_info_callback_func net_state_cb; |
| 37 | mbtk_info_callback_func call_state_cb; |
| 38 | mbtk_info_callback_func sms_state_cb; |
| 39 | mbtk_info_callback_func radio_state_cb; |
| 40 | mbtk_info_callback_func sim_state_cb; |
| 41 | mbtk_info_callback_func pdp_state_cb; |
| 42 | //add signal by xr |
| 43 | mbtk_info_callback_func signal_state_cb; |
| 44 | } mbtk_info_handle_t; |
| 45 | |
| 46 | typedef enum { |
| 47 | MBTK_SIM_ABSENT = 0, |
| 48 | MBTK_SIM_NOT_READY = 1, |
| 49 | MBTK_SIM_READY = 2, |
| 50 | MBTK_SIM_PIN = 3, |
| 51 | MBTK_SIM_PUK = 4, |
| 52 | MBTK_SIM_NETWORK_PERSONALIZATION = 5 |
| 53 | } mbtk_sim_state_enum; |
| 54 | |
| 55 | typedef enum |
| 56 | { |
| 57 | MBTK_DEV_MODEM_MIN_FUN, //Modem 最小功能 |
| 58 | MBTK_DEV_MODEM_FULL_FUN, //Modem 全功能 |
| 59 | MBTK_DEV_MODEM_DISABLE_RECEIVE_RF_CIRCUITS = 3, //Modem 禁用射频接收电路 |
| 60 | MBTK_DEV_MODEM_DISABLE_TRANSMIT_AND_RECEIVE_RF_CIRCUITS, //Modem禁用射频发射和接收电路 |
| 61 | MBTK_DEV_MODEM_DISABLE_SIM, //Modem 禁用(U)SIM 卡 |
| 62 | MBTK_DEV_MODEM_TURN_OFF_FULL_SECONDARY_RECEIVE, //Modem 完全禁用辅助接收 |
| 63 | }MBTK_DEV_MODEM_FUNCTION; |
| 64 | |
| 65 | typedef struct |
| 66 | { |
| 67 | MBTK_DEV_MODEM_FUNCTION fun; |
| 68 | int rst; |
| 69 | } mbtk_modem_info_t; |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | //gsw include |
| 75 | #define GSW_HAL_SUCCESS 0 |
| 76 | #define GSW_HAL_FAIL -1 |
| 77 | #define GSW_HAL_MEM_INVAILD -2 |
| 78 | #define GSW_SIM_ICCID_LENGTH 20+1 |
| 79 | #define GSW_SIM_IMSI_LENGTH 20+1 |
| 80 | #define GSW_SIM_MSISDN_LENGTH 20+1 |
| 81 | #define GSW_SIM_IMEI_LENGTH 20+1 |
| 82 | |
| 83 | typedef enum sim_status |
| 84 | { |
| 85 | SIM_STATUS_ABSENT = 0, /**< sim absent*/ |
| 86 | SIM_STATUS_PRESENT = 1, /**< sim present mtk as ready*/ |
| 87 | SIM_STATUS_ERROR = 2, /**< sim error*/ |
| 88 | SIM_STATUS_READY = 3, /**< sim state ready mtk no this value*/ |
| 89 | SIM_STATUS_PIN = 4, /**< pinlock status*/ |
| 90 | } sim_status_e_type; |
| 91 | |
| 92 | |
| 93 | int (*gsw_sim_sdk_init)(int32_t token); |
| 94 | int (*gsw_sim_sdk_deinit)(void); |
| 95 | int (*gsw_get_sim_status)(int32_t *sim_state); |
| 96 | int (*gsw_get_sim_iccid)(int32_t len, int8_t *iccid); |
| 97 | int (*gsw_get_sim_imsi)(int32_t len, int8_t *imsi); |
| 98 | int (*gsw_get_sim_msisdn)(int32_t len, int8_t *msisdn); |
| 99 | int (*gsw_get_imei)(int32_t len, int8_t *imei); |
| 100 | int (*gsw_set_sim_power_down)(void); |
| 101 | int (*gsw_set_sim_power_up)(void); |
| 102 | int (*gsw_reset_modem)(void); |
| 103 | |
| 104 | |
| 105 | #define lib_gsw_sim_path "/lib/libgsw_lib.so" |
| 106 | static void *dlHandle_sim = NULL; |
| 107 | |
| 108 | |
| 109 | static int gsw_sim_api_import() |
| 110 | { |
| 111 | dlHandle_sim = dlopen(lib_gsw_sim_path, RTLD_NOW); |
| 112 | if (dlHandle_sim == NULL) { |
| 113 | printf("dlopen gsw_sim_sdk_init fail\n"); |
| 114 | return GSW_HAL_FAIL; |
| 115 | } |
| 116 | |
| 117 | gsw_sim_sdk_init = (int(*)(int32_t token))dlsym(dlHandle_sim, "gsw_sim_sdk_init"); |
| 118 | if (gsw_sim_sdk_init == NULL) { |
| 119 | printf("dlsym gsw_sim_sdk_init fail\n"); |
| 120 | return GSW_HAL_FAIL; |
| 121 | } |
| 122 | |
| 123 | gsw_sim_sdk_deinit = (int(*)(void))dlsym(dlHandle_sim, "gsw_sim_sdk_deinit"); |
| 124 | if (gsw_sim_sdk_deinit == NULL) { |
| 125 | printf("dlsym gsw_sim_sdk_deinit fail\n"); |
| 126 | return GSW_HAL_FAIL; |
| 127 | } |
| 128 | |
| 129 | gsw_get_sim_status = (int(*)(int32_t *sim_state))dlsym(dlHandle_sim, "gsw_get_sim_status"); |
| 130 | if (gsw_get_sim_status == NULL) { |
| 131 | printf("dlsym gsw_get_sim_status fail\n"); |
| 132 | return GSW_HAL_FAIL; |
| 133 | } |
| 134 | |
| 135 | gsw_get_sim_iccid = (int(*)(int32_t len, int8_t *iccid))dlsym(dlHandle_sim, "gsw_get_sim_iccid"); |
| 136 | if (gsw_get_sim_iccid == NULL) { |
| 137 | printf("dlsym gsw_get_sim_iccid fail\n"); |
| 138 | return GSW_HAL_FAIL; |
| 139 | } |
| 140 | |
| 141 | gsw_get_sim_imsi = (int(*)(int32_t len, int8_t *imsi))dlsym(dlHandle_sim, "gsw_get_sim_imsi"); |
| 142 | if (gsw_get_sim_imsi == NULL) { |
| 143 | printf("dlsym gsw_get_sim_imsi fail\n"); |
| 144 | return GSW_HAL_FAIL; |
| 145 | } |
| 146 | |
| 147 | gsw_get_sim_msisdn = (int(*)(int32_t len, int8_t *msisdn))dlsym(dlHandle_sim,"gsw_get_sim_msisdn"); |
| 148 | if (gsw_get_sim_msisdn == NULL) { |
| 149 | printf("dlsym gsw_get_sim_msisdn fail\n"); |
| 150 | return GSW_HAL_FAIL; |
| 151 | } |
| 152 | |
| 153 | gsw_get_imei = (int(*)(int32_t len, int8_t *imei))dlsym(dlHandle_sim,"gsw_get_imei"); |
| 154 | if (gsw_get_imei == NULL) { |
| 155 | printf("dlsym gsw_get_imei fail\n"); |
| 156 | return GSW_HAL_FAIL; |
| 157 | } |
| 158 | |
| 159 | gsw_set_sim_power_down = (int(*)(void))dlsym(dlHandle_sim,"gsw_set_sim_power_down"); |
| 160 | if (gsw_set_sim_power_down == NULL) { |
| 161 | printf("dlsym gsw_set_sim_power_down fail\n"); |
| 162 | return GSW_HAL_FAIL; |
| 163 | } |
| 164 | |
| 165 | gsw_set_sim_power_up = (int(*)(void))dlsym(dlHandle_sim,"gsw_set_sim_power_up"); |
| 166 | if (gsw_set_sim_power_up == NULL) { |
| 167 | printf("dlsym gsw_set_sim_power_up fail\n"); |
| 168 | return GSW_HAL_FAIL; |
| 169 | } |
| 170 | |
| 171 | gsw_reset_modem = (int(*)(void))dlsym(dlHandle_sim,"gsw_reset_modem"); |
| 172 | if (gsw_reset_modem == NULL) { |
| 173 | printf("dlsym gsw_reset_modem fail\n"); |
| 174 | return GSW_HAL_FAIL; |
| 175 | } |
| 176 | |
| 177 | return GSW_HAL_SUCCESS; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | int main() |
| 183 | { |
| 184 | char operator[10]; |
| 185 | int opt; |
| 186 | int ret = -1; |
| 187 | |
| 188 | gsw_sim_api_import(); |
| 189 | |
| 190 | while(1) |
| 191 | { |
| 192 | printf("-1.exit\n"); |
| 193 | printf("1.sim init\n"); |
| 194 | printf("2.sim deinit\n"); |
| 195 | printf("3.imsi : Get IMSI.\n"); |
| 196 | printf("4.iccid : Get ICCID.\n"); |
| 197 | printf("5.msisdn : Get phone number.\n"); |
| 198 | printf("6.sim states\n"); |
| 199 | printf("7.sim power_down\n"); |
| 200 | printf("8.sim power_up\n"); |
| 201 | printf("9.reset modem\n"); |
| 202 | printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Enter cmd:"); |
| 203 | |
| 204 | char* temp = NULL; |
| 205 | memset(operator, 0, sizeof(operator)); |
| 206 | temp = fgets(operator, sizeof(operator), stdin); |
| 207 | fflush(stdin); |
| 208 | |
| 209 | printf("temp = %s",temp); |
| 210 | opt = atoi(operator); |
| 211 | switch(opt) |
| 212 | { |
| 213 | case -1: |
| 214 | { |
| 215 | printf("exit\n"); |
| 216 | return 0; |
| 217 | } |
| 218 | //gsw_sim_sdk_init |
| 219 | case 1: |
| 220 | { |
| 221 | printf("sdk_init\n"); |
| 222 | ret = gsw_sim_sdk_init(123); |
| 223 | if(ret == 0) |
| 224 | { |
| 225 | printf("gsw_sim_sdk_init success\n"); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | printf("gsw_sim_sdk_init fail,ret = %d\n",ret); |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | //sim deinit |
| 235 | case 2: |
| 236 | { |
| 237 | printf("sim deinit\n"); |
| 238 | ret = gsw_sim_sdk_deinit(); |
| 239 | if(ret == 0) |
| 240 | { |
| 241 | printf("gsw_sim_sdk_deinit success\n"); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | printf("gsw_sim_sdk_deinit fail,ret = %d\n",ret); |
| 246 | } |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | //imsi |
| 251 | case 3: |
| 252 | { |
| 253 | char imsi[GSW_SIM_IMEI_LENGTH] = {0}; |
| 254 | ret = gsw_get_sim_imsi(GSW_SIM_IMEI_LENGTH, (int8_t *)imsi); |
| 255 | if(ret == 0) |
| 256 | { |
| 257 | printf("gsw_get_sim_imsi success, imsi = %s\n",imsi); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | printf("gsw_get_sim_imsi fail,ret = %d\n",ret); |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | //iccid |
| 267 | case 4: |
| 268 | { |
| 269 | char iccid[GSW_SIM_ICCID_LENGTH] = {0}; |
| 270 | ret = gsw_get_sim_iccid(GSW_SIM_ICCID_LENGTH, (int8_t *)iccid); |
| 271 | if(ret == 0) |
| 272 | { |
| 273 | printf("gsw_get_sim_iccid success, iccid = %s\n",iccid); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | printf("gsw_get_sim_iccid fail,ret = %d\n",ret); |
| 278 | } |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | //msisdn |
| 283 | case 5: |
| 284 | { |
| 285 | char msisdn[GSW_SIM_MSISDN_LENGTH] = {0}; |
| 286 | ret = gsw_get_sim_msisdn(GSW_SIM_MSISDN_LENGTH, (int8_t *)msisdn); |
| 287 | if(ret == 0) |
| 288 | { |
| 289 | printf("gsw_get_sim_msisdn success, msisdn = %s\n",msisdn); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | printf("gsw_get_sim_msisdn fail,ret = %d\n",ret); |
| 294 | } |
| 295 | break; |
| 296 | } |
| 297 | //sim states |
| 298 | case 6: |
| 299 | { |
| 300 | int sim_state; |
| 301 | printf("start gsw_get_sim_status\n"); |
| 302 | ret = gsw_get_sim_status(&sim_state); |
| 303 | printf("end gsw_get_sim_status\n"); |
| 304 | if(ret == 0) |
| 305 | { |
| 306 | printf("gsw_get_sim_status success,sim_state = %d\n",sim_state); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | printf("gsw_get_sim_status fail,ret = %d\n",ret); |
| 311 | } |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | //sim power_down |
| 316 | case 7: |
| 317 | { |
| 318 | ret = gsw_set_sim_power_down(); |
| 319 | if(ret == 0) |
| 320 | { |
| 321 | printf("gsw_set_sim_power_down success\n"); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | printf("gsw_set_sim_power_down fail,ret = %d\n",ret); |
| 326 | } |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | //sim power_up |
| 331 | case 8: |
| 332 | { |
| 333 | ret = gsw_set_sim_power_up(); |
| 334 | if(ret == 0) |
| 335 | { |
| 336 | printf("gsw_set_sim_power_up success\n"); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | printf("gsw_set_sim_power_up fail,ret = %d\n",ret); |
| 341 | } |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | //reset modem |
| 346 | case 9: |
| 347 | { |
| 348 | ret = gsw_reset_modem(); |
| 349 | if(ret == 0) |
| 350 | { |
| 351 | printf("gsw_reset_modem success\n"); |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | printf("gsw_reset_modem fail,ret = %d\n",ret); |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | default : |
| 361 | { |
| 362 | printf("error cmd.\n"); |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | } |