Commit 4656e0a3 authored by Tilman Schmidt's avatar Tilman Schmidt Committed by David S. Miller

isdn/gigaset: restructure modem response parser (4)

Restructure the control structure of the modem response parser
to improve readability and error handling.
Signed-off-by: default avatarTilman Schmidt <tilman@imap.cc>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent d93187eb
...@@ -389,6 +389,20 @@ zsau_resp[] = ...@@ -389,6 +389,20 @@ zsau_resp[] =
{NULL, ZSAU_UNKNOWN} {NULL, ZSAU_UNKNOWN}
}; };
/* check for and remove fixed string prefix
* If s starts with prefix terminated by a non-alphanumeric character,
* return pointer to the first character after that, otherwise return NULL.
*/
static char *skip_prefix(char *s, const char *prefix)
{
while (*prefix)
if (*s++ != *prefix++)
return NULL;
if (isalnum(*s))
return NULL;
return s;
}
/* queue event with CID */ /* queue event with CID */
static void add_cid_event(struct cardstate *cs, int cid, int type, static void add_cid_event(struct cardstate *cs, int cid, int type,
void *ptr, int parameter) void *ptr, int parameter)
...@@ -430,15 +444,11 @@ static void add_cid_event(struct cardstate *cs, int cid, int type, ...@@ -430,15 +444,11 @@ static void add_cid_event(struct cardstate *cs, int cid, int type,
*/ */
void gigaset_handle_modem_response(struct cardstate *cs) void gigaset_handle_modem_response(struct cardstate *cs)
{ {
unsigned char *argv[MAX_REC_PARAMS + 1]; char *eoc, *psep, *ptr;
int params;
int i, j;
char *psep, *ptr;
const struct resp_type_t *rt; const struct resp_type_t *rt;
const struct zsau_resp_t *zr; const struct zsau_resp_t *zr;
int curarg;
int abort;
int cid, parameter; int cid, parameter;
u8 type, value;
if (!cs->cbytes) { if (!cs->cbytes) {
/* ignore additional LFs/CRs (M10x config mode or cx100) */ /* ignore additional LFs/CRs (M10x config mode or cx100) */
...@@ -456,6 +466,19 @@ void gigaset_handle_modem_response(struct cardstate *cs) ...@@ -456,6 +466,19 @@ void gigaset_handle_modem_response(struct cardstate *cs)
return; return;
} }
/* look up response type */
for (rt = resp_type; rt->response; ++rt) {
eoc = skip_prefix(cs->respdata, rt->response);
if (eoc)
break;
}
if (!rt->response) {
add_cid_event(cs, 0, RSP_NONE, NULL, 0);
gig_dbg(DEBUG_EVENT, "unknown modem response: '%s'\n",
cs->respdata);
return;
}
/* check for CID */ /* check for CID */
psep = strrchr(cs->respdata, ';'); psep = strrchr(cs->respdata, ';');
if (psep && if (psep &&
...@@ -468,120 +491,141 @@ void gigaset_handle_modem_response(struct cardstate *cs) ...@@ -468,120 +491,141 @@ void gigaset_handle_modem_response(struct cardstate *cs)
cid = 0; cid = 0;
} }
/* parse line */ gig_dbg(DEBUG_EVENT, "CMD received: %s", cs->respdata);
argv[0] = cs->respdata;
params = 1;
for (i = 0; i < cs->cbytes; i++)
switch (cs->respdata[i]) {
case ';':
case ',':
case '=':
cs->respdata[i] = 0;
if (params > MAX_REC_PARAMS)
dev_warn(cs->dev,
"too many parameters in response\n");
else
argv[params++] = cs->respdata + i + 1;
}
gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]);
if (cid) if (cid)
gig_dbg(DEBUG_EVENT, "CID: %d", cid); gig_dbg(DEBUG_EVENT, "CID: %d", cid);
gig_dbg(DEBUG_EVENT, "available params: %d", params - 1);
for (j = 1; j < params; j++)
gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]);
abort = 1;
curarg = 0;
while (curarg < params) {
for (rt = resp_type; rt->response; ++rt)
if (!strcmp(argv[curarg], rt->response))
break;
if (!rt->response) { switch (rt->type) {
add_cid_event(cs, 0, RSP_NONE, NULL, 0); case RT_NOTHING:
gig_dbg(DEBUG_EVENT, "unknown modem response: '%s'\n", /* check parameter separator */
argv[curarg]); if (*eoc)
break; goto bad_param; /* extra parameter */
}
++curarg; add_cid_event(cs, cid, rt->resp_code, NULL, 0);
break;
switch (rt->type) { case RT_RING:
case RT_NOTHING: /* check parameter separator */
add_cid_event(cs, cid, rt->resp_code, NULL, 0); if (!*eoc)
break; eoc = NULL; /* no parameter */
case RT_RING: else if (*eoc++ != ',')
if (!cid) { goto bad_param;
dev_err(cs->dev,
"received RING without CID!\n"); add_cid_event(cs, 0, rt->resp_code, NULL, cid);
add_cid_event(cs, 0, RSP_INVAL, NULL, 0);
abort = 1; /* process parameters as individual responses */
} else { while (eoc) {
add_cid_event(cs, 0, rt->resp_code, NULL, cid); /* look up parameter type */
abort = 0; psep = NULL;
} for (rt = resp_type; rt->response; ++rt) {
break; psep = skip_prefix(eoc, rt->response);
case RT_ZSAU: if (psep)
if (curarg >= params) {
add_cid_event(cs, cid, rt->resp_code, NULL,
ZSAU_NONE);
break;
}
for (zr = zsau_resp; zr->str; ++zr)
if (!strcmp(argv[curarg], zr->str))
break; break;
if (!zr->str) }
/* all legal parameters are of type RT_STRING */
if (!psep || rt->type != RT_STRING) {
dev_warn(cs->dev, dev_warn(cs->dev,
"%s: unknown parameter %s after ZSAU\n", "illegal RING parameter: '%s'\n",
__func__, argv[curarg]); eoc);
add_cid_event(cs, cid, rt->resp_code, NULL, zr->code); return;
++curarg;
break;
case RT_STRING:
if (curarg < params) {
ptr = kstrdup(argv[curarg], GFP_ATOMIC);
if (!ptr)
dev_err(cs->dev, "out of memory\n");
++curarg;
} else {
ptr = NULL;
} }
gig_dbg(DEBUG_EVENT, "string==%s", ptr ? ptr : "NULL");
/* skip parameter value separator */
if (*psep++ != '=')
goto bad_param;
/* look up end of parameter */
eoc = strchr(psep, ',');
if (eoc)
*eoc++ = 0;
/* retrieve parameter value */
ptr = kstrdup(psep, GFP_ATOMIC);
/* queue event */
add_cid_event(cs, cid, rt->resp_code, ptr, 0); add_cid_event(cs, cid, rt->resp_code, ptr, 0);
break;
case RT_ZCAU:
parameter = -1;
if (curarg + 1 < params) {
u8 type, value;
i = kstrtou8(argv[curarg++], 16, &type);
j = kstrtou8(argv[curarg++], 16, &value);
if (i == 0 && j == 0)
parameter = (type << 8) | value;
} else
curarg = params - 1;
add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
break;
case RT_NUMBER:
if (curarg >= params ||
kstrtoint(argv[curarg++], 10, &parameter))
parameter = -1;
gig_dbg(DEBUG_EVENT, "parameter==%d", parameter);
add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
if (rt->resp_code == RSP_ZDLE)
cs->dle = parameter;
break;
} }
break;
if (abort) case RT_ZSAU:
/* check parameter separator */
if (!*eoc) {
/* no parameter */
add_cid_event(cs, cid, rt->resp_code, NULL, ZSAU_NONE);
break; break;
} }
if (*eoc++ != '=')
goto bad_param;
if (curarg != params) /* look up parameter value */
gig_dbg(DEBUG_EVENT, for (zr = zsau_resp; zr->str; ++zr)
"invalid number of processed parameters: %d/%d", if (!strcmp(eoc, zr->str))
curarg, params); break;
if (!zr->str)
goto bad_param;
add_cid_event(cs, cid, rt->resp_code, NULL, zr->code);
break;
case RT_STRING:
/* check parameter separator */
if (*eoc++ != '=')
goto bad_param;
/* retrieve parameter value */
ptr = kstrdup(eoc, GFP_ATOMIC);
/* queue event */
add_cid_event(cs, cid, rt->resp_code, ptr, 0);
break;
case RT_ZCAU:
/* check parameter separators */
if (*eoc++ != '=')
goto bad_param;
psep = strchr(eoc, ',');
if (!psep)
goto bad_param;
*psep++ = 0;
/* decode parameter values */
if (kstrtou8(eoc, 16, &type) || kstrtou8(psep, 16, &value)) {
*--psep = ',';
goto bad_param;
}
parameter = (type << 8) | value;
add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
break;
case RT_NUMBER:
/* check parameter separator */
if (*eoc++ != '=')
goto bad_param;
/* decode parameter value */
if (kstrtoint(eoc, 10, &parameter))
goto bad_param;
/* special case ZDLE: set flag before queueing event */
if (rt->resp_code == RSP_ZDLE)
cs->dle = parameter;
add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
break;
bad_param:
/* parameter unexpected, incomplete or malformed */
dev_warn(cs->dev, "bad parameter in response '%s'\n",
cs->respdata);
add_cid_event(cs, cid, rt->resp_code, NULL, -1);
break;
default:
dev_err(cs->dev, "%s: internal error on '%s'\n",
__func__, cs->respdata);
}
} }
EXPORT_SYMBOL_GPL(gigaset_handle_modem_response); EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment