--- chan_mobile.c 2010-10-18 19:15:24.000000000 +0200 +++ asterisk-1.8.0/addons/chan_mobile.c 2010-12-03 12:40:27.182789002 +0100 @@ -194,6 +194,14 @@ " Dest - destination\n" " Message - text of the message\n"; +static char *app_mblsendhexsms = "MobileSendHEXSMS"; +static char *mblsendhexsms_synopsis = "MobileSendHEXSMS(Device,Dest,Message)"; +static char *mblsendhexsms_desc = +"MobileSendHexSms(Device,Dest,Message)\n" +" Device - Id of device from mobile.conf\n" +" Dest - destination\n" +" Message - text of the message (HEX representation 09F9A817...)\n"; + static struct ast_channel *mbl_new(int state, struct mbl_pvt *pvt, char *cid_num, const struct ast_channel *requestor); static struct ast_channel *mbl_request(const char *type, format_t format, @@ -375,6 +383,9 @@ static int hfp_send_cnmi(struct hfp_pvt *hfp); static int hfp_send_cmgr(struct hfp_pvt *hfp, int index); static int hfp_send_cmgs(struct hfp_pvt *hfp, const char *number); +static int hfp_send_cscs(struct hfp_pvt *hfp, const char *encoding); +static int hfp_send_csmp(struct hfp_pvt *hfp, const char *params); + static int hfp_send_sms_text(struct hfp_pvt *hfp, const char *message); static int hfp_send_chup(struct hfp_pvt *hfp); static int hfp_send_atd(struct hfp_pvt *hfp, const char *number); @@ -416,6 +427,8 @@ AT_D, AT_CHUP, AT_CKPD, + AT_CSCS, + AT_CSMP, AT_CMGS, AT_VGM, AT_VGS, @@ -792,7 +805,108 @@ goto e_unlock_pvt; } - message = ast_strdup(args.message); + if (hfp_send_cscs(pvt->hfp, "GSM") || msg_queue_push_data(pvt, AT_OK, AT_CSCS, NULL)) { + ast_log(LOG_ERROR,"Bluetooth device %s doesn't support GSM SMS -- SMS will not be sent \n", args.device); + goto e_unlock_pvt; + } + + + if (hfp_send_csmp(pvt->hfp, "17,167,0,0") || msg_queue_push_data(pvt, AT_OK, AT_CSMP, NULL)) { + ast_log(LOG_ERROR,"Bluetooth device %s doesn't support CSMP params -- SMS will not be sent.\n", args.device); + goto e_unlock_pvt; + } + + message=ast_strdup(args.message); + + if (hfp_send_cmgs(pvt->hfp, args.dest) + || msg_queue_push_data(pvt, AT_SMS_PROMPT, AT_CMGS, message)) { + + ast_log(LOG_ERROR, "[%s] problem sending SMS message\n", pvt->id); + goto e_free_message; + } + + ast_mutex_unlock(&pvt->lock); + + return 0; + +e_free_message: + ast_free(message); +e_unlock_pvt: + ast_mutex_unlock(&pvt->lock); +e_return: + return -1; +} + +static int mbl_sendhexsms_exec(struct ast_channel *ast, const char *data) +{ + + struct mbl_pvt *pvt; + char *parse, *message; + + AST_DECLARE_APP_ARGS(args, + AST_APP_ARG(device); + AST_APP_ARG(dest); + AST_APP_ARG(message); + ); + + if (ast_strlen_zero(data)) + return -1; + + parse = ast_strdupa(data); + + AST_STANDARD_APP_ARGS(args, parse); + + if (ast_strlen_zero(args.device)) { + ast_log(LOG_ERROR,"NULL device for message -- SMS will not be sent.\n"); + return -1; + } + + if (ast_strlen_zero(args.dest)) { + ast_log(LOG_ERROR,"NULL destination for message -- SMS will not be sent.\n"); + return -1; + } + + if (ast_strlen_zero(args.message)) { + ast_log(LOG_ERROR,"NULL Message to be sent -- SMS will not be sent.\n"); + return -1; + } + + AST_RWLIST_RDLOCK(&devices); + AST_RWLIST_TRAVERSE(&devices, pvt, entry) { + if (!strcmp(pvt->id, args.device)) + break; + } + AST_RWLIST_UNLOCK(&devices); + + if (!pvt) { + ast_log(LOG_ERROR,"Bluetooth device %s wasn't found in the list -- SMS will not be sent.\n", args.device); + goto e_return; + } + + ast_mutex_lock(&pvt->lock); + if (!pvt->connected) { + ast_log(LOG_ERROR,"Bluetooth device %s wasn't connected -- SMS will not be sent.\n", args.device); + goto e_unlock_pvt; + } + + if (!pvt->has_sms) { + ast_log(LOG_ERROR,"Bluetooth device %s doesn't handle SMS -- SMS will not be sent.\n", args.device); + goto e_unlock_pvt; + } + + + message=ast_strdup(args.message); + + if (hfp_send_cscs(pvt->hfp, "HEX") || msg_queue_push_data(pvt, AT_OK, AT_CSCS, NULL)) { + ast_log(LOG_ERROR,"Bluetooth device %s doesn't support GSM SMS -- SMS will not be sent \n", args.device); + goto e_unlock_pvt; + } + + + if (hfp_send_csmp(pvt->hfp, "17,167,0,8") || msg_queue_push_data(pvt, AT_OK, AT_CSMP, NULL)) { + ast_log(LOG_ERROR,"Bluetooth device %s doesn't support CSMP params -- SMS will not be sent.\n", args.device); + goto e_unlock_pvt; + } if (hfp_send_cmgs(pvt->hfp, args.dest) || msg_queue_push_data(pvt, AT_SMS_PROMPT, AT_CMGS, message)) { @@ -1014,8 +1128,8 @@ ast_mutex_lock(&pvt->lock); ast_channel_set_fd(ast, 0, -1); - close(pvt->sco_socket); - pvt->sco_socket = -1; +// close(pvt->sco_socket); +// pvt->sco_socket = -1; if (pvt->needchup) { hfp_send_chup(pvt->hfp); @@ -2090,6 +2204,10 @@ return "AT+CKPD"; case AT_CMGS: return "AT+CMGS"; + case AT_CSMP: + return "AT+CSMP"; + case AT_CSCS: + return "AT+CSCS"; case AT_VGM: return "AT+VGM"; case AT_VGS: @@ -2245,13 +2363,16 @@ } case 2: /* mark the start of the number */ if (from_number) { - *from_number = &buf[i]; + *from_number = &buf[i+1]; + ast_debug(2, "[%s] found beginning of number '%s'\n", hfp->owner->id, &buf[i]); + i+=1; state++; } /* fall through */ case 3: /* search for the end of the number (") */ if (buf[i] == '"') { buf[i] = '\0'; + ast_debug(2, "[%s] found end of number '%s'\n", hfp->owner->id, *from_number); state++; } break; @@ -2535,14 +2656,41 @@ } /*! + * \brief Issue AT+CSCS setting text representation mode + * \param hfp an hfp_pvt struct + * \param encoding should be "HEX" or "GSM" + */ +static int hfp_send_cscs(struct hfp_pvt *hfp, const char *encoding) +{ + char cmd[64]; + snprintf(cmd, sizeof(cmd), "AT+CSCS=\"%s\"\r", encoding); + return rfcomm_write(hfp->rsock, cmd); +} + + +/*! + * \brief Issue AT+CSMP setting SMS parameters + * \param hfp an hfp_pvt struct + * \param params should be a valid CSMP string like "17,167,0,8" + */ +static int hfp_send_csmp(struct hfp_pvt *hfp, const char *param) +{ + char cmd[64]; + snprintf(cmd, sizeof(cmd), "AT+CSMP=%s\r", param); + return rfcomm_write(hfp->rsock, cmd); +} + + +/*! * \brief Send the text of an SMS message. * \param hfp an hfp_pvt struct * \param message the text of the message */ static int hfp_send_sms_text(struct hfp_pvt *hfp, const char *message) { - char cmd[162]; - snprintf(cmd, sizeof(cmd), "%.160s\x1a", message); + char *cmd; + cmd = (char *)malloc(strlen(message)+2); + sprintf(cmd, "%s\x1a", message); return rfcomm_write(hfp->rsock, cmd); } @@ -3267,6 +3415,12 @@ case AT_CHUP: ast_debug(1, "[%s] successful hangup\n", pvt->id); break; + case AT_CSCS: + ast_debug(1, "[%s] successful CSCS\n", pvt->id); + break; + case AT_CSMP: + ast_debug(1, "[%s] successful CSMP\n", pvt->id); + break; case AT_CMGS: ast_debug(1, "[%s] successfully sent sms message\n", pvt->id); pvt->outgoing_sms = 0; @@ -3372,6 +3526,14 @@ ast_debug(1, "[%s] error sending sms message\n", pvt->id); pvt->outgoing_sms = 0; break; + case AT_CSCS: + ast_debug(1, "[%s] error sending sms message (CSCS)\n", pvt->id); + pvt->outgoing_sms = 0; + break; + case AT_CSMP: + ast_debug(1, "[%s] error sending sms message (CSMP)\n", pvt->id); + pvt->outgoing_sms = 0; + break; case AT_VTS: ast_debug(1, "[%s] error sending digit\n", pvt->id); break; @@ -4478,6 +4640,7 @@ ast_cli_unregister_multiple(mbl_cli, sizeof(mbl_cli) / sizeof(mbl_cli[0])); ast_unregister_application(app_mblstatus); ast_unregister_application(app_mblsendsms); + ast_unregister_application(app_mblsendhexsms); /* signal everyone we are unloading */ set_unloading(); @@ -4574,6 +4737,7 @@ ast_cli_register_multiple(mbl_cli, sizeof(mbl_cli) / sizeof(mbl_cli[0])); ast_register_application(app_mblstatus, mbl_status_exec, mblstatus_synopsis, mblstatus_desc); ast_register_application(app_mblsendsms, mbl_sendsms_exec, mblsendsms_synopsis, mblsendsms_desc); + ast_register_application(app_mblsendhexsms, mbl_sendhexsms_exec, mblsendhexsms_synopsis, mblsendhexsms_desc); return AST_MODULE_LOAD_SUCCESS;