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 * Target callee as a GNS address/name.
106 struct GNUNET_SPEAKER_Handle *speaker;
111 struct GNUNET_MICROPHONE_Handle *mic;
114 * Function to call with events.
116 GNUNET_CONVERSATION_CallEventHandler event_handler;
119 * Closure for @e event_handler
121 void *event_handler_cls;
124 * Handle for transmitting to the CONVERSATION service.
126 struct GNUNET_MQ_Handle *mq;
129 * Connection to GNS (can be NULL).
131 struct GNUNET_GNS_Handle *gns;
134 * Active GNS lookup (or NULL).
136 struct GNUNET_GNS_LookupRequest *gns_lookup;
139 * Target phone record, only valid after the lookup is done.
141 struct GNUNET_CONVERSATION_PhoneRecord phone_record;
144 * State machine for the call.
146 enum CallState state;
152 * The call got disconnected, reconnect to the service.
154 * @param call call to reconnect
157 reconnect_call (struct GNUNET_CONVERSATION_Call *call);
161 * Process recorded audio data.
163 * @param cls closure with the `struct GNUNET_CONVERSATION_Call`
164 * @param data_size number of bytes in @a data
165 * @param data audio data to play
168 transmit_call_audio (void *cls,
172 struct GNUNET_CONVERSATION_Call *call = cls;
173 struct GNUNET_MQ_Envelope *e;
174 struct ClientAudioMessage *am;
176 GNUNET_assert (CS_ACTIVE == call->state);
177 e = GNUNET_MQ_msg_extra (am,
179 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
180 memcpy (&am[1], data, data_size);
181 GNUNET_MQ_send (call->mq, e);
186 * We received a `struct ClientPhoneSuspendMessage`
188 * @param cls the `struct GNUNET_CONVERSATION_Call`
189 * @param msg the message
192 handle_call_suspend (void *cls,
193 const struct GNUNET_MessageHeader *msg)
195 struct GNUNET_CONVERSATION_Call *call = cls;
201 reconnect_call (call);
205 reconnect_call (call);
207 case CS_SUSPENDED_CALLER:
208 call->state = CS_SUSPENDED_BOTH;
209 call->event_handler (call->event_handler_cls,
210 GNUNET_CONVERSATION_EC_CALL_SUSPENDED);
212 case CS_SUSPENDED_CALLEE:
213 case CS_SUSPENDED_BOTH:
217 call->state = CS_SUSPENDED_CALLEE;
218 call->speaker->disable_speaker (call->speaker->cls);
219 call->mic->disable_microphone (call->mic->cls);
220 call->event_handler (call->event_handler_cls,
221 GNUNET_CONVERSATION_EC_CALL_SUSPENDED);
224 GNUNET_CONVERSATION_call_stop (call);
231 * We received a `struct ClientPhoneResumeMessage`
233 * @param cls the `struct GNUNET_CONVERSATION_Call`
234 * @param msg the message
237 handle_call_resume (void *cls,
238 const struct GNUNET_MessageHeader *msg)
240 struct GNUNET_CONVERSATION_Call *call = cls;
246 reconnect_call (call);
250 reconnect_call (call);
252 case CS_SUSPENDED_CALLER:
255 case CS_SUSPENDED_CALLEE:
256 call->state = CS_ACTIVE;
257 call->speaker->enable_speaker (call->speaker->cls);
258 call->mic->enable_microphone (call->mic->cls,
259 &transmit_call_audio,
261 call->event_handler (call->event_handler_cls,
262 GNUNET_CONVERSATION_EC_CALL_RESUMED);
264 case CS_SUSPENDED_BOTH:
265 call->state = CS_SUSPENDED_CALLER;
266 call->event_handler (call->event_handler_cls,
267 GNUNET_CONVERSATION_EC_CALL_RESUMED);
273 GNUNET_CONVERSATION_call_stop (call);
280 * We received a `struct ClientPhonePickedupMessage`
282 * @param cls the `struct GNUNET_CONVERSATION_Call`
283 * @param msg the message
286 handle_call_picked_up (void *cls,
287 const struct GNUNET_MessageHeader *msg)
289 struct GNUNET_CONVERSATION_Call *call = cls;
295 reconnect_call (call);
298 call->state = CS_ACTIVE;
299 call->speaker->enable_speaker (call->speaker->cls);
300 call->mic->enable_microphone (call->mic->cls,
301 &transmit_call_audio,
303 call->event_handler (call->event_handler_cls,
304 GNUNET_CONVERSATION_EC_CALL_PICKED_UP);
306 case CS_SUSPENDED_CALLER:
307 case CS_SUSPENDED_CALLEE:
308 case CS_SUSPENDED_BOTH:
311 reconnect_call (call);
314 GNUNET_CONVERSATION_call_stop (call);
321 * We received a `struct ClientPhoneHangupMessage`
323 * @param cls the `struct GNUNET_CONVERSATION_Call`
324 * @param msg the message
327 handle_call_hangup (void *cls,
328 const struct GNUNET_MessageHeader *msg)
330 struct GNUNET_CONVERSATION_Call *call = cls;
331 GNUNET_CONVERSATION_CallEventHandler eh;
338 reconnect_call (call);
341 case CS_SUSPENDED_CALLER:
342 case CS_SUSPENDED_CALLEE:
343 case CS_SUSPENDED_BOTH:
345 eh = call->event_handler;
346 eh_cls = call->event_handler_cls;
347 GNUNET_CONVERSATION_call_stop (call);
348 eh (eh_cls, GNUNET_CONVERSATION_EC_CALL_HUNG_UP);
351 GNUNET_CONVERSATION_call_stop (call);
358 * We received a `struct ClientAudioMessage`
360 * @param cls the `struct GNUNET_CONVERSATION_Call`
361 * @param msg the message
364 handle_call_audio_message (void *cls,
365 const struct GNUNET_MessageHeader *msg)
367 struct GNUNET_CONVERSATION_Call *call = cls;
368 const struct ClientAudioMessage *am;
370 am = (const struct ClientAudioMessage *) msg;
375 reconnect_call (call);
379 reconnect_call (call);
381 case CS_SUSPENDED_CALLER:
382 /* can happen: we suspended, other peer did not yet
385 case CS_SUSPENDED_CALLEE:
386 case CS_SUSPENDED_BOTH:
387 /* can (rarely) also happen: other peer suspended, but mesh might
388 have had delayed data on the unreliable channel */
391 call->speaker->play (call->speaker->cls,
392 ntohs (msg->size) - sizeof (struct ClientAudioMessage),
396 GNUNET_CONVERSATION_call_stop (call);
403 * Iterator called on obtained result for a GNS lookup.
405 * @param cls closure with the `struct GNUNET_CONVERSATION_Call`
406 * @param rd_count number of records in @a rd
407 * @param rd the records in reply
410 handle_gns_response (void *cls,
412 const struct GNUNET_GNSRECORD_Data *rd)
414 struct GNUNET_CONVERSATION_Call *call = cls;
416 struct GNUNET_MQ_Envelope *e;
417 struct ClientCallMessage *ccm;
419 call->gns_lookup = NULL;
420 for (i=0;i<rd_count;i++)
422 if (GNUNET_GNSRECORD_TYPE_PHONE == rd[i].record_type)
424 if (rd[i].data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
429 memcpy (&call->phone_record,
432 e = GNUNET_MQ_msg (ccm, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL);
433 ccm->line = call->phone_record.line;
434 ccm->target = call->phone_record.peer;
435 ccm->caller_id = *GNUNET_IDENTITY_ego_get_private_key (call->caller_id);
436 GNUNET_MQ_send (call->mq, e);
437 call->state = CS_RINGING;
438 call->event_handler (call->event_handler_cls,
439 GNUNET_CONVERSATION_EC_CALL_RINGING);
444 call->event_handler (call->event_handler_cls,
445 GNUNET_CONVERSATION_EC_CALL_GNS_FAIL);
446 GNUNET_CONVERSATION_call_stop (call);
451 * We encountered an error talking with the conversation service.
453 * @param cls the `struct GNUNET_CONVERSATION_Call`
454 * @param error details about the error
457 call_error_handler (void *cls,
458 enum GNUNET_MQ_Error error)
460 struct GNUNET_CONVERSATION_Call *call = cls;
462 if (CS_SHUTDOWN == call->state)
464 GNUNET_CONVERSATION_call_stop (call);
467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
468 _("Connection to conversation service lost, trying to reconnect\n"));
469 reconnect_call (call);
474 * The call got disconnected, reconnect to the service.
476 * @param call call to reconnect
479 reconnect_call (struct GNUNET_CONVERSATION_Call *call)
481 static struct GNUNET_MQ_MessageHandler handlers[] =
483 { &handle_call_suspend,
484 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
485 sizeof (struct ClientPhoneSuspendMessage) },
486 { &handle_call_resume,
487 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
488 sizeof (struct ClientPhoneResumeMessage) },
489 { &handle_call_picked_up,
490 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP,
491 sizeof (struct ClientPhonePickedupMessage) },
492 { &handle_call_hangup,
493 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
494 sizeof (struct ClientPhoneHangupMessage) },
495 { &handle_call_audio_message,
496 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
500 struct GNUNET_CRYPTO_EcdsaPublicKey my_zone;
502 if (CS_ACTIVE == call->state)
504 call->speaker->disable_speaker (call->speaker->cls);
505 call->mic->disable_microphone (call->mic->cls);
507 if (NULL != call->mq)
509 GNUNET_MQ_destroy (call->mq);
512 if (NULL != call->client)
514 GNUNET_CLIENT_disconnect (call->client);
517 call->state = CS_SHUTDOWN;
518 call->client = GNUNET_CLIENT_connect ("conversation", call->cfg);
519 if (NULL == call->client)
521 call->mq = GNUNET_MQ_queue_for_connection_client (call->client,
525 call->state = CS_LOOKUP;
526 GNUNET_IDENTITY_ego_get_public_key (call->caller_id,
528 call->gns_lookup = GNUNET_GNS_lookup (call->gns,
531 GNUNET_GNSRECORD_TYPE_PHONE,
533 NULL /* FIXME: add shortening support */,
534 &handle_gns_response, call);
535 GNUNET_assert (NULL != call->gns_lookup);
540 * Call the phone of another user.
542 * @param cfg configuration to use, specifies our phone service
543 * @param caller_id identity of the caller
544 * @param callee GNS name of the callee (used to locate the callee's record)
545 * @param speaker speaker to use (will be used automatically immediately once the
546 * #GNUNET_CONVERSATION_EC_CALL_PICKED_UP event is generated); we will NOT generate
547 * a ring tone on the speaker
548 * @param mic microphone to use (will be used automatically immediately once the
549 * #GNUNET_CONVERSATION_EC_CALL_PICKED_UP event is generated)
550 * @param event_handler how to notify the owner of the phone about events
551 * @param event_handler_cls closure for @a event_handler
553 struct GNUNET_CONVERSATION_Call *
554 GNUNET_CONVERSATION_call_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
555 struct GNUNET_IDENTITY_Ego *caller_id,
557 struct GNUNET_SPEAKER_Handle *speaker,
558 struct GNUNET_MICROPHONE_Handle *mic,
559 GNUNET_CONVERSATION_CallEventHandler event_handler,
560 void *event_handler_cls)
562 struct GNUNET_CONVERSATION_Call *call;
564 call = GNUNET_new (struct GNUNET_CONVERSATION_Call);
566 call->caller_id = caller_id;
567 call->callee = GNUNET_strdup (callee);
568 call->speaker = speaker;
570 call->event_handler = event_handler;
571 call->event_handler_cls = event_handler_cls;
572 call->gns = GNUNET_GNS_connect (cfg);
573 reconnect_call (call);
575 if ( (NULL == call->client) ||
576 (NULL == call->gns) )
578 GNUNET_CONVERSATION_call_stop (call);
586 * We've sent the hang up message, now finish terminating the call.
588 * @param cls the `struct GNUNET_CONVERSATION_Call` to terminate
591 finish_stop (void *cls)
593 struct GNUNET_CONVERSATION_Call *call = cls;
595 GNUNET_assert (CS_SHUTDOWN == call->state);
596 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 struct GNUNET_MQ_Envelope *e;
609 struct ClientPhoneHangupMessage *hang;
611 if ( (NULL != call->speaker) &&
612 (CS_ACTIVE == call->state) )
613 call->speaker->disable_speaker (call->speaker->cls);
614 if ( (NULL != call->mic) &&
615 (CS_ACTIVE == call->state) )
616 call->mic->disable_microphone (call->mic->cls);
617 if (CS_SHUTDOWN != call->state)
619 call->state = CS_SHUTDOWN;
620 e = GNUNET_MQ_msg (hang, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
621 GNUNET_MQ_notify_sent (e, &finish_stop, call);
622 GNUNET_MQ_send (call->mq, e);
625 if (NULL != call->mq)
627 GNUNET_MQ_destroy (call->mq);
630 if (NULL != call->client)
632 GNUNET_CLIENT_disconnect (call->client);
635 if (NULL != call->gns_lookup)
637 GNUNET_GNS_lookup_cancel (call->gns_lookup);
638 call->gns_lookup = NULL;
640 if (NULL != call->gns)
642 GNUNET_GNS_disconnect (call->gns);
645 GNUNET_free (call->callee);
651 * Pause a call. Temporarily suspends the use of speaker and
654 * @param call call to pause
657 GNUNET_CONVERSATION_call_suspend (struct GNUNET_CONVERSATION_Call *call)
659 struct GNUNET_MQ_Envelope *e;
660 struct ClientPhoneSuspendMessage *suspend;
662 GNUNET_assert ( (CS_SUSPENDED_CALLEE == call->state) ||
663 (CS_ACTIVE == call->state) );
664 if (CS_ACTIVE == call->state)
666 call->speaker->disable_speaker (call->speaker->cls);
667 call->mic->disable_microphone (call->mic->cls);
669 call->speaker = NULL;
671 e = GNUNET_MQ_msg (suspend, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
672 GNUNET_MQ_send (call->mq, e);
673 if (CS_SUSPENDED_CALLER == call->state)
674 call->state = CS_SUSPENDED_BOTH;
676 call->state = CS_SUSPENDED_CALLER;
681 * Resumes a call after #GNUNET_CONVERSATION_call_suspend.
683 * @param call call to resume
684 * @param speaker speaker to use
685 * a ring tone on the speaker
686 * @param mic microphone to use
689 GNUNET_CONVERSATION_call_resume (struct GNUNET_CONVERSATION_Call *call,
690 struct GNUNET_SPEAKER_Handle *speaker,
691 struct GNUNET_MICROPHONE_Handle *mic)
693 struct GNUNET_MQ_Envelope *e;
694 struct ClientPhoneResumeMessage *resume;
696 GNUNET_assert ( (CS_SUSPENDED_CALLER == call->state) ||
697 (CS_SUSPENDED_BOTH == call->state) );
698 e = GNUNET_MQ_msg (resume, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
699 GNUNET_MQ_send (call->mq, e);
700 call->speaker = speaker;
702 if (CS_SUSPENDED_CALLER == call->state)
704 call->state = CS_ACTIVE;
705 call->speaker->enable_speaker (call->speaker->cls);
706 call->mic->enable_microphone (call->mic->cls,
707 &transmit_call_audio,
712 call->state = CS_SUSPENDED_CALLEE;
717 /* end of conversation_api_call.c */