2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file conversation/conversation_api_call.c
23 * @brief call API to the conversation service
24 * @author Simon Dieterle
25 * @author Andreas Fuchs
26 * @author Christian Grothoff
29 #include "gnunet_conversation_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_gns_service.h"
32 #include "conversation.h"
36 * Possible states of the phone.
41 * We still need to lookup the callee.
46 * The call is ringing.
51 * The call is in an active conversation.
56 * The call is in termination.
61 * The call was suspended by the caller.
66 * The call was suspended by the callee.
71 * The call was suspended by both caller and callee.
78 * Handle for an outgoing call.
80 struct GNUNET_CONVERSATION_Call
86 const struct GNUNET_CONFIGURATION_Handle *cfg;
89 * Handle to talk with CONVERSATION service.
91 struct GNUNET_CLIENT_Connection *client;
94 * Our caller identity.
96 struct GNUNET_IDENTITY_Ego *caller_id;
99 * GNS zone to use to resolve @e callee.
101 struct GNUNET_IDENTITY_Ego *zone_id;
104 * Target callee as a GNS address/name.
111 struct GNUNET_SPEAKER_Handle *speaker;
116 struct GNUNET_MICROPHONE_Handle *mic;
119 * Function to call with events.
121 GNUNET_CONVERSATION_CallEventHandler event_handler;
124 * Closure for @e event_handler
126 void *event_handler_cls;
129 * Handle for transmitting to the CONVERSATION service.
131 struct GNUNET_MQ_Handle *mq;
134 * Connection to GNS (can be NULL).
136 struct GNUNET_GNS_Handle *gns;
139 * Active GNS lookup (or NULL).
141 struct GNUNET_GNS_LookupRequest *gns_lookup;
144 * Target phone record, only valid after the lookup is done.
146 struct GNUNET_CONVERSATION_PhoneRecord phone_record;
149 * State machine for the call.
151 enum CallState state;
157 * The call got disconnected, reconnect to the service.
159 * @param call call to reconnect
162 reconnect_call (struct GNUNET_CONVERSATION_Call *call);
166 * Process recorded audio data.
168 * @param cls closure with the `struct GNUNET_CONVERSATION_Call`
169 * @param data_size number of bytes in @a data
170 * @param data audio data to play
173 transmit_call_audio (void *cls,
177 struct GNUNET_CONVERSATION_Call *call = cls;
178 struct GNUNET_MQ_Envelope *e;
179 struct ClientAudioMessage *am;
181 GNUNET_assert (CS_ACTIVE == call->state);
182 e = GNUNET_MQ_msg_extra (am,
184 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
185 memcpy (&am[1], data, data_size);
186 GNUNET_MQ_send (call->mq, e);
191 * We received a `struct ClientPhoneSuspendMessage`
193 * @param cls the `struct GNUNET_CONVERSATION_Call`
194 * @param msg the message
197 handle_call_suspend (void *cls,
198 const struct GNUNET_MessageHeader *msg)
200 struct GNUNET_CONVERSATION_Call *call = cls;
206 reconnect_call (call);
210 reconnect_call (call);
212 case CS_SUSPENDED_CALLER:
213 call->state = CS_SUSPENDED_BOTH;
214 call->event_handler (call->event_handler_cls,
215 GNUNET_CONVERSATION_EC_CALL_SUSPENDED);
217 case CS_SUSPENDED_CALLEE:
218 case CS_SUSPENDED_BOTH:
222 call->state = CS_SUSPENDED_CALLEE;
223 call->speaker->disable_speaker (call->speaker->cls);
224 call->mic->disable_microphone (call->mic->cls);
225 call->event_handler (call->event_handler_cls,
226 GNUNET_CONVERSATION_EC_CALL_SUSPENDED);
229 GNUNET_CONVERSATION_call_stop (call);
236 * We received a `struct ClientPhoneResumeMessage`
238 * @param cls the `struct GNUNET_CONVERSATION_Call`
239 * @param msg the message
242 handle_call_resume (void *cls,
243 const struct GNUNET_MessageHeader *msg)
245 struct GNUNET_CONVERSATION_Call *call = cls;
251 reconnect_call (call);
255 reconnect_call (call);
257 case CS_SUSPENDED_CALLER:
260 case CS_SUSPENDED_CALLEE:
261 call->state = CS_ACTIVE;
262 call->speaker->enable_speaker (call->speaker->cls);
263 call->mic->enable_microphone (call->mic->cls,
264 &transmit_call_audio,
266 call->event_handler (call->event_handler_cls,
267 GNUNET_CONVERSATION_EC_CALL_RESUMED);
269 case CS_SUSPENDED_BOTH:
270 call->state = CS_SUSPENDED_CALLER;
271 call->event_handler (call->event_handler_cls,
272 GNUNET_CONVERSATION_EC_CALL_RESUMED);
278 GNUNET_CONVERSATION_call_stop (call);
285 * We received a `struct ClientPhonePickedupMessage`
287 * @param cls the `struct GNUNET_CONVERSATION_Call`
288 * @param msg the message
291 handle_call_picked_up (void *cls,
292 const struct GNUNET_MessageHeader *msg)
294 struct GNUNET_CONVERSATION_Call *call = cls;
300 reconnect_call (call);
303 call->state = CS_ACTIVE;
304 call->speaker->enable_speaker (call->speaker->cls);
305 call->mic->enable_microphone (call->mic->cls,
306 &transmit_call_audio,
308 call->event_handler (call->event_handler_cls,
309 GNUNET_CONVERSATION_EC_CALL_PICKED_UP);
311 case CS_SUSPENDED_CALLER:
312 case CS_SUSPENDED_CALLEE:
313 case CS_SUSPENDED_BOTH:
316 reconnect_call (call);
319 GNUNET_CONVERSATION_call_stop (call);
326 * We received a `struct ClientPhoneHangupMessage`
328 * @param cls the `struct GNUNET_CONVERSATION_Call`
329 * @param msg the message
332 handle_call_hangup (void *cls,
333 const struct GNUNET_MessageHeader *msg)
335 struct GNUNET_CONVERSATION_Call *call = cls;
336 GNUNET_CONVERSATION_CallEventHandler eh;
343 reconnect_call (call);
346 case CS_SUSPENDED_CALLER:
347 case CS_SUSPENDED_CALLEE:
348 case CS_SUSPENDED_BOTH:
350 eh = call->event_handler;
351 eh_cls = call->event_handler_cls;
352 GNUNET_CONVERSATION_call_stop (call);
353 eh (eh_cls, GNUNET_CONVERSATION_EC_CALL_HUNG_UP);
356 GNUNET_CONVERSATION_call_stop (call);
363 * We received a `struct ClientAudioMessage`
365 * @param cls the `struct GNUNET_CONVERSATION_Call`
366 * @param msg the message
369 handle_call_audio_message (void *cls,
370 const struct GNUNET_MessageHeader *msg)
372 struct GNUNET_CONVERSATION_Call *call = cls;
373 const struct ClientAudioMessage *am;
375 am = (const struct ClientAudioMessage *) msg;
380 reconnect_call (call);
384 reconnect_call (call);
386 case CS_SUSPENDED_CALLER:
387 /* can happen: we suspended, other peer did not yet
390 case CS_SUSPENDED_CALLEE:
391 case CS_SUSPENDED_BOTH:
392 /* can (rarely) also happen: other peer suspended, but cadet might
393 have had delayed data on the unreliable channel */
396 call->speaker->play (call->speaker->cls,
397 ntohs (msg->size) - sizeof (struct ClientAudioMessage),
401 GNUNET_CONVERSATION_call_stop (call);
408 * Iterator called on obtained result for a GNS lookup.
410 * @param cls closure with the `struct GNUNET_CONVERSATION_Call`
411 * @param rd_count number of records in @a rd
412 * @param rd the records in reply
415 handle_gns_response (void *cls,
417 const struct GNUNET_GNSRECORD_Data *rd)
419 struct GNUNET_CONVERSATION_Call *call = cls;
421 struct GNUNET_MQ_Envelope *e;
422 struct ClientCallMessage *ccm;
424 GNUNET_break (NULL != call->gns_lookup);
425 GNUNET_break (CS_LOOKUP == call->state);
426 call->gns_lookup = NULL;
427 for (i=0;i<rd_count;i++)
429 if (GNUNET_GNSRECORD_TYPE_PHONE == rd[i].record_type)
431 if (rd[i].data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
436 memcpy (&call->phone_record,
439 e = GNUNET_MQ_msg (ccm, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL);
440 ccm->line = call->phone_record.line;
441 ccm->target = call->phone_record.peer;
442 ccm->caller_id = *GNUNET_IDENTITY_ego_get_private_key (call->caller_id);
443 GNUNET_MQ_send (call->mq, e);
444 call->state = CS_RINGING;
445 call->event_handler (call->event_handler_cls,
446 GNUNET_CONVERSATION_EC_CALL_RINGING);
451 call->event_handler (call->event_handler_cls,
452 GNUNET_CONVERSATION_EC_CALL_GNS_FAIL);
453 GNUNET_CONVERSATION_call_stop (call);
458 * We encountered an error talking with the conversation service.
460 * @param cls the `struct GNUNET_CONVERSATION_Call`
461 * @param error details about the error
464 call_error_handler (void *cls,
465 enum GNUNET_MQ_Error error)
467 struct GNUNET_CONVERSATION_Call *call = cls;
469 if (CS_SHUTDOWN == call->state)
471 GNUNET_CONVERSATION_call_stop (call);
474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
475 _("Connection to conversation service lost, trying to reconnect\n"));
476 reconnect_call (call);
481 * The call got disconnected, reconnect to the service.
483 * @param call call to reconnect
486 reconnect_call (struct GNUNET_CONVERSATION_Call *call)
488 static struct GNUNET_MQ_MessageHandler handlers[] =
490 { &handle_call_suspend,
491 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
492 sizeof (struct ClientPhoneSuspendMessage) },
493 { &handle_call_resume,
494 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
495 sizeof (struct ClientPhoneResumeMessage) },
496 { &handle_call_picked_up,
497 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP,
498 sizeof (struct ClientPhonePickedupMessage) },
499 { &handle_call_hangup,
500 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
501 sizeof (struct ClientPhoneHangupMessage) },
502 { &handle_call_audio_message,
503 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
507 struct GNUNET_CRYPTO_EcdsaPublicKey my_zone;
509 if (CS_ACTIVE == call->state)
511 call->speaker->disable_speaker (call->speaker->cls);
512 call->mic->disable_microphone (call->mic->cls);
514 if (NULL != call->mq)
516 GNUNET_MQ_destroy (call->mq);
519 if (NULL != call->client)
521 GNUNET_CLIENT_disconnect (call->client);
524 call->state = CS_SHUTDOWN;
525 call->client = GNUNET_CLIENT_connect ("conversation", call->cfg);
526 if (NULL == call->client)
528 call->event_handler (call->event_handler_cls,
529 GNUNET_CONVERSATION_EC_CALL_ERROR);
533 call->mq = GNUNET_MQ_queue_for_connection_client (call->client,
537 call->state = CS_LOOKUP;
538 GNUNET_IDENTITY_ego_get_public_key (call->zone_id,
540 call->gns_lookup = GNUNET_GNS_lookup (call->gns,
543 GNUNET_GNSRECORD_TYPE_PHONE,
545 NULL /* FIXME: add shortening support */,
546 &handle_gns_response, call);
547 GNUNET_assert (NULL != call->gns_lookup);
552 * Call the phone of another user.
554 * @param cfg configuration to use, specifies our phone service
555 * @param caller_id identity of the caller
556 * @param zone_id GNS zone to use to resolve @a callee
557 * @param callee GNS name of the callee (used to locate the callee's record)
558 * @param speaker speaker to use (will be used automatically immediately once the
559 * #GNUNET_CONVERSATION_EC_CALL_PICKED_UP event is generated); we will NOT generate
560 * a ring tone on the speaker
561 * @param mic microphone to use (will be used automatically immediately once the
562 * #GNUNET_CONVERSATION_EC_CALL_PICKED_UP event is generated)
563 * @param event_handler how to notify the owner of the phone about events
564 * @param event_handler_cls closure for @a event_handler
566 struct GNUNET_CONVERSATION_Call *
567 GNUNET_CONVERSATION_call_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
568 struct GNUNET_IDENTITY_Ego *caller_id,
569 struct GNUNET_IDENTITY_Ego *zone_id,
571 struct GNUNET_SPEAKER_Handle *speaker,
572 struct GNUNET_MICROPHONE_Handle *mic,
573 GNUNET_CONVERSATION_CallEventHandler event_handler,
574 void *event_handler_cls)
576 struct GNUNET_CONVERSATION_Call *call;
578 call = GNUNET_new (struct GNUNET_CONVERSATION_Call);
580 call->caller_id = caller_id;
581 call->zone_id = zone_id;
582 call->callee = GNUNET_strdup (callee);
583 call->speaker = speaker;
585 call->event_handler = event_handler;
586 call->event_handler_cls = event_handler_cls;
587 call->gns = GNUNET_GNS_connect (cfg);
588 reconnect_call (call);
590 if ( (NULL == call->client) ||
591 (NULL == call->gns) )
593 GNUNET_CONVERSATION_call_stop (call);
601 * Terminate a call. The call may be ringing or ready at this time.
603 * @param call call to terminate
606 GNUNET_CONVERSATION_call_stop (struct GNUNET_CONVERSATION_Call *call)
608 if ( (NULL != call->speaker) &&
609 (CS_ACTIVE == call->state) )
610 call->speaker->disable_speaker (call->speaker->cls);
611 if ( (NULL != call->mic) &&
612 (CS_ACTIVE == call->state) )
613 call->mic->disable_microphone (call->mic->cls);
614 if (CS_SHUTDOWN != call->state)
616 call->state = CS_SHUTDOWN;
618 if (NULL != call->mq)
620 GNUNET_MQ_destroy (call->mq);
623 if (NULL != call->client)
625 GNUNET_CLIENT_disconnect (call->client);
628 if (NULL != call->gns_lookup)
630 GNUNET_GNS_lookup_cancel (call->gns_lookup);
631 call->gns_lookup = NULL;
633 if (NULL != call->gns)
635 GNUNET_GNS_disconnect (call->gns);
638 GNUNET_free (call->callee);
644 * Pause a call. Temporarily suspends the use of speaker and
647 * @param call call to pause
650 GNUNET_CONVERSATION_call_suspend (struct GNUNET_CONVERSATION_Call *call)
652 struct GNUNET_MQ_Envelope *e;
653 struct ClientPhoneSuspendMessage *suspend;
655 GNUNET_assert ( (CS_SUSPENDED_CALLEE == call->state) ||
656 (CS_ACTIVE == call->state) );
657 if (CS_ACTIVE == call->state)
659 call->speaker->disable_speaker (call->speaker->cls);
660 call->mic->disable_microphone (call->mic->cls);
662 call->speaker = NULL;
664 e = GNUNET_MQ_msg (suspend, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
665 GNUNET_MQ_send (call->mq, e);
666 if (CS_SUSPENDED_CALLER == call->state)
667 call->state = CS_SUSPENDED_BOTH;
669 call->state = CS_SUSPENDED_CALLER;
674 * Resumes a call after #GNUNET_CONVERSATION_call_suspend.
676 * @param call call to resume
677 * @param speaker speaker to use
678 * a ring tone on the speaker
679 * @param mic microphone to use
682 GNUNET_CONVERSATION_call_resume (struct GNUNET_CONVERSATION_Call *call,
683 struct GNUNET_SPEAKER_Handle *speaker,
684 struct GNUNET_MICROPHONE_Handle *mic)
686 struct GNUNET_MQ_Envelope *e;
687 struct ClientPhoneResumeMessage *resume;
689 GNUNET_assert ( (CS_SUSPENDED_CALLER == call->state) ||
690 (CS_SUSPENDED_BOTH == call->state) );
691 e = GNUNET_MQ_msg (resume, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
692 GNUNET_MQ_send (call->mq, e);
693 call->speaker = speaker;
695 if (CS_SUSPENDED_CALLER == call->state)
697 call->state = CS_ACTIVE;
698 call->speaker->enable_speaker (call->speaker->cls);
699 call->mic->enable_microphone (call->mic->cls,
700 &transmit_call_audio,
705 call->state = CS_SUSPENDED_CALLEE;
710 /* end of conversation_api_call.c */