Completed documentation about instance creation/destruction proceedure.
[lcr.git] / chan_lcr.c
index 906e6a3..a3153a9 100644 (file)
@@ -21,8 +21,9 @@ Now the channel driver is linked to LCR and can receive and make calls.
 Call is initiated by LCR:
 
 If a call is received from LCR, a MESSAGE_NEWREF is received first.
+The ref_was_assigned ist set to 1.
 A new chan_call instance is created. The call reference (ref) is given by
-MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
+the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
 using ast_channel_alloc(1).  The setup information is given to asterisk.
 The new Asterisk instance pointer (ast) is stored to chan_call structure.
@@ -34,8 +35,9 @@ Call is initiated by Asterisk:
 If a call is requested from Asterisk, a new chan_call instance is created.
 The new Asterisk instance pointer (ast) is stored to chan_call structure.
 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
-If the call is received (lcr_call) A MESSASGE_NEWREF is sent to LCR requesting
+If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
 a new call reference (ref).
+The ref_was_assigned ist set to 1.
 Further dialing information is queued.
 After the new callref is received by special MESSAGE_NEWREF reply, new ref
 is stored in the chan_call structure. 
@@ -57,6 +59,7 @@ Call is released by LCR:
 
 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
 The current ref is set to 0, to indicate released reference.
+The ref_was_assigned==1 shows that there is no other ref to be assigned.
 The state changes to CHAN_LCR_STATE_RELEASE.
 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
 the chan_call instance is destroyed.
@@ -73,6 +76,7 @@ lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
 set to CHAN_LCR_STATE_RELEASE.
+The ref_was_assigned==0 shows that a ref is still requested.
 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
 LCR and the chan_call instance is destroyed.
 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
@@ -205,46 +209,24 @@ void chan_lcr_log(int type, const char *file, int line, const char *function, st
  */
 struct chan_call *call_first;
 
-struct chan_call *find_call_ref(unsigned int ref)
-{
-       struct chan_call *call = call_first;
-
-       while(call)
-       {
-               if (call->ref == ref)
-                       break;
-               call = call->next;
-       }
-       return(call);
-}
-
-#if 0
-struct chan_call *find_call_ast(struct ast_channel *ast)
-{
-       struct chan_call *call = call_first;
-
-       while(call)
-       {
-               if (call->ast == ast)
-                       break;
-               call = call->next;
-       }
-       return(call);
-}
+/*
+ * find call by ref
+ * special case: 0: find new ref, that has not been assigned a ref yet
+ */
 
-struct chan_call *find_call_handle(unsigned int handle)
+struct chan_call *find_call_ref(unsigned int ref)
 {
        struct chan_call *call = call_first;
-
+       int assigned = (ref > 0);
+       
        while(call)
        {
-               if (call->bchannel_handle == handle)
+               if (call->ref == ref && call->ref_was_assigned == assigned)
                        break;
                call = call->next;
        }
        return(call);
 }
-#endif
 
 void free_call(struct chan_call *call)
 {
@@ -300,7 +282,6 @@ struct chan_call *alloc_call(void)
        return(*callp);
 }
 
-
 unsigned short new_bridge_id(void)
 {
        struct chan_call *call;
@@ -948,7 +929,7 @@ static void lcr_in_disconnect(struct chan_call *call, int message_type, union pa
 }
 
 /*
- * incoming setup acknowledge from LCR
+ * incoming release from LCR
  */
 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
 {
@@ -1199,6 +1180,7 @@ int receive_message(int message_type, unsigned int ref, union parameter *param)
                        call->state = CHAN_LCR_STATE_IN_PREPARE;
                        /* set ref */
                        call->ref = ref;
+                       call->ref_was_assigned = 1;
                        /* wait for setup (or release from asterisk) */
                } else
                {
@@ -1214,6 +1196,7 @@ int receive_message(int message_type, unsigned int ref, union parameter *param)
                        }
                        /* store new ref */
                        call->ref = ref;
+                       call->ref_was_assigned = 1;
                        /* send pending setup info */
                        if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
                                send_setup_to_lcr(call);
@@ -1428,7 +1411,6 @@ int handle_socket(void)
 int open_socket(void)
 {
        int ret;
-       char *socket_name = SOCKET_NAME;
        int conn;
        struct sockaddr_un sock_address;
        unsigned int on = 1;
@@ -1444,7 +1426,7 @@ int open_socket(void)
        /* set socket address and name */
        memset(&sock_address, 0, sizeof(sock_address));
        sock_address.sun_family = PF_UNIX;
-       strcpy(sock_address.sun_path, socket_name);
+       sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
 
        /* connect socket */
        if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
@@ -2070,6 +2052,7 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                        
                        /*start music onhold*/
                        ast_moh_start(ast,data,ast->musicclass);
+                       call->on_hold = 1;
                         break;
                 case AST_CONTROL_UNHOLD:
                        CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
@@ -2080,8 +2063,14 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
 
                        /*stop moh*/
                        ast_moh_stop(ast);
+                       call->on_hold = 0;
                        break;
-
+#ifdef AST_CONTROL_SRCUPDATE
+               case AST_CONTROL_SRCUPDATE:
+                       CDEBUG(call, ast, "Received indicate AST_CONTROL_SRCUPDATE from Asterisk.\n");
+                        res = -1;
+                        break;
+#endif
                 default:
                        CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
                         res = -1;
@@ -2096,21 +2085,25 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
 /*
  * fixup asterisk
  */
-static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *newast)
+static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
 {
         struct chan_call *call;
 
+       if (!ast) {
+               return -1;
+       }
+
        ast_mutex_lock(&chan_lock);
-       call = oldast->tech_pvt;
+       call = ast->tech_pvt;
        if (!call) {
-               CERROR(NULL, oldast, "Received fixup from Asterisk, but no call instance exists.\n");
+               CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
                ast_mutex_unlock(&chan_lock);
                return -1;
        }
 
-       CDEBUG(call, oldast, "Received fixup from Asterisk.\n");
-       call->ast = newast;
-       ast_mutex_lock(&chan_lock);
+       CDEBUG(call, ast, "Received fixup from Asterisk.\n");
+       call->ast = ast;
+       ast_mutex_unlock(&chan_lock);
        return 0;
 }
 
@@ -2204,6 +2197,30 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
                CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
                lcr_answer(ast2);
        }
+
+       /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
+          so let's do it for them. Hmpf.
+       */
+
+       if (call1->on_hold) {
+               union parameter newparam;
+
+               memset(&newparam, 0, sizeof(union parameter));
+               newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
+               send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
+
+               call1->on_hold = 0;
+       }
+
+       if (call2->on_hold) {
+               union parameter newparam;
+
+               memset(&newparam, 0, sizeof(union parameter));
+               newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
+               send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
+
+               call2->on_hold = 0;
+       }
        
        ast_mutex_unlock(&chan_lock);