--- ../asterisk-1.4.1/channels/chan_cellphone.c	2007-03-08 22:55:34.000000000 +0100
+++ chan_cellphone.c	2007-03-09 03:00:01.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";
 
@@ -143,11 +155,19 @@
 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";
 
@@ -180,8 +200,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);
@@ -201,8 +225,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,
@@ -456,9 +484,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->state == CEL_STATE_INCOMING || pvt->state == CEL_STATE_OUTGOING || pvt->state == CEL_STATE_DIAL1) {
 		sco_flush(pvt);
@@ -493,14 +529,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;
@@ -554,13 +596,25 @@
 	int r;
 
 	if (!pvt->owner)
+#ifdef HAS_ASTERISK_1_2
+		return NULL;
+#else
 		return &ast_null_frame;
+#endif
 
 	if (pvt->state == CEL_STATE_HANGUP)
+#ifdef HAS_ASTERISK_1_2
+		return NULL;
+#else
 		return &ast_null_frame;
+#endif
 
 	if (pvt->sco_socket == -1)
+#ifdef HAS_ASTERISK_1_2
+		return NULL;
+#else
 		return &ast_null_frame;
+#endif
 
 	pvt->fr.frametype = AST_FRAME_VOICE;
 	pvt->fr.subclass = AST_FORMAT_SLINEAR;
@@ -584,9 +638,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 {
@@ -594,8 +656,11 @@
 			ast_log(LOG_DEBUG, "cel_read() read short frame. (%d)\n", r);
 	}
 
+#ifdef HAS_ASTERISK_1_2
+	return NULL;
+#else
 	return &ast_null_frame;
-
+#endif
 }
 
 /*
@@ -693,8 +758,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;
@@ -708,10 +783,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;
 	}
 
@@ -1270,7 +1352,11 @@
 static int start_monitor(struct cel_pvt *pvt)
 {
 
+#ifdef HAS_ASTERISK_1_2
+	if (ast_pthread_create(&pvt->monitor_thread, NULL, do_monitor, pvt) < 0) {
+#else
 	if (ast_pthread_create_background(&pvt->monitor_thread, NULL, do_monitor, pvt) < 0) {
+#endif
 		pvt->monitor_thread = AST_PTHREADT_NULL;
 		return 0;
 	}
@@ -1388,9 +1474,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");
@@ -1428,7 +1522,11 @@
 			port = ast_variable_retrieve(cfg, cat, "port");
 			context = ast_variable_retrieve(cfg, cat, "context");
 			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
 					ast_copy_string(pvt->id, cat, sizeof(pvt->id));
 					ast_copy_string(pvt->bdaddr, address, sizeof(pvt->bdaddr));
 					if (context)
@@ -1465,14 +1563,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;
@@ -1518,7 +1622,11 @@
 
 }
 
+#ifdef HAS_ASTERISK_1_2
+int load_module(void)
+#else
 static int load_module(void)
+#endif
 {
 
 	int dev_id, s;
@@ -1528,26 +1636,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]));
@@ -1562,8 +1694,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
