b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <stdlib.h>
|
| 3 | #include <stdbool.h>
|
| 4 | #include <dlfcn.h>
|
| 5 | #include <stdint.h>
|
| 6 | #include <string.h>
|
| 7 |
|
| 8 |
|
| 9 | #define GSW_SMS_SEND_NUM_MAX 5 /**< dest num max count */
|
| 10 | #define GSW_SMS_ADDRESS_LEN 32 /**< one dest number max length*/
|
| 11 | #define GSW_SMS_CONCAT_SMS_COUNT_MAX 160 /**< one page max bytes*/
|
| 12 | #define GSW_SMS_MSG_CONTENT_LEN_MAX 3 /**< sms page max count*/
|
| 13 | #define GSW_SMS_CONTENT_LEN_MAX 3 /**< newmsg recv one page*/
|
| 14 | #define GSW_SMS_SEND_CONT_MAX (GSW_SMS_MSG_CONTENT_LEN_MAX*GSW_SMS_CONCAT_SMS_COUNT_MAX*2) /**< sms send max len*/
|
| 15 | #define GSW_SMS_RECV_CONT_MAX 1440 /**< sms receive max len*/
|
| 16 |
|
| 17 | #define GSW_HAL_SMS_ADDRESS_LEN GSW_SMS_ADDRESS_LEN
|
| 18 | #define GSW_HAL_SMS_RECV_CONT_MAX GSW_SMS_RECV_CONT_MAX
|
| 19 |
|
| 20 | typedef unsigned short uint16_t;
|
| 21 | typedef unsigned int uint32_t;
|
| 22 | typedef unsigned short uint16;
|
| 23 | typedef uint32_t sms_client_handle_type;
|
| 24 |
|
| 25 | typedef enum
|
| 26 | {
|
| 27 | GSW_SMS_FULL_FLG, /**< sms full flag*/
|
| 28 | GSW_SMS_RECEIVED_FLG, /**<recv new sms flag*/
|
| 29 | }gsw_sms_state_e;
|
| 30 |
|
| 31 | typedef enum
|
| 32 | {
|
| 33 | SMS_FORMAT_GSM_7BIT = 0, /**< 7bit econde*/
|
| 34 | SMS_FORMAT_BINARY_DATA = 1, /**< 8bit binary encode*/
|
| 35 | SMS_FORMAT_UCS2 = 2, /**< ucs2 encode*/
|
| 36 | }gsw_sms_format_e;
|
| 37 |
|
| 38 | typedef struct gsw_hal_sms_date
|
| 39 | {
|
| 40 | unsigned char year[5]; /**< year of date*/
|
| 41 | unsigned char month[3]; /**< month of date*/
|
| 42 | unsigned char day[3]; /**< day of date*/
|
| 43 | unsigned char hour[3]; /**< hour of time*/
|
| 44 | unsigned char minutes[3]; /**< minute of time*/
|
| 45 | unsigned char seconds[3]; /**< second of time*/
|
| 46 | unsigned char timezone[4]; /**< timezone*/
|
| 47 | } gsw_sms_date_t;
|
| 48 |
|
| 49 | typedef struct gsw_hal_sms_msg_type
|
| 50 | {
|
| 51 | char src_num[GSW_SMS_ADDRESS_LEN+1]; /**< sms phone num send msg*/
|
| 52 | char dest_num[GSW_SMS_ADDRESS_LEN + 1]; /**< sms phone num recv msg*/
|
| 53 | gsw_sms_format_e content_encode; /**< sms content is 7bit or 8bit or Ucs2 encode*/
|
| 54 | unsigned int content_len; /**< sms content size*/
|
| 55 | char content[GSW_SMS_RECV_CONT_MAX + 1]; /**< sms content*/
|
| 56 | gsw_sms_date_t date; /**< message time*/
|
| 57 | } gsw_sms_msg_type_t;
|
| 58 |
|
| 59 | typedef void (* GSW_HAL_SMS_CALLBACK_FUN)(gsw_sms_state_e state, gsw_sms_msg_type_t *report_info);
|
| 60 |
|
| 61 |
|
| 62 | int (*gsw_sms_reg_callback)(GSW_HAL_SMS_CALLBACK_FUN handle_ptr);
|
| 63 | int (*gsw_sms_sdk_init)(int32_t token);
|
| 64 | int (*gsw_sms_sdk_deinit)(void);
|
| 65 | int (*gsw_send_sms)(int8_t *phone_num, int32_t char_set, int8_t *msg, int32_t msg_len);
|
| 66 | int (*gsw_get_smsc_address)(int32_t len, int8_t *smsc);
|
| 67 | int (*gsw_set_smsc_address)(const int8_t *smsc);
|
| 68 |
|
| 69 | #define lib_gsw_sms_path "/lib/libgsw_lib.so"
|
| 70 | static void *dlHandle_sms;
|
| 71 |
|
| 72 |
|
| 73 | void gsw_test_callback(gsw_sms_state_e state, gsw_sms_msg_type_t *report_info)
|
| 74 | {
|
| 75 | printf("gsw_test_callback: state = %d\n", state);
|
| 76 | printf("gsw_test_callback: report_info->src_num = %s\n", report_info->src_num);
|
| 77 | printf("gsw_test_callback: report_info->dest_num = %s\n", report_info->dest_num);
|
| 78 | printf("gsw_test_callback: report_info->content_encode = %d\n", report_info->content_encode);
|
| 79 | printf("gsw_test_callback: report_info->content_len = %d\n", report_info->content_len);
|
| 80 | printf("gsw_test_callback: report_info->content = %s\n", report_info->content);
|
| 81 | printf("gsw_test_callback: report_info->date->year = %s\n", report_info->date.year);
|
| 82 | printf("gsw_test_callback: report_info->date->month = %s\n", report_info->date.month);
|
| 83 | printf("gsw_test_callback: report_info->date->day = %s\n", report_info->date.day);
|
| 84 | printf("gsw_test_callback: report_info->date->hour = %s\n", report_info->date.hour);
|
| 85 | printf("gsw_test_callback: report_info->date->minutes = %s\n", report_info->date.minutes);
|
| 86 | printf("gsw_test_callback: report_info->date->seconds = %s\n", report_info->date.seconds);
|
| 87 | printf("gsw_test_callback: report_info->date->timezone = %s\n", report_info->date.timezone);
|
| 88 | }
|
| 89 |
|
| 90 |
|
| 91 | static int gsw_sms_api_import()
|
| 92 | {
|
| 93 | dlHandle_sms = dlopen(lib_gsw_sms_path, RTLD_NOW);
|
| 94 | if (dlHandle_sms == NULL) {
|
| 95 | printf("Failed to open %s\n", lib_gsw_sms_path);
|
| 96 | return -1;
|
| 97 | }
|
| 98 |
|
| 99 | gsw_sms_sdk_init = (int(*)(int32_t token))dlsym(dlHandle_sms,"gsw_sms_sdk_init");
|
| 100 | if(gsw_sms_sdk_init == NULL)
|
| 101 | {
|
| 102 | printf("gsw_sms_sdk_init fail\n");
|
| 103 | return -1;
|
| 104 | }
|
| 105 |
|
| 106 | gsw_sms_reg_callback = (int(*)(GSW_HAL_SMS_CALLBACK_FUN handle_ptr))dlsym(dlHandle_sms,"gsw_sms_reg_callback");
|
| 107 | if(gsw_sms_reg_callback == NULL)
|
| 108 | {
|
| 109 | printf("gsw_sms_reg_callback fail\n");
|
| 110 | return -1;
|
| 111 | }
|
| 112 |
|
| 113 | gsw_send_sms = (int(*)(int8_t *phone_num, int32_t char_set, int8_t *msg, int32_t msg_len))dlsym(dlHandle_sms,"gsw_send_sms");
|
| 114 | if(gsw_send_sms == NULL)
|
| 115 | {
|
| 116 | printf("gsw_send_sms fail\n");
|
| 117 | return -1;
|
| 118 | }
|
| 119 |
|
| 120 | gsw_get_smsc_address = (int(*)(int32_t len, int8_t *smsc))dlsym(dlHandle_sms,"gsw_get_smsc_address");
|
| 121 | if(gsw_get_smsc_address == NULL)
|
| 122 | {
|
| 123 | printf("gsw_get_smsc_address fail\n");
|
| 124 | return -1;
|
| 125 | }
|
| 126 |
|
| 127 | gsw_set_smsc_address = (int(*)(const int8_t *smsc))dlsym(dlHandle_sms,"gsw_set_smsc_address");
|
| 128 | if(gsw_set_smsc_address == NULL)
|
| 129 | {
|
| 130 | printf("gsw_set_smsc_address fail\n");
|
| 131 | return -1;
|
| 132 | }
|
| 133 |
|
| 134 | gsw_sms_sdk_deinit = (int(*)(void))dlsym(dlHandle_sms,"gsw_sms_sdk_deinit");
|
| 135 | if(gsw_sms_sdk_deinit == NULL)
|
| 136 | {
|
| 137 | printf("gsw_sms_sdk_deinit fail\n");
|
| 138 | return -1;
|
| 139 | }
|
| 140 |
|
| 141 | return 0;
|
| 142 | }
|
| 143 |
|
| 144 | int main()
|
| 145 | {
|
| 146 | int ret = -1;
|
| 147 | int opt = -1;
|
| 148 | char operator[10];
|
| 149 | char phone_number[GSW_HAL_SMS_ADDRESS_LEN] = {0};
|
| 150 | char serv_numer[GSW_HAL_SMS_ADDRESS_LEN] = {0};
|
| 151 |
|
| 152 | ret = gsw_sms_api_import();
|
| 153 | if(ret != 0)
|
| 154 | {
|
| 155 | printf("gsw_sms_api_import fail\n");
|
| 156 | return -1;
|
| 157 | }
|
| 158 |
|
| 159 | while(1)
|
| 160 | {
|
| 161 | printf("=========sms main=========\n"
|
| 162 | "\t-1 exit\n"
|
| 163 | "\t1 sms init\n"
|
| 164 | "\t2 send text sms\n"
|
| 165 | "\t3 wait receive new sms\n"
|
| 166 | "\t4 send PDU sms\n"
|
| 167 | "\t7 query service number\n"
|
| 168 | "\t8 set service number\n"
|
| 169 | "\t9 deinit sms\n"
|
| 170 | "operator: >> \n");
|
| 171 |
|
| 172 | opt = -1;
|
| 173 | ret = -1;
|
| 174 | printf("%s\n",fgets(operator, sizeof(operator), stdin));
|
| 175 | fflush(stdin);
|
| 176 | opt = atoi(operator);
|
| 177 | switch (opt)
|
| 178 | {
|
| 179 | case -1:
|
| 180 | {
|
| 181 | printf("main exit\n");
|
| 182 | return 0;
|
| 183 | }
|
| 184 | case 1:
|
| 185 | {
|
| 186 | printf("gsw_sms_sdk_init start");
|
| 187 | ret = gsw_sms_sdk_init(1234);
|
| 188 | if(ret != 0)
|
| 189 | {
|
| 190 | printf("gsw_sms_sdk_init fail,ret = %d\n",ret);
|
| 191 | dlclose(dlHandle_sms);
|
| 192 | continue;
|
| 193 | }
|
| 194 |
|
| 195 | break;
|
| 196 | }
|
| 197 | case 2:
|
| 198 | {
|
| 199 | char *tmp = "all man hello world";
|
| 200 | printf("input phone number:\n");
|
| 201 | memset(phone_number, 0x0, GSW_HAL_SMS_ADDRESS_LEN);
|
| 202 | //fgets(phone_number, MAX_LEN, stdin);
|
| 203 | printf("%d\n",scanf("%15s", phone_number));
|
| 204 | fflush(stdin);
|
| 205 | int len = strlen(tmp);
|
| 206 | ret = gsw_send_sms((int8_t*)phone_number, 0, (int8_t*)tmp, len);
|
| 207 | if(ret != 0)
|
| 208 | {
|
| 209 | printf("gsw_send_sms fail,ret = %d\n",ret);
|
| 210 | continue;
|
| 211 | }
|
| 212 | break;
|
| 213 | }
|
| 214 |
|
| 215 | case 3:
|
| 216 | {
|
| 217 | printf("gsw_sms_reg_callback start\n");
|
| 218 | ret = gsw_sms_reg_callback(gsw_test_callback);
|
| 219 | if(ret != 0)
|
| 220 | {
|
| 221 | printf("gsw_sms_reg_callback fail,ret = %d\n",ret);
|
| 222 | continue;
|
| 223 | }
|
| 224 | printf("gsw_sms_reg_callback end");
|
| 225 | break;
|
| 226 | }
|
| 227 |
|
| 228 | case 4:
|
| 229 | {
|
| 230 | char *tmp = "你好";
|
| 231 | printf("input phone number:\n");
|
| 232 | memset(phone_number, 0x0, GSW_HAL_SMS_ADDRESS_LEN);
|
| 233 | //fgets(phone_number, MAX_LEN, stdin);
|
| 234 | printf("%d\n",scanf("%15s", phone_number));
|
| 235 | fflush(stdin);
|
| 236 | int len = strlen(tmp);
|
| 237 | ret = gsw_send_sms((int8_t*)phone_number, 2, (int8_t *)tmp, len);
|
| 238 | if(ret != 0)
|
| 239 | {
|
| 240 | printf("gsw_send_sms fail,ret = %d\n",ret);
|
| 241 | continue;
|
| 242 | }
|
| 243 | break;
|
| 244 | }
|
| 245 |
|
| 246 | case 7:
|
| 247 | {
|
| 248 | printf("gsw_get_smsc_address start\n");
|
| 249 | ret = gsw_get_smsc_address(GSW_HAL_SMS_ADDRESS_LEN, (int8_t *)serv_numer);
|
| 250 | if(ret != 0)
|
| 251 | {
|
| 252 | printf("gsw_get_smsc_address fail,ret = %d\n",ret);
|
| 253 | continue;
|
| 254 | }
|
| 255 | printf("smsc: %s\n", serv_numer);
|
| 256 | break;
|
| 257 | }
|
| 258 |
|
| 259 | case 8:
|
| 260 | {
|
| 261 | printf("input phone number:\n");
|
| 262 | memset(phone_number, 0x0, GSW_HAL_SMS_ADDRESS_LEN);
|
| 263 | //fgets(phone_number, MAX_LEN, stdin);
|
| 264 | printf("%d\n",scanf("%15s", phone_number));
|
| 265 | fflush(stdin);
|
| 266 | printf("start memcpy\n");
|
| 267 | memcpy(serv_numer, phone_number, GSW_HAL_SMS_ADDRESS_LEN);
|
| 268 | ret = gsw_set_smsc_address((const int8_t *)serv_numer);
|
| 269 | if(ret != 0)
|
| 270 | {
|
| 271 | printf("gsw_set_smsc_address fail,ret = %d\n",ret);
|
| 272 | continue;
|
| 273 | }
|
| 274 | printf("smsc: %s\n", serv_numer);
|
| 275 | break;
|
| 276 | }
|
| 277 |
|
| 278 |
|
| 279 | case 9:
|
| 280 | {
|
| 281 |
|
| 282 | printf("gsw_sms_sdk_deinit start\n");
|
| 283 | ret = gsw_sms_sdk_deinit();
|
| 284 | if(ret != 0)
|
| 285 | {
|
| 286 | printf("gsw_sms_sdk_deinit fail,ret = %d\n",ret);
|
| 287 | dlclose(dlHandle_sms);
|
| 288 | continue;
|
| 289 | }
|
| 290 | break;
|
| 291 | }
|
| 292 |
|
| 293 | default:
|
| 294 | {
|
| 295 | continue;
|
| 296 | }
|
| 297 |
|
| 298 | }
|
| 299 |
|
| 300 |
|
| 301 | }
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 | }
|
| 308 |
|