feat: add cleanup semaphore operation and improve cleanup handling

This commit is contained in:
fujr 2025-03-14 16:00:40 +08:00
parent 0885d287df
commit 8ed5ae70b8
3 changed files with 39 additions and 9 deletions

View File

@ -161,15 +161,18 @@ static void clean_up()
{ {
err_msg("Failed to unlock tty device"); err_msg("Failed to unlock tty device");
} }
if (tcsetattr(s_fds.tty_fd, TCSANOW, &s_fds.old_termios) != 0)
{
err_msg("Error restoring old tty attributes");
return;
}
dbg_msg("Clean up success"); dbg_msg("Clean up success");
tcflush(s_fds.tty_fd, TCIOFLUSH);
if (s_fds.tty_fd >= 0) if (s_fds.tty_fd >= 0)
{
if (tcsetattr(s_fds.tty_fd, TCSANOW, &s_fds.old_termios) != 0)
{
err_msg("Error restoring old tty attributes");
return;
}
tcflush(s_fds.tty_fd, TCIOFLUSH);
close(s_fds.tty_fd); close(s_fds.tty_fd);
}
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -179,6 +182,14 @@ int main(int argc, char *argv[])
parse_user_input(argc, argv, profile); parse_user_input(argc, argv, profile);
dump_profile(); dump_profile();
#ifdef USE_SEMAPHORE #ifdef USE_SEMAPHORE
if (profile->op == CLEANUP_SEMAPHORE_OP)
{
if (unlock_at_port(profile->tty_dev))
{
err_msg("Failed to unlock tty device");
}
return SUCCESS;
}
if (profile->tty_dev != NULL) if (profile->tty_dev != NULL)
{ {
if (lock_at_port(profile->tty_dev)) if (lock_at_port(profile->tty_dev))
@ -189,12 +200,15 @@ int main(int argc, char *argv[])
} }
#endif #endif
// try open tty devices // try open tty devices
atexit(clean_up);
signal(SIGINT, clean_up);
signal(SIGTERM, clean_up);
if (tty_open_device(profile,fds)) if (tty_open_device(profile,fds))
{ {
err_msg("Failed to open tty device"); err_msg("Failed to open tty device");
return COMM_ERROR; return COMM_ERROR;
} }
atexit(clean_up);
if (run_op(profile,fds)) if (run_op(profile,fds))
{ {
err_msg("Failed to run operation %d", profile->op); err_msg("Failed to run operation %d", profile->op);

View File

@ -42,7 +42,10 @@
#define SMS_SEND_OP_L "sms_send" #define SMS_SEND_OP_L "sms_send"
#define SMS_DELETE_OP_S 'd' #define SMS_DELETE_OP_S 'd'
#define SMS_DELETE_OP_L "sms_delete" #define SMS_DELETE_OP_L "sms_delete"
#ifdef USE_SEMAPHORE
#define CLEANUP_SEMAPHORE_OP_S 'C'
#define CLEANUP_SEMAPHORE_OP_L "cleanup"
#endif
#define SET_READ_STORAGE "AT+CPMS=\"%s\"" #define SET_READ_STORAGE "AT+CPMS=\"%s\""
#define SET_PDU_FORMAT "AT+CMGF=0" #define SET_PDU_FORMAT "AT+CMGF=0"
#define READ_ALL_SMS "AT+CMGL=4" #define READ_ALL_SMS "AT+CMGL=4"
@ -141,7 +144,8 @@ enum OPERATIONS {
BINARY_AT_OP, BINARY_AT_OP,
SMS_READ_OP, SMS_READ_OP,
SMS_SEND_OP, SMS_SEND_OP,
SMS_DELETE_OP SMS_DELETE_OP,
CLEANUP_SEMAPHORE_OP
}; };
#endif #endif

View File

@ -287,6 +287,8 @@ int match_operation(char *operation_name)
return SMS_SEND_OP; return SMS_SEND_OP;
case SMS_DELETE_OP_S: case SMS_DELETE_OP_S:
return SMS_DELETE_OP; return SMS_DELETE_OP;
case CLEANUP_SEMAPHORE_OP_S:
return CLEANUP_SEMAPHORE_OP;
default: default:
return INVALID_PARAM; return INVALID_PARAM;
break; break;
@ -314,6 +316,10 @@ int match_operation(char *operation_name)
{ {
return SMS_DELETE_OP; return SMS_DELETE_OP;
} }
else if (strcmp(operation_name, CLEANUP_SEMAPHORE_OP_L) == 0)
{
return CLEANUP_SEMAPHORE_OP;
}
else else
{ {
return INVALID_PARAM; return INVALID_PARAM;
@ -388,11 +394,17 @@ int usage(char* name)
err_msg(" -D, --debug Debug mode Default: off"); err_msg(" -D, --debug Debug mode Default: off");
err_msg(" -p, --sms_pdu <sms pdu> SMS PDU"); err_msg(" -p, --sms_pdu <sms pdu> SMS PDU");
err_msg(" -i, --sms_index <sms index> SMS index"); err_msg(" -i, --sms_index <sms index> SMS index");
#ifdef USE_SEMAPHORE
err_msg(" -C, --cleanup Semaphore cleanup");
#endif
err_msg("Example:"); err_msg("Example:");
err_msg(" %s -c ATI -d /dev/ttyUSB2 -b 115200 -B 8 -o at #advance at mode set bautrate and data bit", name); err_msg(" %s -c ATI -d /dev/ttyUSB2 -b 115200 -B 8 -o at #advance at mode set bautrate and data bit", name);
err_msg(" %s -c ATI -d /dev/ttyUSB2 # normal at mode", name); err_msg(" %s -c ATI -d /dev/ttyUSB2 # normal at mode", name);
err_msg(" %s -c ATI -d /dev/ttyUSB2 -o binary_at -c 4154490D0A # means sending ATI to ttyUSB2", name); err_msg(" %s -c ATI -d /dev/ttyUSB2 -o binary_at -c 4154490D0A # means sending ATI to ttyUSB2", name);
err_msg(" %s -d /dev/mhi_DUN -o r # read sms", name); err_msg(" %s -d /dev/mhi_DUN -o r # read sms", name);
#ifdef USE_SEMAPHORE
err_msg(" %s -d /dev/mhi_DUN -o C # force cleanup semaphore", name);
#endif
exit(-1); exit(-1);
} }