00001 /* ==================================================================== 00002 * The Kannel Software License, Version 1.0 00003 * 00004 * Copyright (c) 2001-2008 Kannel Group 00005 * Copyright (c) 1998-2001 WapIT Ltd. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * 3. The end-user documentation included with the redistribution, 00021 * if any, must include the following acknowledgment: 00022 * "This product includes software developed by the 00023 * Kannel Group (http://www.kannel.org/)." 00024 * Alternately, this acknowledgment may appear in the software itself, 00025 * if and wherever such third-party acknowledgments normally appear. 00026 * 00027 * 4. The names "Kannel" and "Kannel Group" must not be used to 00028 * endorse or promote products derived from this software without 00029 * prior written permission. For written permission, please 00030 * contact org@kannel.org. 00031 * 00032 * 5. Products derived from this software may not be called "Kannel", 00033 * nor may "Kannel" appear in their name, without prior written 00034 * permission of the Kannel Group. 00035 * 00036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00039 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 00040 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00041 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00042 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00043 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00044 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00045 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00046 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 * ==================================================================== 00048 * 00049 * This software consists of voluntary contributions made by many 00050 * individuals on behalf of the Kannel Group. For more information on 00051 * the Kannel Group, please see <http://www.kannel.org/>. 00052 * 00053 * Portions of this software are based upon software originally written at 00054 * WapIT Ltd., Helsinki, Finland for the Kannel project. 00055 */ 00056 00057 /* 00058 * gw/smsc_at2.h 00059 * 00060 * New driver for serial connected AT based 00061 * devices. 00062 * 4.9.2001 00063 * Andreas Fink <afink@smsrelay.com> 00064 * 00065 */ 00066 00067 #ifndef SMSC_AT2_H 00068 #define SMSC_AT2_H 00069 00070 #include "gwlib/gwlib.h" 00071 00072 /* maximum data to attempt to read in one go */ 00073 #define MAX_READ 1023 00074 00075 /* Message types defines */ 00076 #define AT_DELIVER_SM 0 00077 #define AT_SUBMIT_SM 1 00078 #define AT_STATUS_REPORT_SM 2 00079 00080 /* type of phone number defines */ 00081 #define PNT_UNKNOWN 0 00082 #define PNT_INTER 1 00083 #define PNT_NATIONAL 2 00084 00085 /* The number of times to attempt to write a line should writing fail */ 00086 #define RETRY_WRITE 3 00087 00088 /* 00089 * defines for use with the so-called "SIM buffering techinique": 00090 * once in how many seconds to poll the memory locations, 00091 * if keepalive is _not_ set (will use keepalive time if set) 00092 */ 00093 #define AT2_DEFAULT_SMS_POLL_INTERVAL 60 00094 00095 /* 00096 * Structures used in at2 00097 */ 00098 typedef struct ModemDef { 00099 Octstr *id; 00100 Octstr *name; 00101 Octstr *detect_string; 00102 Octstr *detect_string2; 00103 Octstr *init_string; 00104 Octstr *reset_string; 00105 long speed; 00106 Octstr *enable_hwhs; 00107 int need_sleep; 00108 int no_pin; 00109 int no_smsc; 00110 long sendline_sleep; 00111 Octstr *keepalive_cmd; 00112 int broken; 00113 Octstr *message_storage; 00114 int enable_mms; 00115 } ModemDef; 00116 00117 typedef struct PrivAT2data { 00118 gw_prioqueue_t *outgoing_queue; 00119 ModemDef *modem; 00120 long device_thread; 00121 int shutdown; /* Internal signal to shut down */ 00122 Octstr *device; 00123 long speed; 00124 long keepalive; 00125 int fd; /* file descriptor */ 00126 Octstr *ilb; /* input line buffer */ 00127 Octstr *lines; /* the last few lines before OK was seen */ 00128 Octstr *pin; /* PIN code */ 00129 int pin_ready; 00130 SMSCConn *conn; 00131 int phase2plus; 00132 Octstr *validityperiod; 00133 int retry; 00134 Octstr *my_number; 00135 Octstr *sms_center; 00136 Octstr *name; 00137 Octstr *configfile; 00138 Octstr *username; 00139 Octstr *password; 00140 Octstr *login_prompt; 00141 Octstr *password_prompt; 00142 int sms_memory_poll_interval; 00143 int sms_memory_capacity; 00144 int sms_memory_usage; 00145 List *pending_incoming_messages; 00146 long max_error_count; 00147 Octstr *rawtcp_host; 00148 int rawtcp_port; 00149 int is_serial; /* false if device is rawtcp */ 00150 int use_telnet; /* use telnet escape sequences */ 00151 } PrivAT2data; 00152 00153 00154 /* 00155 * Macro that is used inside smsc_at2.c in order to handle 00156 * octstr destruction more carefully. 00157 */ 00158 #define O_DESTROY(a) { if(a) octstr_destroy(a); a = NULL; } 00159 /* 00160 #define at2_write_ctrlz(a) at2_write(a,"\032") 00161 */ 00162 00163 /* 00164 * open the specified device using the serial line 00165 */ 00166 static int at2_open_device(PrivAT2data *privdata); 00167 00168 /* 00169 * close the specified device and hence disconnect from the serial line 00170 */ 00171 static void at2_close_device(PrivAT2data *privdata); 00172 00173 /* 00174 * checks if there are any incoming bytes and adds them to the line buffer 00175 */ 00176 static void at2_read_buffer(PrivAT2data *privdata); 00177 00178 /* 00179 * Looks for a full line to be read from the buffer. 00180 * Returns the line and removes it from the buffer or if no full line 00181 * is yet received waits until the line is there or a timeout occurs. 00182 * If gt_flag is set, it is also looking for a line containing '>' even 00183 * there is no CR yet. 00184 */ 00185 static Octstr *at2_wait_line(PrivAT2data *privdata, time_t timeout, int gt_flag); 00186 00187 /* 00188 * Looks for a full line to be read from the buffer. 00189 * Returns the line and removes it from the buffer or if no full line 00190 * is yet received returns NULL. If gt_flag is set, it is also looking for 00191 * a line containing > even there is no CR yet. 00192 */ 00193 static Octstr *at2_read_line(PrivAT2data *privdata, int gt_flag); 00194 00195 /* 00196 * Writes a line out to the device and adds a carriage return/linefeed to it. 00197 * Returns number of characters sent. 00198 */ 00199 static int at2_write_line(PrivAT2data *privdata, char *line); 00200 static int at2_write_ctrlz(PrivAT2data *privdata); 00201 static int at2_write(PrivAT2data *privdata, char *line); 00202 00203 /* 00204 * Clears incoming buffer 00205 */ 00206 static void at2_flush_buffer(PrivAT2data *privdata); 00207 00208 /* 00209 * Initializes the device after being opened, detects the modem type, 00210 * sets speed settings etc. 00211 * On failure returns -1. 00212 */ 00213 static int at2_init_device(PrivAT2data *privdata); 00214 00215 /* 00216 * Sends an AT command to the modem and waits for a reply 00217 * Return values are: 00218 * 0 = OK 00219 * 1 = ERROR 00220 * 2 = SIM PIN 00221 * 3 = > 00222 * 4 = READY 00223 * 5 = CMGS 00224 * -1 = timeout occurred 00225 */ 00226 static int at2_send_modem_command(PrivAT2data *privdata, char *cmd, time_t timeout, 00227 int greaterflag); 00228 00229 /* 00230 * Waits for the modem to send us something. 00231 */ 00232 static int at2_wait_modem_command(PrivAT2data *privdata, time_t timeout, 00233 int greaterflag, int *output); 00234 00235 /* 00236 * Sets the serial port speed on the device 00237 */ 00238 static void at2_set_speed(PrivAT2data *privdata, int bps); 00239 00240 /* 00241 * This is the main tread "sitting" on the device. 00242 * Its task is to initialize the modem then wait for messages 00243 * to arrive or to be sent 00244 */ 00245 static void at2_device_thread(void *arg); 00246 00247 static int at2_shutdown_cb(SMSCConn *conn, int finish_sending); 00248 static long at2_queued_cb(SMSCConn *conn); 00249 static void at2_start_cb(SMSCConn *conn); 00250 static int at2_add_msg_cb(SMSCConn *conn, Msg *sms); 00251 00252 /* 00253 * Starts the whole thing up 00254 */ 00255 int smsc_at2_create(SMSCConn *conn, CfgGroup *cfg); 00256 00257 /* 00258 * Extracts the first PDU in the string 00259 */ 00260 static int at2_pdu_extract(PrivAT2data *privdata, Octstr **pdu, Octstr *line, Octstr *smsc_number); 00261 00262 /* 00263 * Get the numeric value of the text hex 00264 */ 00265 static int at2_hexchar(int hexc); 00266 00267 /* 00268 * Decode a raw PDU into a Msg 00269 */ 00270 static Msg *at2_pdu_decode(Octstr *data, PrivAT2data *privdata); 00271 00272 /* 00273 * Decode a DELIVER PDU 00274 */ 00275 static Msg *at2_pdu_decode_deliver_sm(Octstr *data, PrivAT2data *privdata); 00276 00277 /* 00278 * Decode a SUBMIT-REPORT PDU 00279 */ 00280 static Msg *at2_pdu_decode_report_sm(Octstr *data, PrivAT2data *privdata); 00281 00282 /* 00283 * Converts the text representation of hexa to binary 00284 */ 00285 static Octstr *at2_convertpdu(Octstr *pdutext); 00286 00287 /* 00288 * Decode 7bit uncompressed user data 00289 */ 00290 static void at2_decode7bituncompressed(Octstr *input, int len, Octstr *decoded, 00291 int offset); 00292 00293 /* 00294 * Sends messages from the queue 00295 */ 00296 static void at2_send_messages(PrivAT2data *privdata); 00297 00298 /* 00299 * Sends a single message. 00300 * After having it sent, the msg is no longe belonging to us 00301 */ 00302 static void at2_send_one_message(PrivAT2data *privdata, Msg *msg); 00303 00304 /* 00305 * Encode a Msg into a PDU 00306 */ 00307 static Octstr *at2_pdu_encode(Msg *msg, PrivAT2data *privdata); 00308 00309 /* 00310 * Encode 7bit uncompressed user data into an Octstr, prefixing with <offset> 0 bits 00311 */ 00312 static Octstr *at2_encode7bituncompressed(Octstr *input, int offset); 00313 00314 /* 00315 * Encode 8bit uncompressed user data into an Octstr 00316 */ 00317 static Octstr *at2_encode8bituncompressed(Octstr *input); 00318 00319 /* 00320 * Code a half-byte to its text hexa representation 00321 */ 00322 static int at2_numtext(int num); 00323 00324 /* 00325 * Try to detect modem speeds 00326 */ 00327 static int at2_detect_speed(PrivAT2data *privdata); 00328 00329 /* 00330 * Test modem speed 00331 */ 00332 static int at2_test_speed(PrivAT2data *privdata, long speed); 00333 00334 /* 00335 * Try to detect modem type 00336 */ 00337 static int at2_detect_modem_type(PrivAT2data *privdata); 00338 00339 /* 00340 * Read all defined modems from the included modem definition file 00341 */ 00342 static ModemDef *at2_read_modems(PrivAT2data *privdata, Octstr *file, Octstr *id, int idnumber); 00343 00344 /* 00345 * Destroy the ModemDef structure components 00346 */ 00347 static void at2_destroy_modem(ModemDef *modem); 00348 00349 /* 00350 * Checks whether any messages are buffered in message storage and extract them. 00351 */ 00352 static int at2_read_sms_memory(PrivAT2data *privdata); 00353 00354 /* 00355 * Memory capacity and usage check 00356 */ 00357 static int at2_check_sms_memory(PrivAT2data *privdata); 00358 00359 /* 00360 * This silly thing here will just translate a "swapped nibble" 00361 * pseodo Hex encoding (from PDU) into something that people can 00362 * actually understand. 00363 * Implementation completly ripped off Dennis Malmstrom timestamp 00364 * patches against 1.0.3. Thanks Dennis! 00365 */ 00366 static int swap_nibbles(unsigned char byte); 00367 00368 /* 00369 * creates a buffer with a valid PDU address field as per [GSM 03.40] 00370 * from an MSISDN number 00371 */ 00372 static Octstr *at2_format_address_field(Octstr *msisdn); 00373 00374 /* 00375 * Check the pending_incoming_messages queue for CMTI notifications. 00376 * Every notification is parsed and the messages are read (and deleted) 00377 * accordingly. 00378 */ 00379 static void at2_read_pending_incoming_messages(PrivAT2data *privdata); 00380 00381 /* 00382 * Set the memory storage location of the modem by sending a +CPMS command 00383 */ 00384 static int at2_set_message_storage(PrivAT2data *privdata, Octstr *memory_name); 00385 00386 /* 00387 * Reads a message from selected memory location and deletes it afterwards. 00388 * returns 0 on failure and 1 on success 00389 */ 00390 static int at2_read_delete_message(PrivAT2data *privdata, int message_number); 00391 00392 /* 00393 * Return appropriate error string for the given error code. 00394 */ 00395 static const char *at2_error_string(int code); 00396 00397 #endif /* SMSC_AT2_H */ 00398