🗽 Sync 2026-06-04 15:57:16

This commit is contained in:
github-actions[bot] committed 2026-06-04 15:57:17 +08:00
1 parent 6775767ca3
commit 2b3a854c9d
40 files changed
+3470 -3049

No files matched your search

+31 -25
View File
@@ -1,28 +1,34 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=sendat
PKG_RELEASE:=1
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/ouyangzq/sendat.git
PKG_SOURCE_DATE:=2023-10-26
PKG_SOURCE_VERSION:=624c58f6331e5be3eac6d144ed985b4a4af94b10
PKG_MIRROR_HASH:=skip
MAKE_PATH:=src
include $(INCLUDE_DIR)/package.mk
include $(TOPDIR)/rules.mk#将openwrt顶层目录下的rules.mk文件中的内容导入进来
PKG_NAME:=sendat#软件包名
PKG_VERSION:=6.8.2#软件包版本
PKG_RELEASE:=2
PKG_BUILD_DIR:= $(BUILD_DIR)/$(PKG_NAME)#真正编译当前软件包的目录
include $(INCLUDE_DIR)/package.mk#将$(TOPDIR)/include目录下的package.mk文件中的内容导入进来
define Package/sendat
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Send AT Commnad
DEPENDS:=+libpthread
SECTION:=wrtnode#软件包类型
CATEGORY:=Daocaoren#menuconfig中软件包所属的一级目录
SUBMENU :=CPE#menuconfig中软件包所属的二级目录
TITLE:=Sendat AT#软件包标题
DEPENDS:=+libpthread#运行本软件依赖的其他包
endef
define Package/sendat/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/sendat $(1)/usr/bin/
define Package/sendat/description #软件包描述
A sample for sendat test
endef
$(eval $(call BuildPackage,sendat))
define Build/Prepare #编译之前的准备动作
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/sendat/install #软件包的安装方法,主要就是将一系列编译好的文件、启动脚本、UCI配置文件等拷贝到指定位置
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/sendat $(1)/bin/
endef
$(eval $(call BuildPackage,sendat))
+5
View File
@@ -0,0 +1,5 @@
# sendat
AT 命令工具
1简化使用AT命令请求方式
使用命令说明
sendat 2 'ATI' 2表示 /dev/ttyUSB2 'ATI' 为at命令
+9
View File
@@ -0,0 +1,9 @@
all:sendat
OBJS = sendat.o
sendat:$(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) -lpthread
clean:
rm -f sendat *.o
+420
View File
@@ -0,0 +1,420 @@
/*
* 2017 - 2021 Cezary Jackiewicz <cezary@eko.one.pl>
* 2014 lovewilliam <ztong@vt.edu>
*/
// Copyright 2011 The Avalon Project Authors. All rights reserved.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the LICENSE file.
//
// SMS encoding/decoding functions, which are based on examples from:
// http://www.dreamfabric.com/sms/
#include "pdu.h"
#include <string.h>
#include <time.h>
enum {
BITMASK_7BITS = 0x7F,
BITMASK_8BITS = 0xFF,
BITMASK_HIGH_4BITS = 0xF0,
BITMASK_LOW_4BITS = 0x0F,
TYPE_OF_ADDRESS_UNKNOWN = 0x81,
TYPE_OF_ADDRESS_INTERNATIONAL_PHONE = 0x91,
TYPE_OF_ADDRESS_NATIONAL_SUBSCRIBER = 0xC8,
TYPE_OF_ADDRESS_ALPHANUMERIC = 0xD0,
SMS_DELIVER_ONE_MESSAGE = 0x04,
SMS_SUBMIT = 0x11,
SMS_MAX_7BIT_TEXT_LENGTH = 160,
};
// Swap decimal digits of a number (e.g. 12 -> 21).
static unsigned char
SwapDecimalNibble(const unsigned char x)
{
return (x / 16) + ((x % 16) * 10);
}
// Encode/Decode PDU: Translate ASCII 7bit characters to 8bit buffer.
// SMS encoding example from: http://www.dreamfabric.com/sms/.
//
// 7-bit ASCII: "hellohello"
// [0]:h [1]:e [2]:l [3]:l [4]:o [5]:h [6]:e [7]:l [8]:l [9]:o
// 1101000 1100101 1101100 1101100 1101111 1101000 1100101 1101100 1101100 1101111
// | ||| ||||| | ||||||| ||||||
// /-------------/ ///-------/// /////-///// \------------\ ||||||| \\\\\\ .
// | ||| ||||| | ||||||| ||||||
// input buffer position
// 10000000 22111111 33322222 44443333 55555333 66666655 77777776 98888888 --999999
// | ||| ||||| | ||||||| ||||||
// 8bit encoded buffer
// 11101000 00110010 10011011 11111101 01000110 10010111 11011001 11101100 00110111
// E8 32 9B FD 46 97 D9 EC 37
// Encode PDU message by merging 7 bit ASCII characters into 8 bit octets.
int
EncodePDUMessage(const char* sms_text, int sms_text_length, unsigned char* output_buffer, int buffer_size)
{
// Check if output buffer is big enough.
if ((sms_text_length * 7 + 7) / 8 > buffer_size)
return -1;
int output_buffer_length = 0;
int carry_on_bits = 1;
int i = 0;
for (; i < sms_text_length - 1; ++i) {
output_buffer[output_buffer_length++] =
((sms_text[i] & BITMASK_7BITS) >> (carry_on_bits - 1)) |
((sms_text[i + 1] & BITMASK_7BITS) << (8 - carry_on_bits));
carry_on_bits++;
if (carry_on_bits == 8) {
carry_on_bits = 1;
++i;
}
}
if (i <= sms_text_length)
output_buffer[output_buffer_length++] = (sms_text[i] & BITMASK_7BITS) >> (carry_on_bits - 1);
return output_buffer_length;
}
// Decode PDU message by splitting 8 bit encoded buffer into 7 bit ASCII
// characters.
int
DecodePDUMessage_GSM_7bit(const unsigned char* buffer, int buffer_length, char* output_sms_text, int sms_text_length)
{
int output_text_length = 0;
if (buffer_length > 0)
output_sms_text[output_text_length++] = BITMASK_7BITS & buffer[0];
if (sms_text_length > 1) {
int carry_on_bits = 1;
int i = 1;
for (; i < buffer_length; ++i) {
output_sms_text[output_text_length++] = BITMASK_7BITS & ((buffer[i] << carry_on_bits) | (buffer[i - 1] >> (8 - carry_on_bits)));
if (output_text_length == sms_text_length) break;
carry_on_bits++;
if (carry_on_bits == 8) {
carry_on_bits = 1;
output_sms_text[output_text_length++] = buffer[i] & BITMASK_7BITS;
if (output_text_length == sms_text_length) break;
}
}
if (output_text_length < sms_text_length) // Add last remainder.
output_sms_text[output_text_length++] = buffer[i - 1] >> (8 - carry_on_bits);
}
return output_text_length;
}
#define GSM_7BITS_ESCAPE 0x1b
static const unsigned char gsm7bits_to_latin1[128] = {
'@', 0xa3, '$', 0xa5, 0xe8, 0xe9, 0xf9, 0xec, 0xf2, 0xc7, '\n', 0xd8, 0xf8, '\r', 0xc5, 0xe5,
0, '_', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xc6, 0xe6, 0xdf, 0xc9,
' ', '!', '"', '#', 0xa4, '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
0xa1, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0xc4, 0xd6, 0xd1, 0xdc, 0xa7,
0xbf, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0xe4, 0xf6, 0xf1, 0xfc, 0xe0,
};
static const unsigned char gsm7bits_extend_to_latin1[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\f', 0, 0, 0, 0, 0,
0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0,'\\',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0,
'|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static int
G7bitToAscii(char* buffer, int buffer_length)
{
int i;
for (i = 0; i<buffer_length; i++) {
if (buffer[i] < 128) {
if (buffer[i] == GSM_7BITS_ESCAPE) {
buffer[i] = gsm7bits_extend_to_latin1[buffer[i + 1]];
memmove(&buffer[i + 1], &buffer[i + 2], buffer_length - i - 1);
buffer_length--;
} else {
buffer[i] = gsm7bits_to_latin1[buffer[i]];
}
}
}
return buffer_length;
}
#define NPC '?'
static const int latin1_to_gsm7bits[256] = {
NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, 0x0a, NPC,-0x0a, 0x0d, NPC, NPC,
NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC,
0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,-0x3c,-0x2f,-0x3e,-0x14, 0x11,
NPC, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,-0x28,-0x40,-0x29,-0x3d, NPC,
NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC,
NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC,
NPC, 0x40, NPC, 0x01, 0x24, 0x03, NPC, 0x5f, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC,
NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, NPC, 0x60,
NPC, NPC, NPC, NPC, 0x5b, 0x0e, 0x1c, 0x09, NPC, 0x1f, NPC, NPC, NPC, NPC, NPC, NPC,
NPC, 0x5d, NPC, NPC, NPC, NPC, 0x5c, NPC, 0x0b, NPC, NPC, NPC, 0x5e, NPC, NPC, 0x1e,
0x7f, NPC, NPC, NPC, 0x7b, 0x0f, 0x1d, NPC, 0x04, 0x05, NPC, NPC, 0x07, NPC, NPC, NPC,
NPC, 0x7d, 0x08, NPC, NPC, NPC, 0x7c, NPC, 0x0c, 0x06, NPC, NPC, 0x7e, NPC, NPC, NPC,
};
static int
AsciiToG7bit(const char* buffer, int buffer_length, unsigned char* output_buffer)
{
int i, j, val;
j=0;
for (i = 0; i < buffer_length; i++) {
val = latin1_to_gsm7bits[buffer[i] & 0xFF];
if (val < 0) {
output_buffer[j++] = GSM_7BITS_ESCAPE;
output_buffer[j++] = -1*val;
} else {
if (((buffer[i] & 0xFF) & 0xE0) == 0xC0) { /* test for two byte utf8 char */
val = NPC;
i++;
} else if (((buffer[i] & 0xFF) & 0xF0) == 0xE0) { /* test for three byte utf8 char */
val = NPC;
i++;
i++;
}
output_buffer[j++] = val;
}
}
return j;
}
// Encode a digit based phone number for SMS based format.
static int
EncodePhoneNumber(const char* phone_number, unsigned char* output_buffer, int buffer_size)
{
int output_buffer_length = 0;
const int phone_number_length = strlen(phone_number);
// Check if the output buffer is big enough.
if ((phone_number_length + 1) / 2 > buffer_size)
return -1;
int i = 0;
for (; i < phone_number_length; ++i) {
if (phone_number[i] < '0' && phone_number[i] > '9')
return -1;
if (i % 2 == 0) {
output_buffer[output_buffer_length++] = BITMASK_HIGH_4BITS | (phone_number[i] - '0');
} else {
output_buffer[output_buffer_length - 1] =
(output_buffer[output_buffer_length - 1] & BITMASK_LOW_4BITS) |
((phone_number[i] - '0') << 4);
}
}
return output_buffer_length;
}
// Decode a digit based phone number for SMS based format.
static int
DecodePhoneNumber(const unsigned char* buffer, int phone_number_length, char* output_phone_number)
{
int i = 0;
for (; i < phone_number_length; ++i) {
if (i % 2 == 0)
output_phone_number[i] = (buffer[i / 2] & BITMASK_LOW_4BITS) + '0';
else
output_phone_number[i] = ((buffer[i / 2] & BITMASK_HIGH_4BITS) >> 4) + '0';
}
output_phone_number[phone_number_length] = '\0'; // Terminate C string.
return phone_number_length;
}
// Encode a SMS message to PDU
int
pdu_encode(const char* service_center_number, const char* phone_number, const char* sms_text,
unsigned char* output_buffer, int buffer_size)
{
if (buffer_size < 2)
return -1;
int output_buffer_length = 0;
// 1. Set SMS center number.
int length = 0;
if (service_center_number && strlen(service_center_number) > 0) {
output_buffer[1] = TYPE_OF_ADDRESS_INTERNATIONAL_PHONE;
length = EncodePhoneNumber(service_center_number,
output_buffer + 2, buffer_size - 2);
if (length < 0 && length >= 254)
return -1;
length++; // Add type of address.
}
output_buffer[0] = length;
output_buffer_length = length + 1;
if (output_buffer_length + 4 > buffer_size)
return -1; // Check if it has space for four more bytes.
// 2. Set type of message.
output_buffer[output_buffer_length++] = SMS_SUBMIT;
output_buffer[output_buffer_length++] = 0x00; // Message reference.
// 3. Set phone number.
output_buffer[output_buffer_length] = strlen(phone_number);
if (strlen(phone_number) < 6) {
output_buffer[output_buffer_length + 1] = TYPE_OF_ADDRESS_UNKNOWN;
} else {
output_buffer[output_buffer_length + 1] = TYPE_OF_ADDRESS_INTERNATIONAL_PHONE;
}
length = EncodePhoneNumber(phone_number,
output_buffer + output_buffer_length + 2,
buffer_size - output_buffer_length - 2);
output_buffer_length += length + 2;
if (output_buffer_length + 4 > buffer_size)
return -1; // Check if it has space for four more bytes.
// 4. Protocol identifiers.
output_buffer[output_buffer_length++] = 0x00; // TP-PID: Protocol identifier.
output_buffer[output_buffer_length++] = 0x00; // TP-DCS: Data coding scheme.
output_buffer[output_buffer_length++] = 0xB0; // TP-VP: Validity: 10 days
// 5. SMS message.
int sms_text_length = strlen(sms_text);
char sms_text_7bit[2*SMS_MAX_7BIT_TEXT_LENGTH];
sms_text_length = AsciiToG7bit(sms_text, sms_text_length, sms_text_7bit);
if (sms_text_length > SMS_MAX_7BIT_TEXT_LENGTH)
return -1;
output_buffer[output_buffer_length++] = sms_text_length;
length = EncodePDUMessage(sms_text_7bit, sms_text_length,
output_buffer + output_buffer_length,
buffer_size - output_buffer_length);
if (length < 0)
return -1;
output_buffer_length += length;
return output_buffer_length;
}
int pdu_decode(const unsigned char* buffer, int buffer_length,
time_t* output_sms_time,
char* output_sender_phone_number, int sender_phone_number_size,
char* output_sms_text, int sms_text_size,
int* tp_dcs,
int* ref_number,
int* total_parts,
int* part_number,
int* skip_bytes)
{
if (buffer_length <= 0)
return -1;
const int sms_deliver_start = 1 + buffer[0];
if (sms_deliver_start + 1 > buffer_length)
return -2;
const int user_data_header_length = (buffer[sms_deliver_start]>>4);
const int sender_number_length = buffer[sms_deliver_start + 1];
if (sender_number_length + 1 > sender_phone_number_size)
return -3; // Buffer too small to hold decoded phone number.
const int sender_type_of_address = buffer[sms_deliver_start + 2];
if (sender_type_of_address == TYPE_OF_ADDRESS_ALPHANUMERIC) {
DecodePDUMessage_GSM_7bit(buffer + sms_deliver_start + 3, (sender_number_length + 1) / 2, output_sender_phone_number, sender_number_length);
} else {
DecodePhoneNumber(buffer + sms_deliver_start + 3, sender_number_length, output_sender_phone_number);
}
const int sms_pid_start = sms_deliver_start + 3 + (buffer[sms_deliver_start + 1] + 1) / 2;
// Decode timestamp.
struct tm sms_broken_time;
sms_broken_time.tm_year = 100 + SwapDecimalNibble(buffer[sms_pid_start + 2]);
sms_broken_time.tm_mon = SwapDecimalNibble(buffer[sms_pid_start + 3]) - 1;
sms_broken_time.tm_mday = SwapDecimalNibble(buffer[sms_pid_start + 4]);
sms_broken_time.tm_hour = SwapDecimalNibble(buffer[sms_pid_start + 5]);
sms_broken_time.tm_min = SwapDecimalNibble(buffer[sms_pid_start + 6]);
sms_broken_time.tm_sec = SwapDecimalNibble(buffer[sms_pid_start + 7]);
(*output_sms_time) = timegm(&sms_broken_time);
const int sms_start = sms_pid_start + 2 + 7;
if (sms_start + 1 > buffer_length) return -1; // Invalid input buffer.
int tmp;
if((user_data_header_length&0x04)==0x04) {
tmp = buffer[sms_start + 1] + 1;
*skip_bytes = tmp;
*ref_number = 0x000000FF&buffer[sms_start + tmp - 2];
*total_parts = 0x000000FF&buffer[sms_start + tmp - 1];
*part_number = 0x000000FF&buffer[sms_start + tmp];
} else {
tmp = 0;
*skip_bytes = tmp;
*ref_number = tmp;
*total_parts = tmp;
*part_number = tmp;
}
int output_sms_text_length = buffer[sms_start];
if (sms_text_size < output_sms_text_length) return -1; // Cannot hold decoded buffer.
const int sms_tp_dcs_start = sms_pid_start + 1;
*tp_dcs = buffer[sms_tp_dcs_start];
switch((*tp_dcs / 4) % 4)
{
case 0:
{
// GSM 7 bit
int decoded_sms_text_size = DecodePDUMessage_GSM_7bit(buffer + sms_start + 1, buffer_length - (sms_start + 1),
output_sms_text, output_sms_text_length);
if (decoded_sms_text_size != output_sms_text_length) return -1; // Decoder length is not as expected.
output_sms_text_length = G7bitToAscii(output_sms_text, output_sms_text_length);
break;
}
case 2:
{
// UCS2
memcpy(output_sms_text, buffer + sms_start + 1, output_sms_text_length);
break;
}
default:
break;
}
// Add a C string end.
if (output_sms_text_length < sms_text_size)
output_sms_text[output_sms_text_length] = 0;
else
output_sms_text[sms_text_size-1] = 0;
return output_sms_text_length;
}
+52
View File
@@ -0,0 +1,52 @@
/*
* 2017 - 2021 Cezary Jackiewicz <cezary@eko.one.pl>
* 2014 lovewilliam <ztong@vt.edu>
*/
// Copyright 2011 The Avalon Project Authors. All rights reserved.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the LICENSE file.
#ifndef SMS_PDU_H_
#define SMS_PDU_H_
#include <time.h>
enum { SMS_MAX_PDU_LENGTH = 256 };
/*
* Encode an SMS message. Output the encoded message into output pdu buffer.
* Returns the length of the SMS encoded message in the output buffer or
* a negative number in case encoding failed (for example provided output buffer
* does not have enough space).
*/
int pdu_encode(const char* service_center_number, const char* phone_number, const char* text,
unsigned char* pdu, int pdu_size);
/*
* Decode an SMS message. Output the decoded message into the sms text buffer.
* Returns the length of the SMS dencoded message or a negative number in
* case encoding failed (for example provided output buffer has not enough
* space).
*/
int pdu_decode(const unsigned char* pdu, int pdu_len,
time_t* sms_time,
char* phone_number, int phone_number_size,
char* text, int text_size,
int* tp_dcs,
int* ref_number,
int* total_parts,
int* part_number,
int* skip_bytes);
int ucs2_to_utf8 (int ucs2, unsigned char * utf8);
int DecodePDUMessage_GSM_7bit(const unsigned char* buffer,
int buffer_length,
char* output_sms_text,
int sms_text_length);
int EncodePDUMessage(const char* sms_text,
int sms_text_length,
unsigned char* output_buffer,
int buffer_size);
#endif // SMS_SMS_H_
+107
View File
@@ -0,0 +1,107 @@
/*
* 2014 lovewilliam <ztong@vt.edu>
* SMS PDU Decoder
*/
#include "pdu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
int ucs2_to_utf8 (int ucs2, unsigned char * utf8);
int sms_decode()
{
char buffer[2*SMS_MAX_PDU_LENGTH+4];
char *p = buffer;
char t[2];
int d;
do
{
t[0] = getchar();
if(t[0]=='\n')
{
break;
}
t[1] = getchar();
if(t[1]=='\n')
{
break;
}
*p = strtol(t,NULL,16);
p++;
}while(1);
time_t sms_time;
char sms_phone[40];
char sms_text[161];
int tp_dcs_type;
int ref_number;
int total_parts;
int part_number;
int skip_bytes;
int sms_text_length = pdu_decode((const unsigned char*)buffer,
sizeof(buffer),
&sms_time,
sms_phone, sizeof(sms_phone),
sms_text, sizeof(sms_text),
&tp_dcs_type,
&ref_number,
&total_parts,
&part_number,
&skip_bytes);
printf("From:%s\n",sms_phone);
printf("Textlen=%d\n",sms_text_length);
char time_data_str[64];
strftime(time_data_str,64,"%D %T", localtime(&sms_time));
printf("Date/Time:%s\n",time_data_str);
if (total_parts > 0) {
printf("Reference number: %d\n", ref_number);
printf("SMS segment %d of %d\n", part_number, total_parts);
}
switch((tp_dcs_type / 4) % 4)
{
case 0:
{
// GSM7 bit
int i = skip_bytes;
if(skip_bytes > 0) i = (skip_bytes*8+6)/7;
for(;i<sms_text_length;i++)
{
printf("%c", sms_text[i]);
}
break;
}
case 2:
{
// UCS2
for(int i = skip_bytes;i<sms_text_length;i+=2)
{
int ucs2_char = 0x000000FF&sms_text[i+1];
ucs2_char|=(0x0000FF00&(sms_text[i]<<8));
unsigned char utf8_char[5];
int len = ucs2_to_utf8(ucs2_char,utf8_char);
int j;
for(j=0;j<len;j++)
{
printf("%c",utf8_char[j]);
}
}
break;
}
default:
break;
}
printf("\n");
return 0;
}
+450
View File
@@ -0,0 +1,450 @@
/*
** Soft:Daocaoren Name:马上有房 qq:168620188
*/
#ifndef _UART_H_
#define _UART_H_
#include <stdio.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/select.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdlib.h>
// #include "pdu.h"
#define MAX_PORTS 4
/*this array hold information about each port we have opened */
struct PortInfo {
char name[16];
int port_fd;
};
int serial_init(int port, int spd, int databits, int parity, \
int stopbits, int RTSCTS, int need_line_input);
int serial_write(int fd, void *src, int len);
int serial_read(int fd, char *buf, int len);
int serial_recv(int fd,char *rbuf,int rbuf_len, int timeout);
//串口常用初始化接口
#define serial_com_init(port, spd, databits, parity, stopbits)\
serial_init(port, spd, databits, parity, stopbits, 0, 0)
//串口默认初始化接口
#define serial_def_init(port, spd) serial_init(port, spd, 8, 'n', 1, 0, 1)
#endif
/*
** File: uart.c
**
** Description:
** Provides an RS-232 interface that is very similar to the CVI provided
** interface library
*/
/*this array hold information about each port we have opened */
struct PortInfo ports[13] =
{
{"/dev/ttyUSB0", 0},
{"/dev/ttyUSB1", 0},
{"/dev/ttyUSB2", 0},
{"/dev/ttyUSB3", 0},
{"/dev/ttyUSB4", 0},
{"/dev/ttyUSB5", 0},
{"/dev/ttyUSB6", 0},
{"/dev/ttyUSB7", 0},
{"/dev/ttyUSB8", 0},
{"/dev/ttyUSB9", 0},
{"/dev/ttyUSB10", 0},
{"/dev/ttyUSB11", 0},
{"/dev/ttyUSB12", 0},
};
FILE* pf;
FILE* pfi;
int spd_arr[] = {B2000000, B1500000, B576000, B500000, B460800, B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400};
int name_arr[] = { 2000000, 1500000, 576000, 500000, 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400 };
/**
*@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void
*/
int set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(spd_arr) / sizeof(int); i++) {
if(speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, spd_arr[i]);
cfsetospeed(&Opt, spd_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
printf("tcsetattr failed");
return 1;
}
tcflush(fd,TCIOFLUSH);
}
}
// printf("set_speed\n");
return 0;
}
/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param databits 类型 int 数据位 取值为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd, int databits, int parity, int stopbits, int RTSCTS)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return -1;
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return -1;
}
options.c_iflag |= INPCK;
cfmakeraw(&options);
//options.c_lflag |= (ICANON | ECHO | ECHOE);
//options.c_lflag &= ~(ICANON | ECHO | ECHOE);
//options.c_iflag &= ~(IXON | IXOFF);
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return -1;
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return -1;
}
/* Set rts/cts */
if (RTSCTS)
{
printf("Set rts/cts");
options.c_cflag |= CRTSCTS;
}
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
printf("SetupSerial failed");
return -1;
}
// printf("set_Parity\n");
return 0;
}
//接收消息结尾加换行符
int serial_set_line_input(int fd)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return -1;
}
options.c_lflag |= ICANON;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return -1;
}
return 0;
}
/**
*@brief
*@param port 串口端口号
*@param spd 串口速度
*@param databits,parity,stopbits,RTSCTS,分别为数据位,校验位,停止位,rtscts位
*@param need_line_input接收数据结尾是否加换行符?
*/
int serial_init(int port, int spd, int databits, int parity, int stopbits, int RTSCTS, int need_line_input)
{
int fd;
if(port < 13)
{
// printf("open port:%d\n", port);
}
else {
printf("error: port:%d > MAX_PORTS\n", port);
return -1;
}
//
fd = open(ports[port].name, O_RDWR|O_NOCTTY);//O_NONBLOCK 非阻塞, O_WRONLY 只读写, O_RDONLY 只读, O_RDWR 读写,O_NOCTTY 阻塞
if (-1 == fd) {
printf("init %s failed\n", ports[port].name);
return -1;
}
pf = fdopen(port, "w");
pfi = fdopen(port, "r");
if (!pf || ! pfi)
fprintf(stderr,"open port failed\n");
set_speed(fd, spd);
set_Parity(fd, databits, parity, stopbits, RTSCTS);
if (need_line_input) {
serial_set_line_input(fd);
}
ports[port].port_fd = fd;
return fd;
}
/**
*@brief
*@param fd 串口端口号文件描述符
*@param src 需要通过串口发送的数据
*@param len 需要发送的数据长度
*@param 成功返回0, 否则返回-1
*/
int serial_write(int fd, void *src, int len)
{
int ret = write(fd, src, len);
if (len != ret) {
perror("oh, write serial failed!");
return -1;
}
return 0;
}
/**
*@brief
*@param fd 串口端口号文件描述符
*@param src 串口接收数据的指针
*@param len 需要接收的数据长度
*@param 成功返回0, 否则返回-1
*/
int serial_read(int fd, char *buf, int len)
{
int ret = read(fd, buf, len-1);
if (-1 == ret) {
perror("oh, read serial failed!");
return -1;
}
buf[ret] = '\0';
return ret;
}
int serial_recv(int fd,char *rbuf,int rbuf_len, int timeout)
{
int retval;
fd_set rset;
struct timeval time_out;
if(!rbuf || rbuf_len <= 0)
{
// printf("serial_recv Invalid parameter.\n");
return -1;
}
if(timeout) //指定延时等待
{
time_out.tv_sec = (time_t)(timeout / 1000);
time_out.tv_usec = 0;
FD_ZERO(&rset);
FD_SET(fd,&rset);
retval = select(fd,&rset,NULL,NULL,&time_out);
if(retval < 0)
{
// printf("%s,Select failed:%s\n",strerror(errno));
return -2;
}
else if(0 == retval)
{
// printf("Time Out.\n");
return 0;
}
}
// usleep(1000);
retval = read(fd, rbuf, rbuf_len);
if( retval <= 0)
{
// printf("Read failed:%s\n",strerror(errno));
return -3;
}
return retval;
}
static void timeout()
{
fprintf(stderr,"No response from modem.\n");
exit(2);
}
/*字符包含判断*/
static int starts_with(const char* prefix, const char* str)
{
while(*prefix)
{
if (*prefix++ != *str++)
{
return 0;
}
}
return 1;
}
/*判断是否存在*/
int FileExist(const char* filename)
{
if (filename && access(filename, F_OK) == 0) {
return 1;
}
return 0;
}
/*字符转小写*/
char* str_tolower(const char* str)
{
size_t len = strlen(str);
char *lower = calloc(len+1, sizeof(char));
for (size_t i = 0; i < len; ++i) {
lower[i] = tolower((unsigned char)str[i]);
}
/*free(upper);*/
return lower;
}
/*字符转大写*/
char* str_toupper(const char* str)
{
size_t len = strlen(str);
char *upper = calloc(len+1, sizeof(char));
for (size_t i = 0; i < len; ++i) {
upper[i] = toupper((unsigned char)str[i]);
}
/*free(upper);*/
return upper;
}
/*char*转char[]*/
char strx_tostrarr(const char* str)
{
return 0;
}
int main(int argc, char **argv)
{
if(argc<3)
{
printf("ERROR demo: sendat 2 'ATI'\n");
exit(1);
return 0;
}
int debug = 1;
int port= 0;
sscanf(argv[1], "%d", &port);
if(FileExist(ports[port].name)==0)
{
printf("AT ERROR absent.\n");
return 0;
}
char *message= argv[2];
char *nty= "\r\n";
char buff[1024];
signal(SIGALRM,timeout);
alarm(2);
/*信号超时3秒自动退出*/
int fd = serial_def_init(port, 1500000);
if(fd < 0) return 0;
char *send= strcat(message,nty);
serial_write(fd,send, strlen(send));
while(1) {
int read = serial_read(fd, buff, sizeof(buff));
if(starts_with("OK", buff)) {
if (debug == 1)
printf("%s", buff);
close(fd);//关闭串口
exit(0);
}
if(starts_with("ERROR", buff)) {
if (debug == 1)
printf("%s", buff);
close(fd);//关闭串口
exit(1);
}
if(starts_with("COMMAND NOT SUPPORT", buff)) {
if (debug == 1)
printf("%s", buff);
close(fd);//关闭串口
exit(1);
}
if(starts_with("+CME ERROR", buff)) {
if (debug == 1)
printf("%s", buff);
close(fd);//关闭串口
exit(1);
}
printf("%s", buff);
}
exit(1);
return 0;
}
+53
View File
@@ -0,0 +1,53 @@
/*
* 2014 lovewilliam <ztong@vt.edu>
* from http://www.lemoda.net/c/ucs2-to-utf8/ucs2-to-utf8.c
*/
/* Input: a Unicode code point, "ucs2".
Output: UTF-8 characters in buffer "utf8".
Return value: the number of bytes written into "utf8", or -1 if
there was an error.
This adds a zero byte to the end of the string. It assumes that the
buffer "utf8" has at least four bytes of space to write to. */
#define UNICODE_SURROGATE_PAIR -2
#define UNICODE_BAD_INPUT -1
int ucs2_to_utf8 (int ucs2, unsigned char * utf8)
{
if (ucs2 < 0x80) {
utf8[0] = ucs2;
utf8[1] = '\0';
return 1;
}
if (ucs2 >= 0x80 && ucs2 < 0x800) {
utf8[0] = (ucs2 >> 6) | 0xC0;
utf8[1] = (ucs2 & 0x3F) | 0x80;
utf8[2] = '\0';
return 2;
}
if (ucs2 >= 0x800 && ucs2 < 0xFFFF) {
if (ucs2 >= 0xD800 && ucs2 <= 0xDFFF) {
/* Ill-formed. */
return UNICODE_SURROGATE_PAIR;
}
utf8[0] = ((ucs2 >> 12) ) | 0xE0;
utf8[1] = ((ucs2 >> 6 ) & 0x3F) | 0x80;
utf8[2] = ((ucs2 ) & 0x3F) | 0x80;
utf8[3] = '\0';
return 3;
}
if (ucs2 >= 0x10000 && ucs2 < 0x10FFFF) {
/* http://tidy.sourceforge.net/cgi-bin/lxr/source/src/utf8.c#L380 */
utf8[0] = 0xF0 | (ucs2 >> 18);
utf8[1] = 0x80 | ((ucs2 >> 12) & 0x3F);
utf8[2] = 0x80 | ((ucs2 >> 6) & 0x3F);
utf8[3] = 0x80 | ((ucs2 & 0x3F));
utf8[4] = '\0';
return 4;
}
return UNICODE_BAD_INPUT;
}