2 This file is part of GNUnet
3 Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 * Our caller identity.
91 struct GNUNET_IDENTITY_Ego *caller_id;
94 * GNS zone to use to resolve @e callee.
96 struct GNUNET_IDENTITY_Ego *zone_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 fail_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 #GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND.
188 * @param cls the `struct GNUNET_CONVERSATION_Call`
189 * @param msg the message
192 handle_call_suspend (void *cls,
193 const struct ClientPhoneSuspendMessage *msg)
195 struct GNUNET_CONVERSATION_Call *call = cls;
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 #GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME.
233 * @param cls the `struct GNUNET_CONVERSATION_Call`
234 * @param msg the message
237 handle_call_resume (void *cls,
238 const struct ClientPhoneResumeMessage *msg)
240 struct GNUNET_CONVERSATION_Call *call = cls;
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 #GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP.
282 * @param cls the `struct GNUNET_CONVERSATION_Call`
283 * @param msg the message
286 handle_call_picked_up (void *cls,
287 const struct ClientPhonePickedupMessage *msg)
289 struct GNUNET_CONVERSATION_Call *call = cls;
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:
314 GNUNET_CONVERSATION_call_stop (call);
321 * We received a #GNUNET_MESSAGE_TYPE_CONVERSATION_CS_HANG_UP.
323 * @param cls the `struct GNUNET_CONVERSATION_Call`
324 * @param msg the message
327 handle_call_hangup (void *cls,
328 const struct ClientPhoneHangupMessage *msg)
330 struct GNUNET_CONVERSATION_Call *call = cls;
331 GNUNET_CONVERSATION_CallEventHandler eh;
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`, check it is well-formed.
360 * @param cls the `struct GNUNET_CONVERSATION_Call`
361 * @param msg the message
362 * @return #GNUNET_OK (always well-formed)
365 check_call_audio (void *cls,
366 const struct ClientAudioMessage *am)
368 /* any payload is OK */
374 * We received a `struct ClientAudioMessage`
376 * @param cls the `struct GNUNET_CONVERSATION_Call`
377 * @param msg the message
380 handle_call_audio (void *cls,
381 const struct ClientAudioMessage *am)
383 struct GNUNET_CONVERSATION_Call *call = cls;
395 case CS_SUSPENDED_CALLER:
396 /* can happen: we suspended, other peer did not yet
399 case CS_SUSPENDED_CALLEE:
400 case CS_SUSPENDED_BOTH:
401 /* can (rarely) also happen: other peer suspended, but cadet might
402 have had delayed data on the unreliable channel */
405 call->speaker->play (call->speaker->cls,
406 ntohs (am->header.size) - sizeof (struct ClientAudioMessage),
410 GNUNET_CONVERSATION_call_stop (call);
417 * Iterator called on obtained result for a GNS lookup.
419 * @param cls closure with the `struct GNUNET_CONVERSATION_Call`
420 * @param rd_count number of records in @a rd
421 * @param rd the records in reply
424 handle_gns_response (void *cls,
426 const struct GNUNET_GNSRECORD_Data *rd)
428 struct GNUNET_CONVERSATION_Call *call = cls;
430 struct GNUNET_MQ_Envelope *e;
431 struct ClientCallMessage *ccm;
433 GNUNET_break (NULL != call->gns_lookup);
434 GNUNET_break (CS_LOOKUP == call->state);
435 call->gns_lookup = NULL;
436 for (i=0;i<rd_count;i++)
438 if (GNUNET_GNSRECORD_TYPE_PHONE == rd[i].record_type)
440 if (rd[i].data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
445 memcpy (&call->phone_record,
448 e = GNUNET_MQ_msg (ccm, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL);
449 ccm->line = call->phone_record.line;
450 ccm->target = call->phone_record.peer;
451 ccm->caller_id = *GNUNET_IDENTITY_ego_get_private_key (call->caller_id);
452 GNUNET_MQ_send (call->mq, e);
453 call->state = CS_RINGING;
454 call->event_handler (call->event_handler_cls,
455 GNUNET_CONVERSATION_EC_CALL_RINGING);
460 call->event_handler (call->event_handler_cls,
461 GNUNET_CONVERSATION_EC_CALL_GNS_FAIL);
462 GNUNET_CONVERSATION_call_stop (call);
467 * We encountered an error talking with the conversation service.
469 * @param cls the `struct GNUNET_CONVERSATION_Call`
470 * @param error details about the error
473 call_error_handler (void *cls,
474 enum GNUNET_MQ_Error error)
476 struct GNUNET_CONVERSATION_Call *call = cls;
478 if (CS_SHUTDOWN == call->state)
480 GNUNET_CONVERSATION_call_stop (call);
483 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
484 _("Connection to conversation service lost, trying to reconnect\n"));
490 * The call got disconnected, destroy the handle.
492 * @param call call to reconnect
495 fail_call (struct GNUNET_CONVERSATION_Call *call)
497 if (CS_ACTIVE == call->state)
499 call->speaker->disable_speaker (call->speaker->cls);
500 call->mic->disable_microphone (call->mic->cls);
502 if (NULL != call->mq)
504 GNUNET_MQ_destroy (call->mq);
507 call->state = CS_SHUTDOWN;
508 call->event_handler (call->event_handler_cls,
509 GNUNET_CONVERSATION_EC_CALL_ERROR);
510 GNUNET_CONVERSATION_call_stop (call);
515 * Call the phone of another user.
517 * @param cfg configuration to use, specifies our phone service
518 * @param caller_id identity of the caller
519 * @param zone_id GNS zone to use to resolve @a callee
520 * @param callee GNS name of the callee (used to locate the callee's record)
521 * @param speaker speaker to use (will be used automatically immediately once the
522 * #GNUNET_CONVERSATION_EC_CALL_PICKED_UP event is generated); we will NOT generate
523 * a ring tone on the speaker
524 * @param mic microphone to use (will be used automatically immediately once the
525 * #GNUNET_CONVERSATION_EC_CALL_PICKED_UP event is generated)
526 * @param event_handler how to notify the owner of the phone about events
527 * @param event_handler_cls closure for @a event_handler
528 * @return handle for the call, NULL on hard errors
530 struct GNUNET_CONVERSATION_Call *
531 GNUNET_CONVERSATION_call_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
532 struct GNUNET_IDENTITY_Ego *caller_id,
533 struct GNUNET_IDENTITY_Ego *zone_id,
535 struct GNUNET_SPEAKER_Handle *speaker,
536 struct GNUNET_MICROPHONE_Handle *mic,
537 GNUNET_CONVERSATION_CallEventHandler event_handler,
538 void *event_handler_cls)
540 GNUNET_MQ_hd_fixed_size (call_suspend,
541 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
542 struct ClientPhoneSuspendMessage);
543 GNUNET_MQ_hd_fixed_size (call_resume,
544 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
545 struct ClientPhoneResumeMessage);
546 GNUNET_MQ_hd_fixed_size (call_picked_up,
547 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP,
548 struct ClientPhonePickedupMessage);
549 GNUNET_MQ_hd_fixed_size (call_hangup,
550 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
551 struct ClientPhoneHangupMessage);
552 GNUNET_MQ_hd_var_size (call_audio,
553 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
554 struct ClientAudioMessage);
555 struct GNUNET_CONVERSATION_Call *call
556 = GNUNET_new (struct GNUNET_CONVERSATION_Call);
557 struct GNUNET_MQ_MessageHandler handlers[] = {
558 make_call_suspend_handler (call),
559 make_call_resume_handler (call),
560 make_call_picked_up_handler (call),
561 make_call_hangup_handler (call),
562 make_call_audio_handler (call),
563 GNUNET_MQ_handler_end ()
565 struct GNUNET_CRYPTO_EcdsaPublicKey my_zone;
567 call->mq = GNUNET_CLIENT_connecT (cfg,
572 if (NULL == call->mq)
579 call->caller_id = caller_id;
580 call->zone_id = zone_id;
581 call->callee = GNUNET_strdup (callee);
582 call->speaker = speaker;
584 call->event_handler = event_handler;
585 call->event_handler_cls = event_handler_cls;
586 call->gns = GNUNET_GNS_connect (cfg);
587 if (NULL == call->gns)
589 GNUNET_CONVERSATION_call_stop (call);
592 call->state = CS_LOOKUP;
593 GNUNET_IDENTITY_ego_get_public_key (call->zone_id,
595 call->gns_lookup = GNUNET_GNS_lookup (call->gns,
598 GNUNET_GNSRECORD_TYPE_PHONE,
600 NULL /* FIXME: add shortening support */,
601 &handle_gns_response, call);
602 GNUNET_assert (NULL != call->gns_lookup);
608 * Terminate a call. The call may be ringing or ready at this time.
610 * @param call call to terminate
613 GNUNET_CONVERSATION_call_stop (struct GNUNET_CONVERSATION_Call *call)
615 if ( (NULL != call->speaker) &&
616 (CS_ACTIVE == call->state) )
617 call->speaker->disable_speaker (call->speaker->cls);
618 if ( (NULL != call->mic) &&
619 (CS_ACTIVE == call->state) )
620 call->mic->disable_microphone (call->mic->cls);
621 if (CS_SHUTDOWN != call->state)
623 call->state = CS_SHUTDOWN;
625 if (NULL != call->mq)
627 GNUNET_MQ_destroy (call->mq);
630 if (NULL != call->gns_lookup)
632 GNUNET_GNS_lookup_cancel (call->gns_lookup);
633 call->gns_lookup = NULL;
635 if (NULL != call->gns)
637 GNUNET_GNS_disconnect (call->gns);
640 GNUNET_free (call->callee);
646 * Pause a call. Temporarily suspends the use of speaker and
649 * @param call call to pause
652 GNUNET_CONVERSATION_call_suspend (struct GNUNET_CONVERSATION_Call *call)
654 struct GNUNET_MQ_Envelope *e;
655 struct ClientPhoneSuspendMessage *suspend;
657 GNUNET_assert ( (CS_SUSPENDED_CALLEE == call->state) ||
658 (CS_ACTIVE == call->state) );
659 if (CS_ACTIVE == call->state)
661 call->speaker->disable_speaker (call->speaker->cls);
662 call->mic->disable_microphone (call->mic->cls);
664 call->speaker = NULL;
666 e = GNUNET_MQ_msg (suspend, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
667 GNUNET_MQ_send (call->mq, e);
668 if (CS_SUSPENDED_CALLER == call->state)
669 call->state = CS_SUSPENDED_BOTH;
671 call->state = CS_SUSPENDED_CALLER;
676 * Resumes a call after #GNUNET_CONVERSATION_call_suspend.
678 * @param call call to resume
679 * @param speaker speaker to use
680 * a ring tone on the speaker
681 * @param mic microphone to use
684 GNUNET_CONVERSATION_call_resume (struct GNUNET_CONVERSATION_Call *call,
685 struct GNUNET_SPEAKER_Handle *speaker,
686 struct GNUNET_MICROPHONE_Handle *mic)
688 struct GNUNET_MQ_Envelope *e;
689 struct ClientPhoneResumeMessage *resume;
691 GNUNET_assert ( (CS_SUSPENDED_CALLER == call->state) ||
692 (CS_SUSPENDED_BOTH == call->state) );
693 e = GNUNET_MQ_msg (resume, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
694 GNUNET_MQ_send (call->mq, e);
695 call->speaker = speaker;
697 if (CS_SUSPENDED_CALLER == call->state)
699 call->state = CS_ACTIVE;
700 call->speaker->enable_speaker (call->speaker->cls);
701 call->mic->enable_microphone (call->mic->cls,
702 &transmit_call_audio,
707 call->state = CS_SUSPENDED_CALLEE;
712 /* end of conversation_api_call.c */