Lines Matching +full:te +full:- +full:source
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008-2009 Fredrik Lindberg
7 * Redistribution and use in source and binary forms, with or without
10 * 1. Redistributions of source code must retain the above copyright
200 struct timer_entry *te, *te2;
202 te = TAILQ_FIRST(&tmrs->head);
203 if (te == NULL)
206 te->timeout -= tmrs->res;
207 while (te->timeout <= 0) {
208 te2 = TAILQ_NEXT(te, next);
209 TAILQ_REMOVE(&tmrs->head, te, next);
210 te->func(te->id, te->arg);
211 free(te);
212 te = te2;
213 if (te == NULL)
222 struct timer_entry *te, *te2, *te3;
224 te = malloc(sizeof(struct timer_entry));
225 memset(te, 0, sizeof(struct timer_entry));
227 te->timeout = timeout;
228 te->func = func;
229 te->arg = arg;
230 te->id = id;
232 te2 = TAILQ_FIRST(&tmrs->head);
234 if (TAILQ_EMPTY(&tmrs->head)) {
235 TAILQ_INSERT_HEAD(&tmrs->head, te, next);
236 } else if (te->timeout < te2->timeout) {
237 te2->timeout -= te->timeout;
238 TAILQ_INSERT_HEAD(&tmrs->head, te, next);
240 while (te->timeout >= te2->timeout) {
241 te->timeout -= te2->timeout;
243 if (te3 == NULL || te3->timeout > te->timeout)
247 TAILQ_INSERT_AFTER(&tmrs->head, te2, te, next);
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;
270 at_cmd_async(ctx->fd, "AT+CREG?\r\n");
279 at_cmd_async(ctx->fd, "AT+CGREG?\r\n");
288 at_cmd_async(ctx->fd, "AT+CSQ\r\n");
301 if (!(ctx->flags & FLG_WATCHDOG))
306 if (tp.tv_sec >= ctx->watchdog) {
310 ctx->flags |= FLG_WDEXP;
360 #define if_ifdown(ifnam) if_setflags(ifnam, -IFF_UP)
374 return (-1);
382 oflags &= ~(-flags);
405 memcpy(&ifra.ifra_addr, sa, sa->sa_len);
406 memcpy(&ifra.ifra_mask, mask, mask->sa_len);
410 return (-1);
428 memcpy(&ifr.ifr_addr, sa, sa->sa_len);
432 return (-1);
452 if (ctx->ns != NULL) {
453 for (i = 0; ctx->ns[i] != NULL; i++) {
454 free(ctx->ns[i]);
456 free(ctx->ns);
457 ctx->ns = NULL;
462 return (-1);
466 if (ctx->resolv != NULL) {
469 write(fd, ctx->resolv, ctx->resolv_sz);
470 free(ctx->resolv);
471 ctx->resolv = NULL;
472 ctx->resolv_sz = 0;
479 ctx->ns = malloc(sizeof(char *) * (ns + 1));
480 if (ctx->ns == NULL) {
482 return (-1);
488 ctx->ns[i] = strdup(p);
490 ctx->ns[i] = NULL;
494 if (ctx->resolv == NULL) {
497 ctx->resolv_sz = sb.st_size;
498 ctx->resolv = malloc(sb.st_size);
499 if (ctx->resolv != NULL) {
500 n = read(fd, ctx->resolv, sb.st_size);
502 free(ctx->resolv);
503 ctx->resolv = NULL;
518 if (ctx->resolv != NULL) {
519 p = ctx->resolv;
520 while ((i = readline_buf(p, ctx->resolv + ctx->resolv_sz, buf,
529 for (i = 0; ctx->ns[i] != NULL; i++) {
530 fprintf(fp, "nameserver %s\n", ctx->ns[i]);
536 /* Read a \n-terminated line from buffer */
546 if (pos >= (bufsz - 1))
555 /* Read a \n-terminated line from file */
567 if (pos >= (bufsz - 1))
599 n = write(ctx->fd, cmd, l);
601 return (-1);
616 FD_SET(ctx->fd, &set);
617 error = select(ctx->fd + 1, &set, NULL, NULL, NULL);
618 if (ctx->flags & FLG_WDEXP) {
620 return (-2);
626 retval = -2;
630 n = readline(ctx->fd, buf, sizeof(buf));
632 retval = -2;
658 retval = -1;
692 int i = ra->val[1].int32;
698 buf = realloc(ra->val[0].ptr, sizeof(char *) * (i + 1));
704 ra->val[0].ptr = buf;
705 ra->val[1].int32 = i + 1;
714 buf = ra->val[0].ptr;
715 for (i = 0; i < ra->val[1].int32; i++) {
734 if (ctx->con_net_stat != 1 && ctx->con_net_stat != 5) {
741 if (ctx->con_net_stat == reg)
744 ctx->con_net_stat = reg;
745 at_cmd_async(ctx->fd, "AT+COPS?\r\n");
761 if (ctx->con_net_stat != 1 && ctx->con_net_stat != 5) {
768 if (ctx->con_net_stat == reg)
771 ctx->con_net_stat = reg;
772 at_cmd_async(ctx->fd, "AT+COPS?\r\n");
788 if (ctx->con_oper != NULL) {
789 if (ctx->con_net_type == at &&
790 strcasecmp(opr, ctx->con_oper) == 0)
792 free(ctx->con_oper);
795 ctx->con_oper = strdup(opr);
796 ctx->con_net_type = at;
798 if (ctx->con_net_stat == 1 || ctx->con_net_stat == 5) {
800 network_reg_status[ctx->con_net_stat],
801 ctx->con_oper, network_access_type[ctx->con_net_type]);
802 if (ctx->con_status != 1) {
803 at_cmd_async(ctx->fd, "AT_OWANCALL=%d,1,1\r\n",
804 ctx->pdp_ctx);
809 network_reg_status[ctx->con_net_stat],
810 network_access_type[ctx->con_net_type]);
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;
837 ctx->dbm = (rssi * 2) - 113;
841 ctx->flags |= FLG_NEWDATA;
854 if (i == ctx->con_status)
857 at_cmd_async(ctx->fd, "AT_OWANDATA=%d\r\n", ctx->pdp_ctx);
859 ctx->con_status = i;
860 if (ctx->con_status == 1) {
862 ctx->con_oper, ctx->con_apn,
863 network_access_type[ctx->con_net_type]);
867 ctx->con_oper, ctx->con_apn);
899 if (ctx->flags & IPASSIGNED) {
900 memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr,
902 ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin);
904 inet_pton(AF_INET, ip, &ctx->ip.s_addr);
905 memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr,
908 error = ifaddr_add(ctx->ifnam, (struct sockaddr *)&sin,
911 logger(LOG_ERR, "failed to set ip-address");
915 if_ifup(ctx->ifnam);
917 ctx->flags |= IPASSIGNED;
919 set_nameservers(ctx, ctx->resolv_path, 0);
920 error = set_nameservers(ctx, ctx->resolv_path, 2, ns1, ns2);
931 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
932 if (ifa->ifa_addr->sa_family != AF_LINK)
934 if (strcmp(ctx->ifnam, ifa->ifa_name) == 0) {
935 memcpy(&sdl, (struct sockaddr_dl *)ifa->ifa_addr,
982 if ((ctx->flags & FLG_DELAYED) && !(ctx->flags & FLG_NODAEMON))
996 n = readline(ctx->fd, buf, sizeof(buf));
998 return (n <= 0 ? -1 : 0);
1055 if (error == 0 && strcasecmp(data, ctx->ifnam) == 0)
1145 ctx->fd = open(tty, O_RDWR);
1146 if (ctx->fd < 0) {
1150 return (-1);
1153 tcgetattr(ctx->fd, &t);
1158 tcsetattr(ctx->fd, TCSAFLUSH, &t);
1161 if (error == -2) {
1163 return (-1);
1184 if (ctx->pin == NULL) {
1189 ctx->pin);
1214 if (cid == ctx->pdp_ctx) {
1215 ctx->con_apn = strdup(apn);
1216 if (ctx->pdp_apn != NULL) {
1217 if (strcmp(apn, ctx->pdp_apn) == 0)
1229 if (ctx->pdp_apn == NULL)
1233 "AT+CGDCONT=%d,,\"%s\"\r\n", ctx->pdp_ctx, ctx->pdp_apn);
1237 ctx->con_apn = strdup(ctx->pdp_apn);
1240 if (ctx->pdp_user != NULL || ctx->pdp_pwd != NULL) {
1242 "AT$QCPDPP=%d,1,\"%s\",\"%s\"\r\n", ctx->pdp_ctx,
1243 (ctx->pdp_user != NULL) ? ctx->pdp_user : "",
1244 (ctx->pdp_pwd != NULL) ? ctx->pdp_pwd : "");
1248 ctx->pdp_ctx);
1250 return (-1);
1252 at_cmd_async(ctx->fd, "AT+CGREG?\r\n");
1253 at_cmd_async(ctx->fd, "AT+CREG?\r\n");
1266 ctx->pdp_ctx);
1267 close(ctx->fd);
1269 /* Remove ip-address from interface */
1270 if (ctx->flags & IPASSIGNED) {
1273 memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr,
1275 ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin);
1277 if_ifdown(ctx->ifnam);
1278 ctx->flags &= ~IPASSIGNED;
1282 set_nameservers(ctx, ctx->resolv_path, 0);
1291 snprintf(ctx->pidfile, 127, PIDFILE, ctx->ifnam);
1293 pfh = pidfile_open(ctx->pidfile, 0600, &opid);
1295 warn("Cannot create pidfile %s", ctx->pidfile);
1299 if (daemon(0, 0) == -1) {
1306 ctx->pfh = pfh;
1307 ctx->flags |= FLG_DAEMON;
1309 snprintf(syslog_title, 63, "%s:%s", getprogname(), ctx->ifnam);
1345 printf("usage %s [-b] [-n] [-a apn] [-c cid] [-p pin] [-u username] "
1346 "[-k password] [-r resolvpath] [-f tty] interface\n", exec);
1347 printf("usage %s -d interface\n", exec);
1377 ctx.con_net_type = -1;
1387 while ((ch = getopt(argc, argv, "?ha:p:c:u:k:r:f:dbn")) != -1) {
1390 ctx.pdp_apn = argv[optind - 1];
1393 ctx.pdp_ctx = strtol(argv[optind - 1], NULL, 10);
1400 ctx.pin = argv[optind - 1];
1403 ctx.pdp_user = argv[optind - 1];
1406 ctx.pdp_pwd = argv[optind - 1];
1409 ctx.resolv_path = argv[optind - 1];
1421 tty = argv[optind - 1];
1431 argc -= optind;
1437 ifnam = argv[argc - 1];