:pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
[gnokii.git] / common / gsm-api.c
1 /*
2
3   $Id$
4   
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12         
13   Provides a generic API for accessing functions on the phone, wherever
14   possible hiding the model specific details.
15
16   The underlying code should run in it's own thread to allow communications to
17   the phone to be run independantly of mailing code that calls these API
18   functions.
19
20   Unless otherwise noted, all functions herein block until they complete.  The
21   functions themselves are defined in a structure in gsm-common.h.
22
23   $Log$
24   Revision 1.1.1.1  2001/11/25 21:59:04  short
25   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
26
27   Revision 1.32  2001/11/14 11:26:18  pkot
28   Getting SMS in 6210/7110 does finally work in some cases :)
29
30   Revision 1.31  2001/11/08 16:45:58  pkot
31   Obsolete old structure, kill treads where possible and make shared library
32
33   Revision 1.30  2001/08/09 11:51:38  pkot
34   Generic AT support updates and cleanup (Manfred Jonsson)
35
36   Revision 1.29  2001/07/27 00:02:20  pkot
37   Generic AT support for the new structure (Manfred Jonsson)
38
39   Revision 1.28  2001/06/06 09:05:56  machek
40   Convert Grab/Release display to new structure.
41
42   Revision 1.27  2001/05/07 14:13:06  machek
43   nokia-2110 module converted to suit new API better. --identify now works.
44
45   Revision 1.26  2001/04/25 12:54:47  machek
46   Partly converted nokia 2110 to "new" form, and moved it to phone
47   directory.
48
49   Revision 1.25  2001/03/26 23:39:21  pkot
50   Minor updates:
51    - Windows INLINE patch (Manfred Jonsson)
52    - patch to configure.in to compile under FreeBSD (Panagiotis Astithas)
53    - other cleanups (me)
54
55   
56 */
57
58 #include <stdio.h>
59 #include <string.h>
60
61 #include "misc.h"
62 #include "gsm-common.h"
63 #include "data/rlp-common.h"
64 #include "gsm-statemachine.h"
65 #include "phones/nk7110.h"
66 #include "phones/nk6100.h"
67 #include "phones/nk3110.h"
68 #include "phones/nk2110.h"
69
70 GSM_Statemachine GSM_SM;
71 GSM_Error (*GSM_F)(GSM_Operation op, GSM_Data *data, GSM_Statemachine *state);
72
73
74 /* GSM_LinkOK is set to true once normal communications with the phone have
75    been established. */
76
77 bool *GSM_LinkOK;
78 bool LinkAlwaysOK = true;
79
80 /* Define pointer to the GSM_Functions structure used by external code to call
81    relevant API functions. This structure is defined in gsm-common.h. */
82
83 GSM_Functions *GSM;
84
85 /* Define pointer to the GSM_Information structure used by external code to
86    obtain information that varies from model to model. This structure is also
87    defined in gsm-common.h */
88
89 GSM_Information         *GSM_Info;
90
91 /* Initialise interface to the phone. Model number should be a string such as
92    3810, 5110, 6110 etc. Device is the serial port to use e.g. /dev/ttyS0, the
93    user must have write permission to the device. */
94
95 static GSM_Error register_phone(GSM_Phone *phone, char *model, char *setupmodel, GSM_Statemachine *sm)
96 {
97         GSM_Data data;
98         GSM_Data *p_data;
99         if (setupmodel) {
100                 GSM_DataClear(&data);
101                 data.Model = setupmodel;
102                 p_data = &data;
103         } else {
104                 p_data = NULL;
105         }
106        if (strstr(phone->Info.Models, model) != NULL)
107                return phone->Functions(GOP_Init, p_data, sm);
108        return GE_UNKNOWNMODEL;
109 }
110
111 #define MODULE(x) { \
112         extern GSM_Functions x##_Functions; \
113         extern GSM_Information x##_Information; \
114         extern bool x##_LinkOK; \
115         if (strstr(x##_Information.Models, model) != NULL) { \
116                 GSM = & x##_Functions; \
117                 GSM_Info = & x##_Information; \
118                 GSM_LinkOK = & x##_LinkOK; \
119                 return (GSM->Initialise(device, initlength, connection, rlp_callback)); \
120         } \
121 }
122
123 #define REGISTER_PHONE(x, y) { \
124         extern GSM_Phone phone_##x; \
125         if ((ret = register_phone(&phone_##x, model, y, sm)) != GE_UNKNOWNMODEL) \
126                 return ret; \
127  } 
128
129  
130 GSM_Error GSM_Initialise(char *model, char *device, char *initlength, GSM_ConnectionType connection, void (*rlp_callback)(RLP_F96Frame *frame), GSM_Statemachine *sm)
131 {
132         GSM_Error ret;
133 #ifndef WIN32  /* MB21 not supported in win32 */
134         if (strstr("2110", model)) {
135                 extern GSM_Phone phone_nokia_2110;
136                 memcpy(&(sm->Phone), &phone_nokia_2110, sizeof(GSM_Phone));
137                 sm->Phone.Functions(GOP_Init, NULL, sm);
138         }
139         MODULE(N2110);
140  
141         GSM_LinkOK = &LinkAlwaysOK;
142         sm->Link.ConnectionType=connection;
143         sm->Link.InitLength=atoi(initlength);
144         strcpy(sm->Link.PortDevice,device);
145  
146         REGISTER_PHONE(nokia_7110, NULL);
147         REGISTER_PHONE(nokia_6100, NULL);
148         REGISTER_PHONE(nokia_3110, NULL);
149         REGISTER_PHONE(at, model);
150
151 #endif /* WIN32 */ 
152         return (GE_UNKNOWNMODEL);
153 }