diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gsmd/usock.c | 245 | ||||
-rw-r--r-- | src/libgsmd/libgsmd_phonebook.c | 48 | ||||
-rw-r--r-- | src/util/shell.c | 88 |
3 files changed, 151 insertions, 230 deletions
diff --git a/src/gsmd/usock.c b/src/gsmd/usock.c index 5c5ec13..128a83f 100644 --- a/src/gsmd/usock.c +++ b/src/gsmd/usock.c @@ -39,6 +39,7 @@ #include <gsmd/atcmd.h> #include <gsmd/usock.h> #include <gsmd/talloc.h> +#include <gsmd/extrsp.h> #include <gsmd/ts0707.h> #include <gsmd/sms.h> @@ -664,77 +665,98 @@ static int usock_rcv_network(struct gsmd_user *gu, struct gsmd_msg_hdr *gph, static int phonebook_find_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp) { struct gsmd_user *gu = ctx; - struct gsmd_phonebooks *gps; - char *fcomma, *lcomma, *ptr1, *ptr2 = NULL; - int num; + struct gsmd_phonebooks gps; + struct gsm_extrsp *er; DEBUGP("resp: %s\n", resp); - /* - * [+CPBF: <index1>,<number>,<type>,<text>[[...] - * <CR><LF>+CPBF: <index2>,<unmber>,<type>,<text>]] - */ - num = 0; - ptr1 = strtok(resp, "\n"); - - while (ptr1) { - gps = talloc(__pb_ctx, struct gsmd_phonebooks); - ptr2 = strchr(ptr1, ' '); - gps->pb.index = atoi(ptr2+1); - - fcomma = strchr(ptr1, '"'); - lcomma = strchr(fcomma+1, '"'); - strncpy(gps->pb.numb, fcomma + 1, (lcomma-fcomma-1)); - gps->pb.numb[(lcomma - fcomma) - 1] = '\0'; + er = extrsp_parse(cmd, resp); - gps->pb.type = atoi(lcomma + 2); - - ptr2 = strrchr(ptr1, ','); - fcomma = ptr2 + 1; - lcomma = strchr(fcomma + 1, '"'); - strncpy(gps->pb.text, fcomma + 1, (lcomma - fcomma - 1)); - gps->pb.text[(lcomma - fcomma) - 1] = '\0'; - - llist_add_tail(&gps->list, &gu->pb_find_list); + if ( !er ) + return -ENOMEM; - num++; + gps.is_last = (cmd->ret == 0 || cmd->ret == 4)? 1:0; - ptr1 = strtok(NULL, "\n"); + if ( !strncmp(resp, "OK", 2) ) { + /* The record is empty or could not read yet */ + gps.pb.index = 0; } + else if ( !strncmp(resp, "+CME", 4) ) { + DEBUGP("== +CME error\n"); + /* +CME ERROR: 21 */ + gps.pb.index = 0 - atoi(strpbrk(resp, "0123456789")); + } + else if ( er->num_tokens == 4 && + er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC && + er->tokens[1].type == GSMD_ECMD_RTT_STRING && + er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC && + er->tokens[3].type == GSMD_ECMD_RTT_STRING ) { + + /* + * [+CPBR: <index1>,<number>,<type>,<text>[[...] + * <CR><LF>+CPBR: <index2>,<unmber>,<type>,<text>]] + */ + + gps.pb.index = er->tokens[0].u.numeric; + strcpy(gps.pb.numb, er->tokens[1].u.string); + gps.pb.type = er->tokens[2].u.numeric; + strcpy(gps.pb.text, er->tokens[3].u.string); + } + else { + DEBUGP("Invalid Input : Parse error\n"); + return -EINVAL; + } + + talloc_free(er); - talloc_free(__pb_ctx); return gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_FIND, - cmd->id, sizeof(num), &num); + cmd->id, sizeof(gps), &gps); } static int phonebook_read_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp) { struct gsmd_user *gu = ctx; struct gsmd_phonebook gp; - char *fcomma, *lcomma; - char *ptr; + struct gsm_extrsp *er; DEBUGP("resp: %s\n", resp); + + er = extrsp_parse(cmd, resp); - /* check the record is empty or not */ - if (!strncmp(resp, "+CPBR", 5)) { - ptr = strchr(resp, ' '); - gp.index = atoi(ptr + 1); - - fcomma = strchr(resp, '"'); - lcomma = strchr(fcomma + 1, '"'); - strncpy(gp.numb, fcomma + 1, (lcomma - fcomma - 1)); - gp.numb[(lcomma-fcomma) - 1] = '\0'; - - gp.type = atoi(lcomma + 2); - - ptr = strrchr(resp, ','); - fcomma = ptr + 1; - lcomma = strchr(fcomma + 1, '"'); - strncpy(gp.text, fcomma + 1, (lcomma-fcomma - 1)); - gp.text[(lcomma - fcomma) - 1] = '\0'; - } else + if ( !er ) + return -ENOMEM; + + if ( !strncmp(resp, "OK", 2) ) { + /* The record is empty or could not read yet */ gp.index = 0; + } + else if ( !strncmp(resp, "+CME", 4) ) { + DEBUGP("== +CME error\n"); + /* +CME ERROR: 21 */ + gp.index = 0 - atoi(strpbrk(resp, "0123456789")); + } + else if ( er->num_tokens == 4 && + er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC && + er->tokens[1].type == GSMD_ECMD_RTT_STRING && + er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC && + er->tokens[3].type == GSMD_ECMD_RTT_STRING ) { + + /* + * [+CPBR: <index1>,<number>,<type>,<text>[[...] + * <CR><LF>+CPBR: <index2>,<unmber>,<type>,<text>]] + */ + + gp.index = er->tokens[0].u.numeric; + strcpy(gp.numb, er->tokens[1].u.string); + gp.type = er->tokens[2].u.numeric; + strcpy(gp.text, er->tokens[3].u.string); + } + else { + DEBUGP("Invalid Input : Parse error\n"); + return -EINVAL; + } + + talloc_free(er); return gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_READ, cmd->id, sizeof(gp), &gp); @@ -743,47 +765,52 @@ static int phonebook_read_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp) static int phonebook_readrg_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp) { struct gsmd_user *gu = ctx; - struct gsmd_phonebooks *gps; - char *fcomma, *lcomma, *ptr1, *ptr2 = NULL; - int num; + struct gsmd_phonebooks gps; + struct gsm_extrsp *er; DEBUGP("resp: %s\n", resp); - /* - * [+CPBR: <index1>,<number>,<type>,<text>[[...] - * <CR><LF>+CPBR: <index2>,<unmber>,<type>,<text>]] - */ - num = 0; - ptr1 = strtok(resp, "\n"); - - while (ptr1) { - gps = talloc(__pb_ctx, struct gsmd_phonebooks); - ptr2 = strchr(ptr1, ' '); - gps->pb.index = atoi(ptr2+1); - - fcomma = strchr(ptr1, '"'); - lcomma = strchr(fcomma+1, '"'); - strncpy(gps->pb.numb, fcomma + 1, (lcomma-fcomma-1)); - gps->pb.numb[(lcomma - fcomma) - 1] = '\0'; + er = extrsp_parse(cmd, resp); - gps->pb.type = atoi(lcomma + 2); - - ptr2 = strrchr(ptr1, ','); - fcomma = ptr2 + 1; - lcomma = strchr(fcomma + 1, '"'); - strncpy(gps->pb.text, fcomma + 1, (lcomma - fcomma - 1)); - gps->pb.text[(lcomma - fcomma) - 1] = '\0'; - - llist_add_tail(&gps->list, &gu->pb_readrg_list); + if ( !er ) + return -ENOMEM; - num++; + gps.is_last = (cmd->ret == 0 || cmd->ret == 4)? 1:0; - ptr1 = strtok(NULL, "\n"); + if ( !strncmp(resp, "OK", 2) ) { + /* The record is empty or could not read yet */ + gps.pb.index = 0; } + else if ( !strncmp(resp, "+CME", 4) ) { + DEBUGP("== +CME error\n"); + /* +CME ERROR: 21 */ + gps.pb.index = 0 - atoi(strpbrk(resp, "0123456789")); + } + else if ( er->num_tokens == 4 && + er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC && + er->tokens[1].type == GSMD_ECMD_RTT_STRING && + er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC && + er->tokens[3].type == GSMD_ECMD_RTT_STRING ) { + + /* + * [+CPBR: <index1>,<number>,<type>,<text>[[...] + * <CR><LF>+CPBR: <index2>,<unmber>,<type>,<text>]] + */ + + gps.pb.index = er->tokens[0].u.numeric; + strcpy(gps.pb.numb, er->tokens[1].u.string); + gps.pb.type = er->tokens[2].u.numeric; + strcpy(gps.pb.text, er->tokens[3].u.string); + } + else { + DEBUGP("Invalid Input : Parse error\n"); + return -EINVAL; + } + + talloc_free(er); - talloc_free(__pb_ctx); return gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_READRG, - cmd->id, sizeof(num), &num); + cmd->id, sizeof(gps), &gps); } static int phonebook_write_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp) @@ -978,54 +1005,6 @@ static int usock_rcv_phonebook(struct gsmd_user *gu, cmd = atcmd_fill("AT+CPBR=?", 9+1, &phonebook_get_support_cb, gu, gph->id, NULL); break; - case GSMD_PHONEBOOK_RETRIEVE_READRG: - if (len < sizeof(*gph) + sizeof(int)) - return -EINVAL; - - num = (int *) ((void *)gph + sizeof(*gph)); - gp = talloc_size(__pb_ctx, sizeof(*gp) * (*num)); - if (!llist_empty(&gu->pb_readrg_list)) - llist_for_each_entry_safe(cur, cur2, - &gu->pb_readrg_list, list) { - gp->index = cur->pb.index; - strcpy(gp->numb, cur->pb.numb); - gp->type = cur->pb.type; - strcpy(gp->text, cur->pb.text); - gp++; - - llist_del(&cur->list); - free(cur); - } - - ret = gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, - GSMD_PHONEBOOK_RETRIEVE_READRG, gph->id, - sizeof(*gp) * (*num), gp); - talloc_free(gp); - return ret; - case GSMD_PHONEBOOK_RETRIEVE_FIND: - if (len < sizeof(*gph) + sizeof(int)) - return -EINVAL; - - num = (int *) ((void *)gph + sizeof(*gph)); - gp = talloc_size(__pb_ctx, sizeof(*gp) * (*num)); - if (!llist_empty(&gu->pb_find_list)) - llist_for_each_entry_safe(cur, cur2, - &gu->pb_find_list, list) { - gp->index = cur->pb.index; - strcpy(gp->numb, cur->pb.numb); - gp->type = cur->pb.type; - strcpy(gp->text, cur->pb.text); - gp++; - - llist_del(&cur->list); - free(cur); - } - - ret = gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, - GSMD_PHONEBOOK_RETRIEVE_FIND, gph->id, - sizeof(*gp) * (*num), gp); - talloc_free(gp); - return ret; default: return -EINVAL; } @@ -1154,8 +1133,6 @@ static int gsmd_usock_cb(int fd, unsigned int what, void *data) newuser->gsmd = g; newuser->subscriptions = 0xffffffff; INIT_LLIST_HEAD(&newuser->finished_ucmds); - INIT_LLIST_HEAD(&newuser->pb_readrg_list); - INIT_LLIST_HEAD(&newuser->pb_find_list); llist_add(&newuser->list, &g->users); gsmd_register_fd(&newuser->gfd); diff --git a/src/libgsmd/libgsmd_phonebook.c b/src/libgsmd/libgsmd_phonebook.c index f139c2a..798cd2f 100644 --- a/src/libgsmd/libgsmd_phonebook.c +++ b/src/libgsmd/libgsmd_phonebook.c @@ -199,51 +199,3 @@ int lgsm_pb_get_support(struct lgsm_handle *lh) { return lgsm_send_simple(lh, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_GET_SUPPORT); } - -int lgsm_pb_retrieve_readrg(struct lgsm_handle *lh, int num) -{ - struct gsmd_msg_hdr *gmh; - int rc; - - gmh = lgsm_gmh_fill(GSMD_MSG_PHONEBOOK, - GSMD_PHONEBOOK_RETRIEVE_READRG, sizeof(int)); - if (!gmh) - return -ENOMEM; - - *(int *)(gmh->data) = num; - - rc = lgsm_send(lh, gmh); - if (rc < gmh->len + sizeof(*gmh)) { - lgsm_gmh_free(gmh); - return -EIO; - } - - lgsm_gmh_free(gmh); - - return 0; -} - -int lgsm_pb_retrieve_find(struct lgsm_handle *lh, int num) -{ - struct gsmd_msg_hdr *gmh; - int rc; - - gmh = lgsm_gmh_fill(GSMD_MSG_PHONEBOOK, - GSMD_PHONEBOOK_RETRIEVE_FIND, sizeof(int)); - if (!gmh) - return -ENOMEM; - - *(int *)(gmh->data) = num; - - rc = lgsm_send(lh, gmh); - if (rc < gmh->len + sizeof(*gmh)) { - lgsm_gmh_free(gmh); - return -EIO; - } - - lgsm_gmh_free(gmh); - - return 0; -} - - diff --git a/src/util/shell.c b/src/util/shell.c index 373ce33..103e161 100644 --- a/src/util/shell.c +++ b/src/util/shell.c @@ -41,8 +41,6 @@ #endif #define STDIN_BUF_SIZE 1024 -static int nFIND = 0; -static int nREADRG = 0; int pending_responses = 0; @@ -57,39 +55,63 @@ static int pt_msghandler(struct lgsm_handle *lh, struct gsmd_msg_hdr *gmh) static int pb_msghandler(struct lgsm_handle *lh, struct gsmd_msg_hdr *gmh) { struct gsmd_phonebook *gp; - struct gsmd_phonebook_support *gps; + struct gsmd_phonebooks *gps; + struct gsmd_phonebook_support *gpsu; struct gsmd_phonebook_storage *gpst; char *payload; - char *fcomma, *lcomma, *ptr = NULL; - int *num; - char buf[128]; int i; switch (gmh->msg_subtype) { case GSMD_PHONEBOOK_FIND: - num = (int *) ((char *)gmh + sizeof(*gmh)); - printf("Records:%d\n", *num); + gps = (struct gsmd_phonebooks *) ((char *)gmh + sizeof(*gmh)); - nFIND = *num; + if (gps->pb.index > 0) + printf("%d, %s, %d, %s\n", + gps->pb.index, gps->pb.numb, + gps->pb.type, gps->pb.text); + else if (gps->pb.index < 0) + /* If index < 0, error happens */ + printf("+CME ERROR %d\n", (0-(gps->pb.index))); + else + /* The record doesn't exist or could not read yet */ + printf("Doesn't exist or couldn't read it yet\n"); + + if (gps->is_last) + pending_responses --; break; case GSMD_PHONEBOOK_READRG: - num = (int *) ((char *)gmh + sizeof(*gmh)); - printf("Records:%d\n", *num); + gps = (struct gsmd_phonebooks *) ((char *)gmh + sizeof(*gmh)); - nREADRG = *num; + if (gps->pb.index > 0) + printf("%d, %s, %d, %s\n", + gps->pb.index, gps->pb.numb, + gps->pb.type, gps->pb.text); + else if (gps->pb.index < 0) + /* If index < 0, error happens */ + printf("+CME ERROR %d\n", (0-(gps->pb.index))); + else + /* The record doesn't exist or could not read yet */ + printf("Doesn't exist or couldn't read it yet\n"); + + if (gps->is_last) + pending_responses --; break; case GSMD_PHONEBOOK_READ: gp = (struct gsmd_phonebook *) ((char *)gmh + sizeof(*gmh)); - if (gp->index) + if (gp->index > 0) printf("%d, %s, %d, %s\n", gp->index, gp->numb, gp->type, gp->text); + else if (gp->index < 0) + /* If index < 0, error happens */ + printf("+CME ERROR %d\n", (0-(gp->index))); else - printf("Empty\n"); + /* The record doesn't exist or could not read yet */ + printf("Doesn't exist or couldn't read it yet\n"); break; case GSMD_PHONEBOOK_GET_SUPPORT: - gps = (struct gsmd_phonebook_support *) ((char *)gmh + sizeof(*gmh)); - printf("(1-%d), %d, %d\n", gps->index, gps->nlength, gps->tlength); + gpsu = (struct gsmd_phonebook_support *) ((char *)gmh + sizeof(*gmh)); + printf("(1-%d), %d, %d\n", gpsu->index, gpsu->nlength, gpsu->tlength); pending_responses --; break; @@ -111,26 +133,6 @@ static int pb_msghandler(struct lgsm_handle *lh, struct gsmd_msg_hdr *gmh) payload = (char *)gmh + sizeof(*gmh); printf("%s\n", payload); break; - case GSMD_PHONEBOOK_RETRIEVE_READRG: - gp = (struct gsmd_phonebook *) ((char *)gmh + sizeof(*gmh)); - - for (i=0; i<nREADRG; i++) { - printf("%d,%s,%d,%s\n", gp->index, gp->numb, gp->type, gp->text); - gp++; - } - - nREADRG = 0; - break; - case GSMD_PHONEBOOK_RETRIEVE_FIND: - gp = (struct gsmd_phonebook *) ((char *)gmh + sizeof(*gmh)); - - for (i = 0; i < nFIND; i++) { - printf("%d,%s,%d,%s\n", gp->index, gp->numb, gp->type, gp->text); - gp++; - } - - nFIND = 0; - break; default: return -EINVAL; } @@ -425,8 +427,6 @@ static int shell_help(void) "\tps\tPB Support\n" "\tpm\tPB Memory\n" "\tpp\tPB Set Memory (pp=storage)\n" - "\tpRr\tRetrieve Readrg Records\n" - "\tpRf\tRetrieve Find Records\n" "\tsd\tSMS Delete (sd=index,delflg)\n" "\tsl\tSMS List (sl=stat)\n" "\tsr\tSMS Read (sr=index)\n" @@ -570,6 +570,7 @@ int shell_main(struct lgsm_handle *lgsmh, int sync) ptr = strchr(buf, ','); pb_readrg.index2 = atoi(ptr+1); lgsm_pb_read_entries(lgsmh, &pb_readrg); + pending_responses ++; } else if ( !strncmp(buf, "pr", 2)) { ptr = strchr(buf, '='); lgsm_pb_read_entry(lgsmh, atoi(ptr+1)); @@ -584,6 +585,7 @@ int shell_main(struct lgsm_handle *lgsmh, int sync) pb_find.findtext[strlen(ptr+1)] = '\0'; lgsm_pb_find_entry(lgsmh, &pb_find); + pending_responses ++; } else if ( !strncmp(buf, "pw", 2)) { printf("Write Phonebook Entry\n"); struct lgsm_phonebook pb; @@ -616,16 +618,6 @@ int shell_main(struct lgsm_handle *lgsmh, int sync) printf("Get Phonebook Support\n"); lgsm_pb_get_support(lgsmh); pending_responses ++; - } else if( !strncmp(buf, "pRr", 3) ) { - printf("Retrieve Readrg Records\n"); - - if ( nREADRG ) - lgsm_pb_retrieve_readrg(lgsmh, nREADRG); - } else if( !strncmp(buf, "pRf", 3) ) { - printf("Retrieve Find Records\n"); - - if ( nFIND ) - lgsm_pb_retrieve_find(lgsmh, nFIND); } else if ( !strncmp(buf, "sd", 2)) { printf("Delete SMS\n"); struct lgsm_sms_delete sms_del; |