11 #include <linux/input.h>
12 #include "dialdetector.h"
14 #include <tapi-ioctl.h>
16 #include <tapi-device.h>
17 #include <tapi-port.h>
21 #include "sip_client.h"
22 #include "sip_agent.h"
23 #include "tapi_agent.h"
25 static struct tapi_device dev;
26 static struct tapi_agent *ports;
28 static struct sip_client sip_client;
30 static void release_session(struct session *session)
35 static void dial(struct tapi_agent *caller, struct agent *callee)
37 struct session *session;
39 session = session_alloc(&dev, &caller->agent, callee, release_session);
44 caller->session = session;
47 static void tel_dial(struct tapi_agent *caller, const char *number)
51 callee = atoi(number) - 1;
53 if (callee < 0 || callee > 1)
55 dial(caller, &ports[callee].agent);
58 static void sip_dial(struct tapi_agent *caller, const char *identifier)
60 struct sip_agent *callee;
62 callee = sip_client_alloc_agent(&sip_client, identifier);
66 dial(caller, &callee->agent);
69 static void dial_callback(struct tapi_port *port, size_t num_digits, const unsigned char *digits)
71 struct tapi_agent *tagent = port_to_tapi_agent(port);
73 struct contact *contact;
76 if (tagent->state != TAPI_AGENT_STATE_IDLE)
79 for (i = 0; i < num_digits; ++i) {
82 number[i] = digits[i] + '0';
86 printf("dial callback: %s\n", number);
88 contact = contact_get(number);
93 if (strncmp("tel:", contact->identifier, 4) == 0) {
94 tel_dial(tagent, contact->identifier + 4);
95 } else if (strncmp("sip:", contact->identifier, 4) == 0) {
96 sip_dial(tagent, contact->identifier);
98 tagent->state = TAPI_AGENT_STATE_ACTIVE;
101 static int incoming_sip_call(struct sip_client *client,
102 struct sip_agent *caller)
104 struct tapi_agent *callee = NULL;;
105 struct session *session;
108 for (i = 0; i < 2; ++i) {
109 if (ports[i].state == TAPI_AGENT_STATE_IDLE) {
118 session = session_alloc(&dev, &caller->agent, &callee->agent,
120 caller->session = session;
125 int main(int argc, char *argv[])
127 struct dialdetector *dd, *dd2;
128 struct account *account;
129 struct sip_client_config config;
130 const char *interface = "eth0";
142 account = get_account();
144 printf("No account\n");
148 ret = tapi_device_open(0, &dev);
150 printf("Failed to open tapi device: %d\n", ret);
154 ports = calloc(dev.num_ports, sizeof(*ports));
155 for (i = 0; i < dev.num_ports; ++i)
156 tapi_agent_init(&dev, i, &ports[i]);
158 dd = dialdetector_alloc(&ports[0].port);
159 dd->dial_callback = dial_callback;
160 dd2 = dialdetector_alloc(&ports[1].port);
161 dd2->dial_callback = dial_callback;
163 config.iface = interface;
164 config.host = account->realm;
165 config.port = account->sip_port;
166 config.username = account->username;
167 config.password = account->password;
169 config.stun_host = account->stun_host;
170 config.stun_port = account->stun_port;
172 sip_client_init(&sip_client, &dev, &config);
174 sip_client.incoming_call_cb = incoming_sip_call;