liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <termios.h> |
| 2 | #include <pthread.h> |
| 3 | #include <sys/un.h> |
| 4 | #include <stdio.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <errno.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <string.h> |
| 10 | #include <sys/epoll.h> |
| 11 | #include <netinet/in.h> |
| 12 | #include <signal.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #include "mbtk_log.h" |
| 16 | |
| 17 | #define MBTK_AT_SOCK "/tmp/atcmd_at" |
| 18 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 19 | typeof (exp) _rc; \ |
| 20 | do { \ |
| 21 | _rc = (exp); \ |
| 22 | } while (_rc == -1 && errno == EINTR); \ |
| 23 | _rc; }) |
| 24 | |
| 25 | static char *at_rsp_complete_tag[] = { |
| 26 | "OK", |
| 27 | "ERROR", |
| 28 | "CONNECT", |
| 29 | "+CMS ERROR:", |
| 30 | "+CME ERROR:", |
| 31 | "NO ANSWER", |
| 32 | "NO DIALTONE", |
| 33 | NULL}; |
| 34 | static int at_fd = -1; |
| 35 | |
| 36 | static int openSocket(const char* sockname) |
| 37 | { |
| 38 | int sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 39 | if (sock < 0) { |
| 40 | LOGE("Error create socket: %s\n", strerror(errno)); |
| 41 | return -1; |
| 42 | } |
| 43 | struct sockaddr_un addr; |
| 44 | memset(&addr, 0, sizeof(addr)); |
| 45 | addr.sun_family = AF_UNIX; |
| 46 | strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)); |
| 47 | while (TEMP_FAILURE_RETRY(connect(sock,(const struct sockaddr*)&addr, sizeof(addr))) != 0) { |
| 48 | LOGE("Error connect to socket %s: %s, try again", sockname, strerror(errno)); |
| 49 | sleep(1); |
| 50 | } |
| 51 | |
| 52 | #if 0 |
| 53 | int sk_flags = fcntl(sock, F_GETFL, 0); |
| 54 | fcntl(sock, F_SETFL, sk_flags | O_NONBLOCK); |
| 55 | #endif |
| 56 | |
| 57 | return sock; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static int at_complete(char *rsp) |
| 62 | { |
| 63 | #if 0 |
| 64 | char *ptr = at_rsp_complete_tag; |
| 65 | while(ptr) { |
| 66 | LOGD("ptr = %s", ptr); |
| 67 | if(strstr(rsp, ptr)) { |
| 68 | LOGD("%s , %s", rsp, ptr); |
| 69 | return 1; |
| 70 | } |
| 71 | ptr++; |
| 72 | } |
| 73 | #else |
| 74 | int i = 0; |
| 75 | while(at_rsp_complete_tag[i]) { |
| 76 | LOGD("ptr = %s", at_rsp_complete_tag[i]); |
| 77 | if(strstr(rsp, at_rsp_complete_tag[i])) { |
| 78 | LOGD("%s , %s", rsp, at_rsp_complete_tag[i]); |
| 79 | return 1; |
| 80 | } |
| 81 | i++; |
| 82 | } |
| 83 | |
| 84 | #endif |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int at_rsp_read(char* rsp, int rsp_len) |
| 89 | { |
| 90 | int len = 0; |
| 91 | int index = 0; |
| 92 | memset(rsp, 0x0, rsp_len); |
| 93 | while(1) { |
| 94 | if((len = read(at_fd, rsp + index, rsp_len - index)) > 0) { |
| 95 | if(at_complete(rsp)) { |
| 96 | LOGD("AT<%s", rsp); |
| 97 | return 0; |
| 98 | } else { |
| 99 | index += len; |
| 100 | |
| 101 | if(index >= rsp_len) { |
| 102 | LOGE("Buffer too small."); |
| 103 | return -1; |
| 104 | } |
| 105 | } |
| 106 | } else { |
| 107 | printf("Read error:%d\n",errno); |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /*=========================================================================== |
| 115 | FUNCTION mbtk_at_init |
| 116 | |
| 117 | DESCRIPTION: |
| 118 | Initial MBTK AT. |
| 119 | |
| 120 | PARAMETERS: |
| 121 | None. |
| 122 | |
| 123 | RETURN VALUE: |
| 124 | int : Return 0 if success,other for failure. |
| 125 | |
| 126 | ===========================================================================*/ |
| 127 | int mbtk_at_init() |
| 128 | { |
| 129 | if(at_fd > 0) { |
| 130 | LOGW("MBTK AT has inited."); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | at_fd = openSocket(MBTK_AT_SOCK); |
| 135 | return at_fd > 0 ? 0 : -1; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /*=========================================================================== |
| 140 | FUNCTION mbtk_at_deinit |
| 141 | |
| 142 | DESCRIPTION: |
| 143 | Deinitial MBTK AT. |
| 144 | |
| 145 | PARAMETERS: |
| 146 | None. |
| 147 | |
| 148 | RETURN VALUE: |
| 149 | int : Return 0 if success,other for failure. |
| 150 | |
| 151 | ===========================================================================*/ |
| 152 | int mbtk_at_deinit() |
| 153 | { |
| 154 | if(at_fd < 0) { |
| 155 | LOGW("MBTK AT not inited."); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | close(at_fd); |
| 160 | at_fd = -1; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /*=========================================================================== |
| 165 | FUNCTION mbtk_at_send |
| 166 | |
| 167 | DESCRIPTION: |
| 168 | Send AT command. |
| 169 | |
| 170 | PARAMETERS: |
| 171 | cmd [IN]: AT command. |
| 172 | rsp [OUT]: AT command response. |
| 173 | rsp_len[IN] : AT command response buffer size. |
| 174 | |
| 175 | RETURN VALUE: |
| 176 | int : Return 0 if success,other for failure. |
| 177 | |
| 178 | ===========================================================================*/ |
| 179 | int mbtk_at_send(char* cmd, char* rsp, int rsp_len) |
| 180 | { |
| 181 | if(cmd == NULL || strlen(cmd) == 0 || rsp == NULL || rsp_len <= 0) { |
| 182 | return -1; |
| 183 | } |
| 184 | char at_cmd[2048] = {0}; |
| 185 | memcpy(at_cmd, cmd, strlen(cmd)); |
| 186 | char *ptr = at_cmd + strlen(at_cmd) - 1; |
| 187 | while(ptr >= at_cmd && (*ptr == '\r' || *ptr == '\n')) |
| 188 | { |
| 189 | *ptr-- = '\0'; |
| 190 | } |
| 191 | if(!strncasecmp(at_cmd, "at", 2)) |
| 192 | { |
| 193 | LOGD("AT>%s", at_cmd); |
| 194 | *(++ptr) = '\r'; |
| 195 | *(++ptr) = '\n'; |
| 196 | if(write(at_fd, at_cmd, strlen(at_cmd)) != strlen(at_cmd)) { |
| 197 | LOGE("Write error:%d",errno); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | return at_rsp_read(rsp, rsp_len); |
| 202 | } else { |
| 203 | LOGE("AT command error:%s",at_cmd); |
| 204 | return -1; |
| 205 | } |
| 206 | } |
| 207 | |