本文主要是介绍c tool系列------c语言编程常用语句,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
针对main函数的argc,argv
for (i = 1; i < argc ;) {if (0 == strcmp(argv[i], "-m") && (argc - i > 1)) {tty_modem = argv[i + 1];i += 2;} else if (0 == strcmp(argv[i], "-p") && (argc - i > 1)) {tty_pc = argv[i + 1];i += 2;} else if (0 == strcmp(argv[i], "-d") && (argc - i > 1)) {i += 2;} else if (0 == strcmp(argv[i], "-f") && (argc - i > 1)) {modem_config_file = argv[i + 1];i += 2;} else if (0 == strcmp(argv[i], "-t") && (argc - i > 1)) {tunnel_switch = argv[i + 1];if (0 == strncmp(tunnel_switch, "on", 2)) {tunnel_mode = 1;}i += 2;}}
使用poll函数
#define AT_CMD_OK 0
#define AT_CMD_REOPEN 1
#define AT_CMD_RESEND 2result = wait_for_data (fd, 3000 );if (result == AT_CMD_OK ) {if (read(fd, buf, buf_size-1) < 0) {LOGE(TAG"Read response from %s error, error num is %d\n", tty_name, errno);return -1;}} else if (result == AT_CMD_RESEND) {poll_cnt++;LOGD(TAG"Wait %s result, poll %s failed %d times\n", at_cmd, tty_name, poll_cnt);sleep(2);if (poll_cnt >= POLL_RETRY_MAX)return -1;} else {LOGD(TAG"Wait %s result, poll %s failed and need reopen.\n", at_cmd, tty_name);modem_hangup_flag = 1;return -1;}
int wait_for_data( int fd, long wait_time_ms)
{struct pollfd fds[1];int poll_ret;int retVal = AT_CMD_OK;fds[0].fd = fd;fds[0].events = POLLIN;fds[0].revents = 0;poll_ret = poll( fds, 1, wait_time_ms);if ( 0 < poll_ret ) {if ( fds[0].revents & POLLHUP ) {LOGE(TAG"device hung up\n");retVal = AT_CMD_REOPEN;goto Exit_Handler;} else if ( fds[0].revents & POLLIN ) {LOGV(TAG"Received response data\n");} else {/* Not sure we will ever get here */LOGE(TAG"Unexpected event while waiting for response\n");retVal = AT_CMD_RESEND;goto Exit_Handler;}} else if ( 0 == poll_ret ) {LOGE(TAG"Timed out while waiting for response\n");retVal = AT_CMD_RESEND;goto Exit_Handler;} else {LOGE(TAG"Error occured polling for response. errno: %d\n", errno);retVal = AT_CMD_RESEND;goto Exit_Handler;}retVal = AT_CMD_OK;Exit_Handler:return retVal;
}
关闭文件操作
static void fdclose()
{if (md_fd != -1) {close(md_fd);md_fd = -1;}if (pc_fd != -1) {/* Add errno trace in case that "unable write usb value" problem of BZ4161 is reproduced.*/if (close(pc_fd) < 0) {LOGE("close(pc_fd:%d) failed, error: %d, %s\n", pc_fd, errno, strerror(errno));}pc_fd = -1;}
}
这篇关于c tool系列------c语言编程常用语句的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!