Lines Matching +full:watchdog +full:- +full:timers
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008-2009 Fredrik Lindberg
103 #define FLG_WATCHDOG 0x20 /* Watchdog enabled */
104 #define FLG_WDEXP 0x40 /* Watchdog expired */
111 time_t watchdog; member
176 struct timers { struct
191 static struct timers timers; variable
198 tmr_run(struct timers *tmrs) in tmr_run()
202 te = TAILQ_FIRST(&tmrs->head); in tmr_run()
206 te->timeout -= tmrs->res; in tmr_run()
207 while (te->timeout <= 0) { in tmr_run()
209 TAILQ_REMOVE(&tmrs->head, te, next); in tmr_run()
210 te->func(te->id, te->arg); in tmr_run()
220 tmr_add(struct timers *tmrs, int id, int timeout, tmr_cb func, void *arg) in tmr_add()
227 te->timeout = timeout; in tmr_add()
228 te->func = func; in tmr_add()
229 te->arg = arg; in tmr_add()
230 te->id = id; in tmr_add()
232 te2 = TAILQ_FIRST(&tmrs->head); in tmr_add()
234 if (TAILQ_EMPTY(&tmrs->head)) { in tmr_add()
235 TAILQ_INSERT_HEAD(&tmrs->head, te, next); in tmr_add()
236 } else if (te->timeout < te2->timeout) { in tmr_add()
237 te2->timeout -= te->timeout; in tmr_add()
238 TAILQ_INSERT_HEAD(&tmrs->head, te, next); in tmr_add()
240 while (te->timeout >= te2->timeout) { in tmr_add()
241 te->timeout -= te2->timeout; in tmr_add()
243 if (te3 == NULL || te3->timeout > te->timeout) in tmr_add()
247 TAILQ_INSERT_AFTER(&tmrs->head, te2, te, next); in tmr_add()
251 #define watchdog_enable(ctx) (ctx)->flags |= FLG_WATCHDOG
252 #define watchdog_disable(ctx) (ctx)->flags &= ~FLG_WATCHDOG
260 ctx->watchdog = tp.tv_sec + timeout; in watchdog_reset()
270 at_cmd_async(ctx->fd, "AT+CREG?\r\n"); in tmr_creg()
279 at_cmd_async(ctx->fd, "AT+CGREG?\r\n"); in tmr_cgreg()
288 at_cmd_async(ctx->fd, "AT+CSQ\r\n"); in tmr_status()
299 tmr_add(&timers, 1, 5, tmr_watchdog, ctx); in tmr_watchdog()
301 if (!(ctx->flags & FLG_WATCHDOG)) in tmr_watchdog()
306 if (tp.tv_sec >= ctx->watchdog) { in tmr_watchdog()
308 fprintf(stderr, "Watchdog expired\n"); in tmr_watchdog()
310 ctx->flags |= FLG_WDEXP; in tmr_watchdog()
328 tmr_run(&timers); in sig_handle()
360 #define if_ifdown(ifnam) if_setflags(ifnam, -IFF_UP)
374 return (-1); in if_setflags()
382 oflags &= ~(-flags); in if_setflags()
405 memcpy(&ifra.ifra_addr, sa, sa->sa_len); in ifaddr_add()
406 memcpy(&ifra.ifra_mask, mask, mask->sa_len); in ifaddr_add()
410 return (-1); in ifaddr_add()
428 memcpy(&ifr.ifr_addr, sa, sa->sa_len); in ifaddr_del()
432 return (-1); in ifaddr_del()
452 if (ctx->ns != NULL) { in set_nameservers()
453 for (i = 0; ctx->ns[i] != NULL; i++) { in set_nameservers()
454 free(ctx->ns[i]); in set_nameservers()
456 free(ctx->ns); in set_nameservers()
457 ctx->ns = NULL; in set_nameservers()
462 return (-1); in set_nameservers()
466 if (ctx->resolv != NULL) { in set_nameservers()
469 write(fd, ctx->resolv, ctx->resolv_sz); in set_nameservers()
470 free(ctx->resolv); in set_nameservers()
471 ctx->resolv = NULL; in set_nameservers()
472 ctx->resolv_sz = 0; in set_nameservers()
479 ctx->ns = malloc(sizeof(char *) * (ns + 1)); in set_nameservers()
480 if (ctx->ns == NULL) { in set_nameservers()
482 return (-1); in set_nameservers()
488 ctx->ns[i] = strdup(p); in set_nameservers()
490 ctx->ns[i] = NULL; in set_nameservers()
494 if (ctx->resolv == NULL) { in set_nameservers()
497 ctx->resolv_sz = sb.st_size; in set_nameservers()
498 ctx->resolv = malloc(sb.st_size); in set_nameservers()
499 if (ctx->resolv != NULL) { in set_nameservers()
500 n = read(fd, ctx->resolv, sb.st_size); in set_nameservers()
502 free(ctx->resolv); in set_nameservers()
503 ctx->resolv = NULL; in set_nameservers()
518 if (ctx->resolv != NULL) { in set_nameservers()
519 p = ctx->resolv; in set_nameservers()
520 while ((i = readline_buf(p, ctx->resolv + ctx->resolv_sz, buf, in set_nameservers()
529 for (i = 0; ctx->ns[i] != NULL; i++) { in set_nameservers()
530 fprintf(fp, "nameserver %s\n", ctx->ns[i]); in set_nameservers()
536 /* Read a \n-terminated line from buffer */
546 if (pos >= (bufsz - 1)) in readline_buf()
555 /* Read a \n-terminated line from file */
567 if (pos >= (bufsz - 1)) in readline()
599 n = write(ctx->fd, cmd, l); in at_cmd()
601 return (-1); in at_cmd()
616 FD_SET(ctx->fd, &set); in at_cmd()
617 error = select(ctx->fd + 1, &set, NULL, NULL, NULL); in at_cmd()
618 if (ctx->flags & FLG_WDEXP) { in at_cmd()
620 return (-2); in at_cmd()
626 retval = -2; in at_cmd()
630 n = readline(ctx->fd, buf, sizeof(buf)); in at_cmd()
632 retval = -2; in at_cmd()
658 retval = -1; in at_cmd()
692 int i = ra->val[1].int32; in saveresp()
698 buf = realloc(ra->val[0].ptr, sizeof(char *) * (i + 1)); in saveresp()
704 ra->val[0].ptr = buf; in saveresp()
705 ra->val[1].int32 = i + 1; in saveresp()
714 buf = ra->val[0].ptr; in freeresp()
715 for (i = 0; i < ra->val[1].int32; i++) { in freeresp()
734 if (ctx->con_net_stat != 1 && ctx->con_net_stat != 5) { in at_async_creg()
735 tmr_add(&timers, 1, 1, tmr_creg, ctx); in at_async_creg()
738 tmr_add(&timers, 1, 30, tmr_creg, ctx); in at_async_creg()
741 if (ctx->con_net_stat == reg) in at_async_creg()
744 ctx->con_net_stat = reg; in at_async_creg()
745 at_cmd_async(ctx->fd, "AT+COPS?\r\n"); in at_async_creg()
761 if (ctx->con_net_stat != 1 && ctx->con_net_stat != 5) { in at_async_cgreg()
762 tmr_add(&timers, 1, 1, tmr_cgreg, ctx); in at_async_cgreg()
765 tmr_add(&timers, 1, 30, tmr_cgreg, ctx); in at_async_cgreg()
768 if (ctx->con_net_stat == reg) in at_async_cgreg()
771 ctx->con_net_stat = reg; in at_async_cgreg()
772 at_cmd_async(ctx->fd, "AT+COPS?\r\n"); in at_async_cgreg()
788 if (ctx->con_oper != NULL) { in at_async_cops()
789 if (ctx->con_net_type == at && in at_async_cops()
790 strcasecmp(opr, ctx->con_oper) == 0) in at_async_cops()
792 free(ctx->con_oper); in at_async_cops()
795 ctx->con_oper = strdup(opr); in at_async_cops()
796 ctx->con_net_type = at; in at_async_cops()
798 if (ctx->con_net_stat == 1 || ctx->con_net_stat == 5) { in at_async_cops()
800 network_reg_status[ctx->con_net_stat], in at_async_cops()
801 ctx->con_oper, network_access_type[ctx->con_net_type]); in at_async_cops()
802 if (ctx->con_status != 1) { in at_async_cops()
803 at_cmd_async(ctx->fd, "AT_OWANCALL=%d,1,1\r\n", in at_async_cops()
804 ctx->pdp_ctx); in at_async_cops()
809 network_reg_status[ctx->con_net_stat], in at_async_cops()
810 network_access_type[ctx->con_net_type]); in at_async_cops()
818 * 0 = -113 dBm or less
819 * 1 = -111 dBm
820 * 2...30 = -109...-53 dBm
821 * 31 = -51 dBm or greater
823 * So, dbm = (rssi * 2) - 113
835 ctx->dbm = 0; in at_async_csq()
837 ctx->dbm = (rssi * 2) - 113; in at_async_csq()
838 tmr_add(&timers, 1, 15, tmr_status, ctx); in at_async_csq()
841 ctx->flags |= FLG_NEWDATA; in at_async_csq()
854 if (i == ctx->con_status) in at_async_owancall()
857 at_cmd_async(ctx->fd, "AT_OWANDATA=%d\r\n", ctx->pdp_ctx); in at_async_owancall()
859 ctx->con_status = i; in at_async_owancall()
860 if (ctx->con_status == 1) { in at_async_owancall()
862 ctx->con_oper, ctx->con_apn, in at_async_owancall()
863 network_access_type[ctx->con_net_type]); in at_async_owancall()
867 ctx->con_oper, ctx->con_apn); in at_async_owancall()
899 if (ctx->flags & IPASSIGNED) { in at_async_owandata()
900 memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr, in at_async_owandata()
902 ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin); in at_async_owandata()
904 inet_pton(AF_INET, ip, &ctx->ip.s_addr); in at_async_owandata()
905 memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr, in at_async_owandata()
908 error = ifaddr_add(ctx->ifnam, (struct sockaddr *)&sin, in at_async_owandata()
911 logger(LOG_ERR, "failed to set ip-address"); in at_async_owandata()
915 if_ifup(ctx->ifnam); in at_async_owandata()
917 ctx->flags |= IPASSIGNED; in at_async_owandata()
919 set_nameservers(ctx, ctx->resolv_path, 0); in at_async_owandata()
920 error = set_nameservers(ctx, ctx->resolv_path, 2, ns1, ns2); in at_async_owandata()
931 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { in at_async_owandata()
932 if (ifa->ifa_addr->sa_family != AF_LINK) in at_async_owandata()
934 if (strcmp(ctx->ifnam, ifa->ifa_name) == 0) { in at_async_owandata()
935 memcpy(&sdl, (struct sockaddr_dl *)ifa->ifa_addr, in at_async_owandata()
982 if ((ctx->flags & FLG_DELAYED) && !(ctx->flags & FLG_NODAEMON)) in at_async_owandata()
996 n = readline(ctx->fd, buf, sizeof(buf)); in at_async()
998 return (n <= 0 ? -1 : 0); in at_async()
1055 if (error == 0 && strcasecmp(data, ctx->ifnam) == 0) in get_tty()
1145 ctx->fd = open(tty, O_RDWR); in do_connect()
1146 if (ctx->fd < 0) { in do_connect()
1150 return (-1); in do_connect()
1153 tcgetattr(ctx->fd, &t); in do_connect()
1158 tcsetattr(ctx->fd, TCSAFLUSH, &t); in do_connect()
1161 if (error == -2) { in do_connect()
1163 return (-1); in do_connect()
1184 if (ctx->pin == NULL) { in do_connect()
1189 ctx->pin); in do_connect()
1214 if (cid == ctx->pdp_ctx) { in do_connect()
1215 ctx->con_apn = strdup(apn); in do_connect()
1216 if (ctx->pdp_apn != NULL) { in do_connect()
1217 if (strcmp(apn, ctx->pdp_apn) == 0) in do_connect()
1229 if (ctx->pdp_apn == NULL) in do_connect()
1233 "AT+CGDCONT=%d,,\"%s\"\r\n", ctx->pdp_ctx, ctx->pdp_apn); in do_connect()
1237 ctx->con_apn = strdup(ctx->pdp_apn); in do_connect()
1240 if (ctx->pdp_user != NULL || ctx->pdp_pwd != NULL) { in do_connect()
1242 "AT$QCPDPP=%d,1,\"%s\",\"%s\"\r\n", ctx->pdp_ctx, in do_connect()
1243 (ctx->pdp_user != NULL) ? ctx->pdp_user : "", in do_connect()
1244 (ctx->pdp_pwd != NULL) ? ctx->pdp_pwd : ""); in do_connect()
1248 ctx->pdp_ctx); in do_connect()
1250 return (-1); in do_connect()
1252 at_cmd_async(ctx->fd, "AT+CGREG?\r\n"); in do_connect()
1253 at_cmd_async(ctx->fd, "AT+CREG?\r\n"); in do_connect()
1255 tmr_add(&timers, 1, 5, tmr_status, ctx); in do_connect()
1266 ctx->pdp_ctx); in do_disconnect()
1267 close(ctx->fd); in do_disconnect()
1269 /* Remove ip-address from interface */ in do_disconnect()
1270 if (ctx->flags & IPASSIGNED) { in do_disconnect()
1273 memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr, in do_disconnect()
1275 ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin); in do_disconnect()
1277 if_ifdown(ctx->ifnam); in do_disconnect()
1278 ctx->flags &= ~IPASSIGNED; in do_disconnect()
1282 set_nameservers(ctx, ctx->resolv_path, 0); in do_disconnect()
1291 snprintf(ctx->pidfile, 127, PIDFILE, ctx->ifnam); in daemonize()
1293 pfh = pidfile_open(ctx->pidfile, 0600, &opid); in daemonize()
1295 warn("Cannot create pidfile %s", ctx->pidfile); in daemonize()
1299 if (daemon(0, 0) == -1) { in daemonize()
1306 ctx->pfh = pfh; in daemonize()
1307 ctx->flags |= FLG_DAEMON; in daemonize()
1309 snprintf(syslog_title, 63, "%s:%s", getprogname(), ctx->ifnam); in daemonize()
1345 printf("usage %s [-b] [-n] [-a apn] [-c cid] [-p pin] [-u username] " in usage()
1346 "[-k password] [-r resolvpath] [-f tty] interface\n", exec); in usage()
1347 printf("usage %s -d interface\n", exec); in usage()
1366 TAILQ_INIT(&timers.head); in main()
1367 timers.res = 1; in main()
1377 ctx.con_net_type = -1; in main()
1387 while ((ch = getopt(argc, argv, "?ha:p:c:u:k:r:f:dbn")) != -1) { in main()
1390 ctx.pdp_apn = argv[optind - 1]; in main()
1393 ctx.pdp_ctx = strtol(argv[optind - 1], NULL, 10); in main()
1400 ctx.pin = argv[optind - 1]; in main()
1403 ctx.pdp_user = argv[optind - 1]; in main()
1406 ctx.pdp_pwd = argv[optind - 1]; in main()
1409 ctx.resolv_path = argv[optind - 1]; in main()
1421 tty = argv[optind - 1]; in main()
1431 argc -= optind; in main()
1437 ifnam = argv[argc - 1]; in main()
1463 tmr_add(&timers, 1, 5, &tmr_watchdog, &ctx); in main()