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