skeleton.c
上传用户:mei_mei897
上传日期:2007-01-05
资源大小:82k
文件大小:8k
源码类别:

手机短信编程

开发平台:

Unix_Linux

  1. /* -------------------------------------------------------------------- */
  2. /* SMS Client, send messages to mobile phones and pagers */
  3. /* */
  4. /* skeleton.c */
  5. /* */
  6. /*  Copyright (C) 1998 Angelo Masci */
  7. /* */
  8. /*  This library is free software; you can redistribute it and/or */
  9. /*  modify it under the terms of the GNU Library General Public */
  10. /*  License as published by the Free Software Foundation; either */
  11. /*  version 2 of the License, or (at your option) any later version. */
  12. /* */
  13. /*  This library is distributed in the hope that it will be useful, */
  14. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of */
  15. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
  16. /*  Library General Public License for more details. */
  17. /* */
  18. /*  You should have received a copy of the GNU Library General Public */
  19. /*  License along with this library; if not, write to the Free */
  20. /*  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  21. /* */
  22. /*  You can contact the author at this e-mail address: */
  23. /* */
  24. /*  angelo@styx.demon.co.uk */
  25. /* */
  26. /* -------------------------------------------------------------------- */
  27. /* $Id$
  28.    -------------------------------------------------------------------- */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "common.h"
  32. #include "logfile.h"
  33. #include "driver.h"
  34. #include "expect.h"
  35. #include "sms_error.h"
  36. #include "sms_resource.h"
  37. /* -------------------------------------------------------------------- */
  38. static char ACK1[] = "Welecome to Skeleton Serverrn";
  39. static char ACK2[] = "Enter you numberrn";
  40. static char ACK3[] = "Enter you messagern";
  41. static char ACK4[] = "Thank You.rn";
  42.      
  43. /* -------------------------------------------------------------------- */
  44. /* The 'env' structure contains Service level data to be used for */
  45. /* sending a message. */
  46. /* -------------------------------------------------------------------- */
  47. static struct skeleton_env
  48. {
  49. DRIVER_DEFAULT_ENV def;
  50. /* Place any extended driver */ 
  51. /* variables here  */
  52. } driver_env;
  53. /* -------------------------------------------------------------------- */
  54. static  RESOURCE resource_list[] = 
  55. {
  56. { RESOURCE_STRING,  "SMS_comms_params",  0, 1, NULL, 0,  "8N1",      0,    &(driver_env.def.comms_params)   },
  57. { RESOURCE_STRING,  "SMS_centre_number",  0, 1, NULL, 0,  NULL,       0,    &(driver_env.def.centre_number)   },
  58. { RESOURCE_NUMERIC, "SMS_baud",  0, 1, NULL, 0,  NULL,       1200, &(driver_env.def.baud)   },
  59. { RESOURCE_NUMERIC, "SMS_deliver_timeout",  0, 0, NULL, 0,  NULL,       30,   &(driver_env.def.deliver_timeout)   },
  60. { RESOURCE_NUMERIC, "SMS_timeout",  0, 0, NULL, 0,  NULL,       10,   &(driver_env.def.timeout)   },
  61. { RESOURCE_NUMERIC, "SMS_write_timeout",  0, 0, NULL, 0,  NULL,       10,   &(driver_env.def.write_timeout)   },
  62. { RESOURCE_NUMERIC, "SMS_max_deliver",  0, 0, NULL, 0,  NULL,       0,    &(driver_env.def.max_deliver)    },
  63. { RESOURCE_NULL,     NULL,  0, 1, NULL, 0,  NULL,       0,    NULL   }
  64. };
  65. /* -------------------------------------------------------------------- */
  66. #define DELIVERTIMEOUT  (driver_env.def.deliver_timeout)
  67. #define TIMEOUT  (driver_env.def.timeout)
  68. #define WRITETIMEOUT  (driver_env.def.write_timeout)
  69. /* -------------------------------------------------------------------- */
  70. #define FD (driver_env.def.fd)
  71. /* -------------------------------------------------------------------- */
  72. /* The following defines are used to signify errors */
  73. /* occuring during the dialogue between the driver and the */
  74. /* service centre. This number will be returned as the delivery */
  75. /* value for the message that was aborted */
  76. /* */
  77. /* The defines MUST be placed in 'sms_error.h' */
  78. #define ESKELETON_NONUMBER  250
  79. #define ESKELETON_NOMESSAGE 251
  80. #define ESKELETON_NODELIVERY 252
  81. /* -------------------------------------------------------------------- */
  82. static void SKELETON_hangup(void);
  83. static int SKELETON_sendmessage(char *msisdn, char *message);
  84. /* -------------------------------------------------------------------- */
  85. /* -------------------------------------------------------------------- */
  86. static void SKELETON_hangup(void)
  87. { default_hangup((DRIVER_DEFAULT_ENV *)(&driver_env));
  88. }
  89. /* -------------------------------------------------------------------- */
  90. /* -------------------------------------------------------------------- */
  91. static int SKELETON_sendmessage(char *msisdn, char *message)
  92. {
  93. char buf[MAX_RESPONSE_BUFSIZE];
  94. if (expstr(FD, buf, ACK2, MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
  95. {
  96. lprintf(LOG_STANDARD, "Received Number Requestn");
  97. }
  98. else
  99. { lprintf(LOG_STANDARD, "No Number Requestn");
  100. SKELETON_hangup();
  101. return ESKELETON_NONUMBER;
  102. }
  103. twrite(FD, msisdn, sms_strlen(msisdn), WRITETIMEOUT);
  104. twrite(FD, "rn", sms_strlen("rn"), WRITETIMEOUT);
  105. if (expstr(FD, buf, ACK3, MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
  106. {
  107. lprintf(LOG_STANDARD, "Received Message Requestn");
  108. }
  109. else
  110. { lprintf(LOG_STANDARD, "No Message Requestn");
  111. SKELETON_hangup();
  112. return ESKELETON_NOMESSAGE;
  113. }
  114. twrite(FD, message, sms_strlen(message), WRITETIMEOUT);
  115. twrite(FD, "rn", sms_strlen("rn"), WRITETIMEOUT);
  116. if (expstr(FD, buf, ACK4, MAX_RESPONSE_BUFSIZE, DELIVERTIMEOUT) == 0)
  117. {
  118. lprintf(LOG_STANDARD, "Received Message Delivery Responsen");
  119. }
  120. else
  121. { lprintf(LOG_STANDARD, "No Message Delivery Responsen");
  122. SKELETON_hangup();
  123. return ESKELETON_NODELIVERY;
  124. }
  125. return 0;
  126. }
  127. /* -------------------------------------------------------------------- */
  128. /* The following structure is used by core driver code.  */
  129. /* The diagram below shows the call sequence of the functions. */
  130. /* -------------------------------------------------------------------- */
  131. DEVICE_ENTRY skeleton_device = {
  132. "SKELETON",
  133. resource_list,
  134. (DRIVER_DEFAULT_ENV *)(&driver_env),
  135. default_init, /* Init */ 
  136. default_main, /* Main */
  137. default_validate_numeric_id, /* Validation */
  138. default_dial, /* Dial */
  139. default_hangup, /* Hangup */
  140. default_send_disconnect, /* Disconnect */
  141. default_single_deliver, /* Deliver  */
  142. SKELETON_sendmessage, /* Send */
  143. default_login /* Login */
  144. };
  145. /* -------------------------------------------------------------------- */
  146. /*                                                                      */
  147. /*       ----------                                                     */
  148. /*       |  Main  |                                                     */
  149. /*       ----+-----                                                     */
  150. /*           |                                                          */
  151. /*           |                                                          */
  152. /*           | For Each Addressed Message                               */
  153. /*      -----+-----                                                     */
  154. /*      | Deliver |                                                     */
  155. /*      -----+-----                                                     */
  156. /*           |                                                          */
  157. /*      +----+------+----------+-------------+--------------+           */
  158. /*      |           |          |             |              |           */
  159. /*   ---+----   ----+----   ---+----   ------+-------   ----+-----      */
  160. /*   | Dial |   | Login |   | Send |   | Disconnect |   | Hangup |      */
  161. /*   --------   ---------   --------   --------------   ----------      */
  162. /*                                                                      */
  163. /*                                                                      */
  164. /* Note. Your driver can supply functions to be used in place of        */
  165. /*       all the default_driver functions.                              */
  166. /*                                                                      */
  167. /* -------------------------------------------------------------------- */