| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | #include <stdio.h> /* Standard input/output definitions */
|
| 2 | #include <stdbool.h> /* Standard bool true/false definitions */
|
| 3 | #include <string.h> /* String function definitions */
|
| 4 | #include <unistd.h> /* UNIX standard function definitions */
|
| 5 | #include <fcntl.h> /* File control definitions */
|
| 6 | #include <errno.h> /* Error number definitions */
|
| 7 | #include <termios.h> /* POSIX terminal control definitions */
|
| 8 | #include <time.h>
|
| 9 | #include <pthread.h>
|
| 10 | #include <stdlib.h>
|
| 11 | #include <signal.h>
|
| 12 | #include <netdb.h>
|
| 13 | #include <sys/types.h>
|
| 14 | #include <sys/wait.h>
|
| 15 | #include <sys/socket.h>
|
| 16 | #include <sys/epoll.h>
|
| 17 | #include <netinet/in.h>
|
| 18 | #include <arpa/inet.h>
|
| 19 |
|
| 20 | #include <strings.h>
|
| 21 | #include <semaphore.h>
|
| 22 |
|
| 23 | #include "gpslog_log.h"
|
| 24 | #include "gpslog_utility.h"
|
| 25 | #include "gpslog_socket_utils.h"
|
| 26 | #include "gpslog_socket_data_coder.h"
|
| 27 |
|
| 28 | #define GPSLOG_UDP_ADDR "mtk_lbs_log_v2s"
|
| 29 |
|
| 30 | #define PRINT_TITLE(...) { printf("\E[0;30;43m"); printf(__VA_ARGS__); printf("\E[0m"); fflush(stdout); }
|
| 31 | #define PRINT_RAW(...) { printf(__VA_ARGS__); fflush(stdout);}
|
| 32 |
|
| 33 | #define GPSLOG_INTERFACE_BUFF_SIZE (80*1024)
|
| 34 | #define GPSLOG_PATH_SIZE (256)
|
| 35 |
|
| 36 | typedef enum {
|
| 37 | GPSLOG_OPEN_LOG = 0,
|
| 38 | GPSLOG_CLOSE_LOG = 1,
|
| 39 | GPSLOG_WRITE_LOG = 2,
|
| 40 | } gpslog_msg_id;
|
| 41 |
|
| 42 | typedef struct {
|
| 43 | char* sock_addr;
|
| 44 | }log_db;
|
| 45 |
|
| 46 | log_db gpslog_db;
|
| 47 |
|
| 48 | typedef struct {
|
| 49 | bool (*create_logfile)(const char* path);
|
| 50 | bool (*write_logdata)(char* data, int len);
|
| 51 | bool (*close_logfile)();
|
| 52 | } gpslog_inf;
|
| 53 |
|
| 54 | static bool gpslog_handle_open_logfile(const char* file_name) {
|
| 55 | time_t tm;
|
| 56 | struct tm *p = NULL;
|
| 57 | char full_file_name[256] = {0};
|
| 58 |
|
| 59 | if (time(&tm)==((time_t)-1)) {
|
| 60 | LOGE("time() fail(%s)!!\r\n", strerror(errno));
|
| 61 | }
|
| 62 | p = localtime(&tm);
|
| 63 |
|
| 64 | if(p == NULL) {
|
| 65 | LOGE("Get localtime fail:[%s]%d", strerror(errno), errno);
|
| 66 | return false;
|
| 67 | }
|
| 68 |
|
| 69 | snprintf(full_file_name, sizeof(full_file_name), "%s_%04d_%02d%02d_%02d%02d%02d", file_name,
|
| 70 | 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday,
|
| 71 | p->tm_hour, p->tm_min, p->tm_sec);
|
| 72 |
|
| 73 | PRINT_RAW("\r\n==============GPSLog start, %s=============\r\n", full_file_name);
|
| 74 | return true;
|
| 75 | }
|
| 76 |
|
| 77 | static bool gpslog_handle_write_logdata(char* data, int len) {
|
| 78 | //PRINT_RAW("%s", data);
|
| 79 | if(write(STDOUT_FILENO, data, len) != len) {
|
| 80 | LOGW("STDOUT print fail:[le:%d][%s]", len, data);
|
| 81 | return false;
|
| 82 | }
|
| 83 | return true;
|
| 84 | }
|
| 85 |
|
| 86 | static bool gpslog_handle_close_logfile() {
|
| 87 | PRINT_RAW("\r\n==============GPSLog end=============\r\n");
|
| 88 | return true;
|
| 89 | }
|
| 90 |
|
| 91 | static gpslog_inf gpslog_handler_interface = {
|
| 92 | gpslog_handle_open_logfile,
|
| 93 | gpslog_handle_write_logdata,
|
| 94 | gpslog_handle_close_logfile
|
| 95 | };
|
| 96 |
|
| 97 | char gpslog_buff[GPSLOG_INTERFACE_BUFF_SIZE] = {0};
|
| 98 |
|
| 99 | static bool gpslog_hdlr(int fd, log_db* database, gpslog_inf* hdlr) {
|
| 100 | int offset = 0;
|
| 101 |
|
| 102 | gpslog_msg_id cmd;
|
| 103 | int ptype;
|
| 104 | int log_type;
|
| 105 | int read_len = 0;
|
| 106 | bool ret = true;
|
| 107 |
|
| 108 | if (NULL == database) {
|
| 109 | LOGE("gpslog_hdlr, database not valid");
|
| 110 | return false;
|
| 111 | }
|
| 112 |
|
| 113 | if (NULL == hdlr) {
|
| 114 | LOGE("[%s]gpslog_hdlr, hdlr not valid", database->sock_addr);
|
| 115 | return false;
|
| 116 | }
|
| 117 |
|
| 118 | if (fd < 0) {
|
| 119 | LOGE("[%s]gpslog_hdlr, fd not valid", database->sock_addr);
|
| 120 | return false;
|
| 121 | }
|
| 122 | memset(gpslog_buff, 0, sizeof(gpslog_buff));
|
| 123 | read_len = gpslog_safe_recv(fd, gpslog_buff, sizeof(gpslog_buff)-1);
|
| 124 | if (read_len <= 0) {
|
| 125 | LOGE("[%s]gpslog_hdlr() gpslog_safe_recv() failed read_len=%d", database->sock_addr, read_len);
|
| 126 | return false;
|
| 127 | }
|
| 128 | ptype = gpslog_socket_get_int(gpslog_buff, &offset);
|
| 129 | UNUSED(ptype);
|
| 130 | cmd = gpslog_socket_get_int(gpslog_buff, &offset);
|
| 131 | log_type = gpslog_socket_get_int(gpslog_buff, &offset);
|
| 132 | UNUSED(log_type);
|
| 133 |
|
| 134 | switch (cmd) {
|
| 135 | case GPSLOG_OPEN_LOG: {
|
| 136 | if (hdlr->create_logfile) {
|
| 137 | char path[GPSLOG_PATH_SIZE] = {0};
|
| 138 | //gpslog_socket_get_string(gpslog_buff, &offset, path, sizeof(path));
|
| 139 | sprintf(path, "GPSHostLog");
|
| 140 | if (!hdlr->create_logfile(path)) {
|
| 141 | LOGE("[%s]gpslog_hdlr() create_logfile fail", database->sock_addr);
|
| 142 | }
|
| 143 | } else {
|
| 144 | LOGE("[%s]gpslog_hdlr() create_logfile is NULL", database->sock_addr);
|
| 145 | ret = false;
|
| 146 | }
|
| 147 | break;
|
| 148 | }
|
| 149 |
|
| 150 | case GPSLOG_WRITE_LOG: {
|
| 151 | if (hdlr->write_logdata) {
|
| 152 | unsigned int len_logdata = gpslog_socket_get_int(gpslog_buff, &offset);
|
| 153 | if (len_logdata <= GPSLOG_INTERFACE_BUFF_SIZE) {
|
| 154 | if (!hdlr->write_logdata(gpslog_buff+offset, len_logdata)) {
|
| 155 | LOGE("[%s]gpslog_hdlr() write_logdata fail", database->sock_addr);
|
| 156 | }
|
| 157 | } else {
|
| 158 | LOGE("[%s]gpslog_hdlr() len_logdata:%u overflow", database->sock_addr, len_logdata);
|
| 159 | ret = false;
|
| 160 | }
|
| 161 | } else {
|
| 162 | LOGE("[%s]gpslog_hdlr() write_logdata is NULL", database->sock_addr);
|
| 163 | ret = false;
|
| 164 | }
|
| 165 | break;
|
| 166 | }
|
| 167 |
|
| 168 | case GPSLOG_CLOSE_LOG: {
|
| 169 | if (hdlr->close_logfile) {
|
| 170 | if (!hdlr->close_logfile()) {
|
| 171 | LOGE("[%s]gpslog_hdlr() close_logfile fail", database->sock_addr);
|
| 172 | }
|
| 173 | } else {
|
| 174 | LOGE("[%s]gpslog_hdlr() close_logfile is NULL", database->sock_addr);
|
| 175 | ret = false;
|
| 176 | }
|
| 177 | break;
|
| 178 | }
|
| 179 |
|
| 180 | default: {
|
| 181 | LOGE("[%s]gpslog_hdlr() unknown cmd=%d", database->sock_addr, cmd);
|
| 182 | ret = false;
|
| 183 | break;
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | return ret;
|
| 188 | }
|
| 189 |
|
| 190 | static void* gpslog_write_thread(void *database)
|
| 191 | {
|
| 192 | #define EPOLL_MAX_NUM 4
|
| 193 | int fd = -1;
|
| 194 | int i = 0;
|
| 195 | int n = 0;
|
| 196 | int epfd = epoll_create(EPOLL_MAX_NUM);
|
| 197 | struct epoll_event events[EPOLL_MAX_NUM];
|
| 198 | log_db* data = (log_db*)database;
|
| 199 |
|
| 200 | if (NULL == data) {
|
| 201 | LOGE("gpslog_write_thread exit, database not valid");
|
| 202 | return 0;
|
| 203 | }
|
| 204 |
|
| 205 | LOGD("[%s]gpslog_write_thread created", data->sock_addr);
|
| 206 |
|
| 207 | if (-1 == epfd) {
|
| 208 | LOGE("[%s]gpslog_write_thread epoll_create failure, reason=[%s]", data->sock_addr, strerror(errno));
|
| 209 | return 0;
|
| 210 | }
|
| 211 |
|
| 212 | //fd = gpslog_socket_bind_udp(data->sock_addr);
|
| 213 | fd = gpslog_socket_server_bind_local(data->sock_addr, SOCK_NS_ABSTRACT);
|
| 214 |
|
| 215 | if (fd < 0) {
|
| 216 | LOGE("[%s]gpslog_write_thread create fd failed, reason=[%s]", data->sock_addr, strerror(errno));
|
| 217 | return 0;
|
| 218 | }
|
| 219 |
|
| 220 | if (gpslog_epoll_add_fd(epfd, fd) == -1) {
|
| 221 | LOGE("[%s]gpslog_write_thread failed for fd_hal failed, reason=[%s]", data->sock_addr, strerror(errno));
|
| 222 | return 0;
|
| 223 | }
|
| 224 |
|
| 225 | while (1) {
|
| 226 | n = epoll_wait(epfd, events, EPOLL_MAX_NUM , -1);
|
| 227 | if (-1 == n) {
|
| 228 | if (errno == EINTR) {
|
| 229 | continue;
|
| 230 | } else {
|
| 231 | LOGE("[%s]gpslog_write_thread epoll_wait failure reason=[%s]", data->sock_addr, strerror(errno));
|
| 232 | return 0;
|
| 233 | }
|
| 234 | }
|
| 235 | for (i = 0; i < n; i++) {
|
| 236 | if (events[i].data.fd == fd) {
|
| 237 | if (events[i].events & EPOLLIN) {
|
| 238 | if (!gpslog_hdlr(fd, data, &gpslog_handler_interface)) {
|
| 239 | LOGE("[%s]gpslog_hdlr failure reason=[%s]", data->sock_addr, strerror(errno));
|
| 240 | }
|
| 241 | }
|
| 242 | } else {
|
| 243 | LOGE("[%s]mnld_main_thread() unknown fd=%d", data->sock_addr, events[i].data.fd);
|
| 244 | }
|
| 245 | }
|
| 246 | }
|
| 247 | LOGE("[%s]gpslog_write_thread exit", data->sock_addr);
|
| 248 | return 0;
|
| 249 | }
|
| 250 |
|
| 251 | static void gpslog_database_init() {
|
| 252 | memset(&gpslog_db, 0x0, sizeof(gpslog_db));
|
| 253 |
|
| 254 | //GPS LOG config
|
| 255 | gpslog_db.sock_addr = GPSLOG_UDP_ADDR;
|
| 256 | }
|
| 257 |
|
| 258 | int gpslog_init() {
|
| 259 | pthread_t pthread_gps;
|
| 260 |
|
| 261 | LOGD("gpslog_init");
|
| 262 | gpslog_database_init();
|
| 263 |
|
| 264 | pthread_create(&pthread_gps, NULL, gpslog_write_thread, (char*)(&gpslog_db));
|
| 265 | return 0;
|
| 266 | }
|
| 267 |
|
| 268 | int main(void) {
|
| 269 | LOGD("LBS debug daemon begin running");
|
| 270 | if (gpslog_init() != 0) {
|
| 271 | LOGE("gpslog_init error, exit");
|
| 272 | return -1;
|
| 273 | }
|
| 274 |
|
| 275 | gpslog_block_here();
|
| 276 | return 0;
|
| 277 | }
|