Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 | /*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <net/if.h>
#include <errno.h>
#include <glib.h>
#include "message.h"
#include "common.h"
#include "modem.h"
#include "socket.h"
#define ISIDBG(m, fmt, ...) \
if ((m) != NULL && (m)->debug != NULL) \
m->debug("gisi: "fmt, ##__VA_ARGS__);
struct _GIsiServiceMux {
GIsiModem *modem;
GSList *pending;
GIsiVersion version;
uint8_t resource;
uint8_t last_utid;
uint16_t object;
unsigned subscriptions;
unsigned registrations;
gboolean reachable;
gboolean version_pending;
};
typedef struct _GIsiServiceMux GIsiServiceMux;
struct _GIsiModem {
unsigned index;
uint8_t device;
GHashTable *services;
gboolean subs_source;
int req_fd;
int ind_fd;
guint req_watch;
guint ind_watch;
GIsiDebugFunc debug;
GIsiNotifyFunc trace;
void *opaque;
unsigned long flags;
};
struct _GIsiPending {
enum GIsiMessageType type;
GIsiServiceMux *service;
gpointer owner;
guint timeout;
GIsiNotifyFunc notify;
GDestroyNotify destroy;
void *data;
uint8_t utid;
uint8_t msgid;
};
static GIsiServiceMux *service_get(GIsiModem *modem, uint8_t resource)
{
GIsiServiceMux *mux;
int key = resource;
mux = g_hash_table_lookup(modem->services, GINT_TO_POINTER(key));
if (mux != NULL)
return mux;
mux = g_try_new0(GIsiServiceMux, 1);
if (mux == NULL)
return NULL;
g_hash_table_insert(modem->services, GINT_TO_POINTER(key), mux);
mux->modem = modem;
mux->resource = resource;
mux->version.major = -1;
mux->version.minor = -1;
mux->reachable = FALSE;
mux->version_pending = FALSE;
return mux;
}
static gint utid_equal(gconstpointer a, gconstpointer b)
{
const GIsiPending *pa = a;
const GIsiPending *pb = b;
return pa->utid - pb->utid;
}
static const char *pend_type_to_str(enum GIsiMessageType type)
{
switch (type) {
case GISI_MESSAGE_TYPE_REQ:
return "REQ";
case GISI_MESSAGE_TYPE_IND:
return "IND";
case GISI_MESSAGE_TYPE_NTF:
return "NTF";
case GISI_MESSAGE_TYPE_RESP:
return "RESP";
case GISI_MESSAGE_TYPE_COMMON:
return "COMMON";
}
return "UNKNOWN";
}
static void pending_dispatch(GIsiPending *pend, GIsiMessage *msg)
{
GIsiModem *modem;
if (pend->notify == NULL)
return;
modem = pend->service->modem;
ISIDBG(modem, "%s %s to %p [res=0x%02X, id=0x%02X, utid=0x%02X]",
g_isi_msg_strerror(msg), pend_type_to_str(pend->type), pend,
g_isi_msg_resource(msg), g_isi_msg_id(msg),
g_isi_msg_utid(msg));
pend->notify(msg, pend->data);
}
static void pending_remove_and_dispatch(GIsiPending *op, GIsiMessage *msg)
{
GIsiModem *modem;
op->service->pending = g_slist_remove(op->service->pending, op);
if (op->notify == NULL || msg == NULL)
goto destroy;
modem = op->service->modem;
ISIDBG(modem, "%s %s to %p [res=0x%02X, id=0x%02X, utid=0x%02X]",
g_isi_msg_error(msg) ? g_isi_msg_strerror(msg) : "normal",
pend_type_to_str(op->type), op,
g_isi_msg_resource(msg), g_isi_msg_id(msg),
g_isi_msg_utid(msg));
op->notify(msg, op->data);
destroy:
if (op->timeout > 0)
g_source_remove(op->timeout);
if (op->destroy != NULL)
op->destroy(op->data);
g_free(op);
}
static void service_dispatch(GIsiServiceMux *mux, GIsiMessage *msg,
gboolean is_indication)
{
uint8_t msgid = g_isi_msg_id(msg);
uint8_t utid = g_isi_msg_utid(msg);
GSList *l = mux->pending;
while (l != NULL) {
GSList *next = l->next;
GIsiPending *pend = l->data;
/*
* REQs, NTFs and INDs are dispatched on message ID. While
* INDs have the unique transaction ID set to zero, NTFs
* typically mirror the UTID of the request that set up the
* session, and REQs can naturally have any transaction ID.
*
* RESPs are dispatched on unique transaction ID, explicitly
* ignoring the msgid. A RESP also completes a transaction,
* so it needs to be removed after being notified of.
*
* Version query responses are dispatched in a similar fashion
* as RESPs, but based on the pending type and the message ID.
* Some of these may be synthesized, but nevertheless need to
* be removed.
*/
if (pend->type < GISI_MESSAGE_TYPE_RESP
&& pend->msgid == msgid) {
pending_dispatch(pend, msg);
} else if (pend->type == GISI_MESSAGE_TYPE_RESP &&
!is_indication && pend->utid == utid) {
pending_remove_and_dispatch(pend, msg);
break;
} else if (pend->type == GISI_MESSAGE_TYPE_COMMON &&
msgid == COMMON_MESSAGE &&
pend->msgid == COMM_ISI_VERSION_GET_REQ) {
pending_remove_and_dispatch(pend, msg);
}
l = next;
}
}
static void common_message_decode(GIsiServiceMux *mux, GIsiMessage *msg)
{
uint8_t code;
uint8_t major;
uint8_t minor;
if (!g_isi_msg_data_get_byte(msg, 0, &code))
return;
switch (code) {
case COMM_ISA_ENTITY_NOT_REACHABLE_RESP:
mux->reachable = FALSE;
msg->error = ENOENT;
break;
case COMM_ISI_VERSION_GET_RESP:
if (g_isi_msg_data_get_byte(msg, 1, &major) &&
g_isi_msg_data_get_byte(msg, 2, &minor)) {
mux->version.major = major;
mux->version.minor = minor;
}
/* fall through */
default:
/*
* PN_SIM doesn't support ISI version, but sends a
* garbage message as a response. Work around this
* modem wart.
*/
mux->object = g_isi_msg_object(msg);
mux->version_pending = FALSE;
mux->reachable = TRUE;
break;
}
msg->version = &mux->version;
}
static void firewall_notify_handle(GIsiModem *modem, GIsiMessage *msg)
{
uint8_t id;
if (!g_isi_msg_data_get_byte(msg, 0, &id))
return;
ISIDBG(modem, "firewall blocked message 0x%02X", id);
}
static gboolean isi_callback(GIOChannel *channel, GIOCondition cond,
gpointer data)
{
GIsiModem *modem = data;
int len;
int fd;
if (cond & (G_IO_NVAL|G_IO_HUP)) {
ISIDBG(modem, "Unexpected event on PhoNet channel %p", channel);
return FALSE;
}
fd = g_io_channel_unix_get_fd(channel);
len = g_isi_phonet_peek_length(channel);
if (len > 0) {
struct sockaddr_pn addr;
uint32_t buf[(len + 3) / 4];
GIsiServiceMux *mux;
GIsiMessage msg;
unsigned key;
len = g_isi_phonet_read(channel, buf, len, &addr);
if (len < 2)
return TRUE;
msg.addr = &addr;
msg.error = 0;
msg.data = buf;
msg.len = len;
if (modem->trace != NULL)
modem->trace(&msg, NULL);
key = addr.spn_resource;
mux = g_hash_table_lookup(modem->services,
GINT_TO_POINTER(key));
if (mux == NULL) {
/*
* Unfortunately, the FW report has the wrong
* resource ID in the N900 modem.
*/
if (key == PN_FIREWALL)
firewall_notify_handle(modem, &msg);
return TRUE;
}
msg.version = &mux->version;
if (g_isi_msg_id(&msg) == COMMON_MESSAGE)
common_message_decode(mux, &msg);
service_dispatch(mux, &msg, fd == modem->ind_fd);
}
return TRUE;
}
static gboolean modem_subs_update(gpointer data)
{
GHashTableIter iter;
gpointer keyptr, value;
GIsiModem *modem = data;
gboolean legacy = modem->flags & GISI_MODEM_FLAG_USE_LEGACY_SUBSCRIBE;
struct sockaddr_pn commgr = {
.spn_family = AF_PHONET,
.spn_resource = PN_COMMGR,
.spn_dev = modem->device,
};
uint8_t msg[4 + 1024] = {
0, /* UTID */
legacy ? PNS_SUBSCRIBED_RESOURCES_IND :
PNS_SUBSCRIBED_RESOURCES_EXTEND_IND,
0, /* Count */
0, /* Filler */
};
uint8_t count = 0;
size_t len;
modem->subs_source = 0;
g_hash_table_iter_init(&iter, modem->services);
while (g_hash_table_iter_next(&iter, &keyptr, &value)) {
GIsiServiceMux *mux = value;
if (mux->subscriptions == 0)
continue;
if (legacy)
msg[3 + count] = mux->resource;
else
/* Resource field is 32bit and Little-endian */
msg[4 + count * 4 + 3] = mux->resource;
count++;
}
len = legacy ? 3 + count : 4 + count * 4;
msg[2] = count;
sendto(modem->ind_fd, msg, len, MSG_NOSIGNAL, (void *) &commgr,
sizeof(commgr));
return FALSE;
}
static void modem_subs_update_when_idle(GIsiModem *modem)
{
if (modem->subs_source > 0)
return;
modem->subs_source = g_idle_add(modem_subs_update, modem);
}
static void service_name_register(GIsiServiceMux *mux)
{
struct sockaddr_pn namesrv = {
.spn_family = AF_PHONET,
.spn_resource = PN_NAMESERVICE,
.spn_dev = mux->modem->device,
};
uint8_t msg[] = {
0, PNS_NAME_ADD_REQ, 0, 0,
0, 0, 0, mux->resource, /* 32-bit Big-Endian name */
0, 0, /* device/object */
0, 0, /* filler */
};
uint16_t object = 0;
if (ioctl(mux->modem->req_fd, SIOCPNGETOBJECT, &object) < 0) {
ISIDBG(mux->modem, "ioctl(SIOCPNGETOBJECT): %s",
strerror(errno));
return;
}
/* Fill in the object ID */
msg[8] = object >> 8;
msg[9] = object & 0xFF;
sendto(mux->modem->req_fd, msg, sizeof(msg), MSG_NOSIGNAL,
(void *) &namesrv, sizeof(namesrv));
}
static void service_name_deregister(GIsiServiceMux *mux)
{
struct sockaddr_pn namesrv = {
.spn_family = AF_PHONET,
.spn_resource = PN_NAMESERVICE,
.spn_dev = mux->modem->device,
};
const uint8_t msg[] = {
0, PNS_NAME_REMOVE_REQ, 0, 0,
0, 0, 0, mux->resource,
};
sendto(mux->modem->req_fd, msg, sizeof(msg), MSG_NOSIGNAL,
(void *) &namesrv, sizeof(namesrv));
}
static void pending_destroy(gpointer value, gpointer user)
{
GIsiPending *op = value;
if (op == NULL)
return;
if (op->timeout > 0)
g_source_remove(op->timeout);
if (op->destroy != NULL)
op->destroy(op->data);
g_free(op);
}
static void service_finalize(gpointer value)
{
GIsiServiceMux *mux = value;
GIsiModem *modem = mux->modem;
if (mux->subscriptions > 0)
modem_subs_update_when_idle(modem);
if (mux->registrations > 0)
service_name_deregister(mux);
g_slist_foreach(mux->pending, pending_destroy, NULL);
g_slist_free(mux->pending);
g_free(mux);
}
GIsiModem *g_isi_modem_create(unsigned index)
{
GIsiModem *modem;
GIOChannel *inds;
GIOChannel *reqs;
if (index == 0) {
errno = ENODEV;
return NULL;
}
modem = g_try_new0(GIsiModem, 1);
if (modem == NULL) {
errno = ENOMEM;
return NULL;
}
inds = g_isi_phonet_new(index);
reqs = g_isi_phonet_new(index);
if (inds == NULL || reqs == NULL) {
g_free(modem);
return NULL;
}
modem->req_fd = g_io_channel_unix_get_fd(reqs);
modem->req_watch = g_io_add_watch(reqs,
G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
isi_callback, modem);
modem->ind_fd = g_io_channel_unix_get_fd(inds);
modem->ind_watch = g_io_add_watch(inds,
G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
isi_callback, modem);
g_io_channel_unref(reqs);
g_io_channel_unref(inds);
modem->index = index;
modem->services = g_hash_table_new_full(g_direct_hash, NULL,
NULL, service_finalize);
return modem;
}
GIsiModem *g_isi_modem_create_by_name(const char *name)
{
return g_isi_modem_create(if_nametoindex(name));
}
void *g_isi_modem_set_userdata(GIsiModem *modem, void *data)
{
void *old;
if (modem == NULL)
return NULL;
old = modem->opaque;
modem->opaque = data;
return old;
}
void *g_isi_modem_get_userdata(GIsiModem *modem)
{
if (modem == NULL)
return NULL;
return modem->opaque;
}
unsigned long g_isi_modem_flags(GIsiModem *modem)
{
if (modem == NULL)
return 0;
return modem->flags;
}
void g_isi_modem_set_flags(GIsiModem *modem, unsigned long flags)
{
if (modem == NULL)
return;
modem->flags = flags;
}
uint8_t g_isi_modem_device(GIsiModem *modem)
{
if (modem == NULL)
return 0;
return modem->device;
}
int g_isi_modem_set_device(GIsiModem *modem, uint8_t remote)
{
if (modem == NULL)
return -EINVAL;
if (remote != PN_DEV_HOST && remote != PN_DEV_MODEM)
return -EINVAL;
modem->device = remote;
return 0;
}
static uint8_t service_next_utid(GIsiServiceMux *mux)
{
if (mux->last_utid == 0x00 || mux->last_utid == 0xFF)
return 1;
return mux->last_utid + 1;
}
static void service_subs_incr(GIsiServiceMux *mux)
{
GIsiModem *modem = mux->modem;
mux->subscriptions++;
if (mux->subscriptions == 1)
modem_subs_update_when_idle(modem);
}
static void service_subs_decr(GIsiServiceMux *mux)
{
GIsiModem *modem = mux->modem;
if (mux->subscriptions == 0)
return;
mux->subscriptions--;
if (mux->subscriptions == 0)
modem_subs_update_when_idle(modem);
}
static void service_regs_incr(GIsiServiceMux *mux)
{
mux->registrations++;
if (mux->registrations == 1)
service_name_register(mux);
}
static void service_regs_decr(GIsiServiceMux *mux)
{
if (mux->registrations == 0)
return;
mux->registrations--;
if (mux->registrations == 0)
service_name_deregister(mux);
}
void g_isi_modem_destroy(GIsiModem *modem)
{
if (modem == NULL)
return;
g_hash_table_remove_all(modem->services);
if (modem->subs_source > 0) {
g_source_remove(modem->subs_source);
modem_subs_update(modem);
}
g_hash_table_unref(modem->services);
if (modem->ind_watch > 0)
g_source_remove(modem->ind_watch);
if (modem->req_watch > 0)
g_source_remove(modem->req_watch);
g_free(modem);
}
unsigned g_isi_modem_index(GIsiModem *modem)
{
return modem != NULL ? modem->index : 0;
}
GIsiPending *g_isi_request_send(GIsiModem *modem, uint8_t resource,
const void *__restrict buf, size_t len,
unsigned timeout, GIsiNotifyFunc notify,
void *data, GDestroyNotify destroy)
{
struct sockaddr_pn dst = {
.spn_family = AF_PHONET,
.spn_resource = resource,
.spn_dev = modem->device,
};
return g_isi_request_sendto(modem, &dst, buf, len, timeout, notify,
data, destroy);
};
GIsiPending *g_isi_request_vsend(GIsiModem *modem, uint8_t resource,
const struct iovec *__restrict iov,
size_t iovlen, unsigned timeout,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy)
{
struct sockaddr_pn dst = {
.spn_family = AF_PHONET,
.spn_resource = resource,
.spn_dev = modem->device,
};
return g_isi_request_vsendto(modem, &dst, iov, iovlen, timeout, notify,
data, destroy);
}
GIsiPending *g_isi_request_sendto(GIsiModem *modem, struct sockaddr_pn *dst,
const void *__restrict buf, size_t len,
unsigned timeout, GIsiNotifyFunc notify,
void *data, GDestroyNotify destroy)
{
const struct iovec iov = {
.iov_base = (void *)buf,
.iov_len = len,
};
return g_isi_request_vsendto(modem, dst, &iov, 1, timeout, notify, data,
destroy);
}
static void vtrace(struct sockaddr_pn *dst,
const struct iovec *__restrict iov, size_t iovlen,
size_t total_len, GIsiNotifyFunc trace)
{
uint8_t buffer[total_len];
uint8_t *ptr = buffer;
GIsiMessage msg = {
.addr = dst,
.data = (const void *)buffer,
.len = total_len,
};
size_t i;
for (i = 0; i < iovlen; i++) {
memcpy(ptr, iov[i].iov_base, iov[i].iov_len);
ptr += iov[i].iov_len;
}
trace(&msg, NULL);
}
static gboolean resp_timeout(gpointer data)
{
GIsiPending *op = data;
GIsiMessage msg = {
.error = ETIMEDOUT,
};
op->timeout = 0;
pending_remove_and_dispatch(op, &msg);
return FALSE;
}
GIsiPending *g_isi_request_vsendto(GIsiModem *modem, struct sockaddr_pn *dst,
const struct iovec *__restrict iov,
size_t iovlen, unsigned timeout,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy)
{
struct iovec _iov[1 + iovlen];
struct msghdr msg = {
.msg_name = (void *)dst,
.msg_namelen = sizeof(struct sockaddr_pn),
.msg_iov = _iov,
.msg_iovlen = 1 + iovlen,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0,
};
ssize_t ret;
size_t i, len;
GIsiServiceMux *mux;
GIsiPending *resp;
if (modem == NULL) {
errno = EINVAL;
return NULL;
}
mux = service_get(modem, dst->spn_resource);
if (mux == NULL) {
errno = ENOMEM;
return NULL;
}
resp = g_try_new0(GIsiPending, 1);
if (resp == NULL) {
errno = ENOMEM;
return NULL;
}
resp->type = GISI_MESSAGE_TYPE_RESP;
resp->utid = service_next_utid(mux);
resp->service = mux;
resp->notify = notify;
resp->destroy = destroy;
resp->data = data;
if (g_slist_find_custom(mux->pending, resp, utid_equal)) {
/*
* FIXME: perhaps retry with randomized access after
* initial miss. Although if the rate at which
* requests are sent is so high that the unique
* transaction ID wraps, it's likely there is
* something wrong and we might as well fail here.
*/
ISIDBG(modem, "ERROR: UTID wrapped, modem busy");
errno = EBUSY;
goto error;
}
_iov[0].iov_base = &resp->utid;
_iov[0].iov_len = 1;
for (i = 0, len = 1; i < iovlen; i++) {
_iov[1 + i] = iov[i];
len += iov[i].iov_len;
}
if (modem->trace != NULL)
vtrace(dst, _iov, 1 + iovlen, len, modem->trace);
ret = sendmsg(modem->req_fd, &msg, MSG_NOSIGNAL);
if (ret == -1)
goto error;
if (ret != (ssize_t)len) {
errno = EMSGSIZE;
goto error;
}
mux->pending = g_slist_prepend(mux->pending, resp);
if (timeout > 0)
resp->timeout = g_timeout_add_seconds(timeout, resp_timeout,
resp);
mux->last_utid = resp->utid;
return resp;
error:
g_free(resp);
return NULL;
}
uint8_t g_isi_request_utid(GIsiPending *resp)
{
return resp != NULL ? resp->utid : 0;
}
void g_isi_pending_remove(GIsiPending *op)
{
if (op == NULL)
return;
if (op->type == GISI_MESSAGE_TYPE_IND)
service_subs_decr(op->service);
if (op->type == GISI_MESSAGE_TYPE_REQ)
service_regs_decr(op->service);
if (op->type == GISI_MESSAGE_TYPE_RESP && op->notify != NULL) {
GIsiMessage msg = {
.error = ESHUTDOWN,
};
pending_remove_and_dispatch(op, &msg);
return;
}
op->service->pending = g_slist_remove(op->service->pending, op);
pending_destroy(op, NULL);
}
static void foreach_destroy(GIsiPending *op)
{
if (op->type == GISI_MESSAGE_TYPE_IND)
service_subs_decr(op->service);
if (op->type == GISI_MESSAGE_TYPE_REQ)
service_regs_decr(op->service);
if (op->type == GISI_MESSAGE_TYPE_RESP && op->notify != NULL) {
GIsiMessage msg = {
.error = ESHUTDOWN,
};
pending_dispatch(op, &msg);
}
pending_destroy(op, NULL);
}
void g_isi_pending_set_owner(GIsiPending *op, gpointer owner)
{
if (op == NULL)
return;
op->owner = owner;
}
void g_isi_remove_pending_by_owner(GIsiModem *modem, uint8_t resource,
gpointer owner)
{
GIsiServiceMux *mux;
GSList *l;
GSList *next;
GIsiPending *op;
GSList *owned = NULL;
mux = service_get(modem, resource);
if (mux == NULL)
return;
for (l = mux->pending; l != NULL; l = next) {
next = l->next;
op = l->data;
if (op->owner != owner)
continue;
mux->pending = g_slist_remove_link(mux->pending, l);
l->next = owned;
owned = l;
}
for (l = owned; l != NULL; l = l->next) {
op = l->data;
foreach_destroy(op);
}
g_slist_free(owned);
}
GIsiPending *g_isi_ntf_subscribe(GIsiModem *modem, uint8_t resource,
uint8_t msgid, GIsiNotifyFunc notify,
void *data, GDestroyNotify destroy)
{
GIsiServiceMux *mux;
GIsiPending *ntf;
mux = service_get(modem, resource);
if (mux == NULL) {
errno = ENOMEM;
return NULL;
}
ntf = g_try_new0(GIsiPending, 1);
if (ntf == NULL) {
errno = ENOMEM;
return NULL;
}
ntf->type = GISI_MESSAGE_TYPE_NTF;
ntf->service = mux;
ntf->notify = notify;
ntf->data = data;
ntf->destroy = destroy;
ntf->msgid = msgid;
mux->pending = g_slist_append(mux->pending, ntf);
ISIDBG(modem, "Subscribed to %s (%p) [res=0x%02X, id=0x%02X]",
pend_type_to_str(ntf->type), ntf, resource, msgid);
return ntf;
}
GIsiPending *g_isi_service_bind(GIsiModem *modem, uint8_t resource,
uint8_t msgid, GIsiNotifyFunc notify,
void *data, GDestroyNotify destroy)
{
GIsiServiceMux *mux;
GIsiPending *srv;
mux = service_get(modem, resource);
if (mux == NULL) {
errno = ENOMEM;
return NULL;
}
srv = g_try_new0(GIsiPending, 1);
if (srv == NULL) {
errno = ENOMEM;
return NULL;
}
srv->type = GISI_MESSAGE_TYPE_REQ;
srv->service = mux;
srv->notify = notify;
srv->data = data;
srv->destroy = destroy;
srv->msgid = msgid;
mux->pending = g_slist_append(mux->pending, srv);
ISIDBG(modem, "Bound service for %s (%p) [res=0x%02X, id=0x%02X]",
pend_type_to_str(srv->type), srv, resource, msgid);
service_regs_incr(mux);
return srv;
}
GIsiPending *g_isi_ind_subscribe(GIsiModem *modem, uint8_t resource,
uint8_t msgid, GIsiNotifyFunc notify,
void *data, GDestroyNotify destroy)
{
GIsiServiceMux *mux;
GIsiPending *ind;
mux = service_get(modem, resource);
if (mux == NULL) {
errno = ENOMEM;
return NULL;
}
ind = g_try_new0(GIsiPending, 1);
if (ind == NULL) {
errno = ENOMEM;
return NULL;
}
ind->type = GISI_MESSAGE_TYPE_IND;
ind->service = mux;
ind->notify = notify;
ind->data = data;
ind->destroy = destroy;
ind->msgid = msgid;
mux->pending = g_slist_append(mux->pending, ind);
ISIDBG(modem, "Subscribed for %s (%p) [res=0x%02X, id=0x%02X]",
pend_type_to_str(ind->type), ind, resource, msgid);
service_subs_incr(mux);
return ind;
}
int g_isi_response_send(GIsiModem *modem, const GIsiMessage *req,
const void *__restrict buf, size_t len)
{
const struct iovec iov = {
.iov_base = (void *)buf,
.iov_len = len,
};
return g_isi_response_vsend(modem, req, &iov, 1);
}
int g_isi_response_vsend(GIsiModem *modem, const GIsiMessage *req,
const struct iovec *__restrict iov,
size_t iovlen)
{
struct iovec _iov[1 + iovlen];
uint8_t utid;
size_t i;
utid = g_isi_msg_utid(req);
_iov[0].iov_base = &utid;
_iov[0].iov_len = 1;
for (i = 0; i < iovlen; i++)
_iov[1 + i] = iov[i];
return g_isi_modem_vsendto(modem, req->addr, _iov, 1 + iovlen);
}
int g_isi_modem_send(GIsiModem *modem, uint8_t resource,
const void *__restrict buf, size_t len)
{
struct sockaddr_pn dst = {
.spn_family = AF_PHONET,
.spn_resource = resource,
.spn_dev = modem->device,
};
return g_isi_modem_sendto(modem, &dst, buf, len);
}
int g_isi_modem_vsend(GIsiModem *modem, uint8_t resource,
const struct iovec *__restrict iov,
size_t iovlen)
{
struct sockaddr_pn dst = {
.spn_family = AF_PHONET,
.spn_resource = resource,
.spn_dev = modem->device,
};
return g_isi_modem_vsendto(modem, &dst, iov, iovlen);
}
int g_isi_modem_sendto(GIsiModem *modem, struct sockaddr_pn *dst,
const void *__restrict buf, size_t len)
{
const struct iovec iov = {
.iov_base = (void *)buf,
.iov_len = len,
};
return g_isi_modem_vsendto(modem, dst, &iov, 1);
}
int g_isi_modem_vsendto(GIsiModem *modem, struct sockaddr_pn *dst,
const struct iovec *__restrict iov,
size_t iovlen)
{
struct msghdr msg = {
.msg_name = (void *)dst,
.msg_namelen = sizeof(struct sockaddr_pn),
.msg_iov = (struct iovec *)iov,
.msg_iovlen = iovlen,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0,
};
ssize_t ret;
size_t i, len;
GIsiServiceMux *mux;
if (modem == NULL)
return -EINVAL;
mux = service_get(modem, dst->spn_resource);
if (mux == NULL)
return -ENOMEM;
for (i = 0, len = 0; i < iovlen; i++)
len += iov[i].iov_len;
if (modem->trace != NULL)
vtrace(dst, iov, iovlen, len, modem->trace);
ret = sendmsg(modem->req_fd, &msg, MSG_NOSIGNAL);
if (ret == -1)
return -errno;
if (ret != (ssize_t)len)
return -EMSGSIZE;
return 0;
}
void g_isi_modem_set_trace(GIsiModem *modem, GIsiNotifyFunc trace)
{
if (modem == NULL)
return;
modem->trace = trace;
}
void g_isi_modem_set_debug(GIsiModem *modem, GIsiDebugFunc debug)
{
if (modem == NULL)
return;
modem->debug = debug;
}
static int version_get_send(GIsiModem *modem, GIsiPending *ping)
{
GIsiServiceMux *mux = ping->service;
struct sockaddr_pn dst = {
.spn_family = AF_PHONET,
.spn_resource = mux->resource,
.spn_dev = modem->device,
};
uint8_t msg[] = {
ping->utid, /* UTID */
COMMON_MESSAGE,
COMM_ISI_VERSION_GET_REQ,
0, /* Filler */
};
ssize_t ret;
if (g_slist_find_custom(mux->pending, ping, utid_equal))
return -EBUSY;
ret = sendto(modem->req_fd, msg, sizeof(msg), MSG_NOSIGNAL,
(void *)&dst, sizeof(dst));
if (ret == -1)
return -errno;
if (ret != (ssize_t)sizeof(msg))
return -EMSGSIZE;
mux->last_utid = ping->utid;
mux->version_pending = TRUE;
return 0;
}
static gboolean reachable_notify(gpointer data)
{
GIsiPending *pong = data;
GIsiServiceMux *mux = pong->service;
struct sockaddr_pn addr = {
.spn_resource = mux->resource,
.spn_dev = mux->object >> 8,
.spn_obj = mux->object & 0xff,
};
GIsiMessage msg = {
.version = &mux->version,
.addr = &addr,
};
pending_remove_and_dispatch(pong, &msg);
return FALSE;
}
GIsiPending *g_isi_resource_ping(GIsiModem *modem, uint8_t resource,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy)
{
GIsiServiceMux *mux;
GIsiPending *ping;
int ret;
mux = service_get(modem, resource);
if (mux == NULL) {
errno = ENOMEM;
return NULL;
}
ping = g_try_new0(GIsiPending, 1);
if (ping == NULL) {
errno = ENOMEM;
return NULL;
}
ping->type = GISI_MESSAGE_TYPE_COMMON;
ping->utid = service_next_utid(mux);
ping->service = mux;
ping->notify = notify;
ping->data = data;
ping->destroy = destroy;
ping->msgid = COMM_ISI_VERSION_GET_REQ;
if (mux->reachable) {
g_idle_add(reachable_notify, ping);
return ping;
}
if (!mux->version_pending) {
ret = version_get_send(modem, ping);
if (ret < 0) {
g_free(ping);
errno = ret;
return NULL;
}
mux->last_utid = ping->utid;
}
ping->timeout = g_timeout_add_seconds(COMMON_TIMEOUT, resp_timeout,
ping);
mux->pending = g_slist_prepend(mux->pending, ping);
mux->version_pending = TRUE;
ISIDBG(modem, "Ping sent %s (%p) [res=0x%02X]",
pend_type_to_str(ping->type), ping, resource);
return ping;
}
|