don't die if skinny cannot figure out hostname
[asterisk/asterisk.git] / channels / chan_skinny.c
index f5e7659..e097499 100755 (executable)
@@ -708,8 +708,6 @@ static struct skinny_subchannel *find_subchannel_by_name(char *dest)
        char *at;
        char *device;
        
-       printf("dest: %s\n", dest);
-
        strncpy(line, dest, sizeof(line) - 1);
        at = strchr(line, '@');
        if (!at) {
@@ -719,10 +717,6 @@ static struct skinny_subchannel *find_subchannel_by_name(char *dest)
        *at = '\0';
        at++;
        device = at;
-
-       printf("line: %s\n", line);
-       printf("device: %s\n", device);
-       
        ast_mutex_lock(&devicelock);
        d = devices;
        while(d) {
@@ -736,16 +730,11 @@ static struct skinny_subchannel *find_subchannel_by_name(char *dest)
                                /* Search for the right line */
                                if (!strcasecmp(l->name, line)) {
                                        ast_mutex_unlock(&devicelock);
-                                       if (skinnydebug) {
-                                               printf("Found line: %s\n", l->name);
-                                       }
                                        return l->sub;
                                }
-                               printf("line cycle\n");
                                l = l->next;
                        }
                }
-               printf("device cycle\n");
                d = d->next;
        }
        /* Device not found*/
@@ -1438,6 +1427,15 @@ static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
     struct skinny_subchannel *sub;
        struct skinnysession *session;
        
+       sub = ast->pvt->pvt;
+    l = sub->parent;
+       session = l->parent->session;
+
+       if (!l->parent->registered) {
+               ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
+               return -1;
+       }
+       
        if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
                ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast->name);
                return -1;
@@ -1446,14 +1444,10 @@ static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
     if (skinnydebug) {
         ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
     }
-       
-       sub = ast->pvt->pvt;
-    l = sub->parent;
-       session = l->parent->session;
 
        if (l->dnd) {
                ast_queue_control(ast, AST_CONTROL_BUSY, 0);
-               return 0;
+               return -1;
        }
    
        switch (l->hookstate) {
@@ -1525,10 +1519,18 @@ static int skinny_hangup(struct ast_channel *ast)
         ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
         return 0;
     }
-    if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_OFFHOOK)) {
-               sub->parent->hookstate = SKINNY_ONHOOK;
-               transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
-               transmit_speaker_mode(s, SKINNY_SPEAKEROFF); 
+
+       if (l->parent->registered) {
+                   if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_OFFHOOK)) {
+                       sub->parent->hookstate = SKINNY_ONHOOK;
+                       transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
+                       transmit_speaker_mode(s, SKINNY_SPEAKEROFF); 
+               } else if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_ONHOOK)) {
+                       transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
+                       transmit_speaker_mode(s, SKINNY_SPEAKEROFF); 
+                       transmit_ringer_mode(s, SKINNY_RING_OFF);
+                       transmit_tone(s, SKINNY_SILENCE);
+               } 
        }
     ast_mutex_lock(&sub->lock);
     sub->owner = NULL;
@@ -1637,11 +1639,10 @@ static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan
 
 static int skinny_senddigit(struct ast_channel *ast, char digit)
 {
-//     struct skinny_subchannel *sub = ast->pvt->pvt;
-       char tmp[2];
-       tmp[0] = digit;
-       tmp[1] = '\0';
-//     transmit_notify_request(sub, tmp);
+       struct skinny_subchannel *sub = ast->pvt->pvt;
+//     int tmp;
+//     sprintf(tmp, "%d", digit);  // not right
+//     transmit_tone(sub->parent->parent->session, digit);
        return -1;
 }
 
@@ -2137,6 +2138,7 @@ static int handle_message(skinny_req *req, struct skinnysession *s)
                }
                f.subclass  = d;  
                f.src = "skinny";
+
                sub = find_subchannel_by_line(s->device->lines);                
 
                if (sub->owner) {
@@ -2471,14 +2473,14 @@ static int reload_config(void)
        
        if (gethostname(ourhost, sizeof(ourhost))) {
                ast_log(LOG_WARNING, "Unable to get hostname, Skinny disabled\n");
-               return 1;
+               return 0;
        }
        cfg = ast_load(config);
 
        /* We *must* have a config file otherwise stop immediately */
        if (!cfg) {
                ast_log(LOG_NOTICE, "Unable to load config %s, Skinny disabled\n", config);
-               return 1;
+               return 0;
        }
 
        /* load the general section */
@@ -2634,9 +2636,13 @@ void delete_devices(void)
 
 int reload(void)
 {
+#if 0
+// XXX Causes Seg
+
        delete_devices();
        reload_config();
        restart_monitor();
+#endif
        return 0;
 }