--- tmp/channels/chan_cellphone.c	2007-03-22 12:21:41.000000000 +0100
+++ chan_cellphone.c	2007-03-22 12:33:45.000000000 +0100
@@ -69,6 +69,18 @@
 
 #define CEL_CONFIG "cellphone.conf"
 
+#ifdef HAS_ASTERISK_1_2
+AST_MUTEX_DEFINE_STATIC(randomlock);
+long int ast_random(void)
+{
+	long int res;
+	ast_mutex_lock(&randomlock);
+	res = random();
+	ast_mutex_unlock(&randomlock);
+	return res;
+}
+#endif
+
 static int prefformat = AST_FORMAT_SLINEAR;
 static char type[] = "CELL";
 
@@ -147,15 +159,27 @@
 static pthread_t sco_listener_thread = AST_PTHREADT_NULL;
 
 /* CLI stuff */
+#ifdef HAS_ASTERISK_1_2
+static char show_usage[] =
+#else
 static const char show_usage[] =
+#endif
 "Usage: cell show devices\n" 
 "       Shows the state of Bluetooth Cell / Mobile devices.\n";
 
+#ifdef HAS_ASTERISK_1_2
+static char search_usage[] =
+#else
 static const char search_usage[] =
+#endif
 "Usage: cell search\n" 
 "       Searches for Bluetooth Cell / Mobile devices in range.\n";
 
+#ifdef HAS_ASTERISK_1_2
+static char rfcomm_usage[] = 
+#else
 static const char rfcomm_usage[] =
+#endif
 "Usage: cell rfcomm command\n" 
 "       Send command to the rfcomm port.\n";
 
@@ -190,8 +214,12 @@
 static int cel_call(struct ast_channel *ast, char *dest, int timeout);
 static int cel_hangup(struct ast_channel *ast);
 static int cel_answer(struct ast_channel *ast);
+#ifdef HAS_ASTERISK_1_2
+static int cel_digit_end(struct ast_channel *c, char digit);
+#else
 static int cel_digit_begin(struct ast_channel *ast, char digit);
 static int cel_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
+#endif
 static struct ast_frame *cel_read(struct ast_channel *ast);
 static int cel_write(struct ast_channel *ast, struct ast_frame *frame);
 static int cel_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
@@ -211,8 +239,12 @@
 	.call = cel_call,
 	.hangup = cel_hangup,
 	.answer = cel_answer,
+#if HAS_ASTERISK_1_2
+	.send_digit = cel_digit_end,
+#else
 	.send_digit_begin = cel_digit_begin,
 	.send_digit_end = cel_digit_end,
+#endif
 	.read = cel_read,
 	.write = cel_write,
 	.fixup = cel_fixup,
@@ -508,9 +540,17 @@
 	if (option_debug)
 		ast_log(LOG_DEBUG, "Hanging up device %s.\n", pvt->id);
 
+#ifdef HAS_ASTERISK_1_2
+	ast_mutex_lock(&ast->lock);
+#else
 	ast_channel_lock(ast);
+#endif
 	ast->fds[0] = -1;
+#ifdef HAS_ASTERISK_1_2
+	ast_mutex_unlock(&ast->lock);
+#else
 	ast_channel_unlock(ast);
+#endif
 
 	if (pvt->type == CEL_TYPE_HEADSET && pvt->sco_socket != -1) {
 		close(pvt->sco_socket);
@@ -549,14 +589,20 @@
 
 }
 
+#ifndef HAS_ASTERISK_1_2
 static int cel_digit_begin(struct ast_channel *chan, char digit)
 {
 
 	return 0;
 
 }
+#endif
 
+#ifdef HAS_ASTERISK_1_2
+static int cel_digit_end(struct ast_channel *ast, char digit)
+#else
 static int cel_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
+#endif
 {
 
 	struct cel_pvt *pvt;
@@ -634,9 +680,17 @@
 			close(pvt->sco_socket);
 			pvt->sco_socket = -1;
 			if (pvt->owner) {
+#ifdef HAS_ASTERISK_1_2
+				ast_mutex_lock(&pvt->owner->lock);
+#else
 				ast_channel_lock(pvt->owner);
+#endif
 				pvt->owner->fds[0] = -1;
+#ifdef HAS_ASTERISK_1_2
+				ast_mutex_unlock(&pvt->owner->lock);
+#else
 				ast_channel_unlock(pvt->owner);
+#endif
 			}
 		}
 	} else {
@@ -741,8 +795,18 @@
 
 	struct ast_channel *chn;
 
+#ifdef HAS_ASTERISK_1_2
+	chn = ast_channel_alloc(1);
+#else
 	chn = ast_channel_alloc(1, state, 0, 0, "CELL/%s-%04lx", pvt->id, ast_random() & 0xffff);
+#endif
 	if (chn) {
+#ifdef HAS_ASTERISK_1_2
+		snprintf(chn->name, sizeof(chn->name) - 1, "CELL/%s-%04lx", pvt->id, ast_random() & 0xffff);
+		chn->_state = state;
+		chn->type = "CELL";
+#endif
+
 		chn->tech = &cel_tech;
 		chn->nativeformats = prefformat;
 		chn->rawreadformat = prefformat;
@@ -767,10 +831,17 @@
 			chn->fds[0] = pvt->sco_socket;
 		ast_copy_string(chn->context, pvt->context, sizeof(chn->context));
 		ast_copy_string(chn->exten, "s", sizeof(chn->exten));
+#ifndef HAS_ASTERISK_1_2
 		ast_string_field_set(chn, language, "en");
+#endif
 		if (cid_num)
+#ifdef HAS_ASTERISK_1_2
+			chn->cid.cid_num = ast_strdupa(cid_num);
+		chn->cid.cid_name = ast_strdupa(pvt->id);
+#else
 			chn->cid.cid_num = ast_strdup(cid_num);
 		chn->cid.cid_name = ast_strdup(pvt->id);
+#endif
 		pvt->owner = chn;
 
 	}
@@ -1389,9 +1460,17 @@
 				if (strstr(buf, "AT+CKPD=")) {
 					pvt->sco_socket = sco_connect(pvt->bdaddr);
 					if (pvt->sco_socket > -1) {
+#ifdef HAS_ASTERISK_1_2
+						ast_mutex_lock(&pvt->owner->lock);
+#else
 						ast_channel_lock(pvt->owner);
+#endif
 						pvt->owner->fds[0] = pvt->sco_socket;
+#ifdef HAS_ASTERISK_1_2
+						ast_mutex_unlock(&pvt->owner->lock);
+#else
 						ast_channel_unlock(pvt->owner);
+#endif
 						ast_queue_control(pvt->owner, AST_CONTROL_ANSWER);
 						pvt->state = CEL_STATE_INCOMING;
 						rfcomm_write(pvt, "\r\n+VGS=13\r\n");
@@ -1438,12 +1517,20 @@
 {
 
 	if (pvt->type == CEL_TYPE_PHONE) {
+#ifdef HAS_ASTERISK_1_2
+		if (ast_pthread_create(&pvt->monitor_thread, NULL, do_monitor_phone, pvt) < 0) {
+#else
 		if (ast_pthread_create_background(&pvt->monitor_thread, NULL, do_monitor_phone, pvt) < 0) {
+#endif
 			pvt->monitor_thread = AST_PTHREADT_NULL;
 			return 0;
 		}
 	} else {
+#ifdef HAS_ASTERISK_1_2
+		if (ast_pthread_create(&pvt->monitor_thread, NULL, do_monitor_headset, pvt) < 0) {
+#else
 		if (ast_pthread_create_background(&pvt->monitor_thread, NULL, do_monitor_headset, pvt) < 0) {
+#endif
 			pvt->monitor_thread = AST_PTHREADT_NULL;
 			return 0;
 		}
@@ -1558,9 +1645,17 @@
 					close(pvt->sco_socket);
 				pvt->sco_socket = ns;
 				if (pvt->owner) {
+#ifdef HAS_ASTERISK_1_2
+					ast_mutex_lock(&pvt->owner->lock);
+#else
 					ast_channel_lock(pvt->owner);
+#endif
 					pvt->owner->fds[0] = ns;
+#ifdef HAS_ASTERISK_1_2
+					ast_mutex_unlock(&pvt->owner->lock);
+#else
 					ast_channel_unlock(pvt->owner);
+#endif
 				}
 			} else if (option_debug)
 				ast_log(LOG_DEBUG, "Could not find device for incoming Audio Connection.\n");
@@ -1599,7 +1694,11 @@
 			context = ast_variable_retrieve(cfg, cat, "context");
 			type = ast_variable_retrieve(cfg, cat, "type");
 			if (address && port) {
+#ifdef HAS_ASTERISK_1_2
+				if ((pvt = malloc(sizeof(struct cel_pvt)))) {
+#else
 				if ((pvt = ast_malloc(sizeof(struct cel_pvt)))) {
+#endif
 					if (type && !strcmp(type, "headset"))
 						pvt->type = CEL_TYPE_HEADSET;
 					else
@@ -1638,14 +1737,20 @@
 
 }
 
+#ifndef HAS_ASTERISK_1_2
 static int reload_module(void)
 {
 
 	return 0;
 
 }
+#endif
 
+#ifdef HAS_ASTERISK_1_2
+int unload_module(void)
+#else
 static int unload_module(void)
+#endif
 {
 
 	struct cel_pvt *pvt;
@@ -1691,7 +1796,11 @@
 
 }
 
+#ifdef HAS_ASTERISK_1_2
+int load_module(void)
+#else
 static int load_module(void)
+#endif
 {
 
 	int dev_id, s;
@@ -1701,26 +1810,50 @@
 	s = hci_open_dev(dev_id);
 	if (dev_id < 0 || s < 0) {
 		ast_log(LOG_ERROR, "No Bluetooth device found. Not loading module.\n");
+#ifdef HAS_ASTERISK_1_2
+		return 0;
+#else
 		return AST_MODULE_LOAD_DECLINE;
+#endif
 	}
 	hci_close_dev(s);
 
 	if (!cel_load_config()) {
 		ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", CEL_CONFIG);
+#ifdef HAS_ASTERISK_1_2
+		return 0;
+#else
 		return AST_MODULE_LOAD_DECLINE;
+#endif
 	}
 
 	/* Spin the discovery thread */
+#ifdef HAS_ASTERISK_1_2
+	if (ast_pthread_create(&discovery_thread, NULL, do_discovery, NULL) < 0) {
+#else
 	if (ast_pthread_create_background(&discovery_thread, NULL, do_discovery, NULL) < 0) {
+#endif
 		ast_log(LOG_ERROR, "Unable to create discovery thread.\n");
+#ifdef HAS_ASTERISK_1_2
+		return -1;
+#else
 		return AST_MODULE_LOAD_DECLINE;
+#endif
 	}
 	/* Spin the sco listener thread */
+#ifdef HAS_ASTERISK_1_2
+	if (ast_pthread_create(&sco_listener_thread, NULL, do_sco_listen, NULL) < 0) {
+#else
 	if (ast_pthread_create_background(&sco_listener_thread, NULL, do_sco_listen, NULL) < 0) {
+#endif
 		ast_log(LOG_ERROR, "Unable to create sco listener thread.\n");
 		pthread_cancel(discovery_thread);
 		pthread_join(discovery_thread, NULL);
+#ifdef HAS_ASTERISK_1_2
+		return -1;
+#else
 		return AST_MODULE_LOAD_DECLINE;
+#endif
 	}
 
 	ast_cli_register_multiple(cel_cli, sizeof(cel_cli) / sizeof(cel_cli[0]));
@@ -1736,8 +1869,33 @@
 	return 0;
 }
 
+#ifdef HAS_ASTERISK_1_2
+static char *ccdesc = "Bluetooth Cellphone Driver";
+char *description()
+{
+	return ccdesc;
+}
+
+static int usecnt = 0;
+AST_MUTEX_DEFINE_STATIC(usecnt_lock);
+int usecount()
+{
+        int res;
+	ast_mutex_lock(&usecnt_lock);
+	res = usecnt;
+	ast_mutex_unlock(&usecnt_lock);
+	return res;
+}
+
+char *key()
+{
+	return ASTERISK_GPL_KEY;
+}
+							
+#else
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Bluetooth Cellphone Driver",
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload_module,
 );
+#endif
