File: misc.c

package info (click to toggle)
python-gammu 0.26-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 648 kB
  • ctags: 643
  • sloc: ansic: 9,254; python: 1,770; makefile: 77; sh: 8
file content (172 lines) | stat: -rw-r--r-- 5,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * python-gammu - Phone communication libary
 * Copyright © 2003 - 2008 Michal Čihař
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * vim: expandtab sw=4 ts=4 sts=4:
 */

/* Miscellaneous conversions, that didn't fit elsewhere */

#include "pyg-config.h"
#include "convertors.h"


GSM_CategoryType StringToCategoryType(const char *s) {
    if (strcmp(s, "ToDo") == 0)             return Category_ToDo;
    else if (strcmp(s, "Phonebook") == 0)   return Category_Phonebook;
    else {
        PyErr_Format(PyExc_ValueError, "Bad value for category type: '%s'", s);
        return 0;
    }
}

GSM_MemoryType StringToMemoryType(const char *s) {
    if (strcmp(s, "ME") == 0)      return MEM_ME;
    else if (strcmp(s, "SM") == 0) return MEM_SM;
    else if (strcmp(s, "ON") == 0) return MEM_ON;
    else if (strcmp(s, "DC") == 0) return MEM_DC;
    else if (strcmp(s, "RC") == 0) return MEM_RC;
    else if (strcmp(s, "MC") == 0) return MEM_MC;
    else if (strcmp(s, "MT") == 0) return MEM_MT;
    else if (strcmp(s, "FD") == 0) return MEM_FD;
    else if (strcmp(s, "VM") == 0) return MEM_VM;
    else if (strcmp(s, "SL") == 0) return MEM_SL;
    else {
        PyErr_Format(PyExc_ValueError, "Bad value for memory type: '%s'", s);
        return 0;
    }
}

char *MemoryTypeToString(GSM_MemoryType t) {
    char *s;

    s = malloc(3);
    if (s == NULL) {
        PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate string");
        return NULL;
    }
    s[0] = 0;
    s[2] = 0;
    switch (t) {
        case MEM_ME: s[0] = 'M'; s[1] = 'E'; return s;
        case MEM_SM: s[0] = 'S'; s[1] = 'M'; return s;
        case MEM_ON: s[0] = 'O'; s[1] = 'N'; return s;
        case MEM_DC: s[0] = 'D'; s[1] = 'C'; return s;
        case MEM_RC: s[0] = 'R'; s[1] = 'C'; return s;
        case MEM_MC: s[0] = 'M'; s[1] = 'C'; return s;
        case MEM_MT: s[0] = 'M'; s[1] = 'T'; return s;
        case MEM_FD: s[0] = 'F'; s[1] = 'D'; return s;
        case MEM_VM: s[0] = 'V'; s[1] = 'M'; return s;
        case MEM_SL: s[0] = 'S'; s[1] = 'L'; return s;
    }
    PyErr_Format(PyExc_ValueError, "Bad value for MemoryType from Gammu: '%02x'", t);
    free(s);
    return NULL;
}


GSM_MemoryType GetMemoryTypeFromDict(PyObject *dict, char *key) {
    char *s;
    s = GetCharFromDict(dict, key);
    if (s == NULL) return -1;

    return StringToMemoryType(s);
}

PyObject *CBToPython(GSM_CBMessage *cb) {
    return Py_BuildValue("{s:i,s:s}",
            "Channel", cb->Channel,
            "Text", cb->Text
            );
}

GSM_USSDStatus StringToUSSDStatus(const char *s) {
	if (strcmp(s, "Unknown") == 0) return USSD_Unknown;
	else if (strcmp(s, "NoActionNeeded") == 0) return USSD_NoActionNeeded;
	else if (strcmp(s, "ActionNeeded") == 0) return USSD_ActionNeeded;
	else if (strcmp(s, "Terminated") == 0) return USSD_Terminated;
	else if (strcmp(s, "AnotherClient") == 0) return USSD_AnotherClient;
	else if (strcmp(s, "NotSupported") == 0) return USSD_NotSupported;
	else if (strcmp(s, "Timeout") == 0) return USSD_Timeout;

    PyErr_Format(PyExc_ValueError, "Bad value for USSD Status '%s'", s);
    return 0;
}

char *USSDStatusToString(GSM_USSDStatus type) {
    char *err = "Err";
    char *s = err;

    switch (type) {
		case USSD_Unknown:
			s = strdup("Unknown");
			break;
		case USSD_NoActionNeeded:
			s = strdup("NoActionNeeded");
			break;
		case USSD_ActionNeeded:
			s = strdup("ActionNeeded");
			break;
		case USSD_Terminated:
			s = strdup("Terminated");
			break;
		case USSD_AnotherClient:
			s = strdup("AnotherClient");
			break;
		case USSD_NotSupported:
			s = strdup("NotSupported");
			break;
		case USSD_Timeout:
			s = strdup("Timeout");
			break;
    }

    if (s == err) {
        PyErr_Format(PyExc_ValueError, "Bad value for USSD Status from Gammu: '%d'", type);
        return NULL;
    }

    if (s == NULL) {
        PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate string");
        return NULL;
    }

    return s;
}

PyObject *USSDToPython(GSM_USSDMessage *ussd) {
    PyObject *text;
    PyObject *result;
    char        *status;

    status = USSDStatusToString(ussd->Status);
    if (status == NULL) return NULL;

    text = UnicodeStringToPython(ussd->Text);
    if (text == NULL) {
        free(status);
        return NULL;
    }

    result = Py_BuildValue("{s:s,s:O}",
            "Status", status,
            "Text", text
            );

    Py_DECREF(text);
    free(status);
    return result;
}