emimsg.c
资源名称:gateway-1.2.1 [点击查看]
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:9k
源码类别:
手机WAP编程
开发平台:
WINDOWS
- /*
- * emimsg.c
- *
- * Functions for working with EMI messages
- * Uoti Urpala 2001 */
- #include "emimsg.h"
- /* Return an error string corresponding to the number. */
- static char *emi_strerror(int errnum)
- {
- switch (errnum) {
- case 1: return "Checksum error";
- case 2: return "Syntax error";
- case 3: return "Operation not supported by system";
- case 4: return "Operation not allowed";
- case 5: return "Call barring active";
- case 6: return "AdC invalid";
- case 7: return "Authentication failure";
- case 8: return "Legitimisation code for all calls, failure";
- case 9: return "GA not valid";
- case 10: return "Repetition not allowed";
- case 11: return "Legitimisation code for repetition, failure";
- case 12: return "Priority call not allowed";
- case 13: return "Legitimisation code for priority call, failure";
- case 14: return "Urgent message not allowed";
- case 15: return "Legitimisation code for urgent message, failure";
- case 16: return "Reverse charging not allowed";
- case 17: return "Legitimisation code for reverse charging, failure";
- case 18: return "Deferred delivery not allowed";
- case 19: return "New AC not valid";
- case 20: return "New legitimisation code not valid";
- case 21: return "Standard text not valid";
- case 22: return "Time period not valid";
- case 23: return "Message type not supported by system";
- case 24: return "Message too long";
- case 25: return "Requested standard text not valid";
- case 26: return "Message type not valid for the pager type";
- case 27: return "Message not found in smsc";
- case 30: return "Subscriber hang-up";
- case 31: return "Fax group not supported";
- case 32: return "Fax message type not supported";
- case 33: return "Address already in list (60 series)";
- case 34: return "Address not in list (60 series)";
- case 35: return "List full, cannot add address to list (60 series)";
- case 36: return "RPID already in use";
- case 37: return "Delivery in progress";
- case 38: return "Message forwarded";
- default: return "!UNRECOGNIZED ERROR CODE!";
- }
- }
- static int field_count_op(int ot, Octstr *whoami)
- {
- switch (ot) {
- case 01:
- return SZ01;
- case 31:
- return 2;
- case 51:
- case 52:
- case 53:
- return SZ50;
- case 60:
- return SZ60;
- default:
- error(0, "EMI2[%s]: Unsupported EMI operation request type %d",
- octstr_get_cstr(whoami), ot);
- return -1;
- }
- }
- static int field_count_reply(int ot, int posit, Octstr *whoami)
- {
- switch(ot) {
- case 01:
- return posit ? 2 : 3;
- case 31:
- return posit ? 2 : 3;
- case 51:
- case 52:
- case 53:
- return 3;
- case 60:
- return posit ? 2 : 3;
- default:
- error(0, "EMI2[%s]: Unsupported EMI operation reply type %d",
- octstr_get_cstr(whoami), ot);
- return -1;
- }
- }
- static struct emimsg *emimsg_create_withlen(int len)
- {
- struct emimsg *ret;
- ret = gw_malloc(sizeof(struct emimsg));
- ret->fields = gw_malloc(len * sizeof(Octstr *));
- ret->num_fields = len;
- while (--len >= 0)
- ret->fields[len] = NULL;
- return ret;
- }
- struct emimsg *emimsg_create_op(int ot, int trn, Octstr *whoami)
- {
- int len;
- struct emimsg *ret;
- len = field_count_op(ot, whoami);
- if (len < 0)
- return NULL;
- ret = emimsg_create_withlen(len);
- ret->ot = ot;
- ret->or = 'O';
- ret->trn = trn;
- return ret;
- }
- static struct emimsg *emimsg_create_reply_s(int ot, int trn, int positive,
- Octstr *whoami)
- {
- int len;
- struct emimsg *ret;
- len = field_count_reply(ot, positive, whoami);
- if (len < 0)
- return NULL;
- ret = emimsg_create_withlen(len);
- ret->ot = ot;
- ret->or = 'R';
- ret->trn = trn;
- return ret;
- }
- struct emimsg *emimsg_create_reply(int ot, int trn, int positive,
- Octstr *whoami)
- {
- struct emimsg *ret;
- ret = emimsg_create_reply_s(ot, trn, positive, whoami);
- if (ret) {
- if (positive)
- ret->fields[0] = octstr_create("A");
- else
- ret->fields[0] = octstr_create("N");
- }
- return ret;
- }
- void emimsg_destroy(struct emimsg *emimsg)
- {
- int len;
- len = emimsg->num_fields;
- while (--len >= 0)
- octstr_destroy(emimsg->fields[len]); /* octstr_destroy(NULL) is ok */
- gw_free(emimsg->fields);
- gw_free(emimsg);
- }
- /* The argument can be either the whole message (with the stx/etx start/end
- characters), or miss the last 3 characters (checksum digits and etx) */
- static int calculate_checksum(Octstr *message)
- {
- int end, i, checksum;
- end = octstr_len(message);
- if (octstr_get_char(message, end - 1) == 3) /* etx, whole message */
- end -= 3;
- checksum = 0;
- for (i = 1; i < end; i++)
- checksum += octstr_get_char(message, i);
- return checksum & 0xff;
- }
- static Octstr *emimsg_tostring(struct emimsg *emimsg)
- {
- int i, checksum;
- Octstr *result, *data;
- char *hexits = "0123456789ABCDEF";
- data = octstr_create("");
- for (i = 0; i < emimsg->num_fields; i++) {
- if (emimsg->fields[i])
- octstr_append(data, emimsg->fields[i]);
- octstr_append_char(data, '/');
- }
- result = octstr_format("