add an option for releasing client ids
[project/uqmi.git] / main.c
diff --git a/main.c b/main.c
index 7bfaba8..3a86c84 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <getopt.h>
+#include <signal.h>
 
 #include "uqmi.h"
 #include "commands.h"
@@ -18,7 +19,8 @@ static const char *device;
 static const struct option uqmi_getopt[] = {
        __uqmi_commands,
        { "device", required_argument, NULL, 'd' },
-       { "keep-client-id", required_argument, NULL, 'K' },
+       { "keep-client-id", required_argument, NULL, 'k' },
+       { "release-client-id", required_argument, NULL, 'r' },
        { NULL, 0, NULL, 0 }
 };
 #undef __uqmi_command
@@ -30,6 +32,7 @@ static int usage(const char *progname)
                "  --device=NAME, -d NAME:           Set device name to NAME (required)\n"
                "  --keep-client-id <name>:          Keep Client ID for service <name>\n"
                "                                    (implies --keep-client-id)\n"
+               "  --release-client-id <name>:       Release Client ID after exiting\n"
                "\n"
                "Services:                           dms, nas, pds, wds, wms\n"
                "\n"
@@ -41,6 +44,7 @@ static int usage(const char *progname)
                wds_helptext
                dms_helptext
                nas_helptext
+               wms_helptext
                "\n", progname);
        return 1;
 }
@@ -55,11 +59,31 @@ static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
        qmi_service_get_client_id(qmi, svc);
 }
 
+static void release_client_id(struct qmi_dev *qmi, const char *optarg)
+{
+       QmiService svc = qmi_service_get_by_name(optarg);
+       if (svc < 0) {
+               fprintf(stderr, "Invalid service %s\n", optarg);
+               exit(1);
+       }
+       qmi_service_release_client_id(qmi, svc);
+}
+
+static void handle_exit_signal(int signal)
+{
+       cancel_all_requests = true;
+       uloop_end();
+}
+
 int main(int argc, char **argv)
 {
        static struct qmi_dev dev;
        int ch;
 
+       uloop_init();
+       signal(SIGINT, handle_exit_signal);
+       signal(SIGTERM, handle_exit_signal);
+
        while ((ch = getopt_long(argc, argv, "d:k:", uqmi_getopt, NULL)) != -1) {
                int cmd_opt = CMD_OPT(ch);
 
@@ -69,6 +93,9 @@ int main(int argc, char **argv)
                }
 
                switch(ch) {
+               case 'r':
+                       release_client_id(&dev, optarg);
+                       break;
                case 'k':
                        keep_client_id(&dev, optarg);
                        break;