Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Rename channel functions
Browse files Browse the repository at this point in the history
Improve USSD sending.
  • Loading branch information
RoEdAl committed Dec 21, 2023
1 parent 119a1f7 commit 977d623
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 53 deletions.
24 changes: 10 additions & 14 deletions src/at_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ int at_enqueue_sms(struct cpvt* cpvt, const char* destination, const char* msg,
if (pdus_len > 1) {
ast_json_object_set(report, "parts", ast_json_integer_create(pdus_len));
}
start_local_report_channel(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, destination, NULL, NULL, 1, report);
channel_start_local_report(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, destination, NULL, NULL, 1, report);

return 0;
}
Expand All @@ -610,7 +610,7 @@ int at_enqueue_ussd(struct cpvt* cpvt, const char* code, int gsm7)
return -1;
}

ast_str_append(&buf, 0, at_cmd);
ast_str_set(&buf, 0, at_cmd);

const size_t code_len = strlen(code);

Expand Down Expand Up @@ -663,7 +663,7 @@ int at_enqueue_ussd(struct cpvt* cpvt, const char* code, int gsm7)
at_queue_cmd_t cmd = ATQ_CMD_DECLARE_DYN(CMD_AT_CUSD);

cmd.length = ast_str_strlen(buf);
cmd.data = ast_strdup(ast_str_buffer(buf));
cmd.data = ast_strndup(ast_str_buffer(buf), ast_str_strlen(buf));
if (!cmd.data) {
chan_quectel_err = E_MALLOC;
return -1;
Expand Down Expand Up @@ -843,13 +843,10 @@ int at_enqueue_dial(struct cpvt* cpvt, const char* number, int clir)
*/
int at_enqueue_answer(struct cpvt* cpvt)
{
DECLARE_AT_CMD(a, "A");
DECLARE_AT_CMD(chld, "+CHLD=2%d");
DECLARE_AT_CMDNT(a, "A");
DECLARE_AT_CMDNT(chld, "+CHLD=2%d");

at_queue_cmd_t cmds[] = {
ATQ_CMD_DECLARE_DYN(CMD_AT_A),
};
unsigned int cnt = ARRAY_LEN(cmds);
at_queue_cmd_t cmd = ATQ_CMD_DECLARE_DYN(CMD_AT_A);
const char* fmt;

switch (cpvt->state) {
Expand All @@ -858,10 +855,9 @@ int at_enqueue_answer(struct cpvt* cpvt)
break;

case CALL_STATE_WAITING:
cmds[0].cmd = CMD_AT_CHLD_2x;
fmt = AT_CMD(chld);
cmd.cmd = CMD_AT_CHLD_2x;
fmt = AT_CMD(chld);
/* no need CMD_AT_DDSETEX in this case? */
cnt--;
break;

default:
Expand All @@ -870,12 +866,12 @@ int at_enqueue_answer(struct cpvt* cpvt)
return -1;
}

if (at_fill_generic_cmd(&cmds[0], fmt, cpvt->call_idx)) {
if (at_fill_generic_cmd(&cmd, fmt, cpvt->call_idx)) {
chan_quectel_err = E_CMD_FORMAT;
return -1;
}

if (at_queue_insert(cpvt, cmds, cnt, 1u)) {
if (at_queue_insert(cpvt, &cmd, 1u, 1)) {
chan_quectel_err = E_QUEUE;
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/at_read.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ int at_wait(int fd, int* ms);
ssize_t at_read(const char* dev, int fd, struct ringbuffer* rb);
void at_clean_data(const char* dev, int fd, struct ringbuffer* const rb);

size_t at_get_iov_size(const struct iovec* iov);
size_t at_get_iov_size_n(const struct iovec* iov, int iovcnt);
size_t attribute_const at_get_iov_size(const struct iovec* iov);
size_t attribute_const at_get_iov_size_n(const struct iovec* iov, int iovcnt);

size_t at_combine_iov(struct ast_str* const, const struct iovec* const, int);

Expand Down
16 changes: 8 additions & 8 deletions src/at_response.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,12 @@ static int at_response_error(struct pvt* const pvt, const at_res_t at_res, const
case CMD_AT_A:
case CMD_AT_CHLD_2x:
at_err_response_err(pvt, ecmd, "Answer failed for call idx:%d", task->cpvt->call_idx);
queue_hangup(task->cpvt->channel, AST_CAUSE_CALL_REJECTED);
channel_enqueue_hangup(task->cpvt->channel, AST_CAUSE_CALL_REJECTED);
break;

case CMD_AT_CHLD_3:
at_err_response_err(pvt, ecmd, "Can't begin conference call idx:%d", task->cpvt->call_idx);
queue_hangup(task->cpvt->channel, AST_CAUSE_CALL_REJECTED);
channel_enqueue_hangup(task->cpvt->channel, AST_CAUSE_CALL_REJECTED);
break;

case CMD_AT_CLIR:
Expand Down Expand Up @@ -971,7 +971,7 @@ static int at_response_error(struct pvt* const pvt, const at_res_t at_res, const
static int start_pbx(struct pvt* const pvt, const char* const number, const int call_idx, const call_state_t state)
{
/* TODO: pass also Subscriber number or other DID info for exten */
struct ast_channel* channel = new_channel(pvt, AST_STATE_RING, number, call_idx, CALL_DIR_INCOMING, state,
struct ast_channel* channel = channel_new(pvt, AST_STATE_RING, number, call_idx, CALL_DIR_INCOMING, state,
pvt->has_subscriber_number ? pvt->subscriber_number : CONF_SHARED(pvt, exten), NULL, NULL, 0);

if (!channel) {
Expand Down Expand Up @@ -1518,7 +1518,7 @@ static int at_response_cmgs(struct pvt* const pvt, const struct ast_str* const r
if (partcnt > 1) {
ast_json_object_set(report, "parts", ast_json_integer_create(partcnt));
}
start_local_report_channel(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, ast_str_buffer(dst), NULL, NULL, 1, report);
channel_start_local_report(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, ast_str_buffer(dst), NULL, NULL, 1, report);
}
return 0;
}
Expand All @@ -1535,7 +1535,7 @@ static int at_response_cmgs_error(struct pvt* const pvt, const at_queue_task_t*
ast_json_object_set(report, "info", ast_json_string_create("Error sending message"));
ast_json_object_set(report, "uid", ast_json_integer_create(task->uid));
AST_JSON_OBJECT_SET(report, msg);
start_local_report_channel(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, ast_str_buffer(dst), NULL, NULL, 0, report);
channel_start_local_report(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, ast_str_buffer(dst), NULL, NULL, 0, report);
} else {
ast_verb(1, "[%s][SMS:%d] Error sending message\n", PVT_ID(pvt), task->uid);
}
Expand Down Expand Up @@ -1705,7 +1705,7 @@ static int at_response_msg(struct pvt* const pvt, const struct ast_str* const re
msg_ack = TRIBOOL_TRUE;
msg_ack_uid = mr;
msg_complete = 1;
start_local_report_channel(pvt, "sms", LOCAL_REPORT_DIRECTION_INCOMING, ast_str_buffer(oa), scts, dt, success, report);
channel_start_local_report(pvt, "sms", LOCAL_REPORT_DIRECTION_INCOMING, ast_str_buffer(oa), scts, dt, success, report);
}

break;
Expand Down Expand Up @@ -1762,7 +1762,7 @@ static int at_response_msg(struct pvt* const pvt, const struct ast_str* const re
ast_verb(1, "[%s][SMS:%d PARTS:%d TS:%s] Got empty message from %s\n", PVT_ID(pvt), (int)udh.ref, (int)udh.parts, scts, ast_str_buffer(oa));
}

start_local_channel_json(pvt, "sms", ast_str_buffer(oa), "SMS", sms);
channel_start_local_json(pvt, "sms", ast_str_buffer(oa), "SMS", sms);
break;
}
}
Expand Down Expand Up @@ -1969,7 +1969,7 @@ static int at_response_cusd(struct pvt* const pvt, const struct ast_str* const r
ast_json_object_set(ussd, "ussd", ast_json_string_create(ast_str_buffer(cusd_str)));
}

start_local_channel_json(pvt, "ussd", "ussd", "USSD", ussd);
channel_start_local_json(pvt, "ussd", "ussd", "USSD", ussd);
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion src/chan_quectel.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,10 @@ static int can_dial(struct pvt* pvt, unsigned int opts, const struct ast_channel
return 1;
}

if ((opts & CALL_FLAG_HOLD_OTHER) == CALL_FLAG_HOLD_OTHER && channels_loop(pvt, requestor)) {
if ((opts & CALL_FLAG_HOLD_OTHER) == CALL_FLAG_HOLD_OTHER && channel_self_request(pvt, requestor)) {
return 0;
}

return pvt_ready4voice_call(pvt, NULL, opts);
}

Expand Down
38 changes: 18 additions & 20 deletions src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int parse_dial_string(char* dialstr, const char** number, int* opts)

#/* */

int channels_loop(struct pvt* pvt, const struct ast_channel* requestor)
int channel_self_request(struct pvt* pvt, const struct ast_channel* requestor)
{
/* not allow hold requester channel :) */
/* FIXME: requestor may be just proxy/masquerade for real channel */
Expand Down Expand Up @@ -169,7 +169,7 @@ static struct ast_channel* channel_request(attribute_unused const char* type, st
}

if (pvt) {
channel = new_channel(pvt, AST_STATE_DOWN, NULL, pvt_get_pseudo_call_idx(pvt), CALL_DIR_OUTGOING, CALL_STATE_INIT, NULL, assignedids, requestor,
channel = channel_new(pvt, AST_STATE_DOWN, NULL, pvt_get_pseudo_call_idx(pvt), CALL_DIR_OUTGOING, CALL_STATE_INIT, NULL, assignedids, requestor,
local_channel);
if (!channel) {
ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
Expand Down Expand Up @@ -401,14 +401,16 @@ static ssize_t iov_write(struct pvt* pvt, int fd, const struct iovec* const iov,
return w;
}

static inline void change_audio_endianness_to_le(attribute_unused struct iovec* iov, attribute_unused int iovcnt)
#if __BYTE_ORDER == __LITTLE_ENDIAN
static inline void change_audio_endianness_to_le(attribute_unused struct iovec* iov, attribute_unused int iovcnt) {}
#else
static void change_audio_endianness_to_le(struct iovec* iov, int iovcnt)
{
#if __BYTE_ORDER == __BIG_ENDIAN
for (; iovcnt-- > 0; ++iov) {
ast_swapcopy_samples(iov->iov_base, iov->iov_base, iov->iov_len / 2);
}
#endif
}
#endif

#/* */

Expand Down Expand Up @@ -890,7 +892,7 @@ static int channel_devicestate(const char* data)
{
int res = AST_DEVICE_INVALID;

const char* const device = ast_strdupa(data ? data : "");
const char* const device = ast_strdupa(S_OR(data, ""));
ast_debug(1, "[%s] Checking device state\n", device);


Expand Down Expand Up @@ -994,20 +996,16 @@ static void set_channel_vars(struct pvt* pvt, struct ast_channel* channel)
}

/* NOTE: called from device and current levels with locked pvt */
struct ast_channel* new_channel(struct pvt* pvt, int ast_state, const char* cid_num, int call_idx, unsigned dir, call_state_t state, const char* dnid,
struct ast_channel* channel_new(struct pvt* pvt, int ast_state, const char* cid_num, int call_idx, unsigned dir, call_state_t state, const char* dnid,
const struct ast_assigned_ids* assignedids, attribute_unused const struct ast_channel* requestor, unsigned local_channel)
{
struct ast_channel* channel;
struct cpvt* cpvt;

cpvt = cpvt_alloc(pvt, call_idx, dir, CALL_STATE_INIT, local_channel);
struct cpvt* const cpvt = cpvt_alloc(pvt, call_idx, dir, CALL_STATE_INIT, local_channel);
if (!cpvt) {
return NULL;
}

channel = ast_channel_alloc(1, ast_state, cid_num, PVT_ID(pvt), NULL, dnid, CONF_SHARED(pvt, context), assignedids, requestor, 0, "%s/%s-%02u%08lx",
channel_tech.type, PVT_ID(pvt), call_idx, pvt->channel_instance);

struct ast_channel* const channel = ast_channel_alloc(1, ast_state, cid_num, PVT_ID(pvt), NULL, dnid, CONF_SHARED(pvt, context), assignedids, requestor, 0,
"%s/%s-%02u%08lx", channel_tech.type, PVT_ID(pvt), call_idx, pvt->channel_instance);
if (!channel) {
cpvt_free(cpvt);
return NULL;
Expand Down Expand Up @@ -1069,7 +1067,7 @@ struct ast_channel* new_channel(struct pvt* pvt, int ast_state, const char* cid_

/* NOTE: bg: hmm ast_queue_hangup() say no need channel lock before call, trylock got deadlock up to 30 seconds here */
/* NOTE: bg: called from device level and change_channel_state() with pvt locked */
int queue_hangup(struct ast_channel* channel, int hangupcause)
int channel_enqueue_hangup(struct ast_channel* channel, int hangupcause)
{
if (!channel) {
return -1;
Expand All @@ -1082,7 +1080,7 @@ int queue_hangup(struct ast_channel* channel, int hangupcause)
return ast_queue_hangup(channel);
}

void start_local_report_channel(struct pvt* pvt, const char* subject, local_report_direction direction, const char* number, const char* ts, const char* dt,
void channel_start_local_report(struct pvt* pvt, const char* subject, local_report_direction direction, const char* number, const char* ts, const char* dt,
int success, struct ast_json* const report)
{
RAII_VAR(struct ast_json*, rprt, ast_json_object_create(), ast_json_unref);
Expand Down Expand Up @@ -1119,12 +1117,12 @@ void start_local_report_channel(struct pvt* pvt, const char* subject, local_repo
ast_json_object_set(rprt, "report", ast_json_copy(report));
}

start_local_channel_json(pvt, "report", number, "REPORT", rprt);
channel_start_local_json(pvt, "report", number, "REPORT", rprt);
}

#/* NOTE: bg: called from device level with pvt locked */

void start_local_channel(struct pvt* pvt, const char* exten, const char* number, const channel_var_t* const vars, const size_t varscnt)
void channel_start_local(struct pvt* pvt, const char* exten, const char* number, const channel_var_t* const vars, const size_t varscnt)
{
static const ssize_t CN_DEF_LEN = 64;

Expand Down Expand Up @@ -1154,11 +1152,11 @@ void start_local_channel(struct pvt* pvt, const char* exten, const char* number,
}
}

void start_local_channel_json(struct pvt* pvt, const char* exten, const char* number, const char* const jname, const struct ast_json* const jvar)
void channel_start_local_json(struct pvt* pvt, const char* exten, const char* number, const char* const jname, const struct ast_json* const jvar)
{
RAII_VAR(char* const, jstr, ast_json_dump_string((struct ast_json*)jvar), ast_json_free);
const channel_var_t var = {jname, jstr};
start_local_channel(pvt, exten, number, &var, 1);
channel_start_local(pvt, exten, number, &var, 1);
}

#/* */
Expand Down
12 changes: 6 additions & 6 deletions src/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ typedef enum local_report_direction { LOCAL_REPORT_DIRECTION_UNKNOWN, LOCAL_REPO

extern struct ast_channel_tech channel_tech;

struct ast_channel* new_channel(struct pvt* pvt, int ast_state, const char* cid_num, int call_idx, unsigned dir, unsigned state, const char* exten,
struct ast_channel* channel_new(struct pvt* pvt, int ast_state, const char* cid_num, int call_idx, unsigned dir, unsigned state, const char* exten,
const struct ast_assigned_ids* assignedids, const struct ast_channel* requestor, unsigned local_channel);

int channels_loop(struct pvt* pvt, const struct ast_channel* requestor);
int channel_self_request(struct pvt* pvt, const struct ast_channel* requestor);

int queue_hangup(struct ast_channel* channel, int hangupcause);
int channel_enqueue_hangup(struct ast_channel* channel, int hangupcause);

void start_local_channel(struct pvt* pvt, const char* exten, const char* number, const channel_var_t* const vars, const size_t varscnt);
void start_local_channel_json(struct pvt* pvt, const char* exten, const char* number, const char* const jname, const struct ast_json* const jvar);
void start_local_report_channel(struct pvt* pvt, const char* subject, local_report_direction direction, const char* number, const char* ts, const char* dt,
void channel_start_local(struct pvt* pvt, const char* exten, const char* number, const channel_var_t* const vars, const size_t varscnt);
void channel_start_local_json(struct pvt* pvt, const char* exten, const char* number, const char* const jname, const struct ast_json* const jvar);
void channel_start_local_report(struct pvt* pvt, const char* subject, local_report_direction direction, const char* number, const char* ts, const char* dt,
int success, struct ast_json* const report);

#endif /* CHAN_QUECTEL_CHANNEL_H_INCLUDED */
2 changes: 1 addition & 1 deletion src/cpvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ static void change_state(struct cpvt* const cpvt, struct pvt* const pvt, struct
/* drop channel -> cpvt reference */
ast_channel_tech_pvt_set(channel, NULL);
cpvt_free(cpvt);
if (queue_hangup(channel, cause)) {
if (channel_enqueue_hangup(channel, cause)) {
ast_log(LOG_ERROR, "[%s] Error queueing hangup...\n", PVT_ID(pvt));
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/monitor_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void handle_expired_reports(struct pvt* pvt)
ast_json_object_set(report, "uid", ast_json_integer_create(uid));
ast_json_object_set(report, "expired", ast_json_integer_create(1));
AST_JSON_OBJECT_SET(report, msg);
start_local_report_channel(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, ast_str_buffer(dst), NULL, NULL, 0, report);
channel_start_local_report(pvt, "sms", LOCAL_REPORT_DIRECTION_OUTGOING, ast_str_buffer(dst), NULL, NULL, 0, report);
}

static int handle_expired_reports_taskproc(void* tpdata) { return PVT_TASKPROC_TRYLOCK_AND_EXECUTE(tpdata, handle_expired_reports); }
Expand Down

0 comments on commit 977d623

Please sign in to comment.